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-02-11 08:21:05 -05:00
|
|
|
return MP_OBJ_FROM_PTR(s);
|
|
|
|
}
|
|
|
|
|
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-03-30 17:39:45 -04:00
|
|
|
ble_drv_attr_c_read(self->p_service->p_periph->conn_handle,
|
|
|
|
self->handle,
|
|
|
|
0,
|
|
|
|
NULL);
|
2017-02-17 19:17:57 -05:00
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(ubluepy_characteristic_read_obj, char_read);
|
|
|
|
|
|
|
|
/// \method write(data)
|
|
|
|
/// Write Characteristic value.
|
|
|
|
///
|
|
|
|
STATIC mp_obj_t char_write(mp_obj_t self_in, mp_obj_t data) {
|
|
|
|
ubluepy_characteristic_obj_t * self = MP_OBJ_TO_PTR(self_in);
|
2017-02-20 15:27:21 -05:00
|
|
|
|
|
|
|
mp_buffer_info_t bufinfo;
|
|
|
|
mp_get_buffer_raise(data, &bufinfo, MP_BUFFER_READ);
|
|
|
|
|
|
|
|
if (self->props & UBLUEPY_PROP_NOTIFY) {
|
|
|
|
ble_drv_attr_notify(self->p_service->p_periph->conn_handle,
|
|
|
|
self->handle,
|
|
|
|
bufinfo.len,
|
|
|
|
bufinfo.buf);
|
|
|
|
} else {
|
|
|
|
ble_drv_attr_write(self->p_service->p_periph->conn_handle,
|
|
|
|
self->handle,
|
|
|
|
bufinfo.len,
|
|
|
|
bufinfo.buf);
|
|
|
|
}
|
2017-02-17 19:17:57 -05:00
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(ubluepy_characteristic_write_obj, char_write);
|
|
|
|
|
|
|
|
|
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-02-06 14:48:01 -05:00
|
|
|
STATIC const mp_map_elem_t ubluepy_characteristic_locals_dict_table[] = {
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_read), (mp_obj_t)(&ubluepy_characteristic_read_obj) },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)(&ubluepy_characteristic_write_obj) },
|
2017-02-17 19:17:57 -05:00
|
|
|
#if 0
|
2017-02-06 14:48:01 -05:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_supportsRead), (mp_obj_t)(&ubluepy_characteristic_supports_read_obj) },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_propertiesToString), (mp_obj_t)(&ubluepy_characteristic_properties_to_str_obj) },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_getHandle), (mp_obj_t)(&ubluepy_characteristic_get_handle_obj) },
|
|
|
|
|
|
|
|
// Properties
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_peripheral), (mp_obj_t)(&ubluepy_characteristic_get_peripheral_obj) },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_uuid), (mp_obj_t)(&ubluepy_characteristic_get_uuid_obj) },
|
2017-02-20 14:28:19 -05:00
|
|
|
#endif
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_properties), (mp_obj_t)(&ubluepy_characteristic_get_properties_obj) },
|
|
|
|
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_PROP_BROADCAST), MP_OBJ_NEW_SMALL_INT(UBLUEPY_PROP_BROADCAST) },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_PROP_READ), MP_OBJ_NEW_SMALL_INT(UBLUEPY_PROP_READ) },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_PROP_WRITE_WO_RESP), MP_OBJ_NEW_SMALL_INT(UBLUEPY_PROP_WRITE_WO_RESP) },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_PROP_WRITE), MP_OBJ_NEW_SMALL_INT(UBLUEPY_PROP_WRITE) },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_PROP_NOTIFY), MP_OBJ_NEW_SMALL_INT(UBLUEPY_PROP_NOTIFY) },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_PROP_INDICATE), MP_OBJ_NEW_SMALL_INT(UBLUEPY_PROP_INDICATE) },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_PROP_AUTH_SIGNED_WR), MP_OBJ_NEW_SMALL_INT(UBLUEPY_PROP_AUTH_SIGNED_WR) },
|
|
|
|
|
|
|
|
#if MICROPY_PY_UBLUEPY_PERIPHERAL
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_ATTR_CCCD), MP_OBJ_NEW_SMALL_INT(UBLUEPY_ATTR_CCCD) },
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if MICROPY_PY_UBLUEPY_CENTRAL
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_PROP_AUTH_SIGNED_WR), MP_OBJ_NEW_SMALL_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,
|
|
|
|
.locals_dict = (mp_obj_t)&ubluepy_characteristic_locals_dict
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // MICROPY_PY_UBLUEPY_PERIPHERAL || MICROPY_PY_UBLUEPY_CENTRAL
|