8000 extmod/modbluetooth: Add send_update arg to gatts_write. · codemee/micropython@1d9e489 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1d9e489

Browse files
jimmodpgeorge
authored andcommitted
extmod/modbluetooth: Add send_update arg to gatts_write.
This allows the write to trigger a notification or indication, but only to subscribed clients. This is different to gatts_notify/gatts_indicate, which will unconditionally notify/indicate. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
1 parent 5733c49 commit 1d9e489

File tree

6 files changed

+42
-18
lines changed

6 files changed

+42
-18
lines changed

docs/library/bluetooth.rst

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -485,10 +485,14 @@ writes from a client to a given characteristic, use
485485
Reads the local value for this handle (which has either been written by
486486
:meth:`gatts_write <BLE.gatts_write>` or by a remote client).
487487

488-
.. method:: BLE.gatts_write(value_handle, data, /)
488+
.. method:: BLE.gatts_write(value_handle, data, send_update=False, /)
489489

490490
Writes the local value for this handle, which can be read by a client.
491491

492+
If *send_update* is ``True``, then any subscribed clients will be notified
493+
(or indicated, depending on what they're subscribed to and which operations
494+
the characteristic supports) about this write.
495+
492496
.. method:: BLE.gatts_notify(conn_handle, value_handle, data=None, /)
493497

494498
Sends a notification request to a connected client.
@@ -499,17 +503,20 @@ writes from a client to a given characteristic, use
499503
Otherwise, if *data* is ``None``, then the current local value (as
500504
set with :meth:`gatts_write <BLE.gatts_write>`) will be sent.
501505

502-
.. method:: BLE.gatts_indicate(conn_handle, value_handle, /)
506+
**Note:** The notification will be sent regardless of the subscription
507+
status of the client to this characteristic.
503508

504-
Sends an indication request to a connected client.
509+
.. method:: BLE.gatts_indicate(conn_handle, value_handle, /)
505510

506-
**Note:** This does not currently support sending a custom value, it will
507-
always send the current local value (as set with :meth:`gatts_write
508-
<BLE.gatts_write>`).
511+
Sends an indication request containing the characteristic's current value to
512+
a connected client.
509513

510514
On acknowledgment (or failure, e.g. timeout), the
511515
``_IRQ_GATTS_INDICATE_DONE`` event will be raised.
512516

517+
**Note:** The indication will be sent regardless of the subscription
518+
status of the client to this characteristic.
519+
513520
.. method:: BLE.gatts_set_buffer(value_handle, len, append=False, /)
514521

515522
Sets the internal buffer size for a value in bytes. This will limit the

extmod/btstack/modbluetooth_btstack.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1085,11 +1085,14 @@ int mp_bluetooth_gatts_read(uint16_t value_handle, uint8_t **value, size_t *valu
10851085
return mp_bluetooth_gatts_db_read(MP_STATE_PORT(bluetooth_btstack_root_pointers)->gatts_db, value_handle, value, value_len);
10861086
}
10871087

1088-
int mp_bluetooth_gatts_write(uint16_t value_handle, const uint8_t *value, size_t value_len) {
1088+
int mp_bluetooth_gatts_write(uint16_t value_handle, const uint8_t *value, size_t value_len, bool send_update) {
10891089
DEBUG_printf("mp_bluetooth_gatts_write\n");
10901090
if (!mp_bluetooth_is_active()) {
10911091
return ERRNO_BLUETOOTH_NOT_ACTIVE;
10921092
}
1093+
if (send_update) {
1094+
return MP_EOPNOTSUPP;
1095+
}
10931096
return mp_bluetooth_gatts_db_write(MP_STATE_PORT(bluetooth_btstack_root_pointers)->gatts_db, value_handle, value, value_len);
10941097
}
10951098

extmod/modbluetooth.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -717,15 +717,17 @@ STATIC mp_obj_t bluetooth_ble_gatts_read(mp_obj_t self_in, mp_obj_t value_handle
717717
}
718718
STATIC MP_DEFINE_CONST_FUN_OBJ_2(bluetooth_ble_gatts_read_obj, bluetooth_ble_gatts_read);
719719

720-
STATIC mp_obj_t bluetooth_ble_gatts_write(mp_obj_t self_in, mp_obj_t value_handle_in, mp_obj_t data) {
721-
(void)self_in;
720+
STATIC mp_obj_t bluetooth_ble_gatts_write(size_t n_args, const mp_obj_t *args) {
722721
mp_buffer_info_t bufinfo = {0};
723-
mp_get_buffer_raise(data, &bufinfo, MP_BUFFER_READ);
724-
int err = mp_bluetooth_gatts_write(mp_obj_get_int(value_handle_in), bufinfo.buf, bufinfo.len);
725-
bluetooth_handle_errno(err);
722+
mp_get_buffer_raise(args[2], &bufinfo, MP_BUFFER_READ);
723+
bool send_update = false;
724+
if (n_args > 3) {
725+
send_update = mp_obj_is_true(args[3]);
726+
}
727+
bluetooth_handle_errno(mp_bluetooth_gatts_write(mp_obj_get_int(args[1]), bufinfo.buf, bufinfo.len, send_update));
726728
return MP_OBJ_NEW_SMALL_INT(bufinfo.len);
727729
}
728-
STATIC MP_DEFINE_CONST_FUN_OBJ_3(bluetooth_ble_gatts_write_obj, bluetooth_ble_gatts_write);
730+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bluetooth_ble_gatts_write_obj, 3, 4, bluetooth_ble_gatts_write);
729731

730732
STATIC mp_obj_t bluetooth_ble_gatts_notify(size_t n_args, const mp_obj_t *args) {
731733
mp_int_t conn_handle = mp_obj_get_int(args[1]);

extmod/modbluetooth.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,8 @@ int mp_bluetooth_gatts_register_service_end(void);
334334

335335
// Read the value from the local gatts db (likely this has been written by a central).
336336
int mp_bluetooth_gatts_read(uint16_t value_handle, uint8_t **value, size_t *value_len);
337-
// Write a value to the local gatts db (ready to be queried by a central).
338-
int mp_bluetooth_gatts_write(uint16_t value_handle, const uint8_t *value, size_t value_len);
337+
// Write a value to the local gatts db (ready to be queried by a central). Optionally send notifications/indications.
338+
int mp_bluetooth_gatts_write(uint16_t value_handle, const uint8_t *value, size_t value_len, bool send_update);
339339
// Notify the central that it should do a read.
340340
int mp_bluetooth_gatts_notify(uint16_t conn_handle, uint16_t value_handle);
341341
// Notify the central, including a data payload. (Note: does not set the gatts db value).

extmod/nimble/modbluetooth_nimble.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,11 @@ STATIC int central_gap_event_cb(struct ble_gap_event *event, void *arg) {
512512

513513
return 0;
514514
}
515+
516+
case BLE_GAP_EVENT_SUBSCRIBE: {
517+
DEBUG_printf("central_gap_event_cb: subscribe: handle=%d, reason=%d notify=%d indicate=%d \n", event->subscribe.attr_handle, event->subscribe.reason, event->subscribe.cur_notify, event->subscribe.cur_indicate);
518+
return 0;
519+
}
515520
}
516521

517522
return commmon_gap_event_cb(event, arg);
@@ -1004,11 +1009,15 @@ int mp_bluetooth_gatts_read(uint16_t value_handle, uint8_t **value, size_t *valu
10041009
return mp_bluetooth_gatts_db_read(MP_STATE_PORT(bluetooth_nimble_root_pointers)->gatts_db, value_handle, value, value_len);
10051010
}
10061011

1007-
int mp_bluetooth_gatts_write(uint16_t value_handle, const uint8_t *value, size_t value_len) {
1012+
int mp_bluetooth_gatts_write(uint16_t value_handle, const uint8_t *value, size_t value_len, bool send_update) {
10081013
if (!mp_bluetooth_is_active()) {
10091014
return ERRNO_BLUETOOTH_NOT_ACTIVE;
10101015
}
1011-
return mp_bluetooth_gatts_db_write(MP_STATE_PORT(bluetooth_nimble_root_pointers)->gatts_db, v D7D1 alue_handle, value, value_len);
1016+
int err = mp_bluetooth_gatts_db_write(MP_STATE_PORT(bluetooth_nimble_root_pointers)->gatts_db, value_handle, value, value_len);
1017+
if (err == 0 && send_update) {
1018+
ble_gatts_chr_updated(value_handle);
1019+
}
1020+
return err;
10121021
}
10131022

10141023
// TODO: Could use ble_gatts_chr_updated to send to all subscribed centrals.

ports/zephyr/modbluetooth_zephyr.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,10 +308,13 @@ int mp_bluetooth_gatts_read(uint16_t value_handle, uint8_t **value, size_t *valu
308308
return mp_bluetooth_gatts_db_read(MP_STATE_PORT(bluetooth_zephyr_root_pointers)->gatts_db, value_handle, value, value_len);
309309
}
310310

311-
int mp_bluetooth_gatts_write(uint16_t value_handle, const uint8_t *value, size_t value_len) {
311+
int mp_bluetooth_gatts_write(uint16_t value_handle, const uint8_t *value, size_t value_len, bool send_update) {
312312
if (!mp_bluetooth_is_active()) {
313313
return ERRNO_BLUETOOTH_NOT_ACTIVE;
314314
}
315+
if (send_update) {
316+
return MP_EOPNOTSUPP;
317+
}
315318
return mp_bluetooth_gatts_db_write(MP_STATE_PORT(bluetooth_zephyr_root_pointers)->gatts_db, value_handle, value, value_len);
316319
}
317320

0 commit comments

Comments
 (0)
0