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
//! Implementation of the Generic Attribute Profile (GATT).
//!
//! GATT describes a service framework that uses the Attribute Protocol for discovery and
//! interaction

pub mod characteristic;

use crate::att::{AttUuid, Attribute, AttributeProvider, Handle, HandleRange};
use crate::uuid::{Uuid128, Uuid16};
use crate::Error;
use core::cmp;

/// A demo `AttributeProvider` that will enumerate as a *Battery Service*.
pub struct BatteryServiceAttrs {
    attributes: [Attribute<&'static [u8]>; 3],
}

impl BatteryServiceAttrs {
    pub fn new() -> Self {
        Self {
            attributes: [
                Attribute::new(
                    Uuid16(0x2800).into(), // "Primary Service"
                    Handle::from_raw(0x0001),
                    &[0x0F, 0x18], // "Battery Service" = 0x180F
                ),
                Attribute::new(
                    Uuid16(0x2803).into(), // "Characteristic"
                    Handle::from_raw(0x0002),
                    &[
                        0x02, // 1 byte properties: READ = 0x02
                        0x03, 0x00, // 2 bytes handle = 0x0003
                        0x19, 0x2A, // 2 bytes UUID = 0x2A19 (Battery Level)
                    ],
                ),
                // Characteristic value (Battery Level)
                Attribute::new(
                    AttUuid::Uuid16(Uuid16(0x2A19)), // "Battery Level"
                    Handle::from_raw(0x0003),
                    &[48u8],
                ),
            ],
        }
    }
}

impl AttributeProvider for BatteryServiceAttrs {
    fn for_attrs_in_range(
        &mut self,
        range: HandleRange,
        mut f: impl FnMut(&Self, &Attribute<dyn AsRef<[u8]>>) -> Result<(), Error>,
    ) -> Result<(), Error> {
        let count = self.attributes.len();
        let start = usize::from(range.start().as_u16() - 1); // handles start at 1, not 0
        let end = usize::from(range.end().as_u16() - 1);

        let attrs = if start >= count {
            &[]
        } else {
            let end = cmp::min(count - 1, end);
            &self.attributes[start..=end]
        };

        for attr in attrs {
            f(self, attr)?;
        }
        Ok(())
    }

    fn is_grouping_attr(&self, uuid: AttUuid) -> bool {
        uuid == Uuid16(0x2800) // FIXME not characteristics?
    }

    fn group_end(&self, handle: Handle) -> Option<&Attribute<dyn AsRef<[u8]>>> {
        match handle.as_u16() {
            0x0001 => Some(&self.attributes[2]),
            0x0002 => Some(&self.attributes[2]),
            _ => None,
        }
    }
}

/// A demo `AttributeProvider` that will enumerate as a *Midi Service*.
///
/// Also refer to <https://www.midi.org/specifications-old/item/bluetooth-le-midi>.
pub struct MidiServiceAttrs {
    attributes: [Attribute<&'static [u8]>; 4],
}

// MIDI Service (UUID: 03B80E5A-EDE8-4B33-A751-6CE34EC4C700)
// MIDI Data I/O Characteristic (UUID: 7772E5DB-3868-4112-A1A9-F2669D106BF3)

impl MidiServiceAttrs {
    pub fn new() -> Self {
        Self {
            attributes: [
                Attribute::new(
                    Uuid16(0x2800).into(), // "Primary Service"
                    Handle::from_raw(0x0001),
                    &[
                        0x00, 0xC7, 0xC4, 0x4E, 0xE3, 0x6C, /* - */
                        0x51, 0xA7, /* - */
                        0x33, 0x4B, /* - */
                        0xE8, 0xED, /* - */
                        0x5A, 0x0E, 0xB8, 0x03,
                    ], // "Midi Service"
                ),
                Attribute::new(
                    Uuid16(0x2803).into(), // "Characteristic"
                    Handle::from_raw(0x0002),
                    &[
                        0x02 | 0x08 | 0x04 | 0x10, // 1 byte properties: READ = 0x02, WRITE_REQ = 0x08, WRITE_CMD = 0x04, NOTIFICATION = 0x10
                        0x03,
                        0x00, // 2 bytes handle = 0x0003
                        // the actual UUID
                        0xF3,
                        0x6B,
                        0x10,
                        0x9D,
                        0x66,
                        0xF2, /*-*/
                        0xA9,
                        0xA1, /*-*/
                        0x12,
                        0x41, /*-*/
                        0x68,
                        0x38, /*-*/
                        0xDB,
                        0xE5,
                        0x72,
                        0x77,
                    ],
                ),
                // Characteristic value (Empty Packet)
                Attribute::new(
                    AttUuid::Uuid128(Uuid128::from_bytes([
                        0xF3, 0x6B, 0x10, 0x9D, 0x66, 0xF2, /*-*/
                        0xA9, 0xA1, /*-*/
                        0x12, 0x41, /*-*/
                        0x68, 0x38, /*-*/
                        0xDB, 0xE5, 0x72, 0x77,
                    ])),
                    Handle::from_raw(0x0003),
                    &[],
                ),
                // CCCD
                Attribute::new(
                    AttUuid::Uuid16(Uuid16(0x2902)),
                    Handle::from_raw(0x0004),
                    &[0x00, 0x00],
                ),
            ],
        }
    }
}

impl AttributeProvider for MidiServiceAttrs {
    fn for_attrs_in_range(
        &mut self,
        range: HandleRange,
        mut f: impl FnMut(&Self, &Attribute<dyn AsRef<[u8]>>) -> Result<(), Error>,
    ) -> Result<(), Error> {
        let count = self.attributes.len();
        let start = usize::from(range.start().as_u16() - 1); // handles start at 1, not 0
        let end = usize::from(range.end().as_u16() - 1);

        let attrs = if start >= count {
            &[]
        } else {
            let end = cmp::min(count - 1, end);
            &self.attributes[start..=end]
        };

        for attr in attrs {
            f(self, attr)?;
        }
        Ok(())
    }

    fn is_grouping_attr(&self, uuid: AttUuid) -> bool {
        uuid == Uuid16(0x2800) // FIXME not characteristics?
    }

    fn group_end(&self, handle: Handle) -> Option<&Attribute<dyn AsRef<[u8]>>> {
        match handle.as_u16() {
            0x0001 => Some(&self.attributes[3]),
            0x0002 => Some(&self.attributes[3]),
            _ => None,
        }
    }
}