Skip to content

Commit 0f4b969

Browse files
committed
discovery of Nordic UART service working
1 parent ac95106 commit 0f4b969

File tree

12 files changed

+368
-229
lines changed

12 files changed

+368
-229
lines changed

devices/ble_hci/common-hal/_bleio/Attribute.c

Lines changed: 10 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -24,55 +24,26 @@
2424
* THE SOFTWARE.
2525
*/
2626

27+
#include "py/runtime.h"
28+
2729
#include "shared-bindings/_bleio/Attribute.h"
2830
#include "shared-bindings/_bleio/Characteristic.h"
2931
#include "shared-bindings/_bleio/Descriptor.h"
3032
#include "shared-bindings/_bleio/Service.h"
3133

32-
// Return the type of the attribute.
33-
ble_attribute_type_uuid bleio_attribute_type_uuid(mp_obj_t *attribute) {
34+
35+
bleio_uuid_obj_t *bleio_attribute_get_uuid(mp_obj_t *attribute) {
3436
if (MP_OBJ_IS_TYPE(attribute, &bleio_characteristic_type)) {
35-
return BLE_TYPE_CHARACTERISTIC;
37+
bleio_characteristic_obj_t *characteristic = MP_OBJ_TO_PTR(attribute);
38+
return characteristic->uuid;
3639
}
3740
if (MP_OBJ_IS_TYPE(attribute, &bleio_descriptor_type)) {
38-
return BLE_TYPE_DESCRIPTOR;
41+
bleio_descriptor_obj_t *descriptor = MP_OBJ_TO_PTR(attribute);
42+
return descriptor->uuid;
3943
}
4044
if (MP_OBJ_IS_TYPE(attribute, &bleio_service_type)) {
4145
bleio_service_obj_t *service = MP_OBJ_TO_PTR(attribute);
42-
return service->is_secondary ? BLE_TYPE_SECONDARY_SERVICE : BLE_TYPE_PRIMARY_SERVICE;
46+
return service->uuid;
4347
}
44-
return BLE_TYPE_UNKNOWN;
48+
mp_raise_RuntimeError(translate("Invalid BLE attribute"));
4549
}
46-
47-
// Convert a _bleio security mode to a ble_gap_conn_sec_mode_t setting.
48-
// void bleio_attribute_gatts_set_security_mode(ble_gap_conn_sec_mode_t *perm, bleio_attribute_security_mode_t security_mode) {
49-
// switch (security_mode) {
50-
// case SECURITY_MODE_NO_ACCESS:
51-
// BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(perm);
52-
// break;
53-
54-
// case SECURITY_MODE_OPEN:
55-
// BLE_GAP_CONN_SEC_MODE_SET_OPEN(perm);
56-
// break;
57-
58-
// case SECURITY_MODE_ENC_NO_MITM:
59-
// BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM(perm);
60-
// break;
61-
62-
// case SECURITY_MODE_ENC_WITH_MITM:
63-
// BLE_GAP_CONN_SEC_MODE_SET_ENC_WITH_MITM(perm);
64-
// break;
65-
66-
// case SECURITY_MODE_LESC_ENC_WITH_MITM:
67-
// BLE_GAP_CONN_SEC_MODE_SET_LESC_ENC_WITH_MITM(perm);
68-
// break;
69-
70-
// case SECURITY_MODE_SIGNED_NO_MITM:
71-
// BLE_GAP_CONN_SEC_MODE_SET_SIGNED_NO_MITM(perm);
72-
// break;
73-
74-
// case SECURITY_MODE_SIGNED_WITH_MITM:
75-
// BLE_GAP_CONN_SEC_MODE_SET_SIGNED_WITH_MITM(perm);
76-
// break;
77-
// }
78-
// }

devices/ble_hci/common-hal/_bleio/Attribute.h

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,23 @@
2828
#define MICROPY_INCLUDED_BLE_HCI_COMMON_HAL_ATTRIBUTE_H
2929

3030
#include "shared-module/_bleio/Attribute.h"
31+
#include "shared-bindings/_bleio/UUID.h"
3132

3233
// Types returned by attribute table lookups. These are UUIDs.
3334
typedef enum {
34-
BLE_TYPE_UNKNOWN = 0x0000,
35-
BLE_TYPE_PRIMARY_SERVICE = 0x2800,
36-
BLE_TYPE_SECONDARY_SERVICE = 0x2801,
37-
BLE_TYPE_CHARACTERISTIC = 0x2803,
38-
BLE_TYPE_DESCRIPTOR = 0x2900
35+
BLE_TYPE_UNKNOWN = 0x0000,
36+
BLE_TYPE_SERVICE_PRIMARY = 0x2800,
37+
BLE_TYPE_SERVICE_SECONDARY = 0x2801,
38+
BLE_TYPE_SERVICE_INCLUDE = 0x2802, // not yet implemented by us
39+
BLE_TYPE_CHARACTERISTIC = 0x2803,
40+
BLE_TYPE_CHAR_EXTENDED_PROPS = 0x2900, // not yet implemented by us
41+
BLE_TYPE_CHAR_USER_DESC = 0x2901, // not yet implemented by us
42+
BLE_TYPE_CCCD = 0x2902,
43+
BLE_TYPE_SCCD = 0x2903, // not yet implemented by us
44+
BLE_TYPE_CHAR_PRESENTATION_FMT = 0x2904, // not yet implemented by us
45+
BLE_TYPE_CHAR_AGGREGATE_FMT = 0x2905, // not yet implemented by us
3946
} ble_attribute_type_uuid;
4047

41-
// typedef struct
42-
// {
43-
// uint8_t sm : 4; /**< Security Mode (1 or 2), 0 for no permissions at all. */
44-
// uint8_t lv : 4; /**< Level (1, 2, 3 or 4), 0 for no permissions at all. */
45-
46-
// } ble_gap_conn_sec_mode_t;
47-
48-
ble_attribute_type_uuid bleio_attribute_type_uuid(mp_obj_t *attribute);
48+
bleio_uuid_obj_t *bleio_attribute_get_uuid(mp_obj_t *attribute);
4949

5050
#endif // MICROPY_INCLUDED_BLE_HCI_COMMON_HAL_ATTRIBUTE_H

devices/ble_hci/common-hal/_bleio/Characteristic.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ void common_hal_bleio_characteristic_add_descriptor(bleio_characteristic_obj_t *
149149
}
150150

151151
descriptor->handle = bleio_adapter_add_attribute(&common_hal_bleio_adapter_obj, MP_OBJ_TO_PTR(descriptor));
152-
// Include this desriptor in the service handles range.
152+
// Include this descriptor in the service handle's range.
153153
self->service->end_handle = descriptor->handle;
154154

155155
// Link together all the descriptors for this characteristic.

devices/ble_hci/common-hal/_bleio/Service.c

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -99,20 +99,30 @@ void common_hal_bleio_service_add_characteristic(bleio_service_obj_t *self,
9999
self->end_handle = characteristic->handle;
100100

101101
if (characteristic->props & (CHAR_PROP_NOTIFY | CHAR_PROP_INDICATE)) {
102-
// We need a CCCD.
102+
// We need a CCCD if this characteristic is doing notify or indicate.
103103
bleio_descriptor_obj_t *cccd = m_new_obj(bleio_descriptor_obj_t);
104104
cccd->base.type = &bleio_descriptor_type;
105-
cccd->read_perm = SECURITY_MODE_OPEN;
106-
// Make CCCD write permission match characteristic read permission.
107-
cccd->write_perm = characteristic->read_perm;
108-
109-
const uint16_t cccd_handle = bleio_adapter_add_attribute(
110-
&common_hal_bleio_adapter_obj, MP_OBJ_TO_PTR(cccd));
111-
cccd->handle = cccd_handle;
112-
characteristic->cccd_handle = cccd_handle;
113-
common_hal_bleio_characteristic_add_descriptor(characteristic, cccd);
114105

115-
self->end_handle = cccd_handle;
106+
uint16_t zero = 0;
107+
mp_buffer_info_t zero_cccd = {
108+
.buf = &zero,
109+
.len = sizeof(zero),
110+
};
111+
112+
common_hal_bleio_descriptor_construct(
113+
cccd,
114+
characteristic,
115+
&cccd_uuid, // 0x2902
116+
SECURITY_MODE_OPEN, // CCCD read perm
117+
characteristic->read_perm, // Make CCCD write perm match characteristic read perm.
118+
2, // 2 bytes
119+
true, // fixed length
120+
&zero_cccd // Initial value is 0.
121+
);
122+
123+
// Adds CCCD to attribute table, and also extends self->end_handle to include the CCCD.
124+
common_hal_bleio_characteristic_add_descriptor(characteristic, cccd);
125+
characteristic->cccd_handle = cccd->handle;
116126
}
117127

118128
// #if CIRCUITPY_VERBOSE_BLE

devices/ble_hci/common-hal/_bleio/UUID.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
// If uuid128 is NULL, this is a Bluetooth SIG 16-bit UUID.
3737
// If uuid128 is not NULL, it's a 128-bit (16-byte) UUID, with bytes 12 and 13 zero'd out, where
3838
// the 16-bit part goes. Those 16 bits are passed in uuid16.
39-
void common_hal_bleio_uuid_construct(bleio_uuid_obj_t *self, uint32_t uuid16, const uint8_t uuid128[16]) {
39+
void common_hal_bleio_uuid_construct(bleio_uuid_obj_t *self, mp_int_t uuid16, const uint8_t uuid128[16]) {
4040
self->size = uuid128 == NULL ? 16 : 128;
4141
self->uuid16 = uuid16;
4242
if (uuid128) {

devices/ble_hci/common-hal/_bleio/__init__.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838
#include "shared-bindings/_bleio/UUID.h"
3939
#include "supervisor/shared/bluetooth.h"
4040

41+
// UUID shared by all cccd's.
42+
bleio_uuid_obj_t cccd_uuid;
43+
4144
bool vm_used_ble;
4245

4346
void check_hci_error(hci_result_t result) {
@@ -112,6 +115,10 @@ void check_hci_error(hci_result_t result) {
112115

113116
// Turn off BLE on a reset or reload.
114117
void bleio_reset() {
118+
// Create a UUID object for all CCCD's.
119+
cccd_uuid.base.type = &bleio_uuid_type;
120+
common_hal_bleio_uuid_construct(&cccd_uuid, BLE_TYPE_CCCD, NULL);
121+
115122
bleio_hci_reset();
116123

117124
if (!common_hal_bleio_adapter_get_enabled(&common_hal_bleio_adapter_obj)) {
@@ -123,6 +130,7 @@ void bleio_reset() {
123130
return;
124131
}
125132
common_hal_bleio_adapter_set_enabled(&common_hal_bleio_adapter_obj, false);
133+
126134
//FIX bonding_reset();
127135
supervisor_start_bluetooth();
128136
}

devices/ble_hci/common-hal/_bleio/__init__.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
#include <stdbool.h>
3131

32+
#include "shared-bindings/_bleio/UUID.h"
33+
3234
#include "hci.h"
3335

3436
void bleio_background(void);
@@ -59,4 +61,7 @@ void check_sec_status(uint8_t sec_status);
5961
// Track if the user code modified the BLE state to know if we need to undo it on reload.
6062
extern bool vm_used_ble;
6163

64+
// UUID shared by all CCCD's.
65+
extern bleio_uuid_obj_t cccd_uuid;
66+
6267
#endif // MICROPY_INCLUDED_BLE_HCI_COMMON_HAL_INIT_H

0 commit comments

Comments
 (0)