moteus 0.5.0

Rust client library for moteus brushless motor controllers
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
// Copyright 2026 mjbots Robotic Systems, LLC.  info@mjbots.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Router factory system for creating CAN-FD transports.
//!
//! This module provides the factory pattern for creating transport devices
//! from different backends (fdcanusb, socketcan, etc.).
//!
//! External crates can register additional factories via [`register()`]:
//!
//! ```rust,ignore
//! moteus::transport::factory::register(Box::new(MyFactory));
//! ```

use std::collections::HashMap;
use std::sync::{Mutex, OnceLock};
use std::time::Duration;

use crate::error::Result;
use crate::transport::args::ArgSpec;
use crate::transport::device::TransportDevice;

/// Default communication timeout.
pub const DEFAULT_TIMEOUT: Duration = Duration::from_millis(100);

/// Options for configuring transport creation.
#[non_exhaustive]
#[derive(Debug, Clone, Default)]
pub struct TransportOptions {
    /// Explicit fdcanusb device paths to use.
    pub fdcanusb_paths: Vec<String>,
    /// Explicit socketcan interfaces to use.
    pub socketcan_interfaces: Vec<String>,
    /// Disable bit rate switching (BRS) for CAN-FD.
    pub disable_brs: bool,
    /// Force a specific transport type ("fdcanusb" or "socketcan").
    pub force_transport: Option<String>,
    /// Communication timeout.
    pub timeout: Duration,
    /// Extra transport-specific options for external factories.
    ///
    /// Keys are argument names, values are lists of values (to support
    /// multi-value arguments). Built-in factories ignore this field;
    /// external factories read from it.
    pub extra: HashMap<String, Vec<String>>,
}

impl TransportOptions {
    /// Create new transport options with default values.
    pub fn new() -> Self {
        Self {
            timeout: DEFAULT_TIMEOUT,
            ..Default::default()
        }
    }

    /// Set explicit fdcanusb paths.
    #[must_use]
    pub fn fdcanusb_paths(mut self, paths: impl IntoIterator<Item = impl Into<String>>) -> Self {
        self.fdcanusb_paths = paths.into_iter().map(Into::into).collect();
        self
    }

    /// Set explicit socketcan interfaces.
    #[must_use]
    pub fn socketcan_interfaces(
        mut self,
        interfaces: impl IntoIterator<Item = impl Into<String>>,
    ) -> Self {
        self.socketcan_interfaces = interfaces.into_iter().map(Into::into).collect();
        self
    }

    /// Disable bit rate switching.
    #[must_use]
    pub fn disable_brs(mut self, disable: bool) -> Self {
        self.disable_brs = disable;
        self
    }

    /// Force a specific transport type.
    #[must_use]
    pub fn force_transport(mut self, transport: impl Into<String>) -> Self {
        self.force_transport = Some(transport.into());
        self
    }

    /// Set the communication timeout.
    #[must_use]
    pub fn timeout(mut self, timeout: Duration) -> Self {
        self.timeout = timeout;
        self
    }

    /// Set an extra transport-specific option.
    #[must_use]
    pub fn extra(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.extra.entry(key.into()).or_default().push(value.into());
        self
    }

    /// Parse transport options from key-value pairs.
    ///
    /// This method allows creating transport options from any CLI parser by
    /// converting parsed arguments to key-value pairs. Keys match the standard
    /// command-line argument names (without leading dashes).
    ///
    /// Unknown keys are stored in the `extra` field for external factories.
    ///
    /// # Supported Keys
    ///
    /// - `fdcanusb`: Path to fdcanusb device (can appear multiple times)
    /// - `can-chan`: SocketCAN interface name (can appear multiple times)
    /// - `can-disable-brs`: "true" to disable CAN-FD bit rate switching
    /// - `force-transport`: Router type name (validated against registered factories)
    /// - `timeout-ms`: Communication timeout in milliseconds
    ///
    /// # Example
    ///
    /// ```
    /// use moteus::transport::factory::TransportOptions;
    ///
    /// let opts = TransportOptions::from_pairs([
    ///     ("can-chan", "can0"),
    ///     ("timeout-ms", "200"),
    /// ]).unwrap();
    /// assert_eq!(opts.socketcan_interfaces, vec!["can0"]);
    /// assert_eq!(opts.timeout, std::time::Duration::from_millis(200));
    /// ```
    pub fn from_pairs<'a>(
        pairs: impl IntoIterator<Item = (&'a str, &'a str)>,
    ) -> std::result::Result<Self, String> {
        let mut opts = TransportOptions::new();
        for (key, value) in pairs {
            match key {
                "fdcanusb" => opts.fdcanusb_paths.push(value.to_string()),
                "can-chan" => opts.socketcan_interfaces.push(value.to_string()),
                "can-disable-brs" => opts.disable_brs = value == "true",
                "force-transport" => {
                    opts.force_transport = Some(value.to_string());
                }
                "timeout-ms" => {
                    let ms: u32 = value
                        .parse()
                        .map_err(|_| format!("invalid timeout: {}", value))?;
                    opts.timeout = Duration::from_millis(ms as u64);
                }
                _ => {
                    opts.extra
                        .entry(key.to_string())
                        .or_default()
                        .push(value.to_string());
                }
            }
        }
        Ok(opts)
    }
}

/// A factory for creating transport devices.
///
/// Factories are tried in priority order (lower numbers first).
/// External crates implement this trait and call [`register()`] to
/// add themselves to the auto-detection flow.
pub trait TransportFactory: Send + Sync {
    /// The priority of this factory (lower = tried first).
    fn priority(&self) -> u32;

    /// The name of this transport type.
    fn name(&self) -> &'static str;

    /// Command-line argument specifications for this factory.
    ///
    /// Override this to declare factory-specific CLI arguments. These are
    /// included by [`transport_arg_specs()`](super::args::transport_arg_specs).
    fn arg_specs(&self) -> Vec<ArgSpec> {
        Vec::new()
    }

    /// Create transport devices using this factory.
    ///
    /// Returns a list of devices that were successfully created.
    fn create(&self, options: &TransportOptions) -> Result<Vec<Box<dyn TransportDevice>>>;
}

/// Factory for fdcanusb devices (CDC serial, Linux only).
#[cfg(target_os = "linux")]
#[derive(Debug, Default)]
pub struct FdcanusbFactory;

#[cfg(target_os = "linux")]
impl FdcanusbFactory {
    /// Create a new fdcanusb factory.
    pub fn new() -> Self {
        Self
    }
}

#[cfg(target_os = "linux")]
impl TransportFactory for FdcanusbFactory {
    fn priority(&self) -> u32 {
        10 // Higher priority than socketcan
    }

    fn name(&self) -> &'static str {
        "fdcanusb"
    }

    fn arg_specs(&self) -> Vec<ArgSpec> {
        use crate::transport::args::ArgType;
        vec![ArgSpec {
            name: "fdcanusb",
            help: "Path to fdcanusb device (can be specified multiple times)",
            arg_type: ArgType::MultiString,
            default: None,
            possible_values: None,
        }]
    }

    fn create(&self, options: &TransportOptions) -> Result<Vec<Box<dyn TransportDevice>>> {
        use crate::transport::discovery::{detect_fdcanusbs, FdcanusbInfo};
        use crate::transport::fdcanusb::FdcanusbDevice;

        let infos: Vec<FdcanusbInfo> = if options.fdcanusb_paths.is_empty() {
            // Auto-detect fdcanusb devices
            detect_fdcanusbs()
        } else {
            // Convert explicit paths to FdcanusbInfo (no serial number)
            options
                .fdcanusb_paths
                .iter()
                .map(|path| FdcanusbInfo {
                    path: path.clone(),
                    serial_number: None,
                })
                .collect()
        };

        let mut devices: Vec<Box<dyn TransportDevice>> = Vec::new();
        for (idx, info) in infos.iter().enumerate() {
            match FdcanusbDevice::with_options(&info.path, options.timeout, options.disable_brs) {
                Ok(mut device) => {
                    device.info.id = idx;
                    device.info.serial_number = info.serial_number.clone();
                    device.info.detail =
                        info.serial_number.as_ref().map(|sn| format!("sn='{}'", sn));
                    devices.push(Box::new(device));
                }
                Err(_) => continue, // Skip devices that fail to open
            }
        }

        Ok(devices)
    }
}

/// Factory for socketcan devices (Linux only).
#[cfg(target_os = "linux")]
#[derive(Debug, Default)]
pub struct SocketCanFactory;

#[cfg(target_os = "linux")]
impl SocketCanFactory {
    /// Create a new socketcan factory.
    pub fn new() -> Self {
        Self
    }
}

#[cfg(target_os = "linux")]
impl TransportFactory for SocketCanFactory {
    fn priority(&self) -> u32 {
        11 // Lower priority than fdcanusb
    }

    fn name(&self) -> &'static str {
        "socketcan"
    }

    fn arg_specs(&self) -> Vec<ArgSpec> {
        use crate::transport::args::ArgType;
        vec![ArgSpec {
            name: "can-chan",
            help: "SocketCAN interface (can be specified multiple times)",
            arg_type: ArgType::MultiString,
            default: None,
            possible_values: None,
        }]
    }

    fn create(&self, options: &TransportOptions) -> Result<Vec<Box<dyn TransportDevice>>> {
        use crate::transport::discovery::detect_socketcan_interfaces;
        use crate::transport::socketcan::SocketCanDevice;

        let interfaces = if options.socketcan_interfaces.is_empty() {
            // Auto-detect interfaces
            detect_socketcan_interfaces()
                .into_iter()
                .map(|info| info.interface)
                .collect()
        } else {
            options.socketcan_interfaces.clone()
        };

        let mut devices: Vec<Box<dyn TransportDevice>> = Vec::new();
        for (idx, interface) in interfaces.iter().enumerate() {
            match SocketCanDevice::with_options(interface, options.timeout, options.disable_brs) {
                Ok(mut device) => {
                    device.info.id = idx;
                    devices.push(Box::new(device));
                }
                Err(_) => continue, // Skip interfaces that fail to open
            }
        }

        Ok(devices)
    }
}

// -- Global transport factory registry --

static REGISTRY: OnceLock<Mutex<Vec<Box<dyn TransportFactory>>>> = OnceLock::new();

fn get_registry() -> &'static Mutex<Vec<Box<dyn TransportFactory>>> {
    REGISTRY.get_or_init(|| {
        #[cfg(target_os = "linux")]
        let factories: Vec<Box<dyn TransportFactory>> =
            vec![Box::new(FdcanusbFactory), Box::new(SocketCanFactory)];
        #[cfg(not(target_os = "linux"))]
        let factories: Vec<Box<dyn TransportFactory>> = Vec::new();
        Mutex::new(factories)
    })
}

/// Register an external transport factory.
///
/// The factory will be included in all subsequent calls to
/// [`get_factories()`], [`create_transports()`], and singleton creation.
///
/// # Example
///
/// ```
/// use moteus::transport::factory::{register, TransportFactory, TransportOptions};
/// use moteus::transport::device::TransportDevice;
///
/// struct Pi3HatFactory;
/// impl TransportFactory for Pi3HatFactory {
///     fn priority(&self) -> u32 { 5 }
///     fn name(&self) -> &'static str { "pi3hat" }
///     fn create(&self, opts: &TransportOptions)
///         -> Result<Vec<Box<dyn TransportDevice>>, moteus::Error>
///     {
///         Ok(vec![])  // placeholder
///     }
/// }
///
/// register(Box::new(Pi3HatFactory));
/// ```
pub fn register(factory: Box<dyn TransportFactory>) {
    get_registry().lock().unwrap().push(factory);
}

/// Get the built-in transport factories.
///
/// Returns fresh instances of the built-in factories. For the full set
/// of registered factories (including external ones), use
/// [`create_transports()`] which reads the global registry.
pub fn get_factories() -> Vec<Box<dyn TransportFactory>> {
    #[cfg(target_os = "linux")]
    {
        vec![
            Box::new(FdcanusbFactory::new()),
            Box::new(SocketCanFactory::new()),
        ]
    }
    #[cfg(not(target_os = "linux"))]
    {
        Vec::new()
    }
}

/// Create transport devices using all registered factories.
///
/// This is the main entry point for creating transports with auto-discovery.
/// It tries each factory in priority order and returns all successfully created
/// devices. Duplicate socketcan interfaces backed by fdcanusb devices are
/// filtered out.
pub fn create_transports(options: &TransportOptions) -> Result<Vec<Box<dyn TransportDevice>>> {
    use std::collections::HashSet;

    let registry = get_registry().lock().unwrap();

    // Collect priorities and filter/sort
    let mut indices: Vec<usize> = (0..registry.len()).collect();
    indices.sort_by_key(|&i| registry[i].priority());

    // Filter by forced transport if specified
    if let Some(ref force) = options.force_transport {
        indices.retain(|&i| registry[i].name() == force.as_str());
    }

    let mut all_devices = Vec::new();
    let mut fdcanusb_serials: HashSet<String> = HashSet::new();

    for &idx in &indices {
        match registry[idx].create(options) {
            Ok(devices) => {
                // Track fdcanusb serial numbers for deduplication
                if registry[idx].name() == "fdcanusb" {
                    for device in &devices {
                        if let Some(serial) = device.info().serial_number.as_ref() {
                            fdcanusb_serials.insert(serial.clone());
                        }
                    }
                }
                all_devices.extend(devices);
            }
            Err(_) => continue,
        }
    }

    // Drop the lock before doing more work
    drop(registry);

    // Deduplicate: remove socketcan interfaces that are backed by fdcanusb
    // devices we're already using via CDC serial
    #[cfg(target_os = "linux")]
    {
        use crate::transport::discovery::detect_socketcan_interfaces;

        let socketcan_infos = detect_socketcan_interfaces();
        let mut filtered_devices = Vec::new();

        for device in all_devices {
            let should_skip = socketcan_infos.iter().any(|info| {
                (device.info().serial_number.as_ref() == Some(&info.interface))
                    && info
                        .fdcanusb_serial
                        .as_ref()
                        .is_some_and(|serial| fdcanusb_serials.contains(serial))
            });

            if !should_skip {
                filtered_devices.push(device);
            }
        }

        all_devices = filtered_devices;
    }

    Ok(all_devices)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_transport_options_builder() {
        let opts = TransportOptions::new()
            .fdcanusb_paths(vec!["/dev/ttyACM0", "/dev/ttyACM1"])
            .socketcan_interfaces(vec!["can0"])
            .disable_brs(true)
            .timeout(Duration::from_millis(200));

        assert_eq!(opts.fdcanusb_paths, vec!["/dev/ttyACM0", "/dev/ttyACM1"]);
        assert_eq!(opts.socketcan_interfaces, vec!["can0"]);
        assert!(opts.disable_brs);
        assert_eq!(opts.timeout, Duration::from_millis(200));
    }

    #[test]
    fn test_transport_options_extra() {
        let opts = TransportOptions::new()
            .extra("pi3hat-cfg", "1=1,2")
            .extra("pi3hat-cfg", "3=3,4");

        assert_eq!(
            opts.extra.get("pi3hat-cfg").unwrap(),
            &vec!["1=1,2".to_string(), "3=3,4".to_string()]
        );
    }

    #[cfg(target_os = "linux")]
    #[test]
    fn test_factory_priorities() {
        let fdcanusb = FdcanusbFactory::new();
        assert_eq!(fdcanusb.priority(), 10);
        let socketcan = SocketCanFactory::new();
        assert!(fdcanusb.priority() < socketcan.priority());
    }

    #[cfg(target_os = "linux")]
    #[test]
    fn test_factory_arg_specs() {
        let fdcanusb = FdcanusbFactory::new();
        let specs = fdcanusb.arg_specs();
        assert_eq!(specs.len(), 1);
        assert_eq!(specs[0].name, "fdcanusb");
    }

    #[cfg(target_os = "linux")]
    #[test]
    fn test_socketcan_factory_arg_specs() {
        let socketcan = SocketCanFactory::new();
        let specs = socketcan.arg_specs();
        assert_eq!(specs.len(), 1);
        assert_eq!(specs[0].name, "can-chan");
    }

    #[test]
    fn test_get_factories() {
        let factories = get_factories();
        assert!(!factories.is_empty());

        let names: Vec<_> = factories.iter().map(|f| f.name()).collect();
        assert!(names.contains(&"fdcanusb"));
    }

    #[test]
    fn test_from_pairs_basic() {
        let opts =
            TransportOptions::from_pairs([("can-chan", "can0"), ("timeout-ms", "200")]).unwrap();
        assert_eq!(opts.socketcan_interfaces, vec!["can0"]);
        assert_eq!(opts.timeout, Duration::from_millis(200));
    }

    #[test]
    fn test_from_pairs_multiple_devices() {
        let opts = TransportOptions::from_pairs([
            ("fdcanusb", "/dev/ttyACM0"),
            ("fdcanusb", "/dev/ttyACM1"),
            ("can-chan", "can0"),
            ("can-chan", "can1"),
        ])
        .unwrap();
        assert_eq!(opts.fdcanusb_paths, vec!["/dev/ttyACM0", "/dev/ttyACM1"]);
        assert_eq!(opts.socketcan_interfaces, vec!["can0", "can1"]);
    }

    #[test]
    fn test_from_pairs_flags() {
        let opts = TransportOptions::from_pairs([
            ("can-disable-brs", "true"),
            ("force-transport", "socketcan"),
        ])
        .unwrap();
        assert!(opts.disable_brs);
        assert_eq!(opts.force_transport, Some("socketcan".to_string()));
    }

    #[test]
    fn test_from_pairs_unknown_key_goes_to_extra() {
        let opts = TransportOptions::from_pairs([("pi3hat-cfg", "1=1,2"), ("custom-opt", "value")])
            .unwrap();
        assert_eq!(
            opts.extra.get("pi3hat-cfg").unwrap(),
            &vec!["1=1,2".to_string()]
        );
        assert_eq!(
            opts.extra.get("custom-opt").unwrap(),
            &vec!["value".to_string()]
        );
    }

    #[test]
    fn test_from_pairs_force_transport_any_value() {
        // force-transport now accepts any value (validated dynamically at creation time)
        let opts = TransportOptions::from_pairs([("force-transport", "pi3hat")]).unwrap();
        assert_eq!(opts.force_transport, Some("pi3hat".to_string()));
    }

    #[test]
    fn test_from_pairs_invalid_timeout() {
        let result = TransportOptions::from_pairs([("timeout-ms", "not_a_number")]);
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("invalid timeout"));
    }
}