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