ubluepy_characteristic.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 "py/obj.h"
  27. #include "py/runtime.h"
  28. #if MICROPY_PY_UBLUEPY_PERIPHERAL || MICROPY_PY_UBLUEPY_CENTRAL
  29. #include "modubluepy.h"
  30. #include "ble_drv.h"
  31. STATIC void ubluepy_characteristic_print(const mp_print_t *print, mp_obj_t o, mp_print_kind_t kind) {
  32. ubluepy_characteristic_obj_t * self = (ubluepy_characteristic_obj_t *)o;
  33. mp_printf(print, "Characteristic(handle: 0x" HEX2_FMT ", conn_handle: " HEX2_FMT ")",
  34. self->handle, self->p_service->p_periph->conn_handle);
  35. }
  36. 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) {
  37. static const mp_arg_t allowed_args[] = {
  38. { MP_QSTR_uuid, MP_ARG_REQUIRED| MP_ARG_OBJ, {.u_obj = mp_const_none} },
  39. { MP_QSTR_props, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = UBLUEPY_PROP_READ | UBLUEPY_PROP_WRITE} },
  40. { MP_QSTR_attrs, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
  41. };
  42. // parse args
  43. mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
  44. mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
  45. ubluepy_characteristic_obj_t *s = m_new_obj(ubluepy_characteristic_obj_t);
  46. s->base.type = type;
  47. mp_obj_t uuid_obj = args[0].u_obj;
  48. if (uuid_obj == mp_const_none) {
  49. return MP_OBJ_FROM_PTR(s);
  50. }
  51. if (MP_OBJ_IS_TYPE(uuid_obj, &ubluepy_uuid_type)) {
  52. s->p_uuid = MP_OBJ_TO_PTR(uuid_obj);
  53. // (void)sd_characterstic_add(s);
  54. } else {
  55. mp_raise_ValueError("Invalid UUID parameter");
  56. }
  57. if (args[1].u_int > 0) {
  58. s->props = (uint8_t)args[1].u_int;
  59. }
  60. if (args[2].u_int > 0) {
  61. s->attrs = (uint8_t)args[2].u_int;
  62. }
  63. // clear pointer to service
  64. s->p_service = NULL;
  65. // clear pointer to char value data
  66. s->value_data = NULL;
  67. return MP_OBJ_FROM_PTR(s);
  68. }
  69. void char_data_callback(mp_obj_t self_in, uint16_t length, uint8_t * p_data) {
  70. ubluepy_characteristic_obj_t * self = MP_OBJ_TO_PTR(self_in);
  71. self->value_data = mp_obj_new_bytearray(length, p_data);
  72. }
  73. /// \method read()
  74. /// Read Characteristic value.
  75. ///
  76. STATIC mp_obj_t char_read(mp_obj_t self_in) {
  77. ubluepy_characteristic_obj_t * self = MP_OBJ_TO_PTR(self_in);
  78. #if MICROPY_PY_UBLUEPY_CENTRAL
  79. // TODO: free any previous allocation of value_data
  80. ble_drv_attr_c_read(self->p_service->p_periph->conn_handle,
  81. self->handle,
  82. self_in,
  83. char_data_callback);
  84. return self->value_data;
  85. #else
  86. (void)self;
  87. return mp_const_none;
  88. #endif
  89. }
  90. STATIC MP_DEFINE_CONST_FUN_OBJ_1(ubluepy_characteristic_read_obj, char_read);
  91. /// \method write(data, [with_response=False])
  92. /// Write Characteristic value.
  93. ///
  94. STATIC mp_obj_t char_write(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
  95. ubluepy_characteristic_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
  96. mp_obj_t data = pos_args[1];
  97. static const mp_arg_t allowed_args[] = {
  98. { MP_QSTR_with_response, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false } },
  99. };
  100. // parse args
  101. mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
  102. mp_arg_parse_all(n_args - 2, pos_args + 2, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
  103. mp_buffer_info_t bufinfo;
  104. mp_get_buffer_raise(data, &bufinfo, MP_BUFFER_READ);
  105. // figure out mode of the Peripheral
  106. ubluepy_role_type_t role = self->p_service->p_periph->role;
  107. if (role == UBLUEPY_ROLE_PERIPHERAL) {
  108. if (self->props & UBLUEPY_PROP_NOTIFY) {
  109. ble_drv_attr_s_notify(self->p_service->p_periph->conn_handle,
  110. self->handle,
  111. bufinfo.len,
  112. bufinfo.buf);
  113. } else {
  114. ble_drv_attr_s_write(self->p_service->p_periph->conn_handle,
  115. self->handle,
  116. bufinfo.len,
  117. bufinfo.buf);
  118. }
  119. } else {
  120. #if MICROPY_PY_UBLUEPY_CENTRAL
  121. bool with_response = args[0].u_bool;
  122. ble_drv_attr_c_write(self->p_service->p_periph->conn_handle,
  123. self->handle,
  124. bufinfo.len,
  125. bufinfo.buf,
  126. with_response);
  127. #endif
  128. }
  129. return mp_const_none;
  130. }
  131. STATIC MP_DEFINE_CONST_FUN_OBJ_KW(ubluepy_characteristic_write_obj, 2, char_write);
  132. /// \method properties()
  133. /// Read Characteristic value properties.
  134. ///
  135. STATIC mp_obj_t char_properties(mp_obj_t self_in) {
  136. ubluepy_characteristic_obj_t * self = MP_OBJ_TO_PTR(self_in);
  137. return MP_OBJ_NEW_SMALL_INT(self->props);
  138. }
  139. STATIC MP_DEFINE_CONST_FUN_OBJ_1(ubluepy_characteristic_get_properties_obj, char_properties);
  140. /// \method uuid()
  141. /// Get UUID instance of the characteristic.
  142. ///
  143. STATIC mp_obj_t char_uuid(mp_obj_t self_in) {
  144. ubluepy_characteristic_obj_t * self = MP_OBJ_TO_PTR(self_in);
  145. return MP_OBJ_FROM_PTR(self->p_uuid);
  146. }
  147. STATIC MP_DEFINE_CONST_FUN_OBJ_1(ubluepy_characteristic_get_uuid_obj, char_uuid);
  148. STATIC const mp_rom_map_elem_t ubluepy_characteristic_locals_dict_table[] = {
  149. { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&ubluepy_characteristic_read_obj) },
  150. { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&ubluepy_characteristic_write_obj) },
  151. #if 0
  152. { MP_ROM_QSTR(MP_QSTR_supportsRead), MP_ROM_PTR(&ubluepy_characteristic_supports_read_obj) },
  153. { MP_ROM_QSTR(MP_QSTR_propertiesToString), MP_ROM_PTR(&ubluepy_characteristic_properties_to_str_obj) },
  154. { MP_ROM_QSTR(MP_QSTR_getHandle), MP_ROM_PTR(&ubluepy_characteristic_get_handle_obj) },
  155. // Properties
  156. { MP_ROM_QSTR(MP_QSTR_peripheral), MP_ROM_PTR(&ubluepy_characteristic_get_peripheral_obj) },
  157. #endif
  158. { MP_ROM_QSTR(MP_QSTR_uuid), MP_ROM_PTR(&ubluepy_characteristic_get_uuid_obj) },
  159. { MP_ROM_QSTR(MP_QSTR_properties), MP_ROM_PTR(&ubluepy_characteristic_get_properties_obj) },
  160. { MP_ROM_QSTR(MP_QSTR_PROP_BROADCAST), MP_ROM_INT(UBLUEPY_PROP_BROADCAST) },
  161. { MP_ROM_QSTR(MP_QSTR_PROP_READ), MP_ROM_INT(UBLUEPY_PROP_READ) },
  162. { MP_ROM_QSTR(MP_QSTR_PROP_WRITE_WO_RESP), MP_ROM_INT(UBLUEPY_PROP_WRITE_WO_RESP) },
  163. { MP_ROM_QSTR(MP_QSTR_PROP_WRITE), MP_ROM_INT(UBLUEPY_PROP_WRITE) },
  164. { MP_ROM_QSTR(MP_QSTR_PROP_NOTIFY), MP_ROM_INT(UBLUEPY_PROP_NOTIFY) },
  165. { MP_ROM_QSTR(MP_QSTR_PROP_INDICATE), MP_ROM_INT(UBLUEPY_PROP_INDICATE) },
  166. { MP_ROM_QSTR(MP_QSTR_PROP_AUTH_SIGNED_WR), MP_ROM_INT(UBLUEPY_PROP_AUTH_SIGNED_WR) },
  167. #if MICROPY_PY_UBLUEPY_PERIPHERAL
  168. { MP_ROM_QSTR(MP_QSTR_ATTR_CCCD), MP_ROM_INT(UBLUEPY_ATTR_CCCD) },
  169. #endif
  170. #if MICROPY_PY_UBLUEPY_CENTRAL
  171. { MP_ROM_QSTR(MP_QSTR_PROP_AUTH_SIGNED_WR), MP_ROM_INT(UBLUEPY_ATTR_SCCD) },
  172. #endif
  173. };
  174. STATIC MP_DEFINE_CONST_DICT(ubluepy_characteristic_locals_dict, ubluepy_characteristic_locals_dict_table);
  175. const mp_obj_type_t ubluepy_characteristic_type = {
  176. { &mp_type_type },
  177. .name = MP_QSTR_Characteristic,
  178. .print = ubluepy_characteristic_print,
  179. .make_new = ubluepy_characteristic_make_new,
  180. .locals_dict = (mp_obj_dict_t*)&ubluepy_characteristic_locals_dict
  181. };
  182. #endif // MICROPY_PY_UBLUEPY_PERIPHERAL || MICROPY_PY_UBLUEPY_CENTRAL