From 95fb6b2747c225cfbfcd9af28ee45d81bea70e6f Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 23 Feb 2017 14:56:10 +0700 Subject: [PATCH] update pairing/bonding. Able to pair/bond with windows 10 --- boards.txt | 2 +- .../examples/hid_keyboard/hid_keyboard.ino | 8 +- libraries/Bluefruit52Lib/src/bluefruit.cpp | 76 ++++++++++--------- libraries/Bluefruit52Lib/src/bluefruit.h | 12 ++- 4 files changed, 53 insertions(+), 45 deletions(-) diff --git a/boards.txt b/boards.txt index 1a2e4f45..fd6d6694 100644 --- a/boards.txt +++ b/boards.txt @@ -15,7 +15,7 @@ # License along with this library; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -menu.debug=Debug Level +menu.debug=Debug feather52.name=Adafruit Bluefruit nRF52 Feather diff --git a/libraries/Bluefruit52Lib/examples/hid_keyboard/hid_keyboard.ino b/libraries/Bluefruit52Lib/examples/hid_keyboard/hid_keyboard.ino index 672b10f8..a38ac657 100644 --- a/libraries/Bluefruit52Lib/examples/hid_keyboard/hid_keyboard.ino +++ b/libraries/Bluefruit52Lib/examples/hid_keyboard/hid_keyboard.ino @@ -72,9 +72,7 @@ void loop() { hasKeyPressed = false; blehid.keyRelease(); - - blehid.consumerKeyRelease(); - + // Delay a bit after a report delay(5); } @@ -88,9 +86,7 @@ void loop() blehid.keyPress(ch); hasKeyPressed = true; - - blehid.consumerKeyPress(HID_USAGE_CONSUMER_MUTE); - + // Delay a bit after a report delay(5); } diff --git a/libraries/Bluefruit52Lib/src/bluefruit.cpp b/libraries/Bluefruit52Lib/src/bluefruit.cpp index e5f51e12..f8bf9206 100644 --- a/libraries/Bluefruit52Lib/src/bluefruit.cpp +++ b/libraries/Bluefruit52Lib/src/bluefruit.cpp @@ -106,8 +106,8 @@ AdafruitBluefruit::AdafruitBluefruit(void) _chars_count = 0; for(uint8_t i=0; ievt.gap_evt.params.connected; if (para->role == BLE_GAP_ROLE_PERIPH) @@ -632,14 +629,13 @@ void AdafruitBluefruit::_poll(void) break; case BLE_GAP_EVT_DISCONNECTED: - // PRINT_MESS("Disconnected"); - // Check if it is peripheral connection if (_conn_hdl == evt->evt.gap_evt.conn_handle) { if (_led_conn) ledOff(LED_CONN); _conn_hdl = BLE_GATT_HANDLE_INVALID; + _bonded = false; varclr(&_peer_addr); vSemaphoreDelete(_txbuf_sem); @@ -667,33 +663,19 @@ void AdafruitBluefruit::_poll(void) case BLE_GAP_EVT_CONN_PARAM_UPDATE: { + // Central set the connection parameter // ble_gap_conn_params_t* param = &evt->evt.gap_evt.params.conn_param_update.conn_params; // PRINT_INT(param->min_conn_interval); // PRINT_INT(param->max_conn_interval); } break; - case BLE_GAP_EVT_SEC_INFO_REQUEST: - { -// PRINT_MESS("BLE_GAP_EVT_SEC_INFO_REQUEST"); - // If bonded previously, return information. Otherwise NULL - ble_gap_evt_sec_info_request_t* sec_request = (ble_gap_evt_sec_info_request_t*) &evt->evt.gap_evt.params.sec_info_request; - - if (_enc_key.master_id.ediv == sec_request->master_id.ediv) - { - PRINT_LOCATION(); - sd_ble_gap_sec_info_reply(evt->evt.gap_evt.conn_handle, &_enc_key.enc_info, &_peer_id.id_info, NULL); - } else - { - PRINT_LOCATION(); - sd_ble_gap_sec_info_reply(evt->evt.gap_evt.conn_handle, NULL, NULL, NULL); - } - } - break; - case BLE_GAP_EVT_SEC_PARAMS_REQUEST: { -// PRINT_MESS("BLE_GAP_EVT_SEC_PARAMS_REQUEST"); + /* Step 1: Pairing/Bonding + * - Central supplies its parameters + * - We replies with our security parameters + */ // ble_gap_sec_params_t* peer = &evt->evt.gap_evt.params.sec_params_request.peer_params; ble_gap_sec_params_t sec_para = @@ -705,21 +687,23 @@ void AdafruitBluefruit::_poll(void) .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 , .oob = 0, .min_key_size = 7, - .max_key_size = 16 + .max_key_size = 16, + .kdist_own = { .enc = 1, .id = 1}, + .kdist_peer = { .enc = 1, .id = 1}, }; ble_gap_sec_keyset_t keyset = { .keys_own = { - .p_enc_key = &_enc_key, + .p_enc_key = &_bond.own_enc, .p_id_key = NULL, .p_sign_key = NULL, .p_pk = NULL }, .keys_peer = { - .p_enc_key = NULL, - .p_id_key = &_peer_id, + .p_enc_key = &_bond.peer_enc, + .p_id_key = &_bond.peer_id, .p_sign_key = NULL, .p_pk = NULL } @@ -729,14 +713,37 @@ void AdafruitBluefruit::_poll(void) } break; + case BLE_GAP_EVT_SEC_INFO_REQUEST: + { + // If bonded previously, Central will ask for stored keys. + // return security information. Otherwise NULL + ble_gap_evt_sec_info_request_t* sec_request = (ble_gap_evt_sec_info_request_t*) &evt->evt.gap_evt.params.sec_info_request; + + PRINT_HEX(sec_request->master_id.ediv); + if (_bond.own_enc.master_id.ediv == sec_request->master_id.ediv) + { + PRINT_LOCATION(); + sd_ble_gap_sec_info_reply(evt->evt.gap_evt.conn_handle, &_bond.own_enc.enc_info, &_bond.peer_id.id_info, NULL); + } else + { + PRINT_LOCATION(); + sd_ble_gap_sec_info_reply(evt->evt.gap_evt.conn_handle, NULL, NULL, NULL); + } + } + break; + + case BLE_GAP_EVT_PASSKEY_DISPLAY: break; case BLE_GAP_EVT_CONN_SEC_UPDATE: { - //PRINT_MESS("BLE_GAP_EVT_CONN_SEC_UPDATE"); + // Connection is secured ble_gap_conn_sec_t* conn_sec = (ble_gap_conn_sec_t*) &evt->evt.gap_evt.params.conn_sec_update.conn_sec; (void) conn_sec; + // TODO check if this connection is bonded + _bonded = true; + // Connection is secured, Apply Service Context sd_ble_gatts_sys_attr_set(_conn_hdl, _sys_attr, _sys_attr_len, SVC_CONTEXT_FLAG); } @@ -746,7 +753,7 @@ void AdafruitBluefruit::_poll(void) { ble_gap_evt_auth_status_t* status = &evt->evt.gap_evt.params.auth_status; // Bonding succeeded --> save encryption keys - //PRINT_HEX(status->auth_status); + PRINT_HEX(status->auth_status); if (BLE_GAP_SEC_STATUS_SUCCESS == status->auth_status) { _bonded = true; @@ -755,7 +762,6 @@ void AdafruitBluefruit::_poll(void) break; case BLE_GATTS_EVT_SYS_ATTR_MISSING: - //PRINT_MESS("BLE_GATTS_EVT_SYS_ATTR_MISSING"); sd_ble_gatts_sys_attr_set(_conn_hdl, NULL, 0, 0); break; diff --git a/libraries/Bluefruit52Lib/src/bluefruit.h b/libraries/Bluefruit52Lib/src/bluefruit.h index fc629238..f5540de1 100644 --- a/libraries/Bluefruit52Lib/src/bluefruit.h +++ b/libraries/Bluefruit52Lib/src/bluefruit.h @@ -167,8 +167,12 @@ class AdafruitBluefruit bool _bonded; // TODO move to bonding place - ble_gap_enc_key_t _enc_key; - ble_gap_id_key_t _peer_id; + struct { + ble_gap_enc_key_t own_enc; + ble_gap_enc_key_t peer_enc; + ble_gap_id_key_t peer_id; + }_bond; + ble_gap_addr_t _peer_addr; @@ -183,10 +187,12 @@ class AdafruitBluefruit disconnect_callback_t _discconnect_cb; bool _addToAdv(bool scan_resp, uint8_t type, const void* data, uint8_t len); - void _poll(void); + void _startConnLed(void); void _stopConnLed(void); + void _poll(void); + friend void adafruit_bluefruit_task(void* arg); friend class BLECentral; };