This commit is contained in:
Mark Qvist 2024-10-11 14:48:42 +02:00
parent c51335a1df
commit 7a8a1b5dee

View File

@ -387,7 +387,8 @@ char bt_devname[11];
bt_ssp_pin = 0; bt_ssp_pin = 0;
bt_state = BT_STATE_ON; bt_state = BT_STATE_ON;
} }
void bt_pairing_complete(uint16_t conn_handle, uint8_t auth_status) {
void bt_pairing_complete(uint16_t conn_handle, uint8_t auth_status) {
if (auth_status == BLE_GAP_SEC_STATUS_SUCCESS) { if (auth_status == BLE_GAP_SEC_STATUS_SUCCESS) {
BLEConnection* connection = Bluefruit.Connection(conn_handle); BLEConnection* connection = Bluefruit.Connection(conn_handle);
@ -420,21 +421,21 @@ void bt_pairing_complete(uint16_t conn_handle, uint8_t auth_status) {
} else { } else {
bt_ssp_pin = 0; bt_ssp_pin = 0;
} }
} }
bool bt_passkey_callback(uint16_t conn_handle, uint8_t const passkey[6], bool match_request) { bool bt_passkey_callback(uint16_t conn_handle, uint8_t const passkey[6], bool match_request) {
for (int i = 0; i < 6; i++) { for (int i = 0; i < 6; i++) {
// multiply by tens however many times needed to make numbers appear in order // multiply by tens however many times needed to make numbers appear in order
bt_ssp_pin += ((int)passkey[i] - 48) * pow(10, 5-i); bt_ssp_pin += ((int)passkey[i] - 48) * pow(10, 5-i);
} }
kiss_indicate_btpin(); kiss_indicate_btpin();
if (bt_allow_pairing) { if (bt_allow_pairing) {
return true; return true;
} }
return false; return false;
} }
void bt_connect_callback(uint16_t conn_handle) { void bt_connect_callback(uint16_t conn_handle) {
bt_state = BT_STATE_CONNECTED; bt_state = BT_STATE_CONNECTED;
cable_state = CABLE_STATE_DISCONNECTED; cable_state = CABLE_STATE_DISCONNECTED;
@ -442,95 +443,95 @@ void bt_connect_callback(uint16_t conn_handle) {
conn->requestPHY(BLE_GAP_PHY_2MBPS); conn->requestPHY(BLE_GAP_PHY_2MBPS);
conn->requestMtuExchange(512+3); conn->requestMtuExchange(512+3);
conn->requestDataLengthUpdate(); conn->requestDataLengthUpdate();
} }
void bt_disconnect_callback(uint16_t conn_handle, uint8_t reason) { void bt_disconnect_callback(uint16_t conn_handle, uint8_t reason) {
if (reason != BLE_GAP_SEC_STATUS_SUCCESS) { if (reason != BLE_GAP_SEC_STATUS_SUCCESS) {
bt_state = BT_STATE_ON; bt_state = BT_STATE_ON;
} }
} }
bool bt_setup_hw() { bool bt_setup_hw() {
if (!bt_ready) { if (!bt_ready) {
#if HAS_EEPROM
if (EEPROM.read(eeprom_addr(ADDR_CONF_BT)) == BT_ENABLE_BYTE) {
#else
if (eeprom_read(eeprom_addr(ADDR_CONF_BT)) == BT_ENABLE_BYTE) {
#endif
bt_enabled = true;
} else {
bt_enabled = false;
}
Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);
Bluefruit.autoConnLed(false);
if (Bluefruit.begin()) {
Bluefruit.setTxPower(8); // Check bluefruit.h for supported values
Bluefruit.Security.setIOCaps(true, false, false); // display, yes; yes / no, no; keyboard, no
// This device is indeed capable of yes / no through the pairing mode
// being set, but I have chosen to set it thus to force the input of the
// pin on the device initiating the pairing.
Bluefruit.Security.setMITM(true);
Bluefruit.Security.setPairPasskeyCallback(bt_passkey_callback);
Bluefruit.Security.setSecuredCallback(bt_connect_callback);
Bluefruit.Periph.setDisconnectCallback(bt_disconnect_callback);
Bluefruit.Security.setPairCompleteCallback(bt_pairing_complete);
//Bluefruit.Periph.setConnInterval(6, 12); // 7.5 - 15 ms
const ble_gap_addr_t gap_addr = Bluefruit.getAddr();
char *data = (char*)malloc(BT_DEV_ADDR_LEN+1);
for (int i = 0; i < BT_DEV_ADDR_LEN; i++) {
data[i] = gap_addr.addr[i];
}
#if HAS_EEPROM #if HAS_EEPROM
data[BT_DEV_ADDR_LEN] = EEPROM.read(eeprom_addr(ADDR_SIGNATURE)); if (EEPROM.read(eeprom_addr(ADDR_CONF_BT)) == BT_ENABLE_BYTE) {
#else #else
data[BT_DEV_ADDR_LEN] = eeprom_read(eeprom_addr(ADDR_SIGNATURE)); if (eeprom_read(eeprom_addr(ADDR_CONF_BT)) == BT_ENABLE_BYTE) {
#endif #endif
unsigned char *hash = MD5::make_hash(data, BT_DEV_ADDR_LEN); bt_enabled = true;
memcpy(bt_dh, hash, BT_DEV_HASH_LEN); } else {
sprintf(bt_devname, "RNode %02X%02X", bt_dh[14], bt_dh[15]); bt_enabled = false;
free(data); }
Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);
Bluefruit.autoConnLed(false);
if (Bluefruit.begin()) {
Bluefruit.setTxPower(8); // Check bluefruit.h for supported values
Bluefruit.Security.setIOCaps(true, false, false); // display, yes; yes / no, no; keyboard, no
// This device is indeed capable of yes / no through the pairing mode
// being set, but I have chosen to set it thus to force the input of the
// pin on the device initiating the pairing.
bt_ready = true; Bluefruit.Security.setMITM(true);
return true; Bluefruit.Security.setPairPasskeyCallback(bt_passkey_callback);
Bluefruit.Security.setSecuredCallback(bt_connect_callback);
Bluefruit.Periph.setDisconnectCallback(bt_disconnect_callback);
Bluefruit.Security.setPairCompleteCallback(bt_pairing_complete);
//Bluefruit.Periph.setConnInterval(6, 12); // 7.5 - 15 ms
const ble_gap_addr_t gap_addr = Bluefruit.getAddr();
char *data = (char*)malloc(BT_DEV_ADDR_LEN+1);
for (int i = 0; i < BT_DEV_ADDR_LEN; i++) {
data[i] = gap_addr.addr[i];
}
#if HAS_EEPROM
data[BT_DEV_ADDR_LEN] = EEPROM.read(eeprom_addr(ADDR_SIGNATURE));
#else
data[BT_DEV_ADDR_LEN] = eeprom_read(eeprom_addr(ADDR_SIGNATURE));
#endif
unsigned char *hash = MD5::make_hash(data, BT_DEV_ADDR_LEN);
memcpy(bt_dh, hash, BT_DEV_HASH_LEN);
sprintf(bt_devname, "RNode %02X%02X", bt_dh[14], bt_dh[15]);
free(data);
bt_ready = true;
return true;
} else { return false; }
} else { return false; } } else { return false; }
} else { return false; } }
}
void bt_start() { void bt_start() {
if (bt_state == BT_STATE_OFF) { if (bt_state == BT_STATE_OFF) {
Bluefruit.setName(bt_devname); Bluefruit.setName(bt_devname);
bledis.setManufacturer(BLE_MANUFACTURER); bledis.setManufacturer(BLE_MANUFACTURER);
bledis.setModel(BLE_MODEL); bledis.setModel(BLE_MODEL);
// start device information service // start device information service
bledis.begin(); bledis.begin();
SerialBT.bufferTXD(true); // enable buffering SerialBT.bufferTXD(true); // enable buffering
SerialBT.setPermission(SECMODE_ENC_WITH_MITM, SECMODE_ENC_WITH_MITM); // enable encryption for BLE serial SerialBT.setPermission(SECMODE_ENC_WITH_MITM, SECMODE_ENC_WITH_MITM); // enable encryption for BLE serial
SerialBT.begin(); SerialBT.begin();
blebas.begin(); blebas.begin();
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
Bluefruit.Advertising.addTxPower(); Bluefruit.Advertising.addTxPower();
// Include bleuart 128-bit uuid // Include bleuart 128-bit uuid
Bluefruit.Advertising.addService(SerialBT); Bluefruit.Advertising.addService(SerialBT);
// There is no room for Name in Advertising packet // There is no room for Name in Advertising packet
// Use Scan response for Name // Use Scan response for Name
Bluefruit.ScanResponse.addName(); Bluefruit.ScanResponse.addName();
Bluefruit.Advertising.start(0); Bluefruit.Advertising.start(0);
bt_state = BT_STATE_ON; bt_state = BT_STATE_ON;
} }
} }
bool bt_init() { bool bt_init() {
bt_state = BT_STATE_OFF; bt_state = BT_STATE_OFF;
if (bt_setup_hw()) { if (bt_setup_hw()) {
if (bt_enabled && !console_active) bt_start(); if (bt_enabled && !console_active) bt_start();
@ -538,18 +539,18 @@ bool bt_init() {
} else { } else {
return false; return false;
} }
}
void bt_enable_pairing() {
if (bt_state == BT_STATE_OFF) bt_start();
bt_allow_pairing = true;
bt_pairing_started = millis();
bt_state = BT_STATE_PAIRING;
}
void update_bt() {
if (bt_allow_pairing && millis()-bt_pairing_started >= BT_PAIRING_TIMEOUT) {
bt_disable_pairing();
} }
}
void bt_enable_pairing() {
if (bt_state == BT_STATE_OFF) bt_start();
bt_allow_pairing = true;
bt_pairing_started = millis();
bt_state = BT_STATE_PAIRING;
}
void update_bt() {
if (bt_allow_pairing && millis()-bt_pairing_started >= BT_PAIRING_TIMEOUT) {
bt_disable_pairing();
}
}
#endif #endif