Skip to content

Commit f35ca05

Browse files
committed
added secured callback
1 parent f0440b5 commit f35ca05

File tree

4 files changed

+45
-32
lines changed

4 files changed

+45
-32
lines changed

libraries/Bluefruit52Lib/src/BLEConnection.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ BLEConnection::BLEConnection(uint16_t conn_hdl, ble_gap_evt_connected_t const* e
5555
_hvn_sem = xSemaphoreCreateCounting(hvn_qsize, hvn_qsize);
5656
_wrcmd_sem = xSemaphoreCreateCounting(wrcmd_qsize, wrcmd_qsize);
5757

58-
_secured = false;
58+
_sec_mode.sm = _sec_mode.lv = 1; // default to open
59+
5960
_bonded = false;
6061
_hvc_sem = NULL;
6162
_hvc_received = false;
@@ -84,7 +85,7 @@ bool BLEConnection::connected(void)
8485

8586
bool BLEConnection::paired (void)
8687
{
87-
return _secured;
88+
return secured();
8889
}
8990

9091
bool BLEConnection::bonded(void)
@@ -94,7 +95,7 @@ bool BLEConnection::bonded(void)
9495

9596
bool BLEConnection::secured(void)
9697
{
97-
return _secured;
98+
return !(_sec_mode.sm == 1 && _sec_mode.lv == 1);
9899
}
99100

100101
uint8_t BLEConnection::getRole (void)
@@ -132,6 +133,11 @@ uint16_t BLEConnection::getPeerName(char* buf, uint16_t bufsize)
132133
return Bluefruit.Gatt.readCharByUuid(_conn_hdl, BLEUuid(BLE_UUID_GAP_CHARACTERISTIC_DEVICE_NAME), buf, bufsize);
133134
}
134135

136+
ble_gap_conn_sec_mode_t BLEConnection::getSecureMode(void)
137+
{
138+
return _sec_mode;
139+
}
140+
135141
static inline bool is_tx_power_valid(int8_t power)
136142
{
137143
#if defined(NRF52832_XXAA)
@@ -267,7 +273,7 @@ bool BLEConnection::removeBondKey(void)
267273
bool BLEConnection::requestPairing(void)
268274
{
269275
// skip if already paired
270-
if ( _secured ) return true;
276+
if ( secured() ) return true;
271277

272278
return Bluefruit.Pairing._authenticate(_conn_hdl);
273279
}
@@ -299,11 +305,11 @@ void BLEConnection::_eventHandler(ble_evt_t* evt)
299305
{
300306
const ble_gap_conn_sec_t* conn_sec = &evt->evt.gap_evt.params.conn_sec_update.conn_sec;
301307

308+
_sec_mode = conn_sec->sec_mode;
309+
302310
// Connection is secured (paired) if encryption level > 1
303-
if ( !( conn_sec->sec_mode.sm == 1 && conn_sec->sec_mode.lv == 1) )
311+
if ( this->secured() )
304312
{
305-
_secured = true;
306-
307313
// Try to restore CCCD with bonded peer, if it doesn't exist (newly bonded), initialize it
308314
if ( !loadCccd() ) sd_ble_gatts_sys_attr_set(_conn_hdl, NULL, 0, 0);
309315
}

libraries/Bluefruit52Lib/src/BLEConnection.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,11 @@ class BLEConnection
5454
uint16_t _ediv;
5555

5656
bool _connected;
57-
bool _secured;
5857
bool _bonded; // have LTK stored in InternalFS
5958
bool _hvc_received;
6059

60+
ble_gap_conn_sec_mode_t _sec_mode;
61+
6162
ble_gap_addr_t _peer_addr; // resolvable connect address
6263
ble_gap_addr_t _bond_id_addr; // address stored as bonded
6364

@@ -66,7 +67,6 @@ class BLEConnection
6667

6768
// On-demand semaphore/data that are created on the fly
6869
SemaphoreHandle_t _hvc_sem;
69-
SemaphoreHandle_t _pair_sem;
7070

7171
public:
7272
BLEConnection(uint16_t conn_hdl, ble_gap_evt_connected_t const * evt_connected, uint8_t hvn_qsize, uint8_t wrcmd_qsize);
@@ -87,6 +87,8 @@ class BLEConnection
8787
ble_gap_addr_t getPeerAddr(void);
8888
uint16_t getPeerName(char* buf, uint16_t bufsize);
8989

90+
ble_gap_conn_sec_mode_t getSecureMode(void);
91+
9092
bool disconnect(void);
9193

9294
bool setTxPower(int8_t power); // set power for this connection

libraries/Bluefruit52Lib/src/BLEPairing.cpp

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,21 @@
3333
#define LESC_SUPPORTED 0
3434
#endif
3535

36+
// default is Just Work
37+
static const ble_gap_sec_params_t _sec_param_default =
38+
{
39+
.bond = 1,
40+
.mitm = 0,
41+
.lesc = LESC_SUPPORTED,
42+
.keypress = 0,
43+
.io_caps = BLE_GAP_IO_CAPS_NONE,
44+
.oob = 0,
45+
.min_key_size = 7,
46+
.max_key_size = 16,
47+
.kdist_own = { .enc = 1, .id = 1},
48+
.kdist_peer = { .enc = 1, .id = 1}
49+
};
50+
3651
//------------- IMPLEMENTATION -------------//
3752

3853
// convert N-byte Number from Big <-> Little Endian to use with BLE
@@ -50,20 +65,16 @@ static void swap_endian(uint8_t data[], uint32_t nbytes)
5065
}
5166
}
5267

53-
// default is Just Work
54-
static const ble_gap_sec_params_t _sec_param_default =
68+
static void _passkey_display_cabllack_dfr(BLEPairing::pair_passkey_cb_t func, uint16_t conn_hdl, uint8_t const passkey[6], bool match_request)
5569
{
56-
.bond = 1,
57-
.mitm = 0,
58-
.lesc = LESC_SUPPORTED,
59-
.keypress = 0,
60-
.io_caps = BLE_GAP_IO_CAPS_NONE,
61-
.oob = 0,
62-
.min_key_size = 7,
63-
.max_key_size = 16,
64-
.kdist_own = { .enc = 1, .id = 1},
65-
.kdist_peer = { .enc = 1, .id = 1}
66-
};
70+
bool matched = func(conn_hdl, passkey, match_request);
71+
72+
if (match_request)
73+
{
74+
// Match request require to report the match (numberic comparison)
75+
sd_ble_gap_auth_key_reply(conn_hdl, matched ? BLE_GAP_AUTH_KEY_TYPE_PASSKEY : BLE_GAP_AUTH_KEY_TYPE_NONE, NULL);
76+
}
77+
}
6778

6879
BLEPairing::BLEPairing(void)
6980
{
@@ -294,13 +305,7 @@ void BLEPairing::_eventHandler(ble_evt_t* evt)
294305
// Invoke display callback
295306
if ( _passkey_cb )
296307
{
297-
bool matched = _passkey_cb(conn_hdl, passkey_display->passkey, passkey_display->match_request);
298-
299-
if (passkey_display->match_request)
300-
{
301-
// Match request require to report the match (numberic comparison)
302-
sd_ble_gap_auth_key_reply(conn_hdl, matched ? BLE_GAP_AUTH_KEY_TYPE_PASSKEY : BLE_GAP_AUTH_KEY_TYPE_NONE, NULL);
303-
}
308+
ada_callback(passkey_display->passkey, 6, _passkey_display_cabllack_dfr, _passkey_cb, conn_hdl, passkey_display->passkey, passkey_display->match_request);
304309
}
305310
}
306311
break;
@@ -392,9 +397,9 @@ void BLEPairing::_eventHandler(ble_evt_t* evt)
392397
const ble_gap_conn_sec_t* conn_sec = &evt->evt.gap_evt.params.conn_sec_update.conn_sec;
393398
LOG_LV2("PAIR", "Security Mode = %d, Level = %d", conn_sec->sec_mode.sm, conn_sec->sec_mode.lv);
394399

395-
if ( conn->secured() && _secured_cb )
400+
if ( _secured_cb )
396401
{
397-
ada_callback(NULL, 0, _secured_cb, conn_hdl, conn_sec->sec_mode.sm, conn_sec->sec_mode.lv);
402+
ada_callback(NULL, 0, _secured_cb, conn_hdl);
398403
}
399404
}
400405
break;

libraries/Bluefruit52Lib/src/BLEPairing.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class BLEPairing
3737
public:
3838
typedef bool (*pair_passkey_cb_t ) (uint16_t conn_hdl, uint8_t const passkey[6], bool match_request);
3939
typedef void (*pair_complete_cb_t) (uint16_t conn_hdl, uint8_t auth_status);
40-
typedef void (*pair_secured_cb_t) (uint16_t conn_hdl, uint8_t sec_mode, uint8_t level);
40+
typedef void (*pair_secured_cb_t) (uint16_t conn_hdl);
4141

4242
BLEPairing(void);
4343

0 commit comments

Comments
 (0)