ubluepy_uuid.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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/objstr.h"
  29. #include "py/misc.h"
  30. #if MICROPY_PY_UBLUEPY
  31. #include "modubluepy.h"
  32. #include "ble_drv.h"
  33. STATIC void ubluepy_uuid_print(const mp_print_t *print, mp_obj_t o, mp_print_kind_t kind) {
  34. ubluepy_uuid_obj_t * self = (ubluepy_uuid_obj_t *)o;
  35. if (self->type == UBLUEPY_UUID_16_BIT) {
  36. mp_printf(print, "UUID(uuid: 0x" HEX2_FMT HEX2_FMT ")",
  37. self->value[1], self->value[0]);
  38. } else {
  39. mp_printf(print, "UUID(uuid: 0x" HEX2_FMT HEX2_FMT ", VS idx: " HEX2_FMT ")",
  40. self->value[1], self->value[0], self->uuid_vs_idx);
  41. }
  42. }
  43. STATIC mp_obj_t ubluepy_uuid_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
  44. enum { ARG_NEW_UUID };
  45. static const mp_arg_t allowed_args[] = {
  46. { ARG_NEW_UUID, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
  47. };
  48. // parse args
  49. mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
  50. mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
  51. ubluepy_uuid_obj_t *s = m_new_obj(ubluepy_uuid_obj_t);
  52. s->base.type = type;
  53. mp_obj_t uuid_obj = args[ARG_NEW_UUID].u_obj;
  54. if (uuid_obj == MP_OBJ_NULL) {
  55. return MP_OBJ_FROM_PTR(s);
  56. }
  57. if (MP_OBJ_IS_INT(uuid_obj)) {
  58. s->type = UBLUEPY_UUID_16_BIT;
  59. s->value[1] = (((uint16_t)mp_obj_get_int(uuid_obj)) >> 8) & 0xFF;
  60. s->value[0] = ((uint8_t)mp_obj_get_int(uuid_obj)) & 0xFF;
  61. } else if (MP_OBJ_IS_STR(uuid_obj)) {
  62. GET_STR_DATA_LEN(uuid_obj, str_data, str_len);
  63. if (str_len == 6) { // Assume hex digit prefixed with 0x
  64. s->type = UBLUEPY_UUID_16_BIT;
  65. s->value[0] = unichar_xdigit_value(str_data[5]);
  66. s->value[0] += unichar_xdigit_value(str_data[4]) << 4;
  67. s->value[1] = unichar_xdigit_value(str_data[3]);
  68. s->value[1] += unichar_xdigit_value(str_data[2]) << 4;
  69. } else if (str_len == 36) {
  70. s->type = UBLUEPY_UUID_128_BIT;
  71. uint8_t buffer[16];
  72. buffer[0] = unichar_xdigit_value(str_data[35]);
  73. buffer[0] += unichar_xdigit_value(str_data[34]) << 4;
  74. buffer[1] = unichar_xdigit_value(str_data[33]);
  75. buffer[1] += unichar_xdigit_value(str_data[32]) << 4;
  76. buffer[2] = unichar_xdigit_value(str_data[31]);
  77. buffer[2] += unichar_xdigit_value(str_data[30]) << 4;
  78. buffer[3] = unichar_xdigit_value(str_data[29]);
  79. buffer[3] += unichar_xdigit_value(str_data[28]) << 4;
  80. buffer[4] = unichar_xdigit_value(str_data[27]);
  81. buffer[4] += unichar_xdigit_value(str_data[26]) << 4;
  82. buffer[5] = unichar_xdigit_value(str_data[25]);
  83. buffer[5] += unichar_xdigit_value(str_data[24]) << 4;
  84. // 23 '-'
  85. buffer[6] = unichar_xdigit_value(str_data[22]);
  86. buffer[6] += unichar_xdigit_value(str_data[21]) << 4;
  87. buffer[7] = unichar_xdigit_value(str_data[20]);
  88. buffer[7] += unichar_xdigit_value(str_data[19]) << 4;
  89. // 18 '-'
  90. buffer[8] = unichar_xdigit_value(str_data[17]);
  91. buffer[8] += unichar_xdigit_value(str_data[16]) << 4;
  92. buffer[9] = unichar_xdigit_value(str_data[15]);
  93. buffer[9] += unichar_xdigit_value(str_data[14]) << 4;
  94. // 13 '-'
  95. buffer[10] = unichar_xdigit_value(str_data[12]);
  96. buffer[10] += unichar_xdigit_value(str_data[11]) << 4;
  97. buffer[11] = unichar_xdigit_value(str_data[10]);
  98. buffer[11] += unichar_xdigit_value(str_data[9]) << 4;
  99. // 8 '-'
  100. // 16-bit field
  101. s->value[0] = unichar_xdigit_value(str_data[7]);
  102. s->value[0] += unichar_xdigit_value(str_data[6]) << 4;
  103. s->value[1] = unichar_xdigit_value(str_data[5]);
  104. s->value[1] += unichar_xdigit_value(str_data[4]) << 4;
  105. buffer[14] = unichar_xdigit_value(str_data[3]);
  106. buffer[14] += unichar_xdigit_value(str_data[2]) << 4;
  107. buffer[15] = unichar_xdigit_value(str_data[1]);
  108. buffer[15] += unichar_xdigit_value(str_data[0]) << 4;
  109. ble_drv_uuid_add_vs(buffer, &s->uuid_vs_idx);
  110. } else {
  111. mp_raise_ValueError("Invalid UUID string length");
  112. }
  113. } else if (MP_OBJ_IS_TYPE(uuid_obj, &ubluepy_uuid_type)) {
  114. // deep copy instance
  115. ubluepy_uuid_obj_t * p_old = MP_OBJ_TO_PTR(uuid_obj);
  116. s->type = p_old->type;
  117. s->value[0] = p_old->value[0];
  118. s->value[1] = p_old->value[1];
  119. } else {
  120. mp_raise_ValueError("Invalid UUID parameter");
  121. }
  122. return MP_OBJ_FROM_PTR(s);
  123. }
  124. /// \method binVal()
  125. /// Get binary value of the 16 or 128 bit UUID. Returned as bytearray type.
  126. ///
  127. STATIC mp_obj_t uuid_bin_val(mp_obj_t self_in) {
  128. ubluepy_uuid_obj_t * self = MP_OBJ_TO_PTR(self_in);
  129. // TODO: Extend the uint16 byte value to 16 byte if 128-bit,
  130. // also encapsulate it in a bytearray. For now, return
  131. // the uint16_t field of the UUID.
  132. return MP_OBJ_NEW_SMALL_INT(self->value[0] | self->value[1] << 8);
  133. }
  134. STATIC MP_DEFINE_CONST_FUN_OBJ_1(ubluepy_uuid_bin_val_obj, uuid_bin_val);
  135. STATIC const mp_rom_map_elem_t ubluepy_uuid_locals_dict_table[] = {
  136. #if 0
  137. { MP_ROM_QSTR(MP_QSTR_getCommonName), MP_ROM_PTR(&ubluepy_uuid_get_common_name_obj) },
  138. #endif
  139. // Properties
  140. { MP_ROM_QSTR(MP_QSTR_binVal), MP_ROM_PTR(&ubluepy_uuid_bin_val_obj) },
  141. };
  142. STATIC MP_DEFINE_CONST_DICT(ubluepy_uuid_locals_dict, ubluepy_uuid_locals_dict_table);
  143. const mp_obj_type_t ubluepy_uuid_type = {
  144. { &mp_type_type },
  145. .name = MP_QSTR_UUID,
  146. .print = ubluepy_uuid_print,
  147. .make_new = ubluepy_uuid_make_new,
  148. .locals_dict = (mp_obj_dict_t*)&ubluepy_uuid_locals_dict
  149. };
  150. #endif // MICROPY_PY_UBLUEPY