2017-02-06 14:48:01 -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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "py/obj.h"
|
2017-02-11 08:21:05 -05:00
|
|
|
#include "py/runtime.h"
|
2017-02-06 14:48:01 -05:00
|
|
|
|
|
|
|
#if MICROPY_PY_UBLUEPY_PERIPHERAL || MICROPY_PY_UBLUEPY_CENTRAL
|
|
|
|
|
2017-02-15 13:33:11 -05:00
|
|
|
#include "modubluepy.h"
|
2017-02-20 15:27:21 -05:00
|
|
|
#include "ble_drv.h"
|
2017-02-15 13:33:11 -05:00
|
|
|
|
2017-02-11 08:21:05 -05:00
|
|
|
STATIC void ubluepy_characteristic_print(const mp_print_t *print, mp_obj_t o, mp_print_kind_t kind) {
|
|
|
|
ubluepy_characteristic_obj_t * self = (ubluepy_characteristic_obj_t *)o;
|
|
|
|
|
2017-02-18 07:10:59 -05:00
|
|
|
mp_printf(print, "Characteristic(handle: 0x" HEX2_FMT ", conn_handle: " HEX2_FMT ")",
|
|
|
|
self->handle, self->p_service->p_periph->conn_handle);
|
2017-02-11 08:21:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
STATIC mp_obj_t ubluepy_characteristic_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
|
|
|
|
static const mp_arg_t allowed_args[] = {
|
2017-02-20 14:28:19 -05:00
|
|
|
{ MP_QSTR_uuid, MP_ARG_REQUIRED| MP_ARG_OBJ, {.u_obj = mp_const_none} },
|
|
|
|
{ MP_QSTR_props, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = UBLUEPY_PROP_READ | UBLUEPY_PROP_WRITE} },
|
|
|
|
{ MP_QSTR_attrs, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
|
2017-02-11 08:21:05 -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_characteristic_obj_t *s = m_new_obj(ubluepy_characteristic_obj_t);
|
|
|
|
s->base.type = type;
|
|
|
|
|
2017-02-20 14:28:19 -05:00
|
|
|
mp_obj_t uuid_obj = args[0].u_obj;
|
2017-02-11 08:21:05 -05:00
|
|
|
|
2017-02-20 14:28:19 -05:00
|
|
|
if (uuid_obj == mp_const_none) {
|
2017-02-11 08:21:05 -05:00
|
|
|
return MP_OBJ_FROM_PTR(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MP_OBJ_IS_TYPE(uuid_obj, &ubluepy_uuid_type)) {
|
|
|
|
s->p_uuid = MP_OBJ_TO_PTR(uuid_obj);
|
|
|
|
// (void)sd_characterstic_add(s);
|
|
|
|
} else {
|
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
|
|
|
|
"Invalid UUID parameter"));
|
|
|
|
}
|
|
|
|
|
2017-02-20 14:28:19 -05:00
|
|
|
if (args[1].u_int > 0) {
|
|
|
|
s->props = (uint8_t)args[1].u_int;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args[2].u_int > 0) {
|
|
|
|
s->attrs = (uint8_t)args[2].u_int;
|
|
|
|
}
|
|
|
|
|
2017-02-18 07:10:59 -05:00
|
|
|
// clear pointer to service
|
|
|
|
s->p_service = NULL;
|
|
|
|
|
2017-04-01 10:39:51 -04:00
|
|
|
// clear pointer to char value data
|
|
|
|
s->value_data = NULL;
|
|
|
|
|
2017-02-11 08:21:05 -05:00
|
|
|
return MP_OBJ_FROM_PTR(s);
|
|
|
|
}
|
|
|
|
|
2017-04-01 10:39:51 -04:00
|
|
|
void char_data_callback(mp_obj_t self_in, uint16_t length, uint8_t * p_data) {
|
|
|
|
ubluepy_characteristic_obj_t * self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
self->value_data = mp_obj_new_bytearray(length, p_data);
|
|
|
|
}
|
|
|
|
|
2017-02-17 19:17:57 -05:00
|
|
|
/// \method read()
|
|
|
|
/// Read Characteristic value.
|
|
|
|
///
|
|
|
|
STATIC mp_obj_t char_read(mp_obj_t self_in) {
|
|
|
|
ubluepy_characteristic_obj_t * self = MP_OBJ_TO_PTR(self_in);
|
2017-04-01 10:39:51 -04:00
|
|
|
|
2017-04-01 18:31:28 -04:00
|
|
|
#if MICROPY_PY_UBLUEPY_CENTRAL
|
2017-04-01 10:39:51 -04:00
|
|
|
// TODO: free any previous allocation of value_data
|
|
|
|
|
2017-03-30 17:39:45 -04:00
|
|
|
ble_drv_attr_c_read(self->p_service->p_periph->conn_handle,
|
|
|
|
self->handle,
|
2017-04-01 10:39:51 -04:00
|
|
|
self_in,
|
|
|
|
char_data_callback);
|
2017-02-17 19:17:57 -05:00
|
|
|
|
2017-04-01 10:39:51 -04:00
|
|
|
return self->value_data;
|
2017-04-01 18:31:28 -04:00
|
|
|
#else
|
|
|
|
(void)self;
|
|
|
|
return mp_const_none;
|
|
|
|
#endif
|
2017-02-17 19:17:57 -05:00
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(ubluepy_characteristic_read_obj, char_read);
|
|
|
|
|
2017-05-30 13:32:26 -04:00
|
|
|
/// \method write(data, [with_response=False])
|
2017-02-17 19:17:57 -05:00
|
|
|
/// Write Characteristic value.
|
|
|
|
///
|
2017-05-30 13:32:26 -04:00
|
|
|
STATIC mp_obj_t char_write(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
|
|
|
ubluepy_characteristic_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
|
|
|
|
mp_obj_t data = pos_args[1];
|
|
|
|
|
|
|
|
static const mp_arg_t allowed_args[] = {
|
|
|
|
{ MP_QSTR_with_response, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false } },
|
|
|
|
};
|
|
|
|
|
|
|
|
// parse args
|
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
|
|
|
mp_arg_parse_all(n_args - 2, pos_args + 2, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
2017-02-20 15:27:21 -05:00
|
|
|
|
|
|
|
mp_buffer_info_t bufinfo;
|
|
|
|
mp_get_buffer_raise(data, &bufinfo, MP_BUFFER_READ);
|
|
|
|
|
2017-05-14 13:01:53 -04:00
|
|
|
// figure out mode of the Peripheral
|
|
|
|
ubluepy_role_type_t role = self->p_service->p_periph->role;
|
|
|
|
|
|
|
|
if (role == UBLUEPY_ROLE_PERIPHERAL) {
|
|
|
|
if (self->props & UBLUEPY_PROP_NOTIFY) {
|
|
|
|
ble_drv_attr_s_notify(self->p_service->p_periph->conn_handle,
|
|
|
|
self->handle,
|
|
|
|
bufinfo.len,
|
|
|
|
bufinfo.buf);
|
|
|
|
} else {
|
|
|
|
ble_drv_attr_s_write(self->p_service->p_periph->conn_handle,
|
|
|
|
self->handle,
|
|
|
|
bufinfo.len,
|
|
|
|
bufinfo.buf);
|
|
|
|
}
|
2017-02-20 15:27:21 -05:00
|
|
|
} else {
|
2017-05-14 13:01:53 -04:00
|
|
|
#if MICROPY_PY_UBLUEPY_CENTRAL
|
2017-05-30 13:32:26 -04:00
|
|
|
bool with_response = args[0].u_bool;
|
|
|
|
|
2017-05-14 13:01:53 -04:00
|
|
|
ble_drv_attr_c_write(self->p_service->p_periph->conn_handle,
|
2017-05-14 12:57:58 -04:00
|
|
|
self->handle,
|
2017-05-14 13:04:49 -04:00
|
|
|
bufinfo.len,
|
2017-05-30 13:32:26 -04:00
|
|
|
bufinfo.buf,
|
|
|
|
with_response);
|
2017-05-14 13:01:53 -04:00
|
|
|
#endif
|
2017-02-20 15:27:21 -05:00
|
|
|
}
|
2017-02-17 19:17:57 -05:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2017-05-30 13:32:26 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(ubluepy_characteristic_write_obj, 2, char_write);
|
2017-02-17 19:17:57 -05:00
|
|
|
|
2017-02-20 14:28:19 -05:00
|
|
|
/// \method properties()
|
|
|
|
/// Read Characteristic value properties.
|
|
|
|
///
|
|
|
|
STATIC mp_obj_t char_properties(mp_obj_t self_in) {
|
|
|
|
ubluepy_characteristic_obj_t * self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(self->props);
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(ubluepy_characteristic_get_properties_obj, char_properties);
|
|
|
|
|
2017-04-01 10:39:51 -04:00
|
|
|
/// \method uuid()
|
|
|
|
/// Get UUID instance of the characteristic.
|
|
|
|
///
|
|
|
|
STATIC mp_obj_t char_uuid(mp_obj_t self_in) {
|
|
|
|
ubluepy_characteristic_obj_t * self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
return MP_OBJ_FROM_PTR(self->p_uuid);
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(ubluepy_characteristic_get_uuid_obj, char_uuid);
|
|
|
|
|
|
|
|
|
2017-05-09 16:54:30 -04:00
|
|
|
STATIC const mp_rom_map_elem_t ubluepy_characteristic_locals_dict_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&ubluepy_characteristic_read_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&ubluepy_characteristic_write_obj) },
|
2017-02-17 19:17:57 -05:00
|
|
|
#if 0
|
2017-05-09 16:54:30 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_supportsRead), MP_ROM_PTR(&ubluepy_characteristic_supports_read_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_propertiesToString), MP_ROM_PTR(&ubluepy_characteristic_properties_to_str_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_getHandle), MP_ROM_PTR(&ubluepy_characteristic_get_handle_obj) },
|
2017-02-06 14:48:01 -05:00
|
|
|
|
|
|
|
// Properties
|
2017-05-09 16:54:30 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_peripheral), MP_ROM_PTR(&ubluepy_characteristic_get_peripheral_obj) },
|
2017-02-20 14:28:19 -05:00
|
|
|
#endif
|
2017-05-09 16:54:30 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_uuid), MP_ROM_PTR(&ubluepy_characteristic_get_uuid_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_properties), MP_ROM_PTR(&ubluepy_characteristic_get_properties_obj) },
|
2017-02-20 14:28:19 -05:00
|
|
|
|
2017-05-09 16:54:30 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_PROP_BROADCAST), MP_ROM_INT(UBLUEPY_PROP_BROADCAST) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_PROP_READ), MP_ROM_INT(UBLUEPY_PROP_READ) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_PROP_WRITE_WO_RESP), MP_ROM_INT(UBLUEPY_PROP_WRITE_WO_RESP) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_PROP_WRITE), MP_ROM_INT(UBLUEPY_PROP_WRITE) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_PROP_NOTIFY), MP_ROM_INT(UBLUEPY_PROP_NOTIFY) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_PROP_INDICATE), MP_ROM_INT(UBLUEPY_PROP_INDICATE) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_PROP_AUTH_SIGNED_WR), MP_ROM_INT(UBLUEPY_PROP_AUTH_SIGNED_WR) },
|
2017-02-20 14:28:19 -05:00
|
|
|
|
|
|
|
#if MICROPY_PY_UBLUEPY_PERIPHERAL
|
2017-05-09 16:54:30 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_ATTR_CCCD), MP_ROM_INT(UBLUEPY_ATTR_CCCD) },
|
2017-02-20 14:28:19 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if MICROPY_PY_UBLUEPY_CENTRAL
|
2017-05-09 16:54:30 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_PROP_AUTH_SIGNED_WR), MP_ROM_INT(UBLUEPY_ATTR_SCCD) },
|
2017-02-06 14:48:01 -05:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(ubluepy_characteristic_locals_dict, ubluepy_characteristic_locals_dict_table);
|
|
|
|
|
|
|
|
const mp_obj_type_t ubluepy_characteristic_type = {
|
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_Characteristic,
|
|
|
|
.print = ubluepy_characteristic_print,
|
|
|
|
.make_new = ubluepy_characteristic_make_new,
|
2017-05-09 16:54:30 -04:00
|
|
|
.locals_dict = (mp_obj_dict_t*)&ubluepy_characteristic_locals_dict
|
2017-02-06 14:48:01 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // MICROPY_PY_UBLUEPY_PERIPHERAL || MICROPY_PY_UBLUEPY_CENTRAL
|