WIP: rework of Characteristic properties; enhance Descriptor; not tested

This commit is contained in:
Dan Halbert 2019-08-02 17:56:44 -04:00
parent ee518b9141
commit 7ce3776b80
26 changed files with 518 additions and 327 deletions

View File

@ -0,0 +1,60 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2019 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
* 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 "shared-bindings/bleio/Attribute.h"
// 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 SEC_MODE_NO_ACCESS:
BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(perm);
break;
case SEC_MODE_OPEN:
BLE_GAP_CONN_SEC_MODE_SET_OPEN(perm);
break;
case SEC_MODE_ENC_NO_MITM:
BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM(perm);
break;
case SEC_MODE_ENC_WITH_MITM:
BLE_GAP_CONN_SEC_MODE_SET_ENC_WITH_MITM(perm);
break;
case SEC_MODE_LESC_ENC_WITH_MITM:
BLE_GAP_CONN_SEC_MODE_SET_LESC_ENC_WITH_MITM(perm);
break;
case SEC_MODE_SIGNED_NO_MITM:
BLE_GAP_CONN_SEC_MODE_SET_SIGNED_NO_MITM(perm);
break;
case SEC_MODE_SIGNED_WITH_MITM:
BLE_GAP_CONN_SEC_MODE_SET_SIGNED_WITH_MITM(perm);
break;
}
}

View File

@ -24,9 +24,9 @@
* THE SOFTWARE.
*/
#ifndef MICROPY_INCLUDED_SHARED_MODULE_BLEIO_INIT_H
#define MICROPY_INCLUDED_SHARED_MODULE_BLEIO_INIT_H
#ifndef MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_ATTRIBUTE_H
#define MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_ATTRIBUTE_H
extern void bleio_reset(void);
// Nothing yet.
#endif // MICROPY_INCLUDED_SHARED_MODULE_BLEIO_INIT_H
#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_ATTRIBUTE_H

View File

@ -31,7 +31,6 @@
#include <stdbool.h>
#include "py/objlist.h"
#include "shared-module/bleio/__init__.h"
#include "shared-module/bleio/Address.h"
typedef struct {

View File

@ -164,7 +164,8 @@ STATIC void gattc_write(bleio_characteristic_obj_t *characteristic, mp_buffer_in
check_connected(conn_handle);
ble_gattc_write_params_t write_params = {
.write_op = characteristic->props.write_no_response ? BLE_GATT_OP_WRITE_CMD : BLE_GATT_OP_WRITE_REQ,
.write_op = (characteristic->props & CHAR_PROP_WRITE_NO_RESPONSE)
? BLE_GATT_OP_WRITE_CMD: BLE_GATT_OP_WRITE_REQ,
.handle = characteristic->handle,
.p_value = bufinfo->buf,
.len = bufinfo->len,
@ -211,12 +212,14 @@ STATIC void characteristic_on_ble_evt(ble_evt_t *ble_evt, void *param) {
}
void common_hal_bleio_characteristic_construct(bleio_characteristic_obj_t *self, bleio_uuid_obj_t *uuid, bleio_characteristic_properties_t props, mp_obj_list_t *descriptor_list) {
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 security_mode, mp_obj_t descriptors) {
self->service = mp_const_none;
self->uuid = uuid;
self->value_data = mp_const_none;
self->props = props;
self->descriptor_list = descriptor_list;
self->security_mode = security_mode;
self->descriptor_list = mp_obj_new_list_from_iter(descriptors);
self->handle = BLE_GATT_HANDLE_INVALID;
ble_drv_add_event_handler(characteristic_on_ble_evt, self);
@ -238,21 +241,23 @@ 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 (common_hal_bleio_service_get_is_remote(self->service)) {
gattc_write(self, bufinfo);
gattc_write(self, bufinfo);
} else {
bool sent = false;
uint16_t cccd = 0;
if (self->props.notify || self->props.indicate) {
cccd = get_cccd(self);
const bool notify = self->props & CHAR_PROP_NOTIFY;
const bool indicate = self->props & CHAR_PROP_INDICATE;
if (notify | indicate) {
cccd = get_cccd(self);
}
// It's possible that both notify and indicate are set.
if (self->props.notify && (cccd & BLE_GATT_HVX_NOTIFICATION)) {
if (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)) {
if (indicate && (cccd & BLE_GATT_HVX_INDICATION)) {
gatts_notify_indicate(self, bufinfo, BLE_GATT_HVX_INDICATION);
sent = true;
}

View File

@ -28,17 +28,19 @@
#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-module/bleio/Characteristic.h"
typedef struct {
mp_obj_base_t base;
bleio_service_obj_t *service;
bleio_uuid_obj_t *uuid;
volatile mp_obj_t value_data;
mp_obj_t value_data;
uint16_t handle;
bleio_characteristic_properties_t props;
bleio_attribute_security_mode_t security_mode;
mp_obj_list_t *descriptor_list;
uint16_t user_desc_handle;
uint16_t cccd_handle;

View File

@ -3,7 +3,7 @@
*
* The MIT License (MIT)
*
* Copyright (c) 2018 Dan Halbert for Adafruit Industries
* Copyright (c) 2019 Dan Halbert for Adafruit Industries
* Copyright (c) 2018 Artur Pacholec
* Copyright (c) 2016 Glenn Ruben Bakke
*
@ -33,10 +33,6 @@ void common_hal_bleio_descriptor_construct(bleio_descriptor_obj_t *self, bleio_u
self->uuid = uuid;
}
mp_int_t common_hal_bleio_descriptor_get_handle(bleio_descriptor_obj_t *self) {
return self->handle;
}
bleio_uuid_obj_t *common_hal_bleio_descriptor_get_uuid(bleio_descriptor_obj_t *self) {
return self->uuid;
}

View File

@ -30,14 +30,17 @@
#define MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_DESCRIPTOR_H
#include "py/obj.h"
#include "common-hal/bleio/Characteristic.h"
#include "shared-bindings/bleio/Characteristic.h"
#include "common-hal/bleio/UUID.h"
typedef struct {
mp_obj_base_t base;
uint16_t handle;
bleio_characteristic_obj_t *characteristic;
bleio_uuid_obj_t *uuid;
mp_obj_t value_data;
uint16_t handle;
bleio_attribute_security_mode_t security_mode;
} bleio_descriptor_obj_t;
#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_DESCRIPTOR_H

View File

@ -138,10 +138,10 @@ STATIC void peripheral_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
// Pairing process completed
ble_gap_evt_auth_status_t* status = &ble_evt->evt.gap_evt.params.auth_status;
if (BLE_GAP_SEC_STATUS_SUCCESS == status->auth_status) {
mp_printf(&mp_plat_print, "Pairing succeeded, status: 0x%04x\n", status->auth_status);
// mp_printf(&mp_plat_print, "Pairing succeeded, status: 0x%04x\n", status->auth_status);
self->pair_status = PAIR_PAIRED;
} else {
mp_printf(&mp_plat_print, "Pairing failed, status: 0x%04x\n", status->auth_status);
// mp_printf(&mp_plat_print, "Pairing failed, status: 0x%04x\n", status->auth_status);
self->pair_status = PAIR_NOT_PAIRED;
}
break;

View File

@ -35,7 +35,6 @@
#include "py/obj.h"
#include "py/objlist.h"
#include "shared-module/bleio/__init__.h"
#include "shared-module/bleio/Address.h"
typedef enum {

View File

@ -29,8 +29,8 @@
#include "ble.h"
#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/Descriptor.h"
#include "shared-bindings/bleio/Service.h"
#include "shared-bindings/bleio/Adapter.h"
@ -74,12 +74,12 @@ void common_hal_bleio_service_add_all_characteristics(bleio_service_obj_t *self)
MP_OBJ_TO_PTR(self->characteristic_list->items[characteristic_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,
.char_props.broadcast = (bool) characteristic->props & CHAR_PROP_BROADCAST,
.char_props.read = (bool) characteristic->props & CHAR_PROP_READ,
.char_props.write_wo_resp = (bool) characteristic->props & CHAR_PROP_WRITE_NO_RESPONSE,
.char_props.write = (bool) characteristic->props & CHAR_PROP_WRITE,
.char_props.notify = (bool) characteristic->props & CHAR_PROP_NOTIFY,
.char_props.indicate = (bool) characteristic->props & CHAR_PROP_INDICATE,
};
ble_gatts_attr_md_t cccd_md = {
@ -93,28 +93,28 @@ void common_hal_bleio_service_add_all_characteristics(bleio_service_obj_t *self)
char_md.p_cccd_md = &cccd_md;
}
ble_uuid_t uuid;
bleio_uuid_convert_to_nrf_ble_uuid(characteristic->uuid, &uuid);
ble_uuid_t char_uuid;
bleio_uuid_convert_to_nrf_ble_uuid(characteristic->uuid, &char_uuid);
ble_gatts_attr_md_t attr_md = {
ble_gatts_attr_md_t char_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_GAP_CONN_SEC_MODE_SET_OPEN(&char_attr_md.read_perm);
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&char_attr_md.write_perm);
ble_gatts_attr_t attr_char_value = {
.p_uuid = &uuid,
.p_attr_md = &attr_md,
ble_gatts_attr_t char_attr = {
.p_uuid = &char_uuid,
.p_attr_md = &char_attr_md,
.init_len = sizeof(uint8_t),
.max_len = GATT_MAX_DATA_LENGTH,
};
ble_gatts_char_handles_t handles;
ble_gatts_char_handles_t char_handles;
uint32_t err_code;
err_code = sd_ble_gatts_characteristic_add(self->handle, &char_md, &attr_char_value, &handles);
err_code = sd_ble_gatts_characteristic_add(self->handle, &char_md, &char_attr, &char_handles);
if (err_code != NRF_SUCCESS) {
mp_raise_OSError_msg_varg(translate("Failed to add characteristic, err 0x%04x"), err_code);
}
@ -123,9 +123,41 @@ void common_hal_bleio_service_add_all_characteristics(bleio_service_obj_t *self)
mp_raise_ValueError(translate("Characteristic already in use by another Service."));
}
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;
characteristic->user_desc_handle = char_handles.user_desc_handle;
characteristic->cccd_handle = char_handles.cccd_handle;
characteristic->sccd_handle = char_handles.sccd_handle;
characteristic->handle = char_handles.value_handle;
// Add the descriptors for this characteristic.
for (size_t descriptor_idx = 0; descriptor_idx < characteristic->descriptor_list->len; ++descriptor_idx) {
bleio_descriptor_obj_t *descriptor =
MP_OBJ_TO_PTR(characteristic->descriptor_list->items[descriptor_idx]);
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 = 1,
};
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&desc_attr_md.read_perm);
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&desc_attr_md.write_perm);
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(descriptor->value_data, &bufinfo, MP_BUFFER_READ);
ble_gatts_attr_t desc_attr = {
.p_uuid = &desc_uuid,
.p_attr_md = &desc_attr_md,
.init_len = bufinfo.len,
.p_value = bufinfo.buf,
.init_offs = 0,
.max_len = GATT_MAX_DATA_LENGTH,
};
err_code = sd_ble_gatts_descriptor_add(characteristic->handle, &desc_attr, &descriptor->handle);
}
}
}

View File

@ -204,17 +204,16 @@ STATIC void on_char_discovery_rsp(ble_gattc_evt_char_disc_rsp_t *response, mp_ob
// For now, just leave the UUID as NULL.
}
bleio_characteristic_properties_t props;
props.broadcast = gattc_char->char_props.broadcast;
props.indicate = gattc_char->char_props.indicate;
props.notify = gattc_char->char_props.notify;
props.read = gattc_char->char_props.read;
props.write = gattc_char->char_props.write;
props.write_no_response = gattc_char->char_props.write_wo_resp;
bleio_characteristic_properties_t props =
(gattc_char->char_props.broadcast ? CHAR_PROP_BROADCAST : 0) |
(gattc_char->char_props.indicate ? CHAR_PROP_INDICATE : 0) |
(gattc_char->char_props.notify ? CHAR_PROP_NOTIFY : 0) |
(gattc_char->char_props.read ? CHAR_PROP_READ : 0) |
(gattc_char->char_props.write ? CHAR_PROP_WRITE : 0) |
(gattc_char->char_props.write_wo_resp ? CHAR_PROP_WRITE_NO_RESPONSE : 0);
// Call common_hal_bleio_characteristic_construct() to initalize some fields and set up evt handler.
common_hal_bleio_characteristic_construct(characteristic, uuid, props, mp_obj_new_list(0, NULL));
common_hal_bleio_characteristic_construct(characteristic, uuid, props, SEC_MODE_OPEN, mp_const_empty_tuple);
characteristic->handle = gattc_char->handle_value;
characteristic->service = m_char_discovery_service;
@ -233,15 +232,15 @@ STATIC void on_desc_discovery_rsp(ble_gattc_evt_desc_disc_rsp_t *response, mp_ob
// Remember handles for certain well-known descriptors.
switch (gattc_desc->uuid.uuid) {
case DESCRIPTOR_UUID_CLIENT_CHARACTERISTIC_CONFIGURATION:
case BLE_UUID_DESCRIPTOR_CLIENT_CHAR_CONFIG:
m_desc_discovery_characteristic->cccd_handle = gattc_desc->handle;
break;
case DESCRIPTOR_UUID_SERVER_CHARACTERISTIC_CONFIGURATION:
case BLE_UUID_DESCRIPTOR_SERVER_CHAR_CONFIG:
m_desc_discovery_characteristic->sccd_handle = gattc_desc->handle;
break;
case DESCRIPTOR_UUID_CHARACTERISTIC_USER_DESCRIPTION:
case BLE_UUID_DESCRIPTOR_CHAR_USER_DESC:
m_desc_discovery_characteristic->user_desc_handle = gattc_desc->handle;
break;
@ -268,7 +267,8 @@ STATIC void on_desc_discovery_rsp(ble_gattc_evt_desc_disc_rsp_t *response, mp_ob
// For now, just leave the UUID as NULL.
}
common_hal_bleio_descriptor_construct(descriptor, uuid);
// TODO: can we find out security mode?
common_hal_bleio_descriptor_construct(descriptor, uuid, SEC_MODE_OPEN);
descriptor->handle = gattc_desc->handle;
descriptor->characteristic = m_desc_discovery_characteristic;
@ -313,6 +313,9 @@ void common_hal_bleio_device_discover_remote_services(mp_obj_t device, mp_obj_t
ble_drv_add_event_handler(discovery_on_ble_evt, device);
// Start over with an empty list.
mp_obj_list_clear(MP_OBJ_FROM_PTR(common_hal_bleio_device_get_remote_services_list(device)));
if (service_uuids_whitelist == mp_const_none) {
// List of service UUID's not given, so discover all available services.

View File

@ -227,6 +227,7 @@ $(filter $(SRC_PATTERNS), \
audioio/AudioOut.c \
bleio/__init__.c \
bleio/Adapter.c \
bleio/Attribute.c \
bleio/Central.c \
bleio/Characteristic.c \
bleio/CharacteristicBuffer.c \
@ -276,6 +277,9 @@ $(filter $(SRC_PATTERNS), \
# All possible sources are listed here, and are filtered by SRC_PATTERNS.
SRC_BINDINGS_ENUMS = \
$(filter $(SRC_PATTERNS), \
bleio/Address.c \
bleio/Attribute.c \
bleio/ScanEntry.c \
digitalio/Direction.c \
digitalio/DriveMode.c \
digitalio/Pull.c \
@ -289,12 +293,6 @@ SRC_BINDINGS_ENUMS += \
help.c \
util.c
SRC_BINDINGS_ENUMS += \
$(filter $(SRC_PATTERNS), \
bleio/Address.c \
bleio/ScanEntry.c \
)
# All possible sources are listed here, and are filtered by SRC_PATTERNS.
SRC_SHARED_MODULE = \
$(filter $(SRC_PATTERNS), \
@ -314,6 +312,7 @@ $(filter $(SRC_PATTERNS), \
bitbangio/__init__.c \
board/__init__.c \
bleio/Address.c \
bleio/Attribute.c \
bleio/ScanEntry.c \
busio/OneWire.c \
displayio/Bitmap.c \

View File

@ -668,6 +668,7 @@ mp_obj_t mp_obj_new_gen_wrap(mp_obj_t fun);
mp_obj_t mp_obj_new_closure(mp_obj_t fun, size_t n_closed, const mp_obj_t *closed);
mp_obj_t mp_obj_new_tuple(size_t n, const mp_obj_t *items);
mp_obj_t mp_obj_new_list(size_t n, mp_obj_t *items);
mp_obj_t mp_obj_new_list_from_iter(mp_obj_t iterable);
mp_obj_t mp_obj_new_dict(size_t n_args);
mp_obj_t mp_obj_new_set(size_t n_args, mp_obj_t *items);
mp_obj_t mp_obj_new_slice(mp_obj_t start, mp_obj_t stop, mp_obj_t step);

View File

@ -68,6 +68,11 @@ STATIC mp_obj_t list_extend_from_iter(mp_obj_t list, mp_obj_t iterable) {
return list;
}
mp_obj_t mp_obj_new_list_from_iter(mp_obj_t iterable) {
mp_obj_t list = mp_obj_new_list(0, NULL);
return list_extend_from_iter(list, iterable);
}
STATIC mp_obj_t list_make_new(const mp_obj_type_t *type_in, size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
(void)type_in;
mp_arg_check_num(n_args, kw_args, 0, 1, false);

View File

@ -174,13 +174,13 @@ STATIC void bleio_address_print(const mp_print_t *print, mp_obj_t self_in, mp_pr
//| A randomly generated address that changes on every connection.
//|
STATIC const mp_rom_map_elem_t bleio_address_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_address_bytes), MP_ROM_PTR(&bleio_address_address_bytes_obj) },
{ MP_ROM_QSTR(MP_QSTR_type), MP_ROM_PTR(&bleio_address_type_obj) },
{ MP_ROM_QSTR(MP_QSTR_address_bytes), MP_ROM_PTR(&bleio_address_address_bytes_obj) },
{ MP_ROM_QSTR(MP_QSTR_type), MP_ROM_PTR(&bleio_address_type_obj) },
// These match the BLE_GAP_ADDR_TYPES values used by the nRF library.
{ MP_ROM_QSTR(MP_QSTR_PUBLIC), MP_OBJ_NEW_SMALL_INT(0) },
{ MP_ROM_QSTR(MP_QSTR_RANDOM_STATIC), MP_OBJ_NEW_SMALL_INT(1) },
{ MP_ROM_QSTR(MP_QSTR_RANDOM_PRIVATE_RESOLVABLE), MP_OBJ_NEW_SMALL_INT(2) },
{ MP_ROM_QSTR(MP_QSTR_RANDOM_PRIVATE_NON_RESOLVABLE), MP_OBJ_NEW_SMALL_INT(3) },
{ MP_ROM_QSTR(MP_QSTR_PUBLIC), MP_ROM_INT(0) },
{ MP_ROM_QSTR(MP_QSTR_RANDOM_STATIC), MP_ROM_INT(1) },
{ MP_ROM_QSTR(MP_QSTR_RANDOM_PRIVATE_RESOLVABLE), MP_ROM_INT(2) },
{ MP_ROM_QSTR(MP_QSTR_RANDOM_PRIVATE_NON_RESOLVABLE), MP_ROM_INT(3) },
};

View File

@ -0,0 +1,93 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2019 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
* 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 "py/objproperty.h"
#include "py/runtime.h"
#include "shared-bindings/bleio/Characteristic.h"
#include "shared-bindings/bleio/UUID.h"
//
//| .. currentmodule:: bleio
//|
//| :class:`Attribute` -- BLE Attribute
//| =========================================================
//|
//| Definitions associated with all BLE attributes: characteristics, descriptors, etc.
//| `Attribute` is, notionally, a superclass of `Characteristic` and `Descriptor`,
//| but is not defined as a Python superclass of those classes.
//|
//| .. class:: Attribute()
//|
//| You cannot create an instance of `Attribute`.
//|
STATIC const mp_rom_map_elem_t bleio_attribute_locals_dict_table[] = {
//| .. data:: NO_ACCESS
//|
//| security mode: access not allowed
//|
//| .. data:: OPEN
//|
//| security_mode: no security (link is not encrypted)
//|
//| .. data:: ENCRYPT_NO_MITM
//|
//| security_mode: unauthenticated encryption, without man-in-the-middle protection
//|
//| .. data:: ENCRYPT_WITH_MITM
//|
//| security_mode: authenticated encryption, with man-in-the-middle protection
//|
//| .. data:: LESC_ENCRYPT_WITH_MITM
//|
//| security_mode: LESC encryption, with man-in-the-middle protection
//|
//| .. data:: SIGNED_NO_MITM
//|
//| security_mode: unauthenticated data signing, without man-in-the-middle protection
//|
//| .. data:: SIGNED_WITH_MITM
//|
//| security_mode: authenticated data signing, without man-in-the-middle protection
//|
{ MP_ROM_QSTR(MP_QSTR_NO_ACCESS), MP_ROM_INT(SEC_MODE_NO_ACCESS) },
{ MP_ROM_QSTR(MP_QSTR_OPEN), MP_ROM_INT(SEC_MODE_OPEN) },
{ MP_ROM_QSTR(MP_QSTR_ENCRYPT_NO_MITM), MP_ROM_INT(SEC_MODE_ENC_NO_MITM) },
{ MP_ROM_QSTR(MP_QSTR_ENCRYPT_WITH_MITM), MP_ROM_INT(SEC_MODE_ENC_WITH_MITM) },
{ MP_ROM_QSTR(MP_QSTR_LESC_ENCRYPT_WITH_MITM), MP_ROM_INT(SEC_MODE_LESC_ENC_WITH_MITM) },
{ MP_ROM_QSTR(MP_QSTR_SIGNED_NO_MITM), MP_ROM_INT(SEC_MODE_SIGNED_NO_MITM) },
{ MP_ROM_QSTR(MP_QSTR_SIGNED_WITH_MITM), MP_ROM_INT(SEC_MODE_SIGNED_WITH_MITM) },
};
STATIC MP_DEFINE_CONST_DICT(bleio_attribute_locals_dict, bleio_attribute_locals_dict_table);
const mp_obj_type_t bleio_attribute_type = {
{ &mp_type_type },
.name = MP_QSTR_Attribute,
.locals_dict = (mp_obj_dict_t*)&bleio_attribute_locals_dict,
};

View File

@ -0,0 +1,38 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2019 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
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_ATTRIBUTE_H
#define MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_ATTRIBUTE_H
#include "py/obj.h"
#include "shared-module/bleio/Attribute.h"
extern const mp_obj_type_t bleio_attribute_type;
extern void common_hal_bleio_attribute_security_mode_check_valid(bleio_attribute_security_mode_t security_mode);
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_ATTRIBUTE_H

View File

@ -28,6 +28,7 @@
#include "py/objproperty.h"
#include "py/runtime.h"
#include "shared-bindings/bleio/Attribute.h"
#include "shared-bindings/bleio/Characteristic.h"
#include "shared-bindings/bleio/UUID.h"
@ -40,30 +41,25 @@
//| and writing of the characteristic's value.
//|
//|
//| .. class:: Characteristic(uuid, *, broadcast=False, indicate=False, notify=False, read=False, write=False, write_no_response=False)
//| .. class:: Characteristic(uuid, *, properties=0, security_mode=`Attribute.OPEN`, descriptors=None)
//|
//| Create a new Characteristic object identified by the specified UUID.
//|
//| :param bleio.UUID uuid: The uuid of the characteristic
//| :param bool broadcast: Allowed in advertising packets
//| :param bool indicate: Server will indicate to the client when the value is set and wait for a response
//| :param bool notify: Server will notify the client when the value is set
//| :param bool read: Clients may read this characteristic
//| :param bool write: Clients may write this characteristic; a response will be sent back
//| :param bool write_no_response: Clients may write this characteristic; no response will be sent back
//| :param int properties: bitmask of these values bitwise-or'd together: `BROADCAST`, `INDICATE`,
//| `NOTIFY`, `READ`, `WRITE`, `WRITE_NO_RESPONSE`
//| :param int security_mode: one of the integer values `Attribute.NO_ACCESS`, `Attribute.OPEN`,
//| `Attribute.ENCRYPT_NO_MITM`, `Attribute.ENCRYPT_WITH_MITM`, `Attribute.LESC_ENCRYPT_WITH_MITM`,
//| `Attribute.SIGNED_NO_MITM`, `Attribute.SIGNED_WITH_MITM`.
//| :param iterable descriptors: BLE descriptors for this characteristic.
//|
STATIC mp_obj_t bleio_characteristic_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum {
ARG_uuid, ARG_broadcast, ARG_indicate, ARG_notify, ARG_read, ARG_write, ARG_write_no_response,
};
enum { ARG_uuid, ARG_properties, ARG_security_mode, ARG_descriptors };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_uuid, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = mp_const_none} },
{ MP_QSTR_broadcast, MP_ARG_KW_ONLY| MP_ARG_BOOL, {.u_bool = false} },
{ MP_QSTR_indicate, MP_ARG_KW_ONLY| MP_ARG_BOOL, {.u_bool = false} },
{ MP_QSTR_notify, MP_ARG_KW_ONLY| MP_ARG_BOOL, {.u_bool = false} },
{ MP_QSTR_read, MP_ARG_KW_ONLY| MP_ARG_BOOL, {.u_bool = false} },
{ MP_QSTR_write, MP_ARG_KW_ONLY| MP_ARG_BOOL, {.u_bool = false} },
{ MP_QSTR_write_no_response, MP_ARG_KW_ONLY| MP_ARG_BOOL, {.u_bool = false} },
{ MP_QSTR_properties, MP_ARG_KW_ONLY| MP_ARG_INT, {.u_int = 0 } },
{ MP_QSTR_security_mode, MP_ARG_KW_ONLY| MP_ARG_INT, {.u_int = SEC_MODE_OPEN } },
{ MP_QSTR_descriptors, MP_ARG_KW_ONLY| MP_ARG_OBJ, {.u_obj = mp_const_none} },
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
@ -76,129 +72,41 @@ STATIC mp_obj_t bleio_characteristic_make_new(const mp_obj_type_t *type, size_t
}
bleio_uuid_obj_t *uuid = MP_OBJ_TO_PTR(uuid_obj);
const bleio_characteristic_properties_t properties = args[ARG_properties].u_int;
if (properties & ~CHAR_PROP_ALL) {
mp_raise_ValueError(translate("Invalid properties"));
}
const bleio_attribute_security_mode_t security_mode = args[ARG_security_mode].u_int;
common_hal_bleio_attribute_security_mode_check_valid(security_mode);
mp_obj_t descriptors = args[ARG_descriptors].u_obj;
if (descriptors == mp_const_none) {
descriptors = mp_const_empty_tuple;
}
bleio_characteristic_obj_t *self = m_new_obj(bleio_characteristic_obj_t);
self->base.type = &bleio_characteristic_type;
bleio_characteristic_properties_t properties;
properties.broadcast = args[ARG_broadcast].u_bool;
properties.indicate = args[ARG_indicate].u_bool;
properties.notify = args[ARG_notify].u_bool;
properties.read = args[ARG_read].u_bool;
properties.write = args[ARG_write].u_bool;
properties.write_no_response = args[ARG_write_no_response].u_bool;
// Initialize, with an empty descriptor list.
common_hal_bleio_characteristic_construct(self, uuid, properties, mp_obj_new_list(0, NULL));
common_hal_bleio_characteristic_construct(self, uuid, properties, security_mode, descriptors);
return MP_OBJ_FROM_PTR(self);
}
//| .. attribute:: broadcast
//| .. attribute:: properties
//|
//| A `bool` specifying if the characteristic allows broadcasting its value. (read-only)
//| An int bitmask representing which properties are set.
//|
STATIC mp_obj_t bleio_characteristic_get_broadcast(mp_obj_t self_in) {
STATIC mp_obj_t bleio_characteristic_get_properties(mp_obj_t self_in) {
bleio_characteristic_obj_t *self = MP_OBJ_TO_PTR(self_in);
return mp_obj_new_bool(common_hal_bleio_characteristic_get_properties(self).broadcast);
return MP_OBJ_NEW_SMALL_INT(common_hal_bleio_characteristic_get_properties(self));
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_characteristic_get_broadcast_obj, bleio_characteristic_get_broadcast);
STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_characteristic_get_properties_obj, bleio_characteristic_get_properties);
const mp_obj_property_t bleio_characteristic_broadcast_obj = {
const mp_obj_property_t bleio_characteristic_properties_obj = {
.base.type = &mp_type_property,
.proxy = { (mp_obj_t)&bleio_characteristic_get_broadcast_obj,
(mp_obj_t)&mp_const_none_obj,
(mp_obj_t)&mp_const_none_obj },
};
//| .. attribute:: indicate
//|
//| A `bool` specifying if the characteristic allows indicating its value. (read-only)
//|
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(common_hal_bleio_characteristic_get_properties(self).indicate);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_characteristic_get_indicate_obj, bleio_characteristic_get_indicate);
const mp_obj_property_t bleio_characteristic_indicate_obj = {
.base.type = &mp_type_property,
.proxy = { (mp_obj_t)&bleio_characteristic_get_indicate_obj,
(mp_obj_t)&mp_const_none_obj,
(mp_obj_t)&mp_const_none_obj },
};
//| .. attribute:: notify
//|
//| A `bool` specifying if the characteristic allows notifying its value. (read-only)
//|
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(common_hal_bleio_characteristic_get_properties(self).notify);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_characteristic_get_notify_obj, bleio_characteristic_get_notify);
const mp_obj_property_t bleio_characteristic_notify_obj = {
.base.type = &mp_type_property,
.proxy = { (mp_obj_t)&bleio_characteristic_get_notify_obj,
(mp_obj_t)&mp_const_none_obj,
(mp_obj_t)&mp_const_none_obj },
};
//| .. attribute:: read
//|
//| A `bool` specifying if the characteristic allows reading its value. (read-only)
//|
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(common_hal_bleio_characteristic_get_properties(self).read);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_characteristic_get_read_obj, bleio_characteristic_get_read);
const mp_obj_property_t bleio_characteristic_read_obj = {
.base.type = &mp_type_property,
.proxy = { (mp_obj_t)&bleio_characteristic_get_read_obj,
(mp_obj_t)&mp_const_none_obj,
(mp_obj_t)&mp_const_none_obj },
};
//| .. attribute:: write
//|
//| A `bool` specifying if the characteristic allows writing to its value. (read-only)
//|
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(common_hal_bleio_characteristic_get_properties(self).write);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_characteristic_get_write_obj, bleio_characteristic_get_write);
const mp_obj_property_t bleio_characteristic_write_obj = {
.base.type = &mp_type_property,
.proxy = { (mp_obj_t)&bleio_characteristic_get_write_obj,
(mp_obj_t)&mp_const_none_obj,
(mp_obj_t)&mp_const_none_obj },
};
//| .. attribute:: write_no_response
//|
//| A `bool` specifying if the characteristic allows writing to its value without response. (read-only)
//|
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(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);
const mp_obj_property_t bleio_characteristic_write_no_response_obj = {
.base.type = &mp_type_property,
.proxy = { (mp_obj_t)&bleio_characteristic_get_write_no_response_obj,
.proxy = { (mp_obj_t)&bleio_characteristic_get_properties_obj,
(mp_obj_t)&mp_const_none_obj,
(mp_obj_t)&mp_const_none_obj },
};
@ -301,16 +209,43 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(bleio_characteristic_set_cccd_obj, 1, bleio_ch
STATIC const mp_rom_map_elem_t bleio_characteristic_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_broadcast), MP_ROM_PTR(&bleio_characteristic_broadcast_obj) },
{ MP_ROM_QSTR(MP_QSTR_descriptors), MP_ROM_PTR(&bleio_characteristic_descriptors_obj) },
{ MP_ROM_QSTR(MP_QSTR_indicate), MP_ROM_PTR(&bleio_characteristic_indicate_obj) },
{ MP_ROM_QSTR(MP_QSTR_notify), MP_ROM_PTR(&bleio_characteristic_notify_obj) },
{ MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&bleio_characteristic_read_obj) },
{ MP_ROM_QSTR(MP_QSTR_set_cccd), MP_ROM_PTR(&bleio_characteristic_set_cccd_obj) },
{ MP_ROM_QSTR(MP_QSTR_uuid), MP_ROM_PTR(&bleio_characteristic_uuid_obj) },
{ MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&bleio_characteristic_value_obj) },
{ MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&bleio_characteristic_write_obj) },
{ MP_ROM_QSTR(MP_QSTR_write_no_response), MP_ROM_PTR(&bleio_characteristic_write_no_response_obj) },
{ MP_ROM_QSTR(MP_QSTR_properties), MP_ROM_PTR(&bleio_characteristic_get_properties) },
{ MP_ROM_QSTR(MP_QSTR_set_cccd), MP_ROM_PTR(&bleio_characteristic_set_cccd_obj) },
{ MP_ROM_QSTR(MP_QSTR_uuid), MP_ROM_PTR(&bleio_characteristic_uuid_obj) },
{ MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&bleio_characteristic_value_obj) },
// Bitmask constants to represent properties
//| .. data:: BROADCAST
//|
//| property: allowed in advertising packets
//|
//| .. data:: INDICATE
//|
//| property: server will indicate to the client when the value is set and wait for a response
//|
//| .. data:: NOTIFY
//|
//| property: server will notify the client when the value is set
//|
//| .. data:: READ
//|
//| property: clients may read this characteristic
//|
//| .. data:: WRITE
//|
//| property: clients may write this characteristic; a response will be sent back
//|
//| .. data:: WRITE_NO_RESPONSE
//|
//| property: clients may write this characteristic; no response will be sent back
//|
{ MP_ROM_QSTR(MP_QSTR_BROADCAST), MP_ROM_INT(CHAR_PROP_BROADCAST) },
{ MP_ROM_QSTR(MP_QSTR_INDICATE), MP_ROM_INT(CHAR_PROP_INDICATE) },
{ MP_ROM_QSTR(MP_QSTR_NOTIFY), MP_ROM_INT(CHAR_PROP_NOTIFY) },
{ MP_ROM_QSTR(MP_QSTR_READ), MP_ROM_INT(CHAR_PROP_READ) },
{ MP_ROM_QSTR(MP_QSTR_WRITE), MP_ROM_INT(CHAR_PROP_WRITE) },
{ MP_ROM_QSTR(MP_QSTR_WRITE_NO_RESPONSE), MP_ROM_INT(CHAR_PROP_WRITE_NO_RESPONSE) },
};
STATIC MP_DEFINE_CONST_DICT(bleio_characteristic_locals_dict, bleio_characteristic_locals_dict_table);
@ -330,5 +265,5 @@ const mp_obj_type_t bleio_characteristic_type = {
.name = MP_QSTR_Characteristic,
.make_new = bleio_characteristic_make_new,
.print = bleio_characteristic_print,
.locals_dict = (mp_obj_dict_t*)&bleio_characteristic_locals_dict
.locals_dict = (mp_obj_dict_t*)&bleio_characteristic_locals_dict,
};

View File

@ -28,12 +28,13 @@
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_CHARACTERISTIC_H
#define MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_CHARACTERISTIC_H
#include "shared-bindings/bleio/Attribute.h"
#include "shared-module/bleio/Characteristic.h"
#include "common-hal/bleio/Characteristic.h"
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, mp_obj_list_t *descriptor_list);
extern 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 security_mode, mp_obj_t descriptors);
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);

View File

@ -28,6 +28,7 @@
#include "py/objproperty.h"
#include "py/runtime.h"
#include "shared-bindings/bleio/Attribute.h"
#include "shared-bindings/bleio/Descriptor.h"
#include "shared-bindings/bleio/UUID.h"
@ -41,23 +42,15 @@
//| information about the characteristic.
//|
//| .. class:: Descriptor(uuid)
//| .. class:: Descriptor(uuid, security_mode=`Attribute.OPEN`)
//|
//| Create a new descriptor object with the UUID uuid.
//| Create a new descriptor object with the UUID uuid and the given value, if any.
//| .. attribute:: handle
//|
//| The descriptor handle. (read-only)
//|
//| .. attribute:: uuid
//|
//| The descriptor uuid. (read-only)
//|
STATIC mp_obj_t bleio_descriptor_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_uuid };
enum { ARG_uuid, ARG_security_mode };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_uuid, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_uuid, MP_ARG_REQUIRED | MP_ARG_OBJ },
{ MP_QSTR_security_mode, MP_ARG_KW_ONLY| MP_ARG_INT, {.u_int = SEC_MODE_OPEN } },
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
@ -69,28 +62,22 @@ STATIC mp_obj_t bleio_descriptor_make_new(const mp_obj_type_t *type, size_t n_ar
mp_raise_ValueError(translate("Expected a UUID"));
}
const bleio_attribute_security_mode_t security_mode = args[ARG_security_mode].u_int;
common_hal_bleio_attribute_security_mode_check_valid(security_mode);
bleio_descriptor_obj_t *self = m_new_obj(bleio_descriptor_obj_t);
self->base.type = type;
bleio_uuid_obj_t *uuid = MP_OBJ_TO_PTR(uuid_arg);
common_hal_bleio_descriptor_construct(self, uuid);
common_hal_bleio_descriptor_construct(self, uuid, security_mode);
return MP_OBJ_FROM_PTR(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);
return mp_obj_new_int(common_hal_bleio_descriptor_get_handle(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(bleio_descriptor_get_handle_obj, bleio_descriptor_get_handle);
const mp_obj_property_t bleio_descriptor_handle_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&bleio_descriptor_get_handle_obj,
(mp_obj_t)&mp_const_none_obj,
(mp_obj_t)&mp_const_none_obj},
};
//| .. attribute:: uuid
//|
//| The descriptor uuid. (read-only)
//|
STATIC mp_obj_t bleio_descriptor_get_uuid(mp_obj_t self_in) {
bleio_descriptor_obj_t *self = MP_OBJ_TO_PTR(self_in);
@ -108,54 +95,7 @@ const mp_obj_property_t bleio_descriptor_uuid_obj = {
STATIC const mp_rom_map_elem_t bleio_descriptor_locals_dict_table[] = {
// Properties
{ MP_ROM_QSTR(MP_QSTR_handle), MP_ROM_PTR(&bleio_descriptor_handle_obj) },
{ MP_ROM_QSTR(MP_QSTR_uuid), MP_ROM_PTR(&bleio_descriptor_uuid_obj) },
// Static variables
{ MP_ROM_QSTR(MP_QSTR_CHARACTERISTIC_EXTENDED_PROPERTIES),
MP_ROM_INT(DESCRIPTOR_UUID_CHARACTERISTIC_EXTENDED_PROPERTIES) },
{ MP_ROM_QSTR(MP_QSTR_CHARACTERISTIC_USER_DESCRIPTION),
MP_ROM_INT(DESCRIPTOR_UUID_CHARACTERISTIC_USER_DESCRIPTION) },
{ MP_ROM_QSTR(MP_QSTR_CLIENT_CHARACTERISTIC_CONFIGURATION),
MP_ROM_INT(DESCRIPTOR_UUID_CLIENT_CHARACTERISTIC_CONFIGURATION) },
{ MP_ROM_QSTR(MP_QSTR_SERVER_CHARACTERISTIC_CONFIGURATION),
MP_ROM_INT(DESCRIPTOR_UUID_SERVER_CHARACTERISTIC_CONFIGURATION) },
{ MP_ROM_QSTR(MP_QSTR_CHARACTERISTIC_PRESENTATION_FORMAT),
MP_ROM_INT(DESCRIPTOR_UUID_CHARACTERISTIC_PRESENTATION_FORMAT) },
{ MP_ROM_QSTR(MP_QSTR_CHARACTERISTIC_AGGREGATE_FORMAT),
MP_ROM_INT(DESCRIPTOR_UUID_CHARACTERISTIC_AGGREGATE_FORMAT) },
{ MP_ROM_QSTR(MP_QSTR_VALID_RANGE),
MP_ROM_INT(DESCRIPTOR_UUID_VALID_RANGE) },
{ MP_ROM_QSTR(MP_QSTR_EXTERNAL_REPORT_REFERENCE),
MP_ROM_INT(DESCRIPTOR_UUID_EXTERNAL_REPORT_REFERENCE) },
{ MP_ROM_QSTR(MP_QSTR_REPORT_REFERENCE),
MP_ROM_INT(DESCRIPTOR_UUID_REPORT_REFERENCE) },
{ MP_ROM_QSTR(MP_QSTR_NUMBER_OF_DIGITALS),
MP_ROM_INT(DESCRIPTOR_UUID_NUMBER_OF_DIGITALS) },
{ MP_ROM_QSTR(MP_QSTR_VALUE_TRIGGER_SETTING),
MP_ROM_INT(DESCRIPTOR_UUID_VALUE_TRIGGER_SETTING) },
{ MP_ROM_QSTR(MP_QSTR_ENVIRONMENTAL_SENSING_CONFIGURATION),
MP_ROM_INT(DESCRIPTOR_UUID_ENVIRONMENTAL_SENSING_CONFIGURATION) },
{ MP_ROM_QSTR(MP_QSTR_ENVIRONMENTAL_SENSING_MEASUREMENT ),
MP_ROM_INT(DESCRIPTOR_UUID_ENVIRONMENTAL_SENSING_MEASUREMENT) },
{ MP_ROM_QSTR(MP_QSTR_ENVIRONMENTAL_SENSING_TRIGGER_SETTING),
MP_ROM_INT(DESCRIPTOR_UUID_ENVIRONMENTAL_SENSING_TRIGGER_SETTING) },
{ MP_ROM_QSTR(MP_QSTR_TIME_TRIGGER_SETTING),
MP_ROM_INT(DESCRIPTOR_UUID_TIME_TRIGGER_SETTING) },
};
STATIC MP_DEFINE_CONST_DICT(bleio_descriptor_locals_dict, bleio_descriptor_locals_dict_table);

View File

@ -28,31 +28,14 @@
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_DESCRIPTOR_H
#define MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_DESCRIPTOR_H
#include "shared-module/bleio/Attribute.h"
#include "common-hal/bleio/Descriptor.h"
#include "common-hal/bleio/UUID.h"
enum {
DESCRIPTOR_UUID_CHARACTERISTIC_EXTENDED_PROPERTIES = 0x2900,
DESCRIPTOR_UUID_CHARACTERISTIC_USER_DESCRIPTION = 0x2901,
DESCRIPTOR_UUID_CLIENT_CHARACTERISTIC_CONFIGURATION = 0x2902,
DESCRIPTOR_UUID_SERVER_CHARACTERISTIC_CONFIGURATION = 0x2903,
DESCRIPTOR_UUID_CHARACTERISTIC_PRESENTATION_FORMAT = 0x2904,
DESCRIPTOR_UUID_CHARACTERISTIC_AGGREGATE_FORMAT = 0x2905,
DESCRIPTOR_UUID_VALID_RANGE = 0x2906,
DESCRIPTOR_UUID_EXTERNAL_REPORT_REFERENCE = 0x2907,
DESCRIPTOR_UUID_REPORT_REFERENCE = 0x2908,
DESCRIPTOR_UUID_NUMBER_OF_DIGITALS = 0x2909,
DESCRIPTOR_UUID_VALUE_TRIGGER_SETTING = 0x290A,
DESCRIPTOR_UUID_ENVIRONMENTAL_SENSING_CONFIGURATION = 0x290B,
DESCRIPTOR_UUID_ENVIRONMENTAL_SENSING_MEASUREMENT = 0x290C,
DESCRIPTOR_UUID_ENVIRONMENTAL_SENSING_TRIGGER_SETTING = 0x290D,
DESCRIPTOR_UUID_TIME_TRIGGER_SETTING = 0x290E,
};
extern const mp_obj_type_t bleio_descriptor_type;
extern void common_hal_bleio_descriptor_construct(bleio_descriptor_obj_t *self, bleio_uuid_obj_t *uuid);
extern mp_int_t common_hal_bleio_descriptor_get_handle(bleio_descriptor_obj_t *self);
extern mp_obj_t common_hal_bleio_descriptor_get_uuid(bleio_descriptor_obj_t *self);
extern void common_hal_bleio_descriptor_construct(bleio_descriptor_obj_t *self, bleio_uuid_obj_t *uuid, bleio_attribute_security_mode_t security_mode);
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_DESCRIPTOR_H

View File

@ -31,21 +31,26 @@
#include "shared-bindings/bleio/Central.h"
#include "shared-bindings/bleio/Characteristic.h"
#include "shared-bindings/bleio/CharacteristicBuffer.h"
// #include "shared-bindings/bleio/Descriptor.h"
#include "shared-bindings/bleio/Descriptor.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"
#include "shared-bindings/bleio/UUID.h"
//| :mod:`bleio` --- Bluetooth Low Energy functionality
//| :mod:`bleio` --- Bluetooth Low Energy (BLE) communication
//| ================================================================
//|
//| .. module:: bleio
//| :synopsis: Bluetooth Low Energy functionality
//| :platform: nRF
//|
//| The `bleio` module contains methods for managing the BLE adapter.
//| The `bleio` module provides necessary low-level functionality for communicating
//| using Bluetooth Low Energy (BLE). We recommend you use `bleio` in conjunction
//| with the `adafruit_ble <https://circuitpython.readthedocs.io/projects/ble/en/latest/>`_
//| CircuitPython library, which builds on `bleio`, and
//| provides higher-level convenience functionality, including predefined beacons, clients,
//| servers.
//|
//| Libraries
//|
@ -72,20 +77,23 @@
//|
STATIC const mp_rom_map_elem_t bleio_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_bleio) },
{ MP_ROM_QSTR(MP_QSTR_Address), MP_ROM_PTR(&bleio_address_type) },
{ MP_ROM_QSTR(MP_QSTR_Central), MP_ROM_PTR(&bleio_central_type) },
{ MP_ROM_QSTR(MP_QSTR_Characteristic), MP_ROM_PTR(&bleio_characteristic_type) },
{ MP_ROM_QSTR(MP_QSTR_CharacteristicBuffer), MP_ROM_PTR(&bleio_characteristic_buffer_type) },
// { MP_ROM_QSTR(MP_QSTR_Descriptor), MP_ROM_PTR(&bleio_descriptor_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) },
{ MP_ROM_QSTR(MP_QSTR_UUID), MP_ROM_PTR(&bleio_uuid_type) },
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_bleio) },
{ MP_ROM_QSTR(MP_QSTR_Address), MP_ROM_PTR(&bleio_address_type) },
{ MP_ROM_QSTR(MP_QSTR_Central), MP_ROM_PTR(&bleio_central_type) },
{ MP_ROM_QSTR(MP_QSTR_Characteristic), MP_ROM_PTR(&bleio_characteristic_type) },
{ MP_ROM_QSTR(MP_QSTR_CharacteristicBuffer), MP_ROM_PTR(&bleio_characteristic_buffer_type) },
{ MP_ROM_QSTR(MP_QSTR_Descriptor), MP_ROM_PTR(&bleio_descriptor_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) },
{ MP_ROM_QSTR(MP_QSTR_UUID), MP_ROM_PTR(&bleio_uuid_type) },
// Properties
{ MP_ROM_QSTR(MP_QSTR_adapter), MP_ROM_PTR(&common_hal_bleio_adapter_obj) },
{ MP_ROM_QSTR(MP_QSTR_adapter), MP_ROM_PTR(&common_hal_bleio_adapter_obj) },
// constants
{ MP_ROM_QSTR(MP_QSTR_SEC), MP_ROM_PTR(&bleio_uuid_type) },
};
STATIC MP_DEFINE_CONST_DICT(bleio_module_globals, bleio_module_globals_table);

View File

@ -34,7 +34,6 @@
#include "shared-bindings/bleio/__init__.h"
#include "shared-bindings/bleio/Adapter.h"
#include "shared-module/bleio/__init__.h"
#include "common-hal/bleio/Adapter.h"
extern const super_adapter_obj_t common_hal_bleio_adapter_obj;

View File

@ -0,0 +1,46 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2019 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
* 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/runtime.h"
#include "shared-bindings/bleio/Attribute.h"
void common_hal_bleio_attribute_security_mode_check_valid(bleio_attribute_security_mode_t security_mode) {
switch (security_mode) {
case SEC_MODE_NO_ACCESS:
case SEC_MODE_OPEN:
case SEC_MODE_ENC_NO_MITM:
case SEC_MODE_ENC_WITH_MITM:
case SEC_MODE_LESC_ENC_WITH_MITM:
case SEC_MODE_SIGNED_NO_MITM:
case SEC_MODE_SIGNED_WITH_MITM:
break;
default:
mp_raise_ValueError(translate("Invalid security_mode"));
break;
}
}

View File

@ -0,0 +1,41 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2019 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
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef MICROPY_INCLUDED_SHARED_MODULE_BLEIO_ATTRIBUTE_H
#define MICROPY_INCLUDED_SHARED_MODULE_BLEIO_ATTRIBUTE_H
// BLE security modes: 0x<level><mode>
typedef enum {
SEC_MODE_NO_ACCESS = 0x00,
SEC_MODE_OPEN = 0x11,
SEC_MODE_ENC_NO_MITM = 0x21,
SEC_MODE_ENC_WITH_MITM = 0x31,
SEC_MODE_LESC_ENC_WITH_MITM = 0x41,
SEC_MODE_SIGNED_NO_MITM = 0x12,
SEC_MODE_SIGNED_WITH_MITM = 0x22,
} bleio_attribute_security_mode_t;
#endif // MICROPY_INCLUDED_SHARED_MODULE_BLEIO_ATTRIBUTE_H

View File

@ -27,14 +27,17 @@
#ifndef MICROPY_INCLUDED_SHARED_MODULE_BLEIO_CHARACTERISTIC_H
#define MICROPY_INCLUDED_SHARED_MODULE_BLEIO_CHARACTERISTIC_H
// Flags for each characteristic property. Common across ports.
typedef struct {
bool broadcast : 1;
bool read : 1;
bool write_no_response : 1;
bool write : 1;
bool notify : 1;
bool indicate : 1;
} bleio_characteristic_properties_t;
typedef enum {
CHAR_PROP_NONE = 0,
CHAR_PROP_BROADCAST = 1u << 0,
CHAR_PROP_INDICATE = 1u << 1,
CHAR_PROP_NOTIFY = 1u << 2,
CHAR_PROP_READ = 1u << 3,
CHAR_PROP_WRITE = 1u << 4,
CHAR_PROP_WRITE_NO_RESPONSE = 1u << 5,
CHAR_PROP_ALL = (CHAR_PROP_BROADCAST | CHAR_PROP_INDICATE | CHAR_PROP_NOTIFY |
CHAR_PROP_READ | CHAR_PROP_WRITE | CHAR_PROP_WRITE_NO_RESPONSE)
} bleio_characteristic_properties_enum_t;
typedef uint8_t bleio_characteristic_properties_t;
#endif // MICROPY_INCLUDED_SHARED_MODULE_BLEIO_CHARACTERISTIC_H