|
| 1 | +use async_trait::async_trait; |
| 2 | +use hello_world_protos::hello_world_service::{HelloRequest, HelloResponse}; |
| 3 | +use log::trace; |
| 4 | +use std::fs::canonicalize; |
| 5 | +use std::path::PathBuf; |
| 6 | +use std::sync::Arc; |
| 7 | +use std::time::Duration; |
| 8 | +use up_rust::UPayloadFormat::UPAYLOAD_FORMAT_PROTOBUF_WRAPPED_IN_ANY; |
| 9 | +use up_rust::{UListener, UMessage, UMessageBuilder, UStatus, UTransport, UUri}; |
| 10 | +use up_transport_vsomeip::UPTransportVsomeip; |
| 11 | + |
| 12 | +const HELLO_SERVICE_ID: u16 = 0x6000; |
| 13 | +const HELLO_INSTANCE_ID: u16 = 0x0001; |
| 14 | +const HELLO_METHOD_ID: u16 = 0x7FFF; |
| 15 | + |
| 16 | +/// IMPORTANT: should match vsomeip::DEFAULT_MAJOR in (interface/vsomeip/constants.hpp): |
| 17 | +// but Autosar works better if vsomeip::DEFAULT_MAJOR is 1 (thus Autosar needs custom vsomeip build) |
| 18 | +const HELLO_SERVICE_MAJOR: u8 = 1; |
| 19 | +const _HELLO_SERVICE_MINOR: u32 = 0; |
| 20 | + |
| 21 | +const HELLO_SERVICE_AUTHORITY: &str = "linux"; |
| 22 | +const HELLO_SERVICE_UE_ID: u32 = (HELLO_INSTANCE_ID as u32) << 16 | HELLO_SERVICE_ID as u32; |
| 23 | +const HELLO_SERVICE_UE_VERSION_MAJOR: u8 = HELLO_SERVICE_MAJOR; |
| 24 | +const HELLO_SERVICE_RESOURCE_ID: u16 = HELLO_METHOD_ID; |
| 25 | + |
| 26 | +const CLIENT_AUTHORITY: &str = "me_authority"; |
| 27 | +const CLIENT_UE_ID: u16 = 0x5678; |
| 28 | +const CLIENT_UE_VERSION_MAJOR: u8 = 1; |
| 29 | +const CLIENT_RESOURCE_ID: u16 = 0; |
| 30 | + |
| 31 | +const REQUEST_TTL: u32 = 1000; |
| 32 | + |
| 33 | +struct ServiceResponseListener; |
| 34 | + |
| 35 | +#[async_trait] |
| 36 | +impl UListener for ServiceResponseListener { |
| 37 | + async fn on_receive(&self, msg: UMessage) { |
| 38 | + println!("ServiceResponseListener: Received a message: {msg:?}"); |
| 39 | + |
| 40 | + let mut msg = msg.clone(); |
| 41 | + |
| 42 | + if let Some(ref mut attributes) = msg.attributes.as_mut() { |
| 43 | + attributes.payload_format = |
| 44 | + ::protobuf::EnumOrUnknown::new(UPAYLOAD_FORMAT_PROTOBUF_WRAPPED_IN_ANY); |
| 45 | + } |
| 46 | + |
| 47 | + let Ok(hello_response) = msg.extract_protobuf_payload::<HelloResponse>() else { |
| 48 | + panic!("Unable to parse into HelloResponse"); |
| 49 | + }; |
| 50 | + |
| 51 | + println!("Here we received response: {hello_response:?}"); |
| 52 | + } |
| 53 | + |
| 54 | + async fn on_error(&self, err: UStatus) { |
| 55 | + println!("ServiceResponseListener: Encountered an error: {err:?}"); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +#[tokio::main] |
| 60 | +async fn main() -> Result<(), UStatus> { |
| 61 | + env_logger::init(); |
| 62 | + |
| 63 | + println!("mE_client"); |
| 64 | + |
| 65 | + let crate_dir = env!("CARGO_MANIFEST_DIR"); |
| 66 | + // TODO: Make configurable to pass the path to the vsomeip config as a command line argument |
| 67 | + let vsomeip_config = PathBuf::from(crate_dir).join("vsomeip_configs/hello_service.json"); |
| 68 | + let vsomeip_config = canonicalize(vsomeip_config).ok(); |
| 69 | + trace!("vsomeip_config: {vsomeip_config:?}"); |
| 70 | + |
| 71 | + // There will be a single vsomeip_transport, as there is a connection into device and a streamer |
| 72 | + // TODO: Add error handling if we fail to create a UPTransportVsomeip |
| 73 | + let client: Arc<dyn UTransport> = Arc::new( |
| 74 | + UPTransportVsomeip::new_with_config( |
| 75 | + &CLIENT_AUTHORITY.to_string(), |
| 76 | + &HELLO_SERVICE_AUTHORITY.to_string(), |
| 77 | + CLIENT_UE_ID, |
| 78 | + &vsomeip_config.unwrap(), |
| 79 | + None, |
| 80 | + ) |
| 81 | + .unwrap(), |
| 82 | + ); |
| 83 | + |
| 84 | + let source = UUri { |
| 85 | + authority_name: CLIENT_AUTHORITY.to_string(), |
| 86 | + ue_id: CLIENT_UE_ID as u32, |
| 87 | + ue_version_major: CLIENT_UE_VERSION_MAJOR as u32, |
| 88 | + resource_id: CLIENT_RESOURCE_ID as u32, |
| 89 | + ..Default::default() |
| 90 | + }; |
| 91 | + let sink = UUri { |
| 92 | + authority_name: HELLO_SERVICE_AUTHORITY.to_string(), |
| 93 | + ue_id: HELLO_SERVICE_UE_ID, |
| 94 | + ue_version_major: HELLO_SERVICE_UE_VERSION_MAJOR as u32, |
| 95 | + resource_id: HELLO_SERVICE_RESOURCE_ID as u32, |
| 96 | + ..Default::default() |
| 97 | + }; |
| 98 | + |
| 99 | + let service_response_listener: Arc<dyn UListener> = Arc::new(ServiceResponseListener); |
| 100 | + client |
| 101 | + .register_listener(&sink, Some(&source), service_response_listener) |
| 102 | + .await?; |
| 103 | + |
| 104 | + let mut i = 0; |
| 105 | + loop { |
| 106 | + tokio::time::sleep(Duration::from_millis(1000)).await; |
| 107 | + |
| 108 | + let hello_request = HelloRequest { |
| 109 | + name: format!("me_client@i={}", i).to_string(), |
| 110 | + ..Default::default() |
| 111 | + }; |
| 112 | + i += 1; |
| 113 | + |
| 114 | + let request_msg = UMessageBuilder::request(sink.clone(), source.clone(), REQUEST_TTL) |
| 115 | + .build_with_protobuf_payload(&hello_request) |
| 116 | + .unwrap(); |
| 117 | + println!("Sending Request message:\n{request_msg:?}"); |
| 118 | + |
| 119 | + client.send(request_msg).await?; |
| 120 | + } |
| 121 | +} |
0 commit comments