summaryrefslogtreecommitdiff
path: root/nrf-softdevice/src/ble/gatt_server.rs
blob: ad12702ef5a45902250d5e6a1307aacecbbda210 (plain)
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
//! Generic Attribute server. GATT servers offer functionality to clients.
//!
//! Typically the peripheral device is the GATT server, but it is not necessary.
//! In a connection any device can be server and client, and even both can be both at the same time.

use crate::ble::*;
use crate::raw;
use crate::util::{get_flexarray, get_union_field, Portal};
use crate::RawError;
use crate::Softdevice;

pub mod builder;
pub mod characteristic;

pub struct Characteristic {
    pub uuid: Uuid,
    pub can_read: bool,
    pub can_write: bool,
    pub can_write_without_response: bool,
    pub can_notify: bool,
    pub can_indicate: bool,
    pub max_len: usize,
    pub vlen: bool,
}

pub struct CharacteristicHandles {
    pub value_handle: u16,
    pub user_desc_handle: u16,
    pub cccd_handle: u16,
    pub sccd_handle: u16,
}

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

impl ServiceHandle {
    pub fn handle(&self) -> u16 {
        self.0
    }
}

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

impl IncludedServiceHandle {
    pub fn handle(&self) -> u16 {
        self.0
    }
}

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

impl DescriptorHandle {
    pub fn handle(&self) -> u16 {
        self.0
    }
}

pub trait Server: Sized {
    type Event;
    fn on_write(&self, handle: u16, data: &[u8]) -> Option<Self::Event>;
}

pub trait Service: Sized {
    type Event;

    fn on_write(&self, handle: u16, data: &[u8]) -> Option<Self::Event>;
}

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum RegisterError {
    Raw(RawError),
}

impl From<RawError> for RegisterError {
    fn from(err: RawError) -> Self {
        RegisterError::Raw(err)
    }
}

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum RunError {
    Disconnected,
    Raw(RawError),
}

impl From<RawError> for RunError {
    fn from(err: RawError) -> Self {
        Self::Raw(err)
    }
}

impl From<DisconnectedError> for RunError {
    fn from(_: DisconnectedError) -> Self {
        Self::Disconnected
    }
}

pub async fn run<'m, F, S>(conn: &Connection, server: &S, mut f: F) -> Result<(), RunError>
where
    F: FnMut(S::Event),
    S: Server,
{
    let conn_handle = conn.with_state(|state| state.check_connected())?;
    portal(conn_handle)
        .wait_many(|ble_evt| unsafe {
            match (*ble_evt).header.evt_id as u32 {
                raw::BLE_GAP_EVTS_BLE_GAP_EVT_DISCONNECTED => {
                    return Some(Err(RunError::Disconnected))
                }
                raw::BLE_GATTS_EVTS_BLE_GATTS_EVT_WRITE => {
                    let evt = &*ble_evt;
                    let gatts_evt = get_union_field(ble_evt, &evt.evt.gatts_evt);
                    let params = get_union_field(ble_evt, &gatts_evt.params.write);
                    let v = get_flexarray(ble_evt, &params.data, params.len as usize);
                    trace!("gatts write handle={:?} data={:?}", params.handle, v);
                    if params.offset != 0 {
                        panic!("gatt_server writes with nonzero offset are not yet supported");
                    }
                    if params.auth_required != 0 {
                        panic!("gatt_server auth_required not yet supported");
                    }

                    server.on_write(params.handle, &v).map(|e| f(e));
                }
                raw::BLE_GATTS_EVTS_BLE_GATTS_EVT_SYS_ATTR_MISSING => {
                    info!("initializing gatt sys att");
                    let ret =
                        raw::sd_ble_gatts_sys_attr_set(conn_handle, ::core::ptr::null(), 0, 0);
                    RawError::convert(ret).err();
                }
                _ => {}
            }
            None
        })
        .await
}

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum GetValueError {
    Truncated,
    Raw(RawError),
}

impl From<RawError> for GetValueError {
    fn from(err: RawError) -> Self {
        Self::Raw(err)
    }
}

pub fn get_value(_sd: &Softdevice, handle: u16, buf: &mut [u8]) -> Result<usize, GetValueError> {
    let mut value = raw::ble_gatts_value_t {
        p_value: buf.as_mut_ptr(),
        len: buf.len() as _,
        offset: 0,
    };
    let ret = unsafe {
        raw::sd_ble_gatts_value_get(raw::BLE_CONN_HANDLE_INVALID as u16, handle, &mut value)
    };
    RawError::convert(ret)?;

    if value.len as usize > buf.len() {
        return Err(GetValueError::Truncated);
    }

    Ok(value.len as _)
}

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum SetValueError {
    Truncated,
    Raw(RawError),
}

impl From<RawError> for SetValueError {
    fn from(err: RawError) -> Self {
        Self::Raw(err)
    }
}

pub fn set_value(_sd: &Softdevice, handle: u16, val: &[u8]) -> Result<(), SetValueError> {
    let mut value = raw::ble_gatts_value_t {
        p_value: val.as_ptr() as _,
        len: val.len() as _,
        offset: 0,
    };
    let ret = unsafe {
        raw::sd_ble_gatts_value_set(raw::BLE_CONN_HANDLE_INVALID as u16, handle, &mut value)
    };
    RawError::convert(ret)?;

    Ok(())
}

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum NotifyValueError {
    Disconnected,
    Raw(RawError),
}

impl From<RawError> for NotifyValueError {
    fn from(err: RawError) -> Self {
        Self::Raw(err)
    }
}

impl From<DisconnectedError> for NotifyValueError {
    fn from(_: DisconnectedError) -> Self {
        Self::Disconnected
    }
}

/// Multiple notifications can be queued. Will fail when the queue is full.
pub fn notify_value(conn: &Connection, handle: u16, val: &[u8]) -> Result<(), NotifyValueError> {
    let conn_handle = conn.with_state(|state| state.check_connected())?;

    let mut len: u16 = val.len() as _;
    let params = raw::ble_gatts_hvx_params_t {
        handle,
        type_: raw::BLE_GATT_HVX_NOTIFICATION as u8,
        offset: 0,
        p_data: val.as_ptr() as _,
        p_len: &mut len,
    };
    let ret = unsafe { raw::sd_ble_gatts_hvx(conn_handle, &params) };
    RawError::convert(ret)?;

    Ok(())
}

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum IndicateValueError {
    Disconnected,
    Raw(RawError),
}

impl From<RawError> for IndicateValueError {
    fn from(err: RawError) -> Self {
        Self::Raw(err)
    }
}

impl From<DisconnectedError> for IndicateValueError {
    fn from(_: DisconnectedError) -> Self {
        Self::Disconnected
    }
}

/// This will fail if an indication is already in progress
pub fn indicate_value(
    conn: &Connection,
    handle: u16,
    val: &[u8],
) -> Result<(), IndicateValueError> {
    let conn_handle = conn.with_state(|state| state.check_connected())?;

    let mut len: u16 = val.len() as _;
    let params = raw::ble_gatts_hvx_params_t {
        handle,
        type_: raw::BLE_GATT_HVX_INDICATION as u8,
        offset: 0,
        p_data: val.as_ptr() as _,
        p_len: &mut len,
    };
    let ret = unsafe { raw::sd_ble_gatts_hvx(conn_handle, &params) };
    RawError::convert(ret)?;

    Ok(())
}

pub(crate) unsafe fn on_evt(ble_evt: *const raw::ble_evt_t) {
    let gatts_evt = get_union_field(ble_evt, &(*ble_evt).evt.gatts_evt);
    match (*ble_evt).header.evt_id as u32 {
        raw::BLE_GATTS_EVTS_BLE_GATTS_EVT_EXCHANGE_MTU_REQUEST => {
            let conn_handle = gatts_evt.conn_handle;
            let params = get_union_field(ble_evt, &gatts_evt.params.exchange_mtu_request);
            let want_mtu = params.client_rx_mtu;
            let max_mtu = crate::Softdevice::steal().att_mtu;
            let mtu = want_mtu
                .min(max_mtu)
                .max(raw::BLE_GATT_ATT_MTU_DEFAULT as u16);
            trace!(
                "att mtu exchange: peer wants mtu {:?}, granting {:?}",
                want_mtu,
                mtu
            );

            let ret = { raw::sd_ble_gatts_exchange_mtu_reply(conn_handle, mtu) };
            if let Err(_err) = RawError::convert(ret) {
                warn!("sd_ble_gatts_exchange_mtu_reply err {:?}", _err);
                return;
            }

            connection::with_state_by_conn_handle(conn_handle, |state| {
                state.att_mtu = mtu;
            });
        }
        _ => {
            portal(gatts_evt.conn_handle).call(ble_evt);
        }
    }
}

const PORTAL_NEW: Portal<*const raw::ble_evt_t> = Portal::new();
static PORTALS: [Portal<*const raw::ble_evt_t>; CONNS_MAX] = [PORTAL_NEW; CONNS_MAX];
pub(crate) fn portal(conn_handle: u16) -> &'static Portal<*const raw::ble_evt_t> {
    &PORTALS[conn_handle as usize]
}