|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2016 Glenn Ruben Bakke |
| 7 | + * Copyright (c) 2018 Artur Pacholec |
| 8 | + * |
| 9 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | + * of this software and associated documentation files (the "Software"), to deal |
| 11 | + * in the Software without restriction, including without limitation the rights |
| 12 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | + * copies of the Software, and to permit persons to whom the Software is |
| 14 | + * furnished to do so, subject to the following conditions: |
| 15 | + * |
| 16 | + * The above copyright notice and this permission notice shall be included in |
| 17 | + * all copies or substantial portions of the Software. |
| 18 | + * |
| 19 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | + * THE SOFTWARE. |
| 26 | + */ |
| 27 | + |
| 28 | +#include <string.h> |
| 29 | + |
| 30 | +#include "ble_drv.h" |
| 31 | +#include "py/mphal.h" |
| 32 | +#include "shared-bindings/bleio/Scanner.h" |
| 33 | +#include "shared-bindings/bleio/ScanEntry.h" |
| 34 | + |
| 35 | +STATIC void adv_event_handler(bleio_scanner_obj_t *self, ble_drv_adv_data_t *data) { |
| 36 | + // TODO: Don't add new entry for each item, group by address and update |
| 37 | + bleio_scanentry_obj_t *item = m_new_obj(bleio_scanentry_obj_t); |
| 38 | + item->base.type = &bleio_scanentry_type; |
| 39 | + |
| 40 | + item->rssi = data->rssi; |
| 41 | + item->data = mp_obj_new_bytearray(data->data_len, data->p_data); |
| 42 | + |
| 43 | + item->address.type = data->addr_type; |
| 44 | + memcpy(item->address.value, data->p_peer_addr, BLEIO_ADDRESS_BYTES); |
| 45 | + |
| 46 | + mp_obj_list_append(self->adv_reports, item); |
| 47 | + |
| 48 | + ble_drv_scan_continue(); |
| 49 | +} |
| 50 | + |
| 51 | +void common_hal_bleio_scanner_scan(bleio_scanner_obj_t *self, mp_int_t timeout) { |
| 52 | + ble_drv_adv_report_handler_set(self, adv_event_handler); |
| 53 | + |
| 54 | + ble_drv_scan_start(self->interval, self->window); |
| 55 | + |
| 56 | + mp_hal_delay_ms(timeout); |
| 57 | + |
| 58 | + ble_drv_scan_stop(); |
| 59 | +} |
0 commit comments