Skip to content

Commit f7b41f3

Browse files
committed
nRF52: Add NRF/BluetoothRemoteGATTServer.updateConnection for setting the phy
Mainly @fanoush's code from master...fanoush:Espruino:f-update-phy
1 parent 9bc16c4 commit f7b41f3

File tree

3 files changed

+113
-1
lines changed

3 files changed

+113
-1
lines changed

ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
nRF52: Add 'phy' and 'mtu' events to NRF/BluetoothDevice when these change on a connection
55
MDBT42Q: Remove SHA512 from the build (needs 2.5k flash, rarely used)
66
Bangle.js2: Adjust accelerometer axes for HRM algorithm to more closely match the datasheet
7+
nRF52: Add NRF/BluetoothRemoteGATTServer.updateConnection for setting the phy
78

89
2v27 : nRF5x: Ensure Bluetooth notifications work correctly when two separate connections use the same handle for their characteristics
910
nRF5x: Remove handlers from our handlers array when a device is disconnected

libs/bluetooth/jswrap_bluetooth.c

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,9 @@ received. It doesn't get called if NFC is started with `NRF.nfcURL` or
598598
* 4 : coded phy
599599
600600
`status` is an integer containing the status code. 0 = success
601+
602+
**This is not part of the Web Bluetooth Specification.** It has been added
603+
specifically for Espruino.
601604
*/
602605
/*JSON{
603606
"type" : "event",
@@ -617,6 +620,9 @@ received. It doesn't get called if NFC is started with `NRF.nfcURL` or
617620
* 4 : coded phy
618621
619622
eg. `7` means all phys (eg any) have been requested
623+
624+
**This is not part of the Web Bluetooth Specification.** It has been added
625+
specifically for Espruino.
620626
*/
621627
/*JSON{
622628
"type" : "event",
@@ -629,6 +635,9 @@ eg. `7` means all phys (eg any) have been requested
629635
}
630636
(2v28+) This event is fired when the MTU changes for the active Bluetooth connection. This is the amount of
631637
data that can be transferred in one packet.
638+
639+
**This is not part of the Web Bluetooth Specification.** It has been added
640+
specifically for Espruino.
632641
*/
633642
/*JSON{
634643
"type" : "event",
@@ -1436,7 +1445,7 @@ it returns the packet that would be advertised as an array.
14361445
14371446
In addition, `options` can contain:
14381447
1439-
* [2v26+] `flags : bool` if `flags:false`, the Bluetooth appearance flags
1448+
* (2v26+) `flags : bool` if `flags:false`, the Bluetooth appearance flags
14401449
are left out (usually `[2,1,6]`). It can be very useful to do this
14411450
if you're using `NRF.getAdvertisingData(...)` to set a scan response packet:
14421451
@@ -4065,6 +4074,9 @@ JsVar *jswrap_BluetoothDevice_gatt(JsVar *parent) {
40654074
"return" : ["bool", "The last received RSSI (signal strength) for this device" ]
40664075
}
40674076
This is set whenever the RSSI of the connection is changed. `BluetoothGATTServer.on("rssi", ...)` is also emitted.
4077+
4078+
**This is not part of the Web Bluetooth Specification.** It has been added
4079+
specifically for Espruino.
40684080
*/
40694081
/*Documentation only*/
40704082
/*JSON{
@@ -4077,6 +4089,9 @@ This is set whenever the RSSI of the connection is changed. `BluetoothGATTServer
40774089
"ifdef" : "NRF52_SERIES"
40784090
}
40794091
This event is fired whenever the RSSI of the connection is changed. `BluetoothDevice.rssi` is also updated
4092+
4093+
**This is not part of the Web Bluetooth Specification.** It has been added
4094+
specifically for Espruino.
40804095
*/
40814096
/*JSON{
40824097
"type" : "event",
@@ -4141,6 +4156,99 @@ void jswrap_ble_BluetoothDevice_sendPasskey(JsVar *parent, JsVar *passkeyVar) {
41414156
}
41424157
#endif
41434158

4159+
4160+
static void jsble_update_connection(uint16_t connection_handle, JsVar *options){
4161+
#ifdef NRF52_SERIES
4162+
uint32_t err_code;
4163+
#if (NRF_SD_BLE_API_VERSION >= 5)
4164+
ble_gap_phys_t gap_phys;
4165+
uint8_t phy = BLE_GAP_PHY_NOT_SET;
4166+
JsVar *advPhy = jsvObjectGetChildIfExists(options, "phy");
4167+
if (jsvIsStringEqual(advPhy,"1mbps")) {
4168+
phy = BLE_GAP_PHY_1MBPS;
4169+
} else if (jsvIsStringEqual(advPhy,"2mbps")) {
4170+
phy = BLE_GAP_PHY_2MBPS;
4171+
} else if (jsvIsStringEqual(advPhy,"auto")) {
4172+
phy = BLE_GAP_PHY_AUTO;
4173+
#if NRF_SD_BLE_API_VERSION>5
4174+
} else if (jsvIsStringEqual(advPhy,"coded")) {
4175+
phy = BLE_GAP_PHY_CODED;
4176+
#endif
4177+
} else jsWarn("Unknown phy %q\n", advPhy);
4178+
jsvUnLock(advPhy);
4179+
if (phy != BLE_GAP_PHY_NOT_SET){
4180+
gap_phys.rx_phys = phy;
4181+
gap_phys.tx_phys = phy;
4182+
err_code = sd_ble_gap_phy_update(connection_handle, &gap_phys);
4183+
jsble_check_error(err_code);
4184+
}
4185+
#endif
4186+
#endif
4187+
#ifdef ESP32
4188+
jsWarn("update not implemented\n");
4189+
#endif
4190+
}
4191+
4192+
/*JSON{
4193+
"type" : "method",
4194+
"class" : "BluetoothRemoteGATTServer",
4195+
"name" : "updateConnection",
4196+
"generate" : "jswrap_BluetoothRemoteGATTServer_updateConnection",
4197+
"params" : [
4198+
["options","JsVar","An object containing connection options"]
4199+
],
4200+
"#if" : "defined(NRF52_SERIES)"
4201+
}
4202+
(2v28+) Update connection parameters on this central connection. Options can be:
4203+
4204+
```
4205+
{
4206+
phy : string // "1mpbs"/"2mpbs"/"coded"/"auto"
4207+
}
4208+
```
4209+
4210+
**This is not part of the Web Bluetooth Specification.** It has been added
4211+
specifically for Espruino.
4212+
*/
4213+
void jswrap_BluetoothRemoteGATTServer_updateConnection(JsVar *parent, JsVar *options) {
4214+
#if CENTRAL_LINK_COUNT>0
4215+
uint16_t central_conn_handle = jswrap_ble_BluetoothRemoteGATTServer_getHandle(parent);
4216+
if (jsvObjectGetBoolChild(parent,"connected") && central_conn_handle != BLE_CONN_HANDLE_INVALID) {
4217+
// we have a connection, update it
4218+
jsble_update_connection(central_conn_handle, options);
4219+
} else {
4220+
jsExceptionHere(JSET_ERROR, "Not connected");
4221+
}
4222+
#endif
4223+
}
4224+
4225+
/*JSON{
4226+
"type" : "staticmethod",
4227+
"class" : "NRF",
4228+
"name" : "updateConnection",
4229+
"ifdef" : "NRF52_SERIES",
4230+
"generate" : "jswrap_ble_updateConnection",
4231+
"params" : [
4232+
["options","JsVar","An object containing connection options"]
4233+
],
4234+
"#if" : "defined(NRF52_SERIES)"
4235+
}
4236+
(2v28+) Update connection parameters on the current peripheral connection. Options can be:
4237+
4238+
```
4239+
{
4240+
phy : string // "1mpbs"/"2mpbs"/"coded"/"auto"
4241+
}
4242+
```
4243+
*/
4244+
void jswrap_ble_updateConnection(JsVar *options) {
4245+
if (jsble_has_peripheral_connection()) {
4246+
jsble_update_connection(m_peripheral_conn_handle, options);
4247+
}else {
4248+
jsExceptionHere(JSET_ERROR, "Not connected");
4249+
}
4250+
}
4251+
41444252
/*JSON{
41454253
"type" : "method",
41464254
"class" : "BluetoothRemoteGATTServer",

libs/bluetooth/jswrap_bluetooth.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ void jswrap_ble_findDevices(JsVar *callback, JsVar *options);
113113
void jswrap_ble_setRSSIHandler(JsVar *callback);
114114
void jswrap_ble_setTxPower(JsVarInt pwr);
115115
void jswrap_ble_setLowPowerConnection(bool lowPower);
116+
void jswrap_ble_updateConnection(JsVar *options);
116117

117118
void jswrap_nfc_URL(JsVar *url);
118119
void jswrap_nfc_pair(JsVar *key);
@@ -160,5 +161,7 @@ JsVar *jswrap_ble_BluetoothRemoteGATTCharacteristic_writeValue(JsVar *characteri
160161
JsVar *jswrap_ble_BluetoothRemoteGATTCharacteristic_readValue(JsVar *characteristic);
161162
JsVar *jswrap_ble_BluetoothRemoteGATTCharacteristic_startNotifications(JsVar *characteristic);
162163
JsVar *jswrap_ble_BluetoothRemoteGATTCharacteristic_stopNotifications(JsVar *characteristic);
164+
void jswrap_BluetoothRemoteGATTServer_updateConnection(JsVar *parent, JsVar *options);
165+
163166

164167
void jswrap_ble_powerusage(JsVar *devices);

0 commit comments

Comments
 (0)