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
use core::cell::{Cell, UnsafeCell};
use core::iter::FusedIterator;

use raw::ble_gap_conn_params_t;

use super::PhySet;
#[cfg(feature = "ble-sec")]
use crate::ble::security::SecurityHandler;
use crate::ble::types::{Address, AddressType, Role, SecurityMode};
use crate::{raw, RawError};

#[cfg(any(feature = "s113", feature = "s132", feature = "s140"))]
const BLE_GAP_DATA_LENGTH_DEFAULT: u8 = 27; //  The stack's default data length. <27-251>

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

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

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

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

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

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

#[cfg(feature = "ble-peripheral")]
impl From<DisconnectedError> for IgnoreSlaveLatencyError {
    fn from(_err: DisconnectedError) -> Self {
        Self::Disconnected
    }
}

#[cfg(feature = "ble-peripheral")]
impl From<RawError> for IgnoreSlaveLatencyError {
    fn from(err: RawError) -> Self {
        Self::Raw(err)
    }
}

pub enum PhyUpdateError {
    Disconnected,
    Raw(RawError),
}

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

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

// Highest ever the softdevice can support.
pub(crate) const CONNS_MAX: usize = 20;

#[cfg(feature = "ble-sec")]
#[derive(Clone, Copy)]
pub(crate) struct EncryptionState {
    pub handler: Option<&'static dyn SecurityHandler>,

    pub own_enc_key: raw::ble_gap_enc_key_t,
    pub peer_enc_key: raw::ble_gap_enc_key_t,
    pub peer_id: raw::ble_gap_id_key_t,
}

#[cfg(feature = "ble-sec")]
const NEW_GAP_ENC_KEY: raw::ble_gap_enc_key_t = raw::ble_gap_enc_key_t {
    enc_info: raw::ble_gap_enc_info_t {
        ltk: [0; 16],
        _bitfield_1: raw::__BindgenBitfieldUnit::new([0; 1]),
    },
    master_id: raw::ble_gap_master_id_t { ediv: 0, rand: [0; 8] },
};

#[cfg(feature = "ble-sec")]
const NEW_GAP_ID_KEY: raw::ble_gap_id_key_t = raw::ble_gap_id_key_t {
    id_info: raw::ble_gap_irk_t { irk: [0; 16] },
    id_addr_info: raw::ble_gap_addr_t {
        _bitfield_1: raw::__BindgenBitfieldUnit::new([0; 1]),
        addr: [0; 6],
    },
};

#[cfg(feature = "ble-sec")]
const NEW_ENCRYPTION_STATE: EncryptionState = EncryptionState {
    handler: None,
    own_enc_key: NEW_GAP_ENC_KEY,
    peer_enc_key: NEW_GAP_ENC_KEY,
    peer_id: NEW_GAP_ID_KEY,
};

// We could make the public Connection type simply hold the softdevice's conn_handle.
// However, that would allow for bugs like:
// - Connection is established with conn_handle=5
// - Client code stores a Connection instance with conn_handle=5
// - Connection gets disconnected
// - A new, unrelated connection is established with same conn_handle=5
// - Client code uses the Connection instance from before, mistakenly doing an operation in an unrelated connection.
//
// To avoid this, the public Connection struct has an "index" into a private ConnectionState array.
// It is refcounted, so an index will never be reused until client code has dropped all Connection instances.

pub(crate) struct ConnectionState {
    // Every Connection instance counts as one ref.
    //
    // When client code drops all instances, refcount becomes 0 and disconnection is initiated.
    // However, disconnection is not complete until the event GAP_DISCONNECTED.
    // so there's a small gap of time where the ConnectionState is not "free" even if refcount=0.
    pub refcount: u8,
    pub conn_handle: Option<u16>,

    pub disconnecting: bool,
    pub role: Role,
    pub peer_address: Address,
    pub security_mode: SecurityMode,

    pub conn_params: ble_gap_conn_params_t,

    #[cfg(feature = "ble-rssi")]
    pub rssi: Option<i8>,

    #[cfg(feature = "ble-gatt")]
    pub att_mtu: u16, // Effective ATT_MTU size (in bytes).
    #[cfg(any(feature = "s113", feature = "s132", feature = "s140"))]
    pub data_length_effective: u8, // Effective data length (in bytes).

    #[cfg(feature = "ble-sec")]
    pub security: EncryptionState,
}

impl ConnectionState {
    const fn dummy() -> Self {
        // The returned value should have bit pattern 0, so that STATES
        // can go into .bss instead of .data, which saves flash space.
        Self {
            refcount: 0,
            conn_handle: None,
            #[cfg(feature = "ble-central")]
            role: Role::Central,
            #[cfg(not(feature = "ble-central"))]
            role: Role::Peripheral,
            peer_address: Address::new(AddressType::Public, [0; 6]),
            security_mode: SecurityMode::NoAccess,
            disconnecting: false,
            conn_params: ble_gap_conn_params_t {
                conn_sup_timeout: 0,
                max_conn_interval: 0,
                min_conn_interval: 0,
                slave_latency: 0,
            },
            #[cfg(feature = "ble-rssi")]
            rssi: None,
            #[cfg(feature = "ble-gatt")]
            att_mtu: 0,
            #[cfg(any(feature = "s113", feature = "s132", feature = "s140"))]
            data_length_effective: 0,
            #[cfg(feature = "ble-sec")]
            security: NEW_ENCRYPTION_STATE,
        }
    }
    pub(crate) fn check_connected(&mut self) -> Result<u16, DisconnectedError> {
        self.conn_handle.ok_or(DisconnectedError)
    }

    pub(crate) fn disconnect(&mut self) -> Result<(), DisconnectedError> {
        let conn_handle = self.check_connected()?;

        if self.disconnecting {
            return Ok(());
        }

        let ret =
            unsafe { raw::sd_ble_gap_disconnect(conn_handle, raw::BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION as u8) };
        unwrap!(RawError::convert(ret), "sd_ble_gap_disconnect");

        self.disconnecting = true;
        Ok(())
    }

    pub(crate) fn on_disconnected(&mut self, _ble_evt: *const raw::ble_evt_t) {
        let conn_handle = unwrap!(self.conn_handle, "bug: on_disconnected when already disconnected");

        let ibh = index_by_handle(conn_handle);
        let _index = unwrap!(ibh.get(), "bug: conn_handle has no index");

        #[cfg(all(feature = "ble-gatt-server", feature = "ble-sec"))]
        if let Some(handler) = self.security.handler {
            let conn = unwrap!(Connection::from_handle(conn_handle), "bug: conn_handle has no index");
            handler.save_sys_attrs(&conn);
        }

        ibh.set(None);

        self.conn_handle = None;

        // Signal possible in-progess operations that the connection has disconnected.
        #[cfg(feature = "ble-gatt-client")]
        crate::ble::gatt_client::portal(conn_handle).call(_ble_evt);
        #[cfg(feature = "ble-gatt-server")]
        crate::ble::gatt_server::portal(conn_handle).call(_ble_evt);
        #[cfg(feature = "ble-l2cap")]
        crate::ble::l2cap::portal(conn_handle).call(_ble_evt);

        trace!("conn {:?}: disconnected", _index);
    }

    pub(crate) fn keyset(&mut self) -> raw::ble_gap_sec_keyset_t {
        #[cfg(feature = "ble-sec")]
        return raw::ble_gap_sec_keyset_t {
            keys_own: raw::ble_gap_sec_keys_t {
                p_enc_key: &mut self.security.own_enc_key,
                p_id_key: core::ptr::null_mut(),
                p_sign_key: core::ptr::null_mut(),
                p_pk: core::ptr::null_mut(),
            },
            keys_peer: raw::ble_gap_sec_keys_t {
                p_enc_key: &mut self.security.peer_enc_key,
                p_id_key: &mut self.security.peer_id,
                p_sign_key: core::ptr::null_mut(),
                p_pk: core::ptr::null_mut(),
            },
        };
        #[cfg(not(feature = "ble-sec"))]
        return raw::ble_gap_sec_keyset_t {
            keys_own: raw::ble_gap_sec_keys_t {
                p_enc_key: core::ptr::null_mut(),
                p_id_key: core::ptr::null_mut(),
                p_sign_key: core::ptr::null_mut(),
                p_pk: core::ptr::null_mut(),
            },
            keys_peer: raw::ble_gap_sec_keys_t {
                p_enc_key: core::ptr::null_mut(),
                p_id_key: core::ptr::null_mut(),
                p_sign_key: core::ptr::null_mut(),
                p_pk: core::ptr::null_mut(),
            },
        };
    }
}

#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Connection {
    index: u8,
}

impl Drop for Connection {
    fn drop(&mut self) {
        self.with_state(|state| {
            state.refcount = unwrap!(
                state.refcount.checked_sub(1),
                "bug: dropping a conn which is already at refcount 0"
            );

            if state.refcount == 0 {
                if state.conn_handle.is_some() {
                    trace!("conn {:?}: dropped, disconnecting", self.index);
                    // We still leave conn_handle set, because the connection is
                    // not really disconnected until we get GAP_DISCONNECTED event.
                    unwrap!(state.disconnect());
                } else {
                    trace!("conn {:?}: dropped, already disconnected", self.index);
                }
            }
        });
    }
}

impl Clone for Connection {
    fn clone(&self) -> Self {
        self.with_state(|state| {
            state.refcount = unwrap!(state.refcount.checked_add(1), "Too many references to same connection");
        });

        Self { index: self.index }
    }
}

impl Connection {
    pub fn role(&self) -> Role {
        self.with_state(|state| state.role)
    }

    pub fn peer_address(&self) -> Address {
        self.with_state(|state| state.peer_address)
    }

    pub fn disconnect(&self) -> Result<(), DisconnectedError> {
        self.with_state(|state| state.disconnect())
    }

    pub fn handle(&self) -> Option<u16> {
        self.with_state(|state| state.conn_handle)
    }

    pub fn from_handle(conn_handle: u16) -> Option<Connection> {
        index_by_handle(conn_handle).get().map(|index| {
            with_state(index, |state| {
                state.refcount = unwrap!(state.refcount.checked_add(1), "Too many references to same connection");
                Connection { index }
            })
        })
    }

    pub(crate) fn new(
        conn_handle: u16,
        role: Role,
        peer_address: Address,
        conn_params: ble_gap_conn_params_t,
    ) -> Result<Self, OutOfConnsError> {
        allocate_index(|index, state| {
            // Initialize
            *state = ConnectionState {
                refcount: 1,
                conn_handle: Some(conn_handle),
                role,
                peer_address,
                security_mode: SecurityMode::Open,

                disconnecting: false,

                conn_params,

                #[cfg(feature = "ble-rssi")]
                rssi: None,

                #[cfg(feature = "ble-gatt")]
                att_mtu: raw::BLE_GATT_ATT_MTU_DEFAULT as _,

                #[cfg(any(feature = "s113", feature = "s132", feature = "s140"))]
                data_length_effective: BLE_GAP_DATA_LENGTH_DEFAULT,

                #[cfg(feature = "ble-sec")]
                security: NEW_ENCRYPTION_STATE,
            };

            // Update index_by_handle
            let ibh = index_by_handle(conn_handle);
            assert!(ibh.get().is_none(), "bug: conn_handle already has index");
            ibh.set(Some(index));

            trace!("conn {:?}: connected", index);
            Self { index }
        })
    }

    #[cfg(feature = "ble-sec")]
    pub(crate) fn with_security_handler(
        conn_handle: u16,
        role: Role,
        peer_address: Address,
        conn_params: ble_gap_conn_params_t,
        handler: &'static dyn SecurityHandler,
    ) -> Result<Self, OutOfConnsError> {
        let conn = Self::new(conn_handle, role, peer_address, conn_params)?;
        conn.with_state(|state| state.security.handler = Some(handler));
        Ok(conn)
    }

    /// Start measuring RSSI on this connection.
    #[cfg(feature = "ble-rssi")]
    pub fn start_rssi(&self) {
        if let Ok(conn_handle) = self.with_state(|state| state.check_connected()) {
            let ret = unsafe { raw::sd_ble_gap_rssi_start(conn_handle, 0, 0) };
            if let Err(err) = RawError::convert(ret) {
                warn!("sd_ble_gap_rssi_start err {:?}", err);
            }
        }
    }

    /// Get the connection's RSSI.
    ///
    /// This will return None if `start_rssi` has not been called yet, or if
    /// no measurement has been done yet.
    #[cfg(feature = "ble-rssi")]
    pub fn rssi(&self) -> Option<i8> {
        self.with_state(|state| state.rssi)
    }

    /// Get the currently active connection params.
    pub fn conn_params(&self) -> ble_gap_conn_params_t {
        with_state(self.index, |s| s.conn_params)
    }

    /// Get the currently active ATT MTU.
    #[cfg(feature = "ble-gatt")]
    pub fn att_mtu(&self) -> u16 {
        with_state(self.index, |s| s.att_mtu)
    }

    pub fn security_mode(&self) -> SecurityMode {
        with_state(self.index, |s| s.security_mode)
    }

    #[cfg(feature = "ble-sec")]
    pub fn security_handler(&self) -> Option<&dyn SecurityHandler> {
        with_state(self.index, |s| s.security.handler)
    }

    /// Set the connection params.
    ///
    /// Note that this just initiates the connection param change, it does not wait for completion.
    /// Immediately after return, the active params will still be the old ones, and after some time they
    /// should change to the new ones.
    ///
    /// For central connections, this will initiate a Link Layer connection parameter update procedure.
    /// For peripheral connections, this will send the corresponding L2CAP request to the central. It is then
    /// up to the central to accept or deny the request.
    pub fn set_conn_params(&self, conn_params: ble_gap_conn_params_t) -> Result<(), SetConnParamsError> {
        let conn_handle = self.with_state(|state| state.check_connected())?;
        let ret = unsafe { raw::sd_ble_gap_conn_param_update(conn_handle, &conn_params) };
        if let Err(err) = RawError::convert(ret) {
            warn!("sd_ble_gap_conn_param_update err {:?}", err);
            return Err(err.into());
        }

        Ok(())
    }

    /// Temporarily ignore slave latency for peripehral connections.
    ///
    /// "Slave latency" is a setting in the conn params that allows the peripheral
    /// to intentionally sleep through and miss up to N connection events if it doesn't
    /// have any data to send to the central.
    ///
    /// Slave latency is useful because it can yield the same power savings on the peripheral
    /// as increasing the conn interval, but it only impacts latency in the central->peripheral
    /// direction, not both.
    ///
    /// However, in some cases, if the peripheral knows the central will send it some data soon
    /// it might be useful to temporarily force ignoring the slave latency setting, ie waking up
    /// at every single conn interval, to lower the latency.
    ///
    /// This only works on peripheral connections.
    #[cfg(feature = "ble-peripheral")]
    pub fn ignore_slave_latency(&mut self, ignore: bool) -> Result<(), IgnoreSlaveLatencyError> {
        let conn_handle = self.with_state(|state| state.check_connected())?;

        let mut disable: raw::ble_gap_opt_slave_latency_disable_t = unsafe { core::mem::zeroed() };
        disable.conn_handle = conn_handle;
        disable.set_disable(ignore as u8); // 0 or 1

        let ret = unsafe {
            raw::sd_ble_opt_set(
                raw::BLE_GAP_OPTS_BLE_GAP_OPT_SLAVE_LATENCY_DISABLE,
                &raw::ble_opt_t {
                    gap_opt: raw::ble_gap_opt_t {
                        slave_latency_disable: disable,
                    },
                },
            )
        };
        if let Err(err) = RawError::convert(ret) {
            warn!("ignore_slave_latency sd_ble_opt_set err {:?}", err);
            return Err(err.into());
        }

        Ok(())
    }

    pub(crate) fn with_state<T>(&self, f: impl FnOnce(&mut ConnectionState) -> T) -> T {
        with_state(self.index, f)
    }

    pub fn iter() -> ConnectionIter {
        ConnectionIter(0)
    }

    /// Send a request to the connected device to change the PHY.
    ///
    /// Note that this just initiates the PHY change, it does not wait for completion.
    /// Immediately after return, the active PHYs will still be the old ones, and after some time
    /// they should change to the new ones.
    pub fn phy_update(&mut self, tx_phys: PhySet, rx_phys: PhySet) -> Result<(), PhyUpdateError> {
        let conn_handle = self.with_state(|state| state.check_connected())?;
        let p_gap_phys = raw::ble_gap_phys_t {
            tx_phys: tx_phys as u8,
            rx_phys: rx_phys as u8,
        };
        let ret = unsafe { raw::sd_ble_gap_phy_update(conn_handle, &p_gap_phys) };
        if let Err(err) = RawError::convert(ret) {
            warn!("sd_ble_gap_phy_update err {:?}", err);
            return Err(err.into());
        }

        Ok(())
    }
}

pub struct ConnectionIter(u8);

impl Iterator for ConnectionIter {
    type Item = Connection;

    fn next(&mut self) -> Option<Self::Item> {
        let n = usize::from(self.0);
        if n < CONNS_MAX {
            unsafe {
                for (i, s) in STATES[n..].iter().enumerate() {
                    let state = &mut *s.get();
                    if state.conn_handle.is_some() {
                        let index = (n + i) as u8;
                        state.refcount =
                            unwrap!(state.refcount.checked_add(1), "Too many references to same connection");
                        self.0 = index + 1;
                        return Some(Connection { index });
                    }
                }
            }
            self.0 = CONNS_MAX as u8;
        }
        None
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        (0, Some(CONNS_MAX - usize::from(self.0)))
    }
}

impl FusedIterator for ConnectionIter {}

// ConnectionStates by index.
const DUMMY_STATE: UnsafeCell<ConnectionState> = UnsafeCell::new(ConnectionState::dummy());
static mut STATES: [UnsafeCell<ConnectionState>; CONNS_MAX] = [DUMMY_STATE; CONNS_MAX];

pub(crate) fn with_state_by_conn_handle<T>(conn_handle: u16, f: impl FnOnce(&mut ConnectionState) -> T) -> T {
    let index = unwrap!(
        index_by_handle(conn_handle).get(),
        "bug: with_state_by_conn_handle on conn_handle that has no state"
    );
    with_state(index, f)
}

pub(crate) fn with_state<T>(index: u8, f: impl FnOnce(&mut ConnectionState) -> T) -> T {
    let state = unsafe { &mut *STATES[index as usize].get() };
    f(state)
}

fn allocate_index<T>(f: impl FnOnce(u8, &mut ConnectionState) -> T) -> Result<T, OutOfConnsError> {
    unsafe {
        for (i, s) in STATES.iter().enumerate() {
            let state = &mut *s.get();
            if state.refcount == 0 && state.conn_handle.is_none() {
                return Ok(f(i as u8, state));
            }
        }
        Err(OutOfConnsError)
    }
}

// conn_handle -> index mapping. Used to make stuff go faster
const INDEX_NONE: Cell<Option<u8>> = Cell::new(None);
static mut INDEX_BY_HANDLE: [Cell<Option<u8>>; CONNS_MAX] = [INDEX_NONE; CONNS_MAX];

fn index_by_handle(conn_handle: u16) -> &'static Cell<Option<u8>> {
    unsafe { &INDEX_BY_HANDLE[conn_handle as usize] }
}