From 1dc3957e729a7bf4d6476c587f140e3d42fd1bd5 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Sun, 30 Dec 2018 22:31:51 -0500 Subject: [PATCH] LocalPeripheral is now Peripheral; more work on basic GATTS support; UART not working yet --- ports/nrf/common-hal/bleio/Characteristic.c | 84 +++++++++++++++-- .../bleio/{LocalPeripheral.c => Peripheral.c} | 24 ++--- .../bleio/{LocalPeripheral.h => Peripheral.h} | 19 +--- ports/nrf/common-hal/bleio/__init__.c | 23 +++++ ports/nrf/common-hal/bleio/__init__.h | 1 + py/obj.h | 1 + py/objarray.c | 6 ++ shared-bindings/bleio/Characteristic.c | 4 +- shared-bindings/bleio/Characteristic.h | 4 +- .../bleio/{LocalPeripheral.c => Peripheral.c} | 90 +++++++++---------- .../bleio/{LocalPeripheral.h => Peripheral.h} | 19 ++-- shared-bindings/bleio/__init__.c | 4 +- shared-module/bleio/Service.h | 2 +- shared-module/bleio/__init__.h | 6 ++ 14 files changed, 192 insertions(+), 95 deletions(-) rename ports/nrf/common-hal/bleio/{LocalPeripheral.c => Peripheral.c} (91%) rename ports/nrf/common-hal/bleio/{LocalPeripheral.h => Peripheral.h} (79%) rename shared-bindings/bleio/{LocalPeripheral.c => Peripheral.c} (68%) rename shared-bindings/bleio/{LocalPeripheral.h => Peripheral.h} (62%) diff --git a/ports/nrf/common-hal/bleio/Characteristic.c b/ports/nrf/common-hal/bleio/Characteristic.c index 17656c2694..7a32e4928c 100644 --- a/ports/nrf/common-hal/bleio/Characteristic.c +++ b/ports/nrf/common-hal/bleio/Characteristic.c @@ -35,13 +35,63 @@ #include "common-hal/bleio/__init__.h" #include "shared-module/bleio/Characteristic.h" - +// TODO - should these be per object?? ***** STATIC volatile bleio_characteristic_obj_t *m_read_characteristic; STATIC volatile uint8_t m_tx_in_progress; STATIC nrf_mutex_t *m_write_mutex; +STATIC uint16_t get_cccd(bleio_characteristic_obj_t *characteristic) { + const uint16_t conn_handle = common_hal_bleio_device_get_conn_handle(characteristic->service->device); + uint16_t cccd; + ble_gatts_value_t value = { + .p_value = (uint8_t*) &cccd, + .len = 2, + }; + + const uint32_t err_code = sd_ble_gatts_value_get(conn_handle, characteristic->cccd_handle, &value); + + + if (err_code == BLE_ERROR_GATTS_SYS_ATTR_MISSING) { + // CCCD is not set, so say that neither Notify nor Indicate is enabled. + cccd = 0; + } else if (err_code != NRF_SUCCESS) { + mp_raise_OSError_msg_varg(translate("Failed to read CCD value, err 0x%04x"), err_code); + } + + return cccd; +} + +STATIC void gatts_read(bleio_characteristic_obj_t *characteristic) { + // This might be BLE_CONN_HANDLE_INVALID if we're not conected, but that's OK, because + // we can still read and write the local value. + const uint16_t conn_handle = common_hal_bleio_device_get_conn_handle(characteristic->service->device); + + mp_buffer_info_t bufinfo; + ble_gatts_value_t gatts_value = { + .p_value = NULL, + .len = 0, + }; + + // Read once to find out what size buffer we need, then read again to fill buffer. + + uint32_t err_code = sd_ble_gatts_value_get(conn_handle, characteristic->handle, &gatts_value); + if (err_code == NRF_SUCCESS) { + characteristic->value_data = mp_obj_new_bytearray_of_zeros(gatts_value.len); + mp_get_buffer_raise(characteristic->value_data, &bufinfo, MP_BUFFER_WRITE); + + // Read again, with the correct size of buffer. + err_code = sd_ble_gatts_value_get(conn_handle, characteristic->handle, &gatts_value); + } + + if (err_code != NRF_SUCCESS) { + mp_raise_OSError_msg_varg(translate("Failed to read gatts value, err 0x%04x"), err_code); + } +} + STATIC void gatts_write(bleio_characteristic_obj_t *characteristic, mp_buffer_info_t *bufinfo) { + // This might be BLE_CONN_HANDLE_INVALID if we're not conected, but that's OK, because + // we can still read and write the local value. const uint16_t conn_handle = common_hal_bleio_device_get_conn_handle(characteristic->service->device); ble_gatts_value_t gatts_value = { @@ -55,12 +105,12 @@ STATIC void gatts_write(bleio_characteristic_obj_t *characteristic, mp_buffer_in } } -STATIC void gatts_notify(bleio_characteristic_obj_t *characteristic, mp_buffer_info_t *bufinfo) { +STATIC void gatts_notify_indicate(bleio_characteristic_obj_t *characteristic, mp_buffer_info_t *bufinfo, uint16_t hvx_type) { uint16_t hvx_len = bufinfo->len; ble_gatts_hvx_params_t hvx_params = { .handle = characteristic->handle, - .type = BLE_GATT_HVX_NOTIFICATION, + .type = hvx_type, .offset = 0, .p_len = &hvx_len, .p_data = bufinfo->buf, @@ -163,30 +213,46 @@ void common_hal_bleio_characteristic_construct(bleio_characteristic_obj_t *self) ble_drv_add_event_handler(on_ble_evt, NULL); } -void common_hal_bleio_characteristic_read_value(bleio_characteristic_obj_t *self) { +void 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); break; + case GATT_ROLE_SERVER: + gatts_read(self); + break; + default: mp_raise_RuntimeError(translate("bad GATT role")); break; } } -void common_hal_bleio_characteristic_write_value(bleio_characteristic_obj_t *self, mp_buffer_info_t *bufinfo) { +void common_hal_bleio_characteristic_set_value(bleio_characteristic_obj_t *self, mp_buffer_info_t *bufinfo) { + bool sent = false; + uint16_t cccd = 0; + switch (common_hal_bleio_device_get_gatt_role(self->service->device)) { case GATT_ROLE_SERVER: - if (self->props.notify) { - gatts_notify(self, bufinfo); - } else { + if (self->props.notify || self->props.indicate) { + cccd = get_cccd(self); + } + // It's possible that both notify and indicate are set. + if (self->props.notify && (cccd & BLE_GATT_HVX_NOTIFICATION)) { + gatts_notify_indicate(self, bufinfo, BLE_GATT_HVX_NOTIFICATION); + sent = true; + } + if (self->props.indicate && (cccd & BLE_GATT_HVX_INDICATION)) { + gatts_notify_indicate(self, bufinfo, BLE_GATT_HVX_INDICATION); + sent = true; + } + if (!sent) { gatts_write(self, bufinfo); } break; case GATT_ROLE_CLIENT: - // TODO: Add indications gattc_write(self, bufinfo); break; diff --git a/ports/nrf/common-hal/bleio/LocalPeripheral.c b/ports/nrf/common-hal/bleio/Peripheral.c similarity index 91% rename from ports/nrf/common-hal/bleio/LocalPeripheral.c rename to ports/nrf/common-hal/bleio/Peripheral.c index 2a502c8dd1..d2eef596d4 100644 --- a/ports/nrf/common-hal/bleio/LocalPeripheral.c +++ b/ports/nrf/common-hal/bleio/Peripheral.c @@ -4,6 +4,7 @@ * The MIT License (MIT) * * Copyright (c) 2018 Artur Pacholec + * Copyright (c) 2018 Dan Halbert for Adafruit Industries * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -35,7 +36,7 @@ #include "py/runtime.h" #include "shared-bindings/bleio/Adapter.h" #include "shared-bindings/bleio/Characteristic.h" -#include "shared-bindings/bleio/LocalPeripheral.h" +#include "shared-bindings/bleio/Peripheral.h" #include "shared-bindings/bleio/Service.h" #include "shared-bindings/bleio/UUID.h" @@ -56,7 +57,7 @@ STATIC void check_data_fit(size_t pos, size_t data_len) { } } -STATIC uint32_t add_services_to_advertisement(bleio_local_peripheral_obj_t *self, size_t* adv_data_pos_p, size_t uuid_len) { +STATIC uint32_t add_services_to_advertisement(bleio_peripheral_obj_t *self, size_t* adv_data_pos_p, size_t uuid_len) { uint32_t uuids_total_size = 0; const mp_obj_list_t *service_list = MP_OBJ_TO_PTR(self->service_list); uint32_t err_code = NRF_SUCCESS; @@ -99,7 +100,7 @@ STATIC uint32_t add_services_to_advertisement(bleio_local_peripheral_obj_t *self -STATIC uint32_t set_advertisement_data(bleio_local_peripheral_obj_t *self, bool connectable, mp_buffer_info_t *raw_data) { +STATIC uint32_t set_advertisement_data(bleio_peripheral_obj_t *self, bool connectable, mp_buffer_info_t *raw_data) { common_hal_bleio_adapter_set_enabled(true); size_t adv_data_pos = 0; @@ -203,7 +204,7 @@ STATIC uint32_t set_advertisement_data(bleio_local_peripheral_obj_t *self, bool m_adv_params.properties.type = BLE_GAP_ADV_TYPE_NONCONNECTABLE_NONSCANNABLE_UNDIRECTED; } - common_hal_bleio_local_peripheral_stop_advertising(self); + common_hal_bleio_peripheral_stop_advertising(self); const ble_gap_adv_data_t ble_gap_adv_data = { .adv_data.p_data = self->adv_data, @@ -221,7 +222,7 @@ STATIC uint32_t set_advertisement_data(bleio_local_peripheral_obj_t *self, bool } STATIC void on_ble_evt(ble_evt_t *ble_evt, void *self_in) { - bleio_local_peripheral_obj_t *self = (bleio_local_peripheral_obj_t*)self_in; + bleio_peripheral_obj_t *self = (bleio_peripheral_obj_t*)self_in; switch (ble_evt->header.evt_id) { case BLE_GAP_EVT_CONNECTED: { @@ -266,14 +267,17 @@ STATIC void on_ble_evt(ble_evt_t *ble_evt, void *self_in) { break; } + default: + mp_printf(&mp_plat_print, "Unhandled event: 0x%04x\n", ble_evt->header.evt_id); + break; } } -void common_hal_bleio_local_peripheral_construct(bleio_local_peripheral_obj_t *self) { +void common_hal_bleio_peripheral_construct(bleio_peripheral_obj_t *self) { common_hal_bleio_adapter_set_enabled(true); // TODO -- Do this somewhere else maybe bleio __init__ - self->gatt_role = GATT_ROLE_NONE; + self->gatt_role = GATT_ROLE_SERVER; self->conn_handle = BLE_CONN_HANDLE_INVALID; // Add all the services. @@ -301,11 +305,11 @@ void common_hal_bleio_local_peripheral_construct(bleio_local_peripheral_obj_t *s } -bool common_hal_bleio_local_peripheral_get_connected(bleio_local_peripheral_obj_t *self) { +bool common_hal_bleio_peripheral_get_connected(bleio_peripheral_obj_t *self) { return self->conn_handle != BLE_CONN_HANDLE_INVALID; } -void common_hal_bleio_local_peripheral_start_advertising(bleio_local_peripheral_obj_t *self, bool connectable, mp_buffer_info_t *raw_data) { +void common_hal_bleio_peripheral_start_advertising(bleio_peripheral_obj_t *self, bool connectable, mp_buffer_info_t *raw_data) { if (connectable) { ble_drv_add_event_handler(on_ble_evt, self); } @@ -316,7 +320,7 @@ void common_hal_bleio_local_peripheral_start_advertising(bleio_local_peripheral_ } } -void common_hal_bleio_local_peripheral_stop_advertising(bleio_local_peripheral_obj_t *self) { +void common_hal_bleio_peripheral_stop_advertising(bleio_peripheral_obj_t *self) { if (m_adv_handle == BLE_GAP_ADV_SET_HANDLE_NOT_SET) return; diff --git a/ports/nrf/common-hal/bleio/LocalPeripheral.h b/ports/nrf/common-hal/bleio/Peripheral.h similarity index 79% rename from ports/nrf/common-hal/bleio/LocalPeripheral.h rename to ports/nrf/common-hal/bleio/Peripheral.h index 8d84867aef..b255fe9f40 100644 --- a/ports/nrf/common-hal/bleio/LocalPeripheral.h +++ b/ports/nrf/common-hal/bleio/Peripheral.h @@ -25,8 +25,8 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_COMMON_HAL_BLEIO_LOCALPERIPHERAL_H -#define MICROPY_INCLUDED_COMMON_HAL_BLEIO_LOCALPERIPHERAL_H +#ifndef MICROPY_INCLUDED_COMMON_HAL_BLEIO_PERIPHERAL_H +#define MICROPY_INCLUDED_COMMON_HAL_BLEIO_PERIPHERAL_H #include @@ -35,17 +35,6 @@ #include "shared-module/bleio/__init__.h" #include "shared-module/bleio/Address.h" -// typedef struct { -// mp_obj_base_t base; -// bool is_peripheral; -// mp_obj_t name; -// bleio_address_obj_t address; -// volatile uint16_t conn_handle; -// mp_obj_t service_list; -// mp_obj_t notif_handler; -// mp_obj_t conn_handler; -// } bleio_device_obj_t; - typedef struct { mp_obj_base_t base; mp_obj_t name; @@ -59,6 +48,6 @@ typedef struct { // there are tricks to get the SD to notice (see DevZone - TBS). uint8_t adv_data[BLE_GAP_ADV_SET_DATA_SIZE_MAX]; -} bleio_local_peripheral_obj_t; +} bleio_peripheral_obj_t; -#endif // MICROPY_INCLUDED_COMMON_HAL_BLEIO_LOCALPERIPHERAL_H +#endif // MICROPY_INCLUDED_COMMON_HAL_BLEIO_PERIPHERAL_H diff --git a/ports/nrf/common-hal/bleio/__init__.c b/ports/nrf/common-hal/bleio/__init__.c index 9429aaf447..cfb701019d 100644 --- a/ports/nrf/common-hal/bleio/__init__.c +++ b/ports/nrf/common-hal/bleio/__init__.c @@ -27,6 +27,7 @@ #include "shared-bindings/bleio/__init__.h" #include "shared-bindings/bleio/Adapter.h" +#include "shared-bindings/bleio/Peripheral.h" #include "common-hal/bleio/__init__.h" // Turn off BLE on a reset or reload. @@ -43,3 +44,25 @@ const super_adapter_obj_t common_hal_bleio_adapter_obj = { .type = &bleio_adapter_type, }, }; + +gatt_role_t common_hal_bleio_device_get_gatt_role(mp_obj_t device) { + if (MP_OBJ_IS_TYPE(device, &bleio_peripheral_type)) { + return ((bleio_peripheral_obj_t*) MP_OBJ_TO_PTR(device))->gatt_role; +// Does not exist yet. +// } else if (MP_OBJ_IS_TYPE(device, &bleio_central_type)) { +// return ((bleio_central_obj_t*) MP_OBJ_TO_PTR(device))->gatt_role; + } else { + return GATT_ROLE_NONE; + } +} + +uint16_t common_hal_bleio_device_get_conn_handle(mp_obj_t device) { + if (MP_OBJ_IS_TYPE(device, &bleio_peripheral_type)) { + return ((bleio_peripheral_obj_t*) MP_OBJ_TO_PTR(device))->conn_handle; +// Does not exist yet. +// } else if (MP_OBJ_IS_TYPE(device, &bleio_central_type)) { +// return ((bleio_central_obj_t*) MP_OBJ_TO_PTR(device))->conn_handle; + } else { + return 0; + } +} diff --git a/ports/nrf/common-hal/bleio/__init__.h b/ports/nrf/common-hal/bleio/__init__.h index aed5df5e76..9e044f37c1 100644 --- a/ports/nrf/common-hal/bleio/__init__.h +++ b/ports/nrf/common-hal/bleio/__init__.h @@ -36,6 +36,7 @@ // 20 bytes max (23 - 3). #define GATT_MAX_DATA_LENGTH (BLE_GATT_ATT_MTU_DEFAULT - 3) +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 diff --git a/py/obj.h b/py/obj.h index 8b67728730..ece3b0ee0d 100644 --- a/py/obj.h +++ b/py/obj.h @@ -640,6 +640,7 @@ mp_obj_t mp_obj_new_str_via_qstr(const char* data, size_t len); mp_obj_t mp_obj_new_str_from_vstr(const mp_obj_type_t *type, vstr_t *vstr); mp_obj_t mp_obj_new_bytes(const byte* data, size_t len); mp_obj_t mp_obj_new_bytearray(size_t n, void *items); +mp_obj_t mp_obj_new_bytearray_of_zeros(size_t n); mp_obj_t mp_obj_new_bytearray_by_ref(size_t n, void *items); #if MICROPY_PY_BUILTINS_FLOAT mp_obj_t mp_obj_new_int_from_float(mp_float_t val); diff --git a/py/objarray.c b/py/objarray.c index 69ff6f328f..0c8c7d8556 100644 --- a/py/objarray.c +++ b/py/objarray.c @@ -611,6 +611,12 @@ mp_obj_t mp_obj_new_bytearray(size_t n, void *items) { return MP_OBJ_FROM_PTR(o); } +mp_obj_t mp_obj_new_bytearray_of_zeros(size_t n) { + mp_obj_array_t *o = array_new(BYTEARRAY_TYPECODE, n); + memset(o->items, 0, n); + return MP_OBJ_FROM_PTR(o); +} + // Create bytearray which references specified memory area mp_obj_t mp_obj_new_bytearray_by_ref(size_t n, void *items) { mp_obj_array_t *o = m_new_obj(mp_obj_array_t); diff --git a/shared-bindings/bleio/Characteristic.c b/shared-bindings/bleio/Characteristic.c index 4fb71de470..d6ae4dfe45 100644 --- a/shared-bindings/bleio/Characteristic.c +++ b/shared-bindings/bleio/Characteristic.c @@ -233,7 +233,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_read_value(self); + common_hal_bleio_characteristic_get_value(self); return self->value_data; } @@ -245,7 +245,7 @@ STATIC mp_obj_t bleio_characteristic_set_value(mp_obj_t self_in, mp_obj_t value_ mp_buffer_info_t bufinfo; mp_get_buffer_raise(value_in, &bufinfo, MP_BUFFER_READ); - common_hal_bleio_characteristic_write_value(self, &bufinfo); + common_hal_bleio_characteristic_set_value(self, &bufinfo); return mp_const_none; } diff --git a/shared-bindings/bleio/Characteristic.h b/shared-bindings/bleio/Characteristic.h index 0e99e97ccd..aec60ebbca 100644 --- a/shared-bindings/bleio/Characteristic.h +++ b/shared-bindings/bleio/Characteristic.h @@ -32,7 +32,7 @@ extern const mp_obj_type_t bleio_characteristic_type; extern void common_hal_bleio_characteristic_construct(bleio_characteristic_obj_t *self); -extern void common_hal_bleio_characteristic_read_value(bleio_characteristic_obj_t *self); -extern void common_hal_bleio_characteristic_write_value(bleio_characteristic_obj_t *self, mp_buffer_info_t *bufinfo); +extern void 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); #endif // MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_CHARACTERISTIC_H diff --git a/shared-bindings/bleio/LocalPeripheral.c b/shared-bindings/bleio/Peripheral.c similarity index 68% rename from shared-bindings/bleio/LocalPeripheral.c rename to shared-bindings/bleio/Peripheral.c index a40e0c0435..1027bbb1ff 100644 --- a/shared-bindings/bleio/LocalPeripheral.c +++ b/shared-bindings/bleio/Peripheral.c @@ -37,20 +37,20 @@ #include "shared-bindings/bleio/Adapter.h" #include "shared-bindings/bleio/AddressType.h" #include "shared-bindings/bleio/Characteristic.h" -#include "shared-bindings/bleio/LocalPeripheral.h" +#include "shared-bindings/bleio/Peripheral.h" #include "shared-bindings/bleio/Service.h" #include "shared-bindings/bleio/UUID.h" #include "shared-module/bleio/AdvertisementData.h" #include "shared-module/bleio/ScanEntry.h" -#include "common-hal/bleio/LocalPeripheral.h" +#include "common-hal/bleio/Peripheral.h" // TODO: Add unique MAC address part to name static const char default_name[] = "CIRCUITPY"; //| .. currentmodule:: bleio //| -//| :class:`LocalPeripheral` -- A BLE peripheral device +//| :class:`Peripheral` -- A BLE peripheral device //| ========================================================= //| //| Implement a BLE peripheral which runs locally. @@ -67,25 +67,25 @@ static const char default_name[] = "CIRCUITPY"; //| serv = bleio.Service(bleio.UUID(0x180f), [chara]) //| //| # Create a peripheral and start it up. -//| periph = bleio.LocalPeripheral([service]) +//| periph = bleio.Peripheral([service]) //| periph.start_advertising() //| //| while not periph.connected(): //| # Wait for connection. //| pass //| -//| .. class:: LocalPeripheral(services, *, name='CIRCUITPY') +//| .. class:: Peripheral(services, *, name='CIRCUITPY') //| -//| Create a new LocalPeripheral object. +//| Create a new Peripheral object. //| :param iterable services: the Service objects representing services available from this peripheral. //| :param str name: The name used when advertising this peripheral //| -STATIC mp_obj_t bleio_local_peripheral_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *pos_args) { +STATIC mp_obj_t bleio_peripheral_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); - bleio_local_peripheral_obj_t *self = m_new_obj(bleio_local_peripheral_obj_t); - self->base.type = &bleio_local_peripheral_type; + 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; @@ -125,7 +125,7 @@ STATIC mp_obj_t bleio_local_peripheral_make_new(const mp_obj_type_t *type, size_ } // Do port-specific initialization. - common_hal_bleio_local_peripheral_construct(self); + common_hal_bleio_peripheral_construct(self); return MP_OBJ_FROM_PTR(self); } @@ -134,17 +134,17 @@ STATIC mp_obj_t bleio_local_peripheral_make_new(const mp_obj_type_t *type, size_ //| //| True if connected to a BLE Central device. //| -STATIC mp_obj_t bleio_local_peripheral_get_connected(mp_obj_t self_in) { - bleio_local_peripheral_obj_t *self = MP_OBJ_TO_PTR(self_in); +STATIC mp_obj_t bleio_peripheral_get_connected(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. - return mp_obj_new_bool(common_hal_bleio_local_peripheral_get_connected(self)); + return mp_obj_new_bool(common_hal_bleio_peripheral_get_connected(self)); } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_local_peripheral_get_connected_obj, bleio_local_peripheral_get_connected); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_peripheral_get_connected_obj, bleio_peripheral_get_connected); -const mp_obj_property_t bleio_local_peripheral_connected_obj = { +const mp_obj_property_t bleio_peripheral_connected_obj = { .base.type = &mp_type_property, - .proxy = { (mp_obj_t)&bleio_local_peripheral_get_connected_obj, + .proxy = { (mp_obj_t)&bleio_peripheral_get_connected_obj, (mp_obj_t)&mp_const_none_obj, (mp_obj_t)&mp_const_none_obj }, }; @@ -153,17 +153,17 @@ const mp_obj_property_t bleio_local_peripheral_connected_obj = { //| //| A `tuple` of `bleio.Service` that are offered by this peripheral. (read-only) //| -STATIC mp_obj_t bleio_local_peripheral_get_services(mp_obj_t self_in) { - bleio_local_peripheral_obj_t *self = MP_OBJ_TO_PTR(self_in); +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); return mp_obj_new_tuple(service_list->len, service_list->items); } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_local_peripheral_get_services_obj, bleio_local_peripheral_get_services); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_peripheral_get_services_obj, bleio_peripheral_get_services); -const mp_obj_property_t bleio_local_peripheral_services_obj = { +const mp_obj_property_t bleio_peripheral_services_obj = { .base.type = &mp_type_property, - .proxy = { (mp_obj_t)&bleio_local_peripheral_get_services_obj, + .proxy = { (mp_obj_t)&bleio_peripheral_get_services_obj, (mp_obj_t)&mp_const_none_obj, (mp_obj_t)&mp_const_none_obj }, }; @@ -172,16 +172,16 @@ const mp_obj_property_t bleio_local_peripheral_services_obj = { //| //| The peripheral's name, included when advertising. (read-only) //| -STATIC mp_obj_t bleio_local_peripheral_get_name(mp_obj_t self_in) { - bleio_local_peripheral_obj_t *self = MP_OBJ_TO_PTR(self_in); +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; } -MP_DEFINE_CONST_FUN_OBJ_1(bleio_local_peripheral_get_name_obj, bleio_local_peripheral_get_name); +MP_DEFINE_CONST_FUN_OBJ_1(bleio_peripheral_get_name_obj, bleio_peripheral_get_name); -const mp_obj_property_t bleio_local_peripheral_name_obj = { +const mp_obj_property_t bleio_peripheral_name_obj = { .base.type = &mp_type_property, - .proxy = { (mp_obj_t)&bleio_local_peripheral_get_name_obj, + .proxy = { (mp_obj_t)&bleio_peripheral_get_name_obj, (mp_obj_t)&mp_const_none_obj, (mp_obj_t)&mp_const_none_obj }, }; @@ -194,8 +194,8 @@ const mp_obj_property_t bleio_local_peripheral_name_obj = { //| :param bool connectable: If `True` then other devices are allowed to connect to this peripheral. //| :param buf data: If not None, then send data bytes in advertising packets. //| -STATIC mp_obj_t bleio_local_peripheral_start_advertising(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { - bleio_local_peripheral_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); +STATIC mp_obj_t bleio_peripheral_start_advertising(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + bleio_peripheral_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); enum { ARG_connectable, ARG_data }; static const mp_arg_t allowed_args[] = { @@ -211,40 +211,40 @@ STATIC mp_obj_t bleio_local_peripheral_start_advertising(mp_uint_t n_args, const mp_get_buffer_raise(args[ARG_data].u_obj, &bufinfo, MP_BUFFER_READ); } - common_hal_bleio_local_peripheral_start_advertising(self, args[ARG_connectable].u_bool, &bufinfo); + common_hal_bleio_peripheral_start_advertising(self, args[ARG_connectable].u_bool, &bufinfo); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_KW(bleio_local_peripheral_start_advertising_obj, 0, bleio_local_peripheral_start_advertising); +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(bleio_peripheral_start_advertising_obj, 0, bleio_peripheral_start_advertising); //| .. method:: stop_advertising() //| //| Stop sending advertising packets. -STATIC mp_obj_t bleio_local_peripheral_stop_advertising(mp_obj_t self_in) { - bleio_local_peripheral_obj_t *self = MP_OBJ_TO_PTR(self_in); +STATIC mp_obj_t bleio_peripheral_stop_advertising(mp_obj_t self_in) { + bleio_peripheral_obj_t *self = MP_OBJ_TO_PTR(self_in); - common_hal_bleio_local_peripheral_stop_advertising(self); + common_hal_bleio_peripheral_stop_advertising(self); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_local_peripheral_stop_advertising_obj, bleio_local_peripheral_stop_advertising); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_peripheral_stop_advertising_obj, bleio_peripheral_stop_advertising); -STATIC const mp_rom_map_elem_t bleio_local_peripheral_locals_dict_table[] = { +STATIC const mp_rom_map_elem_t bleio_peripheral_locals_dict_table[] = { // Methods - { MP_ROM_QSTR(MP_QSTR_start_advertising), MP_ROM_PTR(&bleio_local_peripheral_start_advertising_obj) }, - { MP_ROM_QSTR(MP_QSTR_stop_advertising), MP_ROM_PTR(&bleio_local_peripheral_stop_advertising_obj) }, + { MP_ROM_QSTR(MP_QSTR_start_advertising), MP_ROM_PTR(&bleio_peripheral_start_advertising_obj) }, + { MP_ROM_QSTR(MP_QSTR_stop_advertising), MP_ROM_PTR(&bleio_peripheral_stop_advertising_obj) }, // Properties - { MP_ROM_QSTR(MP_QSTR_connected), MP_ROM_PTR(&bleio_local_peripheral_connected_obj) }, - { MP_ROM_QSTR(MP_QSTR_name), MP_ROM_PTR(&bleio_local_peripheral_name_obj) }, - { MP_ROM_QSTR(MP_QSTR_services), MP_ROM_PTR(&bleio_local_peripheral_services_obj) }, + { MP_ROM_QSTR(MP_QSTR_connected), MP_ROM_PTR(&bleio_peripheral_connected_obj) }, + { MP_ROM_QSTR(MP_QSTR_name), MP_ROM_PTR(&bleio_peripheral_name_obj) }, + { MP_ROM_QSTR(MP_QSTR_services), MP_ROM_PTR(&bleio_peripheral_services_obj) }, }; -STATIC MP_DEFINE_CONST_DICT(bleio_local_peripheral_locals_dict, bleio_local_peripheral_locals_dict_table); +STATIC MP_DEFINE_CONST_DICT(bleio_peripheral_locals_dict, bleio_peripheral_locals_dict_table); -const mp_obj_type_t bleio_local_peripheral_type = { +const mp_obj_type_t bleio_peripheral_type = { { &mp_type_type }, - .name = MP_QSTR_LocalPeripheral, - .make_new = bleio_local_peripheral_make_new, - .locals_dict = (mp_obj_dict_t*)&bleio_local_peripheral_locals_dict + .name = MP_QSTR_Peripheral, + .make_new = bleio_peripheral_make_new, + .locals_dict = (mp_obj_dict_t*)&bleio_peripheral_locals_dict }; diff --git a/shared-bindings/bleio/LocalPeripheral.h b/shared-bindings/bleio/Peripheral.h similarity index 62% rename from shared-bindings/bleio/LocalPeripheral.h rename to shared-bindings/bleio/Peripheral.h index bd6d5cbbbe..02a5c1ef8e 100644 --- a/shared-bindings/bleio/LocalPeripheral.h +++ b/shared-bindings/bleio/Peripheral.h @@ -4,6 +4,7 @@ * The MIT License (MIT) * * Copyright (c) 2018 Artur Pacholec + * Copyright (c) 2018 Dan Halbert for Adafruit Industries * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -24,16 +25,16 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_LOCALPERIPHERAL_H -#define MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_LOCALPERIPHERAL_H +#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_PERIPHERAL_H +#define MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_PERIPHERAL_H -#include "common-hal/bleio/LocalPeripheral.h" +#include "common-hal/bleio/Peripheral.h" -extern const mp_obj_type_t bleio_local_peripheral_type; +extern const mp_obj_type_t bleio_peripheral_type; -extern void common_hal_bleio_local_peripheral_construct(bleio_local_peripheral_obj_t *self); -extern bool common_hal_bleio_local_peripheral_get_connected(bleio_local_peripheral_obj_t *self); -extern void common_hal_bleio_local_peripheral_start_advertising(bleio_local_peripheral_obj_t *device, bool connectable, mp_buffer_info_t *raw_data); -extern void common_hal_bleio_local_peripheral_stop_advertising(bleio_local_peripheral_obj_t *device); +extern void common_hal_bleio_peripheral_construct(bleio_peripheral_obj_t *self); +extern bool common_hal_bleio_peripheral_get_connected(bleio_peripheral_obj_t *self); +extern void common_hal_bleio_peripheral_start_advertising(bleio_peripheral_obj_t *device, bool connectable, mp_buffer_info_t *raw_data); +extern void common_hal_bleio_peripheral_stop_advertising(bleio_peripheral_obj_t *device); -#endif // MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_LOCALPERIPHERAL_H +#endif // MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_PERIPHERAL_H diff --git a/shared-bindings/bleio/__init__.c b/shared-bindings/bleio/__init__.c index b359071595..d30d599421 100644 --- a/shared-bindings/bleio/__init__.c +++ b/shared-bindings/bleio/__init__.c @@ -32,7 +32,7 @@ #include "shared-bindings/bleio/Broadcaster.h" #include "shared-bindings/bleio/Characteristic.h" #include "shared-bindings/bleio/Descriptor.h" -#include "shared-bindings/bleio/PeripheralServer.h" +#include "shared-bindings/bleio/Peripheral.h" #include "shared-bindings/bleio/ScanEntry.h" #include "shared-bindings/bleio/Scanner.h" #include "shared-bindings/bleio/Service.h" @@ -78,7 +78,7 @@ STATIC const mp_rom_map_elem_t bleio_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_Broadcaster), MP_ROM_PTR(&bleio_broadcaster_type) }, { MP_ROM_QSTR(MP_QSTR_Characteristic), MP_ROM_PTR(&bleio_characteristic_type) }, { MP_ROM_QSTR(MP_QSTR_Descriptor), MP_ROM_PTR(&bleio_descriptor_type) }, - { MP_ROM_QSTR(MP_QSTR_PeripheralServer), MP_ROM_PTR(&bleio_peripheral_server_type) }, + { MP_ROM_QSTR(MP_QSTR_Peripheral), MP_ROM_PTR(&bleio_peripheral_type) }, { MP_ROM_QSTR(MP_QSTR_ScanEntry), MP_ROM_PTR(&bleio_scanentry_type) }, { MP_ROM_QSTR(MP_QSTR_Scanner), MP_ROM_PTR(&bleio_scanner_type) }, { MP_ROM_QSTR(MP_QSTR_Service), MP_ROM_PTR(&bleio_service_type) }, diff --git a/shared-module/bleio/Service.h b/shared-module/bleio/Service.h index 7fee57f1d0..828d4cdc87 100644 --- a/shared-module/bleio/Service.h +++ b/shared-module/bleio/Service.h @@ -34,7 +34,7 @@ typedef struct { uint16_t handle; bool is_secondary; bleio_uuid_obj_t *uuid; - // May be a PeripheralServer, CentralClient, etc. + // May be a Peripheral, Central, etc. mp_obj_t *device; mp_obj_t char_list; uint16_t start_handle; diff --git a/shared-module/bleio/__init__.h b/shared-module/bleio/__init__.h index f8f0e06606..07d1485946 100644 --- a/shared-module/bleio/__init__.h +++ b/shared-module/bleio/__init__.h @@ -27,6 +27,12 @@ #ifndef MICROPY_INCLUDED_SHARED_MODULE_BLEIO_INIT_H #define MICROPY_INCLUDED_SHARED_MODULE_BLEIO_INIT_H +typedef enum { + GATT_ROLE_NONE, + GATT_ROLE_SERVER, + GATT_ROLE_CLIENT, +} gatt_role_t; + extern void bleio_reset(void); #endif // MICROPY_INCLUDED_SHARED_MODULE_BLEIO_INIT_H