ble_uart.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2017 Glenn Ruben Bakke
  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. #if BLUETOOTH_SD
  27. #include <string.h>
  28. #include "ble_uart.h"
  29. #include "ringbuffer.h"
  30. #include "mphalport.h"
  31. #include "lib/utils/interrupt_char.h"
  32. #if MICROPY_PY_BLE_NUS
  33. static ubluepy_uuid_obj_t uuid_obj_service = {
  34. .base.type = &ubluepy_uuid_type,
  35. .type = UBLUEPY_UUID_128_BIT,
  36. .value = {0x01, 0x00}
  37. };
  38. static ubluepy_uuid_obj_t uuid_obj_char_tx = {
  39. .base.type = &ubluepy_uuid_type,
  40. .type = UBLUEPY_UUID_128_BIT,
  41. .value = {0x03, 0x00}
  42. };
  43. static ubluepy_uuid_obj_t uuid_obj_char_rx = {
  44. .base.type = &ubluepy_uuid_type,
  45. .type = UBLUEPY_UUID_128_BIT,
  46. .value = {0x02, 0x00}
  47. };
  48. static ubluepy_service_obj_t ble_uart_service = {
  49. .base.type = &ubluepy_service_type,
  50. .p_uuid = &uuid_obj_service,
  51. .type = UBLUEPY_SERVICE_PRIMARY
  52. };
  53. static ubluepy_characteristic_obj_t ble_uart_char_rx = {
  54. .base.type = &ubluepy_characteristic_type,
  55. .p_uuid = &uuid_obj_char_rx,
  56. .props = UBLUEPY_PROP_WRITE | UBLUEPY_PROP_WRITE_WO_RESP,
  57. .attrs = 0,
  58. };
  59. static ubluepy_characteristic_obj_t ble_uart_char_tx = {
  60. .base.type = &ubluepy_characteristic_type,
  61. .p_uuid = &uuid_obj_char_tx,
  62. .props = UBLUEPY_PROP_NOTIFY,
  63. .attrs = UBLUEPY_ATTR_CCCD,
  64. };
  65. static ubluepy_peripheral_obj_t ble_uart_peripheral = {
  66. .base.type = &ubluepy_peripheral_type,
  67. .conn_handle = 0xFFFF,
  68. };
  69. static volatile bool m_cccd_enabled;
  70. static volatile bool m_connected;
  71. ringBuffer_typedef(uint8_t, ringbuffer_t);
  72. static ringbuffer_t m_rx_ring_buffer;
  73. static ringbuffer_t * mp_rx_ring_buffer = &m_rx_ring_buffer;
  74. static uint8_t m_rx_ring_buffer_data[128];
  75. static ubluepy_advertise_data_t m_adv_data_uart_service;
  76. #if BLUETOOTH_WEBBLUETOOTH_REPL
  77. static ubluepy_advertise_data_t m_adv_data_eddystone_url;
  78. #endif // BLUETOOTH_WEBBLUETOOTH_REPL
  79. int mp_hal_stdin_rx_chr(void) {
  80. while (!ble_uart_enabled()) {
  81. // wait for connection
  82. }
  83. while (isBufferEmpty(mp_rx_ring_buffer)) {
  84. ;
  85. }
  86. uint8_t byte;
  87. bufferRead(mp_rx_ring_buffer, byte);
  88. return (int)byte;
  89. }
  90. void mp_hal_stdout_tx_strn(const char *str, size_t len) {
  91. // Not connected: drop output
  92. if (!ble_uart_enabled()) return;
  93. uint8_t *buf = (uint8_t *)str;
  94. size_t send_len;
  95. while (len > 0) {
  96. if (len >= 20) {
  97. send_len = 20; // (GATT_MTU_SIZE_DEFAULT - 3)
  98. } else {
  99. send_len = len;
  100. }
  101. ubluepy_characteristic_obj_t * p_char = &ble_uart_char_tx;
  102. ble_drv_attr_s_notify(p_char->p_service->p_periph->conn_handle,
  103. p_char->handle,
  104. send_len,
  105. buf);
  106. len -= send_len;
  107. buf += send_len;
  108. }
  109. }
  110. void mp_hal_stdout_tx_strn_cooked(const char *str, mp_uint_t len) {
  111. mp_hal_stdout_tx_strn(str, len);
  112. }
  113. STATIC void gap_event_handler(mp_obj_t self_in, uint16_t event_id, uint16_t conn_handle, uint16_t length, uint8_t * data) {
  114. ubluepy_peripheral_obj_t * self = MP_OBJ_TO_PTR(self_in);
  115. if (event_id == 16) { // connect event
  116. self->conn_handle = conn_handle;
  117. m_connected = true;
  118. } else if (event_id == 17) { // disconnect event
  119. self->conn_handle = 0xFFFF; // invalid connection handle
  120. m_connected = false;
  121. m_cccd_enabled = false;
  122. ble_uart_advertise();
  123. }
  124. }
  125. STATIC void gatts_event_handler(mp_obj_t self_in, uint16_t event_id, uint16_t attr_handle, uint16_t length, uint8_t * data) {
  126. ubluepy_peripheral_obj_t * self = MP_OBJ_TO_PTR(self_in);
  127. (void)self;
  128. if (event_id == 80) { // gatts write
  129. if (ble_uart_char_tx.cccd_handle == attr_handle) {
  130. m_cccd_enabled = true;
  131. } else if (ble_uart_char_rx.handle == attr_handle) {
  132. for (uint16_t i = 0; i < length; i++) {
  133. #if MICROPY_KBD_EXCEPTION
  134. if (data[i] == mp_interrupt_char) {
  135. mp_keyboard_interrupt();
  136. m_rx_ring_buffer.start = 0;
  137. m_rx_ring_buffer.end = 0;
  138. } else
  139. #endif
  140. {
  141. bufferWrite(mp_rx_ring_buffer, data[i]);
  142. }
  143. }
  144. }
  145. }
  146. }
  147. void ble_uart_init0(void) {
  148. uint8_t base_uuid[] = {0x9E, 0xCA, 0xDC, 0x24, 0x0E, 0xE5, 0xA9, 0xE0, 0x93, 0xF3, 0xA3, 0xB5, 0x00, 0x00, 0x40, 0x6E};
  149. uint8_t uuid_vs_idx;
  150. (void)ble_drv_uuid_add_vs(base_uuid, &uuid_vs_idx);
  151. uuid_obj_service.uuid_vs_idx = uuid_vs_idx;
  152. uuid_obj_char_tx.uuid_vs_idx = uuid_vs_idx;
  153. uuid_obj_char_rx.uuid_vs_idx = uuid_vs_idx;
  154. (void)ble_drv_service_add(&ble_uart_service);
  155. ble_uart_service.char_list = mp_obj_new_list(0, NULL);
  156. // add TX characteristic
  157. ble_uart_char_tx.service_handle = ble_uart_service.handle;
  158. bool retval = ble_drv_characteristic_add(&ble_uart_char_tx);
  159. if (retval) {
  160. ble_uart_char_tx.p_service = &ble_uart_service;
  161. }
  162. mp_obj_list_append(ble_uart_service.char_list, MP_OBJ_FROM_PTR(&ble_uart_char_tx));
  163. // add RX characteristic
  164. ble_uart_char_rx.service_handle = ble_uart_service.handle;
  165. retval = ble_drv_characteristic_add(&ble_uart_char_rx);
  166. if (retval) {
  167. ble_uart_char_rx.p_service = &ble_uart_service;
  168. }
  169. mp_obj_list_append(ble_uart_service.char_list, MP_OBJ_FROM_PTR(&ble_uart_char_rx));
  170. // setup the peripheral
  171. ble_uart_peripheral.service_list = mp_obj_new_list(0, NULL);
  172. mp_obj_list_append(ble_uart_peripheral.service_list, MP_OBJ_FROM_PTR(&ble_uart_service));
  173. ble_uart_service.p_periph = &ble_uart_peripheral;
  174. ble_drv_gap_event_handler_set(MP_OBJ_FROM_PTR(&ble_uart_peripheral), gap_event_handler);
  175. ble_drv_gatts_event_handler_set(MP_OBJ_FROM_PTR(&ble_uart_peripheral), gatts_event_handler);
  176. ble_uart_peripheral.conn_handle = 0xFFFF;
  177. char device_name[] = "mpus";
  178. mp_obj_t service_list = mp_obj_new_list(0, NULL);
  179. mp_obj_list_append(service_list, MP_OBJ_FROM_PTR(&ble_uart_service));
  180. mp_obj_t * services = NULL;
  181. mp_uint_t num_services;
  182. mp_obj_get_array(service_list, &num_services, &services);
  183. m_adv_data_uart_service.p_services = services;
  184. m_adv_data_uart_service.num_of_services = num_services;
  185. m_adv_data_uart_service.p_device_name = (uint8_t *)device_name;
  186. m_adv_data_uart_service.device_name_len = strlen(device_name);
  187. m_adv_data_uart_service.connectable = true;
  188. m_adv_data_uart_service.p_data = NULL;
  189. #if BLUETOOTH_WEBBLUETOOTH_REPL
  190. // for now point eddystone URL to https://goo.gl/F7fZ69 => https://aykevl.nl/apps/nus/
  191. static uint8_t eddystone_url_data[27] = {0x2, 0x1, 0x6,
  192. 0x3, 0x3, 0xaa, 0xfe,
  193. 19, 0x16, 0xaa, 0xfe, 0x10, 0xee, 0x3, 'g', 'o', 'o', '.', 'g', 'l', '/', 'F', '7', 'f', 'Z', '6', '9'};
  194. // eddystone url adv data
  195. m_adv_data_eddystone_url.p_data = eddystone_url_data;
  196. m_adv_data_eddystone_url.data_len = sizeof(eddystone_url_data);
  197. m_adv_data_eddystone_url.connectable = false;
  198. #endif
  199. m_cccd_enabled = false;
  200. // initialize ring buffer
  201. m_rx_ring_buffer.size = sizeof(m_rx_ring_buffer_data) + 1;
  202. m_rx_ring_buffer.start = 0;
  203. m_rx_ring_buffer.end = 0;
  204. m_rx_ring_buffer.elems = m_rx_ring_buffer_data;
  205. m_connected = false;
  206. ble_uart_advertise();
  207. }
  208. void ble_uart_advertise(void) {
  209. #if BLUETOOTH_WEBBLUETOOTH_REPL
  210. while (!m_connected) {
  211. (void)ble_drv_advertise_data(&m_adv_data_uart_service);
  212. mp_hal_delay_ms(500);
  213. (void)ble_drv_advertise_data(&m_adv_data_eddystone_url);
  214. mp_hal_delay_ms(500);
  215. }
  216. ble_drv_advertise_stop();
  217. #else
  218. (void)ble_drv_advertise_data(&m_adv_data_uart_service);
  219. #endif // BLUETOOTH_WEBBLUETOOTH_REPL
  220. }
  221. bool ble_uart_connected(void) {
  222. return (m_connected);
  223. }
  224. bool ble_uart_enabled(void) {
  225. return (m_cccd_enabled);
  226. }
  227. #endif // MICROPY_PY_BLE_NUS
  228. #endif // BLUETOOTH_SD