Skip to content

Commit 76cbd0d

Browse files
committed
Switch CircuitPython serial off Nordic UUIDs
This will un-break examples that got confused by the presence of two Nordic UART Services. It also adds a version characteristic that gives the CircuitPython build tag back. Fixes #5252
1 parent 8fbb3e6 commit 76cbd0d

File tree

2 files changed

+52
-27
lines changed

2 files changed

+52
-27
lines changed

supervisor/shared/bluetooth/bluetooth.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ const uint8_t private_advertising_data[] = { 0x02, 0x01, 0x06, // 0-2 Flags
7979
uint8_t circuitpython_scan_response_data[] = {
8080
0x0a, 0x09, 0x43, 0x49, 0x52, 0x50, 0x59, 0x00, 0x00, 0x00, 0x00,
8181
#if CIRCUITPY_SERIAL_BLE
82-
0x11, 0x06, 0x9e, 0xca, 0xdc, 0x24, 0x0e, 0xe5, 0xa9, 0xe0, 0x93, 0xf3, 0xa3, 0xb5, 0x01, 0x00, 0x40, 0x6e,
82+
0x11, 0x06, 0x6e, 0x68, 0x74, 0x79, 0x50, 0x74, 0x69, 0x75, 0x63, 0x72, 0x69, 0x43, 0x01, 0x00, 0xaf, 0xad
8383
#endif
8484
};
8585

supervisor/shared/bluetooth/serial.c

Lines changed: 51 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include <string.h>
2828

29+
#include "genhdr/mpversion.h"
2930
#include "shared-bindings/_bleio/__init__.h"
3031
#include "shared-bindings/_bleio/Adapter.h"
3132
#include "shared-bindings/_bleio/Characteristic.h"
@@ -39,15 +40,17 @@
3940

4041
#include "py/mpstate.h"
4142

42-
STATIC bleio_service_obj_t supervisor_ble_serial_service;
43-
STATIC bleio_uuid_obj_t supervisor_ble_serial_service_uuid;
44-
STATIC bleio_characteristic_obj_t supervisor_ble_rx_characteristic;
45-
STATIC bleio_uuid_obj_t supervisor_ble_rx_uuid;
46-
STATIC bleio_characteristic_obj_t supervisor_ble_tx_characteristic;
47-
STATIC bleio_uuid_obj_t supervisor_ble_tx_uuid;
43+
STATIC bleio_service_obj_t supervisor_ble_circuitpython_service;
44+
STATIC bleio_uuid_obj_t supervisor_ble_circuitpython_service_uuid;
45+
STATIC bleio_characteristic_obj_t supervisor_ble_circuitpython_rx_characteristic;
46+
STATIC bleio_uuid_obj_t supervisor_ble_circuitpython_rx_uuid;
47+
STATIC bleio_characteristic_obj_t supervisor_ble_circuitpython_tx_characteristic;
48+
STATIC bleio_uuid_obj_t supervisor_ble_circuitpython_tx_uuid;
49+
STATIC bleio_characteristic_obj_t supervisor_ble_circuitpython_version_characteristic;
50+
STATIC bleio_uuid_obj_t supervisor_ble_circuitpython_version_uuid;
4851

49-
// This is the base UUID for the nordic uart service.
50-
const uint8_t nordic_uart_base_uuid[16] = {0x9e, 0xca, 0xdc, 0x24, 0x0e, 0xe5, 0xa9, 0xe0, 0x93, 0xf3, 0xa3, 0xb5, 0x00, 0x00, 0x40, 0x6e };
52+
// This is the base UUID for the CircuitPython service.
53+
const uint8_t circuitpython_base_uuid[16] = {0x6e, 0x68, 0x74, 0x79, 0x50, 0x74, 0x69, 0x75, 0x63, 0x72, 0x69, 0x43, 0x00, 0x00, 0xaf, 0xad };
5154

5255
STATIC mp_obj_list_t characteristic_list;
5356
STATIC mp_obj_t characteristic_list_items[2];
@@ -64,8 +67,8 @@ STATIC bleio_characteristic_buffer_obj_t _rx_buffer;
6467
STATIC bool _enabled;
6568

6669
void supervisor_start_bluetooth_serial(void) {
67-
supervisor_ble_serial_service_uuid.base.type = &bleio_uuid_type;
68-
common_hal_bleio_uuid_construct(&supervisor_ble_serial_service_uuid, 0x0001, nordic_uart_base_uuid);
70+
supervisor_ble_circuitpython_service_uuid.base.type = &bleio_uuid_type;
71+
common_hal_bleio_uuid_construct(&supervisor_ble_circuitpython_service_uuid, 0x0001, circuitpython_base_uuid);
6972

7073
// We know we'll only be N characteristics so we can statically allocate it.
7174
characteristic_list.base.type = &mp_type_list;
@@ -74,50 +77,72 @@ void supervisor_start_bluetooth_serial(void) {
7477
characteristic_list.items = characteristic_list_items;
7578
mp_seq_clear(characteristic_list.items, 0, characteristic_list.alloc, sizeof(*characteristic_list.items));
7679

77-
supervisor_ble_serial_service.base.type = &bleio_service_type;
78-
_common_hal_bleio_service_construct(&supervisor_ble_serial_service, &supervisor_ble_serial_service_uuid, false /* is secondary */, &characteristic_list);
80+
supervisor_ble_circuitpython_service.base.type = &bleio_service_type;
81+
_common_hal_bleio_service_construct(&supervisor_ble_circuitpython_service, &supervisor_ble_circuitpython_service_uuid, false /* is secondary */, &characteristic_list);
7982

8083
// RX
81-
supervisor_ble_rx_uuid.base.type = &bleio_uuid_type;
82-
common_hal_bleio_uuid_construct(&supervisor_ble_rx_uuid, 0x0002, nordic_uart_base_uuid);
83-
common_hal_bleio_characteristic_construct(&supervisor_ble_rx_characteristic,
84-
&supervisor_ble_serial_service,
84+
supervisor_ble_circuitpython_rx_uuid.base.type = &bleio_uuid_type;
85+
common_hal_bleio_uuid_construct(&supervisor_ble_circuitpython_rx_uuid, 0x0002, circuitpython_base_uuid);
86+
common_hal_bleio_characteristic_construct(&supervisor_ble_circuitpython_rx_characteristic,
87+
&supervisor_ble_circuitpython_service,
8588
0, // handle (for remote only)
86-
&supervisor_ble_rx_uuid,
89+
&supervisor_ble_circuitpython_rx_uuid,
8790
CHAR_PROP_WRITE | CHAR_PROP_WRITE_NO_RESPONSE,
8891
SECURITY_MODE_NO_ACCESS,
8992
SECURITY_MODE_ENC_NO_MITM,
9093
BLE_GATTS_VAR_ATTR_LEN_MAX, // max length
9194
false, // fixed length
9295
NULL, // no initial value
93-
"CircuitPython Serial");
96+
NULL);
9497

9598
// TX
96-
supervisor_ble_tx_uuid.base.type = &bleio_uuid_type;
97-
common_hal_bleio_uuid_construct(&supervisor_ble_tx_uuid, 0x0003, nordic_uart_base_uuid);
98-
common_hal_bleio_characteristic_construct(&supervisor_ble_tx_characteristic,
99-
&supervisor_ble_serial_service,
99+
supervisor_ble_circuitpython_tx_uuid.base.type = &bleio_uuid_type;
100+
common_hal_bleio_uuid_construct(&supervisor_ble_circuitpython_tx_uuid, 0x0003, circuitpython_base_uuid);
101+
common_hal_bleio_characteristic_construct(&supervisor_ble_circuitpython_tx_characteristic,
102+
&supervisor_ble_circuitpython_service,
100103
0, // handle (for remote only)
101-
&supervisor_ble_tx_uuid,
104+
&supervisor_ble_circuitpython_tx_uuid,
102105
CHAR_PROP_NOTIFY,
103106
SECURITY_MODE_ENC_NO_MITM,
104107
SECURITY_MODE_NO_ACCESS,
105108
BLE_GATTS_VAR_ATTR_LEN_MAX, // max length
106109
false, // fixed length
107110
NULL, // no initial value
108-
"CircuitPython Serial");
111+
NULL);
112+
113+
// Version number
114+
const char *version = MICROPY_GIT_TAG;
115+
mp_buffer_info_t bufinfo;
116+
bufinfo.buf = (uint8_t *)version;
117+
bufinfo.len = strlen(version);
118+
119+
supervisor_ble_circuitpython_version_uuid.base.type = &bleio_uuid_type;
120+
common_hal_bleio_uuid_construct(&supervisor_ble_circuitpython_version_uuid, 0x0100, circuitpython_base_uuid);
121+
common_hal_bleio_characteristic_construct(&supervisor_ble_circuitpython_version_characteristic,
122+
&supervisor_ble_circuitpython_service,
123+
0, // handle (for remote only)
124+
&supervisor_ble_circuitpython_version_uuid,
125+
CHAR_PROP_READ,
126+
SECURITY_MODE_OPEN,
127+
SECURITY_MODE_NO_ACCESS,
128+
bufinfo.len, // max length
129+
true, // fixed length
130+
NULL, // no initial value
131+
NULL); // no description
132+
133+
common_hal_bleio_characteristic_set_value(&supervisor_ble_circuitpython_version_characteristic, &bufinfo);
109134

110135
// Use a PacketBuffer to transmit so that we glom characters to transmit
111136
// together and save BLE overhead.
112137
_common_hal_bleio_packet_buffer_construct(
113-
&_tx_packet_buffer, &supervisor_ble_tx_characteristic,
138+
&_tx_packet_buffer, &supervisor_ble_circuitpython_tx_characteristic,
114139
NULL, 0,
115140
_outgoing1, _outgoing2, BLE_GATTS_VAR_ATTR_LEN_MAX,
116141
&tx_static_handler_entry);
117142

118143
// Use a CharacteristicBuffer for rx so we can read a single character at a time.
119144
_common_hal_bleio_characteristic_buffer_construct(&_rx_buffer,
120-
&supervisor_ble_rx_characteristic,
145+
&supervisor_ble_circuitpython_rx_characteristic,
121146
0.1f,
122147
(uint8_t *)_incoming, sizeof(_incoming) * sizeof(uint32_t),
123148
&rx_static_handler_entry);

0 commit comments

Comments
 (0)