summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorarturo182 <arturo182@tlen.pl>2018-07-18 23:47:06 +0200
committerarturo182 <arturo182@tlen.pl>2018-10-21 15:43:48 +0200
commitcc782492269f2311995269507ff1411d3bbb8a2f (patch)
treeae0f4ab71967e273c4172f4b2c145d1df36d9dcf
parentfb422ccf5e4dcf644a4c99fe4843af2838a23907 (diff)
nrf: Move the Characteristic class from ubluepy to the shared bleio module
-rwxr-xr-xports/nrf/Makefile2
-rw-r--r--ports/nrf/common-hal/bleio/Characteristic.c53
-rw-r--r--ports/nrf/common-hal/bleio/UUID.h1
-rw-r--r--ports/nrf/drivers/bluetooth/ble_drv.c151
-rw-r--r--ports/nrf/drivers/bluetooth/ble_drv.h22
-rw-r--r--ports/nrf/modules/ubluepy/modubluepy.c2
-rw-r--r--ports/nrf/modules/ubluepy/modubluepy.h15
-rw-r--r--ports/nrf/modules/ubluepy/ubluepy_characteristic.c224
-rw-r--r--ports/nrf/modules/ubluepy/ubluepy_peripheral.c18
-rw-r--r--ports/nrf/modules/ubluepy/ubluepy_service.c11
-rw-r--r--shared-bindings/bleio/Characteristic.c330
-rw-r--r--shared-bindings/bleio/Characteristic.h37
-rw-r--r--shared-bindings/bleio/__init__.c3
-rw-r--r--shared-module/bleio/Characteristic.h52
14 files changed, 578 insertions, 343 deletions
diff --git a/ports/nrf/Makefile b/ports/nrf/Makefile
index 8a4454a0a..6ed2d3054 100755
--- a/ports/nrf/Makefile
+++ b/ports/nrf/Makefile
@@ -131,7 +131,6 @@ DRIVERS_SRC_C += $(addprefix modules/,\
ubluepy/modubluepy.c \
ubluepy/ubluepy_peripheral.c \
ubluepy/ubluepy_service.c \
- ubluepy/ubluepy_characteristic.c \
)
SRC_COMMON_HAL += \
@@ -163,6 +162,7 @@ ifneq ($(SD), )
SRC_COMMON_HAL += \
bleio/__init__.c \
bleio/Adapter.c \
+ bleio/Characteristic.c \
bleio/Descriptor.c \
bleio/Scanner.c \
bleio/UUID.c
diff --git a/ports/nrf/common-hal/bleio/Characteristic.c b/ports/nrf/common-hal/bleio/Characteristic.c
new file mode 100644
index 000000000..3087b6707
--- /dev/null
+++ b/ports/nrf/common-hal/bleio/Characteristic.c
@@ -0,0 +1,53 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2018 Artur Pacholec
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "ble_drv.h"
+#include "shared-module/bleio/Characteristic.h"
+
+void data_callback(bleio_characteristic_obj_t *self, uint16_t length, uint8_t *data) {
+ self->value_data = mp_obj_new_bytearray(length, data);
+}
+
+void common_hal_bleio_characteristic_read_value(bleio_characteristic_obj_t *self) {
+ ble_drv_attr_c_read(self, data_callback);
+}
+
+void common_hal_bleio_characteristic_write_value(bleio_characteristic_obj_t *self, mp_buffer_info_t *bufinfo) {
+ ubluepy_service_obj_t *service = MP_OBJ_TO_PTR(self->service);
+ ubluepy_role_type_t role = service->p_periph->role;
+
+ if (role == UBLUEPY_ROLE_PERIPHERAL) {
+ // TODO: Add indications
+ if (self->props.notify) {
+ ble_drv_attr_s_notify(self, bufinfo);
+ } else {
+ ble_drv_attr_s_write(self, bufinfo);
+ }
+ } else {
+ ble_drv_attr_c_write(self, bufinfo);
+ }
+
+}
diff --git a/ports/nrf/common-hal/bleio/UUID.h b/ports/nrf/common-hal/bleio/UUID.h
index b919fec28..cb0fddcfd 100644
--- a/ports/nrf/common-hal/bleio/UUID.h
+++ b/ports/nrf/common-hal/bleio/UUID.h
@@ -28,7 +28,6 @@
#ifndef MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_UUID_H
#define MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_UUID_H
-#include "py/obj.h"
#include "shared-bindings/bleio/UUIDType.h"
typedef struct {
diff --git a/ports/nrf/drivers/bluetooth/ble_drv.c b/ports/nrf/drivers/bluetooth/ble_drv.c
index 1d67842ef..6240a18e4 100644
--- a/ports/nrf/drivers/bluetooth/ble_drv.c
+++ b/ports/nrf/drivers/bluetooth/ble_drv.c
@@ -98,7 +98,7 @@ static bleio_scanner_obj_t *mp_adv_observer;
static mp_obj_t mp_gattc_observer;
static mp_obj_t mp_gattc_disc_service_observer;
static mp_obj_t mp_gattc_disc_char_observer;
-static mp_obj_t mp_gattc_char_data_observer;
+static bleio_characteristic_obj_t *mp_gattc_char_data_observer;
#if (BLUETOOTH_SD == 140)
static uint8_t m_adv_handle = BLE_GAP_ADV_SET_HANDLE_NOT_SET;
@@ -270,38 +270,26 @@ bool ble_drv_uuid_add_vs(uint8_t * p_uuid, uint8_t * idx) {
bool ble_drv_service_add(ubluepy_service_obj_t * p_service_obj) {
SD_TEST_OR_ENABLE();
- if (p_service_obj->p_uuid->type > BLE_UUID_TYPE_BLE) {
+ ble_uuid_t uuid;
+ uuid.type = BLE_UUID_TYPE_BLE;
+ uuid.uuid = p_service_obj->p_uuid->value[0] | (p_service_obj->p_uuid->value[1] << 8);
- ble_uuid_t uuid;
+ if (p_service_obj->p_uuid->type == UUID_TYPE_128BIT) {
uuid.type = p_service_obj->p_uuid->uuid_vs_idx;
- uuid.uuid = p_service_obj->p_uuid->value[0];
- uuid.uuid += p_service_obj->p_uuid->value[1] << 8;
-
- if (sd_ble_gatts_service_add(p_service_obj->type,
- &uuid,
- &p_service_obj->handle) != 0) {
- nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
- translate("Can not add Service.")));
- }
- } else if (p_service_obj->p_uuid->type == BLE_UUID_TYPE_BLE) {
- BLE_DRIVER_LOG("adding service\n");
-
- ble_uuid_t uuid;
- uuid.type = p_service_obj->p_uuid->type;
- uuid.uuid = p_service_obj->p_uuid->value[0];
- uuid.uuid += p_service_obj->p_uuid->value[1] << 8;
+ }
- if (sd_ble_gatts_service_add(p_service_obj->type,
- &uuid,
- &p_service_obj->handle) != 0) {
- nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
- translate("Can not add Service.")));
- }
+ uint32_t err_code = sd_ble_gatts_service_add(p_service_obj->type,
+ &uuid,
+ &p_service_obj->handle);
+ if (err_code != 0) {
+ nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
+ translate("Can not add Service. status: 0x%08lX"), err_code));
}
+
return true;
}
-bool ble_drv_characteristic_add(ubluepy_characteristic_obj_t * p_char_obj) {
+bool ble_drv_characteristic_add(bleio_characteristic_obj_t *characteristic) {
ble_gatts_char_md_t char_md;
ble_gatts_attr_md_t cccd_md;
ble_gatts_attr_t attr_char_value;
@@ -310,24 +298,19 @@ bool ble_drv_characteristic_add(ubluepy_characteristic_obj_t * p_char_obj) {
memset(&char_md, 0, sizeof(char_md));
- char_md.char_props.broadcast = (p_char_obj->props & UBLUEPY_PROP_BROADCAST) ? 1 : 0;
- char_md.char_props.read = (p_char_obj->props & UBLUEPY_PROP_READ) ? 1 : 0;
- char_md.char_props.write_wo_resp = (p_char_obj->props & UBLUEPY_PROP_WRITE_WO_RESP) ? 1 : 0;
- char_md.char_props.write = (p_char_obj->props & UBLUEPY_PROP_WRITE) ? 1 : 0;
- char_md.char_props.notify = (p_char_obj->props & UBLUEPY_PROP_NOTIFY) ? 1 : 0;
- char_md.char_props.indicate = (p_char_obj->props & UBLUEPY_PROP_INDICATE) ? 1 : 0;
-#if 0
- char_md.char_props.auth_signed_wr = (p_char_obj->props & UBLUEPY_PROP_NOTIFY) ? 1 : 0;
-#endif
-
+ char_md.char_props.broadcast = characteristic->props.broadcast;
+ char_md.char_props.read = characteristic->props.read;
+ char_md.char_props.write_wo_resp = characteristic->props.write_wo_resp;
+ char_md.char_props.write = characteristic->props.write;
+ char_md.char_props.notify = characteristic->props.notify;
+ char_md.char_props.indicate = characteristic->props.indicate;
char_md.p_char_user_desc = NULL;
char_md.p_char_pf = NULL;
char_md.p_user_desc_md = NULL;
char_md.p_sccd_md = NULL;
- // if cccd
- if (p_char_obj->attrs & UBLUEPY_ATTR_CCCD) {
+ if (characteristic->props.notify || characteristic->props.notify) {
memset(&cccd_md, 0, sizeof(cccd_md));
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm);
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.write_perm);
@@ -337,9 +320,12 @@ bool ble_drv_characteristic_add(ubluepy_characteristic_obj_t * p_char_obj) {
char_md.p_cccd_md = NULL;
}
- uuid.type = p_char_obj->p_uuid->type;
- uuid.uuid = p_char_obj->p_uuid->value[0];
- uuid.uuid += p_char_obj->p_uuid->value[1] << 8;
+ uuid.type = BLE_UUID_TYPE_BLE;
+ if (characteristic->uuid->type == UUID_TYPE_128BIT)
+ uuid.type = characteristic->uuid->uuid_vs_idx;
+
+ uuid.uuid = characteristic->uuid->value[0];
+ uuid.uuid += characteristic->uuid->value[1] << 8;
memset(&attr_md, 0, sizeof(attr_md));
@@ -365,19 +351,20 @@ bool ble_drv_characteristic_add(ubluepy_characteristic_obj_t * p_char_obj) {
ble_gatts_char_handles_t handles;
- if (sd_ble_gatts_characteristic_add(p_char_obj->service_handle,
- &char_md,
- &attr_char_value,
- &handles) != 0) {
+ uint32_t err_code = sd_ble_gatts_characteristic_add(characteristic->service_handle,
+ &char_md,
+ &attr_char_value,
+ &handles);
+ if (err_code != 0) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
- translate("Can not add Characteristic.")));
+ translate("Can not add Characteristic. status: 0x%08lX"), err_code));
}
// apply handles to object instance
- p_char_obj->handle = handles.value_handle;
- p_char_obj->user_desc_handle = handles.user_desc_handle;
- p_char_obj->cccd_handle = handles.cccd_handle;
- p_char_obj->sccd_handle = handles.sccd_handle;
+ characteristic->handle = handles.value_handle;
+ characteristic->user_desc_handle = handles.user_desc_handle;
+ characteristic->cccd_handle = handles.cccd_handle;
+ characteristic->sccd_handle = handles.sccd_handle;
return true;
}
@@ -652,15 +639,18 @@ void ble_drv_attr_s_read(uint16_t conn_handle, uint16_t handle, uint16_t len, ui
}
-void ble_drv_attr_s_write(uint16_t conn_handle, uint16_t handle, uint16_t len, uint8_t * p_data) {
+void ble_drv_attr_s_write(bleio_characteristic_obj_t *characteristic, mp_buffer_info_t *bufinfo) {
+ ubluepy_service_obj_t *service = MP_OBJ_TO_PTR(characteristic->service);
+ uint16_t conn_handle = service->p_periph->conn_handle;
ble_gatts_value_t gatts_value;
+
memset(&gatts_value, 0, sizeof(gatts_value));
- gatts_value.len = len;
+ gatts_value.len = bufinfo->len;
gatts_value.offset = 0;
- gatts_value.p_value = p_data;
+ gatts_value.p_value = bufinfo->buf;
- uint32_t err_code = sd_ble_gatts_value_set(conn_handle, handle, &gatts_value);
+ uint32_t err_code = sd_ble_gatts_value_set(conn_handle, characteristic->handle, &gatts_value);
if (err_code != 0) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
@@ -668,17 +658,19 @@ void ble_drv_attr_s_write(uint16_t conn_handle, uint16_t handle, uint16_t len, u
}
}
-void ble_drv_attr_s_notify(uint16_t conn_handle, uint16_t handle, uint16_t len, uint8_t * p_data) {
- uint16_t hvx_len = len;
+void ble_drv_attr_s_notify(bleio_characteristic_obj_t *characteristic, mp_buffer_info_t *bufinfo) {
+ ubluepy_service_obj_t *service = MP_OBJ_TO_PTR(characteristic->service);
+ uint16_t conn_handle = service->p_periph->conn_handle;
ble_gatts_hvx_params_t hvx_params;
+ uint16_t hvx_len = bufinfo->len;
memset(&hvx_params, 0, sizeof(hvx_params));
- hvx_params.handle = handle;
+ hvx_params.handle = characteristic->handle;
hvx_params.type = BLE_GATT_HVX_NOTIFICATION;
hvx_params.offset = 0;
hvx_params.p_len = &hvx_len;
- hvx_params.p_data = p_data;
+ hvx_params.p_data = bufinfo->buf;
while (m_tx_in_progress) {
;
@@ -713,13 +705,13 @@ void ble_drv_adv_report_handler_set(bleio_scanner_obj_t *self, ble_drv_adv_evt_c
}
-void ble_drv_attr_c_read(uint16_t conn_handle, uint16_t handle, mp_obj_t obj, ble_drv_gattc_char_data_callback_t cb) {
-
- mp_gattc_char_data_observer = obj;
+void ble_drv_attr_c_read(bleio_characteristic_obj_t *characteristic, ble_drv_gattc_char_data_callback_t cb) {
+ ubluepy_service_obj_t *service = MP_OBJ_TO_PTR(characteristic->service);
+ mp_gattc_char_data_observer = characteristic;
gattc_char_data_handle = cb;
- uint32_t err_code = sd_ble_gattc_read(conn_handle,
- handle,
+ const uint32_t err_code = sd_ble_gattc_read(service->p_periph->conn_handle,
+ characteristic->handle,
0);
if (err_code != 0) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
@@ -731,26 +723,26 @@ void ble_drv_attr_c_read(uint16_t conn_handle, uint16_t handle, mp_obj_t obj, bl
}
}
-void ble_drv_attr_c_write(uint16_t conn_handle, uint16_t handle, uint16_t len, uint8_t * p_data, bool w_response) {
+void ble_drv_attr_c_write(bleio_characteristic_obj_t *characteristic, mp_buffer_info_t *bufinfo) {
+ ubluepy_service_obj_t *service = MP_OBJ_TO_PTR(characteristic->service);
+ uint16_t conn_handle = service->p_periph->conn_handle;
ble_gattc_write_params_t write_params;
+ write_params.write_op = BLE_GATT_OP_WRITE_REQ;
- if (w_response) {
- write_params.write_op = BLE_GATT_OP_WRITE_REQ;
- } else {
+ if (characteristic->props.write_wo_resp) {
write_params.write_op = BLE_GATT_OP_WRITE_CMD;
}
write_params.flags = BLE_GATT_EXEC_WRITE_FLAG_PREPARED_CANCEL;
- write_params.handle = handle;
+ write_params.handle = characteristic->handle;
write_params.offset = 0;
- write_params.len = len;
- write_params.p_value = p_data;
+ write_params.len = bufinfo->len;
+ write_params.p_value = bufinfo->buf;
- m_write_done = !w_response;
+ m_write_done = (write_params.write_op == BLE_GATT_OP_WRITE_CMD);
uint32_t err_code = sd_ble_gattc_write(conn_handle, &write_params);
-
if (err_code != 0) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
translate("Can not write attribute value. status: 0x%02x"), (uint16_t)err_code));
@@ -1053,15 +1045,12 @@ static void ble_evt_handler(ble_evt_t * p_ble_evt) {
char_data.decl_handle = p_char->handle_decl;
char_data.value_handle = p_char->handle_value;
- char_data.props |= (p_char->char_props.broadcast) ? UBLUEPY_PROP_BROADCAST : 0;
- char_data.props |= (p_char->char_props.read) ? UBLUEPY_PROP_READ : 0;
- char_data.props |= (p_char->char_props.write_wo_resp) ? UBLUEPY_PROP_WRITE_WO_RESP : 0;
- char_data.props |= (p_char->char_props.write) ? UBLUEPY_PROP_WRITE : 0;
- char_data.props |= (p_char->char_props.notify) ? UBLUEPY_PROP_NOTIFY : 0;
- char_data.props |= (p_char->char_props.indicate) ? UBLUEPY_PROP_INDICATE : 0;
- #if 0
- char_data.props |= (p_char->char_props.auth_signed_wr) ? UBLUEPY_PROP_NOTIFY : 0;
- #endif
+ char_data.props.broadcast = p_char->char_props.broadcast;
+ char_data.props.read = p_char->char_props.read;
+ char_data.props.write_wo_resp = p_char->char_props.write_wo_resp;
+ char_data.props.write = p_char->char_props.write;
+ char_data.props.notify = p_char->char_props.notify;
+ char_data.props.indicate = p_char->char_props.indicate;
disc_add_char_handler(mp_gattc_disc_char_observer, &char_data);
}
diff --git a/ports/nrf/drivers/bluetooth/ble_drv.h b/ports/nrf/drivers/bluetooth/ble_drv.h
index d344e2690..7283dbfce 100644
--- a/ports/nrf/drivers/bluetooth/ble_drv.h
+++ b/ports/nrf/drivers/bluetooth/ble_drv.h
@@ -32,6 +32,7 @@
#include <stdint.h>
#include <stdbool.h>
+#include "shared-module/bleio/Characteristic.h"
#include "shared-module/bleio/Scanner.h"
#include "modubluepy.h"
@@ -61,7 +62,14 @@ typedef struct {
typedef struct {
uint16_t uuid;
uint8_t uuid_type;
- uint8_t props;
+ struct {
+ bool broadcast : 1;
+ bool read : 1;
+ bool write_wo_resp : 1;
+ bool write : 1;
+ bool notify : 1;
+ bool indicate : 1;
+ } props;
uint16_t decl_handle;
uint16_t value_handle;
} ble_drv_char_data_t;
@@ -72,7 +80,7 @@ typedef void (*ble_drv_gattc_evt_callback_t)(mp_obj_t self, uint16_t event_id, u
typedef void (*ble_drv_adv_evt_callback_t)(bleio_scanner_obj_t *self, ble_drv_adv_data_t *data);
typedef void (*ble_drv_disc_add_service_callback_t)(mp_obj_t self, ble_drv_service_data_t * p_service_data);
typedef void (*ble_drv_disc_add_char_callback_t)(mp_obj_t self, ble_drv_char_data_t * p_desc_data);
-typedef void (*ble_drv_gattc_char_data_callback_t)(mp_obj_t self, uint16_t length, uint8_t * p_data);
+typedef void (*ble_drv_gattc_char_data_callback_t)(bleio_characteristic_obj_t *self, uint16_t length, uint8_t * p_data);
uint32_t ble_drv_stack_enable(void);
@@ -86,7 +94,7 @@ bool ble_drv_uuid_add_vs(uint8_t * p_uuid, uint8_t * idx);
bool ble_drv_service_add(ubluepy_service_obj_t * p_service_obj);
-bool ble_drv_characteristic_add(ubluepy_characteristic_obj_t * p_char_obj);
+bool ble_drv_characteristic_add(bleio_characteristic_obj_t *characteristic);
bool ble_drv_advertise_data(ubluepy_advertise_data_t * p_adv_params);
@@ -100,13 +108,13 @@ void ble_drv_gattc_event_handler_set(mp_obj_t obj, ble_drv_gattc_evt_callback_t
void ble_drv_attr_s_read(uint16_t conn_handle, uint16_t handle, uint16_t len, uint8_t * p_data);
-void ble_drv_attr_c_read(uint16_t conn_handle, uint16_t handle, mp_obj_t obj, ble_drv_gattc_char_data_callback_t cb);
+void ble_drv_attr_c_read(bleio_characteristic_obj_t *characteristic, ble_drv_gattc_char_data_callback_t cb);
-void ble_drv_attr_s_write(uint16_t conn_handle, uint16_t handle, uint16_t len, uint8_t * p_data);
+void ble_drv_attr_s_write(bleio_characteristic_obj_t *characteristic, mp_buffer_info_t *bufinfo);
-void ble_drv_attr_s_notify(uint16_t conn_handle, uint16_t handle, uint16_t len, uint8_t * p_data);
+void ble_drv_attr_s_notify(bleio_characteristic_obj_t *characteristic, mp_buffer_info_t *bufinfo);
-void ble_drv_attr_c_write(uint16_t conn_handle, uint16_t handle, uint16_t len, uint8_t * p_data, bool w_response);
+void ble_drv_attr_c_write(bleio_characteristic_obj_t *characteristic, mp_buffer_info_t *bufinfo);
void ble_drv_scan_start(uint16_t interval, uint16_t window);
diff --git a/ports/nrf/modules/ubluepy/modubluepy.c b/ports/nrf/modules/ubluepy/modubluepy.c
index 1470f4a37..2d1a6a550 100644
--- a/ports/nrf/modules/ubluepy/modubluepy.c
+++ b/ports/nrf/modules/ubluepy/modubluepy.c
@@ -30,7 +30,6 @@
extern const mp_obj_type_t ubluepy_peripheral_type;
extern const mp_obj_type_t ubluepy_service_type;
-extern const mp_obj_type_t ubluepy_characteristic_type;
STATIC const mp_rom_map_elem_t mp_module_ubluepy_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_ubluepy) },
@@ -38,7 +37,6 @@ STATIC const mp_rom_map_elem_t mp_module_ubluepy_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_Peripheral), MP_ROM_PTR(&ubluepy_peripheral_type) },
#endif
{ MP_ROM_QSTR(MP_QSTR_Service), MP_ROM_PTR(&ubluepy_service_type) },
- { MP_ROM_QSTR(MP_QSTR_Characteristic), MP_ROM_PTR(&ubluepy_characteristic_type) },
};
STATIC MP_DEFINE_CONST_DICT(mp_module_ubluepy_globals, mp_module_ubluepy_globals_table);
diff --git a/ports/nrf/modules/ubluepy/modubluepy.h b/ports/nrf/modules/ubluepy/modubluepy.h
index 5d80e3536..ad33001cb 100644
--- a/ports/nrf/modules/ubluepy/modubluepy.h
+++ b/ports/nrf/modules/ubluepy/modubluepy.h
@@ -75,7 +75,6 @@ p.advertise(device_name="micr", services=[s])
#include "py/obj.h"
extern const mp_obj_type_t ubluepy_service_type;
-extern const mp_obj_type_t ubluepy_characteristic_type;
extern const mp_obj_type_t ubluepy_peripheral_type;
typedef enum {
@@ -116,20 +115,6 @@ typedef struct _ubluepy_service_obj_t {
uint16_t end_handle;
} ubluepy_service_obj_t;
-typedef struct _ubluepy_characteristic_obj_t {
- mp_obj_base_t base;
- uint16_t handle;
- bleio_uuid_obj_t * p_uuid;
- uint16_t service_handle;
- uint16_t user_desc_handle;
- uint16_t cccd_handle;
- uint16_t sccd_handle;
- uint8_t props;
- uint8_t attrs;
- ubluepy_service_obj_t * p_service;
- mp_obj_t value_data;
-} ubluepy_characteristic_obj_t;
-
typedef struct _ubluepy_advertise_data_t {
uint8_t * p_device_name;
uint8_t device_name_len;
diff --git a/ports/nrf/modules/ubluepy/ubluepy_characteristic.c b/ports/nrf/modules/ubluepy/ubluepy_characteristic.c
deleted file mode 100644
index e0259fb06..000000000
--- a/ports/nrf/modules/ubluepy/ubluepy_characteristic.c
+++ /dev/null
@@ -1,224 +0,0 @@
-/*
- * This file is part of the MicroPython project, http://micropython.org/
- *
- * The MIT License (MIT)
- *
- * Copyright (c) 2017 Glenn Ruben Bakke
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
-
-#include "py/obj.h"
-#include "py/runtime.h"
-#include "supervisor/shared/translate.h"
-
-#if MICROPY_PY_UBLUEPY_PERIPHERAL || MICROPY_PY_UBLUEPY_CENTRAL
-
-#include "modubluepy.h"
-#include "ble_drv.h"
-#include "shared-bindings/bleio/UUID.h"
-
-STATIC void ubluepy_characteristic_print(const mp_print_t *print, mp_obj_t o, mp_print_kind_t kind) {
- ubluepy_characteristic_obj_t * self = (ubluepy_characteristic_obj_t *)o;
-
- mp_printf(print, "Characteristic(handle: 0x" HEX2_FMT ", conn_handle: " HEX2_FMT ")",
- self->handle, self->p_service->p_periph->conn_handle);
-}
-
-STATIC mp_obj_t ubluepy_characteristic_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
- static const mp_arg_t allowed_args[] = {
- { MP_QSTR_uuid, MP_ARG_REQUIRED| MP_ARG_OBJ, {.u_obj = mp_const_none} },
- { MP_QSTR_props, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = UBLUEPY_PROP_READ | UBLUEPY_PROP_WRITE} },
- { MP_QSTR_attrs, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
- };
-
- // parse args
- mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
- mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
-
- ubluepy_characteristic_obj_t *s = m_new_obj(ubluepy_characteristic_obj_t);
- s->base.type = type;
-
- mp_obj_t uuid_obj = args[0].u_obj;
-
- if (uuid_obj == mp_const_none) {
- return MP_OBJ_FROM_PTR(s);
- }
-
- if (MP_OBJ_IS_TYPE(uuid_obj, &bleio_uuid_type)) {
- s->p_uuid = MP_OBJ_TO_PTR(uuid_obj);
- // (void)sd_characterstic_add(s);
- } else {
- nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
- translate("Invalid UUID parameter")));
- }
-
- if (args[1].u_int > 0) {
- s->props = (uint8_t)args[1].u_int;
- }
-
- if (args[2].u_int > 0) {
- s->attrs = (uint8_t)args[2].u_int;
- }
-
- // clear pointer to service
- s->p_service = NULL;
-
- // clear pointer to char value data
- s->value_data = NULL;
-
- return MP_OBJ_FROM_PTR(s);
-}
-
-void char_data_callback(mp_obj_t self_in, uint16_t length, uint8_t * p_data) {
- ubluepy_characteristic_obj_t * self = MP_OBJ_TO_PTR(self_in);
- self->value_data = mp_obj_new_bytearray(length, p_data);
-}
-
-/// \method read()
-/// Read Characteristic value.
-///
-STATIC mp_obj_t char_read(mp_obj_t self_in) {
- ubluepy_characteristic_obj_t * self = MP_OBJ_TO_PTR(self_in);
-
-#if MICROPY_PY_UBLUEPY_CENTRAL
- // TODO: free any previous allocation of value_data
-
- ble_drv_attr_c_read(self->p_service->p_periph->conn_handle,
- self->handle,
- self_in,
- char_data_callback);
-
- return self->value_data;
-#else
- (void)self;
- return mp_const_none;
-#endif
-}
-STATIC MP_DEFINE_CONST_FUN_OBJ_1(ubluepy_characteristic_read_obj, char_read);
-
-/// \method write(data, [with_response=False])
-/// Write Characteristic value.
-///
-STATIC mp_obj_t char_write(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
- ubluepy_characteristic_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
- mp_obj_t data = pos_args[1];
-
- static const mp_arg_t allowed_args[] = {
- { MP_QSTR_with_response, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false } },
- };
-
- // parse args
- mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
- mp_arg_parse_all(n_args - 2, pos_args + 2, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
-
- mp_buffer_info_t bufinfo;
- mp_get_buffer_raise(data, &bufinfo, MP_BUFFER_READ);
-
- // figure out mode of the Peripheral
- ubluepy_role_type_t role = self->p_service->p_periph->role;
-
- if (role == UBLUEPY_ROLE_PERIPHERAL) {
- if (self->props & UBLUEPY_PROP_NOTIFY) {
- ble_drv_attr_s_notify(self->p_service->p_periph->conn_handle,
- self->handle,
- bufinfo.len,
- bufinfo.buf);
- } else {
- ble_drv_attr_s_write(self->p_service->p_periph->conn_handle,
- self->handle,
- bufinfo.len,
- bufinfo.buf);
- }
- } else {
-#if MICROPY_PY_UBLUEPY_CENTRAL
- bool with_response = args[0].u_bool;
-
- ble_drv_attr_c_write(self->p_service->p_periph->conn_handle,
- self->handle,
- bufinfo.len,
- bufinfo.buf,
- with_response);
-#endif
- }
- return mp_const_none;
-}
-STATIC MP_DEFINE_CONST_FUN_OBJ_KW(ubluepy_characteristic_write_obj, 2, char_write);
-
-/// \method properties()
-/// Read Characteristic value properties.
-///
-STATIC mp_obj_t char_properties(mp_obj_t self_in) {
- ubluepy_characteristic_obj_t * self = MP_OBJ_TO_PTR(self_in);
- return MP_OBJ_NEW_SMALL_INT(self->props);
-}
-STATIC MP_DEFINE_CONST_FUN_OBJ_1(ubluepy_characteristic_get_properties_obj, char_properties);
-
-/// \method uuid()
-/// Get UUID instance of the characteristic.
-///
-STATIC mp_obj_t char_uuid(mp_obj_t self_in) {
- ubluepy_characteristic_obj_t * self = MP_OBJ_TO_PTR(self_in);
- return MP_OBJ_FROM_PTR(self->p_uuid);
-}
-STATIC MP_DEFINE_CONST_FUN_OBJ_1(ubluepy_characteristic_get_uuid_obj, char_uuid);
-
-
-STATIC const mp_rom_map_elem_t ubluepy_characteristic_locals_dict_table[] = {
- { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&ubluepy_characteristic_read_obj) },
- { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&ubluepy_characteristic_write_obj) },
-#if 0
- { MP_ROM_QSTR(MP_QSTR_supportsRead), MP_ROM_PTR(&ubluepy_characteristic_supports_read_obj) },
- { MP_ROM_QSTR(MP_QSTR_propertiesToString), MP_ROM_PTR(&ubluepy_characteristic_properties_to_str_obj) },
- { MP_ROM_QSTR(MP_QSTR_getHandle), MP_ROM_PTR(&ubluepy_characteristic_get_handle_obj) },
-
- // Properties
- { MP_ROM_QSTR(MP_QSTR_peripheral), MP_ROM_PTR(&ubluepy_characteristic_get_peripheral_obj) },
-#endif
- { MP_ROM_QSTR(MP_QSTR_uuid), MP_ROM_PTR(&ubluepy_characteristic_get_uuid_obj) },
- { MP_ROM_QSTR(MP_QSTR_properties), MP_ROM_PTR(&ubluepy_characteristic_get_properties_obj) },
-
- { MP_ROM_QSTR(MP_QSTR_PROP_BROADCAST), MP_ROM_INT(UBLUEPY_PROP_BROADCAST) },
- { MP_ROM_QSTR(MP_QSTR_PROP_READ), MP_ROM_INT(UBLUEPY_PROP_READ) },
- { MP_ROM_QSTR(MP_QSTR_PROP_WRITE_WO_RESP), MP_ROM_INT(UBLUEPY_PROP_WRITE_WO_RESP) },
- { MP_ROM_QSTR(MP_QSTR_PROP_WRITE), MP_ROM_INT(UBLUEPY_PROP_WRITE) },
- { MP_ROM_QSTR(MP_QSTR_PROP_NOTIFY), MP_ROM_INT(UBLUEPY_PROP_NOTIFY) },
- { MP_ROM_QSTR(MP_QSTR_PROP_INDICATE), MP_ROM_INT(UBLUEPY_PROP_INDICATE) },
- { MP_ROM_QSTR(MP_QSTR_PROP_AUTH_SIGNED_WR), MP_ROM_INT(UBLUEPY_PROP_AUTH_SIGNED_WR) },
-
-#if MICROPY_PY_UBLUEPY_PERIPHERAL
- { MP_ROM_QSTR(MP_QSTR_ATTR_CCCD), MP_ROM_INT(UBLUEPY_ATTR_CCCD) },
-#endif
-
-#if MICROPY_PY_UBLUEPY_CENTRAL
- { MP_ROM_QSTR(MP_QSTR_PROP_AUTH_SIGNED_WR), MP_ROM_INT(UBLUEPY_ATTR_SCCD) },
-#endif
-};
-
-STATIC MP_DEFINE_CONST_DICT(ubluepy_characteristic_locals_dict, ubluepy_characteristic_locals_dict_table);
-
-const mp_obj_type_t ubluepy_characteristic_type = {
- { &mp_type_type },
- .name = MP_QSTR_Characteristic,
- .print = ubluepy_characteristic_print,
- .make_new = ubluepy_characteristic_make_new,
- .locals_dict = (mp_obj_dict_t*)&ubluepy_characteristic_locals_dict
-};
-
-#endif // MICROPY_PY_UBLUEPY_PERIPHERAL || MICROPY_PY_UBLUEPY_CENTRAL
diff --git a/ports/nrf/modules/ubluepy/ubluepy_peripheral.c b/ports/nrf/modules/ubluepy/ubluepy_peripheral.c
index 6fced1695..2a826db4d 100644
--- a/ports/nrf/modules/ubluepy/ubluepy_peripheral.c
+++ b/ports/nrf/modules/ubluepy/ubluepy_peripheral.c
@@ -34,6 +34,7 @@
#include "ble_drv.h"
#include "common-hal/bleio/UUID.h"
+#include "shared-bindings/bleio/Characteristic.h"
#include "shared-bindings/bleio/UUID.h"
STATIC void ubluepy_peripheral_print(const mp_print_t *print, mp_obj_t o, mp_print_kind_t kind) {
@@ -316,26 +317,31 @@ void static disc_add_service(mp_obj_t self, ble_drv_service_data_t * p_service_d
void static disc_add_char(mp_obj_t service_in, ble_drv_char_data_t * p_desc_data) {
ubluepy_service_obj_t * p_service = MP_OBJ_TO_PTR(service_in);
- ubluepy_characteristic_obj_t * p_char = m_new_obj(ubluepy_characteristic_obj_t);
- p_char->base.type = &ubluepy_characteristic_type;
+ bleio_characteristic_obj_t * p_char = m_new_obj(bleio_characteristic_obj_t);
+ p_char->base.type = &bleio_characteristic_type;
bleio_uuid_obj_t * p_uuid = m_new_obj(bleio_uuid_obj_t);
p_uuid->base.type = &bleio_uuid_type;
- p_char->p_uuid = p_uuid;
+ p_char->uuid = p_uuid;
p_uuid->type = p_desc_data->uuid_type;
p_uuid->value[0] = p_desc_data->uuid & 0xFF;
p_uuid->value[1] = p_desc_data->uuid >> 8;
// add characteristic specific data from discovery
- p_char->props = p_desc_data->props;
+ p_char->props.broadcast = p_desc_data->props.broadcast;
+ p_char->props.indicate = p_desc_data->props.indicate;
+ p_char->props.notify = p_desc_data->props.notify;
+ p_char->props.read = p_desc_data->props.read;
+ p_char->props.write = p_desc_data->props.write;
+ p_char->props.write_wo_resp = p_desc_data->props.write_wo_resp;
p_char->handle = p_desc_data->value_handle;
// equivalent to ubluepy_service.c - service_add_characteristic()
// except the registration of the characteristic towards the bluetooth stack
p_char->service_handle = p_service->handle;
- p_char->p_service = p_service;
+ p_char->service = p_service;
mp_obj_list_append(p_service->char_list, MP_OBJ_FROM_PTR(p_char));
}
@@ -430,7 +436,7 @@ STATIC mp_obj_t peripheral_connect(mp_uint_t n_args, const mp_obj_t *pos_args, m
mp_uint_t num_chars;
mp_obj_get_array(p_service->char_list, &num_chars, &characteristics);
- ubluepy_characteristic_obj_t * p_char = (ubluepy_characteristic_obj_t *)characteristics[num_chars - 1];
+ bleio_characteristic_obj_t * p_char = (bleio_characteristic_obj_t *)characteristics[num_chars - 1];
uint16_t next_handle = p_char->handle + 1;
if ((next_handle) < p_service->end_handle) {
char_disc_retval = ble_drv_discover_characteristic(p_service,
diff --git a/ports/nrf/modules/ubluepy/ubluepy_service.c b/ports/nrf/modules/ubluepy/ubluepy_service.c
index 69d98bb7e..3834c61de 100644
--- a/ports/nrf/modules/ubluepy/ubluepy_service.c
+++ b/ports/nrf/modules/ubluepy/ubluepy_service.c
@@ -94,14 +94,13 @@ STATIC mp_obj_t ubluepy_service_make_new(const mp_obj_type_t *type, size_t n_arg
///
STATIC mp_obj_t service_add_characteristic(mp_obj_t self_in, mp_obj_t characteristic) {
ubluepy_service_obj_t * self = MP_OBJ_TO_PTR(self_in);
- ubluepy_characteristic_obj_t * p_char = MP_OBJ_TO_PTR(characteristic);
+ bleio_characteristic_obj_t * p_char = MP_OBJ_TO_PTR(characteristic);
p_char->service_handle = self->handle;
bool retval = ble_drv_characteristic_add(p_char);
-
if (retval) {
- p_char->p_service = self;
+ p_char->service = self_in;
}
mp_obj_list_append(self->char_list, characteristic);
@@ -139,10 +138,10 @@ STATIC mp_obj_t service_get_characteristic(mp_obj_t self_in, mp_obj_t uuid) {
mp_obj_get_array(self->char_list, &num_chars, &chars);
for (uint8_t i = 0; i < num_chars; i++) {
- ubluepy_characteristic_obj_t * p_char = (ubluepy_characteristic_obj_t *)chars[i];
+ bleio_characteristic_obj_t * p_char = (bleio_characteristic_obj_t *)chars[i];
- bool type_match = p_char->p_uuid->type == p_uuid->type;
- bool uuid_match = ((uint16_t)(*(uint16_t *)&p_char->p_uuid->value[0]) ==
+ bool type_match = p_char->uuid->type == p_uuid->type;
+ bool uuid_match = ((uint16_t)(*(uint16_t *)&p_char->uuid->value[0]) ==
(uint16_t)(*(uint16_t *)&p_uuid->value[0]));
if (type_match && uuid_match) {
diff --git a/shared-bindings/bleio/Characteristic.c b/shared-bindings/bleio/Characteristic.c
new file mode 100644
index 000000000..fb96be703
--- /dev/null
+++ b/shared-bindings/bleio/Characteristic.c
@@ -0,0 +1,330 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2017 Glenn Ruben Bakke
+ * Copyright (c) 2018 Artur Pacholec
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "py/objproperty.h"
+#include "py/runtime.h"
+#include "shared-bindings/bleio/Characteristic.h"
+#include "shared-bindings/bleio/UUID.h"
+
+//| .. currentmodule:: bleio
+//|
+//| :class:`Characteristic` -- BLE service characteristic
+//| =========================================================
+//|
+//| Stores information about a BLE service characteristic and allows to read
+//| and write the characteristic's value.
+//|
+
+//| .. class:: Characteristic(uuid)
+//|
+//| Create a new Characteristic object identified by the specified UUID.
+//|
+//| :param uuid: The uuid of the characteristic
+//|
+
+//| .. attribute:: broadcast
+//|
+//| A `bool` specifying if the characteristic allows broadcasting its value.
+//|
+
+//| .. attribute:: indicate
+//|
+//| A `bool` specifying if the characteristic allows indicating its value.
+//|
+
+//| .. attribute:: notify
+//|
+//| A `bool` specifying if the characteristic allows notifying its value.
+//|
+
+//| .. attribute:: read
+//|
+//| A `bool` specifying if the characteristic allows reading its value.
+//|
+
+//| .. attribute:: uuid
+//|
+//| The UUID of this characteristic. (read-only)
+//|
+
+//| .. attribute:: value
+//|
+//| The value of this characteristic. The value can be written to if the `write` property allows it.
+//| If the `read` property allows it, the value can be read. If the `notify` property is set, writting
+//| to the value will generate a BLE notification.
+//|
+
+//| .. attribute:: write
+//|
+//| A `bool` specifying if the characteristic allows writting to its value.
+//|
+
+//| .. attribute:: write_no_resp
+//|
+//| A `bool` specifying if the characteristic allows writting to its value without response.
+//|
+STATIC void bleio_characteristic_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
+ bleio_characteristic_obj_t *self = MP_OBJ_TO_PTR(self_in);
+
+ mp_printf(print, "Characteristic(uuid: 0x"HEX2_FMT""HEX2_FMT" handle: 0x" HEX2_FMT ")",
+ self->uuid->value[0], self->uuid->value[1], self->handle);
+}
+
+STATIC mp_obj_t bleio_characteristic_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, 1, 1, true);
+ bleio_characteristic_obj_t *self = m_new_obj(bleio_characteristic_obj_t);
+ self->base.type = &bleio_characteristic_type;
+ self->service = mp_const_none;
+ self->value_data = NULL;
+
+ mp_map_t kw_args;
+ mp_map_init_fixed_table(&kw_args, n_kw, pos_args + n_args);
+
+ enum { ARG_uuid };
+ static const mp_arg_t allowed_args[] = {
+ { MP_QSTR_uuid, MP_ARG_REQUIRED| MP_ARG_OBJ, {.u_obj = mp_const_none} },
+ };
+
+ mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
+ mp_arg_parse_all(n_args, pos_args, &kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
+
+ const mp_obj_t uuid = args[ARG_uuid].u_obj;
+
+ if (uuid == mp_const_none) {
+ return MP_OBJ_FROM_PTR(self);
+ }
+
+ if (MP_OBJ_IS_TYPE(uuid, &bleio_uuid_type)) {
+ self->uuid = MP_OBJ_TO_PTR(uuid);
+ } else {
+ nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
+ "Invalid UUID parameter"));
+ }
+
+ return MP_OBJ_FROM_PTR(self);
+}
+
+STATIC mp_obj_t bleio_characteristic_get_broadcast(mp_obj_t self_in) {
+ bleio_characteristic_obj_t *self = MP_OBJ_TO_PTR(self_in);
+
+ return mp_obj_new_bool(self->props.broadcast);
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_characteristic_get_broadcast_obj, bleio_characteristic_get_broadcast);
+
+STATIC mp_obj_t bleio_characteristic_set_broadcast(mp_obj_t self_in, mp_obj_t broadcast_in) {
+ bleio_characteristic_obj_t *self = MP_OBJ_TO_PTR(self_in);
+
+ self->props.broadcast = mp_obj_is_true(broadcast_in);
+
+ return mp_const_none;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_2(bleio_characteristic_set_broadcast_obj, bleio_characteristic_set_broadcast);
+
+const mp_obj_property_t bleio_characteristic_broadcast_obj = {
+ .base.type = &mp_type_property,
+ .proxy = { (mp_obj_t)&bleio_characteristic_get_broadcast_obj,
+ (mp_obj_t)&bleio_characteristic_set_broadcast_obj,
+ (mp_obj_t)&mp_const_none_obj },
+};
+
+STATIC mp_obj_t bleio_characteristic_get_indicate(mp_obj_t self_in) {
+ bleio_characteristic_obj_t *self = MP_OBJ_TO_PTR(self_in);
+
+ return mp_obj_new_bool(self->props.indicate);
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_characteristic_get_indicate_obj, bleio_characteristic_get_indicate);
+
+STATIC mp_obj_t bleio_characteristic_set_indicate(mp_obj_t self_in, mp_obj_t indicate_in) {
+ bleio_characteristic_obj_t *self = MP_OBJ_TO_PTR(self_in);
+
+ self->props.indicate = mp_obj_is_true(indicate_in);
+
+ return mp_const_none;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_2(bleio_characteristic_set_indicate_obj, bleio_characteristic_set_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)&bleio_characteristic_set_indicate_obj,
+ (mp_obj_t)&mp_const_none_obj },
+};
+
+STATIC mp_obj_t bleio_characteristic_get_notify(mp_obj_t self_in) {
+ bleio_characteristic_obj_t *self = MP_OBJ_TO_PTR(self_in);
+
+ return mp_obj_new_bool(self->props.notify);
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_characteristic_get_notify_obj, bleio_characteristic_get_notify);
+
+STATIC mp_obj_t bleio_characteristic_set_notify(mp_obj_t self_in, mp_obj_t notify_in) {
+ bleio_characteristic_obj_t *self = MP_OBJ_TO_PTR(self_in);
+
+ self->props.notify = mp_obj_is_true(notify_in);
+
+ return mp_const_none;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_2(bleio_characteristic_set_notify_obj, bleio_characteristic_set_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)&bleio_characteristic_set_notify_obj,
+ (mp_obj_t)&mp_const_none_obj },
+};
+
+STATIC mp_obj_t bleio_characteristic_get_read(mp_obj_t self_in) {
+ bleio_characteristic_obj_t *self = MP_OBJ_TO_PTR(self_in);
+
+ return mp_obj_new_bool(self->props.read);
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_characteristic_get_read_obj, bleio_characteristic_get_read);
+
+STATIC mp_obj_t bleio_characteristic_set_read(mp_obj_t self_in, mp_obj_t read_in) {
+ bleio_characteristic_obj_t *self = MP_OBJ_TO_PTR(self_in);
+
+ self->props.read = mp_obj_is_true(read_in);
+
+ return mp_const_none;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_2(bleio_characteristic_set_read_obj, bleio_characteristic_set_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)&bleio_characteristic_set_read_obj,
+ (mp_obj_t)&mp_const_none_obj },
+};
+
+STATIC mp_obj_t bleio_characteristic_get_write(mp_obj_t self_in) {
+ bleio_characteristic_obj_t *self = MP_OBJ_TO_PTR(self_in);
+
+ return mp_obj_new_bool(self->props.write);
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_characteristic_get_write_obj, bleio_characteristic_get_write);
+
+STATIC mp_obj_t bleio_characteristic_set_write(mp_obj_t self_in, mp_obj_t write_in) {
+ bleio_characteristic_obj_t *self = MP_OBJ_TO_PTR(self_in);
+
+ self->props.write = mp_obj_is_true(write_in);
+
+ return mp_const_none;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_2(bleio_characteristic_set_write_obj, bleio_characteristic_set_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)&bleio_characteristic_set_write_obj,
+ (mp_obj_t)&mp_const_none_obj },
+};
+
+STATIC mp_obj_t bleio_characteristic_get_write_wo_resp(mp_obj_t self_in) {
+ bleio_characteristic_obj_t *self = MP_OBJ_TO_PTR(self_in);
+
+ return mp_obj_new_bool(self->props.write_wo_resp);
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_characteristic_get_write_wo_resp_obj, bleio_characteristic_get_write_wo_resp);
+
+STATIC mp_obj_t bleio_characteristic_set_write_wo_resp(mp_obj_t self_in, mp_obj_t write_wo_resp_in) {
+ bleio_characteristic_obj_t *self = MP_OBJ_TO_PTR(self_in);
+
+ self->props.write_wo_resp = mp_obj_is_true(write_wo_resp_in);
+
+ return mp_const_none;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_2(bleio_characteristic_set_write_wo_resp_obj, bleio_characteristic_set_write_wo_resp);
+
+const mp_obj_property_t bleio_characteristic_write_wo_resp_obj = {
+ .base.type = &mp_type_property,
+ .proxy = { (mp_obj_t)&bleio_characteristic_get_write_wo_resp_obj,
+ (mp_obj_t)&bleio_characteristic_set_write_wo_resp_obj,
+ (mp_obj_t)&mp_const_none_obj },
+};
+
+STATIC mp_obj_t bleio_characteristic_get_uuid(mp_obj_t self_in) {
+ bleio_characteristic_obj_t *self = MP_OBJ_TO_PTR(self_in);
+
+ return MP_OBJ_FROM_PTR(self->uuid);
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_characteristic_get_uuid_obj, bleio_characteristic_get_uuid);
+
+const mp_obj_property_t bleio_characteristic_uuid_obj = {
+ .base.type = &mp_type_property,
+ .proxy = { (mp_obj_t)&bleio_characteristic_get_uuid_obj,
+ (mp_obj_t)&mp_const_none_obj,
+ (mp_obj_t)&mp_const_none_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);
+
+ return self->value_data;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_characteristic_get_value_obj, bleio_characteristic_get_value);
+
+STATIC mp_obj_t bleio_characteristic_set_value(mp_obj_t self_in, mp_obj_t value_in) {
+ bleio_characteristic_obj_t *self = MP_OBJ_TO_PTR(self_in);
+
+ mp_buffer_info_t bufinfo;
+ mp_get_buffer_raise(value_in, &bufinfo, MP_BUFFER_READ);
+
+ common_hal_bleio_characteristic_write_value(self, &bufinfo);
+
+ return mp_const_none;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_2(bleio_characteristic_set_value_obj, bleio_characteristic_set_value);
+
+const mp_obj_property_t bleio_characteristic_value_obj = {
+ .base.type = &mp_type_property,
+ .proxy = { (mp_obj_t)&bleio_characteristic_get_value_obj,
+ (mp_obj_t)&bleio_characteristic_set_value_obj,
+ (mp_obj_t)&mp_const_none_obj },
+};
+
+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_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_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_wo_resp), MP_ROM_PTR(&bleio_characteristic_write_wo_resp_obj) },
+};
+
+STATIC MP_DEFINE_CONST_DICT(bleio_characteristic_locals_dict, bleio_characteristic_locals_dict_table);
+
+const mp_obj_type_t bleio_characteristic_type = {
+ { &mp_type_type },
+ .name = MP_QSTR_Characteristic,
+ .print = bleio_characteristic_print,
+ .make_new = bleio_characteristic_make_new,
+ .locals_dict = (mp_obj_dict_t*)&bleio_characteristic_locals_dict
+};
diff --git a/shared-bindings/bleio/Characteristic.h b/shared-bindings/bleio/Characteristic.h
new file mode 100644
index 000000000..dd67170d9
--- /dev/null
+++ b/shared-bindings/bleio/Characteristic.h
@@ -0,0 +1,37 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2018 Artur Pacholec
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_CHARACTERISTIC_H
+#define MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_CHARACTERISTIC_H
+
+#include "shared-module/bleio/Characteristic.h"
+
+extern const mp_obj_type_t bleio_characteristic_type;
+
+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);
+
+#endif // MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_CHARACTERISTIC_H
diff --git a/shared-bindings/bleio/__init__.c b/shared-bindings/bleio/__init__.c
index 4ad858ac9..f96abc449 100644
--- a/shared-bindings/bleio/__init__.c
+++ b/shared-bindings/bleio/__init__.c
@@ -30,6 +30,7 @@
#include "shared-bindings/bleio/Address.h"
#include "shared-bindings/bleio/AddressType.h"
#include "shared-bindings/bleio/AdvertisementData.h"
+#include "shared-bindings/bleio/Characteristic.h"
#include "shared-bindings/bleio/Descriptor.h"
#include "shared-bindings/bleio/ScanEntry.h"
#include "shared-bindings/bleio/Scanner.h"
@@ -54,6 +55,7 @@
//| AddressType
//| AdvertisementData
//| Adapter
+//| Characteristic
//| Descriptor
//| ScanEntry
//| Scanner
@@ -71,6 +73,7 @@ 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_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_ScanEntry), MP_ROM_PTR(&bleio_scanentry_type) },
{ MP_ROM_QSTR(MP_QSTR_Scanner), MP_ROM_PTR(&bleio_scanner_type) },
diff --git a/shared-module/bleio/Characteristic.h b/shared-module/bleio/Characteristic.h
new file mode 100644
index 000000000..aa313afd3
--- /dev/null
+++ b/shared-module/bleio/Characteristic.h
@@ -0,0 +1,52 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2018 Artur Pacholec
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef MICROPY_INCLUDED_SHARED_MODULE_BLEIO_CHARACTERISTIC_H
+#define MICROPY_INCLUDED_SHARED_MODULE_BLEIO_CHARACTERISTIC_H
+
+#include "common-hal/bleio/UUID.h"
+
+typedef struct {
+ mp_obj_base_t base;
+ mp_obj_t service;
+ uint16_t service_handle;
+ bleio_uuid_obj_t *uuid;
+ mp_obj_t value_data;
+ uint16_t handle;
+ struct {
+ bool broadcast : 1;
+ bool read : 1;
+ bool write_wo_resp : 1;
+ bool write : 1;
+ bool notify : 1;
+ bool indicate : 1;
+ } props;
+ uint16_t user_desc_handle;
+ uint16_t cccd_handle;
+ uint16_t sccd_handle;
+} bleio_characteristic_obj_t;
+
+#endif // MICROPY_INCLUDED_SHARED_MODULE_BLEIO_CHARACTERISTIC_H