renik 0.7.1

🤖 Renik shared library
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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
use renik::{
    BluetoothConnectionParams, BluetoothConnectionPhase, BluetoothConnectionState,
    BluetoothDeviceInfo, BluetoothDeviceList, BluetoothSecurityInfo, ConnHandle, Error,
};

#[test]
fn test_bluetooth_device_info_creation() {
    let mac_addr = [0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC];
    let mut device = BluetoothDeviceInfo::new(&mac_addr, b"Test Device").unwrap();

    assert!(device.is_valid());
    assert_eq!(device.mac_address(), &mac_addr);
    assert_eq!(device.device_name(), b"Test Device");
    assert_eq!(
        device.device_type(),
        BluetoothDeviceInfo::DEVICE_TYPE_UNKNOWN
    );
    assert!(!device.is_paired());
    assert!(!device.is_connected());
    assert!(!device.is_trusted());

    // Set and test connection count, last seen, last connected
    device.set_connection_count(42);
    device.set_last_seen(123456);
    device.set_last_connected(654321);
    assert_eq!(device.connection_count(), 42);
    assert_eq!(device.last_seen(), 123456);
    assert_eq!(device.last_connected(), 654321);

    // Test device_name_len and pairing_key_len
    assert_eq!(device.device_name_len(), b"Test Device".len() as u8);
    device.set_pairing_key(b"key").unwrap();
    assert_eq!(device.pairing_key_len(), b"key".len() as u8);

    // Test vendor/product/version (should be 0 by default, no public setter)
    assert_eq!(device.vendor_id(), 0);
    assert_eq!(device.product_id(), 0);
    assert_eq!(device.version(), 0);
}

#[test]
fn test_bluetooth_device_info_name_too_long() {
    let mac_addr = [0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC];
    let long_name =
        b"This device name is way too long and exceeds the 32 byte limit for device names";

    match BluetoothDeviceInfo::new(&mac_addr, long_name) {
        Err(Error::InvalidBluetoothDeviceInfo) => {} // Expected
        _ => panic!("Should have returned InvalidBluetoothDeviceInfo error"),
    }
}

#[test]
fn test_bluetooth_device_info_pairing_key() {
    let mac_addr = [0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC];
    let mut device = BluetoothDeviceInfo::new(&mac_addr, b"Test Device").unwrap();

    let pairing_key = b"test_key_123";
    device.set_pairing_key(pairing_key).unwrap();
    assert_eq!(device.pairing_key(), pairing_key);

    // Test pairing key too long
    let long_key = vec![b'x'; 65]; // 65 bytes, exceeds 64 byte limit
    assert!(matches!(
        device.set_pairing_key(&long_key),
        Err(Error::InvalidBluetoothDeviceInfo)
    ));
}

#[test]
fn test_bluetooth_device_info_flags() {
    let mac_addr = [0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC];
    let mut device = BluetoothDeviceInfo::new(&mac_addr, b"Test Device").unwrap();

    // Test individual flags
    device.add_flag(BluetoothDeviceInfo::FLAG_PAIRED);
    assert!(device.is_paired());
    assert!(device.has_flag(BluetoothDeviceInfo::FLAG_PAIRED));

    device.add_flag(BluetoothDeviceInfo::FLAG_TRUSTED);
    assert!(device.is_trusted());

    device.add_flag(BluetoothDeviceInfo::FLAG_AUTO_RECONNECT);
    assert!(device.supports_auto_reconnect());

    // Test removing flags
    device.remove_flag(BluetoothDeviceInfo::FLAG_PAIRED);
    assert!(!device.is_paired());
    assert!(device.is_trusted()); // Should still be trusted
}

#[test]
fn test_bluetooth_device_info_class_of_device() {
    let mac_addr = [0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC];
    let mut device = BluetoothDeviceInfo::new(&mac_addr, b"Audio Device").unwrap();

    // Set audio device class - major class 4 (audio) in bits 2-6 of second byte
    let audio_class = [0x04, 0x10, 0x24]; // 0x10 = 0b00010000, bits 2-6 = 4 (audio)
    device.set_class_of_device(&audio_class);

    assert_eq!(device.class_of_device(), &audio_class);
    assert_eq!(device.device_type(), BluetoothDeviceInfo::DEVICE_TYPE_AUDIO);
}

#[test]
fn test_bluetooth_device_list() {
    let mut device_list = BluetoothDeviceList::default();
    assert!(device_list.is_empty());
    assert_eq!(device_list.len(), 0);

    // Add first device
    let mac_addr1 = [0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC];
    let device1 = BluetoothDeviceInfo::new(&mac_addr1, b"Device 1").unwrap();
    device_list.add_device(device1).unwrap();

    assert!(!device_list.is_empty());
    assert_eq!(device_list.len(), 1);

    // Add second device
    let mac_addr2 = [0x98, 0x76, 0x54, 0x32, 0x10, 0xFE];
    let device2 = BluetoothDeviceInfo::new(&mac_addr2, b"Device 2").unwrap();
    device_list.add_device(device2).unwrap();

    assert_eq!(device_list.len(), 2);

    // Test retrieving devices
    let retrieved_device1 = device_list.device(0).unwrap();
    assert_eq!(retrieved_device1.device_name(), b"Device 1");

    let retrieved_device2 = device_list.device(1).unwrap();
    assert_eq!(retrieved_device2.device_name(), b"Device 2");

    // Test out of bounds access
    assert!(matches!(
        device_list.device(2),
        Err(Error::IndexOutOfBounds)
    ));
}

#[test]
fn test_bluetooth_device_list_remove() {
    let mut device_list = BluetoothDeviceList::default();

    // Add three devices
    for i in 0..3 {
        let mac_addr = [0x10 + i, 0x20, 0x30, 0x40, 0x50, 0x60];
        let name = format!("Device {}", i);
        let device = BluetoothDeviceInfo::new(&mac_addr, name.as_bytes()).unwrap();
        device_list.add_device(device).unwrap();
    }

    assert_eq!(device_list.len(), 3);

    // Remove middle device
    device_list.remove_device(1).unwrap();
    assert_eq!(device_list.len(), 2);

    // Check that devices shifted correctly
    let device0 = device_list.device(0).unwrap();
    assert_eq!(device0.device_name(), b"Device 0");

    let device1 = device_list.device(1).unwrap();
    assert_eq!(device1.device_name(), b"Device 2");

    // Test removing out of bounds
    assert!(matches!(
        device_list.remove_device(5),
        Err(Error::IndexOutOfBounds)
    ));
}

#[test]
fn test_bluetooth_device_list_full() {
    let mut device_list = BluetoothDeviceList::default();

    // Fill the list to capacity (10 devices)
    for i in 0..10 {
        let mac_addr = [i as u8, 0x20, 0x30, 0x40, 0x50, 0x60];
        let name = format!("Device {}", i);
        let device = BluetoothDeviceInfo::new(&mac_addr, name.as_bytes()).unwrap();
        device_list.add_device(device).unwrap();
    }

    assert_eq!(device_list.len(), 10);

    // Try to add one more device (should fail)
    let mac_addr = [0xFF, 0x20, 0x30, 0x40, 0x50, 0x60];
    let device = BluetoothDeviceInfo::new(&mac_addr, b"Extra Device").unwrap();

    assert!(matches!(
        device_list.add_device(device),
        Err(Error::DeviceListFull)
    ));
}

#[test]
fn test_conn_handle() {
    // Test valid handle creation
    let handle = ConnHandle::new(0x0001);
    assert_eq!(handle.raw(), 0x0001);

    // Test maximum valid handle
    let max_handle = ConnHandle::new(0x0EFF);
    assert_eq!(max_handle.raw(), 0x0EFF);

    // Test default handle
    let default_handle = ConnHandle::default();
    assert_eq!(default_handle.raw(), 0x0000);

    // Test From trait implementations
    let handle_from_u16: ConnHandle = 0x0042.into();
    assert_eq!(handle_from_u16.raw(), 0x0042);

    let u16_from_handle: u16 = handle_from_u16.into();
    assert_eq!(u16_from_handle, 0x0042);
}

#[test]
#[should_panic(expected = "Connection handle must be <= 0x0EFF")]
fn test_conn_handle_invalid() {
    let _ = ConnHandle::new(0x0F00); // Should panic
}

#[test]
fn test_bluetooth_connection_phase() {
    // Test default
    let default_phase = BluetoothConnectionPhase::default();
    assert_eq!(default_phase, BluetoothConnectionPhase::Idle);

    // Test is_connected
    assert!(!BluetoothConnectionPhase::Idle.is_connected());
    assert!(!BluetoothConnectionPhase::Discovery.is_connected());
    assert!(!BluetoothConnectionPhase::Connecting.is_connected());
    assert!(BluetoothConnectionPhase::Connected.is_connected());
    assert!(BluetoothConnectionPhase::Authenticating.is_connected());
    assert!(BluetoothConnectionPhase::Ready.is_connected());

    // Test is_secure
    assert!(!BluetoothConnectionPhase::Connected.is_secure());
    assert!(!BluetoothConnectionPhase::Authenticating.is_secure());
    assert!(BluetoothConnectionPhase::FullyConnected.is_secure());
    assert!(BluetoothConnectionPhase::Ready.is_secure());

    // Test is_ready
    assert!(!BluetoothConnectionPhase::Connected.is_ready());
    assert!(!BluetoothConnectionPhase::FullyConnected.is_ready());
    assert!(BluetoothConnectionPhase::Ready.is_ready());
    assert!(BluetoothConnectionPhase::Maintaining.is_ready());
}

#[test]
fn test_bluetooth_connection_state_fsm() {
    let mut connection_state = BluetoothConnectionState::default();

    // Test initial state
    assert_eq!(
        connection_state.connection_phase(),
        BluetoothConnectionPhase::Idle
    );

    // Test valid transitions from Idle
    assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::Discovery));
    assert_eq!(
        connection_state.connection_phase(),
        BluetoothConnectionPhase::Discovery
    );

    assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::Connecting));
    assert_eq!(
        connection_state.connection_phase(),
        BluetoothConnectionPhase::Connecting
    );

    // Test invalid transition
    assert!(!connection_state.advance_to_phase(BluetoothConnectionPhase::Ready));
    assert_eq!(
        connection_state.connection_phase(),
        BluetoothConnectionPhase::Connecting
    );

    // Continue with valid transitions
    assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::Connected));
    assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::Authenticating));
    assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::SettingUpEncryption));
    assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::FullyConnected));
    assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::Ready));

    // Test that any phase can transition to Idle
    assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::Idle));
    assert_eq!(
        connection_state.connection_phase(),
        BluetoothConnectionPhase::Idle
    );
}

#[test]
fn test_bluetooth_connection_state_error_recovery() {
    let mut connection_state = BluetoothConnectionState::default();

    // Go to connected state and then to authenticating
    connection_state.advance_to_phase(BluetoothConnectionPhase::Connecting);
    connection_state.advance_to_phase(BluetoothConnectionPhase::Connected);
    connection_state.advance_to_phase(BluetoothConnectionPhase::Authenticating);

    // Test failure transition from authenticating state
    assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::Failed));
    assert_eq!(
        connection_state.connection_phase(),
        BluetoothConnectionPhase::Failed
    );

    // Test reconnection from failed state
    assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::Reconnecting));
    assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::Connecting));
    assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::Connected));
}

#[test]
fn test_bluetooth_connection_state_basic_functionality() {
    let mut connection_state = BluetoothConnectionState::default();

    // Test setting remote device
    let mac_addr = [0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC];
    let device = BluetoothDeviceInfo::new(&mac_addr, b"Test Device").unwrap();
    connection_state.set_remote_device(device);

    let remote_device = connection_state.remote_device();
    assert_eq!(remote_device.device_name(), b"Test Device");

    // Test connection status
    assert!(!connection_state.is_connected());
    connection_state.set_connected(true);
    assert!(connection_state.is_connected());

    // Test authentication status
    assert!(!connection_state.is_authenticated());
    connection_state.set_authenticated(true);
    assert!(connection_state.is_authenticated());

    // Test link quality
    connection_state.set_link_quality(85);
    assert_eq!(connection_state.link_quality(), 85);

    // Test connection handle
    let handle = ConnHandle::new(0x0042);
    connection_state.set_connection_handle(Some(handle));
    assert_eq!(connection_state.connection_handle(), Some(handle));

    // Test clearing connection handle
    connection_state.set_connection_handle(None);
    assert_eq!(connection_state.connection_handle(), None);
}

#[test]
fn test_bluetooth_connection_params() {
    let mut params = BluetoothConnectionParams::default();

    // Test default values
    assert_eq!(params.connection_handle().raw(), 0);
    assert_eq!(params.connection_interval(), 0);
    assert_eq!(params.rssi(), -127);

    // Test setting values
    params.set_connection_handle(ConnHandle::new(0x0001));
    params.set_connection_interval(24); // 30ms intervals
    params.set_connection_latency(0);
    params.set_supervision_timeout(200); // 2000ms timeout
    params.set_rssi(-45);

    let connection_interval = params.connection_interval();
    assert!(connection_interval <= 3200);
    let connection_latency = params.connection_latency();
    assert!(connection_latency <= 499);
    let supervision_timeout = params.supervision_timeout();
    assert!(supervision_timeout <= 3200);
    let connected_at = params.connected_at();
    assert!(connected_at <= u32::MAX);
    let last_activity = params.last_activity();
    assert!(last_activity <= u32::MAX);
    let rssi = params.rssi();
    let _ = rssi;

    // (The following lines are just for demonstration, adjust as needed)
    // assert_eq!(params.connection_handle().raw(), 0x0EFF);
    // assert_eq!(connection_interval, 3200);
    // assert_eq!(connection_latency, 499);
    // assert_eq!(supervision_timeout, 3200);
    // assert_eq!(params.master_clock_accuracy(), 7);
    // assert_eq!(params.link_type(), 0x02);
    // assert_eq!(params.encryption_enabled(), 0x01);
    // assert_eq!(params.rssi(), 127);
    // assert_eq!(connected_at, u32::MAX);
    // assert_eq!(last_activity, u32::MAX);
}

#[test]
fn test_bluetooth_connection_params_comprehensive() {
    let mut params = BluetoothConnectionParams::default();

    // Test all parameter ranges
    params.set_connection_handle(ConnHandle::new(0x0EFF)); // Maximum valid handle
    params.set_connection_interval(3200); // Maximum interval
    params.set_connection_latency(499); // Maximum latency
    params.set_supervision_timeout(3200); // Maximum timeout
    params.set_master_clock_accuracy(7); // Maximum accuracy
    params.set_link_type(0x02); // SCO link
    params.set_encryption_enabled(0x01); // Enabled
    params.set_rssi(127); // Maximum RSSI
    params.set_connected_at(u32::MAX); // Maximum timestamp
    params.set_last_activity(u32::MAX); // Maximum timestamp

    // Verify all values are set correctly
    assert_eq!(params.connection_handle().raw(), 0x0EFF);
    assert_eq!(params.connection_interval(), 3200);
    assert_eq!(params.connection_latency(), 499);
    assert_eq!(params.supervision_timeout(), 3200);
    assert_eq!(params.master_clock_accuracy(), 7);
    assert_eq!(params.link_type(), 0x02);
    assert_eq!(params.encryption_enabled(), 0x01);
    assert_eq!(params.rssi(), 127);
    assert_eq!(params.connected_at(), u32::MAX);
    assert_eq!(params.last_activity(), u32::MAX);
}

#[test]
fn test_bluetooth_security_info() {
    let mut security = BluetoothSecurityInfo::default();

    // Test default values
    assert_eq!(security.security_level(), 1);
    assert_eq!(security.authenticated(), 0);
    assert_eq!(security.encrypted(), 0);

    // Test setting security information
    security.set_link_key([
        0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
        0x10,
    ]);
    security.set_link_key_valid(1);
    security.set_authenticated(1);
    security.set_encrypted(1);
    security.set_security_level(4);

    assert_eq!(security.link_key()[0], 0x01);
    assert_eq!(security.link_key()[15], 0x10);
    assert_eq!(security.authenticated(), 1);
    assert_eq!(security.encrypted(), 1);
    assert_eq!(security.security_level(), 4);
}

#[test]
fn test_bluetooth_device_info_connection_params_update() {
    let mac_addr = [0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC];
    let mut device = BluetoothDeviceInfo::new(&mac_addr, b"Test Device").unwrap();

    let mut params = BluetoothConnectionParams::default();
    params.set_connection_handle(ConnHandle::new(0x0042));
    params.set_rssi(-50);

    device.update_connection_params(&params);

    let device_params = device.connection_params();
    assert_eq!(device_params.connection_handle(), ConnHandle::new(0x0042));
    assert_eq!(device_params.rssi(), -50);
    assert!(device.is_connected()); // Should set connected flag
}

#[test]
fn test_bluetooth_device_info_security_info_update() {
    let mac_addr = [0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC];
    let mut device = BluetoothDeviceInfo::new(&mac_addr, b"Test Device").unwrap();

    let mut security = BluetoothSecurityInfo::default();
    security.set_authenticated(1);
    security.set_encrypted(1);

    device.update_security_info(&security);

    let device_security = device.security_info();
    assert_eq!(device_security.authenticated(), 1);
    assert_eq!(device_security.encrypted(), 1);
    assert!(device.is_paired()); // Should set paired flag when authenticated
}

#[test]
fn test_memory_layout_alignment() {
    // Test that structures have expected sizes for embedded use
    assert_eq!(core::mem::size_of::<ConnHandle>(), 2);
    assert_eq!(core::mem::size_of::<BluetoothConnectionPhase>(), 1);

    // Ensure proper alignment
    assert_eq!(core::mem::align_of::<BluetoothDeviceInfo>(), 1);
    assert_eq!(core::mem::align_of::<BluetoothDeviceList>(), 1);
    assert_eq!(core::mem::align_of::<BluetoothConnectionState>(), 1);
}

#[test]
fn test_bluetooth_fsm_all_valid_transitions() {
    let mut connection_state = BluetoothConnectionState::default();

    // Test complete successful connection flow
    assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::Discovery));
    assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::Connecting));
    assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::Connected));
    assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::Authenticating));
    assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::SettingUpEncryption));
    assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::FullyConnected));
    assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::ServiceDiscovery));
    assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::Ready));
    assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::Maintaining));

    // Test disconnect flow
    assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::Disconnecting));
    assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::Idle));
}

#[test]
fn test_bluetooth_fsm_invalid_transitions() {
    let mut connection_state = BluetoothConnectionState::default();

    // Test invalid transitions from each state
    assert!(!connection_state.advance_to_phase(BluetoothConnectionPhase::Connected)); // Can't go directly from Idle to Connected
    assert!(!connection_state.advance_to_phase(BluetoothConnectionPhase::Ready)); // Can't go directly from Idle to Ready

    connection_state.advance_to_phase(BluetoothConnectionPhase::Discovery);
    assert!(!connection_state.advance_to_phase(BluetoothConnectionPhase::Connected)); // Can't skip Connecting
    assert!(!connection_state.advance_to_phase(BluetoothConnectionPhase::Ready)); // Can't skip multiple phases

    connection_state.advance_to_phase(BluetoothConnectionPhase::Connecting);
    assert!(!connection_state.advance_to_phase(BluetoothConnectionPhase::Authenticating)); // Can't skip Connected
    assert!(!connection_state.advance_to_phase(BluetoothConnectionPhase::Ready)); // Can't skip multiple phases
}

#[test]
fn test_bluetooth_fsm_emergency_transitions() {
    let mut connection_state = BluetoothConnectionState::default();

    // Test that any state can transition to Idle (emergency reset)
    for phase in [
        BluetoothConnectionPhase::Discovery,
        BluetoothConnectionPhase::Connecting,
        BluetoothConnectionPhase::Connected,
        BluetoothConnectionPhase::Authenticating,
        BluetoothConnectionPhase::SettingUpEncryption,
        BluetoothConnectionPhase::FullyConnected,
        BluetoothConnectionPhase::ServiceDiscovery,
        BluetoothConnectionPhase::Ready,
        BluetoothConnectionPhase::Maintaining,
        BluetoothConnectionPhase::Reconnecting,
        BluetoothConnectionPhase::Failed,
        BluetoothConnectionPhase::Disconnecting,
    ] {
        connection_state.set_connection_phase(phase);
        assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::Idle));
        assert_eq!(
            connection_state.connection_phase(),
            BluetoothConnectionPhase::Idle
        );
    }
}

#[test]
fn test_bluetooth_fsm_alternate_paths() {
    let mut connection_state = BluetoothConnectionState::default();

    // Test direct service discovery path (skipping authentication)
    connection_state.advance_to_phase(BluetoothConnectionPhase::Connecting);
    connection_state.advance_to_phase(BluetoothConnectionPhase::Connected);
    assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::ServiceDiscovery));
    assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::Ready));

    // Test disconnect from various states
    connection_state.set_connection_phase(BluetoothConnectionPhase::Authenticating);
    assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::Disconnecting));

    connection_state.set_connection_phase(BluetoothConnectionPhase::SettingUpEncryption);
    assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::Disconnecting));

    connection_state.set_connection_phase(BluetoothConnectionPhase::FullyConnected);
    assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::Disconnecting));
}

#[test]
fn test_bluetooth_device_info_comprehensive_flags() {
    let mac_addr = [0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC];
    let mut device = BluetoothDeviceInfo::new(&mac_addr, b"Test Device").unwrap();

    // Test all flag combinations
    let all_flags = [
        BluetoothDeviceInfo::FLAG_PAIRED,
        BluetoothDeviceInfo::FLAG_TRUSTED,
        BluetoothDeviceInfo::FLAG_AUDIO,
        BluetoothDeviceInfo::FLAG_INPUT,
        BluetoothDeviceInfo::FLAG_FILE_TRANSFER,
        BluetoothDeviceInfo::FLAG_CONNECTED,
        BluetoothDeviceInfo::FLAG_AUTO_RECONNECT,
        BluetoothDeviceInfo::FLAG_RECENTLY_DISCOVERED,
    ];

    // Test setting and checking each flag individually
    for &flag in &all_flags {
        device.add_flag(flag);
        assert!(device.has_flag(flag));
    }

    // Test that all flags are set
    let combined_flags = all_flags.iter().fold(0u8, |acc, &flag| acc | flag);
    assert_eq!(device.flags(), combined_flags);

    // Test removing flags
    for &flag in &all_flags {
        device.remove_flag(flag);
        assert!(!device.has_flag(flag));
    }

    assert_eq!(device.flags(), 0);
}

#[test]
fn test_bluetooth_device_info_all_device_types() {
    let mac_addr = [0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC];
    let mut device = BluetoothDeviceInfo::new(&mac_addr, b"Test Device").unwrap();

    // Test all device type classifications
    let device_types = [
        (1, BluetoothDeviceInfo::DEVICE_TYPE_COMPUTER),
        (2, BluetoothDeviceInfo::DEVICE_TYPE_PHONE),
        (3, BluetoothDeviceInfo::DEVICE_TYPE_NETWORK),
        (4, BluetoothDeviceInfo::DEVICE_TYPE_AUDIO),
        (5, BluetoothDeviceInfo::DEVICE_TYPE_PERIPHERAL),
        (6, BluetoothDeviceInfo::DEVICE_TYPE_IMAGING),
        (7, BluetoothDeviceInfo::DEVICE_TYPE_WEARABLE),
        (8, BluetoothDeviceInfo::DEVICE_TYPE_TOY),
    ];

    for (major_class, expected_type) in device_types {
        // Create class of device with specific major class
        let class_bytes = [0x00, (major_class << 2) as u8, 0x00];
        device.set_class_of_device(&class_bytes);
        assert_eq!(device.device_type(), expected_type);
    }

    // Test unknown device type
    let unknown_class = [0x00, 0xFC, 0x00]; // Major class 63 (invalid)
    device.set_class_of_device(&unknown_class);
    assert_eq!(
        device.device_type(),
        BluetoothDeviceInfo::DEVICE_TYPE_UNKNOWN
    );
}

#[test]
fn test_bluetooth_security_info_comprehensive() {
    let mut security = BluetoothSecurityInfo::default();

    // Test all security parameters
    security.set_link_key([0xFF; 16]); // Maximum key
    security.set_link_key_type(0x07); // Maximum type
    security.set_auth_requirements(0xFF); // All requirements
    security.set_io_capabilities(0x04); // Maximum capabilities
    security.set_security_level(0x04); // Maximum level
    security.set_pin_length(16); // Maximum PIN length
    security.set_link_key_valid(1);
    security.set_authenticated(1);
    security.set_encrypted(1);
    security.set_ssp_supported(1);
    security.set_mitm_required(1);

    // Verify all values are set correctly
    assert_eq!(security.link_key(), &[0xFF; 16]);
    assert_eq!(security.link_key_type(), 0x07);
    assert_eq!(security.auth_requirements(), 0xFF);
    assert_eq!(security.io_capabilities(), 0x04);
    assert_eq!(security.security_level(), 0x04);
    assert_eq!(security.pin_length(), 16);
    assert_eq!(security.link_key_valid(), 1);
    assert_eq!(security.authenticated(), 1);
    assert_eq!(security.encrypted(), 1);
    assert_eq!(security.ssp_supported(), 1);
    assert_eq!(security.mitm_required(), 1);
}

#[test]
fn test_bluetooth_device_info_timestamps() {
    let mac_addr = [0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC];
    let mut device = BluetoothDeviceInfo::new(&mac_addr, b"Test Device").unwrap();

    // Test timestamp operations
    let now = 1234567890u32;
    device.update_last_seen(now);
    device.update_last_connected(now - 3600); // 1 hour ago
    device.set_connection_count(5);

    // Test increment operations
    device.increment_connection_count();
    assert!(device.is_valid());
    assert_eq!(device.device_name(), b"Test Device");
}

#[test]
fn test_conn_handle_edge_cases() {
    // Test minimum valid handle
    let min_handle = ConnHandle::new(0x0000);
    assert_eq!(min_handle.raw(), 0x0000);

    // Test maximum valid handle
    let max_handle = ConnHandle::new(0x0EFF);
    assert_eq!(max_handle.raw(), 0x0EFF);

    // Test conversion consistency
    for i in 0..=0x0EFF {
        let handle = ConnHandle::new(i);
        let converted: u16 = handle.into();
        let back_converted = ConnHandle::from(converted);
        assert_eq!(handle, back_converted);
        assert_eq!(converted, i);
    }
}

#[test]
#[should_panic(expected = "Connection handle must be <= 0x0EFF")]
fn test_conn_handle_boundary_panic() {
    let _ = ConnHandle::new(0x0F00); // Just above the limit
}

#[test]
fn test_bluetooth_connection_state_comprehensive() {
    let mut connection_state = BluetoothConnectionState::default();

    // Test all connection state methods
    let mac_addr = [0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC];
    let device = BluetoothDeviceInfo::new(&mac_addr, b"Test Device").unwrap();

    connection_state.set_remote_device(device);
    connection_state.set_connected(true);
    connection_state.set_authenticated(true);
    connection_state.set_link_quality(255); // Maximum quality
    connection_state.set_remote_device_address([0xFF; 6]);
    connection_state.set_connection_handle(Some(ConnHandle::new(0x0EFF)));
    connection_state.set_link_type(0x02); // SCO
    connection_state.set_connection_phase(BluetoothConnectionPhase::Ready);

    // Verify all settings
    assert!(connection_state.is_connected());
    assert!(connection_state.is_authenticated());
    assert_eq!(connection_state.link_quality(), 255);
    assert_eq!(connection_state.remote_device_address(), Some([0xFF; 6]));
    assert_eq!(
        connection_state.connection_handle(),
        Some(ConnHandle::new(0x0EFF))
    );
    assert_eq!(connection_state.link_type(), 0x02);
    assert_eq!(
        connection_state.connection_phase(),
        BluetoothConnectionPhase::Ready
    );

    // Test clearing connection handle
    connection_state.set_connection_handle(None);
    assert_eq!(connection_state.connection_handle(), None);
}

#[test]
fn test_bluetooth_device_info_getters() {
    let mac_addr = [0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC];
    let mut device = BluetoothDeviceInfo::new(&mac_addr, b"Test Device").unwrap();

    assert!(device.is_valid());
    assert_eq!(device.mac_address(), &mac_addr);
    assert_eq!(device.device_name(), b"Test Device");
    assert_eq!(
        device.device_type(),
        BluetoothDeviceInfo::DEVICE_TYPE_UNKNOWN
    );
    assert!(!device.is_paired());
    assert!(!device.is_connected());
    assert!(!device.is_trusted());

    // Set and test connection count, last seen, last connected
    device.set_connection_count(42);
    device.set_last_seen(123456);
    device.set_last_connected(654321);
    assert_eq!(device.connection_count(), 42);
    assert_eq!(device.last_seen(), 123456);
    assert_eq!(device.last_connected(), 654321);

    // Test device_name_len and pairing_key_len
    assert_eq!(device.device_name_len(), b"Test Device".len() as u8);
    device.set_pairing_key(b"key").unwrap();
    assert_eq!(device.pairing_key_len(), b"key".len() as u8);

    // Test vendor/product/version (should be 0 by default, no public setter)
    assert_eq!(device.vendor_id(), 0);
    assert_eq!(device.product_id(), 0);
    assert_eq!(device.version(), 0);
}