Skip to main content

zenoh_link_serial/
lib.rs

1//
2// Copyright (c) 2023 ZettaScale Technology
3//
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
7// which is available at https://www.apache.org/licenses/LICENSE-2.0.
8//
9// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
10//
11// Contributors:
12//   ZettaScale Zenoh Team, <zenoh@zettascale.tech>
13//
14
15//! ⚠️ WARNING ⚠️
16//!
17//! This crate is intended for Zenoh's internal use.
18//!
19//! [Click here for Zenoh's documentation](https://docs.rs/zenoh/latest/zenoh)
20mod unicast;
21
22use std::str::FromStr;
23
24use async_trait::async_trait;
25pub use unicast::*;
26use zenoh_core::zconfigurable;
27use zenoh_link_commons::LocatorInspector;
28use zenoh_protocol::{
29    core::{endpoint::Address, EndPoint, Locator, Metadata, Reliability},
30    transport::BatchSize,
31};
32use zenoh_result::ZResult;
33
34// Maximum MTU (Serial PDU) in bytes.
35const SERIAL_MAX_MTU: BatchSize = z_serial::MAX_MTU as BatchSize;
36
37const DEFAULT_BAUDRATE: u32 = 9_600;
38
39const DEFAULT_EXCLUSIVE: bool = true;
40
41const DEFAULT_TIMEOUT: u64 = 50_000;
42
43const DEFAULT_RELEASE_ON_CLOSE: bool = true;
44
45pub const SERIAL_LOCATOR_PREFIX: &str = "serial";
46
47const SERIAL_MTU_LIMIT: BatchSize = SERIAL_MAX_MTU;
48
49const IS_RELIABLE: bool = false;
50
51zconfigurable! {
52    // Default MTU (UDP PDU) in bytes.
53    static ref SERIAL_DEFAULT_MTU: BatchSize = SERIAL_MTU_LIMIT;
54    // Amount of time in microseconds to throttle the accept loop upon an error.
55    // Default set to 100 ms.
56    static ref SERIAL_ACCEPT_THROTTLE_TIME: u64 = 100_000;
57}
58
59#[derive(Default, Clone, Copy)]
60pub struct SerialLocatorInspector;
61#[async_trait]
62impl LocatorInspector for SerialLocatorInspector {
63    fn protocol(&self) -> &str {
64        SERIAL_LOCATOR_PREFIX
65    }
66
67    async fn is_multicast(&self, _locator: &Locator) -> ZResult<bool> {
68        Ok(false)
69    }
70
71    fn is_reliable(&self, locator: &Locator) -> ZResult<bool> {
72        if let Some(reliability) = locator
73            .metadata()
74            .get(Metadata::RELIABILITY)
75            .map(Reliability::from_str)
76            .transpose()?
77        {
78            Ok(reliability == Reliability::Reliable)
79        } else {
80            Ok(IS_RELIABLE)
81        }
82    }
83}
84
85pub fn get_baud_rate(endpoint: &EndPoint) -> u32 {
86    if let Some(baudrate) = endpoint.config().get(config::PORT_BAUD_RATE_RAW) {
87        u32::from_str(baudrate).unwrap_or(DEFAULT_BAUDRATE)
88    } else {
89        DEFAULT_BAUDRATE
90    }
91}
92
93pub fn get_exclusive(endpoint: &EndPoint) -> bool {
94    if let Some(exclusive) = endpoint.config().get(config::PORT_EXCLUSIVE_RAW) {
95        bool::from_str(exclusive).unwrap_or(DEFAULT_EXCLUSIVE)
96    } else {
97        DEFAULT_EXCLUSIVE
98    }
99}
100
101pub fn get_timeout(endpoint: &EndPoint) -> u64 {
102    if let Some(tout) = endpoint.config().get(config::TIMEOUT_RAW) {
103        u64::from_str(tout).unwrap_or(DEFAULT_TIMEOUT)
104    } else {
105        DEFAULT_TIMEOUT
106    }
107}
108
109pub fn get_release_on_close(endpoint: &EndPoint) -> bool {
110    if let Some(release_on_close) = endpoint.config().get(config::RELEASE_ON_CLOSE) {
111        bool::from_str(release_on_close).unwrap_or(DEFAULT_RELEASE_ON_CLOSE)
112    } else {
113        DEFAULT_RELEASE_ON_CLOSE
114    }
115}
116
117pub fn get_unix_path_as_string(address: Address<'_>) -> String {
118    address.as_str().to_owned()
119}
120
121pub mod config {
122    pub const PORT_BAUD_RATE_RAW: &str = "baudrate";
123    pub const PORT_EXCLUSIVE_RAW: &str = "exclusive";
124    pub const TIMEOUT_RAW: &str = "tout";
125    pub const RELEASE_ON_CLOSE: &str = "release_on_close";
126}