Skip to content

Commit 620d2e5

Browse files
committed
Improvements to pairing authentication support, mouse press/release, and a few helpers
1 parent 527e62d commit 620d2e5

File tree

10 files changed

+130
-0
lines changed

10 files changed

+130
-0
lines changed

libraries/Bluefruit52Lib/src/BLEPeriph.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ uint8_t BLEPeriph::connected (void)
7878
return count;
7979
}
8080

81+
bool BLEPeriph::getBonds(bonded_device_info* bondedPeers, uint32_t bondedPeersSize, uint32_t* actualBondedPeersSize)
82+
{
83+
return bond_get_list(BLE_GAP_ROLE_PERIPH, bondedPeers, bondedPeersSize, actualBondedPeersSize);
84+
}
85+
8186
void BLEPeriph::clearBonds(void)
8287
{
8388
bond_clear_prph();

libraries/Bluefruit52Lib/src/BLEPeriph.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939

4040
#include <Arduino.h>
4141
#include "bluefruit_common.h"
42+
#include "utility/bonding.h"
4243

4344
class BLEPeriph
4445
{
@@ -50,6 +51,7 @@ class BLEPeriph
5051
bool connected(uint16_t conn_hdl); // Connected as prph to this connection
5152
uint8_t connected(void); // Number of connected as peripherals
5253

54+
bool getBonds(bonded_device_info* bondedPeers, uint32_t bondedPeersSize, uint32_t* actualBondedPeersSize);
5355
void clearBonds(void);
5456

5557
bool setConnInterval (uint16_t min, uint16_t max);

libraries/Bluefruit52Lib/src/BLESecurity.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,16 @@ static void _passkey_display_cabllack_dfr(BLESecurity::pair_passkey_cb_t func, u
7676
}
7777
}
7878

79+
static void _passkey_request_callback_dfr(BLESecurity::pair_passkey_req_cb_t func, uint16_t conn_hdl)
80+
{
81+
func(conn_hdl);
82+
}
83+
7984
BLESecurity::BLESecurity(void)
8085
{
8186
_sec_param = _sec_param_default;
8287
_passkey_cb = NULL;
88+
_passkey_req_cb = NULL;
8389
_complete_cb = NULL;
8490
_secured_cb = NULL;
8591
}
@@ -207,6 +213,11 @@ bool BLESecurity::setPairPasskeyCallback(pair_passkey_cb_t fp)
207213
return true;
208214
}
209215

216+
void BLESecurity::setPairPasskeyRequestedCallback(pair_passkey_req_cb_t fp)
217+
{
218+
_passkey_req_cb = fp;
219+
}
220+
210221
void BLESecurity::setPairCompleteCallback(pair_complete_cb_t fp)
211222
{
212223
_complete_cb = fp;
@@ -217,6 +228,12 @@ void BLESecurity::setSecuredCallback(secured_conn_cb_t fp)
217228
_secured_cb = fp;
218229
}
219230

231+
bool BLESecurity::enterRequestedPasskey(uint16_t conn_hdl, uint8_t const* passkey)
232+
{
233+
VERIFY_STATUS(sd_ble_gap_auth_key_reply(conn_hdl, BLE_GAP_AUTH_KEY_TYPE_PASSKEY, passkey), false);
234+
return true;
235+
}
236+
220237
bool BLESecurity::_authenticate(uint16_t conn_hdl)
221238
{
222239
VERIFY_STATUS(sd_ble_gap_authenticate(conn_hdl, &_sec_param ), false);
@@ -308,6 +325,16 @@ void BLESecurity::_eventHandler(ble_evt_t* evt)
308325
}
309326
break;
310327

328+
case BLE_GAP_EVT_AUTH_KEY_REQUEST:
329+
{
330+
LOG_LV2("PAIR", "Passkey requested");
331+
if (_passkey_req_cb)
332+
{
333+
ada_callback(NULL, 0, _passkey_request_callback_dfr, _passkey_req_cb, conn_hdl);
334+
}
335+
}
336+
break;
337+
311338
#ifdef NRF_CRYPTOCELL
312339
case BLE_GAP_EVT_LESC_DHKEY_REQUEST:
313340
{

libraries/Bluefruit52Lib/src/BLESecurity.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class BLESecurity
3636
{
3737
public:
3838
typedef bool (*pair_passkey_cb_t ) (uint16_t conn_hdl, uint8_t const passkey[6], bool match_request);
39+
typedef void (*pair_passkey_req_cb_t) (uint16_t conn_hdl);
3940
typedef void (*pair_complete_cb_t) (uint16_t conn_hdl, uint8_t auth_status);
4041
typedef void (*secured_conn_cb_t) (uint16_t conn_hdl);
4142

@@ -57,9 +58,12 @@ class BLESecurity
5758

5859
//------------- Callbacks -------------//
5960
bool setPairPasskeyCallback(pair_passkey_cb_t fp);
61+
void setPairPasskeyRequestedCallback(pair_passkey_req_cb_t fp);
6062
void setPairCompleteCallback(pair_complete_cb_t fp);
6163
void setSecuredCallback(secured_conn_cb_t fp);
6264

65+
bool enterRequestedPasskey(uint16_t conn_hdl, uint8_t const* passkey);
66+
6367
/*------------------------------------------------------------------*/
6468
/* INTERNAL USAGE ONLY
6569
* Although declare as public, it is meant to be invoked by internal
@@ -82,6 +86,7 @@ class BLESecurity
8286
bond_keys_t _bond_keys; // Shared keys with bonded device during securing connection, size ~ 80 bytes
8387

8488
pair_passkey_cb_t _passkey_cb;
89+
pair_passkey_req_cb_t _passkey_req_cb;
8590
pair_complete_cb_t _complete_cb;
8691
secured_conn_cb_t _secured_cb;
8792
};

libraries/Bluefruit52Lib/src/bluefruit.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,21 @@ bool AdafruitBluefruit::connected(uint16_t conn_hdl)
607607
return conn && conn->connected();
608608
}
609609

610+
void AdafruitBluefruit::getConnectionHandles(uint16_t* connectionHandles, uint8_t maxConnectionHandleCount, uint8_t* actualConnectionHandleCount)
611+
{
612+
uint8_t count = 0;
613+
for (uint16_t connectionHandle = 0; (connectionHandle < BLE_MAX_CONNECTION) && (count < maxConnectionHandleCount); ++connectionHandle)
614+
{
615+
if (this->connected(connectionHandle))
616+
{
617+
connectionHandles[count] = connectionHandle;
618+
count++;
619+
}
620+
}
621+
622+
*actualConnectionHandleCount = count;
623+
}
624+
610625
bool AdafruitBluefruit::disconnect(uint16_t conn_hdl)
611626
{
612627
BLEConnection* conn = this->Connection(conn_hdl);

libraries/Bluefruit52Lib/src/bluefruit.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ class AdafruitBluefruit
163163
uint8_t connected (void); // Number of connected
164164
bool connected (uint16_t conn_hdl);
165165

166+
void getConnectionHandles(uint16_t* connectionHandles, uint8_t maxConnectionHandleCount, uint8_t* actualConnectionHandleCount);
167+
166168
uint16_t connHandle (void);
167169

168170
// Alias to BLEConnection API()

libraries/Bluefruit52Lib/src/services/BLEHidAdafruit.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,16 @@ bool BLEHidAdafruit::mouseButtonRelease(uint16_t conn_hdl)
225225
return mouseReport(conn_hdl, 0, 0, 0, 0, 0);
226226
}
227227

228+
bool BLEHidAdafruit::mouseButtonPressSpecific(uint16_t conn_hdl, uint8_t buttons)
229+
{
230+
return mouseReport(conn_hdl, _mse_buttons | buttons, 0, 0, 0, 0);
231+
}
232+
233+
bool BLEHidAdafruit::mouseButtonReleaseSpecific(uint16_t conn_hdl, uint8_t buttons)
234+
{
235+
return mouseReport(conn_hdl, _mse_buttons & ~buttons, 0, 0, 0, 0);
236+
}
237+
228238
bool BLEHidAdafruit::mouseMove(uint16_t conn_hdl, int8_t x, int8_t y)
229239
{
230240
return mouseReport(conn_hdl, _mse_buttons, x, y, 0, 0);
@@ -307,6 +317,16 @@ bool BLEHidAdafruit::mouseButtonRelease(void)
307317
return mouseButtonRelease(BLE_CONN_HANDLE_INVALID);
308318
}
309319

320+
bool BLEHidAdafruit::mouseButtonPressSpecific(uint8_t buttons)
321+
{
322+
return mouseButtonPressSpecific(BLE_CONN_HANDLE_INVALID, buttons);
323+
}
324+
325+
bool BLEHidAdafruit::mouseButtonReleaseSpecific(uint8_t buttons)
326+
{
327+
return mouseButtonReleaseSpecific(BLE_CONN_HANDLE_INVALID, buttons);
328+
}
329+
310330
bool BLEHidAdafruit::mouseMove(int8_t x, int8_t y)
311331
{
312332
return mouseMove(BLE_CONN_HANDLE_INVALID, x, y);

libraries/Bluefruit52Lib/src/services/BLEHidAdafruit.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ class BLEHidAdafruit : public BLEHidGeneric
9090

9191
bool mouseButtonPress(uint8_t buttons);
9292
bool mouseButtonRelease(void);
93+
94+
bool mouseButtonPressSpecific(uint8_t buttons);
95+
bool mouseButtonReleaseSpecific(uint8_t buttons);
9396

9497
bool mouseMove(int8_t x, int8_t y);
9598
bool mouseScroll(int8_t scroll);
@@ -101,6 +104,9 @@ class BLEHidAdafruit : public BLEHidGeneric
101104

102105
bool mouseButtonPress(uint16_t conn_hdl, uint8_t buttons);
103106
bool mouseButtonRelease(uint16_t conn_hdl);
107+
108+
bool mouseButtonPressSpecific(uint16_t conn_hdl, uint8_t buttons);
109+
bool mouseButtonReleaseSpecific(uint16_t conn_hdl, uint8_t buttons);
104110

105111
bool mouseMove(uint16_t conn_hdl, int8_t x, int8_t y);
106112
bool mouseScroll(uint16_t conn_hdl, int8_t scroll);

libraries/Bluefruit52Lib/src/utility/bonding.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,47 @@ bool bond_load_cccd(uint8_t role, uint16_t conn_hdl, ble_gap_addr_t const* id_ad
292292
return loaded;
293293
}
294294

295+
bool bond_get_list(uint8_t role, bonded_device_info* bondedPeers, uint32_t bondedPeersSize, uint32_t* actualBondedPeersSize)
296+
{
297+
char const * dpath = (role == BLE_GAP_ROLE_PERIPH ? BOND_DIR_PRPH : BOND_DIR_CNTR);
298+
299+
File dir(dpath, FILE_O_READ, InternalFS);
300+
File file(InternalFS);
301+
302+
uint32_t peerIndex = 0;
303+
while ((file = dir.openNextFile(FILE_O_READ)) && (peerIndex < bondedPeersSize))
304+
{
305+
if (!file.isDirectory())
306+
{
307+
int len = file.read();
308+
bond_keys_t bondKeys{};
309+
if (len == sizeof(bondKeys))
310+
{
311+
file.read(&bondKeys, len);
312+
313+
bonded_device_info deviceInfo{};
314+
deviceInfo.address = bondKeys.peer_id.id_addr_info;
315+
316+
len = file.read();
317+
if (len > 0)
318+
{
319+
file.read(deviceInfo.name, min(len, sizeof(deviceInfo.name)));
320+
}
321+
322+
bondedPeers[peerIndex] = deviceInfo;
323+
peerIndex++;
324+
}
325+
}
326+
327+
file.close();
328+
}
329+
330+
file.close();
331+
dir.close();
332+
*actualBondedPeersSize = peerIndex;
333+
return true;
334+
}
335+
295336
void bond_print_list(uint8_t role)
296337
{
297338
char const * dpath = (role == BLE_GAP_ROLE_PERIPH ? BOND_DIR_PRPH : BOND_DIR_CNTR);

libraries/Bluefruit52Lib/src/utility/bonding.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ typedef struct
5252
ble_gap_id_key_t peer_id;
5353
} bond_keys_t;
5454

55+
typedef struct
56+
{
57+
ble_gap_addr_t address;
58+
char name[25];
59+
} bonded_device_info;
60+
5561
void bond_init(void);
5662
void bond_clear_prph(void);
5763
void bond_clear_cntr(void);
@@ -65,6 +71,7 @@ bool bond_load_keys(uint8_t role, ble_gap_addr_t* peer_addr, bond_keys_t* bkeys)
6571
bool bond_save_cccd (uint8_t role, uint16_t conn_hdl, ble_gap_addr_t const* id_addr);
6672
bool bond_load_cccd (uint8_t role, uint16_t conn_hdl, ble_gap_addr_t const* id_addr);
6773

74+
bool bond_get_list(uint8_t role, bonded_device_info* bondedPeers, uint32_t bondedPeersSize, uint32_t* actualBondedPeersSize);
6875
void bond_print_list(uint8_t role);
6976

7077
#endif /* BONDING_H_ */

0 commit comments

Comments
 (0)