wip: advertising works, but not connection

This commit is contained in:
Dan Halbert 2018-12-27 00:04:04 -05:00
parent f5b15c9b4d
commit 4167bf5b24
17 changed files with 174 additions and 126 deletions

View File

@ -213,7 +213,7 @@ These provide functionality similar to `analogio`, `digitalio`, `pulseio`, and `
Adafruit SeeSaw <https://circuitpython.readthedocs.io/projects/seesaw/en/latest/>
ADS1x15 Analog-to-Digital Converter <https://circuitpython.readthedocs.io/projects/ads1x15/en/latest/>
Crickit Robotics Boards <<https://circuitpython.readthedocs.io/projects/crickit/en/latest/>
Crickit Robotics Boards <https://circuitpython.readthedocs.io/projects/crickit/en/latest/>
DS2413 OneWire GPIO Expander <https://circuitpython.readthedocs.io/projects/ds2413/en/latest/>
FocalTech Capacitive Touch <https://circuitpython.readthedocs.io/projects/focaltouch/en/latest/>
MCP230xx GPIO Expander <https://circuitpython.readthedocs.io/projects/mcp230xx/en/latest/>

View File

@ -81,9 +81,10 @@ BASE_CFLAGS = \
-DCIRCUITPY_SAFE_RESTART_WORD=0xDEADBEEF \
--param max-inline-insns-single=500
# Use these flags to debug build times and header includes.
# -ftime-report
# -H
# Use these flags to debug build times and header includes.
# -ftime-report
# -H
# NDEBUG disables assert() statements. This reduces code size pretty dramatically, per tannewt.
ifeq ($(CHIP_FAMILY), samd21)

View File

@ -79,12 +79,14 @@ CFLAGS += -DCFG_TUSB_MCU=OPT_MCU_NRF5X -DCFG_TUD_CDC_RX_BUFSIZE=1024 -DCFG_TUD_C
#Debugging/Optimization
ifeq ($(DEBUG), 1)
#ASMFLAGS += -g -gtabs+
CFLAGS += -O1 -ggdb
LDFLAGS += -O1
#ASMFLAGS += -g -gtabs+
CFLAGS += -O1 -ggdb
LDFLAGS += -O1
# You may want to enable these flags to make setting breakpoints easier.
CFLAGS += -fno-inline -fno-ipa-sra
else
CFLAGS += -Os -DNDEBUG
LDFLAGS += -Os
CFLAGS += -Os -DNDEBUG
LDFLAGS += -Os
endif
LIBM_FILE_NAME = $(shell $(CC) $(CFLAGS) -print-file-name=libm.a)
@ -172,7 +174,7 @@ SRC_COMMON_HAL += \
bleio/Adapter.c \
bleio/Characteristic.c \
bleio/Descriptor.c \
bleio/Device.c \
bleio/LocalPeripheral.c \
bleio/Scanner.c \
bleio/Service.c \
bleio/UUID.c

View File

@ -30,16 +30,19 @@
#include "ble_drv.h"
#include "ble_gatts.h"
#include "nrf_soc.h"
#include "py/runtime.h"
#include "common-hal/bleio/__init__.h"
#include "shared-module/bleio/Characteristic.h"
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 volatile bleio_characteristic_obj_t *m_read_characteristic;
STATIC volatile uint8_t m_tx_in_progress;
STATIC nrf_mutex_t *m_write_mutex;
STATIC void gatts_write(bleio_characteristic_obj_t *characteristic, mp_buffer_info_t *bufinfo) {
bleio_device_obj_t *device = characteristic->service->device;
const uint16_t conn_handle = device->conn_handle;
const uint16_t conn_handle = common_hal_bleio_device_get_conn_handle(characteristic->service->device);
ble_gatts_value_t gatts_value = {
.p_value = bufinfo->buf,
@ -48,17 +51,17 @@ STATIC void gatts_write(bleio_characteristic_obj_t *characteristic, mp_buffer_in
const uint32_t err_code = sd_ble_gatts_value_set(conn_handle, characteristic->handle, &gatts_value);
if (err_code != NRF_SUCCESS) {
mp_raise_OSError_msg(translate("Failed to write gatts value"));
mp_raise_OSError_msg_varg(translate("Failed to write gatts value, err 0x%04x"), err_code);
}
}
STATIC void gatts_notify(bleio_characteristic_obj_t *characteristic, mp_buffer_info_t *bufinfo) {
bleio_device_obj_t *device = characteristic->service->device;
uint16_t hvx_len = bufinfo->len;
ble_gatts_hvx_params_t hvx_params = {
.handle = characteristic->handle,
.type = BLE_GATT_HVX_NOTIFICATION,
.offset = 0,
.p_len = &hvx_len,
.p_data = bufinfo->buf,
};
@ -69,25 +72,26 @@ STATIC void gatts_notify(bleio_characteristic_obj_t *characteristic, mp_buffer_i
#endif
}
const uint32_t err_code = sd_ble_gatts_hvx(device->conn_handle, &hvx_params);
const uint16_t conn_handle = common_hal_bleio_device_get_conn_handle(characteristic->service->device);
const uint32_t err_code = sd_ble_gatts_hvx(conn_handle, &hvx_params);
if (err_code != NRF_SUCCESS) {
mp_raise_OSError_msg(translate("Failed to notify attribute value"));
mp_raise_OSError_msg_varg(translate("Failed to notify attribute value, err %0x04x"), err_code);
}
m_tx_in_progress += 1;
}
STATIC void gattc_read(bleio_characteristic_obj_t *characteristic) {
bleio_service_obj_t *service = characteristic->service;
bleio_device_obj_t *device = service->device;
const uint16_t conn_handle = common_hal_bleio_device_get_conn_handle(characteristic->service->device);
m_read_characteristic = characteristic;
const uint32_t err_code = sd_ble_gattc_read(device->conn_handle, characteristic->handle, 0);
const uint32_t err_code = sd_ble_gattc_read(conn_handle, characteristic->handle, 0);
if (err_code != NRF_SUCCESS) {
mp_raise_OSError_msg(translate("Failed to read attribute value"));
mp_raise_OSError_msg_varg(translate("Failed to read attribute value, err %0x04x"), err_code);
}
//
while (m_read_characteristic != NULL) {
#ifdef MICROPY_VM_HOOK_LOOP
MICROPY_VM_HOOK_LOOP
@ -96,7 +100,7 @@ STATIC void gattc_read(bleio_characteristic_obj_t *characteristic) {
}
STATIC void gattc_write(bleio_characteristic_obj_t *characteristic, mp_buffer_info_t *bufinfo) {
bleio_device_obj_t *device = characteristic->service->device;
const uint16_t conn_handle = common_hal_bleio_device_get_conn_handle(characteristic->service->device);
uint32_t err_code;
ble_gattc_write_params_t write_params = {
@ -112,13 +116,13 @@ STATIC void gattc_write(bleio_characteristic_obj_t *characteristic, mp_buffer_in
err_code = sd_mutex_acquire(m_write_mutex);
if (err_code != NRF_SUCCESS) {
mp_raise_OSError_msg(translate("Failed to acquire mutex"));
mp_raise_OSError_msg_varg(translate("Failed to acquire mutex, err 0x%04x"), err_code);
}
}
err_code = sd_ble_gattc_write(device->conn_handle, &write_params);
err_code = sd_ble_gattc_write(conn_handle, &write_params);
if (err_code != NRF_SUCCESS) {
mp_raise_OSError_msg(translate("Failed to write attribute value"));
mp_raise_OSError_msg_varg(translate("Failed to write attribute value, err 0x%04x"), err_code);
}
while (sd_mutex_acquire(m_write_mutex) == NRF_ERROR_SOC_MUTEX_ALREADY_TAKEN) {
@ -129,33 +133,28 @@ STATIC void gattc_write(bleio_characteristic_obj_t *characteristic, mp_buffer_in
err_code = sd_mutex_release(m_write_mutex);
if (err_code != NRF_SUCCESS) {
mp_raise_OSError_msg(translate("Failed to release mutex"));
mp_raise_OSError_msg_varg(translate("Failed to release mutex, err 0x%04x"), err_code);
}
}
STATIC void on_ble_evt(ble_evt_t *ble_evt, void *param) {
switch (ble_evt->header.evt_id) {
#if (BLE_API_VERSION == 4)
case BLE_GATTS_EVT_HVN_TX_COMPLETE:
m_tx_in_progress -= ble_evt->evt.gatts_evt.params.hvn_tx_complete.count;
break;
#else
case BLE_EVT_TX_COMPLETE:
m_tx_in_progress -= ble_evt->evt.common_evt.params.tx_complete.count;
break;
#endif
case BLE_GATTC_EVT_READ_RSP:
{
ble_gattc_evt_read_rsp_t *response = &ble_evt->evt.gattc_evt.params.read_rsp;
m_read_characteristic->value_data = mp_obj_new_bytearray(response->len, response->data);
// Flag to busy-wait loop that we've read the characteristic.
m_read_characteristic = NULL;
break;
}
case BLE_GATTC_EVT_WRITE_RSP:
// Someone else can write now.
sd_mutex_release(m_write_mutex);
// m_write_done = true;
break;
}
}
@ -165,21 +164,34 @@ void common_hal_bleio_characteristic_construct(bleio_characteristic_obj_t *self)
}
void common_hal_bleio_characteristic_read_value(bleio_characteristic_obj_t *self) {
gattc_read(self);
switch (common_hal_bleio_device_get_gatt_role(self->service->device)) {
case GATT_ROLE_CLIENT:
gattc_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) {
const bleio_device_obj_t *device = self->service->device;
if (device->is_peripheral) {
// TODO: Add indications
switch (common_hal_bleio_device_get_gatt_role(self->service->device)) {
case GATT_ROLE_SERVER:
if (self->props.notify) {
gatts_notify(self, bufinfo);
} else {
gatts_write(self, bufinfo);
}
} else {
gattc_write(self, bufinfo);
}
break;
case GATT_ROLE_CLIENT:
// TODO: Add indications
gattc_write(self, bufinfo);
break;
default:
mp_raise_RuntimeError(translate("bad GATT role"));
break;
}
}

View File

@ -72,7 +72,7 @@ STATIC void on_ble_evt(ble_evt_t *ble_evt, void *scanner_in) {
#if (BLUETOOTH_SD == 140)
const uint32_t err_code = sd_ble_gap_scan_start(NULL, &m_scan_buffer);
if (err_code != NRF_SUCCESS) {
mp_raise_OSError_msg(translate("Failed to continue scanning"));
mp_raise_OSError_msg_varg(translate("Failed to continue scanning, err 0x%04x"), err_code);
}
#endif
}
@ -98,7 +98,7 @@ void common_hal_bleio_scanner_scan(bleio_scanner_obj_t *self, mp_int_t timeout)
#endif
if (err_code != NRF_SUCCESS) {
mp_raise_OSError_msg(translate("Failed to start scanning"));
mp_raise_OSError_msg_varg(translate("Failed to start scanning, err 0x%04x"), err_code);
}
if (timeout > 0) {

View File

@ -27,58 +27,69 @@
#include "ble_drv.h"
#include "ble.h"
#include "py/runtime.h"
#include "common-hal/bleio/__init__.h"
#include "shared-bindings/bleio/Service.h"
#include "shared-bindings/bleio/Adapter.h"
void common_hal_bleio_service_add_characteristic(bleio_service_obj_t *self, bleio_characteristic_obj_t *characteristic) {
ble_gatts_char_md_t char_md = {
.char_props.broadcast = characteristic->props.broadcast,
.char_props.read = characteristic->props.read,
.char_props.write_wo_resp = characteristic->props.write_no_response,
.char_props.write = characteristic->props.write,
.char_props.notify = characteristic->props.notify,
.char_props.indicate = characteristic->props.indicate,
};
ble_gatts_attr_md_t cccd_md = {
.vloc = BLE_GATTS_VLOC_STACK,
};
if (char_md.char_props.notify || char_md.char_props.indicate) {
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm);
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.write_perm);
char_md.p_cccd_md = &cccd_md;
}
ble_uuid_t uuid;
bleio_uuid_convert_to_nrf_ble_uuid(characteristic->uuid, &uuid);
ble_gatts_attr_md_t attr_md = {
.vloc = BLE_GATTS_VLOC_STACK,
.vlen = 1,
};
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm);
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm);
ble_gatts_attr_t attr_char_value = {
.p_uuid = &uuid,
.p_attr_md = &attr_md,
.init_len = sizeof(uint8_t),
.max_len = (BLE_GATT_ATT_MTU_DEFAULT - 3),
};
ble_gatts_char_handles_t handles;
uint32_t err_code;
err_code = sd_ble_gatts_characteristic_add(self->handle, &char_md, &attr_char_value, &handles);
if (err_code != NRF_SUCCESS) {
mp_raise_OSError_msg(translate("Could not add characteristic"));
}
characteristic->user_desc_handle = handles.user_desc_handle;
characteristic->cccd_handle = handles.cccd_handle;
characteristic->sccd_handle = handles.sccd_handle;
characteristic->handle = handles.value_handle;
void common_hal_bleio_service_construct(bleio_service_obj_t *self) {
}
// Call this after the Service has been added to the LocalPeripheral.
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];
ble_gatts_char_md_t char_md = {
.char_props.broadcast = characteristic->props.broadcast,
.char_props.read = characteristic->props.read,
.char_props.write_wo_resp = characteristic->props.write_no_response,
.char_props.write = characteristic->props.write,
.char_props.notify = characteristic->props.notify,
.char_props.indicate = characteristic->props.indicate,
};
ble_gatts_attr_md_t cccd_md = {
.vloc = BLE_GATTS_VLOC_STACK,
};
if (char_md.char_props.notify || char_md.char_props.indicate) {
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm);
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.write_perm);
char_md.p_cccd_md = &cccd_md;
}
ble_uuid_t uuid;
bleio_uuid_convert_to_nrf_ble_uuid(characteristic->uuid, &uuid);
ble_gatts_attr_md_t attr_md = {
.vloc = BLE_GATTS_VLOC_STACK,
.vlen = 1,
};
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm);
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm);
ble_gatts_attr_t attr_char_value = {
.p_uuid = &uuid,
.p_attr_md = &attr_md,
.init_len = sizeof(uint8_t),
.max_len = GATT_MAX_DATA_LENGTH,
};
ble_gatts_char_handles_t handles;
uint32_t err_code;
err_code = sd_ble_gatts_characteristic_add(self->handle, &char_md, &attr_char_value, &handles);
if (err_code != NRF_SUCCESS) {
mp_raise_OSError_msg_varg(translate("Failed to add characteristic, err 0x%04x"), err_code);
}
characteristic->user_desc_handle = handles.user_desc_handle;
characteristic->cccd_handle = handles.cccd_handle;
characteristic->sccd_handle = handles.sccd_handle;
characteristic->handle = handles.value_handle;
}
}

View File

@ -51,7 +51,7 @@ void common_hal_bleio_uuid_construct(bleio_uuid_obj_t *self, uint32_t uuid16, ui
// Register this vendor-specific UUID. Bytes 12 and 13 will be zero.
const uint32_t err_code = sd_ble_uuid_vs_add(&vs_uuid, &self->nrf_ble_uuid.type);
if (err_code != NRF_SUCCESS) {
mp_raise_OSError_msg(translate("Could not register Vendor-Specific UUID"));
mp_raise_OSError_msg_varg(translate("Failed to register Vendor-Specific UUID, err 0x%04x"), err_code);
}
}
}
@ -70,7 +70,7 @@ bool common_hal_bleio_uuid_get_uuid128(bleio_uuid_obj_t *self, uint8_t uuid128[1
const uint32_t err_code = sd_ble_uuid_encode(&self->nrf_ble_uuid, &length, uuid128);
if (err_code != NRF_SUCCESS) {
mp_raise_RuntimeError(translate("Could not decode ble_uuid"));
mp_raise_OSError_msg_varg(translate("Could not decode ble_uuid, err 0x%04x"), err_code);
}
// If not 16 bytes, this is not a 128-bit UUID, so return.
return length == 16;

View File

@ -27,6 +27,8 @@
#include "shared-bindings/bleio/__init__.h"
#include "shared-bindings/bleio/Adapter.h"
#include "shared-bindings/bleio/LocalPeripheral.h"
#include "common-hal/bleio/__init__.h"
// The singleton bleio.Adapter object, bound to bleio.adapter
// It currently only has properties and no state
@ -35,3 +37,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_local_peripheral_type)) {
return ((bleio_local_peripheral_obj_t*) MP_OBJ_TO_PTR(device))->gatt_role;
// Does not exist yet.
// } else if (MP_OBJ_IS_TYPE(device, &bleio_local_central_type)) {
// return ((bleio_local_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_local_peripheral_type)) {
return ((bleio_local_peripheral_obj_t*) MP_OBJ_TO_PTR(device))->conn_handle;
// Does not exist yet.
// } else if (MP_OBJ_IS_TYPE(device, &bleio_local_central_type)) {
// return ((bleio_local_central_obj_t*) MP_OBJ_TO_PTR(device))->conn_handle;
} else {
return 0;
}
}

View File

@ -1603,6 +1603,14 @@ NORETURN void mp_raise_OSError_msg(const compressed_string_t *msg) {
mp_raise_msg(&mp_type_OSError, msg);
}
NORETURN void mp_raise_OSError_msg_varg(const compressed_string_t *fmt, ...) {
va_list argptr;
va_start(argptr,fmt);
mp_obj_t exception = mp_obj_new_exception_msg_vlist(&mp_type_OSError, fmt, argptr);
va_end(argptr);
nlr_raise(exception);
}
NORETURN void mp_raise_NotImplementedError(const compressed_string_t *msg) {
mp_raise_msg(&mp_type_NotImplementedError, msg);
}

View File

@ -159,6 +159,7 @@ NORETURN void mp_raise_ImportError(const compressed_string_t *msg);
NORETURN void mp_raise_IndexError(const compressed_string_t *msg);
NORETURN void mp_raise_OSError(int errno_);
NORETURN void mp_raise_OSError_msg(const compressed_string_t *msg);
NORETURN void mp_raise_OSError_msg_varg(const compressed_string_t *fmt, ...);
NORETURN void mp_raise_NotImplementedError(const compressed_string_t *msg);
NORETURN void mp_raise_recursion_depth(void);

View File

@ -80,7 +80,7 @@ STATIC mp_obj_t bleio_address_make_new(const mp_obj_type_t *type, size_t n_args,
enum { ARG_address };
static const mp_arg_t allowed_args[] = {
{ ARG_address, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_address, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];

View File

@ -81,7 +81,7 @@ STATIC mp_obj_t bleio_descriptor_make_new(const mp_obj_type_t *type, size_t n_ar
enum { ARG_uuid };
static const mp_arg_t allowed_args[] = {
{ ARG_uuid, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_uuid, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
@ -99,13 +99,6 @@ STATIC mp_obj_t bleio_descriptor_make_new(const mp_obj_type_t *type, size_t n_ar
return MP_OBJ_FROM_PTR(self);
}
STATIC void bleio_descriptor_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
bleio_descriptor_obj_t *self = MP_OBJ_TO_PTR(self_in);
mp_printf(print, "Descriptor(uuid=");
bleio_uuid_print(print, self->uuid, kind);
mp_printf(print, ", handle=%04x", common_hal_bleio_descriptor_get_handle(self));
}
STATIC mp_obj_t bleio_descriptor_get_handle(mp_obj_t self_in) {
bleio_descriptor_obj_t *self = MP_OBJ_TO_PTR(self_in);
@ -161,7 +154,6 @@ STATIC MP_DEFINE_CONST_DICT(bleio_descriptor_locals_dict, bleio_descriptor_local
const mp_obj_type_t bleio_descriptor_type = {
{ &mp_type_type },
.name = MP_QSTR_Descriptor,
.print = bleio_descriptor_print,
.make_new = bleio_descriptor_make_new,
.locals_dict = (mp_obj_dict_t*)&bleio_descriptor_locals_dict
};

View File

@ -171,7 +171,7 @@ STATIC mp_obj_t bleio_device_make_new(const mp_obj_type_t *type, size_t n_args,
enum { ARG_address, ARG_scan_entry };
static const mp_arg_t allowed_args[] = {
{ ARG_address, MP_ARG_OBJ, {.u_obj = mp_const_none} },
{ 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} },
};

View File

@ -45,23 +45,16 @@
//| To mark the service as secondary, pass `True` as :py:data:`secondary`.
//|
//| :param bleio.UUID uuid: The uuid of the service
//| :param iterable characteristics: the Characteristic objects for this service
//| :param bool secondary: If the service is a secondary one
//|
STATIC void bleio_service_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
bleio_service_obj_t *self = MP_OBJ_TO_PTR(self_in);
mp_printf(print, "Service(");
bleio_uuid_print(print, self->uuid, kind);
mp_printf(print, ")");
}
STATIC mp_obj_t bleio_service_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, 2, 3, true);
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 = NULL;
self->device = mp_const_none;
self->handle = 0xFFFF;
mp_map_t kw_args;
@ -69,7 +62,7 @@ STATIC mp_obj_t bleio_service_make_new(const mp_obj_type_t *type, size_t n_args,
enum { ARG_uuid, ARG_characteristics, ARG_secondary };
static const mp_arg_t allowed_args[] = {
{ ARG_uuid, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = mp_const_none} },
{ MP_QSTR_uuid, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = mp_const_none} },
{ MP_QSTR_characteristics, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = mp_const_none} },
{ MP_QSTR_secondary, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
};
@ -98,13 +91,17 @@ STATIC mp_obj_t bleio_service_make_new(const mp_obj_type_t *type, size_t n_args,
}
bleio_characteristic_obj_t *characteristic_ptr = MP_OBJ_TO_PTR(characteristic);
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_ptr->uuid)) {
// 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);
}
// Do port-specific initialization.
common_hal_bleio_service_construct(self);
return MP_OBJ_FROM_PTR(self);
}
@ -155,7 +152,6 @@ STATIC MP_DEFINE_CONST_DICT(bleio_service_locals_dict, bleio_service_locals_dict
const mp_obj_type_t bleio_service_type = {
{ &mp_type_type },
.name = MP_QSTR_Service,
.print = bleio_service_print,
.make_new = bleio_service_make_new,
.locals_dict = (mp_obj_dict_t*)&bleio_service_locals_dict
};

View File

@ -32,6 +32,7 @@
const mp_obj_type_t bleio_service_type;
extern void common_hal_bleio_service_add_characteristic(bleio_service_obj_t *self, bleio_characteristic_obj_t *characteristic);
extern void common_hal_bleio_service_construct(bleio_service_obj_t *self);
extern void common_hal_bleio_service_add_all_characteristics(bleio_service_obj_t *self);
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_SERVICE_H

View File

@ -31,7 +31,7 @@
#include "shared-bindings/bleio/AdvertisementData.h"
#include "shared-bindings/bleio/Characteristic.h"
#include "shared-bindings/bleio/Descriptor.h"
#include "shared-bindings/bleio/Device.h"
#include "shared-bindings/bleio/LocalPeripheral.h"
#include "shared-bindings/bleio/ScanEntry.h"
#include "shared-bindings/bleio/Scanner.h"
#include "shared-bindings/bleio/Service.h"
@ -76,7 +76,7 @@ STATIC const mp_rom_map_elem_t bleio_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_AdvertisementData), MP_ROM_PTR(&bleio_advertisementdata_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_Device), MP_ROM_PTR(&bleio_device_type) },
{ MP_ROM_QSTR(MP_QSTR_LocalPeripheral), MP_ROM_PTR(&bleio_local_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) },

View File

@ -28,14 +28,14 @@
#define MICROPY_INCLUDED_SHARED_MODULE_BLEIO_SERVICE_H
#include "common-hal/bleio/UUID.h"
#include "shared-module/bleio/Device.h"
typedef struct {
mp_obj_base_t base;
uint16_t handle;
bool is_secondary;
bleio_uuid_obj_t *uuid;
bleio_device_obj_t *device;
// May be a LocalPeripheral, RemotePeripheral, etc.
mp_obj_t *device;
mp_obj_t char_list;
uint16_t start_handle;
uint16_t end_handle;