ubluepy_peripheral.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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. #include <string.h>
  27. #include "py/obj.h"
  28. #include "py/runtime.h"
  29. #include "py/objstr.h"
  30. #include "py/objlist.h"
  31. #if MICROPY_PY_UBLUEPY
  32. #include "ble_drv.h"
  33. STATIC void ubluepy_peripheral_print(const mp_print_t *print, mp_obj_t o, mp_print_kind_t kind) {
  34. ubluepy_peripheral_obj_t * self = (ubluepy_peripheral_obj_t *)o;
  35. (void)self;
  36. mp_printf(print, "Peripheral(conn_handle: " HEX2_FMT ")",
  37. self->conn_handle);
  38. }
  39. STATIC void gap_event_handler(mp_obj_t self_in, uint16_t event_id, uint16_t conn_handle, uint16_t length, uint8_t * data) {
  40. ubluepy_peripheral_obj_t *self = MP_OBJ_TO_PTR(self_in);
  41. if (event_id == 16) { // connect event
  42. self->conn_handle = conn_handle;
  43. } else if (event_id == 17) { // disconnect event
  44. self->conn_handle = 0xFFFF; // invalid connection handle
  45. }
  46. if (self->conn_handler != mp_const_none) {
  47. mp_obj_t args[3];
  48. mp_uint_t num_of_args = 3;
  49. args[0] = MP_OBJ_NEW_SMALL_INT(event_id);
  50. args[1] = MP_OBJ_NEW_SMALL_INT(conn_handle);
  51. if (data != NULL) {
  52. args[2] = mp_obj_new_bytearray_by_ref(length, data);
  53. } else {
  54. args[2] = mp_const_none;
  55. }
  56. // for now hard-code all events to conn_handler
  57. mp_call_function_n_kw(self->conn_handler, num_of_args, 0, args);
  58. }
  59. (void)self;
  60. }
  61. STATIC void gatts_event_handler(mp_obj_t self_in, uint16_t event_id, uint16_t attr_handle, uint16_t length, uint8_t * data) {
  62. ubluepy_peripheral_obj_t *self = MP_OBJ_TO_PTR(self_in);
  63. if (self->conn_handler != mp_const_none) {
  64. mp_obj_t args[3];
  65. mp_uint_t num_of_args = 3;
  66. args[0] = MP_OBJ_NEW_SMALL_INT(event_id);
  67. args[1] = MP_OBJ_NEW_SMALL_INT(attr_handle);
  68. if (data != NULL) {
  69. args[2] = mp_obj_new_bytearray_by_ref(length, data);
  70. } else {
  71. args[2] = mp_const_none;
  72. }
  73. // for now hard-code all events to conn_handler
  74. mp_call_function_n_kw(self->conn_handler, num_of_args, 0, args);
  75. }
  76. }
  77. #if MICROPY_PY_UBLUEPY_CENTRAL
  78. static volatile bool m_disc_evt_received;
  79. STATIC void gattc_event_handler(mp_obj_t self_in, uint16_t event_id, uint16_t attr_handle, uint16_t length, uint8_t * data) {
  80. ubluepy_peripheral_obj_t *self = MP_OBJ_TO_PTR(self_in);
  81. (void)self;
  82. m_disc_evt_received = true;
  83. }
  84. #endif
  85. STATIC mp_obj_t ubluepy_peripheral_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
  86. enum {
  87. ARG_NEW_DEVICE_ADDR,
  88. ARG_NEW_ADDR_TYPE
  89. };
  90. static const mp_arg_t allowed_args[] = {
  91. { ARG_NEW_DEVICE_ADDR, MP_ARG_OBJ, {.u_obj = mp_const_none} },
  92. { ARG_NEW_ADDR_TYPE, MP_ARG_OBJ, {.u_obj = mp_const_none} },
  93. };
  94. // parse args
  95. mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
  96. mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
  97. ubluepy_peripheral_obj_t *s = m_new_obj(ubluepy_peripheral_obj_t);
  98. s->base.type = type;
  99. s->delegate = mp_const_none;
  100. s->conn_handler = mp_const_none;
  101. s->notif_handler = mp_const_none;
  102. s->conn_handle = 0xFFFF;
  103. s->service_list = mp_obj_new_list(0, NULL);
  104. return MP_OBJ_FROM_PTR(s);
  105. }
  106. /// \method withDelegate(DefaultDelegate)
  107. /// Set delegate instance for handling Bluetooth LE events.
  108. ///
  109. STATIC mp_obj_t peripheral_with_delegate(mp_obj_t self_in, mp_obj_t delegate) {
  110. ubluepy_peripheral_obj_t *self = MP_OBJ_TO_PTR(self_in);
  111. self->delegate = delegate;
  112. return mp_const_none;
  113. }
  114. STATIC MP_DEFINE_CONST_FUN_OBJ_2(ubluepy_peripheral_with_delegate_obj, peripheral_with_delegate);
  115. /// \method setNotificationHandler(func)
  116. /// Set handler for Bluetooth LE notification events.
  117. ///
  118. STATIC mp_obj_t peripheral_set_notif_handler(mp_obj_t self_in, mp_obj_t func) {
  119. ubluepy_peripheral_obj_t *self = MP_OBJ_TO_PTR(self_in);
  120. self->notif_handler = func;
  121. return mp_const_none;
  122. }
  123. STATIC MP_DEFINE_CONST_FUN_OBJ_2(ubluepy_peripheral_set_notif_handler_obj, peripheral_set_notif_handler);
  124. /// \method setConnectionHandler(func)
  125. /// Set handler for Bluetooth LE connection events.
  126. ///
  127. STATIC mp_obj_t peripheral_set_conn_handler(mp_obj_t self_in, mp_obj_t func) {
  128. ubluepy_peripheral_obj_t *self = MP_OBJ_TO_PTR(self_in);
  129. self->conn_handler = func;
  130. return mp_const_none;
  131. }
  132. STATIC MP_DEFINE_CONST_FUN_OBJ_2(ubluepy_peripheral_set_conn_handler_obj, peripheral_set_conn_handler);
  133. #if MICROPY_PY_UBLUEPY_PERIPHERAL
  134. /// \method advertise(device_name, [service=[service1, service2, ...]], [data=bytearray], [connectable=True])
  135. /// Start advertising. Connectable advertisment type by default.
  136. ///
  137. STATIC mp_obj_t peripheral_advertise(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
  138. static const mp_arg_t allowed_args[] = {
  139. { MP_QSTR_device_name, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
  140. { MP_QSTR_services, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
  141. { MP_QSTR_data, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
  142. { MP_QSTR_connectable, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
  143. };
  144. ubluepy_peripheral_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
  145. self->role = UBLUEPY_ROLE_PERIPHERAL;
  146. // parse args
  147. mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
  148. mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
  149. // ubluepy_peripheral_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
  150. mp_obj_t device_name_obj = args[0].u_obj;
  151. mp_obj_t service_obj = args[1].u_obj;
  152. mp_obj_t data_obj = args[2].u_obj;
  153. mp_obj_t connectable_obj = args[3].u_obj;
  154. ubluepy_advertise_data_t adv_data;
  155. memset(&adv_data, 0, sizeof(ubluepy_advertise_data_t));
  156. if (device_name_obj != mp_const_none && MP_OBJ_IS_STR(device_name_obj)) {
  157. GET_STR_DATA_LEN(device_name_obj, str_data, str_len);
  158. adv_data.p_device_name = (uint8_t *)str_data;
  159. adv_data.device_name_len = str_len;
  160. }
  161. if (service_obj != mp_const_none) {
  162. mp_obj_t * services = NULL;
  163. mp_uint_t num_services;
  164. mp_obj_get_array(service_obj, &num_services, &services);
  165. if (num_services > 0) {
  166. adv_data.p_services = services;
  167. adv_data.num_of_services = num_services;
  168. }
  169. }
  170. if (data_obj != mp_const_none) {
  171. mp_buffer_info_t bufinfo;
  172. mp_get_buffer_raise(data_obj, &bufinfo, MP_BUFFER_READ);
  173. if (bufinfo.len > 0) {
  174. adv_data.p_data = bufinfo.buf;
  175. adv_data.data_len = bufinfo.len;
  176. }
  177. }
  178. adv_data.connectable = true;
  179. if (connectable_obj != mp_const_none && !(mp_obj_is_true(connectable_obj))) {
  180. adv_data.connectable = false;
  181. } else {
  182. ble_drv_gap_event_handler_set(MP_OBJ_FROM_PTR(self), gap_event_handler);
  183. ble_drv_gatts_event_handler_set(MP_OBJ_FROM_PTR(self), gatts_event_handler);
  184. }
  185. (void)ble_drv_advertise_data(&adv_data);
  186. return mp_const_none;
  187. }
  188. STATIC MP_DEFINE_CONST_FUN_OBJ_KW(ubluepy_peripheral_advertise_obj, 0, peripheral_advertise);
  189. /// \method advertise_stop()
  190. /// Stop advertisment if any onging advertisment.
  191. ///
  192. STATIC mp_obj_t peripheral_advertise_stop(mp_obj_t self_in) {
  193. ubluepy_peripheral_obj_t *self = MP_OBJ_TO_PTR(self_in);
  194. (void)self;
  195. ble_drv_advertise_stop();
  196. return mp_const_none;
  197. }
  198. STATIC MP_DEFINE_CONST_FUN_OBJ_1(ubluepy_peripheral_advertise_stop_obj, peripheral_advertise_stop);
  199. #endif // MICROPY_PY_UBLUEPY_PERIPHERAL
  200. /// \method disconnect()
  201. /// disconnect connection.
  202. ///
  203. STATIC mp_obj_t peripheral_disconnect(mp_obj_t self_in) {
  204. ubluepy_peripheral_obj_t *self = MP_OBJ_TO_PTR(self_in);
  205. (void)self;
  206. return mp_const_none;
  207. }
  208. STATIC MP_DEFINE_CONST_FUN_OBJ_1(ubluepy_peripheral_disconnect_obj, peripheral_disconnect);
  209. /// \method addService(Service)
  210. /// Add service to the Peripheral.
  211. ///
  212. STATIC mp_obj_t peripheral_add_service(mp_obj_t self_in, mp_obj_t service) {
  213. ubluepy_peripheral_obj_t * self = MP_OBJ_TO_PTR(self_in);
  214. ubluepy_service_obj_t * p_service = MP_OBJ_TO_PTR(service);
  215. p_service->p_periph = self;
  216. mp_obj_list_append(self->service_list, service);
  217. return mp_const_none;
  218. }
  219. STATIC MP_DEFINE_CONST_FUN_OBJ_2(ubluepy_peripheral_add_service_obj, peripheral_add_service);
  220. /// \method getServices()
  221. /// Return list with all service registered in the Peripheral.
  222. ///
  223. STATIC mp_obj_t peripheral_get_services(mp_obj_t self_in) {
  224. ubluepy_peripheral_obj_t * self = MP_OBJ_TO_PTR(self_in);
  225. return self->service_list;
  226. }
  227. STATIC MP_DEFINE_CONST_FUN_OBJ_1(ubluepy_peripheral_get_services_obj, peripheral_get_services);
  228. #if MICROPY_PY_UBLUEPY_CENTRAL
  229. void static disc_add_service(mp_obj_t self, ble_drv_service_data_t * p_service_data) {
  230. ubluepy_service_obj_t * p_service = m_new_obj(ubluepy_service_obj_t);
  231. p_service->base.type = &ubluepy_service_type;
  232. ubluepy_uuid_obj_t * p_uuid = m_new_obj(ubluepy_uuid_obj_t);
  233. p_uuid->base.type = &ubluepy_uuid_type;
  234. p_service->p_uuid = p_uuid;
  235. p_uuid->type = p_service_data->uuid_type;
  236. p_uuid->value[0] = p_service_data->uuid & 0xFF;
  237. p_uuid->value[1] = p_service_data->uuid >> 8;
  238. p_service->handle = p_service_data->start_handle;
  239. p_service->start_handle = p_service_data->start_handle;
  240. p_service->end_handle = p_service_data->end_handle;
  241. p_service->char_list = mp_obj_new_list(0, NULL);
  242. peripheral_add_service(self, MP_OBJ_FROM_PTR(p_service));
  243. }
  244. void static disc_add_char(mp_obj_t service_in, ble_drv_char_data_t * p_desc_data) {
  245. ubluepy_service_obj_t * p_service = MP_OBJ_TO_PTR(service_in);
  246. ubluepy_characteristic_obj_t * p_char = m_new_obj(ubluepy_characteristic_obj_t);
  247. p_char->base.type = &ubluepy_characteristic_type;
  248. ubluepy_uuid_obj_t * p_uuid = m_new_obj(ubluepy_uuid_obj_t);
  249. p_uuid->base.type = &ubluepy_uuid_type;
  250. p_char->p_uuid = p_uuid;
  251. p_uuid->type = p_desc_data->uuid_type;
  252. p_uuid->value[0] = p_desc_data->uuid & 0xFF;
  253. p_uuid->value[1] = p_desc_data->uuid >> 8;
  254. // add characteristic specific data from discovery
  255. p_char->props = p_desc_data->props;
  256. p_char->handle = p_desc_data->value_handle;
  257. // equivalent to ubluepy_service.c - service_add_characteristic()
  258. // except the registration of the characteristic towards the bluetooth stack
  259. p_char->service_handle = p_service->handle;
  260. p_char->p_service = p_service;
  261. mp_obj_list_append(p_service->char_list, MP_OBJ_FROM_PTR(p_char));
  262. }
  263. /// \method connect(device_address [, addr_type=ADDR_TYPE_PUBLIC])
  264. /// Connect to device peripheral with the given device address.
  265. /// addr_type can be either ADDR_TYPE_PUBLIC (default) or
  266. /// ADDR_TYPE_RANDOM_STATIC.
  267. ///
  268. STATIC mp_obj_t peripheral_connect(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
  269. ubluepy_peripheral_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
  270. mp_obj_t dev_addr = pos_args[1];
  271. self->role = UBLUEPY_ROLE_CENTRAL;
  272. static const mp_arg_t allowed_args[] = {
  273. { MP_QSTR_addr_type, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = UBLUEPY_ADDR_TYPE_PUBLIC } },
  274. };
  275. // parse args
  276. mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
  277. mp_arg_parse_all(n_args - 2, pos_args + 2, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
  278. uint8_t addr_type = args[0].u_int;
  279. ble_drv_gap_event_handler_set(MP_OBJ_FROM_PTR(self), gap_event_handler);
  280. if (MP_OBJ_IS_STR(dev_addr)) {
  281. GET_STR_DATA_LEN(dev_addr, str_data, str_len);
  282. if (str_len == 17) { // Example "11:22:33:aa:bb:cc"
  283. uint8_t * p_addr = m_new(uint8_t, 6);
  284. p_addr[0] = unichar_xdigit_value(str_data[16]);
  285. p_addr[0] += unichar_xdigit_value(str_data[15]) << 4;
  286. p_addr[1] = unichar_xdigit_value(str_data[13]);
  287. p_addr[1] += unichar_xdigit_value(str_data[12]) << 4;
  288. p_addr[2] = unichar_xdigit_value(str_data[10]);
  289. p_addr[2] += unichar_xdigit_value(str_data[9]) << 4;
  290. p_addr[3] = unichar_xdigit_value(str_data[7]);
  291. p_addr[3] += unichar_xdigit_value(str_data[6]) << 4;
  292. p_addr[4] = unichar_xdigit_value(str_data[4]);
  293. p_addr[4] += unichar_xdigit_value(str_data[3]) << 4;
  294. p_addr[5] = unichar_xdigit_value(str_data[1]);
  295. p_addr[5] += unichar_xdigit_value(str_data[0]) << 4;
  296. ble_drv_connect(p_addr, addr_type);
  297. m_del(uint8_t, p_addr, 6);
  298. }
  299. }
  300. // block until connected
  301. while (self->conn_handle == 0xFFFF) {
  302. ;
  303. }
  304. ble_drv_gattc_event_handler_set(MP_OBJ_FROM_PTR(self), gattc_event_handler);
  305. bool service_disc_retval = ble_drv_discover_services(self, self->conn_handle, 0x0001, disc_add_service);
  306. // continue discovery of primary services ...
  307. while (service_disc_retval) {
  308. // locate the last added service
  309. mp_obj_t * services = NULL;
  310. mp_uint_t num_services;
  311. mp_obj_get_array(self->service_list, &num_services, &services);
  312. ubluepy_service_obj_t * p_service = (ubluepy_service_obj_t *)services[num_services - 1];
  313. service_disc_retval = ble_drv_discover_services(self,
  314. self->conn_handle,
  315. p_service->end_handle + 1,
  316. disc_add_service);
  317. }
  318. // For each service perform a characteristic discovery
  319. mp_obj_t * services = NULL;
  320. mp_uint_t num_services;
  321. mp_obj_get_array(self->service_list, &num_services, &services);
  322. for (uint16_t s = 0; s < num_services; s++) {
  323. ubluepy_service_obj_t * p_service = (ubluepy_service_obj_t *)services[s];
  324. bool char_disc_retval = ble_drv_discover_characteristic(p_service,
  325. self->conn_handle,
  326. p_service->start_handle,
  327. p_service->end_handle,
  328. disc_add_char);
  329. // continue discovery of characteristics ...
  330. while (char_disc_retval) {
  331. mp_obj_t * characteristics = NULL;
  332. mp_uint_t num_chars;
  333. mp_obj_get_array(p_service->char_list, &num_chars, &characteristics);
  334. ubluepy_characteristic_obj_t * p_char = (ubluepy_characteristic_obj_t *)characteristics[num_chars - 1];
  335. uint16_t next_handle = p_char->handle + 1;
  336. if ((next_handle) < p_service->end_handle) {
  337. char_disc_retval = ble_drv_discover_characteristic(p_service,
  338. self->conn_handle,
  339. next_handle,
  340. p_service->end_handle,
  341. disc_add_char);
  342. } else {
  343. break;
  344. }
  345. }
  346. }
  347. return mp_const_none;
  348. }
  349. STATIC MP_DEFINE_CONST_FUN_OBJ_KW(ubluepy_peripheral_connect_obj, 2, peripheral_connect);
  350. #endif
  351. STATIC const mp_rom_map_elem_t ubluepy_peripheral_locals_dict_table[] = {
  352. { MP_ROM_QSTR(MP_QSTR_withDelegate), MP_ROM_PTR(&ubluepy_peripheral_with_delegate_obj) },
  353. { MP_ROM_QSTR(MP_QSTR_setNotificationHandler), MP_ROM_PTR(&ubluepy_peripheral_set_notif_handler_obj) },
  354. { MP_ROM_QSTR(MP_QSTR_setConnectionHandler), MP_ROM_PTR(&ubluepy_peripheral_set_conn_handler_obj) },
  355. { MP_ROM_QSTR(MP_QSTR_getServices), MP_ROM_PTR(&ubluepy_peripheral_get_services_obj) },
  356. #if MICROPY_PY_UBLUEPY_CENTRAL
  357. { MP_ROM_QSTR(MP_QSTR_connect), MP_ROM_PTR(&ubluepy_peripheral_connect_obj) },
  358. #if 0
  359. { MP_ROM_QSTR(MP_QSTR_disconnect), MP_ROM_PTR(&ubluepy_peripheral_disconnect_obj) },
  360. { MP_ROM_QSTR(MP_QSTR_getServiceByUUID), MP_ROM_PTR(&ubluepy_peripheral_get_service_by_uuid_obj) },
  361. { MP_ROM_QSTR(MP_QSTR_getCharacteristics), MP_ROM_PTR(&ubluepy_peripheral_get_chars_obj) },
  362. { MP_ROM_QSTR(MP_QSTR_getDescriptors), MP_ROM_PTR(&ubluepy_peripheral_get_descs_obj) },
  363. { MP_ROM_QSTR(MP_QSTR_waitForNotifications), MP_ROM_PTR(&ubluepy_peripheral_wait_for_notif_obj) },
  364. { MP_ROM_QSTR(MP_QSTR_writeCharacteristic), MP_ROM_PTR(&ubluepy_peripheral_write_char_obj) },
  365. { MP_ROM_QSTR(MP_QSTR_readCharacteristic), MP_ROM_PTR(&ubluepy_peripheral_read_char_obj) },
  366. #endif // 0
  367. #endif // MICROPY_PY_UBLUEPY_CENTRAL
  368. #if MICROPY_PY_UBLUEPY_PERIPHERAL
  369. { MP_ROM_QSTR(MP_QSTR_advertise), MP_ROM_PTR(&ubluepy_peripheral_advertise_obj) },
  370. { MP_ROM_QSTR(MP_QSTR_advertise_stop), MP_ROM_PTR(&ubluepy_peripheral_advertise_stop_obj) },
  371. { MP_ROM_QSTR(MP_QSTR_disconnect), MP_ROM_PTR(&ubluepy_peripheral_disconnect_obj) },
  372. { MP_ROM_QSTR(MP_QSTR_addService), MP_ROM_PTR(&ubluepy_peripheral_add_service_obj) },
  373. #if 0
  374. { MP_ROM_QSTR(MP_QSTR_addCharacteristic), MP_ROM_PTR(&ubluepy_peripheral_add_char_obj) },
  375. { MP_ROM_QSTR(MP_QSTR_addDescriptor), MP_ROM_PTR(&ubluepy_peripheral_add_desc_obj) },
  376. { MP_ROM_QSTR(MP_QSTR_writeCharacteristic), MP_ROM_PTR(&ubluepy_peripheral_write_char_obj) },
  377. { MP_ROM_QSTR(MP_QSTR_readCharacteristic), MP_ROM_PTR(&ubluepy_peripheral_read_char_obj) },
  378. #endif
  379. #endif
  380. #if MICROPY_PY_UBLUEPY_BROADCASTER
  381. { MP_ROM_QSTR(MP_QSTR_advertise), MP_ROM_PTR(&ubluepy_peripheral_advertise_obj) },
  382. #endif
  383. #if MICROPY_PY_UBLUEPY_OBSERVER
  384. // Nothing yet.
  385. #endif
  386. };
  387. STATIC MP_DEFINE_CONST_DICT(ubluepy_peripheral_locals_dict, ubluepy_peripheral_locals_dict_table);
  388. const mp_obj_type_t ubluepy_peripheral_type = {
  389. { &mp_type_type },
  390. .name = MP_QSTR_Peripheral,
  391. .print = ubluepy_peripheral_print,
  392. .make_new = ubluepy_peripheral_make_new,
  393. .locals_dict = (mp_obj_dict_t*)&ubluepy_peripheral_locals_dict
  394. };
  395. #endif // MICROPY_PY_UBLUEPY