1/*
2 * This file is part of the MicroPython project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2020 Jim Mussared
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 <pthread.h>
28#include <unistd.h>
29
30#include "py/runtime.h"
31#include "py/mperrno.h"
32#include "py/mphal.h"
33
34#if MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_BTSTACK && MICROPY_BLUETOOTH_BTSTACK_USB
35
36#include "lib/btstack/src/btstack.h"
37#include "lib/btstack/platform/embedded/btstack_run_loop_embedded.h"
38#include "lib/btstack/platform/embedded/hal_cpu.h"
39#include "lib/btstack/platform/embedded/hal_time_ms.h"
40
41#include "extmod/btstack/modbluetooth_btstack.h"
42
43#include "mpbtstackport.h"
44
45#if !MICROPY_PY_THREAD
46#error Unix btstack requires MICROPY_PY_THREAD
47#endif
48
49STATIC const useconds_t USB_POLL_INTERVAL_US = 1000;
50
51void mp_bluetooth_btstack_port_init_usb(void) {
52 // MICROPYBTUSB can be a ':'' or '-' separated port list.
53 char *path = getenv("MICROPYBTUSB");
54 if (path != NULL) {
55 uint8_t usb_path[7] = {0};
56 size_t usb_path_len = 0;
57
58 while (usb_path_len < MP_ARRAY_SIZE(usb_path)) {
59 char *delimiter;
60 usb_path[usb_path_len++] = strtol(path, &delimiter, 16);
61 if (!delimiter || (*delimiter != ':' && *delimiter != '-')) {
62 break;
63 }
64 path = delimiter + 1;
65 }
66
67 hci_transport_usb_set_path(usb_path_len, usb_path);
68 }
69
70 hci_init(hci_transport_usb_instance(), NULL);
71}
72
73STATIC pthread_t bstack_thread_id;
74
75void mp_bluetooth_btstack_port_deinit(void) {
76 hci_power_control(HCI_POWER_OFF);
77
78 // Wait for the poll loop to terminate when the state is set to OFF.
79 pthread_join(bstack_thread_id, NULL);
80}
81
82
83// Provided by mpbstackport_common.c.
84extern bool mp_bluetooth_hci_poll(void);
85
86STATIC void *btstack_thread(void *arg) {
87 (void)arg;
88 hci_power_control(HCI_POWER_ON);
89
90 // modbluetooth_btstack.c will have set the state to STARTING before
91 // calling mp_bluetooth_btstack_port_start.
92 // This loop will terminate when the HCI_POWER_OFF above results
93 // in modbluetooth_btstack.c setting the state back to OFF.
94 // Or, if a timeout results in it being set to TIMEOUT.
95
96 while (true) {
97 if (!mp_bluetooth_hci_poll()) {
98 break;
99 }
100
101 // The USB transport schedules events to the run loop at 1ms intervals,
102 // and the implementation currently polls rather than selects.
103 usleep(USB_POLL_INTERVAL_US);
104 }
105 return NULL;
106}
107
108void mp_bluetooth_btstack_port_start(void) {
109 // Create a thread to run the btstack loop.
110 pthread_attr_t attr;
111 pthread_attr_init(&attr);
112 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
113 pthread_create(&bstack_thread_id, &attr, &btstack_thread, NULL);
114}
115
116#endif // MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_BTSTACK && MICROPY_BLUETOOTH_BTSTACK_USB
117