Skip to content

Commit 77c1769

Browse files
authored
Merge pull request #10862 from tannewt/zephyr_bleio_central
Implement Zephyr bleio connect and disconnect
2 parents c3f1911 + fdab097 commit 77c1769

File tree

34 files changed

+779
-193
lines changed

34 files changed

+779
-193
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ void common_hal_bleio_characteristic_set_cccd(bleio_characteristic_obj_t *self,
180180
}
181181

182182
const uint16_t conn_handle = bleio_connection_get_conn_handle(self->service->connection);
183-
common_hal_bleio_check_connected(conn_handle);
183+
bleio_check_connected(conn_handle);
184184

185185
uint16_t cccd_value =
186186
(notify ? CCCD_NOTIFY : 0) |

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ typedef struct {
6161
uint8_t disconnect_reason;
6262
} bleio_connection_obj_t;
6363

64+
void bleio_check_connected(uint16_t conn_handle);
6465
uint16_t bleio_connection_get_conn_handle(bleio_connection_obj_t *self);
6566
mp_obj_t bleio_connection_new_from_internal(bleio_connection_internal_t *connection);
6667
bleio_connection_internal_t *bleio_conn_handle_to_connection(uint16_t conn_handle);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ bleio_adapter_obj_t *common_hal_bleio_allocate_adapter_or_raise(void) {
8484
return &common_hal_bleio_adapter_obj;
8585
}
8686

87-
void common_hal_bleio_check_connected(uint16_t conn_handle) {
87+
void bleio_check_connected(uint16_t conn_handle) {
8888
if (conn_handle == BLE_CONN_HANDLE_INVALID) {
8989
mp_raise_ConnectionError(MP_ERROR_TEXT("Not connected"));
9090
}

locale/circuitpython.pot

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ msgstr ""
476476
msgid "AP could not be started"
477477
msgstr ""
478478

479-
#: shared-bindings/_bleio/Address.c shared-bindings/ipaddress/IPv4Address.c
479+
#: shared-bindings/ipaddress/IPv4Address.c
480480
#, c-format
481481
msgid "Address must be %d bytes long"
482482
msgstr ""
@@ -673,11 +673,6 @@ msgstr ""
673673
msgid "Brightness not adjustable"
674674
msgstr ""
675675

676-
#: shared-bindings/_bleio/UUID.c
677-
#, c-format
678-
msgid "Buffer + offset too small %d %d %d"
679-
msgstr ""
680-
681676
#: ports/raspberrypi/bindings/rp2pio/StateMachine.c
682677
msgid "Buffer elements must be 4 bytes long or less"
683678
msgstr ""
@@ -720,10 +715,6 @@ msgstr ""
720715
msgid "Bus pin %d is already in use"
721716
msgstr ""
722717

723-
#: shared-bindings/_bleio/UUID.c
724-
msgid "Byte buffer must be 16 bytes."
725-
msgstr ""
726-
727718
#: shared-bindings/aesio/aes.c
728719
msgid "CBC blocks must be multiples of 16 bytes"
729720
msgstr ""
@@ -1029,12 +1020,18 @@ msgstr ""
10291020
msgid "Failed to buffer the sample"
10301021
msgstr ""
10311022

1023+
#: ports/zephyr-cp/common-hal/_bleio/Adapter.c
1024+
msgid "Failed to connect"
1025+
msgstr ""
1026+
10321027
#: ports/espressif/common-hal/_bleio/Adapter.c
10331028
#: ports/nordic/common-hal/_bleio/Adapter.c
1029+
#: ports/zephyr-cp/common-hal/_bleio/Adapter.c
10341030
msgid "Failed to connect: internal error"
10351031
msgstr ""
10361032

10371033
#: ports/nordic/common-hal/_bleio/Adapter.c
1034+
#: ports/zephyr-cp/common-hal/_bleio/Adapter.c
10381035
msgid "Failed to connect: timeout"
10391036
msgstr ""
10401037

@@ -2247,10 +2244,6 @@ msgstr ""
22472244
msgid "USB error"
22482245
msgstr ""
22492246

2250-
#: shared-bindings/_bleio/UUID.c
2251-
msgid "UUID integer value must be 0-0xffff"
2252-
msgstr ""
2253-
22542247
#: shared-bindings/_bleio/UUID.c
22552248
msgid "UUID string not 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'"
22562249
msgstr ""
@@ -3393,10 +3386,6 @@ msgstr ""
33933386
msgid "initial values must be iterable"
33943387
msgstr ""
33953388

3396-
#: shared-bindings/_bleio/Characteristic.c shared-bindings/_bleio/Descriptor.c
3397-
msgid "initial_value length is wrong"
3398-
msgstr ""
3399-
34003389
#: py/compile.c
34013390
msgid "inline assembler must be a function"
34023391
msgstr ""
@@ -3808,7 +3797,6 @@ msgid "non-hex digit"
38083797
msgstr ""
38093798

38103799
#: ports/nordic/common-hal/_bleio/Adapter.c
3811-
#: ports/zephyr-cp/common-hal/_bleio/Adapter.c
38123800
msgid "non-zero timeout must be > 0.01"
38133801
msgstr ""
38143802

@@ -4292,7 +4280,6 @@ msgid "timeout duration exceeded the maximum supported value"
42924280
msgstr ""
42934281

42944282
#: ports/nordic/common-hal/_bleio/Adapter.c
4295-
#: ports/zephyr-cp/common-hal/_bleio/Adapter.c
42964283
msgid "timeout must be < 655.35 secs"
42974284
msgstr ""
42984285

ports/espressif/common-hal/_bleio/Characteristic.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ void common_hal_bleio_characteristic_set_cccd(bleio_characteristic_obj_t *self,
320320
}
321321

322322
const uint16_t conn_handle = bleio_connection_get_conn_handle(self->service->connection);
323-
common_hal_bleio_check_connected(conn_handle);
323+
bleio_check_connected(conn_handle);
324324

325325
uint16_t cccd_value =
326326
(notify ? 1 << 0 : 0) |

ports/espressif/common-hal/_bleio/Connection.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ void bleio_connection_clear(bleio_connection_internal_t *self);
6464

6565
int bleio_connection_event_cb(struct ble_gap_event *event, void *connection_in);
6666

67+
void bleio_check_connected(uint16_t conn_handle);
6768
uint16_t bleio_connection_get_conn_handle(bleio_connection_obj_t *self);
6869
mp_obj_t bleio_connection_new_from_internal(bleio_connection_internal_t *connection);
6970
bleio_connection_internal_t *bleio_conn_handle_to_connection(uint16_t conn_handle);

ports/espressif/common-hal/_bleio/__init__.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ void check_notify(BaseType_t result) {
155155
mp_raise_msg(&mp_type_TimeoutError, NULL);
156156
}
157157

158-
void common_hal_bleio_check_connected(uint16_t conn_handle) {
158+
void bleio_check_connected(uint16_t conn_handle) {
159159
if (conn_handle == BLEIO_HANDLE_INVALID) {
160160
mp_raise_ConnectionError(MP_ERROR_TEXT("Not connected"));
161161
}

ports/nordic/common-hal/_bleio/Attribute.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@
66

77
#pragma once
88

9+
#include <stdbool.h>
10+
11+
#include "py/obj.h"
12+
913
#include "shared-module/_bleio/Attribute.h"
1014

1115
extern void bleio_attribute_gatts_set_security_mode(ble_gap_conn_sec_mode_t *perm, bleio_attribute_security_mode_t security_mode);
16+
17+
size_t bleio_gatts_read(uint16_t handle, uint16_t conn_handle, uint8_t *buf, size_t len);
18+
void bleio_gatts_write(uint16_t handle, uint16_t conn_handle, mp_buffer_info_t *bufinfo);
19+
size_t bleio_gattc_read(uint16_t handle, uint16_t conn_handle, uint8_t *buf, size_t len);
20+
void bleio_gattc_write(uint16_t handle, uint16_t conn_handle, mp_buffer_info_t *bufinfo, bool write_no_response);

ports/nordic/common-hal/_bleio/Characteristic.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,10 @@ size_t common_hal_bleio_characteristic_get_value(bleio_characteristic_obj_t *sel
138138
if (self->handle != BLE_GATT_HANDLE_INVALID) {
139139
uint16_t conn_handle = bleio_connection_get_conn_handle(self->service->connection);
140140
if (common_hal_bleio_service_get_is_remote(self->service)) {
141-
return common_hal_bleio_gattc_read(self->handle, conn_handle, buf, len);
141+
return bleio_gattc_read(self->handle, conn_handle, buf, len);
142142
} else {
143143
// conn_handle is ignored for non-system attributes.
144-
return common_hal_bleio_gatts_read(self->handle, conn_handle, buf, len);
144+
return bleio_gatts_read(self->handle, conn_handle, buf, len);
145145
}
146146
}
147147

@@ -159,7 +159,7 @@ void common_hal_bleio_characteristic_set_value(bleio_characteristic_obj_t *self,
159159
if (common_hal_bleio_service_get_is_remote(self->service)) {
160160
uint16_t conn_handle = bleio_connection_get_conn_handle(self->service->connection);
161161
// Last argument is true if write-no-reponse desired.
162-
common_hal_bleio_gattc_write(self->handle, conn_handle, bufinfo,
162+
bleio_gattc_write(self->handle, conn_handle, bufinfo,
163163
(self->props & CHAR_PROP_WRITE_NO_RESPONSE));
164164
} else {
165165
// Validate data length for local characteristics only.
@@ -172,7 +172,7 @@ void common_hal_bleio_characteristic_set_value(bleio_characteristic_obj_t *self,
172172

173173
// Always write the value locally even if no connections are active.
174174
// conn_handle is ignored for non-system attributes, so we use BLE_CONN_HANDLE_INVALID.
175-
common_hal_bleio_gatts_write(self->handle, BLE_CONN_HANDLE_INVALID, bufinfo);
175+
bleio_gatts_write(self->handle, BLE_CONN_HANDLE_INVALID, bufinfo);
176176
// Check to see if we need to notify or indicate any active connections.
177177
for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) {
178178
bleio_connection_internal_t *connection = &bleio_connections[i];
@@ -255,7 +255,7 @@ void common_hal_bleio_characteristic_set_cccd(bleio_characteristic_obj_t *self,
255255
}
256256

257257
const uint16_t conn_handle = bleio_connection_get_conn_handle(self->service->connection);
258-
common_hal_bleio_check_connected(conn_handle);
258+
bleio_check_connected(conn_handle);
259259

260260
uint16_t cccd_value =
261261
(notify ? BLE_GATT_HVX_NOTIFICATION : 0) |

ports/nordic/common-hal/_bleio/Connection.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ typedef struct {
6565
void bleio_connection_clear(bleio_connection_internal_t *self);
6666
bool connection_on_ble_evt(ble_evt_t *ble_evt, void *self_in);
6767

68+
void bleio_check_connected(uint16_t conn_handle);
6869
uint16_t bleio_connection_get_conn_handle(bleio_connection_obj_t *self);
6970
mp_obj_t bleio_connection_new_from_internal(bleio_connection_internal_t *connection);
7071
bleio_connection_internal_t *bleio_conn_handle_to_connection(uint16_t conn_handle);

0 commit comments

Comments
 (0)