mirror of
https://github.com/liberatedsystems/RNode_Firmware_CE.git
synced 2025-08-10 07:10:30 -04:00
Incorporate changes from upstream 1.82
This commit is contained in:
commit
fd5520d95f
12 changed files with 251 additions and 128 deletions
213
Bluetooth.h
213
Bluetooth.h
|
@ -165,31 +165,42 @@ char bt_devname[11];
|
|||
}
|
||||
|
||||
#elif HAS_BLE == true
|
||||
bool bt_setup_hw(); void bt_security_setup();
|
||||
BLESecurity *ble_security = new BLESecurity();
|
||||
bool ble_authenticated = false;
|
||||
uint32_t pairing_pin = 0;
|
||||
|
||||
void bt_flush() { if (bt_state == BT_STATE_CONNECTED) { SerialBT.flush(); } }
|
||||
|
||||
void bt_disable_pairing() {
|
||||
void bt_start() {
|
||||
// Serial.println("BT start");
|
||||
display_unblank();
|
||||
bt_allow_pairing = false;
|
||||
bt_ssp_pin = 0;
|
||||
bt_state = BT_STATE_ON;
|
||||
if (bt_state == BT_STATE_OFF) {
|
||||
bt_state = BT_STATE_ON;
|
||||
SerialBT.begin(bt_devname);
|
||||
SerialBT.setTimeout(10);
|
||||
}
|
||||
}
|
||||
|
||||
void bt_passkey_notify_callback(uint32_t passkey) {
|
||||
// Serial.printf("Got passkey notification: %d\n", passkey);
|
||||
bt_ssp_pin = passkey;
|
||||
bt_state = BT_STATE_PAIRING;
|
||||
bt_allow_pairing = true;
|
||||
bt_pairing_started = millis();
|
||||
kiss_indicate_btpin();
|
||||
void bt_stop() {
|
||||
// Serial.println("BT stop");
|
||||
display_unblank();
|
||||
if (bt_state != BT_STATE_OFF) {
|
||||
bt_allow_pairing = false;
|
||||
bt_state = BT_STATE_OFF;
|
||||
SerialBT.end();
|
||||
}
|
||||
}
|
||||
|
||||
bool bt_confirm_pin_callback(uint32_t pin) {
|
||||
// Serial.printf("Confirm PIN callback: %d\n", pin);
|
||||
return true;
|
||||
bool bt_init() {
|
||||
// Serial.println("BT init");
|
||||
bt_state = BT_STATE_OFF;
|
||||
if (bt_setup_hw()) {
|
||||
if (bt_enabled && !console_active) bt_start();
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void bt_debond_all() {
|
||||
|
@ -201,6 +212,44 @@ char bt_devname[11];
|
|||
free(dev_list);
|
||||
}
|
||||
|
||||
void bt_enable_pairing() {
|
||||
// Serial.println("BT enable pairing");
|
||||
display_unblank();
|
||||
if (bt_state == BT_STATE_OFF) bt_start();
|
||||
|
||||
bt_security_setup();
|
||||
|
||||
bt_allow_pairing = true;
|
||||
bt_pairing_started = millis();
|
||||
bt_state = BT_STATE_PAIRING;
|
||||
bt_ssp_pin = pairing_pin;
|
||||
}
|
||||
|
||||
void bt_disable_pairing() {
|
||||
// Serial.println("BT disable pairing");
|
||||
display_unblank();
|
||||
bt_allow_pairing = false;
|
||||
bt_ssp_pin = 0;
|
||||
bt_state = BT_STATE_ON;
|
||||
}
|
||||
|
||||
void bt_passkey_notify_callback(uint32_t passkey) {
|
||||
// Serial.printf("Got passkey notification: %d\n", passkey);
|
||||
if (bt_allow_pairing) {
|
||||
bt_ssp_pin = passkey;
|
||||
bt_pairing_started = millis();
|
||||
kiss_indicate_btpin();
|
||||
} else {
|
||||
// Serial.println("Pairing not allowed, re-init");
|
||||
SerialBT.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
bool bt_confirm_pin_callback(uint32_t pin) {
|
||||
// Serial.printf("Confirm PIN callback: %d\n", pin);
|
||||
return true;
|
||||
}
|
||||
|
||||
void bt_update_passkey() {
|
||||
// Serial.println("Updating passkey");
|
||||
pairing_pin = random(899999)+100000;
|
||||
|
@ -217,31 +266,6 @@ char bt_devname[11];
|
|||
return ble_authenticated;
|
||||
}
|
||||
|
||||
void bt_security_setup() {
|
||||
uint32_t passkey = bt_passkey_callback();
|
||||
|
||||
// Serial.printf("Executing BT security setup, passkey is %d\n", passkey);
|
||||
|
||||
uint8_t key_size = 16;
|
||||
uint8_t init_key = ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK;
|
||||
uint8_t rsp_key = ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK;
|
||||
|
||||
esp_ble_auth_req_t auth_req = ESP_LE_AUTH_REQ_SC_MITM_BOND;
|
||||
uint8_t auth_option = ESP_BLE_ONLY_ACCEPT_SPECIFIED_AUTH_ENABLE;
|
||||
uint8_t oob_support = ESP_BLE_OOB_DISABLE;
|
||||
|
||||
esp_ble_io_cap_t iocap = ESP_IO_CAP_OUT;
|
||||
|
||||
esp_ble_gap_set_security_param(ESP_BLE_SM_SET_STATIC_PASSKEY, &passkey, sizeof(uint32_t));
|
||||
esp_ble_gap_set_security_param(ESP_BLE_SM_AUTHEN_REQ_MODE, &auth_req, sizeof(uint8_t));
|
||||
esp_ble_gap_set_security_param(ESP_BLE_SM_IOCAP_MODE, &iocap, sizeof(uint8_t));
|
||||
esp_ble_gap_set_security_param(ESP_BLE_SM_MAX_KEY_SIZE, &key_size, sizeof(uint8_t));
|
||||
esp_ble_gap_set_security_param(ESP_BLE_SM_ONLY_ACCEPT_SPECIFIED_SEC_AUTH, &auth_option, sizeof(uint8_t));
|
||||
esp_ble_gap_set_security_param(ESP_BLE_SM_OOB_SUPPORT, &oob_support, sizeof(uint8_t));
|
||||
esp_ble_gap_set_security_param(ESP_BLE_SM_SET_INIT_KEY, &init_key, sizeof(uint8_t));
|
||||
esp_ble_gap_set_security_param(ESP_BLE_SM_SET_RSP_KEY, &rsp_key, sizeof(uint8_t));
|
||||
}
|
||||
|
||||
bool bt_security_request_callback() {
|
||||
if (bt_allow_pairing) {
|
||||
// Serial.println("Accepting security request");
|
||||
|
@ -256,11 +280,15 @@ char bt_devname[11];
|
|||
if (auth_result.success == true) {
|
||||
// Serial.println("Authentication success");
|
||||
ble_authenticated = true;
|
||||
bt_state = BT_STATE_CONNECTED;
|
||||
if (bt_state == BT_STATE_PAIRING) {
|
||||
// Serial.println("Pairing complete, disconnecting");
|
||||
delay(2000); SerialBT.disconnect();
|
||||
} else { bt_state = BT_STATE_CONNECTED; }
|
||||
} else {
|
||||
// Serial.println("Authentication fail");
|
||||
ble_authenticated = false;
|
||||
bt_state = BT_STATE_ON;
|
||||
bt_update_passkey();
|
||||
bt_security_setup();
|
||||
}
|
||||
bt_allow_pairing = false;
|
||||
|
@ -268,16 +296,16 @@ char bt_devname[11];
|
|||
}
|
||||
|
||||
void bt_connect_callback(BLEServer *server) {
|
||||
// uint16_t conn_id = server->getConnId();
|
||||
uint16_t conn_id = server->getConnId();
|
||||
// Serial.printf("Connected: %d\n", conn_id);
|
||||
display_unblank();
|
||||
ble_authenticated = false;
|
||||
bt_state = BT_STATE_CONNECTED;
|
||||
if (bt_state != BT_STATE_PAIRING) { bt_state = BT_STATE_CONNECTED; }
|
||||
cable_state = CABLE_STATE_DISCONNECTED;
|
||||
}
|
||||
|
||||
void bt_disconnect_callback(BLEServer *server) {
|
||||
// uint16_t conn_id = server->getConnId();
|
||||
uint16_t conn_id = server->getConnId();
|
||||
// Serial.printf("Disconnected: %d\n", conn_id);
|
||||
display_unblank();
|
||||
ble_authenticated = false;
|
||||
|
@ -285,6 +313,7 @@ char bt_devname[11];
|
|||
}
|
||||
|
||||
bool bt_setup_hw() {
|
||||
// Serial.println("BT setup hw");
|
||||
if (!bt_ready) {
|
||||
if (EEPROM.read(eeprom_addr(ADDR_CONF_BT)) == BT_ENABLE_BYTE) {
|
||||
bt_enabled = true;
|
||||
|
@ -316,45 +345,30 @@ char bt_devname[11];
|
|||
} else { return false; }
|
||||
}
|
||||
|
||||
void bt_start() {
|
||||
display_unblank();
|
||||
if (bt_state == BT_STATE_OFF) {
|
||||
bt_state = BT_STATE_ON;
|
||||
SerialBT.begin(bt_devname);
|
||||
SerialBT.setTimeout(10);
|
||||
}
|
||||
}
|
||||
void bt_security_setup() {
|
||||
// Serial.println("Executing BT security setup");
|
||||
if (pairing_pin == 0) { bt_update_passkey(); }
|
||||
uint32_t passkey = pairing_pin;
|
||||
// Serial.printf("Passkey is %d\n", passkey);
|
||||
|
||||
void bt_stop() {
|
||||
display_unblank();
|
||||
if (bt_state != BT_STATE_OFF) {
|
||||
bt_allow_pairing = false;
|
||||
bt_state = BT_STATE_OFF;
|
||||
SerialBT.end();
|
||||
}
|
||||
}
|
||||
uint8_t key_size = 16;
|
||||
uint8_t init_key = ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK;
|
||||
uint8_t rsp_key = ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK;
|
||||
|
||||
bool bt_init() {
|
||||
bt_state = BT_STATE_OFF;
|
||||
if (bt_setup_hw()) {
|
||||
if (bt_enabled && !console_active) bt_start();
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
esp_ble_auth_req_t auth_req = ESP_LE_AUTH_REQ_SC_MITM_BOND;
|
||||
uint8_t auth_option = ESP_BLE_ONLY_ACCEPT_SPECIFIED_AUTH_ENABLE;
|
||||
uint8_t oob_support = ESP_BLE_OOB_DISABLE;
|
||||
|
||||
void bt_enable_pairing() {
|
||||
display_unblank();
|
||||
if (bt_state == BT_STATE_OFF) bt_start();
|
||||
esp_ble_io_cap_t iocap = ESP_IO_CAP_OUT;
|
||||
|
||||
bt_security_setup();
|
||||
//bt_debond_all();
|
||||
//bt_update_passkey();
|
||||
|
||||
bt_allow_pairing = true;
|
||||
bt_pairing_started = millis();
|
||||
bt_state = BT_STATE_PAIRING;
|
||||
esp_ble_gap_set_security_param(ESP_BLE_SM_SET_STATIC_PASSKEY, &passkey, sizeof(uint32_t));
|
||||
esp_ble_gap_set_security_param(ESP_BLE_SM_AUTHEN_REQ_MODE, &auth_req, sizeof(uint8_t));
|
||||
esp_ble_gap_set_security_param(ESP_BLE_SM_IOCAP_MODE, &iocap, sizeof(uint8_t));
|
||||
esp_ble_gap_set_security_param(ESP_BLE_SM_MAX_KEY_SIZE, &key_size, sizeof(uint8_t));
|
||||
esp_ble_gap_set_security_param(ESP_BLE_SM_ONLY_ACCEPT_SPECIFIED_SEC_AUTH, &auth_option, sizeof(uint8_t));
|
||||
esp_ble_gap_set_security_param(ESP_BLE_SM_OOB_SUPPORT, &oob_support, sizeof(uint8_t));
|
||||
esp_ble_gap_set_security_param(ESP_BLE_SM_SET_INIT_KEY, &init_key, sizeof(uint8_t));
|
||||
esp_ble_gap_set_security_param(ESP_BLE_SM_SET_RSP_KEY, &rsp_key, sizeof(uint8_t));
|
||||
}
|
||||
|
||||
void update_bt() {
|
||||
|
@ -375,6 +389,7 @@ char bt_devname[11];
|
|||
uint8_t eeprom_read(uint32_t mapped_addr);
|
||||
|
||||
void bt_stop() {
|
||||
// Serial.println("BT Stop");
|
||||
if (bt_state != BT_STATE_OFF) {
|
||||
bt_allow_pairing = false;
|
||||
bt_state = BT_STATE_OFF;
|
||||
|
@ -382,7 +397,9 @@ char bt_devname[11];
|
|||
}
|
||||
|
||||
void bt_disable_pairing() {
|
||||
// Serial.println("BT Disable pairing");
|
||||
bt_allow_pairing = false;
|
||||
pairing_pin = 0;
|
||||
bt_ssp_pin = 0;
|
||||
bt_state = BT_STATE_ON;
|
||||
}
|
||||
|
@ -390,10 +407,11 @@ char bt_devname[11];
|
|||
void bt_flush() { if (bt_state == BT_STATE_CONNECTED) { SerialBT.flushTXD(); } }
|
||||
|
||||
void bt_pairing_complete(uint16_t conn_handle, uint8_t auth_status) {
|
||||
// Serial.println("BT pairing complete");
|
||||
BLEConnection* connection = Bluefruit.Connection(conn_handle);
|
||||
if (auth_status == BLE_GAP_SEC_STATUS_SUCCESS) {
|
||||
BLEConnection* connection = Bluefruit.Connection(conn_handle);
|
||||
|
||||
ble_gap_conn_sec_mode_t security = connection->getSecureMode();
|
||||
// Serial.println("Bonding success");
|
||||
|
||||
// On the NRF52 it is not possible with the Arduino library to reject
|
||||
// requests from devices with no IO capabilities, which would allow
|
||||
|
@ -410,21 +428,26 @@ char bt_devname[11];
|
|||
// Requires investigation.
|
||||
|
||||
if (security.sm == 1 && security.lv >= 3) {
|
||||
// Serial.println("Auth level success");
|
||||
bt_state = BT_STATE_CONNECTED;
|
||||
cable_state = CABLE_STATE_DISCONNECTED;
|
||||
connection->disconnect();
|
||||
bt_disable_pairing();
|
||||
} else {
|
||||
if (connection->bonded()) {
|
||||
connection->removeBondKey();
|
||||
}
|
||||
// Serial.println("Auth level failure, debonding");
|
||||
if (connection->bonded()) { connection->removeBondKey(); }
|
||||
connection->disconnect();
|
||||
bt_disable_pairing();
|
||||
}
|
||||
} else {
|
||||
bt_ssp_pin = 0;
|
||||
// Serial.println("Bonding failure");
|
||||
connection->disconnect();
|
||||
bt_disable_pairing();
|
||||
}
|
||||
}
|
||||
|
||||
bool bt_passkey_callback(uint16_t conn_handle, uint8_t const passkey[6], bool match_request) {
|
||||
// Serial.println("Passkey callback");
|
||||
if (bt_allow_pairing) {
|
||||
return true;
|
||||
}
|
||||
|
@ -432,6 +455,7 @@ char bt_devname[11];
|
|||
}
|
||||
|
||||
void bt_connect_callback(uint16_t conn_handle) {
|
||||
// Serial.println("Connect callback");
|
||||
bt_state = BT_STATE_CONNECTED;
|
||||
cable_state = CABLE_STATE_DISCONNECTED;
|
||||
|
||||
|
@ -442,14 +466,16 @@ char bt_devname[11];
|
|||
}
|
||||
|
||||
void bt_disconnect_callback(uint16_t conn_handle, uint8_t reason) {
|
||||
// Serial.println("Disconnect callback");
|
||||
if (reason != BLE_GAP_SEC_STATUS_SUCCESS) {
|
||||
bt_state = BT_STATE_ON;
|
||||
}
|
||||
}
|
||||
|
||||
void bt_update_passkey() {
|
||||
pairing_pin = random(899999)+100000;
|
||||
bt_ssp_pin = pairing_pin;
|
||||
// Serial.println("Update passkey");
|
||||
pairing_pin = random(899999)+100000;
|
||||
bt_ssp_pin = pairing_pin;
|
||||
}
|
||||
|
||||
uint32_t bt_get_passkey() {
|
||||
|
@ -459,6 +485,7 @@ char bt_devname[11];
|
|||
}
|
||||
|
||||
bool bt_setup_hw() {
|
||||
// Serial.println("Setup HW");
|
||||
if (!bt_ready) {
|
||||
#if HAS_EEPROM
|
||||
if (EEPROM.read(eeprom_addr(ADDR_CONF_BT)) == BT_ENABLE_BYTE) {
|
||||
|
@ -513,12 +540,14 @@ char bt_devname[11];
|
|||
}
|
||||
|
||||
void bt_start() {
|
||||
// Serial.println("BT Start");
|
||||
if (bt_state == BT_STATE_OFF) {
|
||||
Bluefruit.setName(bt_devname);
|
||||
bledis.setManufacturer(BLE_MANUFACTURER);
|
||||
bledis.setModel(BLE_MODEL);
|
||||
// start device information service
|
||||
bledis.begin();
|
||||
blebas.begin();
|
||||
|
||||
// Guard to ensure SerialBT service is not duplicated through BT being power cycled
|
||||
if (!SerialBT_init) {
|
||||
|
@ -529,8 +558,6 @@ char bt_devname[11];
|
|||
SerialBT_init = true;
|
||||
}
|
||||
|
||||
blebas.begin();
|
||||
|
||||
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
|
||||
Bluefruit.Advertising.addTxPower();
|
||||
|
||||
|
@ -548,6 +575,7 @@ char bt_devname[11];
|
|||
}
|
||||
|
||||
bool bt_init() {
|
||||
// Serial.println("BT init");
|
||||
bt_state = BT_STATE_OFF;
|
||||
if (bt_setup_hw()) {
|
||||
if (bt_enabled && !console_active) bt_start();
|
||||
|
@ -558,13 +586,22 @@ char bt_devname[11];
|
|||
}
|
||||
|
||||
void bt_enable_pairing() {
|
||||
// Serial.println("BT enable pairing");
|
||||
if (bt_state == BT_STATE_OFF) bt_start();
|
||||
|
||||
uint32_t pin = bt_get_passkey();
|
||||
char pin_char[6];
|
||||
sprintf(pin_char,"%lu", pin);
|
||||
Bluefruit.Security.setPIN(pin_char);
|
||||
|
||||
bt_allow_pairing = true;
|
||||
bt_pairing_started = millis();
|
||||
bt_state = BT_STATE_PAIRING;
|
||||
kiss_indicate_btpin();
|
||||
}
|
||||
|
||||
void bt_debond_all() { }
|
||||
|
||||
void update_bt() {
|
||||
if (bt_allow_pairing && millis()-bt_pairing_started >= BT_PAIRING_TIMEOUT) {
|
||||
bt_disable_pairing();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue