From 7d6dbcfa3f34ac9affd2f28c514512a047822006 Mon Sep 17 00:00:00 2001 From: BeardyWalrus Date: Tue, 29 Sep 2020 19:51:39 -0400 Subject: [PATCH 1/4] Update BluetoothUtil.cpp fix for #357 use presence of ssd1306 display to set display functionality for bluetooth security. --- src/nimble/BluetoothUtil.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/nimble/BluetoothUtil.cpp b/src/nimble/BluetoothUtil.cpp index f294802c..6f4f92cb 100644 --- a/src/nimble/BluetoothUtil.cpp +++ b/src/nimble/BluetoothUtil.cpp @@ -472,7 +472,14 @@ void reinitBluetooth() ble_hs_cfg.gatts_register_cb = gatt_svr_register_cb; ble_hs_cfg.store_status_cb = ble_store_util_status_rr; - ble_hs_cfg.sm_io_cap = BLE_SM_IO_CAP_DISP_ONLY; + if (ssd1306_found == true) { + DEBUG_MSG("Screen Found! : using display for passcode\n"); + ble_hs_cfg.sm_io_cap = BLE_SM_IO_CAP_DISP_ONLY; + } + else { + DEBUG_MSG("No Screen Found! : disabling passcode for pairing\n"); + ble_hs_cfg.sm_io_cap = BLE_SM_IO_CAP_NO_IO; + } ble_hs_cfg.sm_bonding = 1; ble_hs_cfg.sm_mitm = 1; ble_hs_cfg.sm_sc = 1; From 530432411e4dfc870d15d85d7b5d326d0bbecca2 Mon Sep 17 00:00:00 2001 From: BeardyWalrus Date: Sat, 3 Oct 2020 17:02:17 -0400 Subject: [PATCH 2/4] revised fix for #357 Now supports default password for devices with no display, and override with double press of user button --- src/configuration.h | 3 +++ src/main.cpp | 6 ++++++ src/nimble/BluetoothUtil.cpp | 31 +++++++++++++++++++++---------- src/nimble/BluetoothUtil.h | 1 + 4 files changed, 31 insertions(+), 10 deletions(-) diff --git a/src/configuration.h b/src/configuration.h index 026bb188..f9a39742 100644 --- a/src/configuration.h +++ b/src/configuration.h @@ -422,3 +422,6 @@ along with this program. If not, see . #define GPS_POWER_CTRL_CH 3 #define LORA_POWER_CTRL_CH 2 + +// Default Bluetooth PIN +#define defaultBLEPin 123456 diff --git a/src/main.cpp b/src/main.cpp index c53fdfef..03737f38 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -150,6 +150,10 @@ void userButtonPressedLong() { screen.adjustBrightness(); } +void userButtonDoublePressed() +{ + disablePin(); +} void setup() { @@ -189,11 +193,13 @@ void setup() userButton = OneButton(BUTTON_PIN, true, true); userButton.attachClick(userButtonPressed); userButton.attachDuringLongPress(userButtonPressedLong); + userButton.attachDoubleClick(userButtonDoublePressed); #endif #ifdef BUTTON_PIN_ALT userButtonAlt = OneButton(BUTTON_PIN_ALT, true, true); userButtonAlt.attachClick(userButtonPressed); userButton.attachDuringLongPress(userButtonPressedLong); + userButton.attachDoubleClick(userButtonDoublePressed); #endif #ifdef LED_PIN pinMode(LED_PIN, OUTPUT); diff --git a/src/nimble/BluetoothUtil.cpp b/src/nimble/BluetoothUtil.cpp index 6f4f92cb..3e631aef 100644 --- a/src/nimble/BluetoothUtil.cpp +++ b/src/nimble/BluetoothUtil.cpp @@ -15,6 +15,7 @@ #include "meshwifi/meshwifi.h" static bool pinShowing; +static uint32_t doublepressed; static void startCb(uint32_t pin) { @@ -123,6 +124,7 @@ static int gap_event(struct ble_gap_event *event, void *arg) { struct ble_gap_conn_desc desc; int rc; + uint32_t now = millis(); switch (event->type) { case BLE_GAP_EVENT_CONNECT: @@ -221,8 +223,17 @@ static int gap_event(struct ble_gap_event *event, void *arg) if (event->passkey.params.action == BLE_SM_IOACT_DISP) { pkey.action = event->passkey.params.action; - pkey.passkey = random( - 100000, 999999); // This is the passkey to be entered on peer - we pick a number >100,000 to ensure 6 digits + DEBUG_MSG("dp: %d now:%d\n",doublepressed, now); + if ((doublepressed > 0 && (doublepressed + (30*1000)) > now) || ssd1306_found != true) + { + DEBUG_MSG("User has overridden passkey or no display available\n"); + pkey.passkey = defaultBLEPin; + } + else { + DEBUG_MSG("Using random passkey\n"); + pkey.passkey = random( + 100000, 999999); // This is the passkey to be entered on peer - we pick a number >100,000 to ensure 6 digits + } DEBUG_MSG("*** Enter passkey %d on the peer side ***\n", pkey.passkey); startCb(pkey.passkey); @@ -443,6 +454,13 @@ int chr_readwrite8(uint8_t *v, size_t vlen, struct ble_gatt_access_ctxt *ctxt) return 0; // success } +void disablePin() +{ + DEBUG_MSG("User Override, disabling bluetooth pin requirement\n"); + // keep track of when it was pressed, so we know it was within X seconds + doublepressed = millis(); +} + // This routine is called multiple times, once each time we come back from sleep void reinitBluetooth() { @@ -472,14 +490,7 @@ void reinitBluetooth() ble_hs_cfg.gatts_register_cb = gatt_svr_register_cb; ble_hs_cfg.store_status_cb = ble_store_util_status_rr; - if (ssd1306_found == true) { - DEBUG_MSG("Screen Found! : using display for passcode\n"); - ble_hs_cfg.sm_io_cap = BLE_SM_IO_CAP_DISP_ONLY; - } - else { - DEBUG_MSG("No Screen Found! : disabling passcode for pairing\n"); - ble_hs_cfg.sm_io_cap = BLE_SM_IO_CAP_NO_IO; - } + ble_hs_cfg.sm_io_cap = BLE_SM_IO_CAP_DISP_ONLY; ble_hs_cfg.sm_bonding = 1; ble_hs_cfg.sm_mitm = 1; ble_hs_cfg.sm_sc = 1; diff --git a/src/nimble/BluetoothUtil.h b/src/nimble/BluetoothUtil.h index 453fadb5..fe6d0e67 100644 --- a/src/nimble/BluetoothUtil.h +++ b/src/nimble/BluetoothUtil.h @@ -15,6 +15,7 @@ void updateBatteryLevel(uint8_t level); void deinitBLE(); void loopBLE(); void reinitBluetooth(); +void disablePin(); /** * A helper function that implements simple read and write handling for a uint32_t From 4d7cd0a09dd3309ef9dcee7923431cb741985e29 Mon Sep 17 00:00:00 2001 From: BeardyWalrus Date: Sat, 3 Oct 2020 17:23:36 -0400 Subject: [PATCH 3/4] conditional on needing ESP32 bluetooth header loaded --- platformio.ini | 4 ++-- src/main.cpp | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/platformio.ini b/platformio.ini index 8dfc9dc5..3e66a7ef 100644 --- a/platformio.ini +++ b/platformio.ini @@ -34,8 +34,8 @@ build_flags = -Wno-missing-field-initializers -Isrc -Isrc/mesh -Isrc/gps -Ilib/n ; leave this commented out to avoid breaking Windows ;upload_port = /dev/ttyUSB0 ;monitor_port = /dev/ttyUSB0 -upload_port = /dev/cu.SLAB_USBtoUART -monitor_port = /dev/cu.SLAB_USBtoUART +;upload_port = /dev/cu.SLAB_USBtoUART +;monitor_port = /dev/cu.SLAB_USBtoUART ; the default is esptool ; upload_protocol = esp-prog diff --git a/src/main.cpp b/src/main.cpp index 57c304b6..9c9b8089 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -152,7 +152,9 @@ void userButtonPressedLong() } void userButtonDoublePressed() { +#ifndef NO_ESP32 disablePin(); +#endif } void setup() From b4b1b24c8475cfb2e0cd760ccc304eca55ba8b30 Mon Sep 17 00:00:00 2001 From: BeardyWalrus Date: Mon, 5 Oct 2020 20:13:04 -0400 Subject: [PATCH 4/4] always need double press, even if you dont have a screen --- src/nimble/BluetoothUtil.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/nimble/BluetoothUtil.cpp b/src/nimble/BluetoothUtil.cpp index 3e631aef..00c0bcd8 100644 --- a/src/nimble/BluetoothUtil.cpp +++ b/src/nimble/BluetoothUtil.cpp @@ -224,7 +224,7 @@ static int gap_event(struct ble_gap_event *event, void *arg) if (event->passkey.params.action == BLE_SM_IOACT_DISP) { pkey.action = event->passkey.params.action; DEBUG_MSG("dp: %d now:%d\n",doublepressed, now); - if ((doublepressed > 0 && (doublepressed + (30*1000)) > now) || ssd1306_found != true) + if (doublepressed > 0 && (doublepressed + (30*1000)) > now) { DEBUG_MSG("User has overridden passkey or no display available\n"); pkey.passkey = defaultBLEPin;