From 293166f3c518308d74670c78f48fb87811b9e7ad Mon Sep 17 00:00:00 2001 From: Marceau Fillon Date: Tue, 7 Sep 2021 14:02:11 +1000 Subject: [PATCH 1/3] Add function to convert to Nimble uuid and get the services handle --- extmod/modbluetooth.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/extmod/modbluetooth.c b/extmod/modbluetooth.c index cb153f70e9b5c..96f893d087dd8 100644 --- a/extmod/modbluetooth.c +++ b/extmod/modbluetooth.c @@ -35,6 +35,8 @@ #include "py/qstr.h" #include "py/runtime.h" #include "extmod/modbluetooth.h" +#include "nimble/host/include/host/ble_att.h" +#include "nimble/host/include/host/ble_gatt.h" #include #if MICROPY_PY_BLUETOOTH @@ -215,6 +217,28 @@ STATIC mp_int_t bluetooth_uuid_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bu return 0; } +// Note: modbluetooth UUIDs store their data in LE. +STATIC ble_uuid_t *create_nimble_uuid(const mp_obj_bluetooth_uuid_t *uuid, ble_uuid_any_t *storage) { + if (uuid->type == MP_BLUETOOTH_UUID_TYPE_16) { + ble_uuid16_t *result = storage ? &storage->u16 : m_new(ble_uuid16_t, 1); + result->u.type = BLE_UUID_TYPE_16; + result->value = (uuid->data[1] << 8) | uuid->data[0]; + return (ble_uuid_t *)result; + } else if (uuid->type == MP_BLUETOOTH_UUID_TYPE_32) { + ble_uuid32_t *result = storage ? &storage->u32 : m_new(ble_uuid32_t, 1); + result->u.type = BLE_UUID_TYPE_32; + result->value = (uuid->data[1] << 24) | (uuid->data[1] << 16) | (uuid->data[1] << 8) | uuid->data[0]; + return (ble_uuid_t *)result; + } else if (uuid->type == MP_BLUETOOTH_UUID_TYPE_128) { + ble_uuid128_t *result = storage ? &storage->u128 : m_new(ble_uuid128_t, 1); + result->u.type = BLE_UUID_TYPE_128; + memcpy(result->value, uuid->data, 16); + return (ble_uuid_t *)result; + } else { + return NULL; + } +} + #if !MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS && MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE STATIC void ringbuf_put_uuid(ringbuf_t *ringbuf, mp_obj_bluetooth_uuid_t *uuid) { assert(ringbuf_free(ringbuf) >= (size_t)uuid->type + 1); @@ -628,6 +652,19 @@ STATIC mp_obj_t bluetooth_ble_gatts_register_services(mp_obj_t self_in, mp_obj_t } STATIC MP_DEFINE_CONST_FUN_OBJ_2(bluetooth_ble_gatts_register_services_obj, bluetooth_ble_gatts_register_services); +STATIC mp_obj_t bluetooth_ble_gatts_get_service_handle(mp_obj_t self_in, mp_obj_t service_uuid_in){ + (void)self_in; + + const mp_obj_bluetooth_uuid_t *service_uuid = MP_OBJ_TO_PTR(service_uuid_in); + const ble_uuid_t *service_nimble_uuid = create_nimble_uuid(service_uuid, NULL); + uint16_t service_handle = 0; + + ble_gatts_find_svc(service_nimble_uuid, &service_handle); + + return mp_obj_new_int(service_handle); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_2(bluetooth_ble_gatts_get_service_handle_obj, bluetooth_ble_gatts_get_service_handle); + #if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE STATIC mp_obj_t bluetooth_ble_gap_connect(size_t n_args, const mp_obj_t *args) { uint8_t addr_type = mp_obj_get_int(args[1]); @@ -935,6 +972,7 @@ STATIC const mp_rom_map_elem_t bluetooth_ble_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_gatts_notify), MP_ROM_PTR(&bluetooth_ble_gatts_notify_obj) }, { MP_ROM_QSTR(MP_QSTR_gatts_indicate), MP_ROM_PTR(&bluetooth_ble_gatts_indicate_obj) }, { MP_ROM_QSTR(MP_QSTR_gatts_set_buffer), MP_ROM_PTR(&bluetooth_ble_gatts_set_buffer_obj) }, + { MP_ROM_QSTR(MP_QSTR_gatts_get_service_handle), MP_ROM_PTR(&bluetooth_ble_gatts_get_service_handle_obj) }, #if MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT // GATT Client { MP_ROM_QSTR(MP_QSTR_gattc_discover_services), MP_ROM_PTR(&bluetooth_ble_gattc_discover_services_obj) }, From c0ab63f896767832c710c4bc942bcaed5b0866dc Mon Sep 17 00:00:00 2001 From: Marceau Fillon Date: Fri, 10 Sep 2021 09:22:12 +1000 Subject: [PATCH 2/3] Extmod/bluetooth: Generic function to get services handles --- extmod/modbluetooth.c | 27 +-------------------------- extmod/modbluetooth.h | 3 +++ extmod/nimble/modbluetooth_nimble.c | 9 +++++++++ 3 files changed, 13 insertions(+), 26 deletions(-) diff --git a/extmod/modbluetooth.c b/extmod/modbluetooth.c index 96f893d087dd8..bc8f20c5fe26a 100644 --- a/extmod/modbluetooth.c +++ b/extmod/modbluetooth.c @@ -217,28 +217,6 @@ STATIC mp_int_t bluetooth_uuid_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bu return 0; } -// Note: modbluetooth UUIDs store their data in LE. -STATIC ble_uuid_t *create_nimble_uuid(const mp_obj_bluetooth_uuid_t *uuid, ble_uuid_any_t *storage) { - if (uuid->type == MP_BLUETOOTH_UUID_TYPE_16) { - ble_uuid16_t *result = storage ? &storage->u16 : m_new(ble_uuid16_t, 1); - result->u.type = BLE_UUID_TYPE_16; - result->value = (uuid->data[1] << 8) | uuid->data[0]; - return (ble_uuid_t *)result; - } else if (uuid->type == MP_BLUETOOTH_UUID_TYPE_32) { - ble_uuid32_t *result = storage ? &storage->u32 : m_new(ble_uuid32_t, 1); - result->u.type = BLE_UUID_TYPE_32; - result->value = (uuid->data[1] << 24) | (uuid->data[1] << 16) | (uuid->data[1] << 8) | uuid->data[0]; - return (ble_uuid_t *)result; - } else if (uuid->type == MP_BLUETOOTH_UUID_TYPE_128) { - ble_uuid128_t *result = storage ? &storage->u128 : m_new(ble_uuid128_t, 1); - result->u.type = BLE_UUID_TYPE_128; - memcpy(result->value, uuid->data, 16); - return (ble_uuid_t *)result; - } else { - return NULL; - } -} - #if !MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS && MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE STATIC void ringbuf_put_uuid(ringbuf_t *ringbuf, mp_obj_bluetooth_uuid_t *uuid) { assert(ringbuf_free(ringbuf) >= (size_t)uuid->type + 1); @@ -656,10 +634,7 @@ STATIC mp_obj_t bluetooth_ble_gatts_get_service_handle(mp_obj_t self_in, mp_obj_ (void)self_in; const mp_obj_bluetooth_uuid_t *service_uuid = MP_OBJ_TO_PTR(service_uuid_in); - const ble_uuid_t *service_nimble_uuid = create_nimble_uuid(service_uuid, NULL); - uint16_t service_handle = 0; - - ble_gatts_find_svc(service_nimble_uuid, &service_handle); + uint16_t service_handle = mp_bluetooth_ble_gatts_get_service_handle(service_uuid); return mp_obj_new_int(service_handle); } diff --git a/extmod/modbluetooth.h b/extmod/modbluetooth.h index 43519e5941a8b..a02375528f6d0 100644 --- a/extmod/modbluetooth.h +++ b/extmod/modbluetooth.h @@ -347,6 +347,9 @@ int mp_bluetooth_gatts_indicate(uint16_t conn_handle, uint16_t value_handle); // Append-mode means that remote writes will append and local reads will clear after reading. int mp_bluetooth_gatts_set_buffer(uint16_t value_handle, size_t len, bool append); +// Return the value handle of a service identified from its uuid. +uint16_t mp_bluetooth_ble_gatts_get_service_handle(const mp_obj_bluetooth_uuid_t *service_uuid_in); + // Disconnect from a central or peripheral. int mp_bluetooth_gap_disconnect(uint16_t conn_handle); diff --git a/extmod/nimble/modbluetooth_nimble.c b/extmod/nimble/modbluetooth_nimble.c index e4b4cb68af1af..9ba7971074b9d 100644 --- a/extmod/nimble/modbluetooth_nimble.c +++ b/extmod/nimble/modbluetooth_nimble.c @@ -1075,6 +1075,15 @@ int mp_bluetooth_set_preferred_mtu(uint16_t mtu) { return 0; } +uint16_t mp_bluetooth_ble_gatts_get_service_handle(const mp_obj_bluetooth_uuid_t *service_uuid_in){ + const ble_uuid_t *service_nimble_uuid = create_nimble_uuid(service_uuid_in, NULL); + uint16_t service_handle = 0; + + ble_gatts_find_svc(service_nimble_uuid, &service_handle); + + return service_handle; +} + #if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING int mp_bluetooth_gap_pair(uint16_t conn_handle) { DEBUG_printf("mp_bluetooth_gap_pair: conn_handle=%d\n", conn_handle); From 8a3290f6b4fd2050682b7b912b5da7a7f4844d2b Mon Sep 17 00:00:00 2001 From: Marceau Fillon Date: Fri, 10 Sep 2021 09:37:47 +1000 Subject: [PATCH 3/3] Remove unused and not generic imports --- extmod/modbluetooth.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/extmod/modbluetooth.c b/extmod/modbluetooth.c index bc8f20c5fe26a..95e87d013df8f 100644 --- a/extmod/modbluetooth.c +++ b/extmod/modbluetooth.c @@ -35,8 +35,6 @@ #include "py/qstr.h" #include "py/runtime.h" #include "extmod/modbluetooth.h" -#include "nimble/host/include/host/ble_att.h" -#include "nimble/host/include/host/ble_gatt.h" #include #if MICROPY_PY_BLUETOOTH pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy