From 03640672f8937dc3490cd690d4706b4d533a5b6c Mon Sep 17 00:00:00 2001 From: hathach Date: Mon, 6 Mar 2017 21:41:34 +0700 Subject: [PATCH] wrap up bonding --- libraries/Bluefruit52Lib/src/bluefruit.cpp | 140 ++++++++++++------ libraries/Bluefruit52Lib/src/bluefruit.h | 38 ++--- .../nffs/examples/ListFiles/ListFiles.ino | 27 ++-- 3 files changed, 127 insertions(+), 78 deletions(-) diff --git a/libraries/Bluefruit52Lib/src/bluefruit.cpp b/libraries/Bluefruit52Lib/src/bluefruit.cpp index 20a496d5..c1bdc57c 100644 --- a/libraries/Bluefruit52Lib/src/bluefruit.cpp +++ b/libraries/Bluefruit52Lib/src/bluefruit.cpp @@ -55,6 +55,8 @@ #define CFG_SOC_TASK_STACKSIZE ((configMINIMAL_STACK_SIZE*10) / 2 ) #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) extern "C" { @@ -198,15 +200,7 @@ err_t AdafruitBluefruit::begin(bool prph_enable, bool central_enable) // Initialize nffs for bonding (it is safe to call nffs_pkg_init() multiple time) Nffs.begin(); - -#if 0 - Nffs.mkdir_p(CFG_BOND_NFFS_DIR); - PRINT_INT(Nffs.errnum); - - //printTreeDir("/", 0); -#else - -#endif + (void) Nffs.mkdir_p(CFG_BOND_NFFS_DIR); return ERROR_NONE; } @@ -356,23 +350,6 @@ bool AdafruitBluefruit::setPIN(const char* pin) } ) -err_t AdafruitBluefruit::_saveBondedCCCD(void) -{ - uint16_t len=0; - sd_ble_gatts_sys_attr_get(_conn_hdl, NULL, &len, SVC_CONTEXT_FLAG); - - // Free and re-malloc if not enough - if ( _bond_data.sys_attr_len < len ) - { - if (_bond_data.sys_attr) rtos_free(_bond_data.sys_attr); - - _bond_data.sys_attr = (uint8_t*) rtos_malloc( len ); - _bond_data.sys_attr_len = len; - } - - return sd_ble_gatts_sys_attr_get(_conn_hdl, _bond_data.sys_attr, &_bond_data.sys_attr_len, SVC_CONTEXT_FLAG); -} - /*------------------------------------------------------------------*/ /* Private Methods *------------------------------------------------------------------*/ @@ -421,7 +398,6 @@ void adafruit_soc_task(void* arg) { case NRF_EVT_FLASH_OPERATION_SUCCESS: case NRF_EVT_FLASH_OPERATION_ERROR: - // PRINT_INT(soc_evt); if (hal_flash_event_cb) hal_flash_event_cb(soc_evt); break; @@ -442,11 +418,11 @@ void adafruit_ble_task(void* arg) while (1) { - Bluefruit._poll(); + Bluefruit._ble_handler(); } } -void AdafruitBluefruit::_poll(void) +void AdafruitBluefruit::_ble_handler(void) { enum { BLE_STACK_EVT_MSG_BUF_SIZE = (sizeof(ble_evt_t) + (GATT_MTU_SIZE_DEFAULT)) }; @@ -521,10 +497,7 @@ void AdafruitBluefruit::_poll(void) if (_led_conn) ledOff(LED_CONN); // Save all configured cccd - if ( _bonded ) - { - _saveBondedCCCD(); - } + if (_bonded) _saveBondedCCCD(); _conn_hdl = BLE_CONN_HANDLE_INVALID; _bonded = false; @@ -555,6 +528,9 @@ void AdafruitBluefruit::_poll(void) case BLE_GAP_EVT_SEC_PARAMS_REQUEST: { + varclr(&_bond_data); + _bond_data.own_enc.master_id.ediv = 0xFFFF; // invalid value for ediv + /* Step 1: Pairing/Bonding * - Central supplies its parameters * - We replies with our security parameters @@ -611,7 +587,8 @@ COMMENT_OUT( // 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; - if (_bond_data.own_enc.master_id.ediv == sec_request->master_id.ediv) + //if (_bond_data.own_enc.master_id.ediv == sec_request->master_id.ediv) + if ( _loadBondKeys(sec_request->master_id.ediv) ) { sd_ble_gap_sec_info_reply(evt->evt.gap_evt.conn_handle, &_bond_data.own_enc.enc_info, &_bond_data.peer_id.id_info, NULL); } else @@ -635,15 +612,17 @@ COMMENT_OUT( case 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; + // Connection is secured aka Paired - // TODO check if this connection is bonded + COMMENT_OUT( ble_gap_conn_sec_t* conn_sec = (ble_gap_conn_sec_t*) &evt->evt.gap_evt.params.conn_sec_update.conn_sec; ) + + // Previously bonded --> secure by re-connection process + // --> Load & Set Sys Attr (Apply Service Context) + // Else Init Sys Attr + _loadBondedCCCD(_bond_data.own_enc.master_id.ediv); + + // Consider Paired as Bonded _bonded = true; - - // Connection is secured, Apply Service Context - sd_ble_gatts_sys_attr_set(_conn_hdl, _bond_data.sys_attr, _bond_data.sys_attr_len, SVC_CONTEXT_FLAG); } break; @@ -654,6 +633,7 @@ COMMENT_OUT( // Bonding succeeded --> save encryption keys if (BLE_GAP_SEC_STATUS_SUCCESS == status->auth_status) { + _saveBondKeys(); _bonded = true; }else { @@ -663,7 +643,6 @@ COMMENT_OUT( break; case BLE_GATTS_EVT_SYS_ATTR_MISSING: - // TODO Look up bonded information and apply application context sd_ble_gatts_sys_attr_set(_conn_hdl, NULL, 0, 0); break; @@ -682,3 +661,80 @@ COMMENT_OUT( } } } + +bool AdafruitBluefruit::_saveBondKeys(void) +{ + char filename[BOND_FILENAME_LEN]; + sprintf(filename, BOND_FILENAME, _bond_data.own_enc.master_id.ediv); + + return Nffs.writeFile(filename, &_bond_data, sizeof(_bond_data)); +} + +bool AdafruitBluefruit::_loadBondKeys(uint16_t ediv) +{ + char filename[BOND_FILENAME_LEN]; + sprintf(filename, BOND_FILENAME, ediv); + + return Nffs.readFile(filename, &_bond_data, sizeof(_bond_data)) > 0; +} + + + + +void AdafruitBluefruit::_loadBondedCCCD(uint16_t ediv) +{ + bool loaded = false; + + char filename[BOND_FILENAME_LEN]; + sprintf(filename, BOND_FILENAME "_cccd", ediv); + + NffsFile file(filename, FS_ACCESS_READ); + + if ( file.exists() ) + { + uint16_t len = file.size(); + uint8_t* sys_attr = (uint8_t*) rtos_malloc( len ); + + if (sys_attr) + { + if ( file.read(sys_attr, len ) ) + { + if (ERROR_NONE == sd_ble_gatts_sys_attr_set(_conn_hdl, sys_attr, len, SVC_CONTEXT_FLAG) ) + { + loaded = true; + } + } + + rtos_free(sys_attr); + } + } + + file.close(); + + if ( !loaded ) + { + sd_ble_gatts_sys_attr_set(_conn_hdl, NULL, 0, 0); + } +} + +void AdafruitBluefruit::_saveBondedCCCD(void) +{ + if ( _bond_data.own_enc.master_id.ediv == 0xFFFF ) return; + + uint16_t len=0; + sd_ble_gatts_sys_attr_get(_conn_hdl, NULL, &len, SVC_CONTEXT_FLAG); + + uint8_t* sys_attr = (uint8_t*) rtos_malloc( len ); + VERIFY( sys_attr, ); + + if ( ERROR_NONE == sd_ble_gatts_sys_attr_get(_conn_hdl, sys_attr, &len, SVC_CONTEXT_FLAG) ) + { + // save to file + char filename[BOND_FILENAME_LEN]; + sprintf(filename, BOND_FILENAME "_cccd", _bond_data.own_enc.master_id.ediv); + + Nffs.writeFile(filename, sys_attr, len); + } + + rtos_free(sys_attr); +} diff --git a/libraries/Bluefruit52Lib/src/bluefruit.h b/libraries/Bluefruit52Lib/src/bluefruit.h index e8eb67cd..13aa298b 100644 --- a/libraries/Bluefruit52Lib/src/bluefruit.h +++ b/libraries/Bluefruit52Lib/src/bluefruit.h @@ -150,29 +150,25 @@ class AdafruitBluefruit uint16_t _conn_hdl; bool _bonded; + // TODO move to bonding place + +public: // temporary + struct + { + // Keys + ble_gap_enc_key_t own_enc; + ble_gap_enc_key_t peer_enc; + ble_gap_id_key_t peer_id; + } _bond_data; + +private: + ble_gap_addr_t _peer_addr; COMMENT_OUT( uint8_t _auth_type; char _pin[BLE_GAP_PASSKEY_LEN]; ) - // TODO move to bonding place - -public: // temporary - struct { - // Keys - ble_gap_enc_key_t own_enc; - ble_gap_enc_key_t peer_enc; - ble_gap_id_key_t peer_id; - - // System Attr (aka CCCDs) - uint16_t sys_attr_len; - uint8_t* sys_attr; - }_bond_data; - -private: - ble_gap_addr_t _peer_addr; - // Transmission Buffer Count for HVX notification, max is seen at 7 SemaphoreHandle_t _txbuf_sem; @@ -182,9 +178,13 @@ private: bool _addToAdv(bool scan_resp, uint8_t type, const void* data, uint8_t len); - err_t _saveBondedCCCD(void); + bool _saveBondKeys(void); + bool _loadBondKeys(uint16_t ediv); - void _poll(void); + void _saveBondedCCCD(void); + void _loadBondedCCCD(uint16_t ediv); + + void _ble_handler(void); friend void SD_EVT_IRQHandler(void); friend void adafruit_ble_task(void* arg); diff --git a/libraries/nffs/examples/ListFiles/ListFiles.ino b/libraries/nffs/examples/ListFiles/ListFiles.ino index f95b29e6..846db5ea 100644 --- a/libraries/nffs/examples/ListFiles/ListFiles.ino +++ b/libraries/nffs/examples/ListFiles/ListFiles.ino @@ -25,9 +25,6 @@ */ #define MAX_LEVEL 2 -// Current Working Directory Name -char cwd[256] = { 0 }; - // the setup function runs once when you press reset or power the board void setup() { @@ -64,10 +61,10 @@ void loop() @note Recursive call */ /**************************************************************************/ -void printTreeDir(const char* path, uint8_t level) +void printTreeDir(const char* cwd, uint8_t level) { // Open the input folder - NffsDir dir(path); + NffsDir dir(cwd); // Print root if (level == 0) Serial.println("root"); @@ -85,6 +82,12 @@ void printTreeDir(const char* path, uint8_t level) char eName[64]; dirEntry.getName(eName, sizeof(eName)); + + char fullpath[256]; + strcpy(fullpath, cwd); + strcat(fullpath, "/"); + strcat(fullpath, eName); + Serial.print( eName ); if ( dirEntry.isDirectory() ) @@ -95,9 +98,7 @@ void printTreeDir(const char* path, uint8_t level) // High number of MAX_LEVEL can cause memory overflow if ( level < MAX_LEVEL ) { - strcat(cwd, "/"); - strcat(cwd, eName); - printTreeDir( cwd, level+1 ); + printTreeDir( fullpath, level+1 ); } }else { @@ -110,8 +111,7 @@ void printTreeDir(const char* path, uint8_t level) // Print at least one extra space in case current position > 50 Serial.print(' '); - // Print size in bytes - NffsFile file(eName); + NffsFile file(fullpath); Serial.print( file.size() ); Serial.println( " Bytes"); @@ -121,11 +121,4 @@ void printTreeDir(const char* path, uint8_t level) } dir.close(); - - // Change back to Parent if not root - if (level != 0) - { - char* slash = strrchr(cwd, '/'); - *slash = 0; - } }