Skip to content

Commit

Permalink
nRF5x: Add callback param to 'NRF.restart', allowing code to be calle…
Browse files Browse the repository at this point in the history
…d with softdevice disabled
  • Loading branch information
gfwilliams committed Oct 26, 2020
1 parent ef48eea commit 542265b
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 13 deletions.
1 change: 1 addition & 0 deletions ChangeLog
@@ -1,5 +1,6 @@
Bangle.js: increase default advertising interval from 375 to 200ms to ease connections
Fix Math.acos for negative values (fix #1950)
nRF5x: Add callback param to 'NRF.restart', allowing code to be called with softdevice disabled

2v08 : nRF52: Added option to build in I2C slave support
Fix Tensorflow aiGesture regression from 2v07 (re-add opcodes) (fix #1936)
Expand Down
5 changes: 3 additions & 2 deletions libs/bluetooth/bluetooth.h
Expand Up @@ -163,8 +163,9 @@ void jsble_queue_pending(BLEPending blep, uint16_t data);
int jsble_exec_pending(IOEvent *event);

/** Stop and restart the softdevice so that we can update the services in it -
* both user-defined as well as UART/HID */
void jsble_restart_softdevice();
* both user-defined as well as UART/HID. If jsFunction is a function it is
* called when the softdevice is uninitialised. */
void jsble_restart_softdevice(JsVar *jsFunction);

uint32_t jsble_advertising_start();
uint32_t jsble_advertising_update_advdata(char *dPtr, unsigned int dLen);
Expand Down
17 changes: 10 additions & 7 deletions libs/bluetooth/jswrap_bluetooth.c
Expand Up @@ -506,7 +506,10 @@ void jswrap_ble_wake() {
"type" : "staticmethod",
"class" : "NRF",
"name" : "restart",
"generate" : "jswrap_ble_restart"
"generate" : "jswrap_ble_restart",
"params" : [
["callback","JsVar","An optional function to be called while the softdevice is uninitialised. Use with caution - accessing console/bluetooth will almost certainly result in a crash."]
]
}
Restart the Bluetooth softdevice (if there is currently a BLE connection,
it will queue a restart to be done when the connection closes).
Expand All @@ -516,14 +519,14 @@ BLE softdevice has some settings that cannot be reset. For example there
are only a certain number of unique UUIDs. Once these are all used the
only option is to restart the softdevice to clear them all out.
*/
void jswrap_ble_restart() {
void jswrap_ble_restart(JsVar *callback) {
if (jsble_has_connection()) {
jsiConsolePrintf("BLE Connected, queueing BLE restart for later\n");
bleStatus |= BLE_NEEDS_SOFTDEVICE_RESTART;
return;
} else {
// Not connected, so we can restart now
jsble_restart_softdevice();
jsble_restart_softdevice(jsvIsFunction(callback)?callback:NULL);
return;
}
}
Expand Down Expand Up @@ -592,7 +595,7 @@ void jswrap_ble_setAddress(JsVar *address) {
return;
}
jsvObjectSetChild(execInfo.hiddenRoot, BLE_NAME_MAC_ADDRESS, address);
jswrap_ble_restart();
jswrap_ble_restart(NULL);
#else
jsExceptionHere(JSET_ERROR, "Not implemented");
#endif
Expand Down Expand Up @@ -1222,7 +1225,7 @@ void jswrap_ble_setServices(JsVar *data, JsVar *options) {

// work out whether to apply changes
if (bleStatus & (BLE_SERVICES_WERE_SET|BLE_NEEDS_SOFTDEVICE_RESTART)) {
jswrap_ble_restart();
jswrap_ble_restart(NULL);
} else {
/* otherwise, we can set the services now, since we're only adding
* and not changing anything we don't need a restart. */
Expand Down Expand Up @@ -2001,7 +2004,7 @@ void jswrap_ble_setLowPowerConnection(bool lowPower) {
flags &= ~BLE_FLAGS_LOW_POWER;
if (flags != oldflags) {
jsvObjectSetChildAndUnLock(execInfo.hiddenRoot, BLE_NAME_FLAGS, jsvNewFromInteger(flags));
jswrap_ble_restart();
jswrap_ble_restart(NULL);
}
}

Expand Down Expand Up @@ -2811,7 +2814,7 @@ void jswrap_ble_setSecurity(JsVar *options) {
jsble_update_security();
// If we need UART to be encrypted, we need to trigger a restart
if (bleStatus & BLE_NEEDS_SOFTDEVICE_RESTART)
jswrap_ble_restart();
jswrap_ble_restart(NULL);
}
}

Expand Down
2 changes: 1 addition & 1 deletion libs/bluetooth/jswrap_bluetooth.h
Expand Up @@ -65,7 +65,7 @@ void jswrap_ble_reconfigure_softdevice();
void jswrap_ble_disconnect();
void jswrap_ble_sleep();
void jswrap_ble_wake();
void jswrap_ble_restart();
void jswrap_ble_restart(JsVar *callback);
JsVar *jswrap_ble_getAddress();
void jswrap_ble_setAddress(JsVar *address);

Expand Down
4 changes: 3 additions & 1 deletion targets/esp32/bluetooth.c
Expand Up @@ -82,11 +82,13 @@ int jsble_exec_pending(IOEvent *event){
return 0;
}

void jsble_restart_softdevice(){
void jsble_restart_softdevice(JsVar *jsFunction){
bleStatus &= ~(BLE_NEEDS_SOFTDEVICE_RESTART | BLE_SERVICES_WERE_SET);
if (bleStatus & BLE_IS_SCANNING) {
bluetooth_gap_setScan(false);
}
if (jsvIsFunction(jsFunction))
jspExecuteFunction(jsFunction,NULL,0,NULL);
jswrap_ble_reconfigure_softdevice();
}

Expand Down
6 changes: 4 additions & 2 deletions targets/nrf5x/bluetooth.c
Expand Up @@ -1167,7 +1167,7 @@ static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context) {
jsble_queue_pending(BLEP_DISCONNECTED, p_ble_evt->evt.gap_evt.params.disconnected.reason);
}
if ((bleStatus & BLE_NEEDS_SOFTDEVICE_RESTART) && !jsble_has_connection())
jsble_restart_softdevice();
jsble_restart_softdevice(NULL);

break;

Expand Down Expand Up @@ -2495,7 +2495,7 @@ void jsble_kill() {

/** Stop and restart the softdevice so that we can update the services in it -
* both user-defined as well as UART/HID */
void jsble_restart_softdevice() {
void jsble_restart_softdevice(JsVar *jsFunction) {
assert(!jsble_has_connection());
bleStatus &= ~(BLE_NEEDS_SOFTDEVICE_RESTART | BLE_SERVICES_WERE_SET);

Expand All @@ -2508,6 +2508,8 @@ void jsble_restart_softdevice() {
jshUtilTimerDisable(); // don't want the util timer firing during this!
JsSysTime lastTime = jshGetSystemTime();
jsble_kill();
if (jsvIsFunction(jsFunction))
jspExecuteFunction(jsFunction,NULL,0,NULL);
jsble_init();
// reinitialise everything
jswrap_ble_reconfigure_softdevice();
Expand Down

0 comments on commit 542265b

Please sign in to comment.