8000 extmod/modbluetooth: Add optional 4th arg to gattc_write for write mode. · rlangoy/micropython@7aeafe2 · GitHub
[go: up one dir, main page]

Skip to content < 8000 div data-target="react-partial.reactRoot">

Commit 7aeafe2

Browse files
committed
extmod/modbluetooth: Add optional 4th arg to gattc_write for write mode.
This allows the user to explicitly select the behaviour of the write to the remote peripheral. This is needed for peripherals that have characteristics with WRITE_NO_RESPONSE set (instead of normal WRITE). The function's signature is now: BLE.gattc_write(conn_handle, value_handle, data, mode=0) mode=0 means write without response, while mode=1 means write with response. The latter was the original behaviour so this commit is a change in behaviour of this method, and one should specify 1 as the 4th argument to get back the old behaviour. In the future there could be more modes supported, such as long writes.
1 parent 9a849cc commit 7aeafe2

File tree

4 files changed

+33
-7
lines changed

4 files changed

+33
-7
lines changed

docs/library/ubluetooth.rst

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,12 +310,23 @@ Central Role (GATT Client)
310310

311311
On success, the ``_IRQ_GATTC_READ_RESULT`` event will be raised.
312312

313-
.. method:: BLE.gattc_write(conn_handle, value_handle, data)
313+
.. method:: BLE.gattc_write(conn_handle, value_handle, data, mode=0)
314314

315315
Issue a remote write to a connected peripheral for the specified
316316
characteristic or descriptor handle.
317317

318-
On success, the ``_IRQ_GATTC_WRITE_STATUS`` event will be raised.
318+
The argument *mode* specifies the write behaviour, with the currently
319+
supported values being:
320+
321+
* ``mode=0`` (default) is a write-without-response: the write will
322+
be sent to the remote peripheral but no confirmation will be
323+
returned, and no event will be raised.
324+
* ``mode=1`` is a write-with-response: the remote peripheral is
325+
requested to send a response/acknowledgement that it received the
326+
data.
327+
328+
If a response is received from the remote peripheral the
329+
``_IRQ_GATTC_WRITE_STATUS`` event will be raised.
319330

320331

321332
class UUID

extmod/modbluetooth.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -641,9 +641,13 @@ STATIC mp_obj_t bluetooth_ble_gattc_write(size_t n_args, const mp_obj_t *args) {
641641
mp_buffer_info_t bufinfo = {0};
642642
mp_get_buffer_raise(data, &bufinfo, MP_BUFFER_READ);
643643
size_t len = bufinfo.len;
644-
return bluetooth_handle_errno(mp_bluetooth_gattc_write(conn_handle, value_handle, bufinfo.buf, &len));
644+
unsigned int mode = MP_BLUETOOTH_WRITE_MODE_NO_RESPONSE;
645+
if (n_args == 5) {
646+
mode = mp_obj_get_int(args[4]);
647+
}
648+
return bluetooth_handle_errno(mp_bluetooth_gattc_write(conn_handle, value_handle, bufinfo.buf, &len, mode));
645649
}
646-
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bluetooth_ble_gattc_write_obj, 4, 4, bluetooth_ble_gattc_write);
650+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bluetooth_ble_gattc_write_obj, 4, 5, bluetooth_ble_gattc_write);
647651

648652
#endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
649653

extmod/modbluetooth.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@
6565
#define MP_BLUETOOTH_CHARACTERISTIC_FLAG_WRITE (1 << 3)
6666
#define MP_BLUETOOTH_CHARACTERISTIC_FLAG_NOTIFY (1 << 4)
6767

68+
// For mp_bluetooth_gattc_write, the mode parameter
69+
#define MP_BLUETOOTH_WRITE_MODE_NO_RESPONSE (0)
70+
#define MP_BLUETOOTH_WRITE_MODE_WITH_RESPONSE (1)
71+
6872
// Type value also doubles as length.
6973
#define MP_BLUETOOTH_UUID_TYPE_16 (2)
7074
#define MP_BLUETOOTH_UUID_TYPE_32 (4)
@@ -219,7 +223,7 @@ int mp_bluetooth_gattc_discover_descriptors(uint16_t conn_handle, uint16_t start
219223
int mp_bluetooth_gattc_read(uint16_t conn_handle, uint16_t value_handle);
220224

221225
// Write the value to the remote peripheral.
222-
int mp_bluetooth_gattc_write(uint16_t conn_handle, uint16_t value_handle, const uint8_t *value, size_t *value_len);
226+
int mp_bluetooth_gattc_write(uint16_t conn_handle, uint16_t value_handle, const uint8_t *value, size_t *value_len, unsigned int mode);
223227
#endif
224228

225229
/////////////////////////////////////////////////////////////////////////////

extmod/modbluetooth_nimble.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -816,8 +816,15 @@ STATIC int ble_gatt_attr_write_cb(uint16_t conn_handle, const struct ble_gatt_er
816816
}
817817

818818
// Write the value to the remote peripheral.
819-
int mp_bluetooth_gattc_write(uint16_t conn_handle, uint16_t value_handle, const uint8_t *value, size_t *value_len) {
820-
int err = ble_gattc_write_flat(conn_handle, value_handle, value, *value_len, &ble_gatt_attr_write_cb, NULL);
819+
int mp_bluetooth_gattc_write(uint16_t conn_handle, uint16_t value_handle, const uint8_t *value, size_t *value_len, unsigned int mode) {
820+
int err;
821+
if (mode == MP_BLUETOOTH_WRITE_MODE_NO_RESPONSE) {
822+
err = ble_gattc_write_no_rsp_flat(conn_handle, value_handle, value, *value_len);
823+
} else if (mode == MP_BLUETOOTH_WRITE_MODE_WITH_RESPONSE) {
824+
err = ble_gattc_write_flat(conn_handle, value_handle, value, *value_len, &ble_gatt_attr_write_cb, NULL);
825+
} else {
826+
err = BLE_HS_EINVAL;
827+
}
821828
return ble_hs_err_to_errno(err);
822829
}
823830

0 commit comments

Comments
 (0)
0