2017-02-05 15:10:06 -05:00
|
|
|
/*
|
|
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2017 Glenn Ruben Bakke
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2017-02-12 09:17:30 -05:00
|
|
|
#include <string.h>
|
2017-02-05 15:10:06 -05:00
|
|
|
#include "py/obj.h"
|
2017-02-08 12:48:35 -05:00
|
|
|
#include "py/runtime.h"
|
2017-02-11 11:43:47 -05:00
|
|
|
#include "py/objstr.h"
|
2017-02-18 11:57:10 -05:00
|
|
|
#include "py/objlist.h"
|
2017-02-07 16:17:55 -05:00
|
|
|
|
2017-02-08 12:48:35 -05:00
|
|
|
#if MICROPY_PY_UBLUEPY
|
2017-02-05 15:10:06 -05:00
|
|
|
|
2017-02-17 13:10:24 -05:00
|
|
|
#include "ble_drv.h"
|
2017-02-11 11:43:47 -05:00
|
|
|
|
2017-02-07 16:17:55 -05:00
|
|
|
STATIC void ubluepy_peripheral_print(const mp_print_t *print, mp_obj_t o, mp_print_kind_t kind) {
|
2017-02-08 12:48:35 -05:00
|
|
|
ubluepy_peripheral_obj_t * self = (ubluepy_peripheral_obj_t *)o;
|
|
|
|
(void)self;
|
2017-03-30 16:46:45 -04:00
|
|
|
mp_printf(print, "Peripheral(conn_handle: " HEX2_FMT ")",
|
|
|
|
self->conn_handle);
|
2017-02-07 16:17:55 -05:00
|
|
|
}
|
|
|
|
|
2017-02-17 11:32:42 -05:00
|
|
|
STATIC void gap_event_handler(mp_obj_t self_in, uint16_t event_id, uint16_t conn_handle, uint16_t length, uint8_t * data) {
|
2017-02-15 17:32:42 -05:00
|
|
|
ubluepy_peripheral_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
|
2017-02-18 07:17:31 -05:00
|
|
|
if (event_id == 16) { // connect event
|
|
|
|
self->conn_handle = conn_handle;
|
|
|
|
} else if (event_id == 17) { // disconnect event
|
|
|
|
self->conn_handle = 0xFFFF; // invalid connection handle
|
|
|
|
}
|
|
|
|
|
2017-02-16 12:49:47 -05:00
|
|
|
if (self->conn_handler != mp_const_none) {
|
2017-02-18 08:41:46 -05:00
|
|
|
mp_obj_t args[3];
|
|
|
|
mp_uint_t num_of_args = 3;
|
2017-02-16 12:49:47 -05:00
|
|
|
args[0] = MP_OBJ_NEW_SMALL_INT(event_id);
|
2017-03-02 18:04:13 -05:00
|
|
|
args[1] = MP_OBJ_NEW_SMALL_INT(conn_handle);
|
2017-02-16 12:49:47 -05:00
|
|
|
if (data != NULL) {
|
2017-02-18 08:41:46 -05:00
|
|
|
args[2] = mp_obj_new_bytearray_by_ref(length, data);
|
2017-02-16 12:49:47 -05:00
|
|
|
} else {
|
2017-02-18 08:41:46 -05:00
|
|
|
args[2] = mp_const_none;
|
2017-02-16 12:49:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// for now hard-code all events to conn_handler
|
|
|
|
mp_call_function_n_kw(self->conn_handler, num_of_args, 0, args);
|
2017-02-15 17:32:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
(void)self;
|
|
|
|
}
|
|
|
|
|
2017-02-18 18:10:27 -05:00
|
|
|
STATIC void gatts_event_handler(mp_obj_t self_in, uint16_t event_id, uint16_t attr_handle, uint16_t length, uint8_t * data) {
|
|
|
|
ubluepy_peripheral_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
|
|
|
|
if (self->conn_handler != mp_const_none) {
|
|
|
|
mp_obj_t args[3];
|
|
|
|
mp_uint_t num_of_args = 3;
|
|
|
|
args[0] = MP_OBJ_NEW_SMALL_INT(event_id);
|
2017-03-02 18:04:13 -05:00
|
|
|
args[1] = MP_OBJ_NEW_SMALL_INT(attr_handle);
|
2017-02-18 18:10:27 -05:00
|
|
|
if (data != NULL) {
|
|
|
|
args[2] = mp_obj_new_bytearray_by_ref(length, data);
|
|
|
|
} else {
|
|
|
|
args[2] = mp_const_none;
|
|
|
|
}
|
|
|
|
|
|
|
|
// for now hard-code all events to conn_handler
|
|
|
|
mp_call_function_n_kw(self->conn_handler, num_of_args, 0, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-03-28 17:40:28 -04:00
|
|
|
#if MICROPY_PY_UBLUEPY_CENTRAL
|
|
|
|
|
|
|
|
static volatile bool m_disc_evt_received;
|
|
|
|
|
|
|
|
STATIC void gattc_event_handler(mp_obj_t self_in, uint16_t event_id, uint16_t attr_handle, uint16_t length, uint8_t * data) {
|
|
|
|
ubluepy_peripheral_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
(void)self;
|
|
|
|
m_disc_evt_received = true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-02-07 16:17:55 -05:00
|
|
|
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) {
|
2017-02-08 12:48:35 -05:00
|
|
|
enum {
|
|
|
|
ARG_NEW_DEVICE_ADDR,
|
|
|
|
ARG_NEW_ADDR_TYPE
|
|
|
|
};
|
|
|
|
|
2017-02-07 16:17:55 -05:00
|
|
|
static const mp_arg_t allowed_args[] = {
|
2017-02-15 17:32:42 -05:00
|
|
|
{ ARG_NEW_DEVICE_ADDR, MP_ARG_OBJ, {.u_obj = mp_const_none} },
|
|
|
|
{ ARG_NEW_ADDR_TYPE, MP_ARG_OBJ, {.u_obj = mp_const_none} },
|
2017-02-07 16:17:55 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// parse args
|
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
|
|
|
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
|
|
|
|
|
|
|
ubluepy_peripheral_obj_t *s = m_new_obj(ubluepy_peripheral_obj_t);
|
|
|
|
s->base.type = type;
|
|
|
|
|
2017-02-17 13:02:24 -05:00
|
|
|
ble_drv_gap_event_handler_set(MP_OBJ_FROM_PTR(s), gap_event_handler);
|
2017-02-18 18:10:27 -05:00
|
|
|
ble_drv_gatts_event_handler_set(MP_OBJ_FROM_PTR(s), gatts_event_handler);
|
2017-03-28 17:40:28 -04:00
|
|
|
#if MICROPY_PY_UBLUEPY_CENTRAL
|
|
|
|
ble_drv_gattc_event_handler_set(MP_OBJ_FROM_PTR(s), gattc_event_handler);
|
|
|
|
#endif
|
2017-02-07 16:17:55 -05:00
|
|
|
|
2017-02-16 12:49:47 -05:00
|
|
|
s->delegate = mp_const_none;
|
|
|
|
s->conn_handler = mp_const_none;
|
|
|
|
s->notif_handler = mp_const_none;
|
2017-02-17 11:32:42 -05:00
|
|
|
s->conn_handle = 0xFFFF;
|
2017-02-16 12:49:47 -05:00
|
|
|
|
2017-02-18 11:57:10 -05:00
|
|
|
s->service_list = mp_obj_new_list(0, NULL);
|
|
|
|
|
2017-02-15 17:32:42 -05:00
|
|
|
return MP_OBJ_FROM_PTR(s);
|
2017-02-14 18:22:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// \method withDelegate(DefaultDelegate)
|
|
|
|
/// Set delegate instance for handling Bluetooth LE events.
|
|
|
|
///
|
|
|
|
STATIC mp_obj_t peripheral_with_delegate(mp_obj_t self_in, mp_obj_t delegate) {
|
|
|
|
ubluepy_peripheral_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
|
|
|
|
self->delegate = delegate;
|
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(ubluepy_peripheral_with_delegate_obj, peripheral_with_delegate);
|
|
|
|
|
2017-02-15 13:30:45 -05:00
|
|
|
/// \method setNotificationHandler(func)
|
|
|
|
/// Set handler for Bluetooth LE notification events.
|
|
|
|
///
|
|
|
|
STATIC mp_obj_t peripheral_set_notif_handler(mp_obj_t self_in, mp_obj_t func) {
|
|
|
|
ubluepy_peripheral_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
|
|
|
|
self->notif_handler = func;
|
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(ubluepy_peripheral_set_notif_handler_obj, peripheral_set_notif_handler);
|
|
|
|
|
|
|
|
/// \method setConnectionHandler(func)
|
|
|
|
/// Set handler for Bluetooth LE connection events.
|
|
|
|
///
|
|
|
|
STATIC mp_obj_t peripheral_set_conn_handler(mp_obj_t self_in, mp_obj_t func) {
|
|
|
|
ubluepy_peripheral_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
|
|
|
|
self->conn_handler = func;
|
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(ubluepy_peripheral_set_conn_handler_obj, peripheral_set_conn_handler);
|
|
|
|
|
2017-02-11 11:43:47 -05:00
|
|
|
/// \method advertise(device_name, [service=[service1, service2, ...]], [data=bytearray])
|
2017-02-08 12:48:35 -05:00
|
|
|
/// Start advertising.
|
|
|
|
///
|
2017-02-11 11:43:47 -05:00
|
|
|
STATIC mp_obj_t peripheral_advertise(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
|
|
|
static const mp_arg_t allowed_args[] = {
|
2017-02-12 08:23:13 -05:00
|
|
|
{ MP_QSTR_device_name, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
|
|
|
|
{ MP_QSTR_services, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
|
|
|
|
{ MP_QSTR_data, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
|
2017-02-11 11:43:47 -05:00
|
|
|
};
|
2017-02-08 12:48:35 -05:00
|
|
|
|
2017-02-11 11:43:47 -05:00
|
|
|
// parse args
|
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
|
|
|
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
|
|
|
|
|
|
|
// ubluepy_peripheral_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
|
|
|
|
mp_obj_t device_name_obj = args[0].u_obj;
|
|
|
|
mp_obj_t service_obj = args[1].u_obj;
|
|
|
|
mp_obj_t data_obj = args[2].u_obj;
|
|
|
|
|
|
|
|
ubluepy_advertise_data_t adv_data;
|
2017-02-12 09:17:30 -05:00
|
|
|
memset(&adv_data, 0, sizeof(ubluepy_advertise_data_t));
|
2017-02-11 11:43:47 -05:00
|
|
|
|
2017-02-12 08:23:13 -05:00
|
|
|
if (device_name_obj != mp_const_none && MP_OBJ_IS_STR(device_name_obj)) {
|
2017-02-11 11:43:47 -05:00
|
|
|
GET_STR_DATA_LEN(device_name_obj, str_data, str_len);
|
|
|
|
|
|
|
|
adv_data.p_device_name = (uint8_t *)str_data;
|
|
|
|
adv_data.device_name_len = str_len;
|
|
|
|
}
|
|
|
|
|
2017-02-12 08:23:13 -05:00
|
|
|
if (service_obj != mp_const_none) {
|
2017-02-12 09:17:30 -05:00
|
|
|
mp_obj_t * services = NULL;
|
2017-02-12 08:23:13 -05:00
|
|
|
mp_uint_t num_services;
|
|
|
|
mp_obj_get_array(service_obj, &num_services, &services);
|
2017-02-12 09:17:30 -05:00
|
|
|
|
|
|
|
if (num_services > 0) {
|
|
|
|
adv_data.p_services = services;
|
|
|
|
adv_data.num_of_services = num_services;
|
|
|
|
}
|
2017-02-11 11:43:47 -05:00
|
|
|
}
|
|
|
|
|
2017-02-12 08:23:13 -05:00
|
|
|
if (data_obj != mp_const_none) {
|
2017-02-11 11:43:47 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-02-17 13:02:24 -05:00
|
|
|
(void)ble_drv_advertise_data(&adv_data);
|
2017-02-08 12:48:35 -05:00
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2017-02-11 11:43:47 -05:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(ubluepy_peripheral_advertise_obj, 1, peripheral_advertise);
|
2017-02-08 12:48:35 -05:00
|
|
|
|
|
|
|
/// \method disconnect()
|
|
|
|
/// disconnect connection.
|
|
|
|
///
|
|
|
|
STATIC mp_obj_t peripheral_disconnect(mp_obj_t self_in) {
|
|
|
|
ubluepy_peripheral_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
|
|
|
|
(void)self;
|
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(ubluepy_peripheral_disconnect_obj, peripheral_disconnect);
|
|
|
|
|
|
|
|
/// \method addService(Service)
|
2017-02-18 12:16:07 -05:00
|
|
|
/// Add service to the Peripheral.
|
2017-02-08 12:48:35 -05:00
|
|
|
///
|
2017-02-18 07:17:31 -05:00
|
|
|
STATIC mp_obj_t peripheral_add_service(mp_obj_t self_in, mp_obj_t service) {
|
|
|
|
ubluepy_peripheral_obj_t * self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
ubluepy_service_obj_t * p_service = MP_OBJ_TO_PTR(service);
|
2017-02-08 12:48:35 -05:00
|
|
|
|
2017-02-18 07:17:31 -05:00
|
|
|
p_service->p_periph = self;
|
2017-02-08 12:48:35 -05:00
|
|
|
|
2017-02-18 11:57:10 -05:00
|
|
|
mp_obj_list_append(self->service_list, service);
|
|
|
|
|
2017-02-08 12:48:35 -05:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(ubluepy_peripheral_add_service_obj, peripheral_add_service);
|
|
|
|
|
2017-02-18 12:12:37 -05:00
|
|
|
/// \method getServices()
|
2017-02-18 12:16:07 -05:00
|
|
|
/// Return list with all service registered in the Peripheral.
|
2017-02-18 11:57:10 -05:00
|
|
|
///
|
|
|
|
STATIC mp_obj_t peripheral_get_services(mp_obj_t self_in) {
|
|
|
|
ubluepy_peripheral_obj_t * self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
|
|
|
|
return self->service_list;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(ubluepy_peripheral_get_services_obj, peripheral_get_services);
|
2017-02-08 12:48:35 -05:00
|
|
|
|
2017-02-06 14:19:06 -05:00
|
|
|
|
2017-03-29 17:37:32 -04:00
|
|
|
void static disc_add_service(mp_obj_t self, ble_drv_service_data_t * p_service_data) {
|
|
|
|
ubluepy_service_obj_t * p_service = m_new_obj(ubluepy_service_obj_t);
|
|
|
|
p_service->base.type = &ubluepy_service_type;
|
|
|
|
|
|
|
|
ubluepy_uuid_obj_t * p_uuid = m_new_obj(ubluepy_uuid_obj_t);
|
|
|
|
p_uuid->base.type = &ubluepy_uuid_type;
|
|
|
|
|
|
|
|
p_service->p_uuid = p_uuid;
|
|
|
|
|
|
|
|
p_uuid->type = p_service_data->uuid_type;
|
|
|
|
p_uuid->value[0] = p_service_data->uuid & 0xFF;
|
|
|
|
p_uuid->value[1] = p_service_data->uuid >> 8;
|
|
|
|
|
2017-03-30 16:46:45 -04:00
|
|
|
p_service->handle = p_service_data->start_handle;
|
|
|
|
p_service->start_handle = p_service_data->start_handle;
|
|
|
|
p_service->end_handle = p_service_data->end_handle;
|
|
|
|
|
|
|
|
p_service->char_list = mp_obj_new_list(0, NULL);
|
2017-03-29 17:37:32 -04:00
|
|
|
|
|
|
|
peripheral_add_service(self, MP_OBJ_FROM_PTR(p_service));
|
|
|
|
}
|
|
|
|
|
2017-03-30 16:46:45 -04:00
|
|
|
void static disc_add_char(mp_obj_t service_in, ble_drv_char_data_t * p_desc_data) {
|
2017-03-30 16:53:25 -04:00
|
|
|
ubluepy_service_obj_t * p_service = MP_OBJ_TO_PTR(service_in);
|
2017-03-30 16:46:45 -04:00
|
|
|
ubluepy_characteristic_obj_t * p_char = m_new_obj(ubluepy_characteristic_obj_t);
|
|
|
|
p_char->base.type = &ubluepy_characteristic_type;
|
|
|
|
|
|
|
|
ubluepy_uuid_obj_t * p_uuid = m_new_obj(ubluepy_uuid_obj_t);
|
|
|
|
p_uuid->base.type = &ubluepy_uuid_type;
|
|
|
|
|
|
|
|
p_char->p_uuid = p_uuid;
|
|
|
|
|
|
|
|
p_uuid->type = p_desc_data->uuid_type;
|
|
|
|
p_uuid->value[0] = p_desc_data->uuid & 0xFF;
|
|
|
|
p_uuid->value[1] = p_desc_data->uuid >> 8;
|
|
|
|
|
|
|
|
// add characteristic specific data from discovery
|
|
|
|
p_char->props = p_desc_data->props;
|
|
|
|
p_char->handle = p_desc_data->value_handle;
|
|
|
|
|
|
|
|
// equivalent to ubluepy_service.c - service_add_characteristic()
|
|
|
|
// except the registration of the characteristic towards the bluetooth stack
|
|
|
|
p_char->service_handle = p_service->handle;
|
|
|
|
p_char->p_service = p_service;
|
|
|
|
|
|
|
|
mp_obj_list_append(p_service->char_list, MP_OBJ_FROM_PTR(p_char));
|
|
|
|
}
|
2017-03-29 17:37:32 -04:00
|
|
|
|
2017-03-25 12:51:52 -04:00
|
|
|
/// \method connect(device_address)
|
|
|
|
/// Connect to device peripheral with the given device address.
|
|
|
|
///
|
|
|
|
STATIC mp_obj_t peripheral_connect(mp_obj_t self_in, mp_obj_t dev_addr) {
|
|
|
|
ubluepy_peripheral_obj_t * self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
|
2017-03-26 14:45:21 -04:00
|
|
|
if (MP_OBJ_IS_STR(dev_addr)) {
|
|
|
|
GET_STR_DATA_LEN(dev_addr, str_data, str_len);
|
|
|
|
if (str_len == 17) { // Example "11:22:33:aa:bb:cc"
|
|
|
|
|
|
|
|
uint8_t * p_addr = m_new(uint8_t, 6);
|
|
|
|
|
|
|
|
p_addr[0] = unichar_xdigit_value(str_data[16]);
|
|
|
|
p_addr[0] += unichar_xdigit_value(str_data[15]) << 4;
|
|
|
|
p_addr[1] = unichar_xdigit_value(str_data[13]);
|
|
|
|
p_addr[1] += unichar_xdigit_value(str_data[12]) << 4;
|
|
|
|
p_addr[2] = unichar_xdigit_value(str_data[10]);
|
|
|
|
p_addr[2] += unichar_xdigit_value(str_data[9]) << 4;
|
|
|
|
p_addr[3] = unichar_xdigit_value(str_data[7]);
|
|
|
|
p_addr[3] += unichar_xdigit_value(str_data[6]) << 4;
|
|
|
|
p_addr[4] = unichar_xdigit_value(str_data[4]);
|
|
|
|
p_addr[4] += unichar_xdigit_value(str_data[3]) << 4;
|
|
|
|
p_addr[5] = unichar_xdigit_value(str_data[1]);
|
|
|
|
p_addr[5] += unichar_xdigit_value(str_data[0]) << 4;
|
|
|
|
|
|
|
|
ble_drv_connect(p_addr, 1);
|
|
|
|
|
|
|
|
m_del(uint8_t, p_addr, 6);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-28 17:40:28 -04:00
|
|
|
// block until connected
|
|
|
|
while (self->conn_handle == 0xFFFF) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2017-03-30 16:46:45 -04:00
|
|
|
bool retval = ble_drv_discover_services(self, self->conn_handle, disc_add_service);
|
|
|
|
if (retval != true) {
|
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
|
|
|
|
"Error during service discovery"));
|
|
|
|
}
|
|
|
|
|
|
|
|
// For each service perform a characteristic discovery
|
|
|
|
mp_obj_t * services = NULL;
|
|
|
|
mp_uint_t num_services;
|
|
|
|
mp_obj_get_array(self->service_list, &num_services, &services);
|
|
|
|
|
|
|
|
for (uint16_t s = 0; s < num_services; s++) {
|
|
|
|
ubluepy_service_obj_t * p_service = (ubluepy_service_obj_t *)services[s];
|
|
|
|
|
|
|
|
bool retval = ble_drv_discover_characteristic(p_service,
|
|
|
|
self->conn_handle,
|
|
|
|
p_service->start_handle,
|
|
|
|
p_service->end_handle,
|
|
|
|
disc_add_char);
|
|
|
|
if (retval != true) {
|
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
|
|
|
|
"Error during characteristic discovery"));
|
|
|
|
}
|
|
|
|
}
|
2017-03-28 17:40:28 -04:00
|
|
|
|
2017-03-25 12:51:52 -04:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(ubluepy_peripheral_connect_obj, peripheral_connect);
|
|
|
|
|
|
|
|
|
2017-02-08 12:48:35 -05:00
|
|
|
STATIC const mp_map_elem_t ubluepy_peripheral_locals_dict_table[] = {
|
2017-02-15 13:30:45 -05:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_withDelegate), (mp_obj_t)(&ubluepy_peripheral_with_delegate_obj) },
|
2017-02-18 11:57:59 -05:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_setNotificationHandler), (mp_obj_t)(&ubluepy_peripheral_set_notif_handler_obj) },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_setConnectionHandler), (mp_obj_t)(&ubluepy_peripheral_set_conn_handler_obj) },
|
2017-02-18 11:57:10 -05:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_getServices), (mp_obj_t)(&ubluepy_peripheral_get_services_obj) },
|
2017-03-25 12:51:52 -04:00
|
|
|
#if MICROPY_PY_UBLUEPY_CENTRAL
|
2017-02-15 13:30:45 -05:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_connect), (mp_obj_t)(&ubluepy_peripheral_connect_obj) },
|
2017-03-25 12:51:52 -04:00
|
|
|
#if 0
|
2017-02-15 13:30:45 -05:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_disconnect), (mp_obj_t)(&ubluepy_peripheral_disconnect_obj) },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_getServiceByUUID), (mp_obj_t)(&ubluepy_peripheral_get_service_by_uuid_obj) },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_getCharacteristics), (mp_obj_t)(&ubluepy_peripheral_get_chars_obj) },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_getDescriptors), (mp_obj_t)(&ubluepy_peripheral_get_descs_obj) },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_waitForNotifications), (mp_obj_t)(&ubluepy_peripheral_wait_for_notif_obj) },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_writeCharacteristic), (mp_obj_t)(&ubluepy_peripheral_write_char_obj) },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_readCharacteristic), (mp_obj_t)(&ubluepy_peripheral_read_char_obj) },
|
2017-03-25 12:51:52 -04:00
|
|
|
#endif // 0
|
|
|
|
#endif // MICROPY_PY_UBLUEPY_CENTRAL
|
2017-02-08 12:48:35 -05:00
|
|
|
#if MICROPY_PY_UBLUEPY_PERIPHERAL
|
2017-02-15 13:30:45 -05:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_advertise), (mp_obj_t)(&ubluepy_peripheral_advertise_obj) },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_disconnect), (mp_obj_t)(&ubluepy_peripheral_disconnect_obj) },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_addService), (mp_obj_t)(&ubluepy_peripheral_add_service_obj) },
|
2017-02-08 12:48:35 -05:00
|
|
|
#if 0
|
2017-02-15 13:30:45 -05:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_addCharacteristic), (mp_obj_t)(&ubluepy_peripheral_add_char_obj) },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_addDescriptor), (mp_obj_t)(&ubluepy_peripheral_add_desc_obj) },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_writeCharacteristic), (mp_obj_t)(&ubluepy_peripheral_write_char_obj) },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_readCharacteristic), (mp_obj_t)(&ubluepy_peripheral_read_char_obj) },
|
2017-02-08 12:48:35 -05:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#if MICROPY_PY_UBLUEPY_BROADCASTER
|
2017-02-06 14:19:06 -05:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_advertise), (mp_obj_t)(&ubluepy_peripheral_advertise_obj) },
|
|
|
|
#endif
|
2017-02-08 12:48:35 -05:00
|
|
|
#if MICROPY_PY_UBLUEPY_OBSERVER
|
|
|
|
// Nothing yet.
|
|
|
|
#endif
|
2017-02-06 14:19:06 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(ubluepy_peripheral_locals_dict, ubluepy_peripheral_locals_dict_table);
|
|
|
|
|
|
|
|
const mp_obj_type_t ubluepy_peripheral_type = {
|
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_Peripheral,
|
|
|
|
.print = ubluepy_peripheral_print,
|
|
|
|
.make_new = ubluepy_peripheral_make_new,
|
|
|
|
.locals_dict = (mp_obj_t)&ubluepy_peripheral_locals_dict
|
|
|
|
};
|
|
|
|
|
2017-02-08 12:48:35 -05:00
|
|
|
#endif // MICROPY_PY_UBLUEPY
|