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 "py/runtime.h" |
28 | #include "py/mperrno.h" |
29 | #include "py/mphal.h" |
30 | |
31 | #if MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_NIMBLE |
32 | |
33 | #include "nimble/nimble_npl.h" |
34 | |
35 | #include "extmod/nimble/modbluetooth_nimble.h" |
36 | #include "extmod/nimble/hal/hal_uart.h" |
37 | |
38 | #define DEBUG_printf(...) // printf(__VA_ARGS__) |
39 | |
40 | // Called by the UART polling thread in mpbthciport.c. |
41 | bool mp_bluetooth_hci_poll(void) { |
42 | // DEBUG_printf("mp_bluetooth_hci_poll (unix nimble) %d\n", mp_bluetooth_nimble_ble_state); |
43 | |
44 | if (mp_bluetooth_nimble_ble_state == MP_BLUETOOTH_NIMBLE_BLE_STATE_OFF) { |
45 | DEBUG_printf("mp_bluetooth_hci_poll (unix nimble) -- shutdown\n" ); |
46 | return false; |
47 | } |
48 | |
49 | if (mp_bluetooth_nimble_ble_state >= MP_BLUETOOTH_NIMBLE_BLE_STATE_WAITING_FOR_SYNC) { |
50 | // Run any timers. |
51 | mp_bluetooth_nimble_os_callout_process(); |
52 | |
53 | // Process incoming UART data, and run events as they are generated. |
54 | mp_bluetooth_nimble_hci_uart_process(true); |
55 | |
56 | // Run any remaining events (e.g. if there was no UART data). |
57 | mp_bluetooth_nimble_os_eventq_run_all(); |
58 | } |
59 | |
60 | return true; |
61 | } |
62 | |
63 | bool mp_bluetooth_hci_active(void) { |
64 | return mp_bluetooth_nimble_ble_state != MP_BLUETOOTH_NIMBLE_BLE_STATE_OFF; |
65 | } |
66 | |
67 | // Extra port-specific helpers. |
68 | void mp_bluetooth_nimble_hci_uart_wfi(void) { |
69 | // This is called while NimBLE is waiting in ble_npl_sem_pend, i.e. waiting for an HCI ACK. |
70 | // Do not need to run events here, only processing incoming HCI data. |
71 | mp_bluetooth_nimble_hci_uart_process(false); |
72 | } |
73 | |
74 | #endif // MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_NIMBLE |
75 | |