Skip to content

Commit 4fab39f

Browse files
committed
improving bonding management
1 parent a092479 commit 4fab39f

File tree

7 files changed

+242
-53
lines changed

7 files changed

+242
-53
lines changed

libraries/Bluefruit52Lib/src/BLEGap.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,10 @@ void BLEGap::_eventHandler(ble_evt_t* evt)
240240
}
241241
break;
242242

243+
case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
244+
// Pairing in progress, Peer asking for our info
245+
break;
246+
243247
#if SD_VER < 500
244248
case BLE_EVT_TX_COMPLETE:
245249
if ( peer->hvn_tx_sem )

libraries/Bluefruit52Lib/src/BLEGap.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,6 @@ class BLEGap
110110

111111
SemaphoreHandle_t indicate_confirm_sem;
112112
bool hvc_received;
113-
114-
// not really peer data
115-
// ble_gap_sec_params_t sec_param;
116113
} gap_peer_t;
117114

118115
gap_peer_t _peers[BLE_MAX_CONN];

libraries/Bluefruit52Lib/src/bluefruit.cpp

Lines changed: 45 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
#include "bluefruit.h"
3838
#include <Nffs.h>
39+
#include "utility/bonding.h"
3940

4041
#define SVC_CONTEXT_FLAG (BLE_GATTS_SYS_ATTR_FLAG_SYS_SRVCS | BLE_GATTS_SYS_ATTR_FLAG_USR_SRVCS)
4142

@@ -1057,7 +1058,8 @@ void AdafruitBluefruit::_ble_handler(ble_evt_t* evt)
10571058
if (BLE_GAP_SEC_STATUS_SUCCESS == status->auth_status)
10581059
{
10591060
_bonded = true;
1060-
ada_callback(NULL, _adafruit_save_bond_key_dfr, _conn_hdl);
1061+
// ada_callback(NULL, _adafruit_save_bond_key_dfr, _conn_hdl);
1062+
bond_save_keys(evt_conn_hdl, &_bond_data);
10611063
}else
10621064
{
10631065
PRINT_HEX(status->auth_status);
@@ -1149,55 +1151,55 @@ void AdafruitBluefruit::clearBonds(void)
11491151
* - Name 32 bytes
11501152
* - CCCD variable
11511153
*------------------------------------------------------------------*/
1152-
void _adafruit_save_bond_key_dfr(uint32_t conn_handle)
1153-
{
1154-
(void) conn_handle;
1155-
Bluefruit._saveBondKeys();
1156-
}
1154+
//void _adafruit_save_bond_key_dfr(uint32_t conn_handle)
1155+
//{
1156+
// (void) conn_handle;
1157+
// Bluefruit._saveBondKeys();
1158+
//}
11571159

11581160
void _adafruit_save_bond_cccd_dfr(uint32_t conn_handle)
11591161
{
11601162
(void) conn_handle;
11611163
Bluefruit._saveBondCCCD();
11621164
}
11631165

1164-
void AdafruitBluefruit::_saveBondKeys(void)
1165-
{
1166-
char filename[BOND_FILENAME_LEN];
1167-
sprintf(filename, BOND_FILENAME, _bond_data.own_enc.master_id.ediv);
1168-
1169-
char devname[CFG_MAX_DEVNAME_LEN] = { 0 };
1170-
Gap.getPeerName(_conn_hdl, devname, CFG_MAX_DEVNAME_LEN);
1171-
1172-
NffsFile file(filename, FS_ACCESS_WRITE);
1173-
1174-
VERIFY( file.exists(), );
1175-
1176-
bool result = true;
1177-
1178-
// write keys
1179-
if ( !file.write((uint8_t*)&_bond_data, sizeof(_bond_data)) )
1180-
{
1181-
result = false;
1182-
}
1183-
1184-
// write device name
1185-
if ( strlen(devname) && !file.write((uint8_t*) devname, CFG_MAX_DEVNAME_LEN) )
1186-
{
1187-
result = false;
1188-
}
1189-
1190-
file.close();
1191-
1192-
if (result)
1193-
{
1194-
LOG_LV2("BOND", "Keys for \"%s\" is saved to file %s", devname, filename);
1195-
}else
1196-
{
1197-
LOG_LV1("BOND", "Failed to save keys for \"%s\"", devname);
1198-
}
1199-
printBondDir();
1200-
}
1166+
//void AdafruitBluefruit::_saveBondKeys(void)
1167+
//{
1168+
// char filename[BOND_FILENAME_LEN];
1169+
// sprintf(filename, BOND_FILENAME, _bond_data.own_enc.master_id.ediv);
1170+
//
1171+
// char devname[CFG_MAX_DEVNAME_LEN] = { 0 };
1172+
// Gap.getPeerName(_conn_hdl, devname, CFG_MAX_DEVNAME_LEN);
1173+
//
1174+
// NffsFile file(filename, FS_ACCESS_WRITE);
1175+
//
1176+
// VERIFY( file.exists(), );
1177+
//
1178+
// bool result = true;
1179+
//
1180+
// // write keys
1181+
// if ( !file.write((uint8_t*)&_bond_data, sizeof(_bond_data)) )
1182+
// {
1183+
// result = false;
1184+
// }
1185+
//
1186+
// // write device name
1187+
// if ( strlen(devname) && !file.write((uint8_t*) devname, CFG_MAX_DEVNAME_LEN) )
1188+
// {
1189+
// result = false;
1190+
// }
1191+
//
1192+
// file.close();
1193+
//
1194+
// if (result)
1195+
// {
1196+
// LOG_LV2("BOND", "Keys for \"%s\" is saved to file %s", devname, filename);
1197+
// }else
1198+
// {
1199+
// LOG_LV1("BOND", "Failed to save keys for \"%s\"", devname);
1200+
// }
1201+
// printBondDir();
1202+
//}
12011203

12021204
bool AdafruitBluefruit::_loadBondKeys(uint16_t ediv)
12031205
{

libraries/Bluefruit52Lib/src/bluefruit.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
#include "bluefruit_common.h"
4141

4242
#define CFG_ADV_BLINKY_INTERVAL 500
43-
#define CFG_MAX_DEVNAME_LEN 32
4443

4544
/* Note changing these parameters will affect APP_RAM_BASE
4645
* --> need to update RAM region in linker file
@@ -91,6 +90,9 @@
9190

9291
#include "utility/AdaCallback.h"
9392

93+
94+
#include "utility/bonding.h"
95+
9496
enum
9597
{
9698
BANDWIDTH_AUTO = 0,
@@ -234,12 +236,7 @@ class AdafruitBluefruit
234236
ble_gap_sec_params_t _sec_param;
235237

236238
// Shared keys with bonded device, size = 80 bytes
237-
struct
238-
{
239-
ble_gap_enc_key_t own_enc;
240-
ble_gap_enc_key_t peer_enc;
241-
ble_gap_id_key_t peer_id;
242-
} _bond_data;
239+
bond_data_t _bond_data;
243240

244241
enum
245242
{

libraries/Bluefruit52Lib/src/bluefruit_common.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343

4444
#include "utility/AdaMsg.h"
4545

46+
#define CFG_MAX_DEVNAME_LEN 32
47+
4648
#define BLE_GENERIC_TIMEOUT 100
4749

4850
#define BLE_GAP_CONN_SUPERVISION_TIMEOUT_MS 2000
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/**************************************************************************/
2+
/*!
3+
@file bonding.cpp
4+
@author hathach (tinyusb.org)
5+
6+
@section LICENSE
7+
8+
Software License Agreement (BSD License)
9+
10+
Copyright (c) 2018, Adafruit Industries (adafruit.com)
11+
All rights reserved.
12+
13+
Redistribution and use in source and binary forms, with or without
14+
modification, are permitted provided that the following conditions are met:
15+
1. Redistributions of source code must retain the above copyright
16+
notice, this list of conditions and the following disclaimer.
17+
2. Redistributions in binary form must reproduce the above copyright
18+
notice, this list of conditions and the following disclaimer in the
19+
documentation and/or other materials provided with the distribution.
20+
3. Neither the name of the copyright holders nor the
21+
names of its contributors may be used to endorse or promote products
22+
derived from this software without specific prior written permission.
23+
24+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
25+
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
28+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34+
*/
35+
/**************************************************************************/
36+
37+
#include <Arduino.h>
38+
#include <Nffs.h>
39+
#include "bonding.h"
40+
#include "bluefruit.h"
41+
42+
#if CFG_DEBUG >= 2
43+
//#define printBondDir() dbgPrintDir(CFG_BOND_NFFS_DIR)
44+
#define printBondDir()
45+
#else
46+
#define printBondDir()
47+
#endif
48+
49+
50+
51+
/*------------------------------------------------------------------*/
52+
/* Saving Bond Data to Nffs in following layout
53+
* - _bond_data 80 bytes
54+
* - Name 32 bytes
55+
* - CCCD variable
56+
*------------------------------------------------------------------*/
57+
static void bond_save_keys_dfr(uint16_t conn_hdl, bond_data_t* bdata)
58+
{
59+
char filename[BOND_FILENAME_LEN];
60+
sprintf(filename, BOND_FILENAME, bdata->own_enc.master_id.ediv);
61+
62+
char devname[CFG_MAX_DEVNAME_LEN] = { 0 };
63+
Bluefruit.Gap.getPeerName(conn_hdl, devname, CFG_MAX_DEVNAME_LEN);
64+
65+
NffsFile file(filename, FS_ACCESS_WRITE);
66+
67+
VERIFY( file.exists(), );
68+
69+
bool result = true;
70+
71+
// write keys
72+
if ( !file.write((uint8_t*)bdata, sizeof(bond_data_t)) )
73+
{
74+
result = false;
75+
}
76+
77+
// write device name
78+
if ( strlen(devname) && !file.write((uint8_t*) devname, CFG_MAX_DEVNAME_LEN) )
79+
{
80+
result = false;
81+
}
82+
83+
file.close();
84+
85+
if (result)
86+
{
87+
LOG_LV2("BOND", "Keys for \"%s\" is saved to file %s", devname, filename);
88+
}else
89+
{
90+
LOG_LV1("BOND", "Failed to save keys for \"%s\"", devname);
91+
}
92+
93+
printBondDir();
94+
}
95+
96+
void bond_save_keys(uint16_t conn_hdl, bond_data_t* bdata)
97+
{
98+
uint8_t* buf = (uint8_t*) rtos_malloc( sizeof(bond_data_t) );
99+
VERIFY(buf, );
100+
101+
memcpy(buf, bdata, sizeof(bond_data_t));
102+
103+
// queue to execute in Ada Callback thread
104+
ada_callback(buf, bond_save_keys_dfr, conn_hdl, buf);
105+
}
106+
107+
void bond_save_cccd(uint16_t cond_hdl, uint16_t ediv)
108+
{
109+
110+
}
111+
112+
bool bond_load_keys(uint16_t ediv, bond_data_t* bdata)
113+
{
114+
115+
}
116+
117+
bool bond_load_cccd(uint16_t cond_hdl, uint16_t ediv)
118+
{
119+
120+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**************************************************************************/
2+
/*!
3+
@file bonding.h
4+
@author hathach (tinyusb.org)
5+
6+
@section LICENSE
7+
8+
Software License Agreement (BSD License)
9+
10+
Copyright (c) 2018, Adafruit Industries (adafruit.com)
11+
All rights reserved.
12+
13+
Redistribution and use in source and binary forms, with or without
14+
modification, are permitted provided that the following conditions are met:
15+
1. Redistributions of source code must retain the above copyright
16+
notice, this list of conditions and the following disclaimer.
17+
2. Redistributions in binary form must reproduce the above copyright
18+
notice, this list of conditions and the following disclaimer in the
19+
documentation and/or other materials provided with the distribution.
20+
3. Neither the name of the copyright holders nor the
21+
names of its contributors may be used to endorse or promote products
22+
derived from this software without specific prior written permission.
23+
24+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
25+
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
28+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34+
*/
35+
/**************************************************************************/
36+
#ifndef BONDING_H_
37+
#define BONDING_H_
38+
39+
#include "bluefruit_common.h"
40+
41+
#define CFG_BOND_NFFS_DIR "/adafruit/bond"
42+
#define BOND_FILENAME CFG_BOND_NFFS_DIR "/%04x"
43+
#define BOND_FILENAME_LEN (sizeof(CFG_BOND_NFFS_DIR) + 10)
44+
45+
// Shared keys with bonded device, size = 80 bytes
46+
typedef struct
47+
{
48+
ble_gap_enc_key_t own_enc;
49+
ble_gap_enc_key_t peer_enc;
50+
ble_gap_id_key_t peer_id;
51+
} bond_data_t;
52+
53+
enum
54+
{
55+
BOND_FILE_DEVNAME_OFFSET = sizeof(bond_data_t),
56+
BOND_FILE_CCCD_OFFSET = BOND_FILE_DEVNAME_OFFSET + CFG_MAX_DEVNAME_LEN
57+
};
58+
59+
60+
void bond_save_keys(uint16_t conn_hdl, bond_data_t* bdata);
61+
void bond_save_cccd(uint16_t cond_hdl, uint16_t ediv);
62+
63+
bool bond_load_keys(uint16_t ediv, bond_data_t* bdata);
64+
bool bond_load_cccd(uint16_t cond_hdl, uint16_t ediv);
65+
66+
67+
#endif /* BONDING_H_ */

0 commit comments

Comments
 (0)