mirror of
https://github.com/liberatedsystems/RNode_Firmware_CE.git
synced 2024-07-02 14:34:13 +02:00
Implemented Bluetooth power and pairing state control
This commit is contained in:
parent
b6a26b3226
commit
8b7e324a5a
51
Bluetooth.h
51
Bluetooth.h
@ -3,7 +3,7 @@
|
|||||||
#include "esp_bt_device.h"
|
#include "esp_bt_device.h"
|
||||||
|
|
||||||
BluetoothSerial SerialBT;
|
BluetoothSerial SerialBT;
|
||||||
#define BT_PAIRING_TIMEOUT 30000
|
#define BT_PAIRING_TIMEOUT 35000
|
||||||
uint32_t bt_pairing_started = 0;
|
uint32_t bt_pairing_started = 0;
|
||||||
|
|
||||||
#define BT_DEV_ADDR_LEN 6
|
#define BT_DEV_ADDR_LEN 6
|
||||||
@ -13,7 +13,7 @@ char bt_dh[BT_DEV_HASH_LEN];
|
|||||||
char bt_devname[11];
|
char bt_devname[11];
|
||||||
|
|
||||||
bool bt_ready = false;
|
bool bt_ready = false;
|
||||||
bool bt_enabled = true;
|
bool bt_enabled = false;
|
||||||
bool bt_allow_pairing = false;
|
bool bt_allow_pairing = false;
|
||||||
|
|
||||||
#if MCU_VARIANT == MCU_ESP32
|
#if MCU_VARIANT == MCU_ESP32
|
||||||
@ -30,13 +30,29 @@ bool bt_allow_pairing = false;
|
|||||||
|
|
||||||
void bt_pairing_complete(boolean success) {
|
void bt_pairing_complete(boolean success) {
|
||||||
if (success) {
|
if (success) {
|
||||||
Serial.println("Paired");
|
// Pass
|
||||||
} else {
|
} else {
|
||||||
Serial.println("Not Paired");
|
// Pass
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void bt_stop() {
|
||||||
|
if (bt_state != BT_STATE_OFF) {
|
||||||
|
SerialBT.end();
|
||||||
|
bt_allow_pairing = false;
|
||||||
|
bt_state = BT_STATE_OFF;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void bt_start() {
|
||||||
|
if (bt_state == BT_STATE_OFF) {
|
||||||
|
SerialBT.begin(bt_devname);
|
||||||
|
bt_state = BT_STATE_ON;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void bt_enable_pairing() {
|
void bt_enable_pairing() {
|
||||||
|
if (bt_state == BT_STATE_OFF) bt_start();
|
||||||
bt_allow_pairing = true;
|
bt_allow_pairing = true;
|
||||||
bt_pairing_started = millis();
|
bt_pairing_started = millis();
|
||||||
bt_state = BT_STATE_PAIRING;
|
bt_state = BT_STATE_PAIRING;
|
||||||
@ -47,30 +63,18 @@ bool bt_allow_pairing = false;
|
|||||||
bt_state = BT_STATE_ON;
|
bt_state = BT_STATE_ON;
|
||||||
}
|
}
|
||||||
|
|
||||||
void bt_disable() {
|
|
||||||
if (bt_state != BT_STATE_OFF) {
|
|
||||||
SerialBT.end();
|
|
||||||
bt_state = BT_STATE_OFF;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void bt_enable() {
|
|
||||||
if (bt_state == BT_STATE_OFF) {
|
|
||||||
SerialBT.begin(bt_devname);
|
|
||||||
bt_state = BT_STATE_ON;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool bt_setup_hw() {
|
bool bt_setup_hw() {
|
||||||
if (!bt_ready) {
|
if (!bt_ready) {
|
||||||
|
if (EEPROM.read(eeprom_addr(ADDR_CONF_BT)) == BT_ENABLE_BYTE) { bt_enabled = true; } else { bt_enabled = false; }
|
||||||
if (btStart()) {
|
if (btStart()) {
|
||||||
if (esp_bluedroid_init() == ESP_OK) {
|
if (esp_bluedroid_init() == ESP_OK) {
|
||||||
if (esp_bluedroid_enable() == ESP_OK) {
|
if (esp_bluedroid_enable() == ESP_OK) {
|
||||||
const uint8_t* bda_ptr = esp_bt_dev_get_address();
|
const uint8_t* bda_ptr = esp_bt_dev_get_address();
|
||||||
char *data = (char*)malloc(BT_DEV_ADDR_LEN);
|
char *data = (char*)malloc(BT_DEV_ADDR_LEN+1);
|
||||||
for (int i = 0; i < BT_DEV_ADDR_LEN; i++) {
|
for (int i = 0; i < BT_DEV_ADDR_LEN; i++) {
|
||||||
data[i] = bda_ptr[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);
|
unsigned char *hash = MD5::make_hash(data, BT_DEV_ADDR_LEN);
|
||||||
memcpy(bt_dh, hash, BT_DEV_HASH_LEN);
|
memcpy(bt_dh, hash, BT_DEV_HASH_LEN);
|
||||||
sprintf(bt_devname, "RNode %02X%02X", bt_dh[14], bt_dh[15]);
|
sprintf(bt_devname, "RNode %02X%02X", bt_dh[14], bt_dh[15]);
|
||||||
@ -91,21 +95,16 @@ bool bt_allow_pairing = false;
|
|||||||
|
|
||||||
bool bt_init() {
|
bool bt_init() {
|
||||||
bt_state = BT_STATE_OFF;
|
bt_state = BT_STATE_OFF;
|
||||||
if (bt_enabled) {
|
|
||||||
if (bt_setup_hw()) {
|
if (bt_setup_hw()) {
|
||||||
// TODO: Read from conf
|
if (bt_enabled) bt_start();
|
||||||
bt_enable();
|
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void update_bt() {
|
void update_bt() {
|
||||||
if (millis()-bt_pairing_started >= BT_PAIRING_TIMEOUT) {
|
if (bt_allow_pairing && millis()-bt_pairing_started >= BT_PAIRING_TIMEOUT) {
|
||||||
bt_disable_pairing();
|
bt_disable_pairing();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -104,6 +104,8 @@ void draw_bt_icon(int px, int py) {
|
|||||||
stat_area.drawBitmap(px, py, bm_bt+2*32, 16, 16, SSD1306_WHITE, SSD1306_BLACK);
|
stat_area.drawBitmap(px, py, bm_bt+2*32, 16, 16, SSD1306_WHITE, SSD1306_BLACK);
|
||||||
} else if (bt_state == BT_STATE_CONNECTED) {
|
} else if (bt_state == BT_STATE_CONNECTED) {
|
||||||
stat_area.drawBitmap(px, py, bm_bt+3*32, 16, 16, SSD1306_WHITE, SSD1306_BLACK);
|
stat_area.drawBitmap(px, py, bm_bt+3*32, 16, 16, SSD1306_WHITE, SSD1306_BLACK);
|
||||||
|
} else {
|
||||||
|
stat_area.drawBitmap(px, py, bm_bt+0*32, 16, 16, SSD1306_WHITE, SSD1306_BLACK);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,7 +47,6 @@
|
|||||||
#define CMD_RESET 0x55
|
#define CMD_RESET 0x55
|
||||||
#define CMD_RESET_BYTE 0xF8
|
#define CMD_RESET_BYTE 0xF8
|
||||||
|
|
||||||
|
|
||||||
#define DETECT_REQ 0x73
|
#define DETECT_REQ 0x73
|
||||||
#define DETECT_RESP 0x46
|
#define DETECT_RESP 0x46
|
||||||
|
|
||||||
|
@ -40,6 +40,9 @@ volatile uint16_t queued_bytes = 0;
|
|||||||
volatile uint16_t queue_cursor = 0;
|
volatile uint16_t queue_cursor = 0;
|
||||||
volatile uint16_t current_packet_start = 0;
|
volatile uint16_t current_packet_start = 0;
|
||||||
volatile bool serial_buffering = false;
|
volatile bool serial_buffering = false;
|
||||||
|
#if HAS_BLUETOOTH
|
||||||
|
bool bt_init_ran = false;
|
||||||
|
#endif
|
||||||
|
|
||||||
char sbuf[128];
|
char sbuf[128];
|
||||||
|
|
||||||
@ -100,10 +103,6 @@ void setup() {
|
|||||||
|
|
||||||
// Validate board health, EEPROM and config
|
// Validate board health, EEPROM and config
|
||||||
validateStatus();
|
validateStatus();
|
||||||
|
|
||||||
#if HAS_BLUETOOTH
|
|
||||||
bt_init();
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void lora_receive() {
|
void lora_receive() {
|
||||||
@ -677,9 +676,11 @@ void serialCallback(uint8_t sbyte) {
|
|||||||
} else if (command == CMD_BT_CTRL) {
|
} else if (command == CMD_BT_CTRL) {
|
||||||
#if HAS_BLUETOOTH
|
#if HAS_BLUETOOTH
|
||||||
if (sbyte == 0x00) {
|
if (sbyte == 0x00) {
|
||||||
bt_disable();
|
bt_stop();
|
||||||
|
bt_conf_save(false);
|
||||||
} else if (sbyte == 0x01) {
|
} else if (sbyte == 0x01) {
|
||||||
bt_enable();
|
bt_start();
|
||||||
|
bt_conf_save(true);
|
||||||
} else if (sbyte == 0x02) {
|
} else if (sbyte == 0x02) {
|
||||||
bt_enable_pairing();
|
bt_enable_pairing();
|
||||||
}
|
}
|
||||||
@ -861,6 +862,11 @@ void loop() {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if HAS_BLUETOOTH
|
#if HAS_BLUETOOTH
|
||||||
|
if (!bt_init_ran && millis() > 1000) {
|
||||||
|
bt_init();
|
||||||
|
bt_init_ran = true;
|
||||||
|
}
|
||||||
|
|
||||||
if (bt_ready) update_bt();
|
if (bt_ready) update_bt();
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
3
ROM.h
3
ROM.h
@ -42,8 +42,11 @@
|
|||||||
#define ADDR_CONF_FREQ 0xA3
|
#define ADDR_CONF_FREQ 0xA3
|
||||||
#define ADDR_CONF_OK 0xA7
|
#define ADDR_CONF_OK 0xA7
|
||||||
|
|
||||||
|
#define ADDR_CONF_BT 0xB0
|
||||||
|
|
||||||
#define INFO_LOCK_BYTE 0x73
|
#define INFO_LOCK_BYTE 0x73
|
||||||
#define CONF_OK_BYTE 0x73
|
#define CONF_OK_BYTE 0x73
|
||||||
|
#define BT_ENABLE_BYTE 0x73
|
||||||
|
|
||||||
#define EEPROM_RESERVED 200
|
#define EEPROM_RESERVED 200
|
||||||
#endif
|
#endif
|
@ -915,6 +915,14 @@ bool eeprom_checksum_valid() {
|
|||||||
return checksum_valid;
|
return checksum_valid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void bt_conf_save(bool is_enabled) {
|
||||||
|
if (is_enabled) {
|
||||||
|
eeprom_update(eeprom_addr(ADDR_CONF_BT), BT_ENABLE_BYTE);
|
||||||
|
} else {
|
||||||
|
eeprom_update(eeprom_addr(ADDR_CONF_BT), 0x00);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool eeprom_have_conf() {
|
bool eeprom_have_conf() {
|
||||||
if (EEPROM.read(eeprom_addr(ADDR_CONF_OK)) == CONF_OK_BYTE) {
|
if (EEPROM.read(eeprom_addr(ADDR_CONF_OK)) == CONF_OK_BYTE) {
|
||||||
return true;
|
return true;
|
||||||
|
Loading…
Reference in New Issue
Block a user