ubluepy_scanner.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 <string.h>
  27. #include "py/obj.h"
  28. #include "py/runtime.h"
  29. #include "py/objstr.h"
  30. #include "py/objlist.h"
  31. #if MICROPY_PY_UBLUEPY_CENTRAL
  32. #include "ble_drv.h"
  33. #include "mphalport.h"
  34. STATIC void adv_event_handler(mp_obj_t self_in, uint16_t event_id, ble_drv_adv_data_t * data) {
  35. ubluepy_scanner_obj_t *self = MP_OBJ_TO_PTR(self_in);
  36. ubluepy_scan_entry_obj_t * item = m_new_obj(ubluepy_scan_entry_obj_t);
  37. item->base.type = &ubluepy_scan_entry_type;
  38. vstr_t vstr;
  39. vstr_init(&vstr, 17);
  40. vstr_printf(&vstr, ""HEX2_FMT":"HEX2_FMT":"HEX2_FMT":" \
  41. HEX2_FMT":"HEX2_FMT":"HEX2_FMT"",
  42. data->p_peer_addr[5], data->p_peer_addr[4], data->p_peer_addr[3],
  43. data->p_peer_addr[2], data->p_peer_addr[1], data->p_peer_addr[0]);
  44. item->addr = mp_obj_new_str(vstr.buf, vstr.len);
  45. vstr_clear(&vstr);
  46. item->addr_type = data->addr_type;
  47. item->rssi = data->rssi;
  48. item->data = mp_obj_new_bytearray(data->data_len, data->p_data);
  49. mp_obj_list_append(self->adv_reports, item);
  50. // Continue scanning
  51. ble_drv_scan_start(true);
  52. }
  53. STATIC void ubluepy_scanner_print(const mp_print_t *print, mp_obj_t o, mp_print_kind_t kind) {
  54. ubluepy_scanner_obj_t * self = (ubluepy_scanner_obj_t *)o;
  55. (void)self;
  56. mp_printf(print, "Scanner");
  57. }
  58. STATIC mp_obj_t ubluepy_scanner_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
  59. static const mp_arg_t allowed_args[] = {
  60. };
  61. // parse args
  62. mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
  63. mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
  64. ubluepy_scanner_obj_t * s = m_new_obj(ubluepy_scanner_obj_t);
  65. s->base.type = type;
  66. return MP_OBJ_FROM_PTR(s);
  67. }
  68. /// \method scan(timeout)
  69. /// Scan for devices. Timeout is in milliseconds and will set the duration
  70. /// of the scanning.
  71. ///
  72. STATIC mp_obj_t scanner_scan(mp_obj_t self_in, mp_obj_t timeout_in) {
  73. ubluepy_scanner_obj_t * self = MP_OBJ_TO_PTR(self_in);
  74. mp_int_t timeout = mp_obj_get_int(timeout_in);
  75. self->adv_reports = mp_obj_new_list(0, NULL);
  76. ble_drv_adv_report_handler_set(MP_OBJ_FROM_PTR(self), adv_event_handler);
  77. // start
  78. ble_drv_scan_start(false);
  79. // sleep
  80. mp_hal_delay_ms(timeout);
  81. // stop
  82. ble_drv_scan_stop();
  83. return self->adv_reports;
  84. }
  85. STATIC MP_DEFINE_CONST_FUN_OBJ_2(ubluepy_scanner_scan_obj, scanner_scan);
  86. STATIC const mp_rom_map_elem_t ubluepy_scanner_locals_dict_table[] = {
  87. { MP_ROM_QSTR(MP_QSTR_scan), MP_ROM_PTR(&ubluepy_scanner_scan_obj) },
  88. };
  89. STATIC MP_DEFINE_CONST_DICT(ubluepy_scanner_locals_dict, ubluepy_scanner_locals_dict_table);
  90. const mp_obj_type_t ubluepy_scanner_type = {
  91. { &mp_type_type },
  92. .name = MP_QSTR_Scanner,
  93. .print = ubluepy_scanner_print,
  94. .make_new = ubluepy_scanner_make_new,
  95. .locals_dict = (mp_obj_dict_t*)&ubluepy_scanner_locals_dict
  96. };
  97. #endif // MICROPY_PY_UBLUEPY_CENTRAL