iceoryx2_ffi/api/
static_config.rs

1// Copyright (c) 2024 Contributors to the Eclipse Foundation
2//
3// See the NOTICE file(s) distributed with this work for additional
4// information regarding copyright ownership.
5//
6// This program and the accompanying materials are made available under the
7// terms of the Apache Software License 2.0 which is available at
8// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
9// which is available at https://opensource.org/licenses/MIT.
10//
11// SPDX-License-Identifier: Apache-2.0 OR MIT
12
13#![allow(non_camel_case_types)]
14
15use core::ffi::c_char;
16
17use iceoryx2::service::static_config::messaging_pattern::MessagingPattern;
18use iceoryx2::service::static_config::StaticConfig;
19use iceoryx2_bb_log::fatal_panic;
20
21use crate::{
22    iox2_messaging_pattern_e, iox2_static_config_event_t, iox2_static_config_publish_subscribe_t,
23    IOX2_SERVICE_ID_LENGTH, IOX2_SERVICE_NAME_LENGTH,
24};
25
26#[derive(Clone, Copy)]
27#[repr(C)]
28pub union iox2_static_config_details_t {
29    pub event: iox2_static_config_event_t,
30    pub publish_subscribe: iox2_static_config_publish_subscribe_t,
31}
32
33#[derive(Clone, Copy)]
34#[repr(C)]
35pub struct iox2_static_config_t {
36    pub id: [c_char; IOX2_SERVICE_ID_LENGTH],
37    pub name: [c_char; IOX2_SERVICE_NAME_LENGTH],
38    pub messaging_pattern: iox2_messaging_pattern_e,
39    pub details: iox2_static_config_details_t,
40}
41
42impl From<&StaticConfig> for iox2_static_config_t {
43    fn from(value: &StaticConfig) -> Self {
44        Self {
45            id: core::array::from_fn(|n| {
46                let raw_service_id = value.service_id().as_str().as_bytes();
47                if n < raw_service_id.len() {
48                    raw_service_id[n] as _
49                } else {
50                    0
51                }
52            }),
53            name: core::array::from_fn(|n| {
54                debug_assert!(value.name().as_bytes().len() + 1 < IOX2_SERVICE_NAME_LENGTH);
55
56                if n < value.name().as_bytes().len() {
57                    value.name().as_bytes()[n] as _
58                } else {
59                    0
60                }
61            }),
62            messaging_pattern: value.messaging_pattern().into(),
63            details: {
64                match value.messaging_pattern() {
65                    MessagingPattern::Event(event) => iox2_static_config_details_t {
66                        event: event.into(),
67                    },
68                    MessagingPattern::PublishSubscribe(pubsub) => iox2_static_config_details_t {
69                        publish_subscribe: pubsub.into(),
70                    },
71                    _ => {
72                        fatal_panic!(from "StaticConfig", "missing implementation for messaging pattern.")
73                    }
74                }
75            },
76        }
77    }
78}