ubluepy_service.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. #include "py/objlist.h"
  29. #if MICROPY_PY_UBLUEPY_PERIPHERAL || MICROPY_PY_UBLUEPY_CENTRAL
  30. #include "modubluepy.h"
  31. #include "ble_drv.h"
  32. STATIC void ubluepy_service_print(const mp_print_t *print, mp_obj_t o, mp_print_kind_t kind) {
  33. ubluepy_service_obj_t * self = (ubluepy_service_obj_t *)o;
  34. mp_printf(print, "Service(handle: 0x" HEX2_FMT ")", self->handle);
  35. }
  36. STATIC mp_obj_t ubluepy_service_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
  37. enum { ARG_NEW_UUID, ARG_NEW_TYPE };
  38. static const mp_arg_t allowed_args[] = {
  39. { ARG_NEW_UUID, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
  40. { ARG_NEW_TYPE, MP_ARG_INT, {.u_int = UBLUEPY_SERVICE_PRIMARY} },
  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_service_obj_t *s = m_new_obj(ubluepy_service_obj_t);
  46. s->base.type = type;
  47. mp_obj_t uuid_obj = args[ARG_NEW_UUID].u_obj;
  48. if (uuid_obj == MP_OBJ_NULL) {
  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. uint8_t type = args[ARG_NEW_TYPE].u_int;
  54. if (type > 0 && type <= UBLUEPY_SERVICE_PRIMARY) {
  55. s->type = type;
  56. } else {
  57. mp_raise_ValueError("Invalid Service type");
  58. }
  59. (void)ble_drv_service_add(s);
  60. } else {
  61. mp_raise_ValueError("Invalid UUID parameter");
  62. }
  63. // clear reference to peripheral
  64. s->p_periph = NULL;
  65. s->char_list = mp_obj_new_list(0, NULL);
  66. return MP_OBJ_FROM_PTR(s);
  67. }
  68. /// \method addCharacteristic(Characteristic)
  69. /// Add Characteristic to the Service.
  70. ///
  71. STATIC mp_obj_t service_add_characteristic(mp_obj_t self_in, mp_obj_t characteristic) {
  72. ubluepy_service_obj_t * self = MP_OBJ_TO_PTR(self_in);
  73. ubluepy_characteristic_obj_t * p_char = MP_OBJ_TO_PTR(characteristic);
  74. p_char->service_handle = self->handle;
  75. bool retval = ble_drv_characteristic_add(p_char);
  76. if (retval) {
  77. p_char->p_service = self;
  78. }
  79. mp_obj_list_append(self->char_list, characteristic);
  80. // return mp_obj_new_bool(retval);
  81. return mp_const_none;
  82. }
  83. STATIC MP_DEFINE_CONST_FUN_OBJ_2(ubluepy_service_add_char_obj, service_add_characteristic);
  84. /// \method getCharacteristics()
  85. /// Return list with all characteristics registered in the Service.
  86. ///
  87. STATIC mp_obj_t service_get_chars(mp_obj_t self_in) {
  88. ubluepy_service_obj_t * self = MP_OBJ_TO_PTR(self_in);
  89. return self->char_list;
  90. }
  91. STATIC MP_DEFINE_CONST_FUN_OBJ_1(ubluepy_service_get_chars_obj, service_get_chars);
  92. /// \method getCharacteristic(UUID)
  93. /// Return Characteristic with the given UUID.
  94. ///
  95. STATIC mp_obj_t service_get_characteristic(mp_obj_t self_in, mp_obj_t uuid) {
  96. ubluepy_service_obj_t * self = MP_OBJ_TO_PTR(self_in);
  97. ubluepy_uuid_obj_t * p_uuid = MP_OBJ_TO_PTR(uuid);
  98. // validate that there is an UUID object passed in as parameter
  99. if (!(MP_OBJ_IS_TYPE(uuid, &ubluepy_uuid_type))) {
  100. mp_raise_ValueError("Invalid UUID parameter");
  101. }
  102. mp_obj_t * chars = NULL;
  103. mp_uint_t num_chars = 0;
  104. mp_obj_get_array(self->char_list, &num_chars, &chars);
  105. for (uint8_t i = 0; i < num_chars; i++) {
  106. ubluepy_characteristic_obj_t * p_char = (ubluepy_characteristic_obj_t *)chars[i];
  107. bool type_match = p_char->p_uuid->type == p_uuid->type;
  108. bool uuid_match = ((uint16_t)(*(uint16_t *)&p_char->p_uuid->value[0]) ==
  109. (uint16_t)(*(uint16_t *)&p_uuid->value[0]));
  110. if (type_match && uuid_match) {
  111. return MP_OBJ_FROM_PTR(p_char);
  112. }
  113. }
  114. return mp_const_none;
  115. }
  116. STATIC MP_DEFINE_CONST_FUN_OBJ_2(ubluepy_service_get_char_obj, service_get_characteristic);
  117. /// \method uuid()
  118. /// Get UUID instance of the Service.
  119. ///
  120. STATIC mp_obj_t service_uuid(mp_obj_t self_in) {
  121. ubluepy_service_obj_t * self = MP_OBJ_TO_PTR(self_in);
  122. return MP_OBJ_FROM_PTR(self->p_uuid);
  123. }
  124. STATIC MP_DEFINE_CONST_FUN_OBJ_1(ubluepy_service_get_uuid_obj, service_uuid);
  125. STATIC const mp_rom_map_elem_t ubluepy_service_locals_dict_table[] = {
  126. { MP_ROM_QSTR(MP_QSTR_getCharacteristic), MP_ROM_PTR(&ubluepy_service_get_char_obj) },
  127. { MP_ROM_QSTR(MP_QSTR_addCharacteristic), MP_ROM_PTR(&ubluepy_service_add_char_obj) },
  128. { MP_ROM_QSTR(MP_QSTR_getCharacteristics), MP_ROM_PTR(&ubluepy_service_get_chars_obj) },
  129. #if 0
  130. // Properties
  131. { MP_ROM_QSTR(MP_QSTR_peripheral), MP_ROM_PTR(&ubluepy_service_get_peripheral_obj) },
  132. #endif
  133. { MP_ROM_QSTR(MP_QSTR_uuid), MP_ROM_PTR(&ubluepy_service_get_uuid_obj) },
  134. { MP_ROM_QSTR(MP_QSTR_PRIMARY), MP_ROM_INT(UBLUEPY_SERVICE_PRIMARY) },
  135. { MP_ROM_QSTR(MP_QSTR_SECONDARY), MP_ROM_INT(UBLUEPY_SERVICE_SECONDARY) },
  136. };
  137. STATIC MP_DEFINE_CONST_DICT(ubluepy_service_locals_dict, ubluepy_service_locals_dict_table);
  138. const mp_obj_type_t ubluepy_service_type = {
  139. { &mp_type_type },
  140. .name = MP_QSTR_Service,
  141. .print = ubluepy_service_print,
  142. .make_new = ubluepy_service_make_new,
  143. .locals_dict = (mp_obj_dict_t*)&ubluepy_service_locals_dict
  144. };
  145. #endif // MICROPY_PY_UBLUEPY_PERIPHERAL || MICROPY_PY_UBLUEPY_CENTRAL