Skip to content

Commit 7ce3776

Browse files
committed
WIP: rework of Characteristic properties; enhance Descriptor; not tested
1 parent ee518b9 commit 7ce3776

26 files changed

Lines changed: 518 additions & 327 deletions
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2019 Dan Halbert for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "shared-bindings/bleio/Attribute.h"
28+
29+
// Convert a bleio security mode to a ble_gap_conn_sec_mode_t setting.
30+
void bleio_attribute_gatts_set_security_mode(ble_gap_conn_sec_mode_t *perm, bleio_attribute_security_mode_t security_mode) {
31+
switch (security_mode) {
32+
case SEC_MODE_NO_ACCESS:
33+
BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(perm);
34+
break;
35+
36+
case SEC_MODE_OPEN:
37+
BLE_GAP_CONN_SEC_MODE_SET_OPEN(perm);
38+
break;
39+
40+
case SEC_MODE_ENC_NO_MITM:
41+
BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM(perm);
42+
break;
43+
44+
case SEC_MODE_ENC_WITH_MITM:
45+
BLE_GAP_CONN_SEC_MODE_SET_ENC_WITH_MITM(perm);
46+
break;
47+
48+
case SEC_MODE_LESC_ENC_WITH_MITM:
49+
BLE_GAP_CONN_SEC_MODE_SET_LESC_ENC_WITH_MITM(perm);
50+
break;
51+
52+
case SEC_MODE_SIGNED_NO_MITM:
53+
BLE_GAP_CONN_SEC_MODE_SET_SIGNED_NO_MITM(perm);
54+
break;
55+
56+
case SEC_MODE_SIGNED_WITH_MITM:
57+
BLE_GAP_CONN_SEC_MODE_SET_SIGNED_WITH_MITM(perm);
58+
break;
59+
}
60+
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
#ifndef MICROPY_INCLUDED_SHARED_MODULE_BLEIO_INIT_H
28-
#define MICROPY_INCLUDED_SHARED_MODULE_BLEIO_INIT_H
27+
#ifndef MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_ATTRIBUTE_H
28+
#define MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_ATTRIBUTE_H
2929

30-
extern void bleio_reset(void);
30+
// Nothing yet.
3131

32-
#endif // MICROPY_INCLUDED_SHARED_MODULE_BLEIO_INIT_H
32+
#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_ATTRIBUTE_H

ports/nrf/common-hal/bleio/Central.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
#include <stdbool.h>
3232

3333
#include "py/objlist.h"
34-
#include "shared-module/bleio/__init__.h"
3534
#include "shared-module/bleio/Address.h"
3635

3736
typedef struct {

ports/nrf/common-hal/bleio/Characteristic.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ STATIC void gattc_write(bleio_characteristic_obj_t *characteristic, mp_buffer_in
164164
check_connected(conn_handle);
165165

166166
ble_gattc_write_params_t write_params = {
167-
.write_op = characteristic->props.write_no_response ? BLE_GATT_OP_WRITE_CMD : BLE_GATT_OP_WRITE_REQ,
167+
.write_op = (characteristic->props & CHAR_PROP_WRITE_NO_RESPONSE)
168+
? BLE_GATT_OP_WRITE_CMD: BLE_GATT_OP_WRITE_REQ,
168169
.handle = characteristic->handle,
169170
.p_value = bufinfo->buf,
170171
.len = bufinfo->len,
@@ -211,12 +212,14 @@ STATIC void characteristic_on_ble_evt(ble_evt_t *ble_evt, void *param) {
211212

212213
}
213214

214-
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) {
215+
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) {
215216
self->service = mp_const_none;
216217
self->uuid = uuid;
217218
self->value_data = mp_const_none;
218219
self->props = props;
219-
self->descriptor_list = descriptor_list;
220+
self->security_mode = security_mode;
221+
self->descriptor_list = mp_obj_new_list_from_iter(descriptors);
222+
220223
self->handle = BLE_GATT_HANDLE_INVALID;
221224

222225
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
238241

239242
void common_hal_bleio_characteristic_set_value(bleio_characteristic_obj_t *self, mp_buffer_info_t *bufinfo) {
240243
if (common_hal_bleio_service_get_is_remote(self->service)) {
241-
gattc_write(self, bufinfo);
244+
gattc_write(self, bufinfo);
242245
} else {
243246
bool sent = false;
244247
uint16_t cccd = 0;
245248

246-
if (self->props.notify || self->props.indicate) {
247-
cccd = get_cccd(self);
249+
const bool notify = self->props & CHAR_PROP_NOTIFY;
250+
const bool indicate = self->props & CHAR_PROP_INDICATE;
251+
if (notify | indicate) {
252+
cccd = get_cccd(self);
248253
}
249254

250255
// It's possible that both notify and indicate are set.
251-
if (self->props.notify && (cccd & BLE_GATT_HVX_NOTIFICATION)) {
256+
if (notify && (cccd & BLE_GATT_HVX_NOTIFICATION)) {
252257
gatts_notify_indicate(self, bufinfo, BLE_GATT_HVX_NOTIFICATION);
253258
sent = true;
254259
}
255-
if (self->props.indicate && (cccd & BLE_GATT_HVX_INDICATION)) {
260+
if (indicate && (cccd & BLE_GATT_HVX_INDICATION)) {
256261
gatts_notify_indicate(self, bufinfo, BLE_GATT_HVX_INDICATION);
257262
sent = true;
258263
}

ports/nrf/common-hal/bleio/Characteristic.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,19 @@
2828
#ifndef MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_CHARACTERISTIC_H
2929
#define MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_CHARACTERISTIC_H
3030

31+
#include "shared-bindings/bleio/Attribute.h"
32+
#include "shared-module/bleio/Characteristic.h"
3133
#include "common-hal/bleio/Service.h"
3234
#include "common-hal/bleio/UUID.h"
33-
#include "shared-module/bleio/Characteristic.h"
3435

3536
typedef struct {
3637
mp_obj_base_t base;
3738
bleio_service_obj_t *service;
3839
bleio_uuid_obj_t *uuid;
39-
volatile mp_obj_t value_data;
40+
mp_obj_t value_data;
4041
uint16_t handle;
4142
bleio_characteristic_properties_t props;
43+
bleio_attribute_security_mode_t security_mode;
4244
mp_obj_list_t *descriptor_list;
4345
uint16_t user_desc_handle;
4446
uint16_t cccd_handle;

ports/nrf/common-hal/bleio/Descriptor.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* The MIT License (MIT)
55
*
6-
* Copyright (c) 2018 Dan Halbert for Adafruit Industries
6+
* Copyright (c) 2019 Dan Halbert for Adafruit Industries
77
* Copyright (c) 2018 Artur Pacholec
88
* Copyright (c) 2016 Glenn Ruben Bakke
99
*
@@ -33,10 +33,6 @@ void common_hal_bleio_descriptor_construct(bleio_descriptor_obj_t *self, bleio_u
3333
self->uuid = uuid;
3434
}
3535

36-
mp_int_t common_hal_bleio_descriptor_get_handle(bleio_descriptor_obj_t *self) {
37-
return self->handle;
38-
}
39-
4036
bleio_uuid_obj_t *common_hal_bleio_descriptor_get_uuid(bleio_descriptor_obj_t *self) {
4137
return self->uuid;
4238
}

ports/nrf/common-hal/bleio/Descriptor.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,17 @@
3030
#define MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_DESCRIPTOR_H
3131

3232
#include "py/obj.h"
33-
#include "common-hal/bleio/Characteristic.h"
33+
34+
#include "shared-bindings/bleio/Characteristic.h"
3435
#include "common-hal/bleio/UUID.h"
3536

3637
typedef struct {
3738
mp_obj_base_t base;
38-
uint16_t handle;
3939
bleio_characteristic_obj_t *characteristic;
4040
bleio_uuid_obj_t *uuid;
41+
mp_obj_t value_data;
42+
uint16_t handle;
43+
bleio_attribute_security_mode_t security_mode;
4144
} bleio_descriptor_obj_t;
4245

4346
#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_DESCRIPTOR_H

ports/nrf/common-hal/bleio/Peripheral.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,10 @@ STATIC void peripheral_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
138138
// Pairing process completed
139139
ble_gap_evt_auth_status_t* status = &ble_evt->evt.gap_evt.params.auth_status;
140140
if (BLE_GAP_SEC_STATUS_SUCCESS == status->auth_status) {
141-
mp_printf(&mp_plat_print, "Pairing succeeded, status: 0x%04x\n", status->auth_status);
141+
// mp_printf(&mp_plat_print, "Pairing succeeded, status: 0x%04x\n", status->auth_status);
142142
self->pair_status = PAIR_PAIRED;
143143
} else {
144-
mp_printf(&mp_plat_print, "Pairing failed, status: 0x%04x\n", status->auth_status);
144+
// mp_printf(&mp_plat_print, "Pairing failed, status: 0x%04x\n", status->auth_status);
145145
self->pair_status = PAIR_NOT_PAIRED;
146146
}
147147
break;

ports/nrf/common-hal/bleio/Peripheral.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
#include "py/obj.h"
3636
#include "py/objlist.h"
3737

38-
#include "shared-module/bleio/__init__.h"
3938
#include "shared-module/bleio/Address.h"
4039

4140
typedef enum {

ports/nrf/common-hal/bleio/Service.c

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
#include "ble.h"
3030
#include "py/runtime.h"
3131
#include "common-hal/bleio/__init__.h"
32-
#include "common-hal/bleio/Characteristic.h"
3332
#include "shared-bindings/bleio/Characteristic.h"
33+
#include "shared-bindings/bleio/Descriptor.h"
3434
#include "shared-bindings/bleio/Service.h"
3535
#include "shared-bindings/bleio/Adapter.h"
3636

@@ -74,12 +74,12 @@ void common_hal_bleio_service_add_all_characteristics(bleio_service_obj_t *self)
7474
MP_OBJ_TO_PTR(self->characteristic_list->items[characteristic_idx]);
7575

7676
ble_gatts_char_md_t char_md = {
77-
.char_props.broadcast = characteristic->props.broadcast,
78-
.char_props.read = characteristic->props.read,
79-
.char_props.write_wo_resp = characteristic->props.write_no_response,
80-
.char_props.write = characteristic->props.write,
81-
.char_props.notify = characteristic->props.notify,
82-
.char_props.indicate = characteristic->props.indicate,
77+
.char_props.broadcast = (bool) characteristic->props & CHAR_PROP_BROADCAST,
78+
.char_props.read = (bool) characteristic->props & CHAR_PROP_READ,
79+
.char_props.write_wo_resp = (bool) characteristic->props & CHAR_PROP_WRITE_NO_RESPONSE,
80+
.char_props.write = (bool) characteristic->props & CHAR_PROP_WRITE,
81+
.char_props.notify = (bool) characteristic->props & CHAR_PROP_NOTIFY,
82+
.char_props.indicate = (bool) characteristic->props & CHAR_PROP_INDICATE,
8383
};
8484

8585
ble_gatts_attr_md_t cccd_md = {
@@ -93,28 +93,28 @@ void common_hal_bleio_service_add_all_characteristics(bleio_service_obj_t *self)
9393
char_md.p_cccd_md = &cccd_md;
9494
}
9595

96-
ble_uuid_t uuid;
97-
bleio_uuid_convert_to_nrf_ble_uuid(characteristic->uuid, &uuid);
96+
ble_uuid_t char_uuid;
97+
bleio_uuid_convert_to_nrf_ble_uuid(characteristic->uuid, &char_uuid);
9898

99-
ble_gatts_attr_md_t attr_md = {
99+
ble_gatts_attr_md_t char_attr_md = {
100100
.vloc = BLE_GATTS_VLOC_STACK,
101101
.vlen = 1,
102102
};
103103

104-
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm);
105-
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm);
104+
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&char_attr_md.read_perm);
105+
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&char_attr_md.write_perm);
106106

107-
ble_gatts_attr_t attr_char_value = {
108-
.p_uuid = &uuid,
109-
.p_attr_md = &attr_md,
107+
ble_gatts_attr_t char_attr = {
108+
.p_uuid = &char_uuid,
109+
.p_attr_md = &char_attr_md,
110110
.init_len = sizeof(uint8_t),
111111
.max_len = GATT_MAX_DATA_LENGTH,
112112
};
113113

114-
ble_gatts_char_handles_t handles;
114+
ble_gatts_char_handles_t char_handles;
115115

116116
uint32_t err_code;
117-
err_code = sd_ble_gatts_characteristic_add(self->handle, &char_md, &attr_char_value, &handles);
117+
err_code = sd_ble_gatts_characteristic_add(self->handle, &char_md, &char_attr, &char_handles);
118118
if (err_code != NRF_SUCCESS) {
119119
mp_raise_OSError_msg_varg(translate("Failed to add characteristic, err 0x%04x"), err_code);
120120
}
@@ -123,9 +123,41 @@ void common_hal_bleio_service_add_all_characteristics(bleio_service_obj_t *self)
123123
mp_raise_ValueError(translate("Characteristic already in use by another Service."));
124124
}
125125

126-
characteristic->user_desc_handle = handles.user_desc_handle;
127-
characteristic->cccd_handle = handles.cccd_handle;
128-
characteristic->sccd_handle = handles.sccd_handle;
129-
characteristic->handle = handles.value_handle;
126+
characteristic->user_desc_handle = char_handles.user_desc_handle;
127+
characteristic->cccd_handle = char_handles.cccd_handle;
128+
characteristic->sccd_handle = char_handles.sccd_handle;
129+
characteristic->handle = char_handles.value_handle;
130+
131+
// Add the descriptors for this characteristic.
132+
for (size_t descriptor_idx = 0; descriptor_idx < characteristic->descriptor_list->len; ++descriptor_idx) {
133+
bleio_descriptor_obj_t *descriptor =
134+
MP_OBJ_TO_PTR(characteristic->descriptor_list->items[descriptor_idx]);
135+
136+
ble_uuid_t desc_uuid;
137+
bleio_uuid_convert_to_nrf_ble_uuid(descriptor->uuid, &desc_uuid);
138+
139+
ble_gatts_attr_md_t desc_attr_md = {
140+
// Data passed is not in a permanent location and should be copied.
141+
.vloc = BLE_GATTS_VLOC_STACK,
142+
.vlen = 1,
143+
};
144+
145+
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&desc_attr_md.read_perm);
146+
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&desc_attr_md.write_perm);
147+
148+
mp_buffer_info_t bufinfo;
149+
mp_get_buffer_raise(descriptor->value_data, &bufinfo, MP_BUFFER_READ);
150+
151+
ble_gatts_attr_t desc_attr = {
152+
.p_uuid = &desc_uuid,
153+
.p_attr_md = &desc_attr_md,
154+
.init_len = bufinfo.len,
155+
.p_value = bufinfo.buf,
156+
.init_offs = 0,
157+
.max_len = GATT_MAX_DATA_LENGTH,
158+
};
159+
160+
err_code = sd_ble_gatts_descriptor_add(characteristic->handle, &desc_attr, &descriptor->handle);
161+
}
130162
}
131163
}

0 commit comments

Comments
 (0)