erpc_rust 0.1.3

Rust implementation of eRPC (Embedded RPC) protocol
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
//! Client implementation for eRPC

use crate::auxiliary::{MessageInfo, MessageType, RequestContext};
use crate::codec::{BasicCodecFactory, Codec, CodecFactory};
use crate::error::{ErpcResult, RequestError};
use crate::transport::rusb::RusbTransport;
#[cfg(feature = "serial")]
use crate::transport::SerialTransport;
#[cfg(unix)]
use crate::transport::SocketTransport;
use crate::transport::{TcpTransport, Transport};
use async_trait::async_trait;
use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::Arc;
use std::time::Duration;

/// Transport wrapper that can hold different transport types
pub enum TransportWrapper {
    Tcp(TcpTransport),
    #[cfg(unix)]
    Socket(SocketTransport),
    #[cfg(feature = "serial")]
    Serial(SerialTransport),
    Rusb(RusbTransport),
}

#[async_trait]
impl Transport for TransportWrapper {
    async fn send(&mut self, data: &[u8]) -> ErpcResult<()> {
        match self {
            TransportWrapper::Tcp(tcp) => tcp.send(data).await,
            #[cfg(unix)]
            TransportWrapper::Socket(socket) => socket.send(data).await,
            #[cfg(feature = "serial")]
            TransportWrapper::Serial(serial) => serial.send(data).await,
            TransportWrapper::Rusb(rusb) => rusb.send(data).await,
        }
    }

    async fn receive(&mut self) -> ErpcResult<Vec<u8>> {
        match self {
            TransportWrapper::Tcp(tcp) => tcp.receive().await,
            #[cfg(unix)]
            TransportWrapper::Socket(socket) => socket.receive().await,
            #[cfg(feature = "serial")]
            TransportWrapper::Serial(serial) => serial.receive().await,
            TransportWrapper::Rusb(rusb) => rusb.receive().await,
        }
    }

    async fn close(&mut self) -> ErpcResult<()> {
        match self {
            TransportWrapper::Tcp(tcp) => tcp.close().await,
            #[cfg(unix)]
            TransportWrapper::Socket(socket) => socket.close().await,
            #[cfg(feature = "serial")]
            TransportWrapper::Serial(serial) => serial.close().await,
            TransportWrapper::Rusb(rusb) => rusb.close().await,
        }
    }

    fn is_connected(&self) -> bool {
        match self {
            TransportWrapper::Tcp(tcp) => tcp.is_connected(),
            #[cfg(unix)]
            TransportWrapper::Socket(socket) => socket.is_connected(),
            #[cfg(feature = "serial")]
            TransportWrapper::Serial(serial) => serial.is_connected(),
            TransportWrapper::Rusb(rusb) => rusb.is_connected(),
        }
    }

    fn set_timeout(&mut self, timeout: Duration) {
        match self {
            TransportWrapper::Tcp(tcp) => tcp.set_timeout(timeout),
            #[cfg(unix)]
            TransportWrapper::Socket(socket) => socket.set_timeout(timeout),
            #[cfg(feature = "serial")]
            TransportWrapper::Serial(serial) => serial.set_timeout(timeout),
            TransportWrapper::Rusb(rusb) => rusb.set_timeout(timeout),
        }
    }
}

/// Client manager for making RPC calls
pub struct ClientManager<T, F>
where
    T: Transport,
    F: CodecFactory,
{
    transport: T,
    codec_factory: F,
    sequence_counter: Arc<AtomicU32>,
}

impl<T, F> ClientManager<T, F>
where
    T: Transport,
    F: CodecFactory,
{
    /// Create new client manager
    pub fn new(transport: T, codec_factory: F) -> Self {
        Self {
            transport,
            codec_factory,
            sequence_counter: Arc::new(AtomicU32::new(0)),
        }
    }

    /// Create a new client manager builder
    pub fn generic_builder() -> ClientManagerBuilder<T, F> {
        ClientManagerBuilder::new()
    }

    /// Get next sequence number
    fn next_sequence(&self) -> u32 {
        self.sequence_counter.fetch_add(1, Ordering::SeqCst) + 1
    }

    /// Create a new request context
    pub fn create_request(&self, is_oneway: bool) -> RequestContext {
        let sequence = self.next_sequence();
        RequestContext::new(sequence, is_oneway)
    }

    /// Create a new request context with service ID (enhanced)
    pub fn create_request_with_service(&self, service_id: u32, is_oneway: bool) -> RequestContext {
        let sequence = self.next_sequence();
        RequestContext::with_service(sequence, Some(service_id), is_oneway)
    }

    /// Perform a request-response call for generated code (4 parameters)
    pub async fn perform_request(
        &mut self,
        service_id: u8,
        method_id: u8,
        is_oneway: bool,
        request_data: Vec<u8>,
    ) -> ErpcResult<Vec<u8>> {
        let sequence = self.next_sequence();

        // Create message
        let message_type = if is_oneway {
            MessageType::Oneway
        } else {
            MessageType::Invocation
        };

        let message_info = MessageInfo::new(message_type, service_id, method_id, sequence);

        // Encode request
        let mut codec = self.codec_factory.create();
        codec.start_write_message(&message_info)?;
        codec.write_bytes(&request_data)?;

        // Send request
        self.transport.send(codec.as_bytes()).await?;

        if is_oneway {
            return Ok(Vec::new());
        }

        // Receive response
        let response_data = self.transport.receive().await?;
        let mut response_codec = self.codec_factory.create_from_data(response_data);

        // Validate response
        let response_info = response_codec.start_read_message()?;

        if response_info.message_type != MessageType::Reply {
            return Err(RequestError::InvalidMessageType.into());
        }

        if response_info.sequence != sequence {
            return Err(RequestError::UnexpectedSequence {
                expected: sequence,
                actual: response_info.sequence,
            }
            .into());
        }

        // Read response data
        let response_payload = response_codec.get_remaining_bytes()?;
        Ok(response_payload)
    }

    /// Simplified perform request for direct use (2 parameters)
    pub async fn send_raw_request(
        &mut self,
        request_data: &[u8],
        is_oneway: bool,
    ) -> ErpcResult<Vec<u8>> {
        let _context = self.create_request(is_oneway);

        // Write request data to transport
        self.transport.send(request_data).await?;

        if is_oneway {
            // For oneway calls, don't wait for response
            Ok(Vec::new())
        } else {
            // For regular calls, wait for response
            self.transport.receive().await
        }
    }

    /// Send a request without waiting for response
    pub async fn send_request(&mut self, request_data: &[u8]) -> ErpcResult<()> {
        self.send_raw_request(request_data, true).await?;
        Ok(())
    }

    /// Receive a response for a previous request
    pub async fn receive_response(&mut self) -> ErpcResult<Vec<u8>> {
        self.transport.receive().await
    }

    /// Get the codec factory
    pub fn codec_factory(&self) -> &F {
        &self.codec_factory
    }

    /// Check if client is connected
    pub fn is_connected(&self) -> bool {
        self.transport.is_connected()
    }

    /// Close the client connection
    pub async fn close(&mut self) -> ErpcResult<()> {
        self.transport.close().await
    }
}

/// Builder for creating ClientManager with configurable transport and codec
pub struct ClientBuilder {
    transport_config: Option<TransportConfig>,
    codec_config: Option<CodecConfig>,
}

/// Transport configuration options
pub enum TransportConfig {
    Tcp(String),
    #[cfg(unix)]
    Socket(String),
    #[cfg(feature = "serial")]
    Serial {
        port: String,
        baud_rate: u32,
    },
    Rusb {
        vendor_id: u16,
        product_id: u16,
    },
}

/// Codec configuration options  
pub enum CodecConfig {
    Basic,
    // Future: Custom(Box<dyn CodecFactory>),
}

impl ClientBuilder {
    /// Create a new builder with default (null) transport and codec
    pub fn new() -> Self {
        Self {
            transport_config: None,
            codec_config: None,
        }
    }

    /// Configure TCP connection
    pub fn tcp_connection(mut self, address: &str) -> Self {
        self.transport_config = Some(TransportConfig::Tcp(address.to_string()));
        self
    }

    /// Configure Unix socket connection
    #[cfg(unix)]
    pub fn socket_connection(mut self, path: &str) -> Self {
        self.transport_config = Some(TransportConfig::Socket(path.to_string()));
        self
    }

    /// Configure serial connection
    pub fn serial_connection(mut self, port: &str) -> Self {
        self.serial_connection_with_baud(port, 115200) // Default baud rate
    }

    pub fn rusb_connection(mut self, vendor_id: u16, product_id: u16) -> Self {
        self.transport_config = Some(TransportConfig::Rusb {
            vendor_id,
            product_id,
        });
        self
    }

    /// Configure serial connection with custom baud rate
    #[cfg(feature = "serial")]
    pub fn serial_connection_with_baud(mut self, port: &str, baud_rate: u32) -> Self {
        self.transport_config = Some(TransportConfig::Serial {
            port: port.to_string(),
            baud_rate,
        });
        self
    }

    /// Configure serial connection with custom baud rate (no-op when serial feature disabled)
    #[cfg(not(feature = "serial"))]
    pub fn serial_connection_with_baud(self, _port: &str, _baud_rate: u32) -> Self {
        // No-op when serial feature is disabled
        self
    }

    /// Configure codec
    pub fn codec(mut self, codec: CodecConfig) -> Self {
        self.codec_config = Some(codec);
        self
    }

    /// Connect and build the ClientManager
    pub async fn connect(self) -> ErpcResult<ClientManager<TransportWrapper, BasicCodecFactory>> {
        let transport = match self.transport_config {
            Some(TransportConfig::Tcp(address)) => {
                let tcp = TcpTransport::connect(&address).await?;
                TransportWrapper::Tcp(tcp)
            }
            #[cfg(unix)]
            Some(TransportConfig::Socket(path)) => {
                let socket = SocketTransport::connect(&path).await?;
                TransportWrapper::Socket(socket)
            }
            #[cfg(feature = "serial")]
            Some(TransportConfig::Serial { port, baud_rate }) => {
                let serial = SerialTransport::open(&port, baud_rate)?;
                TransportWrapper::Serial(serial)
            }
            Some(TransportConfig::Rusb {
                vendor_id,
                product_id,
            }) => {
                let rusb_transport =
                    crate::transport::rusb::RusbTransport::connect(vendor_id, product_id).await?;
                TransportWrapper::Rusb(rusb_transport)
            }
            None => return Err(RequestError::InvalidServiceId(0).into()), // Transport not configured
        };

        let codec_factory = match self.codec_config {
            Some(CodecConfig::Basic) | None => BasicCodecFactory::new(), // Default to Basic
        };

        Ok(ClientManager::new(transport, codec_factory))
    }
}

impl Default for ClientBuilder {
    fn default() -> Self {
        Self::new()
    }
}

// Add static method to ClientManager for builder access
impl ClientManager<TransportWrapper, BasicCodecFactory> {
    /// Create a new builder for configuring ClientManager  
    pub fn builder() -> ClientBuilder {
        ClientBuilder::new()
    }
}

/// Builder for creating client managers
pub struct ClientManagerBuilder<T, F>
where
    T: Transport,
    F: CodecFactory,
{
    transport: Option<T>,
    codec_factory: Option<F>,
}

impl<T, F> ClientManagerBuilder<T, F>
where
    T: Transport,
    F: CodecFactory,
{
    /// Create new builder
    pub fn new() -> Self {
        Self {
            transport: None,
            codec_factory: None,
        }
    }

    /// Set transport
    pub fn transport(mut self, transport: T) -> Self {
        self.transport = Some(transport);
        self
    }

    /// Set codec factory
    pub fn codec_factory(mut self, codec_factory: F) -> Self {
        self.codec_factory = Some(codec_factory);
        self
    }

    /// Build client manager
    pub fn build(self) -> Result<ClientManager<T, F>, &'static str> {
        let transport = self.transport.ok_or("Transport not set")?;
        let codec_factory = self.codec_factory.ok_or("Codec factory not set")?;

        Ok(ClientManager::new(transport, codec_factory))
    }
}

impl<T, F> Default for ClientManagerBuilder<T, F>
where
    T: Transport,
    F: CodecFactory,
{
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::codec::BasicCodecFactory;
    use crate::transport::memory::MemoryTransport;

    #[tokio::test]
    async fn test_client_manager_creation() {
        let (transport, _) = MemoryTransport::pair();
        let codec_factory = BasicCodecFactory::new();

        let client = ClientManager::new(transport, codec_factory);
        assert!(client.is_connected());
    }

    #[tokio::test]
    async fn test_request_context() {
        let (transport, _) = MemoryTransport::pair();
        let codec_factory = BasicCodecFactory::new();
        let client = ClientManager::new(transport, codec_factory);

        let ctx = client.create_request(false);
        assert!(!ctx.is_oneway());
        assert_eq!(ctx.sequence(), 1);

        let ctx2 = client.create_request(true);
        assert!(ctx2.is_oneway());
        assert_eq!(ctx2.sequence(), 2);
    }

    #[tokio::test]
    async fn test_client_builder() {
        let (transport, _) = MemoryTransport::pair();
        let codec_factory = BasicCodecFactory::new();

        let client = ClientManagerBuilder::new()
            .transport(transport)
            .codec_factory(codec_factory)
            .build()
            .unwrap();

        assert!(client.is_connected());
    }
}