timex_datalink/protocol_3/
sync.rs

1//! Sync implementation for Protocol 3
2//!
3//! This module handles the synchronization protocol for Timex Datalink watches.
4
5use crate::PacketGenerator;
6
7/// Sync structure for Protocol 3
8pub struct Sync {
9    /// Number of SYNC_1_BYTE to use
10    pub length: usize,
11}
12
13impl Default for Sync {
14    fn default() -> Self {
15        Self { length: 300 }
16    }
17}
18
19impl PacketGenerator for Sync {
20    fn packets(&self) -> Vec<Vec<u8>> {
21        // Define constants matching Ruby implementation
22        const PING_BYTE: u8 = 0x78;
23        const SYNC_1_BYTE: u8 = 0x55;
24        const SYNC_2_BYTE: u8 = 0xaa;
25        const SYNC_2_LENGTH: usize = 40;
26
27        // Create a vector to hold our bytes
28        let mut packet = Vec::with_capacity(1 + self.length + SYNC_2_LENGTH);
29        
30        // Add ping byte
31        packet.push(PING_BYTE);
32        
33        // Add SYNC_1 bytes
34        packet.extend(vec![SYNC_1_BYTE; self.length]);
35        
36        // Add SYNC_2 bytes
37        packet.extend(vec![SYNC_2_BYTE; SYNC_2_LENGTH]);
38        
39        vec![packet]
40    }
41}
42
43#[cfg(test)]
44mod tests {
45    use super::*;
46
47    #[test]
48    fn test_sync() {
49        let sync = Sync::default();
50        
51        // From golden fixture: sync.jsonl
52        #[rustfmt::skip]
53        let expected = vec![vec![120,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170,170]];
54
55        assert_eq!(sync.packets(), expected);
56    }
57}