|
1 | | -use async_trait::async_trait; |
2 | | -use std::sync::Arc; |
3 | | -use up_rust::{UListener, UMessage, UStatus, UTransport, UUri, UCode}; |
4 | | - |
5 | | -/// This will be the main struct for our uProtocol transport. |
6 | | -/// It will hold the state necessary to communicate with iceoryx2, |
7 | | -/// such as the service connection and active listeners. |
8 | | -pub struct Iceoryx2Transport {} |
9 | | - |
10 | | -#[async_trait] |
11 | | -impl UTransport for Iceoryx2Transport { |
12 | | - async fn send(&self, _message: UMessage) -> Result<(), UStatus> { |
13 | | - todo!(); |
14 | | - } |
15 | | - |
16 | | - async fn register_listener( |
17 | | - &self, |
18 | | - _source_filter: &UUri, |
19 | | - _sink_filter: Option<&UUri>, |
20 | | - _listener: Arc<dyn UListener>, |
21 | | - ) -> Result<(), UStatus> { |
22 | | - todo!() |
23 | | - } |
24 | | - |
25 | | - async fn unregister_listener( |
26 | | - &self, |
27 | | - _source_filter: &UUri, |
28 | | - _sink_filter: Option<&UUri>, |
29 | | - _listener: Arc<dyn UListener>, |
30 | | - ) -> Result<(), UStatus> { |
31 | | - todo!() |
32 | | - } |
33 | | -} |
34 | | - |
35 | | -impl Iceoryx2Transport { |
36 | | - fn encode_uuri_segments(uuri: &UUri) -> Vec<String> { |
37 | | - vec![ |
38 | | - uuri.authority_name.clone(), |
39 | | - Self::encode_hex(uuri.uentity_type_id() as u32), |
40 | | - Self::encode_hex(uuri.uentity_instance_id() as u32), |
41 | | - Self::encode_hex(uuri.uentity_major_version() as u32), |
42 | | - Self::encode_hex(uuri.resource_id() as u32), |
43 | | - ] |
44 | | - } |
45 | | - |
46 | | - fn encode_hex(value: u32) -> String { |
47 | | - format!("{:X}", value) |
48 | | - } |
49 | | - |
50 | | - fn compute_service_name(source: &UUri,sink: Option<&UUri>) -> Result<String, UStatus> { |
51 | | - let join_segments = |segments: Vec<String>| segments.join("/"); |
52 | | - |
53 | | - // checking for REQUEST: source.resource_id=0 and 1<=sink.resource_id<=0x7FFF |
54 | | - if (source.resource_id==0 )&& !(sink.is_none()){ |
55 | | - if !((1<=sink.unwrap().resource_id && sink.unwrap().resource_id<=0x7FFF)){ |
56 | | - UStatus::fail_with_code(UCode::INVALID_ARGUMENT, "Invalid sink URI for RPC request"); |
57 | | - } |
58 | | - let segments = Self::encode_uuri_segments(sink.unwrap()); |
59 | | - Ok(format!("up/{}", join_segments(segments))) |
60 | | - } |
61 | | - // checking for RESPONSE AND NOTIF: sink.resource_id=0 and 1<=source.resource_id<=0xFFFE |
62 | | - else if (!(sink.is_none()))&&(sink.unwrap().resource_id==0){ |
63 | | - if 1<=source.resource_id && source.resource_id <=0xFFFE{ |
64 | | - let source_segments = Self::encode_uuri_segments(source); |
65 | | - let sink_segments = Self::encode_uuri_segments(sink.unwrap()); |
66 | | - Ok(format!( |
67 | | - "up/{}/{}", |
68 | | - join_segments(source_segments), |
69 | | - join_segments(sink_segments) |
70 | | - )) |
71 | | - } |
72 | | - else{ |
73 | | - Err(UStatus::fail_with_code(UCode::INVALID_ARGUMENT, "Invalid sink and source URIs"))} |
74 | | - } |
75 | | - // checking for PUBLISH: 1 <=source.resource_id<=0x7FFF |
76 | | - else if 1<=source.resource_id && source.resource_id<=0x7FFF { |
77 | | - let segments = Self::encode_uuri_segments(source); |
78 | | - Ok(format!("up/{}", join_segments(segments))) |
79 | | - } |
80 | | - else{ |
81 | | - Err(UStatus::fail_with_code( |
82 | | - UCode::INVALID_ARGUMENT, |
83 | | - "Unsupported UMessageType", |
84 | | - )) |
85 | | - } |
86 | | - |
87 | | - } |
88 | | -} |
89 | | - |
90 | | -#[cfg(test)] |
91 | | -mod tests { |
92 | | - use super::*; |
93 | | - use up_rust::{UMessageBuilder, UPayloadFormat}; |
94 | | - |
95 | | - // fn dummy_uuid() -> up_rust::UUID { |
96 | | - // up_rust::UUID::build() |
97 | | - // } |
98 | | - |
99 | | - fn test_uri(authority: &str, instance: u16, typ: u16, version: u8, resource: u16) -> UUri { |
100 | | - let entity_id = ((instance as u32) << 16) | (typ as u32); |
101 | | - UUri::try_from_parts(authority, entity_id, version, resource).unwrap() |
102 | | - } |
103 | | - |
104 | | - #[test] |
105 | | - fn test_publish_service_name() { |
106 | | - let source = test_uri("device1", 0x0000, 0x10AB, 0x03, 0x7FFF); |
107 | | - |
108 | | - let name = Iceoryx2Transport::compute_service_name(&source,None).unwrap(); |
109 | | - assert_eq!(name, "up/device1/10AB/0/3/7FFF"); |
110 | | - } |
111 | | - |
112 | | - #[test] |
113 | | - fn test_notification_service_name() { |
114 | | - let source = test_uri("device1", 0x0000, 0x10AB, 0x03, 0x80CD); |
115 | | - let sink = test_uri("device1", 0x0000, 0x30EF, 0x04, 0x0000); |
116 | | - let name = Iceoryx2Transport::compute_service_name(&source,Some(&sink)).unwrap(); |
117 | | - assert_eq!(name, "up/device1/10AB/0/3/80CD/device1/30EF/0/4/0"); |
118 | | - } |
119 | | - |
120 | | - #[test] |
121 | | - fn test_rpc_request_service_name() { |
122 | | - let sink = test_uri("device1", 0x0004, 0x03AB, 0x03, 0x0000); |
123 | | - let reply_to = test_uri("device1", 0x0000, 0x00CD, 0x04, 0xB); |
124 | | - |
125 | | - let name = Iceoryx2Transport::compute_service_name(&sink,Some(&reply_to)).unwrap(); |
126 | | - assert_eq!(name, "up/device1/CD/0/4/B"); |
127 | | - } |
128 | | - |
129 | | - #[test] |
130 | | - fn test_rpc_response_service_name() { |
131 | | - let source = test_uri("device1", 0x0000, 0x00CD, 0x04, 0xB); |
132 | | - let sink = test_uri("device1", 0x0004, 0x3AB, 0x3, 0x0000); |
133 | | - |
134 | | - |
135 | | - let name = Iceoryx2Transport::compute_service_name(&source,Some(&sink)).unwrap(); |
136 | | - assert_eq!(name, "up/device1/CD/0/4/B/device1/3AB/4/3/0"); |
137 | | - } |
138 | | - |
139 | | - #[test] |
140 | | - fn test_missing_uri_error() { |
141 | | - let uuri = UUri::new(); |
142 | | - let result = Iceoryx2Transport::compute_service_name(&uuri, None); |
143 | | - |
144 | | - assert!(result.is_err_and(|err| err.get_code() == UCode::INVALID_ARGUMENT)); |
145 | | - } |
146 | | -} |
| 1 | +use async_trait::async_trait; |
| 2 | +use std::sync::Arc; |
| 3 | +use up_rust::{UCode, UListener, UMessage, UStatus, UTransport, UUri}; |
| 4 | + |
| 5 | +/// This will be the main struct for our uProtocol transport. |
| 6 | +/// It will hold the state necessary to communicate with iceoryx2, |
| 7 | +/// such as the service connection and active listeners. |
| 8 | +pub struct Iceoryx2Transport {} |
| 9 | + |
| 10 | +// The #[async_trait] attribute enables async functions in our trait impl. |
| 11 | +#[async_trait] |
| 12 | +impl UTransport for Iceoryx2Transport { |
| 13 | + async fn send(&self, _message: UMessage) -> Result<(), UStatus> { |
| 14 | + todo!(); |
| 15 | + } |
| 16 | + |
| 17 | + async fn register_listener( |
| 18 | + &self, |
| 19 | + _source_filter: &UUri, |
| 20 | + _sink_filter: Option<&UUri>, |
| 21 | + _listener: Arc<dyn UListener>, |
| 22 | + ) -> Result<(), UStatus> { |
| 23 | + todo!() |
| 24 | + } |
| 25 | + |
| 26 | + async fn unregister_listener( |
| 27 | + &self, |
| 28 | + _source_filter: &UUri, |
| 29 | + _sink_filter: Option<&UUri>, |
| 30 | + _listener: Arc<dyn UListener>, |
| 31 | + ) -> Result<(), UStatus> { |
| 32 | + todo!() |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +#[allow(dead_code)] |
| 37 | +impl Iceoryx2Transport { |
| 38 | + fn encode_uuri_segments(uuri: &UUri) -> Vec<String> { |
| 39 | + vec![ |
| 40 | + uuri.authority_name.clone(), |
| 41 | + Self::encode_hex(uuri.uentity_type_id() as u32), |
| 42 | + Self::encode_hex(uuri.uentity_instance_id() as u32), |
| 43 | + Self::encode_hex(uuri.uentity_major_version() as u32), |
| 44 | + Self::encode_hex(uuri.resource_id() as u32), |
| 45 | + ] |
| 46 | + } |
| 47 | + |
| 48 | + fn encode_hex(value: u32) -> String { |
| 49 | + format!("{:X}", value) |
| 50 | + } |
| 51 | + |
| 52 | + fn compute_service_name(source: &UUri, sink: Option<&UUri>) -> Result<String, UStatus> { |
| 53 | + let join_segments = |segments: Vec<String>| segments.join("/"); |
| 54 | + |
| 55 | + match (source, sink) { |
| 56 | + // RPC Request: source is a stub (0), sink is a valid method (1..=0x7FFF) |
| 57 | + (s, Some(sink)) if s.is_rpc_response() && sink.is_rpc_method() => { |
| 58 | + let segments = Self::encode_uuri_segments(sink); |
| 59 | + Ok(format!("up/{}", join_segments(segments))) |
| 60 | + } |
| 61 | + |
| 62 | + // Notification or RPC Response: sink is stub (0), source is valid (1..=0xFFFE) |
| 63 | + (source, Some(sink)) |
| 64 | + if sink.is_rpc_response() && (1..=0xFFFE).contains(&source.resource_id) => |
| 65 | + { |
| 66 | + let source_segments = Self::encode_uuri_segments(source); |
| 67 | + let sink_segments = Self::encode_uuri_segments(sink); |
| 68 | + Ok(format!( |
| 69 | + "up/{}/{}", |
| 70 | + join_segments(source_segments), |
| 71 | + join_segments(sink_segments) |
| 72 | + )) |
| 73 | + } |
| 74 | + |
| 75 | + // Publish: source is valid (1..=0x7FFF), sink is None |
| 76 | + (source, None) if (1..=0x7FFF).contains(&source.resource_id) => { |
| 77 | + let segments = Self::encode_uuri_segments(source); |
| 78 | + Ok(format!("up/{}", join_segments(segments))) |
| 79 | + } |
| 80 | + |
| 81 | + // Invalid cases |
| 82 | + _ => Err(UStatus::fail_with_code( |
| 83 | + UCode::INVALID_ARGUMENT, |
| 84 | + "Unsupported or invalid UUri combination", |
| 85 | + )), |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +#[cfg(test)] |
| 91 | +mod tests { |
| 92 | + use super::*; |
| 93 | + |
| 94 | + fn test_uri(authority: &str, instance: u16, typ: u16, version: u8, resource: u16) -> UUri { |
| 95 | + let entity_id = ((instance as u32) << 16) | (typ as u32); |
| 96 | + UUri::try_from_parts(authority, entity_id, version, resource).unwrap() |
| 97 | + } |
| 98 | + |
| 99 | + #[test] |
| 100 | + // [specitem,oft-sid="dsn~up-transport-iceoryx2-service-name~1",oft-needs="utest"] |
| 101 | + fn test_publish_service_name() { |
| 102 | + let source = test_uri("device1", 0x0000, 0x10AB, 0x03, 0x7FFF); |
| 103 | + |
| 104 | + let name = Iceoryx2Transport::compute_service_name(&source, None).unwrap(); |
| 105 | + assert_eq!(name, "up/device1/10AB/0/3/7FFF"); |
| 106 | + } |
| 107 | + |
| 108 | + #[test] |
| 109 | + // [specitem,oft-sid="dsn~up-transport-iceoryx2-service-name~1",oft-needs="utest"] |
| 110 | + fn test_notification_service_name() { |
| 111 | + let source = test_uri("device1", 0x0000, 0x10AB, 0x03, 0x80CD); |
| 112 | + let sink = test_uri("device1", 0x0000, 0x30EF, 0x04, 0x0000); |
| 113 | + let name = Iceoryx2Transport::compute_service_name(&source, Some(&sink)).unwrap(); |
| 114 | + assert_eq!(name, "up/device1/10AB/0/3/80CD/device1/30EF/0/4/0"); |
| 115 | + } |
| 116 | + |
| 117 | + #[test] |
| 118 | + // [specitem,oft-sid="dsn~up-transport-iceoryx2-service-name~1",oft-needs="utest"] |
| 119 | + fn test_rpc_request_service_name() { |
| 120 | + let sink = test_uri("device1", 0x0004, 0x03AB, 0x03, 0x0000); |
| 121 | + let reply_to = test_uri("device1", 0x0000, 0x00CD, 0x04, 0xB); |
| 122 | + |
| 123 | + let name = Iceoryx2Transport::compute_service_name(&sink, Some(&reply_to)).unwrap(); |
| 124 | + assert_eq!(name, "up/device1/CD/0/4/B"); |
| 125 | + } |
| 126 | + |
| 127 | + #[test] |
| 128 | + // [specitem,oft-sid="dsn~up-transport-iceoryx2-service-name~1",oft-needs="utest"] |
| 129 | + fn test_rpc_response_service_name() { |
| 130 | + let source = test_uri("device1", 0x0000, 0x00CD, 0x04, 0xB); |
| 131 | + let sink = test_uri("device1", 0x0004, 0x3AB, 0x3, 0x0000); |
| 132 | + |
| 133 | + let name = Iceoryx2Transport::compute_service_name(&source, Some(&sink)).unwrap(); |
| 134 | + assert_eq!(name, "up/device1/CD/0/4/B/device1/3AB/4/3/0"); |
| 135 | + } |
| 136 | + |
| 137 | + #[test] |
| 138 | + // [specitem,oft-sid="dsn~up-transport-iceoryx2-service-name~1",oft-needs="utest"] |
| 139 | + fn test_missing_uri_error() { |
| 140 | + let uuri = UUri::new(); |
| 141 | + let result = Iceoryx2Transport::compute_service_name(&uuri, None); |
| 142 | + |
| 143 | + assert!(result.is_err()); |
| 144 | + assert_eq!(result.unwrap_err().get_code(), UCode::INVALID_ARGUMENT); |
| 145 | + } |
| 146 | +} |
0 commit comments