Skip to content

Commit 95fb6b2

Browse files
committed
update pairing/bonding. Able to pair/bond with windows 10
1 parent 9a1d6bf commit 95fb6b2

File tree

4 files changed

+53
-45
lines changed

4 files changed

+53
-45
lines changed

boards.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# License along with this library; if not, write to the Free Software
1616
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1717

18-
menu.debug=Debug Level
18+
menu.debug=Debug
1919

2020
feather52.name=Adafruit Bluefruit nRF52 Feather
2121

libraries/Bluefruit52Lib/examples/hid_keyboard/hid_keyboard.ino

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,7 @@ void loop()
7272
{
7373
hasKeyPressed = false;
7474
blehid.keyRelease();
75-
76-
blehid.consumerKeyRelease();
77-
75+
7876
// Delay a bit after a report
7977
delay(5);
8078
}
@@ -88,9 +86,7 @@ void loop()
8886

8987
blehid.keyPress(ch);
9088
hasKeyPressed = true;
91-
92-
blehid.consumerKeyPress(HID_USAGE_CONSUMER_MUTE);
93-
89+
9490
// Delay a bit after a report
9591
delay(5);
9692
}

libraries/Bluefruit52Lib/src/bluefruit.cpp

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ AdafruitBluefruit::AdafruitBluefruit(void)
106106
_chars_count = 0;
107107
for(uint8_t i=0; i<BLE_MAX_CHARS; i++) _chars_list[i] = NULL;
108108

109-
varclr(&_enc_key);
110-
varclr(&_peer_id);
109+
varclr(&_bond);
110+
111111
_sys_attr = NULL;
112112
_sys_attr_len = 0;
113113

@@ -582,10 +582,8 @@ void AdafruitBluefruit::_poll(void)
582582
default: break;
583583
}
584584
}
585-
else
586-
{
587-
// Error, do nothing nowx
588-
}
585+
// Error, do nothing now
586+
else { }
589587

590588
/*------------- BLE Event -------------*/
591589
uint32_t ev_buf[BLE_STACK_EVT_MSG_BUF_SIZE/4 + 4];
@@ -611,7 +609,6 @@ void AdafruitBluefruit::_poll(void)
611609
{
612610
case BLE_GAP_EVT_CONNECTED:
613611
{
614-
// PRINT_MESS("Connected");
615612
ble_gap_evt_connected_t* para = &evt->evt.gap_evt.params.connected;
616613

617614
if (para->role == BLE_GAP_ROLE_PERIPH)
@@ -632,14 +629,13 @@ void AdafruitBluefruit::_poll(void)
632629
break;
633630

634631
case BLE_GAP_EVT_DISCONNECTED:
635-
// PRINT_MESS("Disconnected");
636-
637632
// Check if it is peripheral connection
638633
if (_conn_hdl == evt->evt.gap_evt.conn_handle)
639634
{
640635
if (_led_conn) ledOff(LED_CONN);
641636

642637
_conn_hdl = BLE_GATT_HANDLE_INVALID;
638+
_bonded = false;
643639
varclr(&_peer_addr);
644640

645641
vSemaphoreDelete(_txbuf_sem);
@@ -667,33 +663,19 @@ void AdafruitBluefruit::_poll(void)
667663

668664
case BLE_GAP_EVT_CONN_PARAM_UPDATE:
669665
{
666+
// Central set the connection parameter
670667
// ble_gap_conn_params_t* param = &evt->evt.gap_evt.params.conn_param_update.conn_params;
671668
// PRINT_INT(param->min_conn_interval);
672669
// PRINT_INT(param->max_conn_interval);
673670
}
674671
break;
675672

676-
case BLE_GAP_EVT_SEC_INFO_REQUEST:
677-
{
678-
// PRINT_MESS("BLE_GAP_EVT_SEC_INFO_REQUEST");
679-
// If bonded previously, return information. Otherwise NULL
680-
ble_gap_evt_sec_info_request_t* sec_request = (ble_gap_evt_sec_info_request_t*) &evt->evt.gap_evt.params.sec_info_request;
681-
682-
if (_enc_key.master_id.ediv == sec_request->master_id.ediv)
683-
{
684-
PRINT_LOCATION();
685-
sd_ble_gap_sec_info_reply(evt->evt.gap_evt.conn_handle, &_enc_key.enc_info, &_peer_id.id_info, NULL);
686-
} else
687-
{
688-
PRINT_LOCATION();
689-
sd_ble_gap_sec_info_reply(evt->evt.gap_evt.conn_handle, NULL, NULL, NULL);
690-
}
691-
}
692-
break;
693-
694673
case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
695674
{
696-
// PRINT_MESS("BLE_GAP_EVT_SEC_PARAMS_REQUEST");
675+
/* Step 1: Pairing/Bonding
676+
* - Central supplies its parameters
677+
* - We replies with our security parameters
678+
*/
697679
// ble_gap_sec_params_t* peer = &evt->evt.gap_evt.params.sec_params_request.peer_params;
698680

699681
ble_gap_sec_params_t sec_para =
@@ -705,21 +687,23 @@ void AdafruitBluefruit::_poll(void)
705687
.io_caps = BLE_GAP_IO_CAPS_NONE, // (CFG_PIN_ENABLED && nvm_data.core.passkey_enable) ? BLE_GAP_IO_CAPS_DISPLAY_ONLY : BLE_GAP_IO_CAPS_NONE ,
706688
.oob = 0,
707689
.min_key_size = 7,
708-
.max_key_size = 16
690+
.max_key_size = 16,
691+
.kdist_own = { .enc = 1, .id = 1},
692+
.kdist_peer = { .enc = 1, .id = 1},
709693
};
710694

711695
ble_gap_sec_keyset_t keyset =
712696
{
713697
.keys_own = {
714-
.p_enc_key = &_enc_key,
698+
.p_enc_key = &_bond.own_enc,
715699
.p_id_key = NULL,
716700
.p_sign_key = NULL,
717701
.p_pk = NULL
718702
},
719703

720704
.keys_peer = {
721-
.p_enc_key = NULL,
722-
.p_id_key = &_peer_id,
705+
.p_enc_key = &_bond.peer_enc,
706+
.p_id_key = &_bond.peer_id,
723707
.p_sign_key = NULL,
724708
.p_pk = NULL
725709
}
@@ -729,14 +713,37 @@ void AdafruitBluefruit::_poll(void)
729713
}
730714
break;
731715

716+
case BLE_GAP_EVT_SEC_INFO_REQUEST:
717+
{
718+
// If bonded previously, Central will ask for stored keys.
719+
// return security information. Otherwise NULL
720+
ble_gap_evt_sec_info_request_t* sec_request = (ble_gap_evt_sec_info_request_t*) &evt->evt.gap_evt.params.sec_info_request;
721+
722+
PRINT_HEX(sec_request->master_id.ediv);
723+
if (_bond.own_enc.master_id.ediv == sec_request->master_id.ediv)
724+
{
725+
PRINT_LOCATION();
726+
sd_ble_gap_sec_info_reply(evt->evt.gap_evt.conn_handle, &_bond.own_enc.enc_info, &_bond.peer_id.id_info, NULL);
727+
} else
728+
{
729+
PRINT_LOCATION();
730+
sd_ble_gap_sec_info_reply(evt->evt.gap_evt.conn_handle, NULL, NULL, NULL);
731+
}
732+
}
733+
break;
734+
735+
732736
case BLE_GAP_EVT_PASSKEY_DISPLAY: break;
733737

734738
case BLE_GAP_EVT_CONN_SEC_UPDATE:
735739
{
736-
//PRINT_MESS("BLE_GAP_EVT_CONN_SEC_UPDATE");
740+
// Connection is secured
737741
ble_gap_conn_sec_t* conn_sec = (ble_gap_conn_sec_t*) &evt->evt.gap_evt.params.conn_sec_update.conn_sec;
738742
(void) conn_sec;
739743

744+
// TODO check if this connection is bonded
745+
_bonded = true;
746+
740747
// Connection is secured, Apply Service Context
741748
sd_ble_gatts_sys_attr_set(_conn_hdl, _sys_attr, _sys_attr_len, SVC_CONTEXT_FLAG);
742749
}
@@ -746,7 +753,7 @@ void AdafruitBluefruit::_poll(void)
746753
{
747754
ble_gap_evt_auth_status_t* status = &evt->evt.gap_evt.params.auth_status;
748755
// Bonding succeeded --> save encryption keys
749-
//PRINT_HEX(status->auth_status);
756+
PRINT_HEX(status->auth_status);
750757
if (BLE_GAP_SEC_STATUS_SUCCESS == status->auth_status)
751758
{
752759
_bonded = true;
@@ -755,7 +762,6 @@ void AdafruitBluefruit::_poll(void)
755762
break;
756763

757764
case BLE_GATTS_EVT_SYS_ATTR_MISSING:
758-
//PRINT_MESS("BLE_GATTS_EVT_SYS_ATTR_MISSING");
759765
sd_ble_gatts_sys_attr_set(_conn_hdl, NULL, 0, 0);
760766
break;
761767

libraries/Bluefruit52Lib/src/bluefruit.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,12 @@ class AdafruitBluefruit
167167
bool _bonded;
168168

169169
// TODO move to bonding place
170-
ble_gap_enc_key_t _enc_key;
171-
ble_gap_id_key_t _peer_id;
170+
struct {
171+
ble_gap_enc_key_t own_enc;
172+
ble_gap_enc_key_t peer_enc;
173+
ble_gap_id_key_t peer_id;
174+
}_bond;
175+
172176

173177
ble_gap_addr_t _peer_addr;
174178

@@ -183,10 +187,12 @@ class AdafruitBluefruit
183187
disconnect_callback_t _discconnect_cb;
184188

185189
bool _addToAdv(bool scan_resp, uint8_t type, const void* data, uint8_t len);
186-
void _poll(void);
190+
187191
void _startConnLed(void);
188192
void _stopConnLed(void);
189193

194+
void _poll(void);
195+
190196
friend void adafruit_bluefruit_task(void* arg);
191197
friend class BLECentral;
192198
};

0 commit comments

Comments
 (0)