8000 Bleio attribute api revamp by dhalbert · Pull Request #2092 · adafruit/circuitpython · GitHub
[go: up one dir, main page]

Skip to content

Bleio attribute api revamp #2092

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Aug 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
164 changes: 75 additions & 89 deletions locale/ID.po

Large diffs are not rendered by default.

164 changes: 75 additions & 89 deletions locale/circuitpython.pot

Large diffs are not rendered by default.

170 changes: 81 additions & 89 deletions locale/de_DE.po

Large diffs are not rendered by default.

164 changes: 75 additions & 89 deletions locale/en_US.po

Large diffs are not rendered by default.

164 changes: 75 additions & 89 deletions locale/en_x_pirate.po

Large diffs are not rendered by default.

177 changes: 86 additions & 91 deletions locale/es.po

Large diffs are not rendered by default.

164 changes: 75 additions & 89 deletions locale/fil.po

Large diffs are not rendered by default.

174 changes: 85 additions & 89 deletions locale/fr.po

Large diffs are not rendered by default.

170 changes: 81 additions & 89 deletions locale/it_IT.po

Large diffs are not rendered by default.

174 changes: 85 additions & 89 deletions locale/pl.po

Large diffs are not rendered by default.

164 changes: 75 additions & 89 deletions locale/pt_BR.po

Large diffs are not rendered by default.

173 changes: 84 additions & 89 deletions locale/zh_Latn_pinyin.po

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions ports/nrf/bluetooth/ble_uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@
#include "py/mphal.h"
#include "py/runtime.h"
#include "lib/utils/interrupt_char.h"
#include "shared-bindings/bleio/Adapter.h"
#include "shared-bindings/bleio/Characteristic.h"
#include "shared-bindings/bleio/Device.h"
#include "shared-bindings/bleio/Service.h"
#include "shared-bindings/bleio/UUID.h"
#include "shared-bindings/_bleio/Adapter.h"
#include "shared-bindings/_bleio/Characteristic.h"
#include "shared-bindings/_bleio/Device.h"
#include "shared-bindings/_bleio/Service.h"
#include "shared-bindings/_bleio/UUID.h"

#if (MICROPY_PY_BLE_NUS == 1)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,13 @@
#include "py/objstr.h"
#include "py/runtime.h"
#include "supervisor/usb.h"
#include "shared-bindings/bleio/Adapter.h"
#include "shared-bindings/bleio/Address.h"
#include "shared-bindings/_bleio/Adapter.h"
#include "shared-bindings/_bleio/Address.h"

#define BLE_MIN_CONN_INTERVAL MSEC_TO_UNITS(15, UNIT_0_625_MS)
#define BLE_MAX_CONN_INTERVAL MSEC_TO_UNITS(15, UNIT_0_625_MS)
#define BLE_SLAVE_LATENCY 0
#define BLE_CONN_SUP_TIMEOUT MSEC_TO_UNITS(4000, UNIT_10_MS)

STATIC void softdevice_assert_handler(uint32_t id, uint32_t pc, uint32_t info) {
mp_raise_msg_varg(&mp_type_AssertionError,
Expand All @@ -61,12 +66,14 @@ STATIC uint32_t ble_stack_enable(void) {
};

uint32_t err_code = sd_softdevice_enable(&clock_config, softdevice_assert_handler);
if (err_code != NRF_SUCCESS)
if (err_code != NRF_SUCCESS) {
return err_code;
}

err_code = sd_nvic_EnableIRQ(SD_EVT_IRQn);
if (err_code != NRF_SUCCESS)
if (err_code != NRF_SUCCESS) {
return err_code;
}

// Start with no event handlers, etc.
ble_drv_reset();
Expand Down Expand Up @@ -97,8 +104,22 @@ STATIC uint32_t ble_stack_enable(void) {
return err_code;

err_code = sd_ble_enable(&app_ram_start);
if (err_code != NRF_SUCCESS)
return err_code;

ble_gap_conn_params_t gap_conn_params = {
.min_conn_interval = BLE_MIN_CONN_INTERVAL,
.max_conn_interval = BLE_MAX_CONN_INTERVAL,
.slave_latency = BLE_SLAVE_LATENCY,
.conn_sup_timeout = BLE_CONN_SUP_TIMEOUT,
};
err_code = sd_ble_gap_ppcp_set(&gap_conn_params);
if (err_code != NRF_SUCCESS) {
return err_code;
}

return err_code;
err_code = sd_ble_gap_appearance_set(BLE_APPEARANCE_UNKNOWN);
return err_code;
}

void common_hal_bleio_adapter_set_enabled(bool enabled) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
* THE SOFTWARE.
*/

#include "shared-bindings/bleio/Attribute.h"
#include "shared-bindings/_bleio/Attribute.h"

// Convert a bleio security mode to a ble_gap_conn_sec_mode_t setting.
// Convert a _bleio security mode to a ble_gap_conn_sec_mode_t setting.
void bleio_attribute_gatts_set_security_mode(ble_gap_conn_sec_mode_t *perm, bleio_attribute_security_mode_t security_mode) {
switch (security_mode) {
case SECURITY_MODE_NO_ACCESS:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#ifndef MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_ATTRIBUTE_H
#define MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_ATTRIBUTE_H

// Nothing yet.
#include "shared-module/_bleio/Attribute.h"

extern void bleio_attribute_gatts_set_security_mode(ble_gap_conn_sec_mode_t *perm, bleio_attribute_security_mode_t security_mode);

#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_ATTRIBUTE_H
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
#include "nrf_soc.h"
#include "py/objstr.h"
#include "py/runtime.h"
#include "shared-bindings/bleio/__init__.h"
#include "shared-bindings/bleio/Adapter.h"
#include "shared-bindings/bleio/Central.h"
#include "shared-bindings/_bleio/__init__.h"
#include "shared-bindings/_bleio/Adapter.h"
#include "shared-bindings/_bleio/Central.h"

STATIC void central_on_ble_evt(ble_evt_t *ble_evt, void *central_in) {
bleio_central_obj_t *central = (bleio_central_obj_t*)central_in;
Expand Down Expand Up @@ -76,7 +76,7 @@ STATIC void central_on_ble_evt(ble_evt_t *ble_evt, void *central_in) {
void common_hal_bleio_central_construct(bleio_central_obj_t *self) {
common_hal_bleio_adapter_set_enabled(true);

self->remote_services_list = mp_obj_new_list(0, NULL);
self->remote_service_list = mp_obj_new_list(0, NULL);
self->conn_handle = BLE_CONN_HANDLE_INVALID;
}

Expand Down F438 Expand Up @@ -134,12 +134,12 @@ bool common_hal_bleio_central_get_connected(bleio_central_obj_t *self) {
mp_obj_tuple_t *common_hal_bleio_central_discover_remote_services(bleio_central_obj_t *self, mp_obj_t service_uuids_whitelist) {
common_hal_bleio_device_discover_remote_services(MP_OBJ_FROM_PTR(self), service_uuids_whitelist);
// Convert to a tuple and then clear the list so the callee will take ownership.
mp_obj_tuple_t *services_tuple = mp_obj_new_tuple(self->remote_services_list->len,
self->remote_services_list->items);
mp_obj_list_clear(self->remote_services_list);
mp_obj_tuple_t *services_tuple = mp_obj_new_tuple(self->remote_service_list->len,
self->remote_service_list->items);
mp_obj_list_clear(self->remote_service_list);
return services_tuple;
}

mp_obj_list_t *common_hal_bleio_central_get_remote_services(bleio_central_obj_t *self) {
return self->remote_services_list;
return self->remote_service_list;
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@
#include <stdbool.h>

#include "py/objlist.h"
#include "shared-module/bleio/Address.h"
#include "shared-module/_bleio/Address.h"

typedef struct {
mp_obj_base_t base;
volatile bool waiting_to_connect;
volatile uint16_t conn_handle;
// Services discovered after connecting to a remote peripheral.
mp_obj_list_t *remote_services_list;
mp_obj_list_t *remote_service_list;
} bleio_central_obj_t;

#endif // MICROPY_INCLUDED_SHARED_MODULE_BLEIO_CENTRAL_H
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@

#include "py/runtime.h"

#include "shared-bindings/bleio/__init__.h"
#include "shared-bindings/bleio/Characteristic.h"
#include "shared-bindings/bleio/Descriptor.h"
#include "shared-bindings/bleio/Service.h"
#include "shared-bindings/_bleio/__init__.h"
#include "shared-bindings/_bleio/Characteristic.h"
#include "shared-bindings/_bleio/Descriptor.h"
#include "shared-bindings/_bleio/Service.h"

static volatile bleio_characteristic_obj_t *m_read_characteristic;

Expand Down Expand Up @@ -124,15 +124,14 @@ STATIC void characteristic_gattc_read(bleio_characteristic_obj_t *characteristic
ble_drv_remove_event_handler(characteristic_on_gattc_read_rsp_evt, characteristic);
}

void common_hal_bleio_characteristic_construct(bleio_characteristic_obj_t *self, bleio_uuid_obj_t *uuid, bleio_characteristic_properties_t props, bleio_attribute_security_mode_t read_perm, bleio_attribute_security_mode_t write_perm, mp_int_t max_length, bool fixed_length, mp_obj_list_t *descriptor_list) {
self->service = MP_OBJ_NULL;
void common_hal_bleio_characteristic_construct(bleio_characteristic_obj_t *self, bleio_service_obj_t *service, bleio_uuid_obj_t *uuid, bleio_characteristic_properties_t props, bleio_attribute_security_mode_t read_perm, bleio_attribute_security_mode_t write_perm, mp_int_t max_length, bool fixed_length, mp_buffer_info_t *initial_value_bufinfo) {
self->service = service;
self->uuid = uuid;
self->value = mp_const_empty_bytes;
self->handle = BLE_GATT_HANDLE_INVALID;
self->props = props;
self->read_perm = read_perm;
self->write_perm = write_perm;
self->descriptor_list = descriptor_list;
self->descriptor_list = mp_obj_new_list(0, NULL);

const mp_int_t max_length_max = fixed_length ? BLE_GATTS_FIX_ATTR_LEN_MAX : BLE_GATTS_VAR_ATTR_LEN_MAX;
if (max_length < 0 || max_length > max_length_max) {
Expand All @@ -142,11 +141,7 @@ void common_hal_bleio_characteristic_construct(bleio_characteristic_obj_t *self,
self->max_length = max_length;
self->fixed_length = fixed_length;

for (size_t descriptor_idx = 0; descriptor_idx < descriptor_list->len; ++descriptor_idx) {
bleio_descriptor_obj_t *descriptor =
MP_OBJ_TO_PTR(descriptor_list->items[descriptor_idx]);
descriptor->characteristic = self;
}
common_hal_bleio_characteristic_set_value(self, initial_value_bufinfo);
}

mp_obj_list_t *common_hal_bleio_characteristic_get_descriptor_list(bleio_characteristic_obj_t *self) {
Expand All @@ -173,6 +168,15 @@ mp_obj_t common_hal_bleio_characteristic_get_value(bleio_characteristic_obj_t *s
}

void common_hal_bleio_characteristic_set_value(bleio_characteristic_obj_t *self, mp_buffer_info_t *bufinfo) {
if (self->fixed_length && bufinfo->len != self->max_length) {
mp_raise_ValueError(translate("Value length != required fixed length"));
}
if (bufinfo->len > self->max_length) {
mp_raise_ValueError(translate("Value length > max_length"));
}

self->value = mp_obj_new_bytes(bufinfo->buf, bufinfo->len);

// Do GATT operations only if this characteristic has been added to a registered service.
if (self->handle != BLE_GATT_HANDLE_INVALID) {
uint16_t conn_handle = common_hal_bleio_device_get_conn_handle(self->service->device);
Expand All @@ -182,12 +186,6 @@ void common_hal_bleio_characteristic_set_value(bleio_characteristic_obj_t *self,
common_hal_bleio_gattc_write(self->handle, conn_handle, bufinfo,
(self->props & CHAR_PROP_WRITE_NO_RESPONSE));
} else {
if (self->fixed_length && bufinfo->len != self->max_length) {
mp_raise_ValueError(translate("Value length != required fixed length"));
}
if (bufinfo->len > self->max_length) {
mp_raise_ValueError(translate("Value length > max_length"));
}

bool sent = false;
uint16_t cccd = 0;
Expand All @@ -213,8 +211,6 @@ void common_hal_bleio_characteristic_set_value(bleio_characteristic_obj_t *self,
}
}
}

self->value = mp_obj_new_bytes(bufinfo->buf, bufinfo->len);
}

bleio_uuid_obj_t *common_hal_bleio_characteristic_get_uuid(bleio_characteristic_obj_t *self) {
Expand All @@ -225,6 +221,40 @@ bleio_characteristic_properties_t common_hal_bleio_characteristic_get_properties
return self->props;
}

void common_hal_bleio_characteristic_add_descriptor(bleio_characteristic_obj_t *self, bleio_descriptor_obj_t *descriptor) {
ble_uuid_t desc_uuid;
bleio_uuid_convert_to_nrf_ble_uuid(descriptor->uuid, &desc_uuid);

ble_gatts_attr_md_t desc_attr_md = {
// Data passed is not in a permanent location and should be copied.
.vloc = BLE_GATTS_VLOC_STACK,
.vlen = !descriptor->fixed_length,
};

bleio_attribute_gatts_set_security_mode(&desc_attr_md.read_perm, descriptor->read_perm);
bleio_attribute_gatts_set_security_mode(&desc_attr_md.write_perm, descriptor->write_perm);

mp_buffer_info_t desc_value_bufinfo;
mp_get_buffer_raise(descriptor->value, &desc_value_bufinfo, MP_BUFFER_READ);

ble_gatts_attr_t desc_attr = {
.p_uuid = &desc_uuid,
.p_attr_md = &desc_attr_md,
.init_len = desc_value_bufinfo.len,
.p_value = desc_value_bufinfo.buf,
.init_offs = 0,
.max_len = descriptor->max_length,
};

uint32_t err_code = sd_ble_gatts_descriptor_add(self->handle, &desc_attr, &descriptor->handle);

if (err_code != NRF_SUCCESS) {
mp_raise_OSError_msg_varg(translate("Failed to add descriptor, err 0x%04x"), err_code);
}

mp_obj_list_append(self->descriptor_list, MP_OBJ_FROM_PTR(descriptor));
}

void common_hal_bleio_characteristic_set_cccd(bleio_characteristic_obj_t *self, bool notify, bool indicate) {
if (self->cccd_handle == BLE_GATT_HANDLE_INVALID) {
mp_raise_ValueError(translate("No CCCD for this Characteristic"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
#ifndef MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_CHARACTERISTIC_H
#define MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_CHARACTERISTIC_H

#include "shared-bindings/bleio/Attribute.h"
#include "shared-module/bleio/Characteristic.h"
#include "common-hal/bleio/Service.h"
#include "common-hal/bleio/UUID.h"
#include "shared-bindings/_bleio/Attribute.h"
#include "shared-module/_bleio/Characteristic.h"
#include "common-hal/_bleio/Service.h"
#include "common-hal/_bleio/UUID.h"

typedef struct {
mp_obj_base_t base;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@

#include "tick.h"

#include "shared-bindings/bleio/__init__.h"
#include "common-hal/bleio/CharacteristicBuffer.h"
#include "shared-bindings/_bleio/__init__.h"
#include "common-hal/_bleio/CharacteristicBuffer.h"

STATIC void write_to_ringbuf(bleio_characteristic_buffer_obj_t *self, uint8_t *data, uint16_t len) {
// Push all the data onto the ring buffer.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#include "nrf_soc.h"

#include "py/ringbuf.h"
#include "shared-bindings/bleio/Characteristic.h"
#include "shared-bindings/_bleio/Characteristic.h"

typedef struct {
mp_obj_base_t base;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,16 @@

#include "py/runtime.h"

#include "shared-bindings/bleio/__init__.h"
#include "shared-bindings/bleio/Descriptor.h"
#include "shared-bindings/bleio/Service.h"
#include "shared-bindings/bleio/UUID.h"
#include "shared-bindings/_bleio/__init__.h"
#include "shared-bindings/_bleio/Descriptor.h"
#include "shared-bindings/_bleio/Service.h"
#include "shared-bindings/_bleio/UUID.h"

static volatile bleio_descriptor_obj_t *m_read_descriptor;

void common_hal_bleio_descriptor_construct(bleio_descriptor_obj_t *self, bleio_uuid_obj_t *uuid, bleio_attribute_security_mode_t read_perm, bleio_attribute_security_mode_t write_perm, mp_int_t max_length, bool fixed_length) {
self->characteristic = MP_OBJ_NULL;
void common_hal_bleio_descriptor_construct(bleio_descriptor_obj_t *self, bleio_characteristic_obj_t *characteristic, bleio_uuid_obj_t *uuid, bleio_attribute_security_mode_t read_perm, bleio_attribute_security_mode_t write_perm, mp_int_t max_length, bool fixed_length, mp_buffer_info_t *initial_value_bufinfo) {
self->characteristic = characteristic;
self->uuid = uuid;
self->value = mp_const_empty_bytes;
self->handle = BLE_GATT_HANDLE_INVALID;
self->read_perm = read_perm;
self->write_perm = write_perm;
Expand All @@ -50,6 +49,8 @@ void common_hal_bleio_descriptor_construct(bleio_descriptor_obj_t *self, bleio_u
}
self->max_length = max_length;
self->fixed_length = fixed_length;

common_hal_bleio_descriptor_set_value(self, initial_value_bufinfo);
}

bleio_uuid_obj_t *common_hal_bleio_descriptor_get_uuid(bleio_descriptor_obj_t *self) {
Expand Down Expand Up @@ -119,23 +120,24 @@ mp_obj_t common_hal_bleio_descriptor_get_value(bleio_descriptor_obj_t *self) {
}

void common_hal_bleio_descriptor_set_value(bleio_descriptor_obj_t *self, mp_buffer_info_t *bufinfo) {
if (self->fix E4ED ed_length && bufinfo->len != self->max_length) {
mp_raise_ValueError(translate("Value length != required fixed length"));
}
if (bufinfo->len > self->max_length) {
mp_raise_ValueError(translate("Value length > max_length"));
}

self->value = mp_obj_new_bytes(bufinfo->buf, bufinfo->len);

// Do GATT operations only if this descriptor has been registered.
if (self->handle != BLE_GATT_HANDLE_INVALID) {
uint16_t conn_handle = common_hal_bleio_device_get_conn_handle(self->characteristic->service->device);
if (common_hal_bleio_service_get_is_remote(self->characteristic->service)) {
// false means WRITE_REQ, not write-no-response
common_hal_bleio_gattc_write(self->handle, conn_handle, bufinfo, false);
} else {
if (self->fixed_length && bufinfo->len != self->max_length) {
mp_raise_ValueError(translate("Value length != required fixed length"));
}
if (bufinfo->len > self->max_length) {
mp_raise_ValueError(translate("Value length > max_length"));
}

common_hal_bleio_gatts_write(self->handle, conn_handle, bufinfo);
}
}

self->value = mp_obj_new_bytes(bufinfo->buf, bufinfo->len);
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@

#include "py/obj.h"

#include "shared-bindings/bleio/Characteristic.h"
#include "common-hal/bleio/UUID.h"
#include "common-hal/_bleio/Characteristic.h"
#include "common-hal/_bleio/UUID.h"

typedef struct {
mp_obj_base_t base;
Expand Down
Loading
0