From 4881e1ff55768c027404cc6a1375c43d4c3acf2a Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Fri, 21 Jun 2019 18:04:04 -0400 Subject: WIP: Central compiles; now will test --- shared-bindings/bleio/Central.c | 67 +++++++--------------------- shared-bindings/bleio/Central.h | 8 ++-- shared-bindings/bleio/Characteristic.c | 8 ++-- shared-bindings/bleio/CharacteristicBuffer.c | 2 +- shared-bindings/bleio/Device.h | 42 ----------------- shared-bindings/bleio/Peripheral.c | 6 +-- shared-bindings/bleio/Service.c | 24 +++++----- 7 files changed, 42 insertions(+), 115 deletions(-) delete mode 100644 shared-bindings/bleio/Device.h (limited to 'shared-bindings') diff --git a/shared-bindings/bleio/Central.c b/shared-bindings/bleio/Central.c index e5fd66348..b3be90606 100644 --- a/shared-bindings/bleio/Central.c +++ b/shared-bindings/bleio/Central.c @@ -35,11 +35,10 @@ #include "py/objstr.h" #include "py/runtime.h" #include "shared-bindings/bleio/Adapter.h" -#include "shared-bindings/bleio/AddressType.h" +#include "shared-bindings/bleio/Address.h" #include "shared-bindings/bleio/Characteristic.h" #include "shared-bindings/bleio/Central.h" #include "shared-bindings/bleio/Service.h" -#include "shared-bindings/bleio/UUID.h" //| .. currentmodule:: bleio //| @@ -62,44 +61,28 @@ //| break //| //| central = bleio.Central(my_entry.address) -//| central.connect() +//| central.connect(10.0) # timeout after 10 seconds //| -//| .. class:: Central(address=None, scan_entry=None) -//| -//| Create a new Central object. If the `address` or :py:data:`scan_entry` parameters are not `None`, -//| the role is set to Central, otherwise it's set to Peripheral. +//| .. class:: Central(address) //| +//| Create a new Central object. //| :param bleio.Address address: The address of the central to connect to -//| :param bleio.ScanEntry scan_entry: The scan entry returned from `bleio.Scanner` //| -STATIC mp_obj_t bleio_central_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *pos_args) { - mp_arg_check_num(n_args, n_kw, 0, 1, true); +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); + bleio_central_obj_t *self = m_new_obj(bleio_central_obj_t); self->base.type = &bleio_central_type; - self->service_list = mp_obj_new_list(0, NULL); - self->conn_handler = mp_const_none; - self->conn_handle = 0xFFFF; - - mp_map_t kw_args; - mp_map_init_fixed_table(&kw_args, n_kw, pos_args + n_args); - - enum { ARG_address, ARG_scan_entry }; - static const mp_arg_t allowed_args[] = { - { MP_QSTR_address, MP_ARG_OBJ, {.u_obj = mp_const_none} }, - { MP_QSTR_scan_entry, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = mp_const_none} }, - }; - - mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; - mp_arg_parse_all(n_args, pos_args, &kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); - - const mp_obj_t address_obj = args[ARG_address].u_obj; - const mp_obj_t scan_entry_obj = args[ARG_scan_entry].u_obj; + const mp_obj_t address_obj = pos_args[0]; if (!MP_OBJ_IS_TYPE(address_obj, &bleio_address_type)) { - mp_raise_ValueError("Expected an Address"); + 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); + return MP_OBJ_FROM_PTR(self); } @@ -109,19 +92,15 @@ STATIC mp_obj_t bleio_central_make_new(const mp_obj_type_t *type, size_t n_args, //| //| Attempts a connection to the remote peripheral. If the connection is successful, //| -STATIC mp_obj_t bleio_central_connect(mp_obj_t self_in) { +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); - if (self->is_peripheral) { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, - translate("Can't connect in Peripheral mode"))); - } - - common_hal_bleio_central_connect(self); + mp_float_t timeout = mp_obj_float_get(timeout_in); + common_hal_bleio_central_connect(self, timeout); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_central_connect_obj, bleio_central_connect); +STATIC MP_DEFINE_CONST_FUN_OBJ_2(bleio_central_connect_obj, bleio_central_connect); //| .. method:: disconnect() @@ -137,17 +116,6 @@ STATIC mp_obj_t bleio_central_disconnect(mp_obj_t self_in) { } STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_central_disconnect_obj, bleio_central_disconnect); -//| .. attribute:: remote_name (read-only) -//| -//| The name of the remote peripheral, if connected. May be None if no name was advertised. -//| -STATIC mp_obj_t bleio_central_get_remote_name(mp_obj_t self_in) { - bleio_central_obj_t *self = MP_OBJ_TO_PTR(self_in); - - return self->remote_name; -} -MP_DEFINE_CONST_FUN_OBJ_1(bleio_central_get_remote_name_obj, bleio_central_get_remote_name); - //| .. attribute:: remote_services (read-only) //| //| Empty until connected, then a list of services provided by the remote peripheral. @@ -155,7 +123,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(bleio_central_get_remote_name_obj, bleio_central_get_r STATIC mp_obj_t bleio_central_get_remote_services(mp_obj_t self_in) { bleio_central_obj_t *self = MP_OBJ_TO_PTR(self_in); - return self->service_list; + return common_hal_bleio_central_get_remote_services(self); } STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_central_get_remote_services_obj, bleio_central_get_remote_services); @@ -172,7 +140,6 @@ STATIC const mp_rom_map_elem_t bleio_central_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_disconnect), MP_ROM_PTR(&bleio_central_disconnect_obj) }, // Properties - { MP_ROM_QSTR(MP_QSTR_remote_name), MP_ROM_PTR(&bleio_central_remote_name_obj) }, { MP_ROM_QSTR(MP_QSTR_remote_services), MP_ROM_PTR(&bleio_central_remote_services_obj) }, }; diff --git a/shared-bindings/bleio/Central.h b/shared-bindings/bleio/Central.h index 5ea2119af..c6fb50bc0 100644 --- a/shared-bindings/bleio/Central.h +++ b/shared-bindings/bleio/Central.h @@ -31,9 +31,11 @@ #include "common-hal/bleio/Central.h" #include "common-hal/bleio/Service.h" -extern const mp_obj_type_t bleio_device_type; +extern const mp_obj_type_t bleio_central_type; -extern void common_hal_bleio_device_connect(bleio_device_obj_t *device); -extern void common_hal_bleio_device_disconnect(bleio_device_obj_t *device); +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_disconnect(bleio_central_obj_t *self); +extern mp_obj_t common_hal_bleio_central_get_remote_services(bleio_central_obj_t *self); #endif // MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_CENTRAL_H diff --git a/shared-bindings/bleio/Characteristic.c b/shared-bindings/bleio/Characteristic.c index 859611f91..050992bb8 100644 --- a/shared-bindings/bleio/Characteristic.c +++ b/shared-bindings/bleio/Characteristic.c @@ -69,12 +69,12 @@ STATIC mp_obj_t bleio_characteristic_make_new(const mp_obj_type_t *type, size_t mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); - const mp_obj_t uuid = args[ARG_uuid].u_obj; + const mp_obj_t uuid_obj = args[ARG_uuid].u_obj; - if (!MP_OBJ_IS_TYPE(uuid, &bleio_uuid_type)) { + if (!MP_OBJ_IS_TYPE(uuid_obj, &bleio_uuid_type)) { mp_raise_ValueError(translate("Expected a UUID")); } - bleio_uuid_obj_t *uuid_obj = MP_OBJ_TO_PTR(uuid); + bleio_uuid_obj_t *uuid = MP_OBJ_TO_PTR(uuid_obj); bleio_characteristic_obj_t *self = m_new_obj(bleio_characteristic_obj_t); self->base.type = &bleio_characteristic_type; @@ -88,7 +88,7 @@ STATIC mp_obj_t bleio_characteristic_make_new(const mp_obj_type_t *type, size_t properties.write = args[ARG_write].u_bool; properties.write_no_response = args[ARG_write_no_response].u_bool; - common_hal_bleio_characteristic_construct(self, uuid_obj, properties); + common_hal_bleio_characteristic_construct(self, uuid, properties); return MP_OBJ_FROM_PTR(self); } diff --git a/shared-bindings/bleio/CharacteristicBuffer.c b/shared-bindings/bleio/CharacteristicBuffer.c index da79c5d7a..827f4947b 100644 --- a/shared-bindings/bleio/CharacteristicBuffer.c +++ b/shared-bindings/bleio/CharacteristicBuffer.c @@ -86,7 +86,7 @@ STATIC mp_obj_t bleio_characteristic_buffer_make_new(const mp_obj_type_t *type, bleio_characteristic_buffer_obj_t *self = m_new_obj(bleio_characteristic_buffer_obj_t); self->base.type = &bleio_characteristic_buffer_type; - common_hal_bleio_characteristic_buffer_construct(self, characteristic, timeout, buffer_size); + common_hal_bleio_characteristic_buffer_construct(self, MP_OBJ_TO_PTR(characteristic), timeout, buffer_size); return MP_OBJ_FROM_PTR(self); } diff --git a/shared-bindings/bleio/Device.h b/shared-bindings/bleio/Device.h deleted file mode 100644 index d19b8c886..000000000 --- a/shared-bindings/bleio/Device.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2019 Dan Halbert for Adafruit Industries - * Copyright (c) 2018 Artur Pacholec - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_DEVICE_H -#define MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_DEVICE_H - -#include "shared-module/bleio/Device.h" -#include "common-hal/bleio/Service.h" - -extern const mp_obj_type_t bleio_device_type; - -extern void common_hal_bleio_device_add_service(bleio_device_obj_t *device, bleio_service_obj_t *service); -extern void common_hal_bleio_device_start_advertising(bleio_device_obj_t *device, bool connectable, mp_buffer_info_t *raw_data); -extern void common_hal_bleio_device_stop_advertising(bleio_device_obj_t *device); -extern void common_hal_bleio_device_connect(bleio_device_obj_t *device); -extern void common_hal_bleio_device_disconnect(bleio_device_obj_t *device); - -#endif // MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_DEVICE_H diff --git a/shared-bindings/bleio/Peripheral.c b/shared-bindings/bleio/Peripheral.c index e270c599c..002999b5d 100644 --- a/shared-bindings/bleio/Peripheral.c +++ b/shared-bindings/bleio/Peripheral.c @@ -107,8 +107,8 @@ STATIC mp_obj_t bleio_peripheral_make_new(const mp_obj_type_t *type, size_t n_ar self->base.type = &bleio_peripheral_type; // Copy the services list and validate its items. - mp_obj_t service_list = mp_obj_new_list(0, NULL); - mp_obj_list_t *service_list_obj = MP_OBJ_FROM_PTR(service_list); + mp_obj_t service_list_obj = mp_obj_new_list(0, NULL); + mp_obj_list_t *service_list = MP_OBJ_FROM_PTR(service_list_obj); mp_obj_t service; while ((service = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) { @@ -128,7 +128,7 @@ STATIC mp_obj_t bleio_peripheral_make_new(const mp_obj_type_t *type, size_t n_ar mp_raise_ValueError(translate("name must be a string")); } - common_hal_bleio_peripheral_construct(self, service_list_obj, name_str); + common_hal_bleio_peripheral_construct(self, service_list, name_str); return MP_OBJ_FROM_PTR(self); } diff --git a/shared-bindings/bleio/Service.c b/shared-bindings/bleio/Service.c index 2719164d8..e687019d1 100644 --- a/shared-bindings/bleio/Service.c +++ b/shared-bindings/bleio/Service.c @@ -61,9 +61,9 @@ STATIC mp_obj_t bleio_service_make_new(const mp_obj_type_t *type, size_t n_args, mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); - const mp_obj_t uuid = args[ARG_uuid].u_obj; + const mp_obj_t uuid_obj = args[ARG_uuid].u_obj; - if (!MP_OBJ_IS_TYPE(uuid, &bleio_uuid_type)) { + if (!MP_OBJ_IS_TYPE(uuid_obj, &bleio_uuid_type)) { mp_raise_ValueError(translate("Expected a UUID")); } @@ -71,31 +71,31 @@ STATIC mp_obj_t bleio_service_make_new(const mp_obj_type_t *type, size_t n_args, self->base.type = &bleio_service_type; const bool is_secondary = args[ARG_secondary].u_bool; - bleio_uuid_obj_t *uuid_obj = MP_OBJ_TO_PTR(uuid); + bleio_uuid_obj_t *uuid = MP_OBJ_TO_PTR(uuid_obj); // If characteristics is not an iterable, an exception will be thrown. mp_obj_iter_buf_t iter_buf; mp_obj_t iterable = mp_getiter(args[ARG_characteristics].u_obj, &iter_buf); - mp_obj_t characteristic; + mp_obj_t characteristic_obj; // Copy the characteristics list and validate its items. - mp_obj_t char_list = mp_obj_new_list(0, NULL); - mp_obj_list_t *char_list_obj = MP_OBJ_FROM_PTR(char_list); + mp_obj_t char_list_obj = mp_obj_new_list(0, NULL); + mp_obj_list_t *char_list = MP_OBJ_FROM_PTR(char_list); - while ((characteristic = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) { - if (!MP_OBJ_IS_TYPE(characteristic, &bleio_characteristic_type)) { + while ((characteristic_obj = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) { + if (!MP_OBJ_IS_TYPE(characteristic_obj, &bleio_characteristic_type)) { mp_raise_ValueError(translate("characteristics includes an object that is not a Characteristic")); } - bleio_characteristic_obj_t *characteristic_ptr = MP_OBJ_TO_PTR(characteristic); + bleio_characteristic_obj_t *characteristic = MP_OBJ_TO_PTR(characteristic_obj); if (common_hal_bleio_uuid_get_uuid128_reference(uuid) != - common_hal_bleio_uuid_get_uuid128_reference(characteristic_ptr->uuid)) { + common_hal_bleio_uuid_get_uuid128_reference(characteristic->uuid)) { // The descriptor base UUID doesn't match the characteristic base UUID. mp_raise_ValueError(translate("Characteristic UUID doesn't match Service UUID")); } - mp_obj_list_append(char_list, characteristic); + mp_obj_list_append(char_list_obj, characteristic_obj); } - common_hal_bleio_service_construct(self, uuid_obj, char_list_obj, is_secondary); + common_hal_bleio_service_construct(self, uuid, char_list, is_secondary); return MP_OBJ_FROM_PTR(self); } -- cgit v1.2.3