From 4fab39f26a068a6b9aef4b413a4be4b65a550886 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 14 Feb 2018 00:22:40 +0700 Subject: [PATCH] improving bonding management --- libraries/Bluefruit52Lib/src/BLEGap.cpp | 4 + libraries/Bluefruit52Lib/src/BLEGap.h | 3 - libraries/Bluefruit52Lib/src/bluefruit.cpp | 88 ++++++------- libraries/Bluefruit52Lib/src/bluefruit.h | 11 +- .../Bluefruit52Lib/src/bluefruit_common.h | 2 + .../Bluefruit52Lib/src/utility/bonding.cpp | 120 ++++++++++++++++++ .../Bluefruit52Lib/src/utility/bonding.h | 67 ++++++++++ 7 files changed, 242 insertions(+), 53 deletions(-) create mode 100644 libraries/Bluefruit52Lib/src/utility/bonding.cpp create mode 100644 libraries/Bluefruit52Lib/src/utility/bonding.h diff --git a/libraries/Bluefruit52Lib/src/BLEGap.cpp b/libraries/Bluefruit52Lib/src/BLEGap.cpp index 75507a14..4f544777 100644 --- a/libraries/Bluefruit52Lib/src/BLEGap.cpp +++ b/libraries/Bluefruit52Lib/src/BLEGap.cpp @@ -240,6 +240,10 @@ void BLEGap::_eventHandler(ble_evt_t* evt) } break; + case BLE_GAP_EVT_SEC_PARAMS_REQUEST: + // Pairing in progress, Peer asking for our info + break; + #if SD_VER < 500 case BLE_EVT_TX_COMPLETE: if ( peer->hvn_tx_sem ) diff --git a/libraries/Bluefruit52Lib/src/BLEGap.h b/libraries/Bluefruit52Lib/src/BLEGap.h index d8e28d28..723ccdc4 100644 --- a/libraries/Bluefruit52Lib/src/BLEGap.h +++ b/libraries/Bluefruit52Lib/src/BLEGap.h @@ -110,9 +110,6 @@ class BLEGap SemaphoreHandle_t indicate_confirm_sem; bool hvc_received; - - // not really peer data -// ble_gap_sec_params_t sec_param; } gap_peer_t; gap_peer_t _peers[BLE_MAX_CONN]; diff --git a/libraries/Bluefruit52Lib/src/bluefruit.cpp b/libraries/Bluefruit52Lib/src/bluefruit.cpp index 123a80c7..6737d99f 100644 --- a/libraries/Bluefruit52Lib/src/bluefruit.cpp +++ b/libraries/Bluefruit52Lib/src/bluefruit.cpp @@ -36,6 +36,7 @@ #include "bluefruit.h" #include +#include "utility/bonding.h" #define SVC_CONTEXT_FLAG (BLE_GATTS_SYS_ATTR_FLAG_SYS_SRVCS | BLE_GATTS_SYS_ATTR_FLAG_USR_SRVCS) @@ -1057,7 +1058,8 @@ void AdafruitBluefruit::_ble_handler(ble_evt_t* evt) if (BLE_GAP_SEC_STATUS_SUCCESS == status->auth_status) { _bonded = true; - ada_callback(NULL, _adafruit_save_bond_key_dfr, _conn_hdl); +// ada_callback(NULL, _adafruit_save_bond_key_dfr, _conn_hdl); + bond_save_keys(evt_conn_hdl, &_bond_data); }else { PRINT_HEX(status->auth_status); @@ -1149,11 +1151,11 @@ void AdafruitBluefruit::clearBonds(void) * - Name 32 bytes * - CCCD variable *------------------------------------------------------------------*/ -void _adafruit_save_bond_key_dfr(uint32_t conn_handle) -{ - (void) conn_handle; - Bluefruit._saveBondKeys(); -} +//void _adafruit_save_bond_key_dfr(uint32_t conn_handle) +//{ +// (void) conn_handle; +// Bluefruit._saveBondKeys(); +//} void _adafruit_save_bond_cccd_dfr(uint32_t conn_handle) { @@ -1161,43 +1163,43 @@ void _adafruit_save_bond_cccd_dfr(uint32_t conn_handle) Bluefruit._saveBondCCCD(); } -void AdafruitBluefruit::_saveBondKeys(void) -{ - char filename[BOND_FILENAME_LEN]; - sprintf(filename, BOND_FILENAME, _bond_data.own_enc.master_id.ediv); - - char devname[CFG_MAX_DEVNAME_LEN] = { 0 }; - Gap.getPeerName(_conn_hdl, devname, CFG_MAX_DEVNAME_LEN); - - NffsFile file(filename, FS_ACCESS_WRITE); - - VERIFY( file.exists(), ); - - bool result = true; - - // write keys - if ( !file.write((uint8_t*)&_bond_data, sizeof(_bond_data)) ) - { - result = false; - } - - // write device name - if ( strlen(devname) && !file.write((uint8_t*) devname, CFG_MAX_DEVNAME_LEN) ) - { - result = false; - } - - file.close(); - - if (result) - { - LOG_LV2("BOND", "Keys for \"%s\" is saved to file %s", devname, filename); - }else - { - LOG_LV1("BOND", "Failed to save keys for \"%s\"", devname); - } - printBondDir(); -} +//void AdafruitBluefruit::_saveBondKeys(void) +//{ +// char filename[BOND_FILENAME_LEN]; +// sprintf(filename, BOND_FILENAME, _bond_data.own_enc.master_id.ediv); +// +// char devname[CFG_MAX_DEVNAME_LEN] = { 0 }; +// Gap.getPeerName(_conn_hdl, devname, CFG_MAX_DEVNAME_LEN); +// +// NffsFile file(filename, FS_ACCESS_WRITE); +// +// VERIFY( file.exists(), ); +// +// bool result = true; +// +// // write keys +// if ( !file.write((uint8_t*)&_bond_data, sizeof(_bond_data)) ) +// { +// result = false; +// } +// +// // write device name +// if ( strlen(devname) && !file.write((uint8_t*) devname, CFG_MAX_DEVNAME_LEN) ) +// { +// result = false; +// } +// +// file.close(); +// +// if (result) +// { +// LOG_LV2("BOND", "Keys for \"%s\" is saved to file %s", devname, filename); +// }else +// { +// LOG_LV1("BOND", "Failed to save keys for \"%s\"", devname); +// } +// printBondDir(); +//} bool AdafruitBluefruit::_loadBondKeys(uint16_t ediv) { diff --git a/libraries/Bluefruit52Lib/src/bluefruit.h b/libraries/Bluefruit52Lib/src/bluefruit.h index 02159a8d..d1281e3f 100644 --- a/libraries/Bluefruit52Lib/src/bluefruit.h +++ b/libraries/Bluefruit52Lib/src/bluefruit.h @@ -40,7 +40,6 @@ #include "bluefruit_common.h" #define CFG_ADV_BLINKY_INTERVAL 500 -#define CFG_MAX_DEVNAME_LEN 32 /* Note changing these parameters will affect APP_RAM_BASE * --> need to update RAM region in linker file @@ -91,6 +90,9 @@ #include "utility/AdaCallback.h" + +#include "utility/bonding.h" + enum { BANDWIDTH_AUTO = 0, @@ -234,12 +236,7 @@ class AdafruitBluefruit ble_gap_sec_params_t _sec_param; // Shared keys with bonded device, size = 80 bytes - struct - { - ble_gap_enc_key_t own_enc; - ble_gap_enc_key_t peer_enc; - ble_gap_id_key_t peer_id; - } _bond_data; + bond_data_t _bond_data; enum { diff --git a/libraries/Bluefruit52Lib/src/bluefruit_common.h b/libraries/Bluefruit52Lib/src/bluefruit_common.h index c6250fb7..f6f98c93 100644 --- a/libraries/Bluefruit52Lib/src/bluefruit_common.h +++ b/libraries/Bluefruit52Lib/src/bluefruit_common.h @@ -43,6 +43,8 @@ #include "utility/AdaMsg.h" +#define CFG_MAX_DEVNAME_LEN 32 + #define BLE_GENERIC_TIMEOUT 100 #define BLE_GAP_CONN_SUPERVISION_TIMEOUT_MS 2000 diff --git a/libraries/Bluefruit52Lib/src/utility/bonding.cpp b/libraries/Bluefruit52Lib/src/utility/bonding.cpp new file mode 100644 index 00000000..7c53d80e --- /dev/null +++ b/libraries/Bluefruit52Lib/src/utility/bonding.cpp @@ -0,0 +1,120 @@ +/**************************************************************************/ +/*! + @file bonding.cpp + @author hathach (tinyusb.org) + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2018, Adafruit Industries (adafruit.com) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the copyright holders nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY + EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +/**************************************************************************/ + +#include +#include +#include "bonding.h" +#include "bluefruit.h" + +#if CFG_DEBUG >= 2 +//#define printBondDir() dbgPrintDir(CFG_BOND_NFFS_DIR) +#define printBondDir() +#else +#define printBondDir() +#endif + + + +/*------------------------------------------------------------------*/ +/* Saving Bond Data to Nffs in following layout + * - _bond_data 80 bytes + * - Name 32 bytes + * - CCCD variable + *------------------------------------------------------------------*/ +static void bond_save_keys_dfr(uint16_t conn_hdl, bond_data_t* bdata) +{ + char filename[BOND_FILENAME_LEN]; + sprintf(filename, BOND_FILENAME, bdata->own_enc.master_id.ediv); + + char devname[CFG_MAX_DEVNAME_LEN] = { 0 }; + Bluefruit.Gap.getPeerName(conn_hdl, devname, CFG_MAX_DEVNAME_LEN); + + NffsFile file(filename, FS_ACCESS_WRITE); + + VERIFY( file.exists(), ); + + bool result = true; + + // write keys + if ( !file.write((uint8_t*)bdata, sizeof(bond_data_t)) ) + { + result = false; + } + + // write device name + if ( strlen(devname) && !file.write((uint8_t*) devname, CFG_MAX_DEVNAME_LEN) ) + { + result = false; + } + + file.close(); + + if (result) + { + LOG_LV2("BOND", "Keys for \"%s\" is saved to file %s", devname, filename); + }else + { + LOG_LV1("BOND", "Failed to save keys for \"%s\"", devname); + } + + printBondDir(); +} + +void bond_save_keys(uint16_t conn_hdl, bond_data_t* bdata) +{ + uint8_t* buf = (uint8_t*) rtos_malloc( sizeof(bond_data_t) ); + VERIFY(buf, ); + + memcpy(buf, bdata, sizeof(bond_data_t)); + + // queue to execute in Ada Callback thread + ada_callback(buf, bond_save_keys_dfr, conn_hdl, buf); +} + +void bond_save_cccd(uint16_t cond_hdl, uint16_t ediv) +{ + +} + +bool bond_load_keys(uint16_t ediv, bond_data_t* bdata) +{ + +} + +bool bond_load_cccd(uint16_t cond_hdl, uint16_t ediv) +{ + +} diff --git a/libraries/Bluefruit52Lib/src/utility/bonding.h b/libraries/Bluefruit52Lib/src/utility/bonding.h new file mode 100644 index 00000000..aad06ec3 --- /dev/null +++ b/libraries/Bluefruit52Lib/src/utility/bonding.h @@ -0,0 +1,67 @@ +/**************************************************************************/ +/*! + @file bonding.h + @author hathach (tinyusb.org) + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2018, Adafruit Industries (adafruit.com) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the copyright holders nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY + EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +/**************************************************************************/ +#ifndef BONDING_H_ +#define BONDING_H_ + +#include "bluefruit_common.h" + +#define CFG_BOND_NFFS_DIR "/adafruit/bond" +#define BOND_FILENAME CFG_BOND_NFFS_DIR "/%04x" +#define BOND_FILENAME_LEN (sizeof(CFG_BOND_NFFS_DIR) + 10) + +// Shared keys with bonded device, size = 80 bytes +typedef struct +{ + ble_gap_enc_key_t own_enc; + ble_gap_enc_key_t peer_enc; + ble_gap_id_key_t peer_id; +} bond_data_t; + +enum +{ + BOND_FILE_DEVNAME_OFFSET = sizeof(bond_data_t), + BOND_FILE_CCCD_OFFSET = BOND_FILE_DEVNAME_OFFSET + CFG_MAX_DEVNAME_LEN +}; + + +void bond_save_keys(uint16_t conn_hdl, bond_data_t* bdata); +void bond_save_cccd(uint16_t cond_hdl, uint16_t ediv); + +bool bond_load_keys(uint16_t ediv, bond_data_t* bdata); +bool bond_load_cccd(uint16_t cond_hdl, uint16_t ediv); + + +#endif /* BONDING_H_ */