aboutsummaryrefslogtreecommitdiff
path: root/extmod/modbluetooth.c
diff options
context:
space:
mode:
authorJim Mussared <jim.mussared@gmail.com>2020-09-11 18:12:36 +1000
committerJim Mussared <jim.mussared@gmail.com>2020-09-18 12:51:21 +1000
commit857e2c8fd54bc1d5ec3d3c0a38e4ac989e91413a (patch)
tree6aec87ed86a6bcbfcac79c99ad1994b981b49096 /extmod/modbluetooth.c
parentf271b96b5c8d6a4abedc81459a1c5710eb187abb (diff)
extmod/modbluetooth: Implement MTU.
Diffstat (limited to 'extmod/modbluetooth.c')
-rw-r--r--extmod/modbluetooth.c28
1 files changed, 28 insertions, 0 deletions
diff --git a/extmod/modbluetooth.c b/extmod/modbluetooth.c
index f77c7eb63..daf9cd0d1 100644
--- a/extmod/modbluetooth.c
+++ b/extmod/modbluetooth.c
@@ -316,6 +316,8 @@ STATIC mp_obj_t bluetooth_ble_config(size_t n_args, const mp_obj_t *args, mp_map
}
case MP_QSTR_rxbuf:
return mp_obj_new_int(self->ringbuf.size);
+ case MP_QSTR_mtu:
+ return mp_obj_new_int(mp_bluetooth_get_preferred_mtu());
default:
mp_raise_ValueError(MP_ERROR_TEXT("unknown config param"));
}
@@ -368,6 +370,11 @@ STATIC mp_obj_t bluetooth_ble_config(size_t n_args, const mp_obj_t *args, mp_map
m_del(uint8_t, old_irq_data_buf, old_irq_data_alloc);
break;
}
+ case MP_QSTR_mtu: {
+ mp_int_t mtu = mp_obj_get_int(e->value);
+ bluetooth_handle_errno(mp_bluetooth_set_preferred_mtu(mtu));
+ break;
+ }
case MP_QSTR_addr_mode: {
mp_int_t addr_mode = mp_obj_get_int(e->value);
mp_bluetooth_set_address_mode(addr_mode);
@@ -770,6 +777,13 @@ STATIC mp_obj_t bluetooth_ble_gattc_write(size_t n_args, const mp_obj_t *args) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bluetooth_ble_gattc_write_obj, 4, 5, bluetooth_ble_gattc_write);
+STATIC mp_obj_t bluetooth_ble_gattc_exchange_mtu(mp_obj_t self_in, mp_obj_t conn_handle_in) {
+ (void)self_in;
+ uint16_t conn_handle = mp_obj_get_int(conn_handle_in);
+ return bluetooth_handle_errno(mp_bluetooth_gattc_exchange_mtu(conn_handle));
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_2(bluetooth_ble_gattc_exchange_mtu_obj, bluetooth_ble_gattc_exchange_mtu);
+
#endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
// ----------------------------------------------------------------------------
@@ -802,6 +816,7 @@ STATIC const mp_rom_map_elem_t bluetooth_ble_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_gattc_discover_descriptors), MP_ROM_PTR(&bluetooth_ble_gattc_discover_descriptors_obj) },
{ MP_ROM_QSTR(MP_QSTR_gattc_read), MP_ROM_PTR(&bluetooth_ble_gattc_read_obj) },
{ MP_ROM_QSTR(MP_QSTR_gattc_write), MP_ROM_PTR(&bluetooth_ble_gattc_write_obj) },
+ { MP_ROM_QSTR(MP_QSTR_gattc_exchange_mtu), MP_ROM_PTR(&bluetooth_ble_gattc_exchange_mtu_obj) },
#endif
};
STATIC MP_DEFINE_CONST_DICT(bluetooth_ble_locals_dict, bluetooth_ble_locals_dict_table);
@@ -911,6 +926,9 @@ STATIC mp_obj_t bluetooth_ble_invoke_irq(mp_obj_t none_in) {
} else if (event == MP_BLUETOOTH_IRQ_GATTS_INDICATE_DONE) {
// conn_handle, value_handle, status
ringbuf_extract(&o->ringbuf, data_tuple, 2, 1, NULL, 0, NULL, NULL);
+ } else if (event == MP_BLUETOOTH_IRQ_MTU_EXCHANGED) {
+ // conn_handle, mtu
+ ringbuf_extract(&o->ringbuf, data_tuple, 2, 0, NULL, 0, NULL, NULL);
#if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
} else if (event == MP_BLUETOOTH_IRQ_SCAN_RESULT) {
// addr_type, addr, adv_type, rssi, adv_data
@@ -1184,6 +1202,16 @@ bool mp_bluetooth_gatts_on_read_request(uint16_t conn_handle, uint16_t value_han
}
#endif
+void mp_bluetooth_gatts_on_mtu_exchanged(uint16_t conn_handle, uint16_t value) {
+ MICROPY_PY_BLUETOOTH_ENTER
+ mp_obj_bluetooth_ble_t *o = MP_OBJ_TO_PTR(MP_STATE_VM(bluetooth));
+ if (enqueue_irq(o, 2 + 2, MP_BLUETOOTH_IRQ_MTU_EXCHANGED)) {
+ ringbuf_put16(&o->ringbuf, conn_handle);
+ ringbuf_put16(&o->ringbuf, value);
+ }
+ schedule_ringbuf(atomic_state);
+}
+
void mp_bluetooth_gatts_db_create_entry(mp_gatts_db_t db, uint16_t handle, size_t len) {
mp_map_elem_t *elem = mp_map_lookup(db, MP_OBJ_NEW_SMALL_INT(handle), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
mp_bluetooth_gatts_db_entry_t *entry = m_new(mp_bluetooth_gatts_db_entry_t, 1);