2022-10-30 08:51:36 -04:00
|
|
|
#include "BluetoothSerial.h"
|
|
|
|
#include "esp_bt_main.h"
|
|
|
|
#include "esp_bt_device.h"
|
|
|
|
|
|
|
|
BluetoothSerial SerialBT;
|
2022-10-30 09:52:22 -04:00
|
|
|
#define BT_PAIRING_TIMEOUT 35000
|
2022-10-30 08:51:36 -04:00
|
|
|
uint32_t bt_pairing_started = 0;
|
|
|
|
|
|
|
|
#define BT_DEV_ADDR_LEN 6
|
|
|
|
#define BT_DEV_HASH_LEN 16
|
2022-11-01 16:11:41 -04:00
|
|
|
uint8_t dev_bt_mac[BT_DEV_ADDR_LEN];
|
2022-10-30 08:51:36 -04: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 11:49:35 -04:00
|
|
|
bt_ssp_pin = numVal;
|
2022-10-30 08:51:36 -04:00
|
|
|
if (bt_allow_pairing) {
|
|
|
|
SerialBT.confirmReply(true);
|
|
|
|
} else {
|
|
|
|
SerialBT.confirmReply(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-30 09:52:22 -04:00
|
|
|
void bt_stop() {
|
2022-10-30 08:51:36 -04:00
|
|
|
if (bt_state != BT_STATE_OFF) {
|
|
|
|
SerialBT.end();
|
2022-10-30 09:52:22 -04:00
|
|
|
bt_allow_pairing = false;
|
2022-10-30 08:51:36 -04:00
|
|
|
bt_state = BT_STATE_OFF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-30 09:52:22 -04:00
|
|
|
void bt_start() {
|
2022-10-30 08:51:36 -04:00
|
|
|
if (bt_state == BT_STATE_OFF) {
|
|
|
|
SerialBT.begin(bt_devname);
|
|
|
|
bt_state = BT_STATE_ON;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-30 09:52:22 -04: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 11:49:35 -04:00
|
|
|
bt_ssp_pin = 0;
|
2022-10-30 09:52:22 -04:00
|
|
|
bt_state = BT_STATE_ON;
|
|
|
|
}
|
|
|
|
|
2022-10-30 11:49:35 -04: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 13:35:53 -04:00
|
|
|
cable_state = CABLE_STATE_DISCONNECTED;
|
2022-10-30 11:49:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if(event == ESP_SPP_CLOSE_EVT ){
|
|
|
|
bt_state = BT_STATE_ON;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-01 16:11:41 -04:00
|
|
|
bool bt_early_init_done = false;
|
|
|
|
bool bt_early_init() {
|
|
|
|
if (!bt_early_init_done) {
|
2022-10-30 08:51:36 -04: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-01 16:11:41 -04:00
|
|
|
memcpy(dev_bt_mac, bda_ptr, BT_DEV_HASH_LEN);
|
2022-10-30 08:51:36 -04:00
|
|
|
return true;
|
2022-11-01 16:11:41 -04:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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; }
|
|
|
|
if (bt_early_init()) {
|
|
|
|
const uint8_t* bda_ptr = esp_bt_dev_get_address();
|
|
|
|
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;
|
|
|
|
return true;
|
2022-10-30 08:51:36 -04:00
|
|
|
|
|
|
|
} else { return false; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool bt_init() {
|
|
|
|
bt_state = BT_STATE_OFF;
|
2022-10-30 09:52:22 -04:00
|
|
|
if (bt_setup_hw()) {
|
|
|
|
if (bt_enabled) bt_start();
|
|
|
|
return true;
|
2022-10-30 08:51:36 -04:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void update_bt() {
|
2022-10-30 09:52:22 -04:00
|
|
|
if (bt_allow_pairing && millis()-bt_pairing_started >= BT_PAIRING_TIMEOUT) {
|
2022-10-30 08:51:36 -04:00
|
|
|
bt_disable_pairing();
|
|
|
|
}
|
|
|
|
}
|
2022-10-30 09:52:22 -04:00
|
|
|
|
2022-10-30 08:51:36 -04:00
|
|
|
#endif
|