diff options
| author | Dan Halbert <halbert@halwitz.org> | 2019-06-18 23:46:20 -0400 |
|---|---|---|
| committer | Dan Halbert <halbert@halwitz.org> | 2019-06-18 23:46:20 -0400 |
| commit | 35b919185747c42e183ccfc41f99fd370acca450 (patch) | |
| tree | 5c1a383a99c53592c16daed8c61f84a4176d99f0 | |
| parent | 1356819de195810da09b5fc051ef5cc9dc21b2f4 (diff) | |
Don't operate directly on bleio objects in shared-bindings: use common_hal
routines instead. Changes made but not yet tested.
34 files changed, 294 insertions, 113 deletions
diff --git a/ports/nrf/common-hal/bleio/Broadcaster.h b/ports/nrf/common-hal/bleio/Broadcaster.h index 72f93a443..2d1ae69a1 100644 --- a/ports/nrf/common-hal/bleio/Broadcaster.h +++ b/ports/nrf/common-hal/bleio/Broadcaster.h @@ -25,8 +25,8 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_COMMON_HAL_BLEIO_BROADCASTER_H -#define MICROPY_INCLUDED_COMMON_HAL_BLEIO_BROADCASTER_H +#ifndef MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_BROADCASTER_H +#define MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_BROADCASTER_H #include "ble.h" @@ -44,4 +44,4 @@ typedef struct { } bleio_broadcaster_obj_t; -#endif // MICROPY_INCLUDED_COMMON_HAL_BLEIO_BROADCASTER_H +#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_BROADCASTER_H diff --git a/ports/nrf/common-hal/bleio/Characteristic.c b/ports/nrf/common-hal/bleio/Characteristic.c index 9409b73ce..45fb1d092 100644 --- a/ports/nrf/common-hal/bleio/Characteristic.c +++ b/ports/nrf/common-hal/bleio/Characteristic.c @@ -237,7 +237,11 @@ void common_hal_bleio_characteristic_construct(bleio_characteristic_obj_t *self, } -void common_hal_bleio_characteristic_get_value(bleio_characteristic_obj_t *self) { +void common_hal_bleio_characteristic_set_service(bleio_characteristic_obj_t *self, bleio_service_obj_t *service) { + self->service = service; +} + +mp_obj_t common_hal_bleio_characteristic_get_value(bleio_characteristic_obj_t *self) { switch (common_hal_bleio_device_get_gatt_role(self->service->device)) { case GATT_ROLE_CLIENT: gattc_read(self); @@ -251,6 +255,8 @@ void common_hal_bleio_characteristic_get_value(bleio_characteristic_obj_t *self) mp_raise_RuntimeError(translate("bad GATT role")); break; } + + return self->value_data; } void common_hal_bleio_characteristic_set_value(bleio_characteristic_obj_t *self, mp_buffer_info_t *bufinfo) { @@ -285,3 +291,11 @@ void common_hal_bleio_characteristic_set_value(bleio_characteristic_obj_t *self, break; } } + +bleio_uuid_obj_t *common_hal_bleio_characteristic_get_uuid(bleio_characteristic_obj_t *self) { + return self->uuid; +} + +bleio_characteristic_properties_t common_hal_bleio_characteristic_get_properties(bleio_characteristic_obj_t *self) { + return self->props; +} diff --git a/ports/nrf/common-hal/bleio/Characteristic.h b/ports/nrf/common-hal/bleio/Characteristic.h index bce1eec1d..662485bad 100644 --- a/ports/nrf/common-hal/bleio/Characteristic.h +++ b/ports/nrf/common-hal/bleio/Characteristic.h @@ -24,11 +24,11 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_COMMON_HAL_BLEIO_CHARACTERISTIC_H -#define MICROPY_INCLUDED_COMMON_HAL_BLEIO_CHARACTERISTIC_H +#ifndef MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_CHARACTERISTIC_H +#define MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_CHARACTERISTIC_H #include "shared-module/bleio/Characteristic.h" -#include "shared-module/bleio/Service.h" +#include "common-hal/bleio/Service.h" #include "common-hal/bleio/UUID.h" typedef struct { @@ -43,4 +43,4 @@ typedef struct { uint16_t sccd_handle; } bleio_characteristic_obj_t; -#endif // MICROPY_INCLUDED_COMMON_HAL_BLEIO_CHARACTERISTIC_H +#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_CHARACTERISTIC_H diff --git a/ports/nrf/common-hal/bleio/CharacteristicBuffer.h b/ports/nrf/common-hal/bleio/CharacteristicBuffer.h index b36f63fec..6a44229f7 100644 --- a/ports/nrf/common-hal/bleio/CharacteristicBuffer.h +++ b/ports/nrf/common-hal/bleio/CharacteristicBuffer.h @@ -24,8 +24,8 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_COMMON_HAL_BLEIO_CHARACTERISTICBUFFER_H -#define MICROPY_INCLUDED_COMMON_HAL_BLEIO_CHARACTERISTICBUFFER_H +#ifndef MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_CHARACTERISTICBUFFER_H +#define MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_CHARACTERISTICBUFFER_H #include "nrf_soc.h" @@ -40,4 +40,4 @@ typedef struct { ringbuf_t ringbuf; } bleio_characteristic_buffer_obj_t; -#endif // MICROPY_INCLUDED_COMMON_HAL_BLEIO_CHARACTERISTICBUFFER_H +#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_CHARACTERISTICBUFFER_H diff --git a/ports/nrf/common-hal/bleio/Device.c b/ports/nrf/common-hal/bleio/Device.c index 5f625829e..b1ac72e75 100644 --- a/ports/nrf/common-hal/bleio/Device.c +++ b/ports/nrf/common-hal/bleio/Device.c @@ -323,7 +323,7 @@ STATIC void on_primary_srv_discovery_rsp(ble_gattc_evt_prim_srvc_disc_rsp_t *res bleio_service_obj_t *service = m_new_obj(bleio_service_obj_t); service->base.type = &bleio_service_type; service->device = device; - service->char_list = mp_obj_new_list(0, NULL); + service->characteristic_list = mp_obj_new_list(0, NULL); service->start_handle = gattc_service->handle_range.start_handle; service->end_handle = gattc_service->handle_range.end_handle; service->handle = gattc_service->handle_range.start_handle; @@ -366,7 +366,7 @@ STATIC void on_char_discovery_rsp(ble_gattc_evt_char_disc_rsp_t *response, bleio characteristic->handle = gattc_char->handle_value; characteristic->service = m_char_discovery_service; - mp_obj_list_append(m_char_discovery_service->char_list, MP_OBJ_FROM_PTR(characteristic)); + mp_obj_list_append(m_char_discovery_service->characteristic_list, MP_OBJ_FROM_PTR(characteristic)); } if (response->count > 0) { @@ -491,9 +491,9 @@ void common_hal_bleio_device_add_service(bleio_device_obj_t *device, bleio_servi mp_raise_OSError_msg(translate("Failed to add service")); } - const mp_obj_list_t *char_list = MP_OBJ_TO_PTR(service->char_list); - for (size_t i = 0; i < char_list->len; ++i) { - bleio_characteristic_obj_t *characteristic = char_list->items[i]; + const mp_obj_list_t *characteristic_list = MP_OBJ_TO_PTR(service->characteristic_list); + for (size_t i = 0; i < characteristic_list->len; ++i) { + bleio_characteristic_obj_t *characteristic = characteristic_list->items[i]; common_hal_bleio_service_add_characteristic(service, characteristic); } } @@ -583,8 +583,8 @@ void common_hal_bleio_device_connect(bleio_device_obj_t *device) { bool found_char = discover_characteristics(device, service, service->start_handle); while (found_char) { - const mp_obj_list_t *char_list = MP_OBJ_TO_PTR(service->char_list); - const bleio_characteristic_obj_t *characteristic = char_list->items[char_list->len - 1]; + const mp_obj_list_t *characteristic_list = MP_OBJ_TO_PTR(service->characteristic_list); + const bleio_characteristic_obj_t *characteristic = characteristic_list->items[characteristic_list->len - 1]; const uint16_t next_handle = characteristic->handle + 1; if (next_handle >= service->end_handle) { diff --git a/ports/nrf/common-hal/bleio/Peripheral.c b/ports/nrf/common-hal/bleio/Peripheral.c index e59676fde..36c20373d 100644 --- a/ports/nrf/common-hal/bleio/Peripheral.c +++ b/ports/nrf/common-hal/bleio/Peripheral.c @@ -33,6 +33,7 @@ #include "ble_hci.h" #include "nrf_soc.h" #include "py/gc.h" +#include "py/objlist.h" #include "py/objstr.h" #include "py/runtime.h" #include "shared-bindings/bleio/Adapter.h" @@ -40,6 +41,7 @@ #include "shared-bindings/bleio/Peripheral.h" #include "shared-bindings/bleio/Service.h" #include "shared-bindings/bleio/UUID.h" +#include "common-hal/bleio/Service.h" #define BLE_MIN_CONN_INTERVAL MSEC_TO_UNITS(15, UNIT_0_625_MS) #define BLE_MAX_CONN_INTERVAL MSEC_TO_UNITS(300, UNIT_0_625_MS) @@ -117,19 +119,23 @@ STATIC void peripheral_on_ble_evt(ble_evt_t *ble_evt, void *self_in) { } } -void common_hal_bleio_peripheral_construct(bleio_peripheral_obj_t *self) { +void common_hal_bleio_peripheral_construct(bleio_peripheral_obj_t *self, mp_obj_list_t *service_list, mp_obj_t name) { common_hal_bleio_adapter_set_enabled(true); + self->service_list = service_list; + self->name = name; + self->gatt_role = GATT_ROLE_SERVER; self->conn_handle = BLE_CONN_HANDLE_INVALID; self->adv_handle = BLE_GAP_ADV_SET_HANDLE_NOT_SET; // Add all the services. - mp_obj_list_t *service_list = MP_OBJ_TO_PTR(self->service_list); for (size_t service_idx = 0; service_idx < service_list->len; ++service_idx) { bleio_service_obj_t *service = MP_OBJ_TO_PTR(service_list->items[service_idx]); + common_hal_bleio_service_set_device(service, MP_OBJ_FROM_PTR(self)); + ble_uuid_t uuid; bleio_uuid_convert_to_nrf_ble_uuid(service->uuid, &uuid); @@ -149,10 +155,18 @@ void common_hal_bleio_peripheral_construct(bleio_peripheral_obj_t *self) { } +mp_obj_list_t *common_hal_bleio_peripheral_get_service_list(bleio_peripheral_obj_t *self) { + return self->service_list; +} + bool common_hal_bleio_peripheral_get_connected(bleio_peripheral_obj_t *self) { return self->conn_handle != BLE_CONN_HANDLE_INVALID; } +mp_obj_t common_hal_bleio_peripheral_get_name(bleio_peripheral_obj_t *self) { + return self->name; +} + void common_hal_bleio_peripheral_start_advertising(bleio_peripheral_obj_t *self, bool connectable, mp_float_t interval, mp_buffer_info_t *advertising_data_bufinfo, mp_buffer_info_t *scan_response_data_bufinfo) { // interval value has already been validated. diff --git a/ports/nrf/common-hal/bleio/Peripheral.h b/ports/nrf/common-hal/bleio/Peripheral.h index eb0b0417a..9103fcf94 100644 --- a/ports/nrf/common-hal/bleio/Peripheral.h +++ b/ports/nrf/common-hal/bleio/Peripheral.h @@ -25,13 +25,16 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_COMMON_HAL_BLEIO_PERIPHERAL_H -#define MICROPY_INCLUDED_COMMON_HAL_BLEIO_PERIPHERAL_H +#ifndef MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_PERIPHERAL_H +#define MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_PERIPHERAL_H #include <stdbool.h> #include "ble.h" +#include "py/obj.h" +#include "py/objlist.h" + #include "shared-module/bleio/__init__.h" #include "shared-module/bleio/Address.h" @@ -40,8 +43,7 @@ typedef struct { mp_obj_t name; gatt_role_t gatt_role; volatile uint16_t conn_handle; - mp_obj_t service_list; - mp_obj_t notif_handler; + mp_obj_list_t *service_list; mp_obj_t conn_handler; // The advertising data and scan response buffers are held by us, not by the SD, so we must // maintain them and not change it. If we need to change the contents during advertising, @@ -52,4 +54,4 @@ typedef struct { } bleio_peripheral_obj_t; -#endif // MICROPY_INCLUDED_COMMON_HAL_BLEIO_PERIPHERAL_H +#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_PERIPHERAL_H diff --git a/ports/nrf/common-hal/bleio/Scanner.c b/ports/nrf/common-hal/bleio/Scanner.c index 08600bc0a..64f7decad 100644 --- a/ports/nrf/common-hal/bleio/Scanner.c +++ b/ports/nrf/common-hal/bleio/Scanner.c @@ -30,6 +30,7 @@ #include "ble_drv.h" #include "ble_gap.h" #include "py/mphal.h" +#include "py/objlist.h" #include "py/runtime.h" #include "shared-bindings/bleio/Adapter.h" #include "shared-bindings/bleio/ScanEntry.h" @@ -68,6 +69,10 @@ STATIC void on_ble_evt(ble_evt_t *ble_evt, void *scanner_in) { } } +void common_hal_bleio_scanner_construct(bleio_scanner_obj_t *self) { + self->adv_reports = mp_obj_new_list(0, NULL); +} + void common_hal_bleio_scanner_scan(bleio_scanner_obj_t *self, mp_float_t timeout, mp_float_t interval, mp_float_t window) { common_hal_bleio_adapter_set_enabled(true); ble_drv_add_event_handler(on_ble_evt, self); @@ -78,7 +83,8 @@ void common_hal_bleio_scanner_scan(bleio_scanner_obj_t *self, mp_float_t timeout .scan_phys = BLE_GAP_PHY_1MBPS, }; - common_hal_bleio_adapter_set_enabled(true); + // Empty the advertising reports list. + mp_obj_list_clear(self->adv_reports); uint32_t err_code; err_code = sd_ble_gap_scan_start(&scan_params, &m_scan_buffer); @@ -90,3 +96,7 @@ void common_hal_bleio_scanner_scan(bleio_scanner_obj_t *self, mp_float_t timeout mp_hal_delay_ms(timeout * 1000); sd_ble_gap_scan_stop(); } + +mp_obj_t common_hal_bleio_scanner_get_adv_reports(bleio_scanner_obj_t *self) { + return self->adv_reports; +} diff --git a/shared-module/bleio/Scanner.h b/ports/nrf/common-hal/bleio/Scanner.h index 76f5e5866..a71066487 100644 --- a/shared-module/bleio/Scanner.h +++ b/ports/nrf/common-hal/bleio/Scanner.h @@ -3,6 +3,7 @@ * * 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 @@ -24,16 +25,16 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_SHARED_MODULE_BLEIO_SCANNER_H -#define MICROPY_INCLUDED_SHARED_MODULE_BLEIO_SCANNER_H +#ifndef MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_SCANNER_H +#define MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_SCANNER_H #include "py/obj.h" typedef struct { mp_obj_base_t base; - mp_obj_t adv_reports; + mp_obj_t adv_reports; // List of reports. uint16_t interval; uint16_t window; } bleio_scanner_obj_t; -#endif // MICROPY_INCLUDED_SHARED_MODULE_BLEIO_SCANNER_H +#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_SCANNER_H diff --git a/ports/nrf/common-hal/bleio/Service.c b/ports/nrf/common-hal/bleio/Service.c index 26a1f1cff..77d3f0259 100644 --- a/ports/nrf/common-hal/bleio/Service.c +++ b/ports/nrf/common-hal/bleio/Service.c @@ -29,18 +29,46 @@ #include "py/runtime.h" #include "common-hal/bleio/__init__.h" #include "common-hal/bleio/Characteristic.h" +#include "shared-bindings/bleio/Characteristic.h" #include "shared-bindings/bleio/Service.h" #include "shared-bindings/bleio/Adapter.h" -void common_hal_bleio_service_construct(bleio_service_obj_t *self) { +void common_hal_bleio_service_construct(bleio_service_obj_t *self, bleio_uuid_obj_t *uuid, mp_obj_list_t *characteristic_list, bool is_secondary) { + self->device = mp_const_none; + self->handle = 0xFFFF; + self->uuid = uuid; + self->characteristic_list = characteristic_list; + self->is_secondary = is_secondary; + + for (size_t characteristic_idx = 0; characteristic_idx < characteristic_list->len; ++characteristic_idx) { + bleio_characteristic_obj_t *characteristic = MP_OBJ_TO_PTR(characteristic_list->items[characteristic_idx]); + common_hal_bleio_characteristic_set_service(characteristic, self); + } + +} + +bleio_uuid_obj_t *common_hal_bleio_service_get_uuid(bleio_service_obj_t *self) { + return self->uuid; +} + +mp_obj_list_t *common_hal_bleio_service_get_characteristic_list(bleio_service_obj_t *self) { + return self->characteristic_list; +} + +bool common_hal_bleio_service_get_is_secondary(bleio_service_obj_t *self) { + return self->is_secondary; +} + +void common_hal_bleio_service_set_device(bleio_service_obj_t *self, mp_obj_t device) { + self->device = device; } // Call this after the Service has been added to the Peripheral. void common_hal_bleio_service_add_all_characteristics(bleio_service_obj_t *self) { // Add all the characteristics. - const mp_obj_list_t *char_list = MP_OBJ_TO_PTR(self->char_list); - for (size_t char_idx = 0; char_idx < char_list->len; ++char_idx) { - bleio_characteristic_obj_t *characteristic = char_list->items[char_idx]; + const mp_obj_list_t *characteristic_list = MP_OBJ_TO_PTR(self->characteristic_list); + for (size_t characteristic_idx = 0; characteristic_idx < characteristic_list->len; ++characteristic_idx) { + bleio_characteristic_obj_t *characteristic = characteristic_list->items[characteristic_idx]; ble_gatts_char_md_t char_md = { .char_props.broadcast = characteristic->props.broadcast, diff --git a/shared-module/bleio/Service.h b/ports/nrf/common-hal/bleio/Service.h index 828d4cdc8..7998cc862 100644 --- a/shared-module/bleio/Service.h +++ b/ports/nrf/common-hal/bleio/Service.h @@ -24,8 +24,8 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_SHARED_MODULE_BLEIO_SERVICE_H -#define MICROPY_INCLUDED_SHARED_MODULE_BLEIO_SERVICE_H +#ifndef MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_SERVICE_H +#define MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_SERVICE_H #include "common-hal/bleio/UUID.h" @@ -36,9 +36,9 @@ typedef struct { bleio_uuid_obj_t *uuid; // May be a Peripheral, Central, etc. mp_obj_t *device; - mp_obj_t char_list; + mp_obj_t characteristic_list; uint16_t start_handle; uint16_t end_handle; } bleio_service_obj_t; -#endif // MICROPY_INCLUDED_SHARED_MODULE_BLEIO_SERVICE_H +#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_SERVICE_H diff --git a/ports/nrf/common-hal/bleio/__init__.h b/ports/nrf/common-hal/bleio/__init__.h index 9e044f37c..0c46b631f 100644 --- a/ports/nrf/common-hal/bleio/__init__.h +++ b/ports/nrf/common-hal/bleio/__init__.h @@ -24,8 +24,8 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_COMMON_HAL_BLEIO_INIT_H -#define MICROPY_INCLUDED_COMMON_HAL_BLEIO_INIT_H +#ifndef MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_INIT_H +#define MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_INIT_H #include "shared-bindings/bleio/__init__.h" #include "shared-bindings/bleio/Adapter.h" @@ -39,4 +39,4 @@ gatt_role_t common_hal_bleio_device_get_gatt_role(mp_obj_t device); uint16_t common_hal_bleio_device_get_conn_handle(mp_obj_t device); -#endif // MICROPY_INCLUDED_COMMON_HAL_BLEIO_INIT_H +#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_INIT_H diff --git a/py/circuitpy_defns.mk b/py/circuitpy_defns.mk index fd43a11a6..435cf2c33 100644 --- a/py/circuitpy_defns.mk +++ b/py/circuitpy_defns.mk @@ -308,6 +308,8 @@ $(filter $(SRC_PATTERNS), \ bitbangio/SPI.c \ bitbangio/__init__.c \ board/__init__.c \ + bleio/Address.c \ + bleio/ScanEntry.c \ busio/OneWire.c \ displayio/Bitmap.c \ displayio/ColorConverter.c \ @@ -763,6 +763,7 @@ void mp_obj_tuple_del(mp_obj_t self_in); mp_int_t mp_obj_tuple_hash(mp_obj_t self_in); // list +mp_obj_t mp_obj_list_clear(mp_obj_t self_in); mp_obj_t mp_obj_list_append(mp_obj_t self_in, mp_obj_t arg); mp_obj_t mp_obj_list_remove(mp_obj_t self_in, mp_obj_t value); void mp_obj_list_get(mp_obj_t self_in, size_t *len, mp_obj_t **items); diff --git a/py/objlist.c b/py/objlist.c index 16ca4b353..558c4c611 100644 --- a/py/objlist.c +++ b/py/objlist.c @@ -340,7 +340,7 @@ mp_obj_t mp_obj_list_sort(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_ return mp_const_none; } -STATIC mp_obj_t list_clear(mp_obj_t self_in) { +mp_obj_t mp_obj_list_clear(mp_obj_t self_in) { mp_check_self(MP_OBJ_IS_TYPE(self_in, &mp_type_list)); mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in); self->len = 0; @@ -418,7 +418,7 @@ STATIC mp_obj_t list_reverse(mp_obj_t self_in) { STATIC MP_DEFINE_CONST_FUN_OBJ_2(list_append_obj, mp_obj_list_append); STATIC MP_DEFINE_CONST_FUN_OBJ_2(list_extend_obj, list_extend); -STATIC MP_DEFINE_CONST_FUN_OBJ_1(list_clear_obj, list_clear); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(list_clear_obj, mp_obj_list_clear); STATIC MP_DEFINE_CONST_FUN_OBJ_1(list_copy_obj, list_copy); STATIC MP_DEFINE_CONST_FUN_OBJ_2(list_count_obj, list_count); STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_index_obj, 2, 4, list_index); diff --git a/shared-bindings/bleio/Address.c b/shared-bindings/bleio/Address.c index 7367da535..5875cf9bd 100644 --- a/shared-bindings/bleio/Address.c +++ b/shared-bindings/bleio/Address.c @@ -3,6 +3,7 @@ * * 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 @@ -74,13 +75,12 @@ STATIC mp_obj_t bleio_address_make_new(const mp_obj_type_t *type, size_t n_args, mp_raise_ValueError_varg(translate("Address must be %d bytes long"), NUM_BLEIO_ADDRESS_BYTES); } - memcpy(self->bytes, buf_info.buf, buf_info.len); - const mp_int_t address_type = args[ARG_address_type].u_int; if (address_type < BLEIO_ADDRESS_TYPE_MIN || address_type > BLEIO_ADDRESS_TYPE_MAX) { mp_raise_ValueError(translate("Address type out of range")); } - self->type = address_type; + + common_hal_bleio_address_construct(self, buf_info.buf, buf_info.len, address_type); return MP_OBJ_FROM_PTR(self); } @@ -97,7 +97,7 @@ STATIC mp_obj_t bleio_address_make_new(const mp_obj_type_t *type, size_t n_args, STATIC mp_obj_t bleio_address_get_address_bytes(mp_obj_t self_in) { bleio_address_obj_t *self = MP_OBJ_TO_PTR(self_in); - return mp_obj_new_bytes(self->bytes, NUM_BLEIO_ADDRESS_BYTES); + return common_hal_bleio_address_get_address_bytes(self); } MP_DEFINE_CONST_FUN_OBJ_1(bleio_address_get_address_bytes_obj, bleio_address_get_address_bytes); @@ -113,7 +113,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(bleio_address_get_address_bytes_obj, bleio_address_get STATIC mp_obj_t bleio_address_get_type(mp_obj_t self_in) { bleio_address_obj_t *self = MP_OBJ_TO_PTR(self_in); - return MP_OBJ_NEW_SMALL_INT(self->type); + return MP_OBJ_NEW_SMALL_INT(common_hal_bleio_address_get_type(self)); } MP_DEFINE_CONST_FUN_OBJ_1(bleio_address_get_type_obj, bleio_address_get_type); diff --git a/shared-bindings/bleio/Address.h b/shared-bindings/bleio/Address.h index ef84d7432..9652a9841 100644 --- a/shared-bindings/bleio/Address.h +++ b/shared-bindings/bleio/Address.h @@ -28,6 +28,7 @@ #define MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_ADDRESS_H #include "py/objtype.h" +#include "shared-module/bleio/Address.h" #define BLEIO_ADDRESS_TYPE_PUBLIC (0) #define BLEIO_ADDRESS_TYPE_RANDOM_STATIC (1) @@ -39,4 +40,8 @@ extern const mp_obj_type_t bleio_address_type; +extern void common_hal_bleio_address_construct(bleio_address_obj_t *self, uint8_t *bytes, size_t bytes_length, uint8_t address_type); +extern mp_obj_t common_hal_bleio_address_get_address_bytes(bleio_address_obj_t *self); +extern uint8_t common_hal_bleio_address_get_type(bleio_address_obj_t *self); + #endif // MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_ADDRESS_H diff --git a/shared-bindings/bleio/Characteristic.c b/shared-bindings/bleio/Characteristic.c index ae83de864..e48cfd433 100644 --- a/shared-bindings/bleio/Characteristic.c +++ b/shared-bindings/bleio/Characteristic.c @@ -73,10 +73,10 @@ STATIC mp_obj_t bleio_characteristic_make_new(const mp_obj_type_t *type, size_t if (!MP_OBJ_IS_TYPE(uuid, &bleio_uuid_type)) { mp_raise_ValueError(translate("Expected a UUID")); } + bleio_uuid_obj_t *uuid_obj = MP_OBJ_TO_PTR(uuid); bleio_characteristic_obj_t *self = m_new_obj(bleio_characteristic_obj_t); self->base.type = &bleio_characteristic_type; - self->uuid = MP_OBJ_TO_PTR(uuid); bleio_characteristic_properties_t properties; @@ -87,7 +87,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, properties); + common_hal_bleio_characteristic_construct(self, uuid_obj, properties); return MP_OBJ_FROM_PTR(self); } @@ -99,7 +99,7 @@ STATIC mp_obj_t bleio_characteristic_make_new(const mp_obj_type_t *type, size_t STATIC mp_obj_t bleio_characteristic_get_broadcast(mp_obj_t self_in) { bleio_characteristic_obj_t *self = MP_OBJ_TO_PTR(self_in); - return mp_obj_new_bool(self->props.broadcast); + return mp_obj_new_bool(common_hal_bleio_characteristic_get_properties(self).broadcast); } STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_characteristic_get_broadcast_obj, bleio_characteristic_get_broadcast); @@ -117,7 +117,7 @@ const mp_obj_property_t bleio_characteristic_broadcast_obj = { STATIC mp_obj_t bleio_characteristic_get_indicate(mp_obj_t self_in) { bleio_characteristic_obj_t *self = MP_OBJ_TO_PTR(self_in); - return mp_obj_new_bool(self->props.indicate); + return mp_obj_new_bool(common_hal_bleio_characteristic_get_properties(self).indicate); } STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_characteristic_get_indicate_obj, bleio_characteristic_get_indicate); @@ -136,7 +136,7 @@ const mp_obj_property_t bleio_characteristic_indicate_obj = { STATIC mp_obj_t bleio_characteristic_get_notify(mp_obj_t self_in) { bleio_characteristic_obj_t *self = MP_OBJ_TO_PTR(self_in); - return mp_obj_new_bool(self->props.notify); + return mp_obj_new_bool(common_hal_bleio_characteristic_get_properties(self).notify); } STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_characteristic_get_notify_obj, bleio_characteristic_get_notify); @@ -154,7 +154,7 @@ const mp_obj_property_t bleio_characteristic_notify_obj = { STATIC mp_obj_t bleio_characteristic_get_read(mp_obj_t self_in) { bleio_characteristic_obj_t *self = MP_OBJ_TO_PTR(self_in); - return mp_obj_new_bool(self->props.read); + return mp_obj_new_bool(common_hal_bleio_characteristic_get_properties(self).read); } STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_characteristic_get_read_obj, bleio_characteristic_get_read); @@ -172,7 +172,7 @@ const mp_obj_property_t bleio_characteristic_read_obj = { STATIC mp_obj_t bleio_characteristic_get_write(mp_obj_t self_in) { bleio_characteristic_obj_t *self = MP_OBJ_TO_PTR(self_in); - return mp_obj_new_bool(self->props.write); + return mp_obj_new_bool(common_hal_bleio_characteristic_get_properties(self).write); } STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_characteristic_get_write_obj, bleio_characteristic_get_write); @@ -190,7 +190,7 @@ const mp_obj_property_t bleio_characteristic_write_obj = { STATIC mp_obj_t bleio_characteristic_get_write_no_response(mp_obj_t self_in) { bleio_characteristic_obj_t *self = MP_OBJ_TO_PTR(self_in); - return mp_obj_new_bool(self->props.write_no_response); + return mp_obj_new_bool(common_hal_bleio_characteristic_get_properties(self).write_no_response); } STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_characteristic_get_write_no_response_obj, bleio_characteristic_get_write_no_response); @@ -208,7 +208,7 @@ const mp_obj_property_t bleio_characteristic_write_no_response_obj = { 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(self->uuid); + return MP_OBJ_FROM_PTR(common_hal_bleio_characteristic_get_uuid(self)); } STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_characteristic_get_uuid_obj, bleio_characteristic_get_uuid); @@ -228,9 +228,7 @@ const mp_obj_property_t bleio_characteristic_uuid_obj = { STATIC mp_obj_t bleio_characteristic_get_value(mp_obj_t self_in) { bleio_characteristic_obj_t *self = MP_OBJ_TO_PTR(self_in); - common_hal_bleio_characteristic_get_value(self); - - return self->value_data; + return common_hal_bleio_characteristic_get_value(self); } STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_characteristic_get_value_obj, bleio_characteristic_get_value); diff --git a/shared-bindings/bleio/Characteristic.h b/shared-bindings/bleio/Characteristic.h index 206cbcd40..ffee6bdbc 100644 --- a/shared-bindings/bleio/Characteristic.h +++ b/shared-bindings/bleio/Characteristic.h @@ -32,7 +32,10 @@ extern const mp_obj_type_t bleio_characteristic_type; extern void common_hal_bleio_characteristic_construct(bleio_characteristic_obj_t *self, bleio_uuid_obj_t *uuid, bleio_characteristic_properties_t props); -extern void common_hal_bleio_characteristic_get_value(bleio_characteristic_obj_t *self); +extern void common_hal_bleio_characteristic_set_service(bleio_characteristic_obj_t *self, bleio_service_obj_t *service); +extern mp_obj_t common_hal_bleio_characteristic_get_value(bleio_characteristic_obj_t *self); extern void common_hal_bleio_characteristic_set_value(bleio_characteristic_obj_t *self, mp_buffer_info_t *bufinfo); +extern bleio_characteristic_properties_t common_hal_bleio_characteristic_get_properties(bleio_characteristic_obj_t *self); +extern bleio_uuid_obj_t *common_hal_bleio_characteristic_get_uuid(bleio_characteristic_obj_t *self); #endif // MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_CHARACTERISTIC_H diff --git a/shared-bindings/bleio/CharacteristicBuffer.c b/shared-bindings/bleio/CharacteristicBuffer.c index a1dc663fd..da79c5d7a 100644 --- a/shared-bindings/bleio/CharacteristicBuffer.c +++ b/shared-bindings/bleio/CharacteristicBuffer.c @@ -85,9 +85,8 @@ 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; - self->characteristic = MP_OBJ_TO_PTR(characteristic); - common_hal_bleio_characteristic_buffer_construct(self, self->characteristic, timeout, buffer_size); + common_hal_bleio_characteristic_buffer_construct(self, characteristic, timeout, buffer_size); return MP_OBJ_FROM_PTR(self); } diff --git a/shared-bindings/bleio/Device.h b/shared-bindings/bleio/Device.h index 1f85abe9a..e452a8ad6 100644 --- a/shared-bindings/bleio/Device.h +++ b/shared-bindings/bleio/Device.h @@ -28,7 +28,7 @@ #define MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_DEVICE_H #include "shared-module/bleio/Device.h" -#include "shared-module/bleio/Service.h" +#include "common-hal/bleio/Service.h" extern const mp_obj_type_t bleio_device_type; diff --git a/shared-bindings/bleio/Peripheral.c b/shared-bindings/bleio/Peripheral.c index 4492a8e26..5397f4c3a 100644 --- a/shared-bindings/bleio/Peripheral.c +++ b/shared-bindings/bleio/Peripheral.c @@ -101,32 +101,33 @@ STATIC mp_obj_t bleio_peripheral_make_new(const mp_obj_type_t *type, size_t n_ar // If services is not an iterable, an exception will be thrown. mp_obj_iter_buf_t iter_buf; mp_obj_t iterable = mp_getiter(args[ARG_services].u_obj, &iter_buf); - mp_obj_t service; bleio_peripheral_obj_t *self = m_new_obj(bleio_peripheral_obj_t); self->base.type = &bleio_peripheral_type; - self->service_list = mp_obj_new_list(0, NULL); - self->notif_handler = mp_const_none; + + // 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; 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")); } - bleio_service_obj_t *service_ptr = MP_OBJ_TO_PTR(service); - service_ptr->device = MP_OBJ_FROM_PTR(self); - mp_obj_list_append(self->service_list, service); + mp_obj_list_append(service_list, service); } const mp_obj_t name = args[ARG_name].u_obj; + mp_obj_t name_str; if (name == MP_OBJ_NULL || name == mp_const_none) { - self->name = mp_obj_new_str(default_name, strlen(default_name)); + name_str = mp_obj_new_str(default_name, strlen(default_name)); } else if (MP_OBJ_IS_STR(name)) { - self->name = name; + name_str = name; } else { mp_raise_ValueError(translate("name must be a string")); } - // Do port-specific initialization. - common_hal_bleio_peripheral_construct(self); + common_hal_bleio_peripheral_construct(self, service_list_obj, name_str); return MP_OBJ_FROM_PTR(self); } @@ -157,7 +158,7 @@ const mp_obj_property_t bleio_peripheral_connected_obj = { STATIC mp_obj_t bleio_peripheral_get_services(mp_obj_t self_in) { bleio_peripheral_obj_t *self = MP_OBJ_TO_PTR(self_in); // Return list as a tuple so user won't be able to change it. - mp_obj_list_t *service_list = MP_OBJ_TO_PTR(self->service_list); + mp_obj_list_t *service_list = common_hal_bleio_peripheral_get_service_list(self); return mp_obj_new_tuple(service_list->len, service_list->items); } STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_peripheral_get_services_obj, bleio_peripheral_get_services); @@ -176,7 +177,7 @@ const mp_obj_property_t bleio_peripheral_services_obj = { STATIC mp_obj_t bleio_peripheral_get_name(mp_obj_t self_in) { bleio_peripheral_obj_t *self = MP_OBJ_TO_PTR(self_in); - return self->name; + return common_hal_bleio_peripheral_get_name(self); } MP_DEFINE_CONST_FUN_OBJ_1(bleio_peripheral_get_name_obj, bleio_peripheral_get_name); diff --git a/shared-bindings/bleio/Peripheral.h b/shared-bindings/bleio/Peripheral.h index 7ef45edcd..6468325c2 100644 --- a/shared-bindings/bleio/Peripheral.h +++ b/shared-bindings/bleio/Peripheral.h @@ -32,8 +32,10 @@ extern const mp_obj_type_t bleio_peripheral_type; -extern void common_hal_bleio_peripheral_construct(bleio_peripheral_obj_t *self); +extern void common_hal_bleio_peripheral_construct(bleio_peripheral_obj_t *self, mp_obj_list_t *service_list, mp_obj_t name); +extern mp_obj_list_t *common_hal_bleio_peripheral_get_service_list(bleio_peripheral_obj_t *self); extern bool common_hal_bleio_peripheral_get_connected(bleio_peripheral_obj_t *self); +extern mp_obj_t common_hal_bleio_peripheral_get_name(bleio_peripheral_obj_t *self); extern void common_hal_bleio_peripheral_start_advertising(bleio_peripheral_obj_t *device, bool connectable, float interval, mp_buffer_info_t *advertising_data_bufinfo, mp_buffer_info_t *scan_response_data_bufinfo); extern void common_hal_bleio_peripheral_stop_advertising(bleio_peripheral_obj_t *device); diff --git a/shared-bindings/bleio/ScanEntry.c b/shared-bindings/bleio/ScanEntry.c index f66b3df4b..145d2b023 100644 --- a/shared-bindings/bleio/ScanEntry.c +++ b/shared-bindings/bleio/ScanEntry.c @@ -3,6 +3,7 @@ * * The MIT License (MIT) * + * Copyright (c) 2019 Dan Halbert for Adafruit Industries * Copyright (c) 2018 Artur Pacholec * Copyright (c) 2017 Glenn Ruben Bakke * @@ -27,10 +28,7 @@ #include <string.h> -#include "py/objarray.h" #include "py/objproperty.h" -#include "py/objstr.h" -#include "py/objtuple.h" #include "shared-bindings/bleio/Address.h" #include "shared-bindings/bleio/ScanEntry.h" #include "shared-bindings/bleio/UUID.h" @@ -53,18 +51,13 @@ //| STATIC mp_obj_t bleio_scanentry_get_address(mp_obj_t self_in) { bleio_scanentry_obj_t *self = MP_OBJ_TO_PTR(self_in); - - bleio_address_obj_t *address = m_new_obj(bleio_address_obj_t); - address->base.type = &bleio_address_type; - memcpy(address->bytes, self->address.bytes, NUM_BLEIO_ADDRESS_BYTES); - address->type = self->address.type; - return MP_OBJ_TO_PTR(address); + return common_hal_bleio_scanentry_get_address(self); } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(bluepy_scanentry_get_address_obj, bleio_scanentry_get_address); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_scanentry_get_address_obj, bleio_scanentry_get_address); const mp_obj_property_t bleio_scanentry_address_obj = { .base.type = &mp_type_property, - .proxy = { (mp_obj_t)&bluepy_scanentry_get_address_obj, + .proxy = { (mp_obj_t)&bleio_scanentry_get_address_obj, (mp_obj_t)&mp_const_none_obj, (mp_obj_t)&mp_const_none_obj }, }; @@ -75,7 +68,7 @@ const mp_obj_property_t bleio_scanentry_address_obj = { //| STATIC mp_obj_t scanentry_get_raw_data(mp_obj_t self_in) { bleio_scanentry_obj_t *self = MP_OBJ_TO_PTR(self_in); - return self->data; + return common_hal_bleio_scanentry_get_raw_data(self); } STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_scanentry_get_raw_data_obj, scanentry_get_raw_data); @@ -92,14 +85,13 @@ const mp_obj_property_t bleio_scanentry_raw_data_obj = { //| STATIC mp_obj_t scanentry_get_rssi(mp_obj_t self_in) { bleio_scanentry_obj_t *self = MP_OBJ_TO_PTR(self_in); - - return mp_obj_new_int(self->rssi); + return mp_obj_new_int(common_hal_bleio_scanentry_get_rssi(self)); } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(bluepy_scanentry_get_rssi_obj, scanentry_get_rssi); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_scanentry_get_rssi_obj, scanentry_get_rssi); const mp_obj_property_t bleio_scanentry_rssi_obj = { .base.type = &mp_type_property, - .proxy = { (mp_obj_t)&bluepy_scanentry_get_rssi_obj, + .proxy = { (mp_obj_t)&bleio_scanentry_get_rssi_obj, (mp_obj_t)&mp_const_none_obj, (mp_obj_t)&mp_const_none_obj }, }; diff --git a/shared-bindings/bleio/ScanEntry.h b/shared-bindings/bleio/ScanEntry.h index 2b44ba3f4..c1b52e6b4 100644 --- a/shared-bindings/bleio/ScanEntry.h +++ b/shared-bindings/bleio/ScanEntry.h @@ -29,7 +29,12 @@ #define MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_SCANENTRY_H #include "py/obj.h" +#include "shared-module/bleio/ScanEntry.h" extern const mp_obj_type_t bleio_scanentry_type; +mp_obj_t common_hal_bleio_scanentry_get_address(bleio_scanentry_obj_t *self); +mp_obj_t common_hal_bleio_scanentry_get_raw_data(bleio_scanentry_obj_t *self); +mp_int_t common_hal_bleio_scanentry_get_rssi(bleio_scanentry_obj_t *self); + #endif // MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_SCANENTRY_H diff --git a/shared-bindings/bleio/Scanner.c b/shared-bindings/bleio/Scanner.c index a249d5343..92f6a2a18 100644 --- a/shared-bindings/bleio/Scanner.c +++ b/shared-bindings/bleio/Scanner.c @@ -61,6 +61,8 @@ STATIC mp_obj_t bleio_scanner_make_new(const mp_obj_type_t *type, size_t n_args, bleio_scanner_obj_t *self = m_new_obj(bleio_scanner_obj_t); self->base.type = type; + common_hal_bleio_scanner_construct(self); + return MP_OBJ_FROM_PTR(self); } @@ -108,11 +110,9 @@ STATIC mp_obj_t bleio_scanner_scan(size_t n_args, const mp_obj_t *pos_args, mp_m mp_raise_ValueError(translate("window must be <= interval")); } - self->adv_reports = mp_obj_new_list(0, NULL); - common_hal_bleio_scanner_scan(self, timeout, interval, window); - return self->adv_reports; + return common_hal_bleio_scanner_get_adv_reports(self); } STATIC MP_DEFINE_CONST_FUN_OBJ_KW(bleio_scanner_scan_obj, 2, bleio_scanner_scan); diff --git a/shared-bindings/bleio/Scanner.h b/shared-bindings/bleio/Scanner.h index 54c09675b..b9e3ecdee 100644 --- a/shared-bindings/bleio/Scanner.h +++ b/shared-bindings/bleio/Scanner.h @@ -29,11 +29,13 @@ #define MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_SCANNER_H #include "py/objtype.h" -#include "shared-module/bleio/Scanner.h" +#include "common-hal/bleio/Scanner.h" extern const mp_obj_type_t bleio_scanner_type; -void common_hal_bleio_scanner_scan(bleio_scanner_obj_t *self, mp_float_t timeout, mp_float_t interval, mp_float_t window); +extern void common_hal_bleio_scanner_construct(bleio_scanner_obj_t *self); +extern void common_hal_bleio_scanner_scan(bleio_scanner_obj_t *self, mp_float_t timeout, mp_float_t interval, mp_float_t window); extern void common_hal_bleio_scanner_stop(bleio_scanner_obj_t *self); +extern mp_obj_t common_hal_bleio_scanner_get_adv_reports(bleio_scanner_obj_t *self); #endif // MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_SCANNER_H diff --git a/shared-bindings/bleio/Service.c b/shared-bindings/bleio/Service.c index d9bc4021f..352ef8d6a 100644 --- a/shared-bindings/bleio/Service.c +++ b/shared-bindings/bleio/Service.c @@ -67,18 +67,20 @@ STATIC mp_obj_t bleio_service_make_new(const mp_obj_type_t *type, size_t n_args, } bleio_service_obj_t *self = m_new_obj(bleio_service_obj_t); - self->char_list = mp_obj_new_list(0, NULL); self->base.type = &bleio_service_type; - self->device = mp_const_none; - self->handle = 0xFFFF; - self->is_secondary = args[ARG_secondary].u_bool; - self->uuid = MP_OBJ_TO_PTR(uuid); + + const bool is_secondary = args[ARG_secondary].u_bool; + bleio_uuid_obj_t *uuid_obj = MP_OBJ_TO_PTR(uuid); // 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; + // 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); + while ((characteristic = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) { if (!MP_OBJ_IS_TYPE(characteristic, &bleio_characteristic_type)) { mp_raise_ValueError(translate("characteristics includes an object that is not a Characteristic")); @@ -89,12 +91,10 @@ STATIC mp_obj_t bleio_service_make_new(const mp_obj_type_t *type, size_t n_args, // The descriptor base UUID doesn't match the characteristic base UUID. mp_raise_ValueError(translate("Characteristic UUID doesn't match Service UUID")); } - characteristic_ptr->service = self; - mp_obj_list_append(self->char_list, characteristic); + mp_obj_list_append(char_list, characteristic); } - // Do port-specific initialization. - common_hal_bleio_service_construct(self); + common_hal_bleio_service_construct(self, uuid_obj, char_list_obj, is_secondary); return MP_OBJ_FROM_PTR(self); } @@ -106,7 +106,7 @@ STATIC mp_obj_t bleio_service_make_new(const mp_obj_type_t *type, size_t n_args, STATIC mp_obj_t bleio_service_get_characteristics(mp_obj_t self_in) { bleio_service_obj_t *self = MP_OBJ_TO_PTR(self_in); // Return list as a tuple so user won't be able to change it. - mp_obj_list_t *char_list = MP_OBJ_TO_PTR(self->char_list); + mp_obj_list_t *char_list = common_hal_bleio_service_get_characteristic_list(self); return mp_obj_new_tuple(char_list->len, char_list->items); } STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_service_get_characteristics_obj, bleio_service_get_characteristics); @@ -125,7 +125,7 @@ const mp_obj_property_t bleio_service_characteristics_obj = { STATIC mp_obj_t bleio_service_get_secondary(mp_obj_t self_in) { bleio_service_obj_t *self = MP_OBJ_TO_PTR(self_in); - return mp_obj_new_bool(self->is_secondary); + return mp_obj_new_bool(common_hal_bleio_service_get_is_secondary(self)); } STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_service_get_secondary_obj, bleio_service_get_secondary); @@ -143,7 +143,7 @@ const mp_obj_property_t bleio_service_secondary_obj = { 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(self->uuid); + return MP_OBJ_FROM_PTR(common_hal_bleio_service_get_uuid(self)); } STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_service_get_uuid_obj, bleio_service_get_uuid); diff --git a/shared-bindings/bleio/Service.h b/shared-bindings/bleio/Service.h index 389db3b2e..27d79f953 100644 --- a/shared-bindings/bleio/Service.h +++ b/shared-bindings/bleio/Service.h @@ -28,11 +28,15 @@ #define MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_SERVICE_H #include "shared-module/bleio/Characteristic.h" -#include "shared-module/bleio/Service.h" +#include "common-hal/bleio/Service.h" const mp_obj_type_t bleio_service_type; -extern void common_hal_bleio_service_construct(bleio_service_obj_t *self); +extern void common_hal_bleio_service_construct(bleio_service_obj_t *self, bleio_uuid_obj_t *uuid, mp_obj_list_t *char_list, bool is_secondary); +extern bleio_uuid_obj_t *common_hal_bleio_service_get_uuid(bleio_service_obj_t *self); +extern mp_obj_list_t *common_hal_bleio_service_get_characteristic_list(bleio_service_obj_t *self); +extern bool common_hal_bleio_service_get_is_secondary(bleio_service_obj_t *self); +extern void common_hal_bleio_service_set_device(bleio_service_obj_t *self, mp_obj_t device); extern void common_hal_bleio_service_add_all_characteristics(bleio_service_obj_t *self); #endif // MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_SERVICE_H diff --git a/shared-module/bleio/Address.c b/shared-module/bleio/Address.c new file mode 100644 index 000000000..e26bbe2ac --- /dev/null +++ b/shared-module/bleio/Address.c @@ -0,0 +1,45 @@ +/* + * 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. + */ + +#include <string.h> + +#include "py/objproperty.h" +#include "shared-bindings/bleio/Address.h" +#include "shared-module/bleio/Address.h" + +void common_hal_bleio_address_construct(bleio_address_obj_t *self, uint8_t *bytes, size_t bytes_length, uint8_t address_type) { + memcpy(self->bytes, bytes, bytes_length); + self->type = address_type; +} + +mp_obj_t common_hal_bleio_address_get_address_bytes(bleio_address_obj_t *self) { + return mp_obj_new_bytes(self->bytes, NUM_BLEIO_ADDRESS_BYTES); +} + +uint8_t common_hal_bleio_address_get_type(bleio_address_obj_t *self) { + return self->type; +} diff --git a/shared-module/bleio/Address.h b/shared-module/bleio/Address.h index 65f493c16..f66e97d64 100644 --- a/shared-module/bleio/Address.h +++ b/shared-module/bleio/Address.h @@ -27,6 +27,8 @@ #ifndef MICROPY_INCLUDED_SHARED_MODULE_BLEIO_ADDRESS_H #define MICROPY_INCLUDED_SHARED_MODULE_BLEIO_ADDRESS_H +#include "py/obj.h" + #define NUM_BLEIO_ADDRESS_BYTES 6 typedef struct { diff --git a/shared-module/bleio/Characteristic.h b/shared-module/bleio/Characteristic.h index 958837e64..3132fd47b 100644 --- a/shared-module/bleio/Characteristic.h +++ b/shared-module/bleio/Characteristic.h @@ -37,5 +37,6 @@ typedef struct { bool indicate : 1; } bleio_characteristic_properties_t; +// bleio_characteristic_obj_t is defined in ports/*/common-hal. #endif // MICROPY_INCLUDED_SHARED_MODULE_BLEIO_CHARACTERISTIC_H diff --git a/shared-module/bleio/ScanEntry.c b/shared-module/bleio/ScanEntry.c new file mode 100644 index 000000000..44b50a4ce --- /dev/null +++ b/shared-module/bleio/ScanEntry.c @@ -0,0 +1,49 @@ +/* + * 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 + * Copyright (c) 2017 Glenn Ruben Bakke + * + * 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. + */ + +#include <string.h> + +#include "shared-bindings/bleio/Address.h" +#include "shared-module/bleio/Address.h" +#include "shared-module/bleio/ScanEntry.h" + +mp_obj_t common_hal_bleio_scanentry_get_address(bleio_scanentry_obj_t *self) { + bleio_address_obj_t *address = m_new_obj(bleio_address_obj_t); + address->base.type = &bleio_address_type; + memcpy(address->bytes, self->address.bytes, NUM_BLEIO_ADDRESS_BYTES); + address->type = self->address.type; + return MP_OBJ_TO_PTR(address); +} + +mp_obj_t common_hal_bleio_scanentry_get_raw_data(bleio_scanentry_obj_t *self) { + return self->data; +} + +mp_int_t common_hal_bleio_scanentry_get_rssi(bleio_scanentry_obj_t *self) { + return self->rssi; +} diff --git a/shared-module/bleio/ScanEntry.h b/shared-module/bleio/ScanEntry.h index 6ae7334a8..40e02f9fa 100644 --- a/shared-module/bleio/ScanEntry.h +++ b/shared-module/bleio/ScanEntry.h @@ -27,7 +27,8 @@ #ifndef MICROPY_INCLUDED_SHARED_MODULE_BLEIO_SCANENTRY_H #define MICROPY_INCLUDED_SHARED_MODULE_BLEIO_SCANENTRY_H -#include "shared-module/bleio/Address.h" +#include "py/obj.h" +#include "shared-bindings/bleio/Address.h" typedef struct { mp_obj_base_t base; |
