2023-01-13 16:32:50 -07:00
|
|
|
// Copyright (C) 2023, Mark Qvist
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2024-01-25 14:35:40 -07:00
|
|
|
#if MCU_VARIANT == MCU_ESP32
|
2022-10-30 06:51:36 -06:00
|
|
|
#include "BluetoothSerial.h"
|
|
|
|
#include "esp_bt_main.h"
|
|
|
|
#include "esp_bt_device.h"
|
|
|
|
|
2024-01-25 14:35:40 -07:00
|
|
|
#elif MCU_VARIANT == MCU_NRF52
|
|
|
|
#include <bluefruit.h>
|
|
|
|
#include <math.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if MCU_VARIANT == MCU_ESP32
|
2022-10-30 06:51:36 -06:00
|
|
|
BluetoothSerial SerialBT;
|
2024-01-25 14:35:40 -07:00
|
|
|
#elif MCU_VARIANT == MCU_NRF52
|
|
|
|
BLEUart SerialBT;
|
|
|
|
BLEDis bledis;
|
|
|
|
BLEBas blebas;
|
|
|
|
#endif
|
|
|
|
|
2022-10-30 07:52:22 -06:00
|
|
|
#define BT_PAIRING_TIMEOUT 35000
|
2022-10-30 06:51:36 -06:00
|
|
|
uint32_t bt_pairing_started = 0;
|
|
|
|
|
|
|
|
#define BT_DEV_ADDR_LEN 6
|
|
|
|
#define BT_DEV_HASH_LEN 16
|
2022-11-01 14:11:41 -06:00
|
|
|
uint8_t dev_bt_mac[BT_DEV_ADDR_LEN];
|
2022-10-30 06:51:36 -06:00
|
|
|
char bt_da[BT_DEV_ADDR_LEN];
|
|
|
|
char bt_dh[BT_DEV_HASH_LEN];
|
|
|
|
char bt_devname[11];
|
|
|
|
|
|
|
|
#if MCU_VARIANT == MCU_ESP32
|
|
|
|
|
|
|
|
void bt_confirm_pairing(uint32_t numVal) {
|
2022-10-30 09:49:35 -06:00
|
|
|
bt_ssp_pin = numVal;
|
2023-02-04 05:46:04 -07:00
|
|
|
kiss_indicate_btpin();
|
2022-10-30 06:51:36 -06:00
|
|
|
if (bt_allow_pairing) {
|
|
|
|
SerialBT.confirmReply(true);
|
|
|
|
} else {
|
|
|
|
SerialBT.confirmReply(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-30 07:52:22 -06:00
|
|
|
void bt_stop() {
|
2022-10-30 06:51:36 -06:00
|
|
|
if (bt_state != BT_STATE_OFF) {
|
|
|
|
SerialBT.end();
|
2022-10-30 07:52:22 -06:00
|
|
|
bt_allow_pairing = false;
|
2022-10-30 06:51:36 -06:00
|
|
|
bt_state = BT_STATE_OFF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-30 07:52:22 -06:00
|
|
|
void bt_start() {
|
2022-10-30 06:51:36 -06:00
|
|
|
if (bt_state == BT_STATE_OFF) {
|
|
|
|
SerialBT.begin(bt_devname);
|
|
|
|
bt_state = BT_STATE_ON;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-30 07:52:22 -06:00
|
|
|
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 bt_disable_pairing() {
|
|
|
|
bt_allow_pairing = false;
|
2022-10-30 09:49:35 -06:00
|
|
|
bt_ssp_pin = 0;
|
2022-10-30 07:52:22 -06:00
|
|
|
bt_state = BT_STATE_ON;
|
|
|
|
}
|
|
|
|
|
2022-10-30 09:49:35 -06:00
|
|
|
void bt_pairing_complete(boolean success) {
|
|
|
|
if (success) {
|
|
|
|
bt_disable_pairing();
|
|
|
|
} else {
|
|
|
|
bt_ssp_pin = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void bt_connection_callback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param){
|
|
|
|
if(event == ESP_SPP_SRV_OPEN_EVT) {
|
|
|
|
bt_state = BT_STATE_CONNECTED;
|
2022-10-30 11:35:53 -06:00
|
|
|
cable_state = CABLE_STATE_DISCONNECTED;
|
2022-10-30 09:49:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if(event == ESP_SPP_CLOSE_EVT ){
|
|
|
|
bt_state = BT_STATE_ON;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-02 12:02:22 -06:00
|
|
|
bool bt_setup_hw() {
|
|
|
|
if (!bt_ready) {
|
|
|
|
if (EEPROM.read(eeprom_addr(ADDR_CONF_BT)) == BT_ENABLE_BYTE) {
|
|
|
|
bt_enabled = true;
|
|
|
|
} else {
|
|
|
|
bt_enabled = false;
|
|
|
|
}
|
2022-10-30 06:51:36 -06:00
|
|
|
if (btStart()) {
|
|
|
|
if (esp_bluedroid_init() == ESP_OK) {
|
|
|
|
if (esp_bluedroid_enable() == ESP_OK) {
|
|
|
|
const uint8_t* bda_ptr = esp_bt_dev_get_address();
|
2022-11-02 12:02:22 -06:00
|
|
|
char *data = (char*)malloc(BT_DEV_ADDR_LEN+1);
|
|
|
|
for (int i = 0; i < BT_DEV_ADDR_LEN; i++) {
|
|
|
|
data[i] = bda_ptr[i];
|
|
|
|
}
|
|
|
|
data[BT_DEV_ADDR_LEN] = EEPROM.read(eeprom_addr(ADDR_SIGNATURE));
|
|
|
|
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);
|
|
|
|
|
|
|
|
SerialBT.enableSSP();
|
|
|
|
SerialBT.onConfirmRequest(bt_confirm_pairing);
|
|
|
|
SerialBT.onAuthComplete(bt_pairing_complete);
|
|
|
|
SerialBT.register_callback(bt_connection_callback);
|
|
|
|
|
|
|
|
bt_ready = true;
|
2022-10-30 06:51:36 -06:00
|
|
|
return true;
|
|
|
|
|
2022-11-02 12:02:22 -06:00
|
|
|
} else { return false; }
|
|
|
|
} else { return false; }
|
2022-10-30 06:51:36 -06:00
|
|
|
} else { return false; }
|
2023-12-10 14:40:06 -07:00
|
|
|
} else { return false; }
|
2022-10-30 06:51:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
bool bt_init() {
|
|
|
|
bt_state = BT_STATE_OFF;
|
2022-10-30 07:52:22 -06:00
|
|
|
if (bt_setup_hw()) {
|
2023-01-07 08:35:07 -07:00
|
|
|
if (bt_enabled && !console_active) bt_start();
|
2022-10-30 07:52:22 -06:00
|
|
|
return true;
|
2022-10-30 06:51:36 -06:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void update_bt() {
|
2022-10-30 07:52:22 -06:00
|
|
|
if (bt_allow_pairing && millis()-bt_pairing_started >= BT_PAIRING_TIMEOUT) {
|
2022-10-30 06:51:36 -06:00
|
|
|
bt_disable_pairing();
|
|
|
|
}
|
|
|
|
}
|
2022-10-30 07:52:22 -06:00
|
|
|
|
2024-01-25 14:35:40 -07:00
|
|
|
#elif MCU_VARIANT == MCU_NRF52
|
|
|
|
uint8_t eeprom_read(uint32_t mapped_addr);
|
|
|
|
|
|
|
|
void bt_stop() {
|
|
|
|
if (bt_state != BT_STATE_OFF) {
|
|
|
|
bt_allow_pairing = false;
|
|
|
|
bt_state = BT_STATE_OFF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void bt_disable_pairing() {
|
|
|
|
bt_allow_pairing = false;
|
|
|
|
bt_ssp_pin = 0;
|
|
|
|
bt_state = BT_STATE_ON;
|
|
|
|
}
|
|
|
|
|
|
|
|
void bt_pairing_complete(uint16_t conn_handle, uint8_t auth_status) {
|
|
|
|
if (auth_status == BLE_GAP_SEC_STATUS_SUCCESS) {
|
|
|
|
bt_disable_pairing();
|
|
|
|
} else {
|
|
|
|
bt_ssp_pin = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool bt_passkey_callback(uint16_t conn_handle, uint8_t const passkey[6], bool match_request) {
|
|
|
|
for (int i = 0; i < 6; i++) {
|
|
|
|
// multiply by tens however many times needed to make numbers appear in order
|
|
|
|
bt_ssp_pin += ((int)passkey[i] - 48) * pow(10, 5-i);
|
|
|
|
}
|
|
|
|
kiss_indicate_btpin();
|
|
|
|
if (match_request) {
|
|
|
|
if (bt_allow_pairing) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void bt_connect_callback(uint16_t conn_handle) {
|
|
|
|
bt_state = BT_STATE_CONNECTED;
|
|
|
|
cable_state = CABLE_STATE_DISCONNECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
void bt_disconnect_callback(uint16_t conn_handle, uint8_t reason) {
|
|
|
|
bt_state = BT_STATE_ON;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool bt_setup_hw() {
|
|
|
|
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(4); // Check bluefruit.h for supported values
|
|
|
|
Bluefruit.Security.setIOCaps(true, true, false);
|
|
|
|
Bluefruit.Security.setPairPasskeyCallback(bt_passkey_callback);
|
|
|
|
Bluefruit.Periph.setConnectCallback(bt_connect_callback);
|
|
|
|
Bluefruit.Periph.setDisconnectCallback(bt_disconnect_callback);
|
|
|
|
Bluefruit.Security.setIOCaps(true, true, false);
|
|
|
|
Bluefruit.Security.setPairCompleteCallback(bt_pairing_complete);
|
|
|
|
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; }
|
|
|
|
}
|
|
|
|
|
|
|
|
void bt_start() {
|
|
|
|
if (bt_state == BT_STATE_OFF) {
|
|
|
|
Bluefruit.setName(bt_devname);
|
|
|
|
bledis.setManufacturer("Adafruit Industries");
|
|
|
|
bledis.setModel("Bluefruit Feather52");
|
|
|
|
// start device information service
|
|
|
|
bledis.begin();
|
|
|
|
|
|
|
|
SerialBT.begin();
|
|
|
|
|
|
|
|
blebas.begin();
|
|
|
|
|
|
|
|
// non-connectable advertising
|
|
|
|
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
|
|
|
|
Bluefruit.Advertising.addTxPower();
|
|
|
|
|
|
|
|
// Include bleuart 128-bit uuid
|
|
|
|
Bluefruit.Advertising.addService(SerialBT);
|
|
|
|
|
|
|
|
// There is no room for Name in Advertising packet
|
|
|
|
// Use Scan response for Name
|
|
|
|
Bluefruit.ScanResponse.addName();
|
|
|
|
|
|
|
|
Bluefruit.Advertising.start(0);
|
|
|
|
|
|
|
|
bt_state = BT_STATE_ON;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool 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_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();
|
|
|
|
}
|
|
|
|
}
|
2022-10-30 06:51:36 -06:00
|
|
|
#endif
|