mirror of
https://github.com/meshtastic/firmware.git
synced 2025-04-27 10:21:40 +00:00
Another try to get the code format correct.
This commit is contained in:
parent
ca8d2204ba
commit
c4c85777d0
1097
src/mqtt/MQTT.cpp
1097
src/mqtt/MQTT.cpp
File diff suppressed because it is too large
Load Diff
@ -32,23 +32,23 @@ static uint16_t connectionHandle;
|
|||||||
|
|
||||||
class BluetoothPhoneAPI : public PhoneAPI
|
class BluetoothPhoneAPI : public PhoneAPI
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Subclasses can use this as a hook to provide custom notifications for their transport (i.e. bluetooth notifies)
|
* Subclasses can use this as a hook to provide custom notifications for their transport (i.e. bluetooth notifies)
|
||||||
*/
|
*/
|
||||||
virtual void onNowHasData(uint32_t fromRadioNum) override
|
virtual void onNowHasData(uint32_t fromRadioNum) override
|
||||||
{
|
{
|
||||||
PhoneAPI::onNowHasData(fromRadioNum);
|
PhoneAPI::onNowHasData(fromRadioNum);
|
||||||
|
|
||||||
LOG_INFO("BLE notify fromNum\n");
|
LOG_INFO("BLE notify fromNum\n");
|
||||||
fromNum.notify32(fromRadioNum);
|
fromNum.notify32(fromRadioNum);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Check the current underlying physical link to see if the client is currently connected
|
/// Check the current underlying physical link to see if the client is currently connected
|
||||||
virtual bool checkIsConnected() override
|
virtual bool checkIsConnected() override
|
||||||
{
|
{
|
||||||
BLEConnection *connection = Bluefruit.Connection(connectionHandle);
|
BLEConnection *connection = Bluefruit.Connection(connectionHandle);
|
||||||
return connection->connected();
|
return connection->connected();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
static BluetoothPhoneAPI *bluetoothPhoneAPI;
|
static BluetoothPhoneAPI *bluetoothPhoneAPI;
|
||||||
@ -56,12 +56,12 @@ static BluetoothPhoneAPI *bluetoothPhoneAPI;
|
|||||||
void onConnect(uint16_t conn_handle)
|
void onConnect(uint16_t conn_handle)
|
||||||
{
|
{
|
||||||
|
|
||||||
// Get the reference to current connection
|
// Get the reference to current connection
|
||||||
BLEConnection *connection = Bluefruit.Connection(conn_handle);
|
BLEConnection *connection = Bluefruit.Connection(conn_handle);
|
||||||
connectionHandle = conn_handle;
|
connectionHandle = conn_handle;
|
||||||
char central_name[32] = {0};
|
char central_name[32] = {0};
|
||||||
connection->getPeerName(central_name, sizeof(central_name));
|
connection->getPeerName(central_name, sizeof(central_name));
|
||||||
LOG_INFO("BLE Connected to %s\n", central_name);
|
LOG_INFO("BLE Connected to %s\n", central_name);
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Callback invoked when a connection is dropped
|
* Callback invoked when a connection is dropped
|
||||||
@ -70,261 +70,248 @@ void onConnect(uint16_t conn_handle)
|
|||||||
*/
|
*/
|
||||||
void onDisconnect(uint16_t conn_handle, uint8_t reason)
|
void onDisconnect(uint16_t conn_handle, uint8_t reason)
|
||||||
{
|
{
|
||||||
// FIXME - we currently assume only one active connection
|
// FIXME - we currently assume only one active connection
|
||||||
LOG_INFO("BLE Disconnected, reason = 0x%x\n", reason);
|
LOG_INFO("BLE Disconnected, reason = 0x%x\n", reason);
|
||||||
}
|
}
|
||||||
void onCccd(uint16_t conn_hdl, BLECharacteristic *chr, uint16_t cccd_value)
|
void onCccd(uint16_t conn_hdl, BLECharacteristic *chr, uint16_t cccd_value)
|
||||||
{
|
{
|
||||||
// Display the raw request packet
|
// Display the raw request packet
|
||||||
LOG_INFO("CCCD Updated: %u\n", cccd_value);
|
LOG_INFO("CCCD Updated: %u\n", cccd_value);
|
||||||
// Check the characteristic this CCCD update is associated with in case
|
// Check the characteristic this CCCD update is associated with in case
|
||||||
// this handler is used for multiple CCCD records.
|
// this handler is used for multiple CCCD records.
|
||||||
|
|
||||||
// According to the GATT spec: cccd value = 0x0001 means notifications are enabled
|
// According to the GATT spec: cccd value = 0x0001 means notifications are enabled
|
||||||
// and cccd value = 0x0002 means indications are enabled
|
// and cccd value = 0x0002 means indications are enabled
|
||||||
|
|
||||||
if (chr->uuid == fromNum.uuid || chr->uuid == logRadio.uuid)
|
if (chr->uuid == fromNum.uuid || chr->uuid == logRadio.uuid) {
|
||||||
{
|
auto result = cccd_value == 2 ? chr->indicateEnabled(conn_hdl) : chr->notifyEnabled(conn_hdl);
|
||||||
auto result = cccd_value == 2 ? chr->indicateEnabled(conn_hdl) : chr->notifyEnabled(conn_hdl);
|
if (result) {
|
||||||
if (result)
|
LOG_INFO("Notify/Indicate enabled\n");
|
||||||
{
|
} else {
|
||||||
LOG_INFO("Notify/Indicate enabled\n");
|
LOG_INFO("Notify/Indicate disabled\n");
|
||||||
}
|
}
|
||||||
else
|
}
|
||||||
{
|
|
||||||
LOG_INFO("Notify/Indicate disabled\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
void startAdv(void)
|
void startAdv(void)
|
||||||
{
|
{
|
||||||
// Advertising packet
|
// Advertising packet
|
||||||
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
|
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
|
||||||
// IncludeService UUID
|
// IncludeService UUID
|
||||||
// Bluefruit.ScanResponse.addService(meshBleService);
|
// Bluefruit.ScanResponse.addService(meshBleService);
|
||||||
Bluefruit.ScanResponse.addTxPower();
|
Bluefruit.ScanResponse.addTxPower();
|
||||||
Bluefruit.ScanResponse.addName();
|
Bluefruit.ScanResponse.addName();
|
||||||
// Include Name
|
// Include Name
|
||||||
// Bluefruit.Advertising.addName();
|
// Bluefruit.Advertising.addName();
|
||||||
Bluefruit.Advertising.addService(meshBleService);
|
Bluefruit.Advertising.addService(meshBleService);
|
||||||
/* Start Advertising
|
/* Start Advertising
|
||||||
* - Enable auto advertising if disconnected
|
* - Enable auto advertising if disconnected
|
||||||
* - Interval: fast mode = 20 ms, slow mode = 152.5 ms
|
* - Interval: fast mode = 20 ms, slow mode = 152.5 ms
|
||||||
* - Timeout for fast mode is 30 seconds
|
* - Timeout for fast mode is 30 seconds
|
||||||
* - Start(timeout) with timeout = 0 will advertise forever (until connected)
|
* - Start(timeout) with timeout = 0 will advertise forever (until connected)
|
||||||
*
|
*
|
||||||
* For recommended advertising interval
|
* For recommended advertising interval
|
||||||
* https://developer.apple.com/library/content/qa/qa1931/_index.html
|
* https://developer.apple.com/library/content/qa/qa1931/_index.html
|
||||||
*/
|
*/
|
||||||
Bluefruit.Advertising.restartOnDisconnect(true);
|
Bluefruit.Advertising.restartOnDisconnect(true);
|
||||||
Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
|
Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
|
||||||
Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
|
Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
|
||||||
Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds. FIXME, we should stop advertising after X
|
Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds. FIXME, we should stop advertising after X
|
||||||
}
|
}
|
||||||
// Just ack that the caller is allowed to read
|
// Just ack that the caller is allowed to read
|
||||||
static void authorizeRead(uint16_t conn_hdl)
|
static void authorizeRead(uint16_t conn_hdl)
|
||||||
{
|
{
|
||||||
ble_gatts_rw_authorize_reply_params_t reply = {.type = BLE_GATTS_AUTHORIZE_TYPE_READ};
|
ble_gatts_rw_authorize_reply_params_t reply = {.type = BLE_GATTS_AUTHORIZE_TYPE_READ};
|
||||||
reply.params.write.gatt_status = BLE_GATT_STATUS_SUCCESS;
|
reply.params.write.gatt_status = BLE_GATT_STATUS_SUCCESS;
|
||||||
sd_ble_gatts_rw_authorize_reply(conn_hdl, &reply);
|
sd_ble_gatts_rw_authorize_reply(conn_hdl, &reply);
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* client is starting read, pull the bytes from our API class
|
* client is starting read, pull the bytes from our API class
|
||||||
*/
|
*/
|
||||||
void onFromRadioAuthorize(uint16_t conn_hdl, BLECharacteristic *chr, ble_gatts_evt_read_t *request)
|
void onFromRadioAuthorize(uint16_t conn_hdl, BLECharacteristic *chr, ble_gatts_evt_read_t *request)
|
||||||
{
|
{
|
||||||
if (request->offset == 0)
|
if (request->offset == 0) {
|
||||||
{
|
// If the read is long, we will get multiple authorize invocations - we only populate data on the first
|
||||||
// If the read is long, we will get multiple authorize invocations - we only populate data on the first
|
size_t numBytes = bluetoothPhoneAPI->getFromRadio(fromRadioBytes);
|
||||||
size_t numBytes = bluetoothPhoneAPI->getFromRadio(fromRadioBytes);
|
// Someone is going to read our value as soon as this callback returns. So fill it with the next message in the queue
|
||||||
// Someone is going to read our value as soon as this callback returns. So fill it with the next message in the queue
|
// or make empty if the queue is empty
|
||||||
// or make empty if the queue is empty
|
fromRadio.write(fromRadioBytes, numBytes);
|
||||||
fromRadio.write(fromRadioBytes, numBytes);
|
} else {
|
||||||
}
|
// LOG_INFO("Ignoring successor read\n");
|
||||||
else
|
}
|
||||||
{
|
authorizeRead(conn_hdl);
|
||||||
// LOG_INFO("Ignoring successor read\n");
|
|
||||||
}
|
|
||||||
authorizeRead(conn_hdl);
|
|
||||||
}
|
}
|
||||||
void onToRadioWrite(uint16_t conn_hdl, BLECharacteristic *chr, uint8_t *data, uint16_t len)
|
void onToRadioWrite(uint16_t conn_hdl, BLECharacteristic *chr, uint8_t *data, uint16_t len)
|
||||||
{
|
{
|
||||||
LOG_INFO("toRadioWriteCb data %p, len %u\n", data, len);
|
LOG_INFO("toRadioWriteCb data %p, len %u\n", data, len);
|
||||||
bluetoothPhoneAPI->handleToRadio(data, len);
|
bluetoothPhoneAPI->handleToRadio(data, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
void setupMeshService(void)
|
void setupMeshService(void)
|
||||||
{
|
{
|
||||||
bluetoothPhoneAPI = new BluetoothPhoneAPI();
|
bluetoothPhoneAPI = new BluetoothPhoneAPI();
|
||||||
meshBleService.begin();
|
meshBleService.begin();
|
||||||
// Note: You must call .begin() on the BLEService before calling .begin() on
|
// Note: You must call .begin() on the BLEService before calling .begin() on
|
||||||
// any characteristic(s) within that service definition.. Calling .begin() on
|
// any characteristic(s) within that service definition.. Calling .begin() on
|
||||||
// a BLECharacteristic will cause it to be added to the last BLEService that
|
// a BLECharacteristic will cause it to be added to the last BLEService that
|
||||||
// was 'begin()'ed!
|
// was 'begin()'ed!
|
||||||
auto secMode =
|
auto secMode =
|
||||||
config.bluetooth.mode == meshtastic_Config_BluetoothConfig_PairingMode_NO_PIN ? SECMODE_OPEN : SECMODE_ENC_NO_MITM;
|
config.bluetooth.mode == meshtastic_Config_BluetoothConfig_PairingMode_NO_PIN ? SECMODE_OPEN : SECMODE_ENC_NO_MITM;
|
||||||
fromNum.setProperties(CHR_PROPS_NOTIFY | CHR_PROPS_READ);
|
fromNum.setProperties(CHR_PROPS_NOTIFY | CHR_PROPS_READ);
|
||||||
fromNum.setPermission(secMode, SECMODE_NO_ACCESS); // FIXME, secure this!!!
|
fromNum.setPermission(secMode, SECMODE_NO_ACCESS); // FIXME, secure this!!!
|
||||||
fromNum.setFixedLen(
|
fromNum.setFixedLen(
|
||||||
0); // Variable len (either 0 or 4) FIXME consider changing protocol so it is fixed 4 byte len, where 0 means empty
|
0); // Variable len (either 0 or 4) FIXME consider changing protocol so it is fixed 4 byte len, where 0 means empty
|
||||||
fromNum.setMaxLen(4);
|
fromNum.setMaxLen(4);
|
||||||
fromNum.setCccdWriteCallback(onCccd); // Optionally capture CCCD updates
|
fromNum.setCccdWriteCallback(onCccd); // Optionally capture CCCD updates
|
||||||
// We don't yet need to hook the fromNum auth callback
|
// We don't yet need to hook the fromNum auth callback
|
||||||
// fromNum.setReadAuthorizeCallback(fromNumAuthorizeCb);
|
// fromNum.setReadAuthorizeCallback(fromNumAuthorizeCb);
|
||||||
fromNum.write32(0); // Provide default fromNum of 0
|
fromNum.write32(0); // Provide default fromNum of 0
|
||||||
fromNum.begin();
|
fromNum.begin();
|
||||||
|
|
||||||
fromRadio.setProperties(CHR_PROPS_READ);
|
fromRadio.setProperties(CHR_PROPS_READ);
|
||||||
fromRadio.setPermission(secMode, SECMODE_NO_ACCESS);
|
fromRadio.setPermission(secMode, SECMODE_NO_ACCESS);
|
||||||
fromRadio.setMaxLen(sizeof(fromRadioBytes));
|
fromRadio.setMaxLen(sizeof(fromRadioBytes));
|
||||||
fromRadio.setReadAuthorizeCallback(
|
fromRadio.setReadAuthorizeCallback(
|
||||||
onFromRadioAuthorize,
|
onFromRadioAuthorize,
|
||||||
false); // We don't call this callback via the adafruit queue, because we can safely run in the BLE context
|
false); // We don't call this callback via the adafruit queue, because we can safely run in the BLE context
|
||||||
fromRadio.setBuffer(fromRadioBytes, sizeof(fromRadioBytes)); // we preallocate our fromradio buffer so we won't waste space
|
fromRadio.setBuffer(fromRadioBytes, sizeof(fromRadioBytes)); // we preallocate our fromradio buffer so we won't waste space
|
||||||
// for two copies
|
// for two copies
|
||||||
fromRadio.begin();
|
fromRadio.begin();
|
||||||
|
|
||||||
toRadio.setProperties(CHR_PROPS_WRITE);
|
toRadio.setProperties(CHR_PROPS_WRITE);
|
||||||
toRadio.setPermission(secMode, secMode); // FIXME secure this!
|
toRadio.setPermission(secMode, secMode); // FIXME secure this!
|
||||||
toRadio.setFixedLen(0);
|
toRadio.setFixedLen(0);
|
||||||
toRadio.setMaxLen(512);
|
toRadio.setMaxLen(512);
|
||||||
toRadio.setBuffer(toRadioBytes, sizeof(toRadioBytes));
|
toRadio.setBuffer(toRadioBytes, sizeof(toRadioBytes));
|
||||||
// We don't call this callback via the adafruit queue, because we can safely run in the BLE context
|
// We don't call this callback via the adafruit queue, because we can safely run in the BLE context
|
||||||
toRadio.setWriteCallback(onToRadioWrite, false);
|
toRadio.setWriteCallback(onToRadioWrite, false);
|
||||||
toRadio.begin();
|
toRadio.begin();
|
||||||
|
|
||||||
logRadio.setProperties(CHR_PROPS_INDICATE | CHR_PROPS_NOTIFY | CHR_PROPS_READ);
|
logRadio.setProperties(CHR_PROPS_INDICATE | CHR_PROPS_NOTIFY | CHR_PROPS_READ);
|
||||||
logRadio.setPermission(secMode, SECMODE_NO_ACCESS);
|
logRadio.setPermission(secMode, SECMODE_NO_ACCESS);
|
||||||
logRadio.setMaxLen(512);
|
logRadio.setMaxLen(512);
|
||||||
logRadio.setCccdWriteCallback(onCccd);
|
logRadio.setCccdWriteCallback(onCccd);
|
||||||
logRadio.write32(0);
|
logRadio.write32(0);
|
||||||
logRadio.begin();
|
logRadio.begin();
|
||||||
}
|
}
|
||||||
static uint32_t configuredPasskey;
|
static uint32_t configuredPasskey;
|
||||||
void NRF52Bluetooth::shutdown()
|
void NRF52Bluetooth::shutdown()
|
||||||
{
|
{
|
||||||
// Shutdown bluetooth for minimum power draw
|
// Shutdown bluetooth for minimum power draw
|
||||||
LOG_INFO("Disable NRF52 bluetooth\n");
|
LOG_INFO("Disable NRF52 bluetooth\n");
|
||||||
uint8_t connection_num = Bluefruit.connected();
|
uint8_t connection_num = Bluefruit.connected();
|
||||||
if (connection_num)
|
if (connection_num) {
|
||||||
{
|
for (uint8_t i = 0; i < connection_num; i++) {
|
||||||
for (uint8_t i = 0; i < connection_num; i++)
|
LOG_INFO("NRF52 bluetooth disconnecting handle %d\n", i);
|
||||||
{
|
Bluefruit.disconnect(i);
|
||||||
LOG_INFO("NRF52 bluetooth disconnecting handle %d\n", i);
|
}
|
||||||
Bluefruit.disconnect(i);
|
delay(100); // wait for ondisconnect;
|
||||||
}
|
}
|
||||||
delay(100); // wait for ondisconnect;
|
Bluefruit.Advertising.stop();
|
||||||
}
|
|
||||||
Bluefruit.Advertising.stop();
|
|
||||||
}
|
}
|
||||||
void NRF52Bluetooth::startDisabled()
|
void NRF52Bluetooth::startDisabled()
|
||||||
{
|
{
|
||||||
// Setup Bluetooth
|
// Setup Bluetooth
|
||||||
nrf52Bluetooth->setup();
|
nrf52Bluetooth->setup();
|
||||||
// Shutdown bluetooth for minimum power draw
|
// Shutdown bluetooth for minimum power draw
|
||||||
Bluefruit.Advertising.stop();
|
Bluefruit.Advertising.stop();
|
||||||
Bluefruit.setTxPower(-40); // Minimum power
|
Bluefruit.setTxPower(-40); // Minimum power
|
||||||
LOG_INFO("Disabling NRF52 Bluetooth. (Workaround: tx power min, advertising stopped)\n");
|
LOG_INFO("Disabling NRF52 Bluetooth. (Workaround: tx power min, advertising stopped)\n");
|
||||||
}
|
}
|
||||||
bool NRF52Bluetooth::isConnected()
|
bool NRF52Bluetooth::isConnected()
|
||||||
{
|
{
|
||||||
return Bluefruit.connected(connectionHandle);
|
return Bluefruit.connected(connectionHandle);
|
||||||
}
|
}
|
||||||
int NRF52Bluetooth::getRssi()
|
int NRF52Bluetooth::getRssi()
|
||||||
{
|
{
|
||||||
return 0; // FIXME figure out where to source this
|
return 0; // FIXME figure out where to source this
|
||||||
}
|
}
|
||||||
void NRF52Bluetooth::setup()
|
void NRF52Bluetooth::setup()
|
||||||
{
|
{
|
||||||
// Initialise the Bluefruit module
|
// Initialise the Bluefruit module
|
||||||
LOG_INFO("Initialize the Bluefruit nRF52 module\n");
|
LOG_INFO("Initialize the Bluefruit nRF52 module\n");
|
||||||
Bluefruit.autoConnLed(false);
|
Bluefruit.autoConnLed(false);
|
||||||
Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);
|
Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);
|
||||||
Bluefruit.begin();
|
Bluefruit.begin();
|
||||||
// Clear existing data.
|
// Clear existing data.
|
||||||
Bluefruit.Advertising.stop();
|
Bluefruit.Advertising.stop();
|
||||||
Bluefruit.Advertising.clearData();
|
Bluefruit.Advertising.clearData();
|
||||||
Bluefruit.ScanResponse.clearData();
|
Bluefruit.ScanResponse.clearData();
|
||||||
if (config.bluetooth.mode != meshtastic_Config_BluetoothConfig_PairingMode_NO_PIN)
|
if (config.bluetooth.mode != meshtastic_Config_BluetoothConfig_PairingMode_NO_PIN) {
|
||||||
{
|
configuredPasskey = config.bluetooth.mode == meshtastic_Config_BluetoothConfig_PairingMode_FIXED_PIN
|
||||||
configuredPasskey = config.bluetooth.mode == meshtastic_Config_BluetoothConfig_PairingMode_FIXED_PIN
|
? config.bluetooth.fixed_pin
|
||||||
? config.bluetooth.fixed_pin
|
: random(100000, 999999);
|
||||||
: random(100000, 999999);
|
auto pinString = std::to_string(configuredPasskey);
|
||||||
auto pinString = std::to_string(configuredPasskey);
|
LOG_INFO("Bluetooth pin set to '%i'\n", configuredPasskey);
|
||||||
LOG_INFO("Bluetooth pin set to '%i'\n", configuredPasskey);
|
Bluefruit.Security.setPIN(pinString.c_str());
|
||||||
Bluefruit.Security.setPIN(pinString.c_str());
|
Bluefruit.Security.setIOCaps(true, false, false);
|
||||||
Bluefruit.Security.setIOCaps(true, false, false);
|
Bluefruit.Security.setPairPasskeyCallback(NRF52Bluetooth::onPairingPasskey);
|
||||||
Bluefruit.Security.setPairPasskeyCallback(NRF52Bluetooth::onPairingPasskey);
|
Bluefruit.Security.setPairCompleteCallback(NRF52Bluetooth::onPairingCompleted);
|
||||||
Bluefruit.Security.setPairCompleteCallback(NRF52Bluetooth::onPairingCompleted);
|
Bluefruit.Security.setSecuredCallback(NRF52Bluetooth::onConnectionSecured);
|
||||||
Bluefruit.Security.setSecuredCallback(NRF52Bluetooth::onConnectionSecured);
|
meshBleService.setPermission(SECMODE_ENC_WITH_MITM, SECMODE_ENC_WITH_MITM);
|
||||||
meshBleService.setPermission(SECMODE_ENC_WITH_MITM, SECMODE_ENC_WITH_MITM);
|
} else {
|
||||||
}
|
Bluefruit.Security.setIOCaps(false, false, false);
|
||||||
else
|
meshBleService.setPermission(SECMODE_OPEN, SECMODE_OPEN);
|
||||||
{
|
}
|
||||||
Bluefruit.Security.setIOCaps(false, false, false);
|
// Set the advertised device name (keep it short!)
|
||||||
meshBleService.setPermission(SECMODE_OPEN, SECMODE_OPEN);
|
Bluefruit.setName(getDeviceName());
|
||||||
}
|
// Set the connect/disconnect callback handlers
|
||||||
// Set the advertised device name (keep it short!)
|
Bluefruit.Periph.setConnectCallback(onConnect);
|
||||||
Bluefruit.setName(getDeviceName());
|
Bluefruit.Periph.setDisconnectCallback(onDisconnect);
|
||||||
// Set the connect/disconnect callback handlers
|
|
||||||
Bluefruit.Periph.setConnectCallback(onConnect);
|
|
||||||
Bluefruit.Periph.setDisconnectCallback(onDisconnect);
|
|
||||||
#ifndef BLE_DFU_SECURE
|
#ifndef BLE_DFU_SECURE
|
||||||
bledfu.setPermission(SECMODE_ENC_WITH_MITM, SECMODE_ENC_WITH_MITM);
|
bledfu.setPermission(SECMODE_ENC_WITH_MITM, SECMODE_ENC_WITH_MITM);
|
||||||
bledfu.begin(); // Install the DFU helper
|
bledfu.begin(); // Install the DFU helper
|
||||||
#else
|
#else
|
||||||
bledfusecure.setPermission(SECMODE_ENC_WITH_MITM, SECMODE_ENC_WITH_MITM); // add by WayenWeng
|
bledfusecure.setPermission(SECMODE_ENC_WITH_MITM, SECMODE_ENC_WITH_MITM); // add by WayenWeng
|
||||||
bledfusecure.begin(); // Install the DFU helper
|
bledfusecure.begin(); // Install the DFU helper
|
||||||
#endif
|
#endif
|
||||||
// Configure and Start the Device Information Service
|
// Configure and Start the Device Information Service
|
||||||
LOG_INFO("Configuring the Device Information Service\n");
|
LOG_INFO("Configuring the Device Information Service\n");
|
||||||
bledis.setModel(optstr(HW_VERSION));
|
bledis.setModel(optstr(HW_VERSION));
|
||||||
bledis.setFirmwareRev(optstr(APP_VERSION));
|
bledis.setFirmwareRev(optstr(APP_VERSION));
|
||||||
bledis.begin();
|
bledis.begin();
|
||||||
// Start the BLE Battery Service and set it to 100%
|
// Start the BLE Battery Service and set it to 100%
|
||||||
LOG_INFO("Configuring the Battery Service\n");
|
LOG_INFO("Configuring the Battery Service\n");
|
||||||
blebas.begin();
|
blebas.begin();
|
||||||
blebas.write(0); // Unknown battery level for now
|
blebas.write(0); // Unknown battery level for now
|
||||||
// Setup the Heart Rate Monitor service using
|
// Setup the Heart Rate Monitor service using
|
||||||
// BLEService and BLECharacteristic classes
|
// BLEService and BLECharacteristic classes
|
||||||
LOG_INFO("Configuring the Mesh bluetooth service\n");
|
LOG_INFO("Configuring the Mesh bluetooth service\n");
|
||||||
setupMeshService();
|
setupMeshService();
|
||||||
// Setup the advertising packet(s)
|
// Setup the advertising packet(s)
|
||||||
LOG_INFO("Setting up the advertising payload(s)\n");
|
LOG_INFO("Setting up the advertising payload(s)\n");
|
||||||
startAdv();
|
startAdv();
|
||||||
LOG_INFO("Advertising\n");
|
LOG_INFO("Advertising\n");
|
||||||
}
|
}
|
||||||
void NRF52Bluetooth::resumeAdvertising()
|
void NRF52Bluetooth::resumeAdvertising()
|
||||||
{
|
{
|
||||||
Bluefruit.Advertising.restartOnDisconnect(true);
|
Bluefruit.Advertising.restartOnDisconnect(true);
|
||||||
Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
|
Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
|
||||||
Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
|
Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
|
||||||
Bluefruit.Advertising.start(0);
|
Bluefruit.Advertising.start(0);
|
||||||
}
|
}
|
||||||
/// Given a level between 0-100, update the BLE attribute
|
/// Given a level between 0-100, update the BLE attribute
|
||||||
void updateBatteryLevel(uint8_t level)
|
void updateBatteryLevel(uint8_t level)
|
||||||
{
|
{
|
||||||
blebas.write(level);
|
blebas.write(level);
|
||||||
}
|
}
|
||||||
void NRF52Bluetooth::clearBonds()
|
void NRF52Bluetooth::clearBonds()
|
||||||
{
|
{
|
||||||
LOG_INFO("Clearing bluetooth bonds!\n");
|
LOG_INFO("Clearing bluetooth bonds!\n");
|
||||||
bond_print_list(BLE_GAP_ROLE_PERIPH);
|
bond_print_list(BLE_GAP_ROLE_PERIPH);
|
||||||
bond_print_list(BLE_GAP_ROLE_CENTRAL);
|
bond_print_list(BLE_GAP_ROLE_CENTRAL);
|
||||||
Bluefruit.Periph.clearBonds();
|
Bluefruit.Periph.clearBonds();
|
||||||
Bluefruit.Central.clearBonds();
|
Bluefruit.Central.clearBonds();
|
||||||
}
|
}
|
||||||
void NRF52Bluetooth::onConnectionSecured(uint16_t conn_handle)
|
void NRF52Bluetooth::onConnectionSecured(uint16_t conn_handle)
|
||||||
{
|
{
|
||||||
LOG_INFO("BLE connection secured\n");
|
LOG_INFO("BLE connection secured\n");
|
||||||
}
|
}
|
||||||
bool NRF52Bluetooth::onPairingPasskey(uint16_t conn_handle, uint8_t const passkey[6], bool match_request)
|
bool NRF52Bluetooth::onPairingPasskey(uint16_t conn_handle, uint8_t const passkey[6], bool match_request)
|
||||||
{
|
{
|
||||||
LOG_INFO("BLE pairing process started with passkey %.3s %.3s\n", passkey, passkey + 3);
|
LOG_INFO("BLE pairing process started with passkey %.3s %.3s\n", passkey, passkey + 3);
|
||||||
powerFSM.trigger(EVENT_BLUETOOTH_PAIR);
|
powerFSM.trigger(EVENT_BLUETOOTH_PAIR);
|
||||||
#if !defined(MESHTASTIC_EXCLUDE_SCREEN)
|
#if !defined(MESHTASTIC_EXCLUDE_SCREEN)
|
||||||
screen->startAlert([](OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y) -> void
|
screen->startAlert([](OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y) -> void {
|
||||||
{
|
|
||||||
char btPIN[16] = "888888";
|
char btPIN[16] = "888888";
|
||||||
snprintf(btPIN, sizeof(btPIN), "%06u", configuredPasskey);
|
snprintf(btPIN, sizeof(btPIN), "%06u", configuredPasskey);
|
||||||
int x_offset = display->width() / 2;
|
int x_offset = display->width() / 2;
|
||||||
@ -347,35 +334,34 @@ bool NRF52Bluetooth::onPairingPasskey(uint16_t conn_handle, uint8_t const passke
|
|||||||
String deviceName = "Name: ";
|
String deviceName = "Name: ";
|
||||||
deviceName.concat(getDeviceName());
|
deviceName.concat(getDeviceName());
|
||||||
y_offset = display->height() == 64 ? y_offset + FONT_HEIGHT_LARGE - 6 : y_offset + FONT_HEIGHT_LARGE + 5;
|
y_offset = display->height() == 64 ? y_offset + FONT_HEIGHT_LARGE - 6 : y_offset + FONT_HEIGHT_LARGE + 5;
|
||||||
display->drawString(x_offset + x, y_offset + y, deviceName); });
|
display->drawString(x_offset + x, y_offset + y, deviceName);
|
||||||
|
});
|
||||||
#endif
|
#endif
|
||||||
if (match_request)
|
if (match_request) {
|
||||||
{
|
uint32_t start_time = millis();
|
||||||
uint32_t start_time = millis();
|
while (millis() < start_time + 30000) {
|
||||||
while (millis() < start_time + 30000)
|
if (!Bluefruit.connected(conn_handle))
|
||||||
{
|
break;
|
||||||
if (!Bluefruit.connected(conn_handle))
|
}
|
||||||
break;
|
}
|
||||||
}
|
LOG_INFO("BLE passkey pairing: match_request=%i\n", match_request);
|
||||||
}
|
return true;
|
||||||
LOG_INFO("BLE passkey pairing: match_request=%i\n", match_request);
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
void NRF52Bluetooth::onPairingCompleted(uint16_t conn_handle, uint8_t auth_status)
|
void NRF52Bluetooth::onPairingCompleted(uint16_t conn_handle, uint8_t auth_status)
|
||||||
{
|
{
|
||||||
if (auth_status == BLE_GAP_SEC_STATUS_SUCCESS)
|
if (auth_status == BLE_GAP_SEC_STATUS_SUCCESS)
|
||||||
LOG_INFO("BLE pairing success\n");
|
LOG_INFO("BLE pairing success\n");
|
||||||
else
|
else
|
||||||
LOG_INFO("BLE pairing failed\n");
|
LOG_INFO("BLE pairing failed\n");
|
||||||
screen->endAlert();
|
screen->endAlert();
|
||||||
}
|
}
|
||||||
|
|
||||||
void NRF52Bluetooth::sendLog(const uint8_t *logMessage, size_t length)
|
void NRF52Bluetooth::sendLog(const uint8_t *logMessage, size_t length)
|
||||||
{
|
{
|
||||||
if (!isConnected() || length > 512)
|
if (!isConnected() || length > 512)
|
||||||
return;
|
return;
|
||||||
if (logRadio.indicateEnabled())
|
if (logRadio.indicateEnabled())
|
||||||
logRadio.indicate(logMessage, (uint16_t)length);
|
logRadio.indicate(logMessage, (uint16_t)length);
|
||||||
else
|
else
|
||||||
logRadio.notify(logMessage, (uint16_t)length);
|
logRadio.notify(logMessage, (uint16_t)length);
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user