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
use core::mem;
use core::num::NonZeroU16;

use crate::{raw, RawError};

#[repr(transparent)]
#[derive(Copy, Clone)]
pub struct Uuid {
    inner: raw::ble_uuid_t,
}

impl Uuid {
    pub const fn from_raw(raw: raw::ble_uuid_t) -> Option<Self> {
        if raw.type_ == raw::BLE_UUID_TYPE_UNKNOWN as u8 {
            None
        } else {
            Some(Self { inner: raw })
        }
    }

    pub const fn new_16(uuid: u16) -> Self {
        Self {
            inner: raw::ble_uuid_t {
                type_: raw::BLE_UUID_TYPE_BLE as u8,
                uuid,
            },
        }
    }

    // Create a new 128-bit UUID.
    //
    // Note that `uuid` needs to be in little-endian format, i.e. opposite to what you would
    // normally write UUIDs.
    pub fn new_128(uuid: &[u8; 16]) -> Self {
        let mut uuid_type: u8 = 0;
        let ret = unsafe { raw::sd_ble_uuid_vs_add(uuid.as_ptr() as _, &mut uuid_type as _) };
        match RawError::convert(ret) {
            Ok(()) => {}
            Err(e) => panic!("sd_ble_uuid_vs_add err {:?}", e),
        }

        Self {
            inner: raw::ble_uuid_t {
                type_: uuid_type,
                uuid: ((uuid[13] as u16) << 8) | (uuid[12] as u16),
            },
        }
    }

    pub fn as_raw_ptr(&self) -> *const raw::ble_uuid_t {
        &self.inner as _
    }

    pub fn into_raw(self) -> raw::ble_uuid_t {
        self.inner
    }
}

impl Eq for Uuid {}
impl PartialEq for Uuid {
    fn eq(&self, other: &Uuid) -> bool {
        self.inner.type_ == other.inner.type_ && self.inner.uuid == other.inner.uuid
    }
}

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Role {
    #[cfg(feature = "ble-central")]
    Central,
    #[cfg(feature = "ble-peripheral")]
    Peripheral,
}

impl Role {
    pub fn from_raw(raw: u8) -> Self {
        match raw as u32 {
            #[cfg(feature = "ble-central")]
            raw::BLE_GAP_ROLE_CENTRAL => Self::Central,
            #[cfg(feature = "ble-peripheral")]
            raw::BLE_GAP_ROLE_PERIPH => Self::Peripheral,
            _ => panic!("unknown role {:?}", raw),
        }
    }
}

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum SecurityMode {
    NoAccess,
    Open,
    JustWorks,
    Mitm,
    LescMitm,
    Signed,
    SignedMitm,
}

impl Default for SecurityMode {
    fn default() -> Self {
        Self::Open
    }
}

impl SecurityMode {
    pub fn try_from_raw(raw: raw::ble_gap_conn_sec_mode_t) -> Option<Self> {
        match (raw.sm(), raw.lv()) {
            (0, 0) => Some(SecurityMode::NoAccess),
            (1, 1) => Some(SecurityMode::Open),
            (1, 2) => Some(SecurityMode::JustWorks),
            (1, 3) => Some(SecurityMode::Mitm),
            (1, 4) => Some(SecurityMode::LescMitm),
            (2, 1) => Some(SecurityMode::Signed),
            (2, 2) => Some(SecurityMode::SignedMitm),
            _ => None,
        }
    }

    pub fn into_raw(self) -> raw::ble_gap_conn_sec_mode_t {
        let (sm, lv) = match self {
            SecurityMode::NoAccess => (0, 0),
            SecurityMode::Open => (1, 1),
            SecurityMode::JustWorks => (1, 2),
            SecurityMode::Mitm => (1, 3),
            SecurityMode::LescMitm => (1, 4),
            SecurityMode::Signed => (2, 1),
            SecurityMode::SignedMitm => (2, 2),
        };

        raw::ble_gap_conn_sec_mode_t {
            _bitfield_1: raw::ble_gap_conn_sec_mode_t::new_bitfield_1(sm, lv),
        }
    }
}

#[repr(u8)]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum AddressType {
    /// Public (identity) address
    Public = 0x00,
    /// Random static (identity) address.
    RandomStatic = 0x01,
    /// Random private resolvable address.
    RandomPrivateResolvable = 0x02,
    /// Random private non-resolvable address.
    RandomPrivateNonResolvable = 0x03,
    /// An advertiser may advertise without its address. This type of advertising is called anonymous.
    Anonymous = 0x7F,
}

#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct InvalidAddressType;

impl TryFrom<u8> for AddressType {
    type Error = InvalidAddressType;

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        if value == 0x00 {
            Ok(AddressType::Public)
        } else if value == 0x01 {
            Ok(AddressType::RandomStatic)
        } else if value == 0x02 {
            Ok(AddressType::RandomPrivateResolvable)
        } else if value == 0x03 {
            Ok(AddressType::RandomPrivateNonResolvable)
        } else if value == 0x7F {
            Ok(AddressType::Anonymous)
        } else {
            Err(InvalidAddressType)
        }
    }
}

// Note: this type MUST be layout-compatible with raw::ble_gap_addr_t
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct Address {
    // bit 0: is resolved private address
    // bits 7-1: type
    pub flags: u8,
    pub bytes: [u8; 6],
}

impl PartialEq for Address {
    fn eq(&self, other: &Self) -> bool {
        // bit 0 of flags indicates a peer identity address resolved from a resolvable private address by the
        // Softdevice. It is irrelevant for comparing addresses.
        (self.flags & 0xfe) == (other.flags & 0xfe) && self.bytes == other.bytes
    }
}

impl Eq for Address {}

impl Address {
    pub const fn new(address_type: AddressType, bytes: [u8; 6]) -> Self {
        Self {
            flags: (address_type as u8) << 1,
            bytes,
        }
    }

    pub fn address_type(&self) -> AddressType {
        unwrap!((self.flags >> 1).try_into())
    }

    pub fn is_resolved_peer_id(&self) -> bool {
        (self.flags & 1) != 0
    }

    pub fn bytes(&self) -> [u8; 6] {
        self.bytes
    }

    pub fn as_raw(&self) -> &raw::ble_gap_addr_t {
        // Safety: `Self` has the same layout as `raw::ble_gap_addr_t` and all bit patterns are valid
        unsafe { mem::transmute(self) }
    }

    pub fn from_raw(raw: raw::ble_gap_addr_t) -> Self {
        // Safety: `Self` has the same layout as `raw::ble_gap_addr_t` and all bit patterns are valid
        unsafe { mem::transmute(raw) }
    }
}

#[cfg(feature = "defmt")]
impl defmt::Format for Address {
    fn format(&self, fmt: defmt::Formatter) {
        if self.is_resolved_peer_id() {
            defmt::write!(fmt, "{:?}(resolved):{=[u8]:x}", self.address_type(), self.bytes())
        } else {
            defmt::write!(fmt, "{:?}:{=[u8]:x}", self.address_type(), self.bytes())
        }
    }
}

#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Eq, PartialEq, Copy, Clone)]
#[repr(i8)]
pub enum TxPower {
    Minus40dBm = -40,
    Minus20dBm = -20,
    Minus16dBm = -16,
    Minus12dBm = -12,
    Minus8dBm = -8,
    Minus4dBm = -4,
    ZerodBm = 0,
    #[cfg(feature = "s140")]
    Plus2dBm = 2,
    Plus3dBm = 3,
    Plus4dBm = 4,
    #[cfg(feature = "s140")]
    Plus5dBm = 5,
    #[cfg(feature = "s140")]
    Plus6dBm = 6,
    #[cfg(feature = "s140")]
    Plus7dBm = 7,
    #[cfg(feature = "s140")]
    Plus8dBm = 8,
}

#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Eq, PartialEq, Copy, Clone)]
#[repr(u8)]
pub enum Phy {
    /// 1Mbps phy
    M1 = 1,
    /// 2Mbps phy
    M2 = 2,
    /// Coded phy (125kbps, S=8)
    #[cfg(feature = "s140")]
    Coded = 4,
}

#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Eq, PartialEq, Copy, Clone)]
#[repr(u8)]
pub enum PhySet {
    /// 1Mbps phy
    M1 = 1,
    /// 2Mbps phy
    M2 = 2,
    /// 1Mbps + 2Mbps phys
    M1M2 = 3,
    /// Coded phy (125kbps, S=8)
    #[cfg(feature = "s140")]
    Coded = 4,
    /// 1Mbps and Coded phys
    #[cfg(feature = "s140")]
    M1Coded = 5,
    /// 2Mbps and Coded phys
    #[cfg(feature = "s140")]
    M2Coded = 6,
    /// 1Mbps, 2Mbps and Coded phys
    #[cfg(feature = "s140")]
    M1M2Coded = 7,
}

#[repr(C)]
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Hash)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct MasterId {
    /// Encrypted diversifier
    pub ediv: u16,
    /// Random number
    pub rand: [u8; 8],
}

impl MasterId {
    pub fn from_raw(raw: raw::ble_gap_master_id_t) -> Self {
        MasterId {
            ediv: raw.ediv,
            rand: raw.rand,
        }
    }
}

// Note: this type MUST be layout-compatible with raw::ble_gap_enc_info_t
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct EncryptionInfo {
    /// Long term key
    pub ltk: [u8; 16],
    pub flags: u8,
}

impl EncryptionInfo {
    pub fn as_raw(&self) -> &raw::ble_gap_enc_info_t {
        // Safety: `Self` has the same layout as `raw::ble_gap_enc_info_t` and all bit patterns are valid
        unsafe { mem::transmute(self) }
    }

    pub fn from_raw(raw: raw::ble_gap_enc_info_t) -> Self {
        // Safety: `raw::ble_gap_enc_info_t` has the same layout as `Self` and all bit patterns are valid
        unsafe { mem::transmute(raw) }
    }
}

// Note: this type MUST be layout-compatible with raw::ble_gap_irk_t
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct IdentityResolutionKey {
    irk: [u8; 16],
}

impl IdentityResolutionKey {
    pub fn from_raw(raw: raw::ble_gap_irk_t) -> Self {
        Self { irk: raw.irk }
    }

    pub fn as_raw(&self) -> &raw::ble_gap_irk_t {
        // Safety: `Self` has the same layout as `raw::ble_gap_irk_t` and all bit patterns are valid
        unsafe { core::mem::transmute(self) }
    }
}

// Note: this type MUST be layout-compatible with raw::ble_gap_id_key_t
#[repr(C)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct IdentityKey {
    /// Identity resolution key
    pub irk: IdentityResolutionKey,
    /// Address
    pub addr: Address,
}

impl IdentityKey {
    pub fn is_match(&self, addr: Address) -> bool {
        match addr.address_type() {
            AddressType::Public | AddressType::RandomStatic => self.addr == addr,
            AddressType::RandomPrivateResolvable => {
                let local_hash = random_address_hash(self.irk, addr.bytes()[3..].try_into().unwrap());
                addr.bytes()[..3] == local_hash
            }
            AddressType::RandomPrivateNonResolvable | AddressType::Anonymous => false,
        }
    }

    pub fn from_raw(raw: raw::ble_gap_id_key_t) -> Self {
        Self {
            irk: IdentityResolutionKey::from_raw(raw.id_info),
            addr: Address::from_raw(raw.id_addr_info),
        }
    }

    pub fn from_addr(addr: Address) -> Self {
        Self {
            irk: Default::default(),
            addr,
        }
    }

    pub fn as_raw(&self) -> &raw::ble_gap_id_key_t {
        // Safety: `Self` has the same layout as `raw::ble_gap_id_key_t` and all bit patterns are valid
        unsafe { core::mem::transmute(self) }
    }
}

fn random_address_hash(key: IdentityResolutionKey, r: [u8; 3]) -> [u8; 3] {
    let mut cleartext = [0; 16];
    cleartext[13..].copy_from_slice(&r);
    cleartext[13..].reverse(); // big-endian to little-endian

    let mut ecb_hal_data: raw::nrf_ecb_hal_data_t = raw::nrf_ecb_hal_data_t {
        key: key.irk,
        cleartext,
        ciphertext: [0; 16],
    };

    ecb_hal_data.key.reverse(); // big-endian to little-endian

    // Can only return NRF_SUCCESS
    let _ = unsafe { raw::sd_ecb_block_encrypt(&mut ecb_hal_data) };

    let mut res: [u8; 3] = ecb_hal_data.ciphertext[13..].try_into().unwrap();
    res.reverse(); // little-endian to big-endian
    res
}

#[derive(PartialEq, Eq, Clone, Copy)]
pub struct GattError(NonZeroU16);

#[derive(PartialEq, Eq, Clone, Copy)]
pub struct GattStatus(u16);

impl GattError {
    pub const fn new(err: u16) -> Option<Self> {
        match NonZeroU16::new(err) {
            Some(n) => Some(Self(n)),
            None => None,
        }
    }

    /// Construct an arbitrary ATT protocol error code
    pub const fn from_att_error(err: u8) -> Self {
        Self(unsafe { NonZeroU16::new_unchecked(0x100 + err as u16) })
    }

    pub const fn to_status(self) -> GattStatus {
        GattStatus(self.0.get())
    }
}

#[cfg(feature = "defmt")]
impl defmt::Format for GattError {
    fn format(&self, fmt: defmt::Formatter) {
        defmt::Format::format(&self.to_status(), fmt)
    }
}

impl core::fmt::Debug for GattError {
    fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        core::fmt::Debug::fmt(&self.to_status(), fmt)
    }
}

impl From<GattError> for u16 {
    fn from(value: GattError) -> Self {
        value.0.get()
    }
}

impl GattStatus {
    pub const SUCCESS: GattStatus = GattStatus(raw::BLE_GATT_STATUS_SUCCESS as u16);

    pub const fn new(status: u16) -> Self {
        Self(status)
    }

    pub const fn is_app_error(self) -> bool {
        self.0 >= raw::BLE_GATT_STATUS_ATTERR_APP_BEGIN as u16 && self.0 <= raw::BLE_GATT_STATUS_ATTERR_APP_END as u16
    }

    pub const fn to_result(self) -> Result<(), GattError> {
        match NonZeroU16::new(self.0) {
            None => Ok(()),
            Some(err) => Err(GattError(err)),
        }
    }
}

impl From<GattError> for GattStatus {
    fn from(value: GattError) -> Self {
        value.to_status()
    }
}

impl From<u16> for GattStatus {
    fn from(value: u16) -> Self {
        Self(value)
    }
}

impl From<GattStatus> for u16 {
    fn from(value: GattStatus) -> Self {
        value.0
    }
}

macro_rules! error_codes {
    (
        $(
            $(#[$docs:meta])*
            ($konst:ident, $raw:ident, $phrase:expr);
        )+
    ) => {
        impl GattError {
        $(
            $(#[$docs])*
            pub const $konst: GattError = GattError(unsafe { NonZeroU16::new_unchecked(raw::$raw as u16) });
        )+
        }

        #[cfg(feature = "defmt")]
        impl defmt::Format for GattStatus {
            fn format(&self, fmt: defmt::Formatter) {
                if self.is_app_error() {
                    defmt::write!(fmt, "Application Error: 0x{:02x}", self.0 as u8);
                } else {
                    match *self {
                        Self::SUCCESS => defmt::write!(fmt, "Success"),
                        $(
                        Self::$konst => defmt::write!(fmt, $phrase),
                        )+
                        _ => defmt::write!(fmt, "Unknown GATT status: 0x{:04x}", self.0),
                    }
                }
            }
        }

        impl core::fmt::Debug for GattStatus {
            fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
                if self.is_app_error() {
                    core::write!(fmt, "Application Error: 0x{:02x}", self.0 as u8)
                } else {
                    match *self {
                        Self::SUCCESS => core::write!(fmt, "Success"),
                        $(
                        Self::$konst => core::write!(fmt, $phrase),
                        )+
                        _ => core::write!(fmt, "Unknown GATT status: 0x{:04x}", self.0),
                    }
                }
            }
        }


        impl GattStatus {
        $(
            $(#[$docs])*
            pub const $konst: GattStatus = GattError::$konst.to_status();
        )+
        }
    }
}

error_codes! {
    /// Unknown or not applicable status.
    (UNKNOWN, BLE_GATT_STATUS_UNKNOWN, "Unknown");
    /// ATT Error: Invalid Error Code.
    (ATTERR_INVALID, BLE_GATT_STATUS_ATTERR_INVALID, "Invalid Error Code");
    /// ATT Error: Invalid Attribute Handle.
    (ATTERR_INVALID_HANDLE, BLE_GATT_STATUS_ATTERR_INVALID_HANDLE, "Invalid Handle");
    /// ATT Error: Read not permitted.
    (ATTERR_READ_NOT_PERMITTED, BLE_GATT_STATUS_ATTERR_READ_NOT_PERMITTED, "Read Not Permitted");
    /// ATT Error: Write not permitted.
    (ATTERR_WRITE_NOT_PERMITTED, BLE_GATT_STATUS_ATTERR_WRITE_NOT_PERMITTED, "Write Not Permitted");
    /// ATT Error: Used in ATT as Invalid PDU.
    (ATTERR_INVALID_PDU, BLE_GATT_STATUS_ATTERR_INVALID_PDU, "Invalid PDU");
    /// ATT Error: Authenticated link required.
    (ATTERR_INSUF_AUTHENTICATION, BLE_GATT_STATUS_ATTERR_INSUF_AUTHENTICATION, "Insufficient Authentication");
    /// ATT Error: Used in ATT as Request Not Supported.
    (ATTERR_REQUEST_NOT_SUPPORTED, BLE_GATT_STATUS_ATTERR_REQUEST_NOT_SUPPORTED, "Request Not Supported");
    /// ATT Error: Offset specified was past the end of the attribute.
    (ATTERR_INVALID_OFFSET, BLE_GATT_STATUS_ATTERR_INVALID_OFFSET, "Invalid Offset");
    /// ATT Error: Used in ATT as Insufficient Authorization.
    (ATTERR_INSUF_AUTHORIZATION, BLE_GATT_STATUS_ATTERR_INSUF_AUTHORIZATION, "Insufficient Authorization");
    /// ATT Error: Used in ATT as Prepare Queue Full.
    (ATTERR_PREPARE_QUEUE_FULL, BLE_GATT_STATUS_ATTERR_PREPARE_QUEUE_FULL, "Prepare Queue Full");
    /// ATT Error: Used in ATT as Attribute not found.
    (ATTERR_ATTRIBUTE_NOT_FOUND, BLE_GATT_STATUS_ATTERR_ATTRIBUTE_NOT_FOUND, "Attribute Not Found");
    /// ATT Error: Attribute cannot be read or written using read/write blob requests.
    (ATTERR_ATTRIBUTE_NOT_LONG, BLE_GATT_STATUS_ATTERR_ATTRIBUTE_NOT_LONG, "Attribute Not Long");
    /// ATT Error: Encryption key size used is insufficient.
    (ATTERR_INSUF_ENC_KEY_SIZE, BLE_GATT_STATUS_ATTERR_INSUF_ENC_KEY_SIZE, "Insufficient Encryption Key Size");
    /// ATT Error: Invalid value size.
    (ATTERR_INVALID_ATT_VAL_LENGTH, BLE_GATT_STATUS_ATTERR_INVALID_ATT_VAL_LENGTH, "Invalid Attribute Value Size");
    /// ATT Error: Very unlikely error.
    (ATTERR_UNLIKELY_ERROR, BLE_GATT_STATUS_ATTERR_UNLIKELY_ERROR, "Unlikely Error");
    /// ATT Error: Encrypted link required.
    (ATTERR_INSUF_ENCRYPTION, BLE_GATT_STATUS_ATTERR_INSUF_ENCRYPTION, "Insufficient Encryption");
    /// ATT Error: Attribute type is not a supported grouping attribute.
    (ATTERR_UNSUPPORTED_GROUP_TYPE, BLE_GATT_STATUS_ATTERR_UNSUPPORTED_GROUP_TYPE, "Unsupported Group Type");
    /// ATT Error: Insufficient resources.
    (ATTERR_INSUF_RESOURCES, BLE_GATT_STATUS_ATTERR_INSUF_RESOURCES, "Insufficient Resources");
    /// ATT Common Profile and Service Error: Write request rejected.
    (ATTERR_CPS_WRITE_REQ_REJECTED, BLE_GATT_STATUS_ATTERR_CPS_WRITE_REQ_REJECTED, "Write Request Rejected");
    /// ATT Common Profile and Service Error: Client Characteristic Configuration Descriptor improperly configured.
    (ATTERR_CPS_CCCD_CONFIG_ERROR, BLE_GATT_STATUS_ATTERR_CPS_CCCD_CONFIG_ERROR, "Client Characteristic Configration Descriptor Improperly Configured");
    /// ATT Common Profile and Service Error: Procedure Already in Progress.
    (ATTERR_CPS_PROC_ALR_IN_PROG, BLE_GATT_STATUS_ATTERR_CPS_PROC_ALR_IN_PROG, "Procedure Already in Progress");
    /// ATT Common Profile and Service Error: Out Of Range.
    (ATTERR_CPS_OUT_OF_RANGE, BLE_GATT_STATUS_ATTERR_CPS_OUT_OF_RANGE, "Out of Range");
}