From b028af0d82ac9623dc1cef7d5f896dadd4551085 Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Tue, 16 Aug 2022 20:42:43 -0500 Subject: [PATCH] Bluetooth modes (#1633) * Formatting and comments * Esp32 bluetooth modes * Comment --- platformio.ini | 2 +- src/mesh/NodeDB.cpp | 1 + src/platform/esp32/ESP32Bluetooth.cpp | 21 ++++++++++++++------- src/platform/nrf52/NRF52Bluetooth.cpp | 21 +++++---------------- 4 files changed, 21 insertions(+), 24 deletions(-) diff --git a/platformio.ini b/platformio.ini index f06c8cff3..7cb590823 100644 --- a/platformio.ini +++ b/platformio.ini @@ -103,7 +103,7 @@ debug_init_break = tbreak setup build_flags = ${arduino_base.build_flags} -Wall -Wextra -Isrc/platform/esp32 -lnimble -std=c++11 -DLOG_LOCAL_LEVEL=ESP_LOG_DEBUG -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG -DMYNEWT_VAL_BLE_HS_LOG_LVL=LOG_LEVEL_CRITICAL - -DAXP_DEBUG_PORT=Serial -DUSE_NEW_ESP32_BLUETOOTH -DCONFIG_BT_NIMBLE_ENABLED -DCONFIG_NIMBLE_CPP_LOG_LEVEL=1 + -DAXP_DEBUG_PORT=Serial -DUSE_NEW_ESP32_BLUETOOTH -DCONFIG_BT_NIMBLE_ENABLED -DCONFIG_NIMBLE_CPP_LOG_LEVEL=1 -DCONFIG_BT_NIMBLE_MAX_CCCDS=20 lib_deps = ${arduino_base.lib_deps} ${networking_base.lib_deps} diff --git a/src/mesh/NodeDB.cpp b/src/mesh/NodeDB.cpp index 01ddf2245..8145fe1e8 100644 --- a/src/mesh/NodeDB.cpp +++ b/src/mesh/NodeDB.cpp @@ -159,6 +159,7 @@ void NodeDB::installDefaultConfig() // FIXME: Default to bluetooth capability of platform as default config.bluetooth.enabled = true; config.bluetooth.fixed_pin = defaultBLEPin; + config.bluetooth.mode = screen_found ? Config_BluetoothConfig_PairingMode_RandomPin : Config_BluetoothConfig_PairingMode_FixedPin; // for backward compat, default position flags are ALT+MSL config.position.position_flags = (Config_PositionConfig_PositionFlags_POS_ALTITUDE | Config_PositionConfig_PositionFlags_POS_ALT_MSL); } diff --git a/src/platform/esp32/ESP32Bluetooth.cpp b/src/platform/esp32/ESP32Bluetooth.cpp index 0a6a21cef..27c6c76d4 100644 --- a/src/platform/esp32/ESP32Bluetooth.cpp +++ b/src/platform/esp32/ESP32Bluetooth.cpp @@ -72,13 +72,13 @@ class ESP32BluetoothFromRadioCallback : public NimBLECharacteristicCallbacks class ESP32BluetoothServerCallback : public NimBLEServerCallbacks { virtual uint32_t onPassKeyRequest() { - - uint32_t passkey = 0; + uint32_t passkey = config.bluetooth.fixed_pin; if (doublepressed > 0 && (doublepressed + (30 * 1000)) > millis()) { - DEBUG_MSG("User has overridden passkey\n"); - passkey = defaultBLEPin; - } else { + DEBUG_MSG("User has set BLE pairing mode to fixed-pin\n"); + config.bluetooth.mode = Config_BluetoothConfig_PairingMode_FixedPin; + nodeDB.saveToDisk(); + } else if (config.bluetooth.mode == Config_BluetoothConfig_PairingMode_RandomPin) { DEBUG_MSG("Using random passkey\n"); // This is the passkey to be entered on peer - we pick a number >100,000 to ensure 6 digits passkey = random(100000, 999999); @@ -134,8 +134,15 @@ void ESP32Bluetooth::setup() NimBLEDevice::init(getDeviceName()); NimBLEDevice::setPower(ESP_PWR_LVL_P9); - NimBLEDevice::setSecurityAuth(true, true, true); - NimBLEDevice::setSecurityIOCap(BLE_HS_IO_DISPLAY_ONLY); + // FIXME fails in iOS + if (config.bluetooth.mode == Config_BluetoothConfig_PairingMode_NoPin) { + NimBLEDevice::setSecurityIOCap(BLE_HS_IO_NO_INPUT_OUTPUT); + NimBLEDevice::setSecurityAuth(true, false, true); + } + else { + NimBLEDevice::setSecurityAuth(true, true, true); + NimBLEDevice::setSecurityIOCap(BLE_HS_IO_DISPLAY_ONLY); + } bleServer = NimBLEDevice::createServer(); ESP32BluetoothServerCallback *serverCallbacks = new ESP32BluetoothServerCallback(); diff --git a/src/platform/nrf52/NRF52Bluetooth.cpp b/src/platform/nrf52/NRF52Bluetooth.cpp index 28aa4fea2..ede288b27 100644 --- a/src/platform/nrf52/NRF52Bluetooth.cpp +++ b/src/platform/nrf52/NRF52Bluetooth.cpp @@ -130,12 +130,8 @@ void fromRadioAuthorizeCb(uint16_t conn_hdl, BLECharacteristic *chr, ble_gatts_e { if (request->offset == 0) { // If the read is long, we will get multiple authorize invocations - we only populate data on the first - size_t numBytes = bluetoothPhoneAPI->getFromRadio(fromRadioBytes); - // DEBUG_MSG("fromRadioAuthorizeCb numBytes=%u\n", numBytes); - // if (numBytes >= 2) DEBUG_MSG("fromRadio bytes %x %x\n", fromRadioBytes[0], fromRadioBytes[1]); - // 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 fromRadio.write(fromRadioBytes, numBytes); @@ -175,17 +171,13 @@ void setupMeshService(void) fromNum.setProperties(CHR_PROPS_NOTIFY | CHR_PROPS_READ); fromNum.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS); // FIXME, secure this!!! - fromNum.setFixedLen( - 0); // Variable len (either 0 or 4) FIXME consider changing protocol so it is fixed 4 byte len, where 0 means empty + fromNum.setFixedLen(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.setCccdWriteCallback(cccd_callback); // Optionally capture CCCD updates // We don't yet need to hook the fromNum auth callback // fromNum.setReadAuthorizeCallback(fromNumAuthorizeCb); fromNum.write32(0); // Provide default fromNum of 0 fromNum.begin(); - // uint8_t hrmdata[2] = {0b00000110, 0x40}; // Set the characteristic to use 8-bit values, with the sensor connected and - // detected - // hrmc.write(hrmdata, 2); fromRadio.setProperties(CHR_PROPS_READ); fromRadio.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS); // FIXME secure this! @@ -202,9 +194,8 @@ void setupMeshService(void) toRadio.setFixedLen(0); toRadio.setMaxLen(512); toRadio.setBuffer(toRadioBytes, sizeof(toRadioBytes)); - toRadio.setWriteCallback( - toRadioWriteCb, - false); // 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(toRadioWriteCb, false); toRadio.begin(); } @@ -239,8 +230,6 @@ void NRF52Bluetooth::setup() // Configure and Start the Device Information Service DEBUG_MSG("Configuring the Device Information Service\n"); - // FIXME, we should set a mfg string based on our HW_VENDOR enum - // bledis.setManufacturer(HW_VENDOR); bledis.setModel(optstr(HW_VERSION)); bledis.setFirmwareRev(optstr(APP_VERSION)); bledis.begin(); @@ -249,7 +238,6 @@ void NRF52Bluetooth::setup() DEBUG_MSG("Configuring the Battery Service\n"); blebas.begin(); blebas.write(0); // Unknown battery level for now - bledfu.begin(); // Install the DFU helper // Setup the Heart Rate Monitor service using @@ -258,7 +246,8 @@ void NRF52Bluetooth::setup() setupMeshService(); // Supposedly debugging works with soft device if you disable advertising - if (isSoftDeviceAllowed) { + if (isSoftDeviceAllowed) + { // Setup the advertising packet(s) DEBUG_MSG("Setting up the advertising payload(s)\n"); startAdv();