1 /*
2 * This file is part of the MicroPython project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2020 Damien P. George
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 "py/runtime.h"
28 #include "py/mperrno.h"
29 #include "py/mphal.h"
30
31 #if MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_BTSTACK
32
33 #include "extmod/btstack/modbluetooth_btstack.h"
34 #include "extmod/modbluetooth.h"
35
36 #include "lib/btstack/src/btstack.h"
37
38 #define DEBUG_printf(...) // printf("btstack: " __VA_ARGS__)
39
40 #ifndef MICROPY_PY_BLUETOOTH_DEFAULT_GAP_NAME
41 #define MICROPY_PY_BLUETOOTH_DEFAULT_GAP_NAME "MPY BTSTACK"
42 #endif
43
44 // How long to wait for a controller to init/deinit.
45 // Some controllers can take up to 5-6 seconds in normal operation.
46 STATIC const uint32_t BTSTACK_INIT_DEINIT_TIMEOUT_MS = 15000;
47
48 // We need to know the attribute handle for the GAP device name (see GAP_DEVICE_NAME_UUID)
49 // so it can be put into the gatts_db before registering the services, and accessed
50 // efficiently when requesting an attribute in att_read_callback. Because this is the
51 // first characteristic of the first service, it always has a handle value of 3.
52 STATIC const uint16_t BTSTACK_GAP_DEVICE_NAME_HANDLE = 3;
53
54 volatile int mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_OFF;
55
56 // sm_set_authentication_requirements is set-only, so cache current value.
57 #if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING
58 STATIC uint8_t mp_bluetooth_btstack_sm_auth_req = 0;
59 #endif
60
61 #define ERRNO_BLUETOOTH_NOT_ACTIVE MP_ENODEV
62
btstack_error_to_errno(int err)63 STATIC int btstack_error_to_errno(int err) {
64 DEBUG_printf(" --> btstack error: %d\n", err);
65 if (err == ERROR_CODE_SUCCESS) {
66 return 0;
67 } else if (err == BTSTACK_ACL_BUFFERS_FULL || err == BTSTACK_MEMORY_ALLOC_FAILED) {
68 return MP_ENOMEM;
69 } else if (err == GATT_CLIENT_IN_WRONG_STATE) {
70 return MP_EALREADY;
71 } else if (err == GATT_CLIENT_BUSY) {
72 return MP_EBUSY;
73 } else if (err == GATT_CLIENT_NOT_CONNECTED) {
74 return MP_ENOTCONN;
75 } else {
76 return MP_EINVAL;
77 }
78 }
79
80 #if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
create_mp_uuid(uint16_t uuid16,const uint8_t * uuid128)81 STATIC mp_obj_bluetooth_uuid_t create_mp_uuid(uint16_t uuid16, const uint8_t *uuid128) {
82 mp_obj_bluetooth_uuid_t result;
83 result.base.type = &mp_type_bluetooth_uuid;
84 if (uuid16 != 0) {
85 result.data[0] = uuid16 & 0xff;
86 result.data[1] = (uuid16 >> 8) & 0xff;
87 result.type = MP_BLUETOOTH_UUID_TYPE_16;
88 } else {
89 reverse_128(uuid128, result.data);
90 result.type = MP_BLUETOOTH_UUID_TYPE_128;
91 }
92 return result;
93 }
94 #endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
95
96 // Notes on supporting background ops (e.g. an attempt to gatts_notify while
97 // an existing notification is in progress):
98
99 // GATTS Notify/Indicate (att_server_notify/indicate)
100 // * When available, copies buffer immediately.
101 // * Otherwise fails with BTSTACK_ACL_BUFFERS_FULL
102 // * Use att_server_request_to_send_notification/indication to get callback
103 // * Takes btstack_context_callback_registration_t (and takes ownership) and conn_handle.
104 // * Callback is invoked with just the context member of the btstack_context_callback_registration_t
105
106 // GATTC Write without response (gatt_client_write_value_of_characteristic_without_response)
107 // * When available, copies buffer immediately.
108 // * Otherwise, fails with GATT_CLIENT_BUSY.
109 // * Use gatt_client_request_can_write_without_response_event to get callback
110 // * Takes btstack_packet_handler_t (function pointer) and conn_handle
111 // * Callback is invoked, use gatt_event_can_write_without_response_get_handle to get the conn_handle (no other context)
112 // * There can only be one pending gatt_client_request_can_write_without_response_event (otherwise we fail with EALREADY).
113
114 // GATTC Write with response (gatt_client_write_value_of_characteristic)
115 // * When peripheral is available, takes ownership of buffer.
116 // * Otherwise, fails with GATT_CLIENT_IN_WRONG_STATE (we fail the operation).
117 // * Raises GATT_EVENT_QUERY_COMPLETE to the supplied packet handler.
118
119 // For notify/indicate/write-without-response that proceed immediately, nothing extra required.
120 // For all other cases, buffer needs to be copied and protected from GC.
121 // For notify/indicate:
122 // * btstack_context_callback_registration_t:
123 // * needs to be malloc'ed
124 // * needs to be protected from GC
125 // * context arg needs to point back to the callback registration so it can be freed and un-protected
126 // For write-without-response
127 // * only the conn_handle is available in the callback
128 // * so we need a queue of conn_handle->(value_handle, copied buffer)
129
130 // Pending operation types.
131 enum {
132 // Queued for sending when possible.
133 MP_BLUETOOTH_BTSTACK_PENDING_NOTIFY, // Waiting for context callback
134 MP_BLUETOOTH_BTSTACK_PENDING_INDICATE, // Waiting for context callback
135 MP_BLUETOOTH_BTSTACK_PENDING_WRITE_NO_RESPONSE, // Waiting for conn handle
136 // Hold buffer pointer until complete.
137 MP_BLUETOOTH_BTSTACK_PENDING_WRITE, // Waiting for write done event
138 };
139
140 // Pending operation:
141 // - Holds a GC reference to the copied outgoing buffer.
142 // - Provides enough information for the callback handler to execute the desired operation.
143 struct _mp_btstack_pending_op_t {
144 btstack_linked_item_t *next; // Must be first field to match btstack_linked_item.
145
146 // See enum above.
147 uint16_t op_type;
148
149 // For all op types.
150 uint16_t conn_handle;
151 uint16_t value_handle;
152
153 // For notify/indicate only.
154 // context_registration.context will point back to this struct.
155 btstack_context_callback_registration_t context_registration;
156
157 // For notify/indicate/write-without-response, this is the actual buffer to send.
158 // For write-with-response, just holding onto the buffer for GC ref.
159 size_t len;
160 uint8_t buf[];
161 };
162
163 // Must hold MICROPY_PY_BLUETOOTH_ENTER.
btstack_remove_pending_operation(mp_btstack_pending_op_t * pending_op,bool del)164 STATIC void btstack_remove_pending_operation(mp_btstack_pending_op_t *pending_op, bool del) {
165 bool removed = btstack_linked_list_remove(&MP_STATE_PORT(bluetooth_btstack_root_pointers)->pending_ops, (btstack_linked_item_t *)pending_op);
166 assert(removed);
167 (void)removed;
168 if (del) {
169 m_del_var(mp_btstack_pending_op_t, uint8_t, pending_op->len, pending_op);
170 }
171 }
172
173 // Called in response to a gatts_notify/indicate being unable to complete, which then calls
174 // att_server_request_to_send_notification.
175 // We now have an opportunity to re-try the operation with an empty ACL buffer.
btstack_notify_indicate_ready_handler(void * context)176 STATIC void btstack_notify_indicate_ready_handler(void *context) {
177 MICROPY_PY_BLUETOOTH_ENTER
178 mp_btstack_pending_op_t *pending_op = (mp_btstack_pending_op_t *)context;
179 DEBUG_printf("btstack_notify_indicate_ready_handler op_type=%d conn_handle=%d value_handle=%d len=%zu\n", pending_op->op_type, pending_op->conn_handle, pending_op->value_handle, pending_op->len);
180 if (pending_op->op_type == MP_BLUETOOTH_BTSTACK_PENDING_NOTIFY) {
181 int err = att_server_notify(pending_op->conn_handle, pending_op->value_handle, pending_op->buf, pending_op->len);
182 DEBUG_printf("btstack_notify_indicate_ready_handler: sending notification err=%d\n", err);
183 assert(err == ERROR_CODE_SUCCESS);
184 (void)err;
185 } else {
186 assert(pending_op->op_type == MP_BLUETOOTH_BTSTACK_PENDING_INDICATE);
187 int err = att_server_indicate(pending_op->conn_handle, pending_op->value_handle, NULL, 0);
188 DEBUG_printf("btstack_notify_indicate_ready_handler: sending indication err=%d\n", err);
189 assert(err == ERROR_CODE_SUCCESS);
190 (void)err;
191 }
192 // Can't free the pending op as we're in IRQ context. Leave it for the GC.
193 btstack_remove_pending_operation(pending_op, false /* del */);
194 MICROPY_PY_BLUETOOTH_EXIT
195 }
196
197 // Register a pending background operation -- copies the buffer, and makes it known to the GC.
btstack_enqueue_pending_operation(uint16_t op_type,uint16_t conn_handle,uint16_t value_handle,const uint8_t * buf,size_t len)198 STATIC mp_btstack_pending_op_t *btstack_enqueue_pending_operation(uint16_t op_type, uint16_t conn_handle, uint16_t value_handle, const uint8_t *buf, size_t len) {
199 DEBUG_printf("btstack_enqueue_pending_operation op_type=%d conn_handle=%d value_handle=%d len=%zu\n", op_type, conn_handle, value_handle, len);
200 mp_btstack_pending_op_t *pending_op = m_new_obj_var(mp_btstack_pending_op_t, uint8_t, len);
201 pending_op->op_type = op_type;
202 pending_op->conn_handle = conn_handle;
203 pending_op->value_handle = value_handle;
204 pending_op->len = len;
205 memcpy(pending_op->buf, buf, len);
206
207 if (op_type == MP_BLUETOOTH_BTSTACK_PENDING_NOTIFY || op_type == MP_BLUETOOTH_BTSTACK_PENDING_INDICATE) {
208 pending_op->context_registration.callback = &btstack_notify_indicate_ready_handler;
209 pending_op->context_registration.context = pending_op;
210 }
211
212 MICROPY_PY_BLUETOOTH_ENTER
213 bool added = btstack_linked_list_add(&MP_STATE_PORT(bluetooth_btstack_root_pointers)->pending_ops, (btstack_linked_item_t *)pending_op);
214 assert(added);
215 (void)added;
216 MICROPY_PY_BLUETOOTH_EXIT
217
218 return pending_op;
219 }
220
221 #if MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT
222
223 // Cleans up a pending op of the specified type for this conn_handle (and if specified, value_handle).
224 // Used by MP_BLUETOOTH_BTSTACK_PENDING_WRITE and MP_BLUETOOTH_BTSTACK_PENDING_WRITE_NO_RESPONSE.
225 // At the moment, both will set value_handle=0xffff as the events do not know their value_handle.
226 // TODO: Can we make btstack give us the value_handle for regular write (with response) so that we
227 // know for sure that we're using the correct entry.
btstack_finish_pending_operation(uint16_t op_type,uint16_t conn_handle,uint16_t value_handle,bool del)228 STATIC mp_btstack_pending_op_t *btstack_finish_pending_operation(uint16_t op_type, uint16_t conn_handle, uint16_t value_handle, bool del) {
229 MICROPY_PY_BLUETOOTH_ENTER
230 DEBUG_printf("btstack_finish_pending_operation op_type=%d conn_handle=%d value_handle=%d\n", op_type, conn_handle, value_handle);
231 btstack_linked_list_iterator_t it;
232 btstack_linked_list_iterator_init(&it, &MP_STATE_PORT(bluetooth_btstack_root_pointers)->pending_ops);
233 while (btstack_linked_list_iterator_has_next(&it)) {
234 mp_btstack_pending_op_t *pending_op = (mp_btstack_pending_op_t *)btstack_linked_list_iterator_next(&it);
235
236 if (pending_op->op_type == op_type && pending_op->conn_handle == conn_handle && (value_handle == 0xffff || pending_op->value_handle == value_handle)) {
237 DEBUG_printf("btstack_finish_pending_operation: found value_handle=%d len=%zu\n", pending_op->value_handle, pending_op->len);
238 btstack_remove_pending_operation(pending_op, del);
239 MICROPY_PY_BLUETOOTH_EXIT
240 return del ? NULL : pending_op;
241 }
242 }
243 DEBUG_printf("btstack_finish_pending_operation: not found\n");
244 MICROPY_PY_BLUETOOTH_EXIT
245 return NULL;
246 }
247 #endif
248
249 // This needs to be separate to btstack_packet_handler otherwise we get
250 // dual-delivery of the HCI_EVENT_LE_META event.
btstack_packet_handler_att_server(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)251 STATIC void btstack_packet_handler_att_server(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) {
252 (void)channel;
253 (void)size;
254 DEBUG_printf("btstack_packet_handler_att_server(packet_type=%u, packet=%p)\n", packet_type, packet);
255 if (packet_type != HCI_EVENT_PACKET) {
256 return;
257 }
258
259 uint8_t event_type = hci_event_packet_get_type(packet);
260
261 if (event_type == ATT_EVENT_CONNECTED) {
262 DEBUG_printf(" --> att connected\n");
263 // The ATT_EVENT_*CONNECTED events are fired for both peripheral and central role, with no way to tell which.
264 // So we use the HCI_EVENT_LE_META event directly in the main packet handler.
265 } else if (event_type == ATT_EVENT_DISCONNECTED) {
266 DEBUG_printf(" --> att disconnected\n");
267 } else if (event_type == ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE) {
268 DEBUG_printf(" --> att indication complete\n");
269 uint16_t conn_handle = att_event_handle_value_indication_complete_get_conn_handle(packet);
270 uint16_t value_handle = att_event_handle_value_indication_complete_get_attribute_handle(packet);
271 uint8_t status = att_event_handle_value_indication_complete_get_status(packet);
272 mp_bluetooth_gatts_on_indicate_complete(conn_handle, value_handle, status);
273 } else if (event_type == ATT_EVENT_MTU_EXCHANGE_COMPLETE) {
274 // This is triggered in peripheral mode, when exchange initiated by us or remote.
275 uint16_t conn_handle = att_event_mtu_exchange_complete_get_handle(packet);
276 uint16_t mtu = att_event_mtu_exchange_complete_get_MTU(packet);
277 mp_bluetooth_gatts_on_mtu_exchanged(conn_handle, mtu);
278 } else if (event_type == HCI_EVENT_LE_META || event_type == HCI_EVENT_DISCONNECTION_COMPLETE) {
279 // Ignore, duplicated by att_server.c.
280 } else {
281 DEBUG_printf(" --> hci att server event type: unknown (0x%02x)\n", event_type);
282 }
283 }
284
285 #if MICROPY_BLUETOOTH_USE_ZEPHYR_STATIC_ADDRESS
286 // During startup, the controller (e.g. Zephyr) might give us a static address that we can use.
287 STATIC uint8_t controller_static_addr[6] = {0};
288 STATIC bool controller_static_addr_available = false;
289
290 STATIC const uint8_t read_static_address_command_complete_prefix[] = { 0x0e, 0x1b, 0x01, 0x09, 0xfc };
291 #endif
292
btstack_packet_handler(uint8_t packet_type,uint8_t * packet,uint8_t irq)293 STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t irq) {
294 DEBUG_printf("btstack_packet_handler(packet_type=%u, packet=%p)\n", packet_type, packet);
295 if (packet_type != HCI_EVENT_PACKET) {
296 return;
297 }
298
299 uint8_t event_type = hci_event_packet_get_type(packet);
300
301 if (event_type == HCI_EVENT_LE_META) {
302 DEBUG_printf(" --> hci le meta\n");
303 switch (hci_event_le_meta_get_subevent_code(packet)) {
304 case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: {
305 uint16_t conn_handle = hci_subevent_le_connection_complete_get_connection_handle(packet);
306 uint8_t addr_type = hci_subevent_le_connection_complete_get_peer_address_type(packet);
307 bd_addr_t addr;
308 hci_subevent_le_connection_complete_get_peer_address(packet, addr);
309 uint16_t irq_event;
310 if (hci_subevent_le_connection_complete_get_role(packet) == 0) {
311 // Master role.
312 irq_event = MP_BLUETOOTH_IRQ_PERIPHERAL_CONNECT;
313 } else {
314 // Slave role.
315 irq_event = MP_BLUETOOTH_IRQ_CENTRAL_CONNECT;
316 }
317 mp_bluetooth_gap_on_connected_disconnected(irq_event, conn_handle, addr_type, addr);
318 break;
319 }
320 case HCI_SUBEVENT_LE_CONNECTION_UPDATE_COMPLETE: {
321 uint8_t status = hci_subevent_le_connection_update_complete_get_status(packet);
322 uint16_t conn_handle = hci_subevent_le_connection_update_complete_get_connection_handle(packet);
323 uint16_t conn_interval = hci_subevent_le_connection_update_complete_get_conn_interval(packet);
324 uint16_t conn_latency = hci_subevent_le_connection_update_complete_get_conn_latency(packet);
325 uint16_t supervision_timeout = hci_subevent_le_connection_update_complete_get_supervision_timeout(packet);
326 DEBUG_printf("- LE Connection %04x: connection update - connection interval %u.%02u ms, latency %u, timeout %u\n",
327 conn_handle, conn_interval * 125 / 100, 25 * (conn_interval & 3), conn_latency, supervision_timeout);
328 mp_bluetooth_gap_on_connection_update(conn_handle, conn_interval, conn_latency, supervision_timeout, status);
329 break;
330 }
331 }
332 } else if (event_type == BTSTACK_EVENT_STATE) {
333 uint8_t state = btstack_event_state_get_state(packet);
334 DEBUG_printf(" --> btstack event state 0x%02x\n", state);
335 if (state == HCI_STATE_WORKING) {
336 // Signal that initialisation has completed.
337 mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_ACTIVE;
338 } else if (state == HCI_STATE_HALTING) {
339 // Signal that de-initialisation has begun.
340 mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_HALTING;
341 } else if (state == HCI_STATE_OFF) {
342 // Signal that de-initialisation has completed.
343 mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_OFF;
344 }
345 } else if (event_type == BTSTACK_EVENT_POWERON_FAILED) {
346 // Signal that initialisation has failed.
347 mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_OFF;
348 } else if (event_type == HCI_EVENT_TRANSPORT_PACKET_SENT) {
349 DEBUG_printf(" --> hci transport packet sent\n");
350 } else if (event_type == HCI_EVENT_COMMAND_COMPLETE) {
351 DEBUG_printf(" --> hci command complete\n");
352 #if MICROPY_BLUETOOTH_USE_ZEPHYR_STATIC_ADDRESS
353 if (memcmp(packet, read_static_address_command_complete_prefix, sizeof(read_static_address_command_complete_prefix)) == 0) {
354 DEBUG_printf(" --> static address available\n");
355 reverse_48(&packet[7], controller_static_addr);
356 controller_static_addr_available = true;
357 }
358 #endif // MICROPY_BLUETOOTH_USE_ZEPHYR_STATIC_ADDRESS
359 } else if (event_type == HCI_EVENT_COMMAND_STATUS) {
360 DEBUG_printf(" --> hci command status\n");
361 } else if (event_type == HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS) {
362 DEBUG_printf(" --> hci number of completed packets\n");
363 } else if (event_type == BTSTACK_EVENT_NR_CONNECTIONS_CHANGED) {
364 DEBUG_printf(" --> btstack # conns changed\n");
365 } else if (event_type == HCI_EVENT_VENDOR_SPECIFIC) {
366 DEBUG_printf(" --> hci vendor specific\n");
367 } else if (event_type == SM_EVENT_AUTHORIZATION_RESULT ||
368 event_type == SM_EVENT_PAIRING_COMPLETE ||
369 // event_type == GAP_EVENT_DEDICATED_BONDING_COMPLETED || // No conn_handle
370 event_type == HCI_EVENT_ENCRYPTION_CHANGE) {
371 DEBUG_printf(" --> enc/auth/pair/bond change\n", );
372 #if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING
373 uint16_t conn_handle;
374 switch (event_type) {
375 case SM_EVENT_AUTHORIZATION_RESULT:
376 conn_handle = sm_event_authorization_result_get_handle(packet);
377 break;
378 case SM_EVENT_PAIRING_COMPLETE:
379 conn_handle = sm_event_pairing_complete_get_handle(packet);
380 break;
381 case HCI_EVENT_ENCRYPTION_CHANGE:
382 conn_handle = hci_event_encryption_change_get_connection_handle(packet);
383 break;
384 default:
385 return;
386 }
387
388 hci_connection_t *hci_con = hci_connection_for_handle(conn_handle);
389 sm_connection_t *desc = &hci_con->sm_connection;
390 mp_bluetooth_gatts_on_encryption_update(conn_handle,
391 desc->sm_connection_encrypted,
392 desc->sm_connection_authenticated,
393 desc->sm_le_db_index != -1,
394 desc->sm_actual_encryption_key_size);
395 #endif // MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING
396 } else if (event_type == HCI_EVENT_DISCONNECTION_COMPLETE) {
397 DEBUG_printf(" --> hci disconnect complete\n");
398 uint16_t conn_handle = hci_event_disconnection_complete_get_connection_handle(packet);
399 const hci_connection_t *conn = hci_connection_for_handle(conn_handle);
400 uint16_t irq_event;
401 if (conn == NULL || conn->role == 0) {
402 // Master role.
403 irq_event = MP_BLUETOOTH_IRQ_PERIPHERAL_DISCONNECT;
404 } else {
405 // Slave role.
406 irq_event = MP_BLUETOOTH_IRQ_CENTRAL_DISCONNECT;
407 }
408 uint8_t addr[6] = {0};
409 mp_bluetooth_gap_on_connected_disconnected(irq_event, conn_handle, 0xff, addr);
410 #if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
411 } else if (event_type == GAP_EVENT_ADVERTISING_REPORT) {
412 DEBUG_printf(" --> gap advertising report\n");
413 bd_addr_t address;
414 gap_event_advertising_report_get_address(packet, address);
415 uint8_t adv_event_type = gap_event_advertising_report_get_advertising_event_type(packet);
416 uint8_t address_type = gap_event_advertising_report_get_address_type(packet);
417 int8_t rssi = gap_event_advertising_report_get_rssi(packet);
418 uint8_t length = gap_event_advertising_report_get_data_length(packet);
419 const uint8_t *data = gap_event_advertising_report_get_data(packet);
420 mp_bluetooth_gap_on_scan_result(address_type, address, adv_event_type, rssi, data, length);
421 #endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
422 #if MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT
423 } else if (event_type == GATT_EVENT_QUERY_COMPLETE) {
424 uint16_t conn_handle = gatt_event_query_complete_get_handle(packet);
425 uint16_t status = gatt_event_query_complete_get_att_status(packet);
426 DEBUG_printf(" --> gatt query complete irq=%d conn_handle=%d status=%d\n", irq, conn_handle, status);
427 if (irq == MP_BLUETOOTH_IRQ_GATTC_READ_DONE || irq == MP_BLUETOOTH_IRQ_GATTC_WRITE_DONE) {
428 // TODO there is no value_handle available to pass here.
429 // TODO try and get this implemented in btstack.
430 mp_bluetooth_gattc_on_read_write_status(irq, conn_handle, 0xffff, status);
431 // Unref the saved buffer for the write operation on this conn_handle.
432 if (irq == MP_BLUETOOTH_IRQ_GATTC_WRITE_DONE) {
433 btstack_finish_pending_operation(MP_BLUETOOTH_BTSTACK_PENDING_WRITE, conn_handle, 0xffff, false /* del */);
434 }
435 } else if (irq == MP_BLUETOOTH_IRQ_GATTC_SERVICE_DONE ||
436 irq == MP_BLUETOOTH_IRQ_GATTC_CHARACTERISTIC_DONE ||
437 irq == MP_BLUETOOTH_IRQ_GATTC_DESCRIPTOR_DONE) {
438 mp_bluetooth_gattc_on_discover_complete(irq, conn_handle, status);
439 }
440 } else if (event_type == GATT_EVENT_SERVICE_QUERY_RESULT) {
441 DEBUG_printf(" --> gatt service query result\n");
442 uint16_t conn_handle = gatt_event_service_query_result_get_handle(packet);
443 gatt_client_service_t service;
444 gatt_event_service_query_result_get_service(packet, &service);
445 mp_obj_bluetooth_uuid_t service_uuid = create_mp_uuid(service.uuid16, service.uuid128);
446 mp_bluetooth_gattc_on_primary_service_result(conn_handle, service.start_group_handle, service.end_group_handle, &service_uuid);
447 } else if (event_type == GATT_EVENT_CHARACTERISTIC_QUERY_RESULT) {
448 DEBUG_printf(" --> gatt characteristic query result\n");
449 uint16_t conn_handle = gatt_event_characteristic_query_result_get_handle(packet);
450 gatt_client_characteristic_t characteristic;
451 gatt_event_characteristic_query_result_get_characteristic(packet, &characteristic);
452 mp_obj_bluetooth_uuid_t characteristic_uuid = create_mp_uuid(characteristic.uuid16, characteristic.uuid128);
453 mp_bluetooth_gattc_on_characteristic_result(conn_handle, characteristic.start_handle, characteristic.value_handle, characteristic.properties, &characteristic_uuid);
454 } else if (event_type == GATT_EVENT_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT) {
455 DEBUG_printf(" --> gatt descriptor query result\n");
456 uint16_t conn_handle = gatt_event_all_characteristic_descriptors_query_result_get_handle(packet);
457 gatt_client_characteristic_descriptor_t descriptor;
458 gatt_event_all_characteristic_descriptors_query_result_get_characteristic_descriptor(packet, &descriptor);
459 mp_obj_bluetooth_uuid_t descriptor_uuid = create_mp_uuid(descriptor.uuid16, descriptor.uuid128);
460 mp_bluetooth_gattc_on_descriptor_result(conn_handle, descriptor.handle, &descriptor_uuid);
461 } else if (event_type == GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT) {
462 DEBUG_printf(" --> gatt characteristic value query result\n");
463 uint16_t conn_handle = gatt_event_characteristic_value_query_result_get_handle(packet);
464 uint16_t value_handle = gatt_event_characteristic_value_query_result_get_value_handle(packet);
465 uint16_t len = gatt_event_characteristic_value_query_result_get_value_length(packet);
466 const uint8_t *data = gatt_event_characteristic_value_query_result_get_value(packet);
467 mp_bluetooth_gattc_on_data_available(MP_BLUETOOTH_IRQ_GATTC_READ_RESULT, conn_handle, value_handle, &data, &len, 1);
468 } else if (event_type == GATT_EVENT_NOTIFICATION) {
469 DEBUG_printf(" --> gatt notification\n");
470 uint16_t conn_handle = gatt_event_notification_get_handle(packet);
471 uint16_t value_handle = gatt_event_notification_get_value_handle(packet);
472 uint16_t len = gatt_event_notification_get_value_length(packet);
473 const uint8_t *data = gatt_event_notification_get_value(packet);
474 mp_bluetooth_gattc_on_data_available(MP_BLUETOOTH_IRQ_GATTC_NOTIFY, conn_handle, value_handle, &data, &len, 1);
475 } else if (event_type == GATT_EVENT_INDICATION) {
476 DEBUG_printf(" --> gatt indication\n");
477 uint16_t conn_handle = gatt_event_indication_get_handle(packet);
478 uint16_t value_handle = gatt_event_indication_get_value_handle(packet);
479 uint16_t len = gatt_event_indication_get_value_length(packet);
480 const uint8_t *data = gatt_event_indication_get_value(packet);
481 mp_bluetooth_gattc_on_data_available(MP_BLUETOOTH_IRQ_GATTC_INDICATE, conn_handle, value_handle, &data, &len, 1);
482 } else if (event_type == GATT_EVENT_CAN_WRITE_WITHOUT_RESPONSE) {
483 uint16_t conn_handle = gatt_event_can_write_without_response_get_handle(packet);
484 DEBUG_printf(" --> gatt can write without response %d\n", conn_handle);
485 mp_btstack_pending_op_t *pending_op = btstack_finish_pending_operation(MP_BLUETOOTH_BTSTACK_PENDING_WRITE_NO_RESPONSE, conn_handle, 0xffff, false /* !del */);
486 if (pending_op) {
487 DEBUG_printf(" --> ready for value_handle=%d len=%zu\n", pending_op->value_handle, pending_op->len);
488 gatt_client_write_value_of_characteristic_without_response(pending_op->conn_handle, pending_op->value_handle, pending_op->len, (uint8_t *)pending_op->buf);
489 // Note: Can't "del" the pending_op from IRQ context. Leave it for the GC.
490 }
491
492 #endif // MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT
493 } else {
494 DEBUG_printf(" --> hci event type: unknown (0x%02x)\n", event_type);
495 }
496 }
497
498 // Because the packet handler callbacks don't support an argument, we use a specific
499 // handler when we need to provide additional state to the handler (in the "irq" parameter).
500 // This is the generic handler for when you don't need extra state.
btstack_packet_handler_generic(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)501 STATIC void btstack_packet_handler_generic(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) {
502 (void)channel;
503 (void)size;
504 btstack_packet_handler(packet_type, packet, 0);
505 }
506
507 STATIC btstack_packet_callback_registration_t hci_event_callback_registration = {
508 .callback = &btstack_packet_handler_generic
509 };
510
511 #if MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT
512 // For when the handler is being used for service discovery.
btstack_packet_handler_discover_services(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)513 STATIC void btstack_packet_handler_discover_services(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) {
514 (void)channel;
515 (void)size;
516 btstack_packet_handler(packet_type, packet, MP_BLUETOOTH_IRQ_GATTC_SERVICE_DONE);
517 }
518
519 // For when the handler is being used for characteristic discovery.
btstack_packet_handler_discover_characteristics(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)520 STATIC void btstack_packet_handler_discover_characteristics(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) {
521 (void)channel;
522 (void)size;
523 btstack_packet_handler(packet_type, packet, MP_BLUETOOTH_IRQ_GATTC_CHARACTERISTIC_DONE);
524 }
525
526 // For when the handler is being used for descriptor discovery.
btstack_packet_handler_discover_descriptors(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)527 STATIC void btstack_packet_handler_discover_descriptors(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) {
528 (void)channel;
529 (void)size;
530 btstack_packet_handler(packet_type, packet, MP_BLUETOOTH_IRQ_GATTC_DESCRIPTOR_DONE);
531 }
532
533 // For when the handler is being used for a read query.
btstack_packet_handler_read(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)534 STATIC void btstack_packet_handler_read(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) {
535 (void)channel;
536 (void)size;
537 btstack_packet_handler(packet_type, packet, MP_BLUETOOTH_IRQ_GATTC_READ_DONE);
538 }
539
540 // For when the handler is being used for write-with-response.
btstack_packet_handler_write_with_response(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)541 STATIC void btstack_packet_handler_write_with_response(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) {
542 (void)channel;
543 (void)size;
544 btstack_packet_handler(packet_type, packet, MP_BLUETOOTH_IRQ_GATTC_WRITE_DONE);
545 }
546 #endif // MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT
547
548 STATIC btstack_timer_source_t btstack_init_deinit_timeout;
549
btstack_init_deinit_timeout_handler(btstack_timer_source_t * ds)550 STATIC void btstack_init_deinit_timeout_handler(btstack_timer_source_t *ds) {
551 (void)ds;
552
553 // Stop waiting for initialisation.
554 // This signals both the loops in mp_bluetooth_init and mp_bluetooth_deinit,
555 // as well as ports that run a polling loop.
556 mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_TIMEOUT;
557 }
558
559 #if !MICROPY_BLUETOOTH_USE_MP_HAL_GET_MAC_STATIC_ADDRESS
btstack_static_address_ready(void * arg)560 STATIC void btstack_static_address_ready(void *arg) {
561 DEBUG_printf("btstack_static_address_ready.\n");
562 *(volatile bool *)arg = true;
563 }
564 #endif
565
set_public_address(void)566 STATIC bool set_public_address(void) {
567 bd_addr_t local_addr;
568 gap_local_bd_addr(local_addr);
569 bd_addr_t null_addr = {0};
570 if (memcmp(local_addr, null_addr, 6) == 0) {
571 DEBUG_printf("set_public_address: No public address available.\n");
572 return false;
573 }
574 DEBUG_printf("set_public_address: Using controller's public address.\n");
575 gap_random_address_set_mode(GAP_RANDOM_ADDRESS_TYPE_OFF);
576 return true;
577 }
578
set_random_address(void)579 STATIC void set_random_address(void) {
580 #if MICROPY_BLUETOOTH_USE_ZEPHYR_STATIC_ADDRESS
581 if (controller_static_addr_available) {
582 DEBUG_printf("set_random_address: Using static address supplied by controller.\n");
583 gap_random_address_set(controller_static_addr);
584 } else
585 #endif // MICROPY_BLUETOOTH_USE_ZEPHYR_STATIC_ADDRESS
586 {
587 bd_addr_t static_addr;
588
589 #if MICROPY_BLUETOOTH_USE_MP_HAL_GET_MAC_STATIC_ADDRESS
590
591 DEBUG_printf("set_random_address: Generating static address using mp_hal_get_mac\n");
592 mp_hal_get_mac(MP_HAL_MAC_BDADDR, static_addr);
593 // Mark it as STATIC (not RPA or NRPA).
594 static_addr[0] |= 0xc0;
595
596 #else
597
598 DEBUG_printf("set_random_address: Generating random static address.\n");
599 btstack_crypto_random_t sm_crypto_random_request;
600 volatile bool ready = false;
601 btstack_crypto_random_generate(&sm_crypto_random_request, static_addr, 6, &btstack_static_address_ready, (void *)&ready);
602 while (!ready) {
603 MICROPY_EVENT_POLL_HOOK
604 }
605
606 #endif // MICROPY_BLUETOOTH_USE_MP_HAL_GET_MAC_STATIC_ADDRESS
607
608 DEBUG_printf("set_random_address: Address generated.\n");
609 gap_random_address_set(static_addr);
610 }
611
612 // Wait for the controller to accept this address.
613 while (true) {
614 uint8_t addr_type;
615 bd_addr_t addr;
616 gap_le_get_own_address(&addr_type, addr);
617
618 bd_addr_t null_addr = {0};
619 if (memcmp(addr, null_addr, 6) != 0) {
620 break;
621 }
622
623 MICROPY_EVENT_POLL_HOOK
624 }
625 DEBUG_printf("set_random_address: Address loaded by controller\n");
626 }
627
mp_bluetooth_init(void)628 int mp_bluetooth_init(void) {
629 DEBUG_printf("mp_bluetooth_init\n");
630
631 if (mp_bluetooth_btstack_state == MP_BLUETOOTH_BTSTACK_STATE_ACTIVE) {
632 return 0;
633 }
634
635 // Clean up if necessary.
636 mp_bluetooth_deinit();
637
638 btstack_memory_init();
639
640 #if MICROPY_BLUETOOTH_USE_ZEPHYR_STATIC_ADDRESS
641 controller_static_addr_available = false;
642 #endif
643
644 MP_STATE_PORT(bluetooth_btstack_root_pointers) = m_new0(mp_bluetooth_btstack_root_pointers_t, 1);
645 mp_bluetooth_gatts_db_create(&MP_STATE_PORT(bluetooth_btstack_root_pointers)->gatts_db);
646
647 // Set the default GAP device name.
648 const char *gap_name = MICROPY_PY_BLUETOOTH_DEFAULT_GAP_NAME;
649 size_t gap_len = strlen(gap_name);
650 mp_bluetooth_gatts_db_create_entry(MP_STATE_PORT(bluetooth_btstack_root_pointers)->gatts_db, BTSTACK_GAP_DEVICE_NAME_HANDLE, gap_len);
651 mp_bluetooth_gap_set_device_name((const uint8_t *)gap_name, gap_len);
652
653 mp_bluetooth_btstack_port_init();
654 mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_STARTING;
655
656 l2cap_init();
657 le_device_db_init();
658 sm_init();
659
660 // Set blank ER/IR keys to suppress BTstack warning.
661 // TODO handle this correctly.
662 sm_key_t dummy_key;
663 memset(dummy_key, 0, sizeof(dummy_key));
664 sm_set_er(dummy_key);
665 sm_set_ir(dummy_key);
666
667 #if MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT
668 gatt_client_init();
669
670 // We always require explicitly exchanging MTU with ble.gattc_exchange_mtu().
671 gatt_client_mtu_enable_auto_negotiation(false);
672 #endif // MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT
673
674 // Register for HCI events.
675 hci_add_event_handler(&hci_event_callback_registration);
676
677 // Register for ATT server events.
678 att_server_register_packet_handler(&btstack_packet_handler_att_server);
679
680 // Set a timeout for HCI initialisation.
681 btstack_run_loop_set_timer(&btstack_init_deinit_timeout, BTSTACK_INIT_DEINIT_TIMEOUT_MS);
682 btstack_run_loop_set_timer_handler(&btstack_init_deinit_timeout, btstack_init_deinit_timeout_handler);
683 btstack_run_loop_add_timer(&btstack_init_deinit_timeout);
684
685 DEBUG_printf("mp_bluetooth_init: waiting for stack startup\n");
686
687 // Either the HCI event will set state to ACTIVE, or the timeout will set it to TIMEOUT.
688 mp_bluetooth_btstack_port_start();
689 while (mp_bluetooth_btstack_state == MP_BLUETOOTH_BTSTACK_STATE_STARTING) {
690 MICROPY_EVENT_POLL_HOOK
691 }
692 btstack_run_loop_remove_timer(&btstack_init_deinit_timeout);
693
694 // Check for timeout.
695 if (mp_bluetooth_btstack_state != MP_BLUETOOTH_BTSTACK_STATE_ACTIVE) {
696 DEBUG_printf("mp_bluetooth_init: stack startup timed out\n");
697
698 bool timeout = mp_bluetooth_btstack_state == MP_BLUETOOTH_BTSTACK_STATE_TIMEOUT;
699
700 // Required to stop the polling loop.
701 mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_OFF;
702 // Attempt a shutdown (may not do anything).
703 mp_bluetooth_btstack_port_deinit();
704
705 // Clean up.
706 MP_STATE_PORT(bluetooth_btstack_root_pointers) = NULL;
707
708 return timeout ? MP_ETIMEDOUT : MP_EINVAL;
709 }
710
711 DEBUG_printf("mp_bluetooth_init: stack startup complete\n");
712
713 // At this point if the controller has its own public address, btstack will know this.
714 // However, if this is not available, then attempt to get a static address:
715 // - For a Zephyr controller on nRF, a static address will be available during startup.
716 // - Otherwise we ask the controller to generate a static address for us.
717 // In either case, calling gap_random_address_set will set the mode to STATIC, and then
718 // immediately set the address on the controller. We then wait until this address becomes available.
719
720 if (!set_public_address()) {
721 set_random_address();
722 }
723
724 #if MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT
725 // Enable GATT_EVENT_NOTIFICATION/GATT_EVENT_INDICATION for all connections and handles.
726 gatt_client_listen_for_characteristic_value_updates(&MP_STATE_PORT(bluetooth_btstack_root_pointers)->notification, &btstack_packet_handler_generic, GATT_CLIENT_ANY_CONNECTION, NULL);
727 #endif // MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT
728
729 return 0;
730 }
731
mp_bluetooth_deinit(void)732 void mp_bluetooth_deinit(void) {
733 DEBUG_printf("mp_bluetooth_deinit\n");
734
735 // Nothing to do if not initialised.
736 if (!MP_STATE_PORT(bluetooth_btstack_root_pointers)) {
737 return;
738 }
739
740 mp_bluetooth_gap_advertise_stop();
741
742 #if MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT
743 // Remove our registration for notify/indicate.
744 gatt_client_stop_listening_for_characteristic_value_updates(&MP_STATE_PORT(bluetooth_btstack_root_pointers)->notification);
745 #endif // MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT
746
747 // Set a timer that will forcibly set the state to TIMEOUT, which will stop the loop below.
748 btstack_run_loop_set_timer(&btstack_init_deinit_timeout, BTSTACK_INIT_DEINIT_TIMEOUT_MS);
749 btstack_run_loop_add_timer(&btstack_init_deinit_timeout);
750
751 // This should result in a clean shutdown, which will set the state to OFF.
752 // On Unix this is blocking (it joins on the poll thread), on other ports the loop below will wait unil
753 // either timeout or clean shutdown.
754 mp_bluetooth_btstack_port_deinit();
755 while (mp_bluetooth_btstack_state == MP_BLUETOOTH_BTSTACK_STATE_ACTIVE) {
756 MICROPY_EVENT_POLL_HOOK
757 }
758 btstack_run_loop_remove_timer(&btstack_init_deinit_timeout);
759
760 mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_OFF;
761 MP_STATE_PORT(bluetooth_btstack_root_pointers) = NULL;
762
763 DEBUG_printf("mp_bluetooth_deinit: complete\n");
764 }
765
mp_bluetooth_is_active(void)766 bool mp_bluetooth_is_active(void) {
767 return mp_bluetooth_btstack_state == MP_BLUETOOTH_BTSTACK_STATE_ACTIVE;
768 }
769
mp_bluetooth_get_current_address(uint8_t * addr_type,uint8_t * addr)770 void mp_bluetooth_get_current_address(uint8_t *addr_type, uint8_t *addr) {
771 if (!mp_bluetooth_is_active()) {
772 mp_raise_OSError(ERRNO_BLUETOOTH_NOT_ACTIVE);
773 }
774
775 DEBUG_printf("mp_bluetooth_get_current_address\n");
776 gap_le_get_own_address(addr_type, addr);
777 }
778
mp_bluetooth_set_address_mode(uint8_t addr_mode)779 void mp_bluetooth_set_address_mode(uint8_t addr_mode) {
780 if (!mp_bluetooth_is_active()) {
781 mp_raise_OSError(ERRNO_BLUETOOTH_NOT_ACTIVE);
782 }
783
784 switch (addr_mode) {
785 case MP_BLUETOOTH_ADDRESS_MODE_PUBLIC: {
786 DEBUG_printf("mp_bluetooth_set_address_mode: public\n");
787 if (!set_public_address()) {
788 // No public address available.
789 mp_raise_OSError(MP_EINVAL);
790 }
791 break;
792 }
793 case MP_BLUETOOTH_ADDRESS_MODE_RANDOM: {
794 DEBUG_printf("mp_bluetooth_set_address_mode: random\n");
795 set_random_address();
796 break;
797 }
798 case MP_BLUETOOTH_ADDRESS_MODE_RPA:
799 case MP_BLUETOOTH_ADDRESS_MODE_NRPA:
800 // Not yet supported.
801 mp_raise_OSError(MP_EINVAL);
802 }
803 }
804
805 #if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING
mp_bluetooth_set_bonding(bool enabled)806 void mp_bluetooth_set_bonding(bool enabled) {
807 if (enabled) {
808 mp_bluetooth_btstack_sm_auth_req |= SM_AUTHREQ_BONDING;
809 } else {
810 mp_bluetooth_btstack_sm_auth_req &= ~SM_AUTHREQ_BONDING;
811 }
812 sm_set_authentication_requirements(mp_bluetooth_btstack_sm_auth_req);
813 }
814
mp_bluetooth_set_mitm_protection(bool enabled)815 void mp_bluetooth_set_mitm_protection(bool enabled) {
816 if (enabled) {
817 mp_bluetooth_btstack_sm_auth_req |= SM_AUTHREQ_MITM_PROTECTION;
818 } else {
819 mp_bluetooth_btstack_sm_auth_req &= ~SM_AUTHREQ_MITM_PROTECTION;
820 }
821 sm_set_authentication_requirements(mp_bluetooth_btstack_sm_auth_req);
822 }
823
mp_bluetooth_set_le_secure(bool enabled)824 void mp_bluetooth_set_le_secure(bool enabled) {
825 if (enabled) {
826 mp_bluetooth_btstack_sm_auth_req |= SM_AUTHREQ_SECURE_CONNECTION;
827 } else {
828 mp_bluetooth_btstack_sm_auth_req &= ~SM_AUTHREQ_SECURE_CONNECTION;
829 }
830 sm_set_authentication_requirements(mp_bluetooth_btstack_sm_auth_req);
831 }
832
mp_bluetooth_set_io_capability(uint8_t capability)833 void mp_bluetooth_set_io_capability(uint8_t capability) {
834 sm_set_io_capabilities(capability);
835 }
836 #endif // MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING
837
mp_bluetooth_gap_get_device_name(const uint8_t ** buf)838 size_t mp_bluetooth_gap_get_device_name(const uint8_t **buf) {
839 uint8_t *value = NULL;
840 size_t value_len = 0;
841 mp_bluetooth_gatts_db_read(MP_STATE_PORT(bluetooth_btstack_root_pointers)->gatts_db, BTSTACK_GAP_DEVICE_NAME_HANDLE, &value, &value_len);
842 *buf = value;
843 return value_len;
844 }
845
mp_bluetooth_gap_set_device_name(const uint8_t * buf,size_t len)846 int mp_bluetooth_gap_set_device_name(const uint8_t *buf, size_t len) {
847 return mp_bluetooth_gatts_db_write(MP_STATE_PORT(bluetooth_btstack_root_pointers)->gatts_db, BTSTACK_GAP_DEVICE_NAME_HANDLE, buf, len);
848 }
849
mp_bluetooth_gap_advertise_start(bool connectable,int32_t interval_us,const uint8_t * adv_data,size_t adv_data_len,const uint8_t * sr_data,size_t sr_data_len)850 int mp_bluetooth_gap_advertise_start(bool connectable, int32_t interval_us, const uint8_t *adv_data, size_t adv_data_len, const uint8_t *sr_data, size_t sr_data_len) {
851 DEBUG_printf("mp_bluetooth_gap_advertise_start\n");
852
853 if (!mp_bluetooth_is_active()) {
854 return ERRNO_BLUETOOTH_NOT_ACTIVE;
855 }
856
857 uint16_t adv_int_min = interval_us / 625;
858 uint16_t adv_int_max = interval_us / 625;
859 uint8_t adv_type = connectable ? 0 : 2;
860 bd_addr_t null_addr = {0};
861
862 uint8_t direct_address_type = 0;
863 uint8_t channel_map = 0x07; // Use all three broadcast channels.
864 uint8_t filter_policy = 0x00; // None.
865
866 gap_advertisements_set_params(adv_int_min, adv_int_max, adv_type, direct_address_type, null_addr, channel_map, filter_policy);
867
868 // Copy the adv_data and sr_data into a persistent buffer (which is findable via the btstack root pointers).
869 size_t total_bytes = adv_data_len + sr_data_len;
870 if (total_bytes > MP_STATE_PORT(bluetooth_btstack_root_pointers)->adv_data_alloc) {
871 // Resize if necessary.
872 MP_STATE_PORT(bluetooth_btstack_root_pointers)->adv_data = m_new(uint8_t, total_bytes);
873 MP_STATE_PORT(bluetooth_btstack_root_pointers)->adv_data_alloc = total_bytes;
874 }
875 uint8_t *data = MP_STATE_PORT(bluetooth_btstack_root_pointers)->adv_data;
876
877 if (adv_data) {
878 memcpy(data, (uint8_t *)adv_data, adv_data_len);
879 gap_advertisements_set_data(adv_data_len, data);
880 data += adv_data_len;
881 }
882 if (sr_data) {
883 memcpy(data, (uint8_t *)sr_data, sr_data_len);
884 gap_scan_response_set_data(sr_data_len, data);
885 }
886
887 gap_advertisements_enable(true);
888 return 0;
889 }
890
mp_bluetooth_gap_advertise_stop(void)891 void mp_bluetooth_gap_advertise_stop(void) {
892 DEBUG_printf("mp_bluetooth_gap_advertise_stop\n");
893
894 if (!mp_bluetooth_is_active()) {
895 return;
896 }
897
898 gap_advertisements_enable(false);
899 MP_STATE_PORT(bluetooth_btstack_root_pointers)->adv_data_alloc = 0;
900 MP_STATE_PORT(bluetooth_btstack_root_pointers)->adv_data = NULL;
901 }
902
mp_bluetooth_gatts_register_service_begin(bool append)903 int mp_bluetooth_gatts_register_service_begin(bool append) {
904 DEBUG_printf("mp_bluetooth_gatts_register_service_begin\n");
905
906 if (!mp_bluetooth_is_active()) {
907 return ERRNO_BLUETOOTH_NOT_ACTIVE;
908 }
909
910 if (!append) {
911 // This will reset the DB.
912 // Becase the DB is statically allocated, there's no problem with just re-initing it.
913 // Note this would be a memory leak if we enabled HAVE_MALLOC (there's no API to free the existing db).
914 att_db_util_init();
915
916 att_db_util_add_service_uuid16(GAP_SERVICE_UUID);
917 uint16_t handle = att_db_util_add_characteristic_uuid16(GAP_DEVICE_NAME_UUID, ATT_PROPERTY_READ | ATT_PROPERTY_DYNAMIC, ATT_SECURITY_NONE, ATT_SECURITY_NONE, NULL, 0);
918 assert(handle == BTSTACK_GAP_DEVICE_NAME_HANDLE);
919 (void)handle;
920
921 att_db_util_add_service_uuid16(0x1801);
922 att_db_util_add_characteristic_uuid16(0x2a05, ATT_PROPERTY_READ, ATT_SECURITY_NONE, ATT_SECURITY_NONE, NULL, 0);
923 }
924
925 return 0;
926 }
927
att_read_callback(hci_con_handle_t connection_handle,uint16_t att_handle,uint16_t offset,uint8_t * buffer,uint16_t buffer_size)928 STATIC uint16_t att_read_callback(hci_con_handle_t connection_handle, uint16_t att_handle, uint16_t offset, uint8_t *buffer, uint16_t buffer_size) {
929 // Should return data length, 0 for error, or -1 for delayed response.
930 // For more details search "*att_read_callback*" in micropython/lib/btstack/doc/manual/docs/profiles.md
931 (void)connection_handle;
932 DEBUG_printf("att_read_callback (handle: %u, offset: %u, buffer: %p, size: %u)\n", att_handle, offset, buffer, buffer_size);
933 mp_bluetooth_gatts_db_entry_t *entry = mp_bluetooth_gatts_db_lookup(MP_STATE_PORT(bluetooth_btstack_root_pointers)->gatts_db, att_handle);
934 if (!entry) {
935 DEBUG_printf("att_read_callback handle not found\n");
936 return 0;
937 }
938
939 // Allow Python code to override value (by using gatts_write), or deny (by returning false) the read.
940 // Note this will be a no-op if the ringbuffer implementation is being used, as the Python callback cannot
941 // be executed synchronously. This is currently always the case for btstack.
942 if ((buffer == NULL) && (buffer_size == 0)) {
943 if (!mp_bluetooth_gatts_on_read_request(connection_handle, att_handle)) {
944 DEBUG_printf("att_read_callback: read request denied\n");
945 return 0;
946 }
947 }
948
949 uint16_t ret = att_read_callback_handle_blob(entry->data, entry->data_len, offset, buffer, buffer_size);
950 return ret;
951 }
952
att_write_callback(hci_con_handle_t connection_handle,uint16_t att_handle,uint16_t transaction_mode,uint16_t offset,uint8_t * buffer,uint16_t buffer_size)953 STATIC int att_write_callback(hci_con_handle_t connection_handle, uint16_t att_handle, uint16_t transaction_mode, uint16_t offset, uint8_t *buffer, uint16_t buffer_size) {
954 (void)offset;
955 (void)transaction_mode;
956 DEBUG_printf("att_write_callback (handle: %u, mode: %u, offset: %u, buffer: %p, size: %u)\n", att_handle, transaction_mode, offset, buffer, buffer_size);
957 mp_bluetooth_gatts_db_entry_t *entry = mp_bluetooth_gatts_db_lookup(MP_STATE_PORT(bluetooth_btstack_root_pointers)->gatts_db, att_handle);
958 if (!entry) {
959 DEBUG_printf("att_write_callback handle not found\n");
960 return 0; // TODO: Find status code for not-found.
961 }
962
963 // TODO: Use `offset` arg.
964 size_t append_offset = 0;
965 if (entry->append) {
966 append_offset = entry->data_len;
967 }
968 entry->data_len = MIN(entry->data_alloc, buffer_size + append_offset);
969 memcpy(entry->data + append_offset, buffer, entry->data_len - append_offset);
970
971 mp_bluetooth_gatts_on_write(connection_handle, att_handle);
972
973 return 0;
974 }
975
get_uuid16(const mp_obj_bluetooth_uuid_t * uuid)976 STATIC inline uint16_t get_uuid16(const mp_obj_bluetooth_uuid_t *uuid) {
977 return (uuid->data[1] << 8) | uuid->data[0];
978 }
979
980 // Map MP_BLUETOOTH_CHARACTERISTIC_FLAG_ values to btstack read/write permission values.
get_characteristic_permissions(uint16_t flags,uint16_t * read_permission,uint16_t * write_permission)981 STATIC void get_characteristic_permissions(uint16_t flags, uint16_t *read_permission, uint16_t *write_permission) {
982 if (flags & MP_BLUETOOTH_CHARACTERISTIC_FLAG_READ_ENCRYPTED) {
983 *read_permission = ATT_SECURITY_ENCRYPTED;
984 } else if (flags & MP_BLUETOOTH_CHARACTERISTIC_FLAG_READ_AUTHENTICATED) {
985 *read_permission = ATT_SECURITY_AUTHENTICATED;
986 } else if (flags & MP_BLUETOOTH_CHARACTERISTIC_FLAG_READ_AUTHORIZED) {
987 *read_permission = ATT_SECURITY_AUTHORIZED;
988 } else {
989 *read_permission = ATT_SECURITY_NONE;
990 }
991
992 if (flags & MP_BLUETOOTH_CHARACTERISTIC_FLAG_WRITE_ENCRYPTED) {
993 *write_permission = ATT_SECURITY_ENCRYPTED;
994 } else if (flags & MP_BLUETOOTH_CHARACTERISTIC_FLAG_WRITE_AUTHENTICATED) {
995 *write_permission = ATT_SECURITY_AUTHENTICATED;
996 } else if (flags & MP_BLUETOOTH_CHARACTERISTIC_FLAG_WRITE_AUTHORIZED) {
997 *write_permission = ATT_SECURITY_AUTHORIZED;
998 } else {
999 *write_permission = ATT_SECURITY_NONE;
1000 }
1001 }
1002
mp_bluetooth_gatts_register_service(mp_obj_bluetooth_uuid_t * service_uuid,mp_obj_bluetooth_uuid_t ** characteristic_uuids,uint16_t * characteristic_flags,mp_obj_bluetooth_uuid_t ** descriptor_uuids,uint16_t * descriptor_flags,uint8_t * num_descriptors,uint16_t * handles,size_t num_characteristics)1003 int mp_bluetooth_gatts_register_service(mp_obj_bluetooth_uuid_t *service_uuid, mp_obj_bluetooth_uuid_t **characteristic_uuids, uint16_t *characteristic_flags, mp_obj_bluetooth_uuid_t **descriptor_uuids, uint16_t *descriptor_flags, uint8_t *num_descriptors, uint16_t *handles, size_t num_characteristics) {
1004 DEBUG_printf("mp_bluetooth_gatts_register_service\n");
1005 // Note: btstack expects BE UUIDs (which it immediately convertes to LE).
1006 // So we have to convert all our modbluetooth LE UUIDs to BE just for the att_db_util_add_* methods (using get_uuid16 above, and reverse_128 from btstackutil.h).
1007
1008 // TODO: btstack's att_db_util_add_* methods have no bounds checking or validation.
1009 // Need some way to prevent additional services being added if we're out of space in the static buffer.
1010
1011 if (service_uuid->type == MP_BLUETOOTH_UUID_TYPE_16) {
1012 att_db_util_add_service_uuid16(get_uuid16(service_uuid));
1013 } else if (service_uuid->type == MP_BLUETOOTH_UUID_TYPE_128) {
1014 uint8_t buffer[16];
1015 reverse_128(service_uuid->data, buffer);
1016 att_db_util_add_service_uuid128(buffer);
1017 } else {
1018 return MP_EINVAL;
1019 }
1020
1021 size_t handle_index = 0;
1022 size_t descriptor_index = 0;
1023 static uint8_t cccb_buf[2] = {0};
1024
1025 for (size_t i = 0; i < num_characteristics; ++i) {
1026 uint16_t props = (characteristic_flags[i] & 0x7f) | ATT_PROPERTY_DYNAMIC;
1027 uint16_t read_permission, write_permission;
1028 get_characteristic_permissions(characteristic_flags[i], &read_permission, &write_permission);
1029 if (characteristic_uuids[i]->type == MP_BLUETOOTH_UUID_TYPE_16) {
1030 handles[handle_index] = att_db_util_add_characteristic_uuid16(get_uuid16(characteristic_uuids[i]), props, read_permission, write_permission, NULL, 0);
1031 } else if (characteristic_uuids[i]->type == MP_BLUETOOTH_UUID_TYPE_128) {
1032 uint8_t buffer[16];
1033 reverse_128(characteristic_uuids[i]->data, buffer);
1034 handles[handle_index] = att_db_util_add_characteristic_uuid128(buffer, props, read_permission, write_permission, NULL, 0);
1035 } else {
1036 return MP_EINVAL;
1037 }
1038 mp_bluetooth_gatts_db_create_entry(MP_STATE_PORT(bluetooth_btstack_root_pointers)->gatts_db, handles[handle_index], MP_BLUETOOTH_DEFAULT_ATTR_LEN);
1039 // If a NOTIFY or INDICATE characteristic is added, then we need to manage a value for the CCCB.
1040 if (props & (ATT_PROPERTY_NOTIFY | ATT_PROPERTY_INDICATE)) {
1041 // btstack creates the CCCB as the next handle.
1042 mp_bluetooth_gatts_db_create_entry(MP_STATE_PORT(bluetooth_btstack_root_pointers)->gatts_db, handles[handle_index] + 1, MP_BLUETOOTH_CCCB_LEN);
1043 int ret = mp_bluetooth_gatts_db_write(MP_STATE_PORT(bluetooth_btstack_root_pointers)->gatts_db, handles[handle_index] + 1, cccb_buf, sizeof(cccb_buf));
1044 if (ret) {
1045 return ret;
1046 }
1047 }
1048 DEBUG_printf("mp_bluetooth_gatts_register_service: Registered char with handle %u\n", handles[handle_index]);
1049 ++handle_index;
1050
1051 for (size_t j = 0; j < num_descriptors[i]; ++j) {
1052 props = (descriptor_flags[descriptor_index] & 0x7f) | ATT_PROPERTY_DYNAMIC;
1053 get_characteristic_permissions(descriptor_flags[descriptor_index], &read_permission, &write_permission);
1054
1055 if (descriptor_uuids[descriptor_index]->type == MP_BLUETOOTH_UUID_TYPE_16) {
1056 handles[handle_index] = att_db_util_add_descriptor_uuid16(get_uuid16(descriptor_uuids[descriptor_index]), props, read_permission, write_permission, NULL, 0);
1057 } else if (descriptor_uuids[descriptor_index]->type == MP_BLUETOOTH_UUID_TYPE_128) {
1058 uint8_t buffer[16];
1059 reverse_128(descriptor_uuids[descriptor_index]->data, buffer);
1060 handles[handle_index] = att_db_util_add_descriptor_uuid128(buffer, props, read_permission, write_permission, NULL, 0);
1061 } else {
1062 return MP_EINVAL;
1063 }
1064 mp_bluetooth_gatts_db_create_entry(MP_STATE_PORT(bluetooth_btstack_root_pointers)->gatts_db, handles[handle_index], MP_BLUETOOTH_DEFAULT_ATTR_LEN);
1065 DEBUG_printf("mp_bluetooth_gatts_register_service: Registered desc with handle %u\n", handles[handle_index]);
1066 ++descriptor_index;
1067 ++handle_index;
1068 }
1069 }
1070
1071 return 0;
1072 }
1073
mp_bluetooth_gatts_register_service_end(void)1074 int mp_bluetooth_gatts_register_service_end(void) {
1075 DEBUG_printf("mp_bluetooth_gatts_register_service_end\n");
1076 att_server_init(att_db_util_get_address(), &att_read_callback, &att_write_callback);
1077 return 0;
1078 }
1079
mp_bluetooth_gatts_read(uint16_t value_handle,uint8_t ** value,size_t * value_len)1080 int mp_bluetooth_gatts_read(uint16_t value_handle, uint8_t **value, size_t *value_len) {
1081 DEBUG_printf("mp_bluetooth_gatts_read\n");
1082 if (!mp_bluetooth_is_active()) {
1083 return ERRNO_BLUETOOTH_NOT_ACTIVE;
1084 }
1085 return mp_bluetooth_gatts_db_read(MP_STATE_PORT(bluetooth_btstack_root_pointers)->gatts_db, value_handle, value, value_len);
1086 }
1087
mp_bluetooth_gatts_write(uint16_t value_handle,const uint8_t * value,size_t value_len,bool send_update)1088 int mp_bluetooth_gatts_write(uint16_t value_handle, const uint8_t *value, size_t value_len, bool send_update) {
1089 DEBUG_printf("mp_bluetooth_gatts_write\n");
1090 if (!mp_bluetooth_is_active()) {
1091 return ERRNO_BLUETOOTH_NOT_ACTIVE;
1092 }
1093 if (send_update) {
1094 return MP_EOPNOTSUPP;
1095 }
1096 return mp_bluetooth_gatts_db_write(MP_STATE_PORT(bluetooth_btstack_root_pointers)->gatts_db, value_handle, value, value_len);
1097 }
1098
mp_bluetooth_gatts_notify(uint16_t conn_handle,uint16_t value_handle)1099 int mp_bluetooth_gatts_notify(uint16_t conn_handle, uint16_t value_handle) {
1100 DEBUG_printf("mp_bluetooth_gatts_notify\n");
1101
1102 if (!mp_bluetooth_is_active()) {
1103 return ERRNO_BLUETOOTH_NOT_ACTIVE;
1104 }
1105
1106 // Note: btstack doesn't appear to support sending a notification without a value, so include the stored value.
1107 uint8_t *data = NULL;
1108 size_t len = 0;
1109 mp_bluetooth_gatts_db_read(MP_STATE_PORT(bluetooth_btstack_root_pointers)->gatts_db, value_handle, &data, &len);
1110 return mp_bluetooth_gatts_notify_send(conn_handle, value_handle, data, len);
1111 }
1112
mp_bluetooth_gatts_notify_send(uint16_t conn_handle,uint16_t value_handle,const uint8_t * value,size_t value_len)1113 int mp_bluetooth_gatts_notify_send(uint16_t conn_handle, uint16_t value_handle, const uint8_t *value, size_t value_len) {
1114 DEBUG_printf("mp_bluetooth_gatts_notify_send\n");
1115
1116 if (!mp_bluetooth_is_active()) {
1117 return ERRNO_BLUETOOTH_NOT_ACTIVE;
1118 }
1119
1120 // Attempt to send immediately. If it succeeds, btstack will copy the buffer.
1121 MICROPY_PY_BLUETOOTH_ENTER
1122 int err = att_server_notify(conn_handle, value_handle, value, value_len);
1123 MICROPY_PY_BLUETOOTH_EXIT
1124
1125 if (err == BTSTACK_ACL_BUFFERS_FULL) {
1126 DEBUG_printf("mp_bluetooth_gatts_notify_send: ACL buffer full, scheduling callback\n");
1127 // Schedule callback, making a copy of the buffer.
1128 mp_btstack_pending_op_t *pending_op = btstack_enqueue_pending_operation(MP_BLUETOOTH_BTSTACK_PENDING_NOTIFY, conn_handle, value_handle, value, value_len);
1129
1130 err = att_server_request_to_send_notification(&pending_op->context_registration, conn_handle);
1131
1132 if (err != ERROR_CODE_SUCCESS) {
1133 // Failure. Unref and free the pending operation.
1134 btstack_remove_pending_operation(pending_op, true /* del */);
1135 }
1136
1137 return 0;
1138 } else {
1139 return btstack_error_to_errno(err);
1140 }
1141 }
1142
mp_bluetooth_gatts_indicate(uint16_t conn_handle,uint16_t value_handle)1143 int mp_bluetooth_gatts_indicate(uint16_t conn_handle, uint16_t value_handle) {
1144 DEBUG_printf("mp_bluetooth_gatts_indicate\n");
1145
1146 if (!mp_bluetooth_is_active()) {
1147 return ERRNO_BLUETOOTH_NOT_ACTIVE;
1148 }
1149
1150 uint8_t *data = NULL;
1151 size_t len = 0;
1152 mp_bluetooth_gatts_db_read(MP_STATE_PORT(bluetooth_btstack_root_pointers)->gatts_db, value_handle, &data, &len);
1153
1154 // Indicate will raise ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE when
1155 // acknowledged (or timeout/error).
1156
1157 // Attempt to send immediately, will copy buffer.
1158 MICROPY_PY_BLUETOOTH_ENTER
1159 int err = att_server_indicate(conn_handle, value_handle, data, len);
1160 MICROPY_PY_BLUETOOTH_EXIT
1161
1162 if (err == BTSTACK_ACL_BUFFERS_FULL) {
1163 DEBUG_printf("mp_bluetooth_gatts_indicate: ACL buffer full, scheduling callback\n");
1164 // Schedule callback, making a copy of the buffer.
1165 mp_btstack_pending_op_t *pending_op = btstack_enqueue_pending_operation(MP_BLUETOOTH_BTSTACK_PENDING_INDICATE, conn_handle, value_handle, data, len);
1166
1167 err = att_server_request_to_send_indication(&pending_op->context_registration, conn_handle);
1168
1169 if (err != ERROR_CODE_SUCCESS) {
1170 // Failure. Unref and free the pending operation.
1171 btstack_remove_pending_operation(pending_op, true /* del */);
1172 }
1173
1174 return 0;
1175 } else {
1176 return btstack_error_to_errno(err);
1177 }
1178 }
1179
mp_bluetooth_gatts_set_buffer(uint16_t value_handle,size_t len,bool append)1180 int mp_bluetooth_gatts_set_buffer(uint16_t value_handle, size_t len, bool append) {
1181 DEBUG_printf("mp_bluetooth_gatts_set_buffer\n");
1182 if (!mp_bluetooth_is_active()) {
1183 return ERRNO_BLUETOOTH_NOT_ACTIVE;
1184 }
1185 return mp_bluetooth_gatts_db_resize(MP_STATE_PORT(bluetooth_btstack_root_pointers)->gatts_db, value_handle, len, append);
1186 }
1187
mp_bluetooth_get_preferred_mtu(void)1188 int mp_bluetooth_get_preferred_mtu(void) {
1189 if (!mp_bluetooth_is_active()) {
1190 mp_raise_OSError(ERRNO_BLUETOOTH_NOT_ACTIVE);
1191 }
1192 return l2cap_max_le_mtu();
1193 }
1194
mp_bluetooth_set_preferred_mtu(uint16_t mtu)1195 int mp_bluetooth_set_preferred_mtu(uint16_t mtu) {
1196 if (!mp_bluetooth_is_active()) {
1197 return ERRNO_BLUETOOTH_NOT_ACTIVE;
1198 }
1199 l2cap_set_max_le_mtu(mtu);
1200 if (l2cap_max_le_mtu() != mtu) {
1201 return MP_EINVAL;
1202 }
1203 return 0;
1204 }
1205
mp_bluetooth_gap_disconnect(uint16_t conn_handle)1206 int mp_bluetooth_gap_disconnect(uint16_t conn_handle) {
1207 DEBUG_printf("mp_bluetooth_gap_disconnect\n");
1208 if (!mp_bluetooth_is_active()) {
1209 return ERRNO_BLUETOOTH_NOT_ACTIVE;
1210 }
1211 gap_disconnect(conn_handle);
1212 return 0;
1213 }
1214
1215 #if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING
1216
mp_bluetooth_gap_pair(uint16_t conn_handle)1217 int mp_bluetooth_gap_pair(uint16_t conn_handle) {
1218 DEBUG_printf("mp_bluetooth_gap_pair: conn_handle=%d\n", conn_handle);
1219 sm_request_pairing(conn_handle);
1220 return 0;
1221 }
1222
mp_bluetooth_gap_passkey(uint16_t conn_handle,uint8_t action,mp_int_t passkey)1223 int mp_bluetooth_gap_passkey(uint16_t conn_handle, uint8_t action, mp_int_t passkey) {
1224 DEBUG_printf("mp_bluetooth_gap_passkey: conn_handle=%d action=%d passkey=%d\n", conn_handle, action, (int)passkey);
1225 return MP_EOPNOTSUPP;
1226 }
1227
1228 #endif // MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING
1229
1230 #if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
1231 STATIC btstack_timer_source_t scan_duration_timeout;
1232
scan_duration_timeout_handler(btstack_timer_source_t * ds)1233 STATIC void scan_duration_timeout_handler(btstack_timer_source_t *ds) {
1234 (void)ds;
1235 mp_bluetooth_gap_scan_stop();
1236 }
1237
mp_bluetooth_gap_scan_start(int32_t duration_ms,int32_t interval_us,int32_t window_us,bool active_scan)1238 int mp_bluetooth_gap_scan_start(int32_t duration_ms, int32_t interval_us, int32_t window_us, bool active_scan) {
1239 DEBUG_printf("mp_bluetooth_gap_scan_start\n");
1240
1241 if (!mp_bluetooth_is_active()) {
1242 return ERRNO_BLUETOOTH_NOT_ACTIVE;
1243 }
1244
1245 if (duration_ms > 0) {
1246 btstack_run_loop_set_timer(&scan_duration_timeout, duration_ms);
1247 btstack_run_loop_set_timer_handler(&scan_duration_timeout, scan_duration_timeout_handler);
1248 btstack_run_loop_add_timer(&scan_duration_timeout);
1249 }
1250
1251 gap_set_scan_parameters(active_scan ? 1 : 0, interval_us / 625, window_us / 625);
1252 gap_start_scan();
1253
1254 return 0;
1255 }
1256
mp_bluetooth_gap_scan_stop(void)1257 int mp_bluetooth_gap_scan_stop(void) {
1258 DEBUG_printf("mp_bluetooth_gap_scan_stop\n");
1259 if (!mp_bluetooth_is_active()) {
1260 return ERRNO_BLUETOOTH_NOT_ACTIVE;
1261 }
1262 btstack_run_loop_remove_timer(&scan_duration_timeout);
1263 gap_stop_scan();
1264 mp_bluetooth_gap_on_scan_complete();
1265 return 0;
1266 }
1267
mp_bluetooth_gap_peripheral_connect(uint8_t addr_type,const uint8_t * addr,int32_t duration_ms)1268 int mp_bluetooth_gap_peripheral_connect(uint8_t addr_type, const uint8_t *addr, int32_t duration_ms) {
1269 DEBUG_printf("mp_bluetooth_gap_peripheral_connect\n");
1270
1271 uint16_t conn_scan_interval = 60000 / 625;
1272 uint16_t conn_scan_window = 30000 / 625;
1273 uint16_t conn_interval_min = 10000 / 1250;
1274 uint16_t conn_interval_max = 30000 / 1250;
1275 uint16_t conn_latency = 4;
1276 uint16_t supervision_timeout = duration_ms / 10; // default = 720
1277 uint16_t min_ce_length = 10000 / 625;
1278 uint16_t max_ce_length = 30000 / 625;
1279
1280 gap_set_connection_parameters(conn_scan_interval, conn_scan_window, conn_interval_min, conn_interval_max, conn_latency, supervision_timeout, min_ce_length, max_ce_length);
1281
1282 bd_addr_t btstack_addr;
1283 memcpy(btstack_addr, addr, BD_ADDR_LEN);
1284 return btstack_error_to_errno(gap_connect(btstack_addr, addr_type));
1285 }
1286
1287 #endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
1288
1289 #if MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT
1290
mp_bluetooth_gattc_discover_primary_services(uint16_t conn_handle,const mp_obj_bluetooth_uuid_t * uuid)1291 int mp_bluetooth_gattc_discover_primary_services(uint16_t conn_handle, const mp_obj_bluetooth_uuid_t *uuid) {
1292 DEBUG_printf("mp_bluetooth_gattc_discover_primary_services\n");
1293
1294 if (!mp_bluetooth_is_active()) {
1295 return ERRNO_BLUETOOTH_NOT_ACTIVE;
1296 }
1297
1298 uint8_t err;
1299 if (uuid) {
1300 if (uuid->type == MP_BLUETOOTH_UUID_TYPE_16) {
1301 err = gatt_client_discover_primary_services_by_uuid16(&btstack_packet_handler_discover_services, conn_handle, get_uuid16(uuid));
1302 } else if (uuid->type == MP_BLUETOOTH_UUID_TYPE_128) {
1303 uint8_t buffer[16];
1304 reverse_128(uuid->data, buffer);
1305 err = gatt_client_discover_primary_services_by_uuid128(&btstack_packet_handler_discover_services, conn_handle, buffer);
1306 } else {
1307 DEBUG_printf(" --> unknown UUID size\n");
1308 return MP_EINVAL;
1309 }
1310 } else {
1311 err = gatt_client_discover_primary_services(&btstack_packet_handler_discover_services, conn_handle);
1312 }
1313 return btstack_error_to_errno(err);
1314 }
1315
mp_bluetooth_gattc_discover_characteristics(uint16_t conn_handle,uint16_t start_handle,uint16_t end_handle,const mp_obj_bluetooth_uuid_t * uuid)1316 int mp_bluetooth_gattc_discover_characteristics(uint16_t conn_handle, uint16_t start_handle, uint16_t end_handle, const mp_obj_bluetooth_uuid_t *uuid) {
1317 DEBUG_printf("mp_bluetooth_gattc_discover_characteristics\n");
1318
1319 if (!mp_bluetooth_is_active()) {
1320 return ERRNO_BLUETOOTH_NOT_ACTIVE;
1321 }
1322
1323 gatt_client_service_t service = {
1324 // Only start/end handles needed for gatt_client_discover_characteristics_for_service.
1325 .start_group_handle = start_handle,
1326 .end_group_handle = end_handle,
1327 .uuid16 = 0,
1328 .uuid128 = {0},
1329 };
1330 uint8_t err;
1331 if (uuid) {
1332 if (uuid->type == MP_BLUETOOTH_UUID_TYPE_16) {
1333 err = gatt_client_discover_characteristics_for_service_by_uuid16(&btstack_packet_handler_discover_characteristics, conn_handle, &service, get_uuid16(uuid));
1334 } else if (uuid->type == MP_BLUETOOTH_UUID_TYPE_128) {
1335 uint8_t buffer[16];
1336 reverse_128(uuid->data, buffer);
1337 err = gatt_client_discover_characteristics_for_service_by_uuid128(&btstack_packet_handler_discover_characteristics, conn_handle, &service, buffer);
1338 } else {
1339 DEBUG_printf(" --> unknown UUID size\n");
1340 return MP_EINVAL;
1341 }
1342 } else {
1343 err = gatt_client_discover_characteristics_for_service(&btstack_packet_handler_discover_characteristics, conn_handle, &service);
1344 }
1345 return btstack_error_to_errno(err);
1346 }
1347
mp_bluetooth_gattc_discover_descriptors(uint16_t conn_handle,uint16_t start_handle,uint16_t end_handle)1348 int mp_bluetooth_gattc_discover_descriptors(uint16_t conn_handle, uint16_t start_handle, uint16_t end_handle) {
1349 DEBUG_printf("mp_bluetooth_gattc_discover_descriptors\n");
1350
1351 if (!mp_bluetooth_is_active()) {
1352 return ERRNO_BLUETOOTH_NOT_ACTIVE;
1353 }
1354
1355 gatt_client_characteristic_t characteristic = {
1356 // Only start/end handles needed for gatt_client_discover_characteristic_descriptors.
1357 .start_handle = start_handle,
1358 .value_handle = 0,
1359 .end_handle = end_handle,
1360 .properties = 0,
1361 .uuid16 = 0,
1362 .uuid128 = {0},
1363 };
1364 return btstack_error_to_errno(gatt_client_discover_characteristic_descriptors(&btstack_packet_handler_discover_descriptors, conn_handle, &characteristic));
1365 }
1366
mp_bluetooth_gattc_read(uint16_t conn_handle,uint16_t value_handle)1367 int mp_bluetooth_gattc_read(uint16_t conn_handle, uint16_t value_handle) {
1368 DEBUG_printf("mp_bluetooth_gattc_read\n");
1369 if (!mp_bluetooth_is_active()) {
1370 return ERRNO_BLUETOOTH_NOT_ACTIVE;
1371 }
1372 return btstack_error_to_errno(gatt_client_read_value_of_characteristic_using_value_handle(&btstack_packet_handler_read, conn_handle, value_handle));
1373 }
1374
mp_bluetooth_gattc_write(uint16_t conn_handle,uint16_t value_handle,const uint8_t * value,size_t * value_len,unsigned int mode)1375 int mp_bluetooth_gattc_write(uint16_t conn_handle, uint16_t value_handle, const uint8_t *value, size_t *value_len, unsigned int mode) {
1376 DEBUG_printf("mp_bluetooth_gattc_write\n");
1377
1378 if (!mp_bluetooth_is_active()) {
1379 return ERRNO_BLUETOOTH_NOT_ACTIVE;
1380 }
1381
1382 // We should be distinguishing between gatt_client_write_value_of_characteristic vs
1383 // gatt_client_write_characteristic_descriptor_using_descriptor_handle.
1384 // However both are implemented using send_gatt_write_attribute_value_request under the hood,
1385 // and we get the exact same event to the packet handler.
1386 // Same story for the "without response" version.
1387
1388 int err;
1389 mp_btstack_pending_op_t *pending_op = NULL;
1390
1391 if (mode == MP_BLUETOOTH_WRITE_MODE_NO_RESPONSE) {
1392 // If possible, this will send immediately, copying the buffer directly to the ACL buffer.
1393 err = gatt_client_write_value_of_characteristic_without_response(conn_handle, value_handle, *value_len, (uint8_t *)value);
1394 if (err == GATT_CLIENT_BUSY) {
1395 DEBUG_printf("mp_bluetooth_gattc_write: client busy\n");
1396 // Can't send right now, need to take a copy of the buffer and add it to the queue.
1397 pending_op = btstack_enqueue_pending_operation(MP_BLUETOOTH_BTSTACK_PENDING_WRITE_NO_RESPONSE, conn_handle, value_handle, value, *value_len);
1398 // Notify when this conn_handle can write.
1399 err = gatt_client_request_can_write_without_response_event(&btstack_packet_handler_generic, conn_handle);
1400 } else {
1401 DEBUG_printf("mp_bluetooth_gattc_write: other failure: %d\n", err);
1402 }
1403 } else if (mode == MP_BLUETOOTH_WRITE_MODE_WITH_RESPONSE) {
1404 // Pending operation copies the value buffer and keeps a GC reference
1405 // until the response comes back (there is always a response).
1406 pending_op = btstack_enqueue_pending_operation(MP_BLUETOOTH_BTSTACK_PENDING_WRITE, conn_handle, value_handle, value, *value_len);
1407 err = gatt_client_write_value_of_characteristic(&btstack_packet_handler_write_with_response, conn_handle, value_handle, pending_op->len, pending_op->buf);
1408 } else {
1409 return MP_EINVAL;
1410 }
1411
1412 if (pending_op && err != ERROR_CODE_SUCCESS) {
1413 // Failure. Unref and free the pending operation.
1414 btstack_remove_pending_operation(pending_op, true /* del */);
1415 }
1416
1417 return btstack_error_to_errno(err);
1418 }
1419
mp_bluetooth_gattc_exchange_mtu(uint16_t conn_handle)1420 int mp_bluetooth_gattc_exchange_mtu(uint16_t conn_handle) {
1421 DEBUG_printf("mp_bluetooth_exchange_mtu: conn_handle=%d mtu=%d\n", conn_handle, l2cap_max_le_mtu());
1422
1423 gatt_client_send_mtu_negotiation(&btstack_packet_handler_att_server, conn_handle);
1424
1425 return 0;
1426 }
1427 #endif // MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT
1428
1429 #if MICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS
1430
mp_bluetooth_l2cap_listen(uint16_t psm,uint16_t mtu)1431 int mp_bluetooth_l2cap_listen(uint16_t psm, uint16_t mtu) {
1432 DEBUG_printf("mp_bluetooth_l2cap_listen: psm=%d, mtu=%d\n", psm, mtu);
1433 return MP_EOPNOTSUPP;
1434 }
1435
mp_bluetooth_l2cap_connect(uint16_t conn_handle,uint16_t psm,uint16_t mtu)1436 int mp_bluetooth_l2cap_connect(uint16_t conn_handle, uint16_t psm, uint16_t mtu) {
1437 DEBUG_printf("mp_bluetooth_l2cap_connect: conn_handle=%d, psm=%d, mtu=%d\n", conn_handle, psm, mtu);
1438 return MP_EOPNOTSUPP;
1439 }
1440
mp_bluetooth_l2cap_disconnect(uint16_t conn_handle,uint16_t cid)1441 int mp_bluetooth_l2cap_disconnect(uint16_t conn_handle, uint16_t cid) {
1442 DEBUG_printf("mp_bluetooth_l2cap_disconnect: conn_handle=%d, cid=%d\n", conn_handle, cid);
1443 return MP_EOPNOTSUPP;
1444 }
1445
mp_bluetooth_l2cap_send(uint16_t conn_handle,uint16_t cid,const uint8_t * buf,size_t len,bool * stalled)1446 int mp_bluetooth_l2cap_send(uint16_t conn_handle, uint16_t cid, const uint8_t *buf, size_t len, bool *stalled) {
1447 DEBUG_printf("mp_bluetooth_l2cap_send: conn_handle=%d, cid=%d, len=%d\n", conn_handle, cid, (int)len);
1448 return MP_EOPNOTSUPP;
1449 }
1450
mp_bluetooth_l2cap_recvinto(uint16_t conn_handle,uint16_t cid,uint8_t * buf,size_t * len)1451 int mp_bluetooth_l2cap_recvinto(uint16_t conn_handle, uint16_t cid, uint8_t *buf, size_t *len) {
1452 DEBUG_printf("mp_bluetooth_l2cap_recvinto: conn_handle=%d, cid=%d, len=%d\n", conn_handle, cid, (int)*len);
1453 return MP_EOPNOTSUPP;
1454 }
1455
1456 #endif // MICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS
1457
1458 #endif // MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_BTSTACK
1459