hisi-rf-core 0.1.0-alpha.24

Chip-neutral async radio controller contracts for HiSilicon embedded Rust
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
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
//! Chip-neutral Bluetooth Low Energy GAP configuration contracts.

const LEGACY_ADV_DATA_CAPACITY: usize = 31;

/// Address kind carried by a BLE GAP operation.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum AddressType {
    /// Controller-assigned public device address.
    Public,
    /// Static random device address.
    RandomStatic,
    /// Resolvable private address whose identity may be resolved by an IRK.
    ResolvablePrivate,
    /// Non-resolvable private address used only as an ephemeral identity.
    NonResolvablePrivate,
}

/// Measured receive signal strength in dBm.
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct RssiDbm(i8);

impl RssiDbm {
    /// Convert a controller measurement, treating the Bluetooth `0x7f`
    /// sentinel as unavailable.
    pub const fn from_controller(value: i8) -> Option<Self> {
        if value == 0x7f {
            None
        } else {
            Some(Self(value))
        }
    }

    /// Return the signed dBm measurement.
    pub const fn get(self) -> i8 {
        self.0
    }
}

/// Controller-reported BLE link termination reason.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct DisconnectReason(u32);

impl DisconnectReason {
    /// Preserve one vendor reason without making raw values part of control flow.
    pub const fn from_vendor(value: u32) -> Self {
        Self(value)
    }

    /// Return the diagnostic vendor representation.
    pub const fn vendor_code(self) -> u32 {
        self.0
    }
}

/// Validated BLE device address.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct BluetoothAddress {
    bytes: [u8; 6],
    kind: AddressType,
}

impl BluetoothAddress {
    /// Validate a non-zero public device address.
    pub const fn public(bytes: [u8; 6]) -> Option<Self> {
        if all_equal(bytes, 0) {
            None
        } else {
            Some(Self {
                bytes,
                kind: AddressType::Public,
            })
        }
    }

    /// Validate a static random address according to the two most-significant bits.
    pub const fn random_static(bytes: [u8; 6]) -> Option<Self> {
        let random_part_is_zero = bytes[0] == 0
            && bytes[1] == 0
            && bytes[2] == 0
            && bytes[3] == 0
            && bytes[4] == 0
            && bytes[5] & 0x3f == 0;
        let random_part_is_one = bytes[0] == 0xff
            && bytes[1] == 0xff
            && bytes[2] == 0xff
            && bytes[3] == 0xff
            && bytes[4] == 0xff
            && bytes[5] & 0x3f == 0x3f;
        if bytes[5] & 0xc0 != 0xc0 || random_part_is_zero || random_part_is_one {
            None
        } else {
            Some(Self {
                bytes,
                kind: AddressType::RandomStatic,
            })
        }
    }

    /// Classify and validate a controller-reported random device address.
    ///
    /// The two most-significant bits distinguish non-resolvable private,
    /// resolvable private, and static random addresses. The reserved `10`
    /// pattern is rejected.
    pub const fn random(bytes: [u8; 6]) -> Option<Self> {
        match bytes[5] & 0xc0 {
            0xc0 => Self::random_static(bytes),
            0x40 => {
                let random_part =
                    bytes[3] as u32 | ((bytes[4] as u32) << 8) | (((bytes[5] & 0x3f) as u32) << 16);
                if random_part == 0 || random_part == 0x3f_ffff {
                    None
                } else {
                    Some(Self {
                        bytes,
                        kind: AddressType::ResolvablePrivate,
                    })
                }
            }
            0x00 => {
                if all_equal(bytes, 0) {
                    None
                } else {
                    Some(Self {
                        bytes,
                        kind: AddressType::NonResolvablePrivate,
                    })
                }
            }
            _ => None,
        }
    }

    /// Return the address bytes in controller byte order.
    pub const fn bytes(self) -> [u8; 6] {
        self.bytes
    }

    /// Return the validated address kind.
    pub const fn address_type(self) -> AddressType {
        self.kind
    }
}

/// Whether successful pairing should create a persistent bond.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Bonding {
    /// Encrypt the active link without retaining long-term peer state.
    Disabled,
    /// Retain the peer relationship through an injected bond store.
    Enabled,
}

/// Local input/output capabilities used by Bluetooth pairing association.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum IoCapability {
    /// The device can display a value but cannot confirm or enter one.
    DisplayOnly,
    /// The device can display a value and accept a yes/no confirmation.
    DisplayYesNo,
    /// The device can enter a value but cannot display one.
    KeyboardOnly,
    /// The device has no pairing input or output capability.
    NoInputNoOutput,
    /// The device can both display and enter a value.
    KeyboardDisplay,
}

/// Minimum link-security property requested from the BLE host.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum SecurityRequirement {
    /// Unauthenticated pairing followed by link encryption.
    Encrypted,
    /// Authenticated pairing followed by link encryption.
    Authenticated,
    /// Authenticated LE Secure Connections pairing and encryption.
    SecureConnectionsAuthenticated,
}

/// Validated BLE pairing policy.
///
/// The chip adapter maps this semantic policy to its own GAP security values;
/// raw controller encodings and key bytes never cross this boundary.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct SecurityConfig {
    bonding: Bonding,
    io_capability: IoCapability,
    requirement: SecurityRequirement,
}

impl SecurityConfig {
    /// Construct an explicit pairing policy.
    pub const fn new(
        bonding: Bonding,
        io_capability: IoCapability,
        requirement: SecurityRequirement,
    ) -> Self {
        Self {
            bonding,
            io_capability,
            requirement,
        }
    }

    /// Whether a successful pairing should be retained.
    pub const fn bonding(self) -> Bonding {
        self.bonding
    }

    /// Local association-model capability.
    pub const fn io_capability(self) -> IoCapability {
        self.io_capability
    }

    /// Minimum security property required for completion.
    pub const fn requirement(self) -> SecurityRequirement {
        self.requirement
    }
}

/// Pairing lifecycle state reported without exposing vendor encodings.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum PairingState {
    /// The peer has no active or completed pairing relationship.
    NotPaired,
    /// A pairing operation is in progress.
    Pairing,
    /// Pairing completed successfully for the active peer.
    Paired,
}

/// Validated six-digit Bluetooth pairing passkey.
///
/// The numeric value is deliberately omitted from [`core::fmt::Debug`] output
/// so diagnostics cannot accidentally disclose an active pairing secret.
#[derive(Clone, Copy, Eq, PartialEq)]
pub struct Passkey(u32);

impl Passkey {
    /// Largest value representable by the Bluetooth six-digit passkey field.
    pub const MAX: u32 = 999_999;

    /// Validate a passkey received from a trusted UI or controller prompt.
    pub const fn try_new(value: u32) -> Option<Self> {
        if value <= Self::MAX {
            Some(Self(value))
        } else {
            None
        }
    }

    /// Return the validated numeric value for a chip integration adapter.
    #[doc(hidden)]
    pub const fn as_u32(self) -> u32 {
        self.0
    }
}

impl core::fmt::Debug for Passkey {
    fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        formatter.write_str("Passkey([REDACTED])")
    }
}

const fn all_equal(bytes: [u8; 6], value: u8) -> bool {
    bytes[0] == value
        && bytes[1] == value
        && bytes[2] == value
        && bytes[3] == value
        && bytes[4] == value
        && bytes[5] == value
}

/// Legacy advertising interval in 0.625 ms controller units.
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct AdvertisingInterval(u16);

impl AdvertisingInterval {
    /// Validate the Bluetooth legacy-advertising interval range.
    pub const fn try_from_units(units: u16) -> Option<Self> {
        if units < 0x20 || units > 0x4000 {
            None
        } else {
            Some(Self(units))
        }
    }

    /// Return the 0.625 ms controller units.
    pub const fn as_units(self) -> u16 {
        self.0
    }
}

/// Ordered advertising interval range.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct AdvertisingTiming {
    minimum: AdvertisingInterval,
    maximum: AdvertisingInterval,
}

impl AdvertisingTiming {
    /// Validate that the minimum does not exceed the maximum.
    pub const fn try_new(
        minimum: AdvertisingInterval,
        maximum: AdvertisingInterval,
    ) -> Option<Self> {
        if minimum.0 > maximum.0 {
            None
        } else {
            Some(Self { minimum, maximum })
        }
    }

    /// Smallest selected interval.
    pub const fn minimum(self) -> AdvertisingInterval {
        self.minimum
    }

    /// Largest selected interval.
    pub const fn maximum(self) -> AdvertisingInterval {
        self.maximum
    }
}

/// Non-empty subset of the three primary advertising channels.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct AdvertisingChannels(u8);

impl AdvertisingChannels {
    /// Channels 37, 38, and 39.
    pub const ALL: Self = Self(0x07);

    /// Validate a controller channel bitmap.
    pub const fn try_from_bits(bits: u8) -> Option<Self> {
        if bits == 0 || bits & !0x07 != 0 {
            None
        } else {
            Some(Self(bits))
        }
    }

    /// Return the three-bit controller bitmap.
    pub const fn bits(self) -> u8 {
        self.0
    }
}

/// Owned, bounded legacy advertising payload.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct AdvertisingPayload {
    bytes: [u8; LEGACY_ADV_DATA_CAPACITY],
    len: u8,
}

impl AdvertisingPayload {
    /// Copy at most 31 bytes into fixed-capacity storage.
    pub fn try_from_slice(value: &[u8]) -> Option<Self> {
        if value.len() > LEGACY_ADV_DATA_CAPACITY {
            return None;
        }
        let mut bytes = [0; LEGACY_ADV_DATA_CAPACITY];
        bytes[..value.len()].copy_from_slice(value);
        Some(Self {
            bytes,
            len: value.len() as u8,
        })
    }

    /// Exact payload bytes.
    pub fn as_bytes(&self) -> &[u8] {
        &self.bytes[..self.len as usize]
    }
}

/// Typed legacy advertising request.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct AdvertisingConfig {
    timing: AdvertisingTiming,
    channels: AdvertisingChannels,
    payload: AdvertisingPayload,
}

impl AdvertisingConfig {
    /// Construct a request from independently validated fields.
    pub const fn new(
        timing: AdvertisingTiming,
        channels: AdvertisingChannels,
        payload: AdvertisingPayload,
    ) -> Self {
        Self {
            timing,
            channels,
            payload,
        }
    }

    /// Selected timing range.
    pub const fn timing(self) -> AdvertisingTiming {
        self.timing
    }

    /// Selected primary channels.
    pub const fn channels(self) -> AdvertisingChannels {
        self.channels
    }

    /// Borrow the bounded payload.
    pub const fn payload(&self) -> &AdvertisingPayload {
        &self.payload
    }
}

/// Scan interval or window in 0.625 ms controller units.
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct ScanInterval(u16);

impl ScanInterval {
    /// Validate the controller scan timing range.
    pub const fn try_from_units(units: u16) -> Option<Self> {
        if units < 0x0004 || units > 0x4000 {
            None
        } else {
            Some(Self(units))
        }
    }

    /// Return the 0.625 ms controller units.
    pub const fn as_units(self) -> u16 {
        self.0
    }
}

/// Scan interval and window with `window <= interval` guaranteed.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ScanTiming {
    interval: ScanInterval,
    window: ScanInterval,
}

impl ScanTiming {
    /// Validate one scan timing pair.
    pub const fn try_new(interval: ScanInterval, window: ScanInterval) -> Option<Self> {
        if window.0 > interval.0 {
            None
        } else {
            Some(Self { interval, window })
        }
    }

    /// Repetition interval.
    pub const fn interval(self) -> ScanInterval {
        self.interval
    }

    /// Active scan window.
    pub const fn window(self) -> ScanInterval {
        self.window
    }
}

/// Host scan behavior.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ScanMode {
    /// Listen without sending scan requests.
    Passive,
    /// Send scan requests when the peer supports them.
    Active,
}

/// Typed BLE scan request.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ScanConfig {
    timing: ScanTiming,
    mode: ScanMode,
    filter_duplicates: bool,
}

impl ScanConfig {
    /// Construct a request from validated timing and explicit behavior.
    pub const fn new(timing: ScanTiming, mode: ScanMode, filter_duplicates: bool) -> Self {
        Self {
            timing,
            mode,
            filter_duplicates,
        }
    }

    /// Selected scan timing.
    pub const fn timing(self) -> ScanTiming {
        self.timing
    }

    /// Active or passive behavior.
    pub const fn mode(self) -> ScanMode {
        self.mode
    }

    /// Whether duplicate reports should be filtered.
    pub const fn filter_duplicates(self) -> bool {
        self.filter_duplicates
    }
}

/// Bluetooth GATT UUID without controller-specific byte layout.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum GattUuid {
    /// Bluetooth 16-bit UUID.
    Uuid16(u16),
    /// Full 128-bit UUID in network byte order.
    Uuid128([u8; 16]),
}

/// Valid GATT attribute permissions.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct GattPermissions(u8);

impl GattPermissions {
    /// Attribute can be read by the peer.
    pub const READ: Self = Self(1 << 0);
    /// Attribute can be written by the peer.
    pub const WRITE: Self = Self(1 << 1);

    /// Combine independently named permissions.
    pub const fn union(self, other: Self) -> Self {
        Self(self.0 | other.0)
    }

    /// Test whether all requested permissions are present.
    pub const fn contains(self, other: Self) -> bool {
        self.0 & other.0 == other.0
    }

    /// Return the reviewed portable permission bitmap for a chip adapter.
    #[doc(hidden)]
    pub const fn __bits(self) -> u8 {
        self.0
    }
}

/// Valid GATT characteristic operations.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct GattProperties(u8);

impl GattProperties {
    /// Peer may read the characteristic value.
    pub const READ: Self = Self(1 << 0);
    /// Peer may write and receive a response.
    pub const WRITE: Self = Self(1 << 1);
    /// Peer may write without a response.
    pub const WRITE_WITHOUT_RESPONSE: Self = Self(1 << 2);
    /// Server may send notifications.
    pub const NOTIFY: Self = Self(1 << 3);
    /// Server may send acknowledged indications.
    pub const INDICATE: Self = Self(1 << 4);

    /// Combine independently named properties.
    pub const fn union(self, other: Self) -> Self {
        Self(self.0 | other.0)
    }

    /// Test whether all requested properties are present.
    pub const fn contains(self, other: Self) -> bool {
        self.0 & other.0 == other.0
    }

    /// Return the reviewed portable property bitmap for a chip adapter.
    #[doc(hidden)]
    pub const fn __bits(self) -> u8 {
        self.0
    }
}

/// Static GATT descriptor definition.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct GattDescriptorDefinition {
    uuid: GattUuid,
    permissions: GattPermissions,
    initial_value: &'static [u8],
    maximum_len: u16,
}

impl GattDescriptorDefinition {
    /// Validate a descriptor's initial and maximum value lengths.
    pub const fn try_new(
        uuid: GattUuid,
        permissions: GattPermissions,
        initial_value: &'static [u8],
        maximum_len: u16,
    ) -> Option<Self> {
        if maximum_len == 0 || initial_value.len() > maximum_len as usize {
            None
        } else {
            Some(Self {
                uuid,
                permissions,
                initial_value,
                maximum_len,
            })
        }
    }

    /// Descriptor UUID.
    pub const fn uuid(self) -> GattUuid {
        self.uuid
    }

    /// Peer permissions.
    pub const fn permissions(self) -> GattPermissions {
        self.permissions
    }

    /// Initial descriptor bytes copied into backend-owned storage.
    pub const fn initial_value(self) -> &'static [u8] {
        self.initial_value
    }

    /// Maximum accepted value length.
    pub const fn maximum_len(self) -> u16 {
        self.maximum_len
    }
}

/// Static GATT characteristic definition.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct GattCharacteristicDefinition {
    uuid: GattUuid,
    permissions: GattPermissions,
    properties: GattProperties,
    initial_value: &'static [u8],
    maximum_len: u16,
    descriptors: &'static [GattDescriptorDefinition],
}

impl GattCharacteristicDefinition {
    /// Validate value capacity and the notification/indication CCC contract.
    pub const fn try_new(
        uuid: GattUuid,
        permissions: GattPermissions,
        properties: GattProperties,
        initial_value: &'static [u8],
        maximum_len: u16,
        descriptors: &'static [GattDescriptorDefinition],
    ) -> Option<Self> {
        if maximum_len == 0 || initial_value.len() > maximum_len as usize {
            return None;
        }
        let publishes = properties.contains(GattProperties::NOTIFY)
            || properties.contains(GattProperties::INDICATE);
        if publishes && !has_ccc_descriptor(descriptors) {
            return None;
        }
        Some(Self {
            uuid,
            permissions,
            properties,
            initial_value,
            maximum_len,
            descriptors,
        })
    }

    /// Characteristic UUID.
    pub const fn uuid(self) -> GattUuid {
        self.uuid
    }

    /// Peer permissions.
    pub const fn permissions(self) -> GattPermissions {
        self.permissions
    }

    /// Supported GATT operations.
    pub const fn properties(self) -> GattProperties {
        self.properties
    }

    /// Initial value copied into backend-owned storage.
    pub const fn initial_value(self) -> &'static [u8] {
        self.initial_value
    }

    /// Maximum accepted value length.
    pub const fn maximum_len(self) -> u16 {
        self.maximum_len
    }

    /// Static descriptor definitions.
    pub const fn descriptors(self) -> &'static [GattDescriptorDefinition] {
        self.descriptors
    }
}

const fn has_ccc_descriptor(descriptors: &[GattDescriptorDefinition]) -> bool {
    let mut index = 0;
    while index < descriptors.len() {
        if matches!(descriptors[index].uuid, GattUuid::Uuid16(0x2902)) {
            return true;
        }
        index += 1;
    }
    false
}

/// Static GATT service definition.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct GattServiceDefinition {
    uuid: GattUuid,
    primary: bool,
    characteristics: &'static [GattCharacteristicDefinition],
}

impl GattServiceDefinition {
    /// Require at least one characteristic in a service.
    pub const fn try_new(
        uuid: GattUuid,
        primary: bool,
        characteristics: &'static [GattCharacteristicDefinition],
    ) -> Option<Self> {
        if characteristics.is_empty() {
            None
        } else {
            Some(Self {
                uuid,
                primary,
                characteristics,
            })
        }
    }

    /// Service UUID.
    pub const fn uuid(self) -> GattUuid {
        self.uuid
    }

    /// Whether this is a primary service.
    pub const fn is_primary(self) -> bool {
        self.primary
    }

    /// Static characteristic definitions.
    pub const fn characteristics(self) -> &'static [GattCharacteristicDefinition] {
        self.characteristics
    }
}

/// Complete caller-owned static GATT server database.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct GattServerDefinition {
    app_uuid: GattUuid,
    services: &'static [GattServiceDefinition],
}

impl GattServerDefinition {
    /// Require at least one service in the static database.
    pub const fn try_new(
        app_uuid: GattUuid,
        services: &'static [GattServiceDefinition],
    ) -> Option<Self> {
        if services.is_empty() {
            None
        } else {
            Some(Self { app_uuid, services })
        }
    }

    /// Application UUID used to register the server.
    pub const fn app_uuid(self) -> GattUuid {
        self.app_uuid
    }

    /// Static service definitions.
    pub const fn services(self) -> &'static [GattServiceDefinition] {
        self.services
    }
}

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

    #[test]
    fn rejects_invalid_addresses_and_timing() {
        assert!(BluetoothAddress::public([0; 6]).is_none());
        assert!(BluetoothAddress::random_static([1, 2, 3, 4, 5, 0x80]).is_none());
        assert!(BluetoothAddress::random_static([0, 0, 0, 0, 0, 0xc0]).is_none());
        assert!(BluetoothAddress::random_static([0xff; 6]).is_none());
        assert!(BluetoothAddress::random([1, 2, 3, 4, 5, 0x80]).is_none());
        assert!(BluetoothAddress::random([0; 6]).is_none());
        assert!(AdvertisingInterval::try_from_units(0x1f).is_none());
        let interval = ScanInterval::try_from_units(0x20).unwrap();
        let window = ScanInterval::try_from_units(0x30).unwrap();
        assert!(ScanTiming::try_new(interval, window).is_none());
    }

    #[test]
    fn classifies_controller_random_addresses() {
        let static_address = BluetoothAddress::random([1, 2, 3, 4, 5, 0xc0]).unwrap();
        assert_eq!(static_address.address_type(), AddressType::RandomStatic);

        let resolvable = BluetoothAddress::random([1, 2, 3, 4, 5, 0x40]).unwrap();
        assert_eq!(resolvable.address_type(), AddressType::ResolvablePrivate);

        let non_resolvable = BluetoothAddress::random([1, 2, 3, 4, 5, 0x00]).unwrap();
        assert_eq!(
            non_resolvable.address_type(),
            AddressType::NonResolvablePrivate
        );
    }

    #[test]
    fn bounds_legacy_advertising_data() {
        assert!(AdvertisingPayload::try_from_slice(&[0; 31]).is_some());
        assert!(AdvertisingPayload::try_from_slice(&[0; 32]).is_none());
        assert!(AdvertisingChannels::try_from_bits(0).is_none());
        assert!(AdvertisingChannels::try_from_bits(0x08).is_none());
    }

    #[test]
    fn security_config_preserves_explicit_policy() {
        const CONFIG: SecurityConfig = SecurityConfig::new(
            Bonding::Enabled,
            IoCapability::DisplayYesNo,
            SecurityRequirement::SecureConnectionsAuthenticated,
        );

        assert_eq!(CONFIG.bonding(), Bonding::Enabled);
        assert_eq!(CONFIG.io_capability(), IoCapability::DisplayYesNo);
        assert_eq!(
            CONFIG.requirement(),
            SecurityRequirement::SecureConnectionsAuthenticated
        );
        assert_eq!(PairingState::NotPaired, PairingState::NotPaired);
    }

    #[test]
    fn passkey_is_bounded_and_redacted() {
        assert_eq!(Passkey::try_new(0).unwrap().as_u32(), 0);
        assert_eq!(Passkey::try_new(Passkey::MAX).unwrap().as_u32(), 999_999);
        assert!(Passkey::try_new(Passkey::MAX + 1).is_none());
        assert_eq!(
            std::format!("{:?}", Passkey::try_new(123_456).unwrap()),
            "Passkey([REDACTED])"
        );
    }

    #[test]
    fn validates_static_gatt_database_relations() {
        const CCC: GattDescriptorDefinition = GattDescriptorDefinition::try_new(
            GattUuid::Uuid16(0x2902),
            GattPermissions::READ.union(GattPermissions::WRITE),
            &[0, 0],
            2,
        )
        .unwrap();
        const CHARACTERISTIC: GattCharacteristicDefinition = GattCharacteristicDefinition::try_new(
            GattUuid::Uuid16(0xabcd),
            GattPermissions::READ.union(GattPermissions::WRITE),
            GattProperties::READ
                .union(GattProperties::WRITE)
                .union(GattProperties::NOTIFY),
            b"U3",
            16,
            &[CCC],
        )
        .unwrap();
        const SERVICE: GattServiceDefinition =
            GattServiceDefinition::try_new(GattUuid::Uuid16(0xcdef), true, &[CHARACTERISTIC])
                .unwrap();
        const DATABASE: GattServerDefinition =
            GattServerDefinition::try_new(GattUuid::Uuid16(0xb301), &[SERVICE]).unwrap();

        assert_eq!(DATABASE.services()[0], SERVICE);
        assert!(
            GattCharacteristicDefinition::try_new(
                GattUuid::Uuid16(1),
                GattPermissions::READ,
                GattProperties::NOTIFY,
                &[],
                1,
                &[],
            )
            .is_none()
        );
        assert!(GattServiceDefinition::try_new(GattUuid::Uuid16(1), true, &[]).is_none());
    }
}