add Bluefruit.setPIN() for static passkey pairing
This commit is contained in:
8 files changed
+80
-49
No files matched your search
@@ -50,7 +50,8 @@ void BLECharacteristic::_init(void)
|
||||
varclr(&_properties);
|
||||
|
||||
varclr(&_attr_meta);
|
||||
_attr_meta.read_perm = _attr_meta.write_perm = BLE_SECMODE_OPEN;
|
||||
_attr_meta.read_perm = BLE_SECMODE_OPEN;
|
||||
_attr_meta.write_perm = BLE_SECMODE_OPEN;
|
||||
_attr_meta.vlen = 1;
|
||||
_attr_meta.vloc = BLE_GATTS_VLOC_STACK;
|
||||
_userbuf = NULL;
|
||||
@@ -212,15 +213,19 @@ ble_gatts_char_handles_t BLECharacteristic::handles(void)
|
||||
return _handles;
|
||||
}
|
||||
|
||||
// return the higher security mode
|
||||
static inline ble_gap_conn_sec_mode_t max_secmode(ble_gap_conn_sec_mode_t sm1, ble_gap_conn_sec_mode_t sm2)
|
||||
{
|
||||
if ( (sm1.sm > sm2.sm) || (sm1.sm == sm2.sm && sm1.lv > sm2.lv) ) return sm1;
|
||||
return sm2;
|
||||
}
|
||||
|
||||
err_t BLECharacteristic::begin(void)
|
||||
{
|
||||
_service = BLEService::lastService;
|
||||
|
||||
// Add UUID128 if needed
|
||||
(void) uuid.begin();
|
||||
|
||||
// Permission is OPEN if passkey is disabled.
|
||||
// if (!nvm_data.core.passkey_enable) BLE_GAP_CONN_SEC_MODE_SET_OPEN(&p_char_def->permission);
|
||||
uuid.begin();
|
||||
|
||||
// Correct Read/Write permission according to properties
|
||||
if ( !(_properties.read || _properties.notify || _properties.indicate ) )
|
||||
@@ -233,6 +238,22 @@ err_t BLECharacteristic::begin(void)
|
||||
_attr_meta.write_perm = BLE_SECMODE_NO_ACCESS;
|
||||
}
|
||||
|
||||
// Correct Read/Write permission according to parent service
|
||||
// Use service permission if it has higher secure mode
|
||||
BleSecurityMode svc_secmode = _service->getPermission();
|
||||
ble_gap_conn_sec_mode_t svc_perm;
|
||||
memcpy(&svc_perm, &svc_secmode, 1);
|
||||
|
||||
if ( _attr_meta.read_perm.sm != 0 ) // skip no access
|
||||
{
|
||||
_attr_meta.read_perm = max_secmode(_attr_meta.read_perm, svc_perm);
|
||||
}
|
||||
|
||||
if ( _attr_meta.write_perm.sm != 0 ) // skip no access
|
||||
{
|
||||
_attr_meta.write_perm = max_secmode(_attr_meta.write_perm, svc_perm);
|
||||
}
|
||||
|
||||
/* CCCD attribute metadata */
|
||||
ble_gatts_attr_md_t cccd_md;
|
||||
|
||||
|
||||
@@ -43,23 +43,6 @@
|
||||
class AdafruitBluefruit;
|
||||
class BLEService;
|
||||
|
||||
enum BleSecurityMode
|
||||
{
|
||||
SECMODE_NO_ACCESS = 0x00,
|
||||
SECMODE_OPEN = 0x11,
|
||||
SECMODE_ENC_NO_MITM = 0x21,
|
||||
SECMODE_ENC_WITH_MITM = 0x31,
|
||||
SECMODE_SIGNED_NO_MITM = 0x12,
|
||||
SECMODE_SIGNED_WITH_MITM = 0x22
|
||||
};
|
||||
|
||||
#define BLE_SECMODE_NO_ACCESS ((ble_gap_conn_sec_mode_t) { .sm = 0, .lv = 0 })
|
||||
#define BLE_SECMODE_OPEN ((ble_gap_conn_sec_mode_t) { .sm = 1, .lv = 1 })
|
||||
#define BLE_SECMODE_ENC_NO_MITM ((ble_gap_conn_sec_mode_t) { .sm = 1, .lv = 2 })
|
||||
#define BLE_SECMODE_ENC_WITH_MITM ((ble_gap_conn_sec_mode_t) { .sm = 1, .lv = 3 })
|
||||
#define BLE_SECMODE_SIGNED_NO_MITM ((ble_gap_conn_sec_mode_t) { .sm = 2, .lv = 1 })
|
||||
#define BLE_SECMODE_SIGNED_WITH_MITM ((ble_gap_conn_sec_mode_t) { .sm = 2, .lv = 2 })
|
||||
|
||||
enum CharsProperties
|
||||
{
|
||||
CHR_PROPS_BROADCAST = bit(0),
|
||||
|
||||
@@ -41,6 +41,7 @@ BLEService* BLEService::lastService = NULL;
|
||||
void BLEService::_init(void)
|
||||
{
|
||||
_handle = BLE_GATT_HANDLE_INVALID;
|
||||
_permission = SECMODE_OPEN;
|
||||
}
|
||||
|
||||
BLEService::BLEService(void)
|
||||
@@ -60,6 +61,16 @@ void BLEService::setUuid(BLEUuid bleuuid)
|
||||
uuid = bleuuid;
|
||||
}
|
||||
|
||||
void BLEService::setPermission(BleSecurityMode permission)
|
||||
{
|
||||
_permission = permission;
|
||||
}
|
||||
|
||||
BleSecurityMode BLEService::getPermission(void)
|
||||
{
|
||||
return _permission;
|
||||
}
|
||||
|
||||
err_t BLEService::begin(void)
|
||||
{
|
||||
// Add UUID128 if needed
|
||||
|
||||
@@ -43,6 +43,7 @@ class BLEService
|
||||
{
|
||||
protected:
|
||||
uint16_t _handle; // service gatt handle
|
||||
BleSecurityMode _permission;
|
||||
|
||||
void _init(void);
|
||||
|
||||
@@ -59,6 +60,9 @@ class BLEService
|
||||
|
||||
void setUuid(BLEUuid bleuuid);
|
||||
|
||||
void setPermission(BleSecurityMode permission);
|
||||
BleSecurityMode getPermission(void);
|
||||
|
||||
virtual err_t begin(void);
|
||||
|
||||
friend class BLEGatt;
|
||||
|
||||
@@ -189,11 +189,6 @@ AdafruitBluefruit::AdafruitBluefruit(void)
|
||||
.kdist_own = { .enc = 1, .id = 1},
|
||||
.kdist_peer = { .enc = 1, .id = 1},
|
||||
});
|
||||
|
||||
COMMENT_OUT(
|
||||
_auth_type = BLE_GAP_AUTH_KEY_TYPE_NONE;
|
||||
varclr(_pin);
|
||||
)
|
||||
}
|
||||
|
||||
void AdafruitBluefruit::configServiceChanged(bool changed)
|
||||
@@ -658,23 +653,26 @@ void AdafruitBluefruit::setRssiCallback(rssi_callback_t fp)
|
||||
}
|
||||
|
||||
|
||||
COMMENT_OUT (
|
||||
// Use Legacy SC static Passkey
|
||||
bool AdafruitBluefruit::setPIN(const char* pin)
|
||||
{
|
||||
VERIFY ( strlen(pin) == BLE_GAP_PASSKEY_LEN );
|
||||
|
||||
_auth_type = BLE_GAP_AUTH_KEY_TYPE_PASSKEY;
|
||||
memcpy(_pin, pin, BLE_GAP_PASSKEY_LEN);
|
||||
// Static Passkey requires using
|
||||
// - Legacy SC
|
||||
// - IO cap: Display
|
||||
// - MITM is on
|
||||
_sec_param.lesc = 0;
|
||||
_sec_param.mitm = 1;
|
||||
_sec_param.bond = 1;
|
||||
_sec_param.io_caps = BLE_GAP_IO_CAPS_DISPLAY_ONLY;
|
||||
|
||||
// Config Static Passkey
|
||||
// ble_opt_t opt
|
||||
// uint8_t passkey[] = STATIC_PASSKEY;
|
||||
// m_static_pin_option.gap.passkey.p_passkey = passkey;
|
||||
//err_code = sd_ble_opt_set(BLE_GAP_OPT_PASSKEY, &m_static_pin_option);
|
||||
ble_opt_t opt;
|
||||
opt.gap_opt.passkey.p_passkey = (const uint8_t*) pin;
|
||||
VERIFY_STATUS( sd_ble_opt_set(BLE_GAP_OPT_PASSKEY, &opt), false);
|
||||
|
||||
return true;
|
||||
}
|
||||
)
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* Thread & SoftDevice Event handler
|
||||
|
||||
@@ -146,11 +146,6 @@ class AdafruitBluefruit
|
||||
bool setAppearance (uint16_t appear);
|
||||
uint16_t getAppearance (void);
|
||||
|
||||
ble_gap_sec_params_t getSecureParam(void)
|
||||
{
|
||||
return _sec_param;
|
||||
}
|
||||
|
||||
void autoConnLed (bool enabled);
|
||||
void setConnLedInterval (uint32_t ms);
|
||||
|
||||
@@ -173,14 +168,18 @@ class AdafruitBluefruit
|
||||
|
||||
BLEConnection* Connection(uint16_t conn_hdl);
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Security
|
||||
//--------------------------------------------------------------------+
|
||||
ble_gap_sec_params_t getSecureParam(void) { return _sec_param; }
|
||||
bool setPIN(const char* pin); // Static Passkey
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* Callbacks
|
||||
*------------------------------------------------------------------*/
|
||||
void setRssiCallback(rssi_callback_t fp);
|
||||
void setEventCallback( void (*fp) (ble_evt_t*) );
|
||||
|
||||
COMMENT_OUT ( bool setPIN(const char* pin); )
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* INTERNAL USAGE ONLY
|
||||
* Although declare as public, it is meant to be invoked by internal
|
||||
@@ -228,11 +227,6 @@ class AdafruitBluefruit
|
||||
rssi_callback_t _rssi_cb;
|
||||
void (*_event_cb) (ble_evt_t*);
|
||||
|
||||
COMMENT_OUT(
|
||||
uint8_t _auth_type;
|
||||
char _pin[BLE_GAP_PASSKEY_LEN];
|
||||
)
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* INTERNAL USAGE ONLY
|
||||
*------------------------------------------------------------------*/
|
||||
|
||||
@@ -68,4 +68,23 @@
|
||||
typedef void (*ble_connect_callback_t ) (uint16_t conn_hdl);
|
||||
typedef void (*ble_disconnect_callback_t ) (uint16_t conn_hdl, uint8_t reason);
|
||||
|
||||
enum BleSecurityMode
|
||||
{
|
||||
SECMODE_NO_ACCESS = 0x00,
|
||||
SECMODE_OPEN = 0x11,
|
||||
SECMODE_ENC_NO_MITM = 0x21,
|
||||
SECMODE_ENC_WITH_MITM = 0x31,
|
||||
SECMODE_ENC_WITH_LESC_MITM = 0x41, // LESC MITM with 128-bit key
|
||||
SECMODE_SIGNED_NO_MITM = 0x12,
|
||||
SECMODE_SIGNED_WITH_MITM = 0x22
|
||||
};
|
||||
|
||||
#define BLE_SECMODE_NO_ACCESS ((ble_gap_conn_sec_mode_t) { .sm = 0, .lv = 0 })
|
||||
#define BLE_SECMODE_OPEN ((ble_gap_conn_sec_mode_t) { .sm = 1, .lv = 1 })
|
||||
#define BLE_SECMODE_ENC_NO_MITM ((ble_gap_conn_sec_mode_t) { .sm = 1, .lv = 2 })
|
||||
#define BLE_SECMODE_ENC_WITH_MITM ((ble_gap_conn_sec_mode_t) { .sm = 1, .lv = 3 })
|
||||
#define BLE_SECMODE_SIGNED_NO_MITM ((ble_gap_conn_sec_mode_t) { .sm = 2, .lv = 1 })
|
||||
#define BLE_SECMODE_SIGNED_WITH_MITM ((ble_gap_conn_sec_mode_t) { .sm = 2, .lv = 2 })
|
||||
|
||||
|
||||
#endif /* BLUEFRUIT_COMMON_H_ */
|
||||
@@ -190,7 +190,8 @@ static void bledfu_control_wr_authorize_cb(uint16_t conn_hdl, BLECharacteristic*
|
||||
}
|
||||
}
|
||||
|
||||
BLEDfu::BLEDfu(void) : BLEService(UUID128_SVC_DFU_OTA), _chr_control(UUID128_CHR_DFU_CONTROL)
|
||||
BLEDfu::BLEDfu(void)
|
||||
: BLEService(UUID128_SVC_DFU_OTA), _chr_control(UUID128_CHR_DFU_CONTROL)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user