diff options
| author | Dan Halbert <halbert@halwitz.org> | 2019-06-29 00:20:06 -0400 |
|---|---|---|
| committer | Dan Halbert <halbert@halwitz.org> | 2019-06-29 00:20:06 -0400 |
| commit | 6ea01ea9b0334a175419f9d449be25a5838627da (patch) | |
| tree | ca91eaa919c301696f1543f336dcaa6c26505345 /shared-bindings | |
| parent | 140904ec84ad1900f280c1febaa89a499c42b8c5 (diff) | |
Central is connecting; characteristics can be read and written
Diffstat (limited to 'shared-bindings')
| -rw-r--r-- | shared-bindings/bleio/Central.c | 59 | ||||
| -rw-r--r-- | shared-bindings/bleio/Central.h | 4 | ||||
| -rw-r--r-- | shared-bindings/bleio/Characteristic.c | 17 | ||||
| -rw-r--r-- | shared-bindings/bleio/Peripheral.c | 4 | ||||
| -rw-r--r-- | shared-bindings/bleio/Service.c | 17 | ||||
| -rw-r--r-- | shared-bindings/bleio/UUID.c | 3 | ||||
| -rw-r--r-- | shared-bindings/bleio/__init__.c | 5 |
7 files changed, 82 insertions, 27 deletions
diff --git a/shared-bindings/bleio/Central.c b/shared-bindings/bleio/Central.c index b3be90606..dd2dd0d50 100644 --- a/shared-bindings/bleio/Central.c +++ b/shared-bindings/bleio/Central.c @@ -64,43 +64,68 @@ //| central.connect(10.0) # timeout after 10 seconds //| -//| .. class:: Central(address) +//| .. class:: Central() //| //| Create a new Central object. -//| :param bleio.Address address: The address of the central to connect to //| STATIC mp_obj_t bleio_central_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { - mp_arg_check_num(n_args, kw_args, 1, 1, false); + mp_arg_check_num(n_args, kw_args, 0, 0, false); bleio_central_obj_t *self = m_new_obj(bleio_central_obj_t); self->base.type = &bleio_central_type; - const mp_obj_t address_obj = pos_args[0]; - if (!MP_OBJ_IS_TYPE(address_obj, &bleio_address_type)) { - mp_raise_ValueError(translate("Expected an Address")); - } - - bleio_address_obj_t *address = MP_OBJ_TO_PTR(address_obj); - common_hal_bleio_central_construct(self, address); + common_hal_bleio_central_construct(self); return MP_OBJ_FROM_PTR(self); } -//| .. method:: connect() -//| +//| .. method:: connect(address, timeout, *, service_uuids=None) //| Attempts a connection to the remote peripheral. If the connection is successful, +//| Do BLE discovery for the listed services, to find their handles and characteristics. +//| The attribute `remote_services` will contain a list of all discovered services. //| -STATIC mp_obj_t bleio_central_connect(mp_obj_t self_in, mp_obj_t timeout_in) { - bleio_central_obj_t *self = MP_OBJ_TO_PTR(self_in); +//| :param bleio.Address address: The address of the peripheral to connect to +//| :param float/int timeout: Try to connect for timeout seconds. +//| :param iterable service_uuids: a collection of `UUID` objects for the services +//| provided by the peripheral that you want to use. +//| The peripheral may provide more services, but services not listed are ignored. +//| If a service in `service_uuids` is not found during discovery, it will not +//| appear in `remote_services`. +//| +//| If `services` is None, then all services will undergo discovery, which can be slow. +//| +//| If the service UUID is 128-bit, or its characteristic UUID's are 128-bit, you +//| you must have already created a `UUID` object for that UUID in order for the +//| service or characteristic to be discovered. (This restriction may be lifted in the future.) +//| +STATIC mp_obj_t bleio_central_connect(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + bleio_central_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); + + enum { ARG_address, ARG_timeout, ARG_service_uuids }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_address, MP_ARG_REQUIRED | MP_ARG_OBJ }, + { MP_QSTR_timeout, MP_ARG_REQUIRED | MP_ARG_OBJ }, + { MP_QSTR_service_uuids, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} }, + }; + + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + if (!MP_OBJ_IS_TYPE(args[ARG_address].u_obj, &bleio_address_type)) { + mp_raise_ValueError(translate("Expected an Address")); + } + + bleio_address_obj_t *address = MP_OBJ_TO_PTR(args[ARG_address].u_obj); + mp_float_t timeout = mp_obj_get_float(args[ARG_timeout].u_obj); - mp_float_t timeout = mp_obj_float_get(timeout_in); - common_hal_bleio_central_connect(self, timeout); + // common_hal_bleio_central_connect() will validate that services is an iterable or None. + common_hal_bleio_central_connect(self, address, timeout, args[ARG_service_uuids].u_obj); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_2(bleio_central_connect_obj, bleio_central_connect); +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(bleio_central_connect_obj, 3, bleio_central_connect); //| .. method:: disconnect() diff --git a/shared-bindings/bleio/Central.h b/shared-bindings/bleio/Central.h index c6fb50bc0..542ba8fa2 100644 --- a/shared-bindings/bleio/Central.h +++ b/shared-bindings/bleio/Central.h @@ -33,8 +33,8 @@ extern const mp_obj_type_t bleio_central_type; -extern void common_hal_bleio_central_construct(bleio_central_obj_t *self, bleio_address_obj_t *address); -extern void common_hal_bleio_central_connect(bleio_central_obj_t *self, mp_float_t timeout); +extern void common_hal_bleio_central_construct(bleio_central_obj_t *self); +extern void common_hal_bleio_central_connect(bleio_central_obj_t *self, bleio_address_obj_t *address, mp_float_t timeout, mp_obj_t service_uuids); extern void common_hal_bleio_central_disconnect(bleio_central_obj_t *self); extern mp_obj_t common_hal_bleio_central_get_remote_services(bleio_central_obj_t *self); diff --git a/shared-bindings/bleio/Characteristic.c b/shared-bindings/bleio/Characteristic.c index 050992bb8..ede38c86b 100644 --- a/shared-bindings/bleio/Characteristic.c +++ b/shared-bindings/bleio/Characteristic.c @@ -205,11 +205,13 @@ const mp_obj_property_t bleio_characteristic_write_no_response_obj = { //| .. attribute:: uuid //| //| The UUID of this characteristic. (read-only) +//| Will be ``None`` if the 128-bit UUID for this characteristic is not known. //| STATIC mp_obj_t bleio_characteristic_get_uuid(mp_obj_t self_in) { bleio_characteristic_obj_t *self = MP_OBJ_TO_PTR(self_in); - return MP_OBJ_FROM_PTR(common_hal_bleio_characteristic_get_uuid(self)); + bleio_uuid_obj_t *uuid = common_hal_bleio_characteristic_get_uuid(self); + return uuid ? MP_OBJ_FROM_PTR(uuid) : mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_characteristic_get_uuid_obj, bleio_characteristic_get_uuid); @@ -262,12 +264,23 @@ STATIC const mp_rom_map_elem_t bleio_characteristic_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&bleio_characteristic_write_obj) }, { MP_ROM_QSTR(MP_QSTR_write_no_response), MP_ROM_PTR(&bleio_characteristic_write_no_response_obj) }, }; - STATIC MP_DEFINE_CONST_DICT(bleio_characteristic_locals_dict, bleio_characteristic_locals_dict_table); +STATIC void bleio_characteristic_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { + bleio_characteristic_obj_t *self = MP_OBJ_TO_PTR(self_in); + mp_printf(print, "Characteristic("); + if (self->uuid) { + bleio_uuid_print(print, MP_OBJ_FROM_PTR(self->uuid), kind); + } else { + mp_printf(print, "Unregistered uUID"); + } + mp_printf(print, ")"); +} + const mp_obj_type_t bleio_characteristic_type = { { &mp_type_type }, .name = MP_QSTR_Characteristic, .make_new = bleio_characteristic_make_new, + .print = bleio_characteristic_print, .locals_dict = (mp_obj_dict_t*)&bleio_characteristic_locals_dict }; diff --git a/shared-bindings/bleio/Peripheral.c b/shared-bindings/bleio/Peripheral.c index 002999b5d..37a765ae9 100644 --- a/shared-bindings/bleio/Peripheral.c +++ b/shared-bindings/bleio/Peripheral.c @@ -113,7 +113,7 @@ STATIC mp_obj_t bleio_peripheral_make_new(const mp_obj_type_t *type, size_t n_ar mp_obj_t service; while ((service = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) { if (!MP_OBJ_IS_TYPE(service, &bleio_service_type)) { - mp_raise_ValueError(translate("services includes an object that is not a Service")); + mp_raise_ValueError(translate("non-Service found in services")); } mp_obj_list_append(service_list, service); } @@ -237,7 +237,7 @@ STATIC mp_obj_t bleio_peripheral_start_advertising(mp_uint_t n_args, const mp_ob return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_KW(bleio_peripheral_start_advertising_obj, 0, bleio_peripheral_start_advertising); +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(bleio_peripheral_start_advertising_obj, 2, bleio_peripheral_start_advertising); //| .. method:: stop_advertising() //| diff --git a/shared-bindings/bleio/Service.c b/shared-bindings/bleio/Service.c index e687019d1..e6bb7dcc3 100644 --- a/shared-bindings/bleio/Service.c +++ b/shared-bindings/bleio/Service.c @@ -140,11 +140,13 @@ const mp_obj_property_t bleio_service_secondary_obj = { //| .. attribute:: uuid //| //| The UUID of this service. (read-only) +//| Will be ``None`` if the 128-bit UUID for this service is not known. //| STATIC mp_obj_t bleio_service_get_uuid(mp_obj_t self_in) { bleio_service_obj_t *self = MP_OBJ_TO_PTR(self_in); - return MP_OBJ_FROM_PTR(common_hal_bleio_service_get_uuid(self)); + bleio_uuid_obj_t *uuid = common_hal_bleio_service_get_uuid(self); + return uuid ? MP_OBJ_FROM_PTR(uuid) : mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_service_get_uuid_obj, bleio_service_get_uuid); @@ -160,12 +162,23 @@ STATIC const mp_rom_map_elem_t bleio_service_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_secondary), MP_ROM_PTR(&bleio_service_secondary_obj) }, { MP_ROM_QSTR(MP_QSTR_uuid), MP_ROM_PTR(&bleio_service_uuid_obj) }, }; - STATIC MP_DEFINE_CONST_DICT(bleio_service_locals_dict, bleio_service_locals_dict_table); +STATIC void bleio_service_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { + bleio_service_obj_t *self = MP_OBJ_TO_PTR(self_in); + mp_printf(print, "Service("); + if (self->uuid) { + bleio_uuid_print(print, MP_OBJ_FROM_PTR(self->uuid), kind); + } else { + mp_printf(print, "unregistered UUID"); + } + mp_printf(print, ")"); +} + const mp_obj_type_t bleio_service_type = { { &mp_type_type }, .name = MP_QSTR_Service, .make_new = bleio_service_make_new, + .print = bleio_service_print, .locals_dict = (mp_obj_dict_t*)&bleio_service_locals_dict }; diff --git a/shared-bindings/bleio/UUID.c b/shared-bindings/bleio/UUID.c index 0db7435f4..dca7ea010 100644 --- a/shared-bindings/bleio/UUID.c +++ b/shared-bindings/bleio/UUID.c @@ -50,6 +50,9 @@ //| - a buffer object (bytearray, bytes) of 16 bytes in little-endian order (128-bit UUID) //| - a string of hex digits of the form 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' //| +//| Creating a 128-bit UUID registers the UUID with the onboard BLE software, and provides a +//| temporary 16-bit UUID that can be used in place of the full 128-bit UUID. +//| //| :param value: The uuid value to encapsulate //| :type value: int or typing.ByteString //| diff --git a/shared-bindings/bleio/__init__.c b/shared-bindings/bleio/__init__.c index ca45a846e..9e80b49e3 100644 --- a/shared-bindings/bleio/__init__.c +++ b/shared-bindings/bleio/__init__.c @@ -28,9 +28,10 @@ #include "shared-bindings/bleio/__init__.h" #include "shared-bindings/bleio/Address.h" +#include "shared-bindings/bleio/Central.h" #include "shared-bindings/bleio/Characteristic.h" #include "shared-bindings/bleio/CharacteristicBuffer.h" -#include "shared-bindings/bleio/Descriptor.h" +// #include "shared-bindings/bleio/Descriptor.h" #include "shared-bindings/bleio/Peripheral.h" #include "shared-bindings/bleio/ScanEntry.h" #include "shared-bindings/bleio/Scanner.h" @@ -74,7 +75,7 @@ STATIC const mp_rom_map_elem_t bleio_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_bleio) }, { MP_ROM_QSTR(MP_QSTR_Address), MP_ROM_PTR(&bleio_address_type) }, -// { MP_ROM_QSTR(MP_QSTR_Central), MP_ROM_PTR(&bleio_central_type) }, + { MP_ROM_QSTR(MP_QSTR_Central), MP_ROM_PTR(&bleio_central_type) }, { MP_ROM_QSTR(MP_QSTR_Characteristic), MP_ROM_PTR(&bleio_characteristic_type) }, { MP_ROM_QSTR(MP_QSTR_CharacteristicBuffer), MP_ROM_PTR(&bleio_characteristic_buffer_type) }, // { MP_ROM_QSTR(MP_QSTR_Descriptor), MP_ROM_PTR(&bleio_descriptor_type) }, |
