|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2019 Dan Halbert for Adafruit Industries |
| 7 | + * Copyright (c) 2018 Artur Pacholec |
| 8 | + * Copyright (c) 2017 Glenn Ruben Bakke |
| 9 | + * |
| 10 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 11 | + * of this software and associated documentation files (the "Software"), to deal |
| 12 | + * in the Software without restriction, including without limitation the rights |
| 13 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 14 | + * copies of the Software, and to permit persons to whom the Software is |
| 15 | + * furnished to do so, subject to the following conditions: |
| 16 | + * |
| 17 | + * The above copyright notice and this permission notice shall be included in |
| 18 | + * all copies or substantial portions of the Software. |
| 19 | + * |
| 20 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 21 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 23 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 24 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 25 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 26 | + * THE SOFTWARE. |
| 27 | + */ |
| 28 | + |
| 29 | +#include <string.h> |
| 30 | + |
| 31 | +#include "lib/utils/interrupt_char.h" |
| 32 | +#include "py/objstr.h" |
| 33 | +#include "py/runtime.h" |
| 34 | +#include "shared-bindings/wifi/__init__.h" |
| 35 | +#include "shared-bindings/wifi/Network.h" |
| 36 | +#include "shared-bindings/wifi/Radio.h" |
| 37 | +#include "shared-bindings/wifi/ScannedNetworks.h" |
| 38 | + |
| 39 | +#include "esp-idf/components/esp_wifi/include/esp_wifi.h" |
| 40 | + |
| 41 | +static void wifi_scannednetworks_done(wifi_scannednetworks_obj_t *self) { |
| 42 | + self->done = true; |
| 43 | + free(self->results); |
| 44 | + self->results = NULL; |
| 45 | +} |
| 46 | + |
| 47 | +static bool wifi_scannednetworks_wait_for_scan(wifi_scannednetworks_obj_t *self) { |
| 48 | + EventBits_t bits = xEventGroupWaitBits(self->radio_event_group, |
| 49 | + WIFI_SCAN_DONE_BIT, |
| 50 | + pdTRUE, |
| 51 | + pdTRUE, |
| 52 | + 0); |
| 53 | + while ((bits & WIFI_SCAN_DONE_BIT) == 0 && !mp_hal_is_interrupted()) { |
| 54 | + RUN_BACKGROUND_TASKS; |
| 55 | + bits = xEventGroupWaitBits(self->radio_event_group, |
| 56 | + WIFI_SCAN_DONE_BIT, |
| 57 | + pdTRUE, |
| 58 | + pdTRUE, |
| 59 | + 0); |
| 60 | + } |
| 61 | + return !mp_hal_is_interrupted(); |
| 62 | +} |
| 63 | + |
| 64 | +mp_obj_t common_hal_wifi_scannednetworks_next(wifi_scannednetworks_obj_t *self) { |
| 65 | + if (self->done) { |
| 66 | + return mp_const_none; |
| 67 | + } |
| 68 | + // If we are scanning, wait and then load them. |
| 69 | + if (self->scanning) { |
| 70 | + // We may have to scan more than one channel to get a result. |
| 71 | + while (!self->done) { |
| 72 | + if (!wifi_scannednetworks_wait_for_scan(self)) { |
| 73 | + wifi_scannednetworks_done(self); |
| 74 | + return mp_const_none; |
| 75 | + } |
| 76 | + |
| 77 | + esp_wifi_scan_get_ap_num(&self->total_results); |
| 78 | + if (self->total_results > 0) { |
| 79 | + break; |
| 80 | + } |
| 81 | + // If total_results is zero then we need to start a scan and wait again. |
| 82 | + wifi_scannednetworks_scan_next_channel(self); |
| 83 | + } |
| 84 | + // We not have found any more results so we're done. |
| 85 | + if (self->done) { |
| 86 | + return mp_const_none; |
| 87 | + } |
| 88 | + // If we need more space than we have, realloc. |
| 89 | + if (self->total_results > self->max_results) { |
| 90 | + wifi_ap_record_t* results = m_renew_maybe(wifi_ap_record_t, |
| 91 | + self->results, |
| 92 | + self->max_results, |
| 93 | + self->total_results, |
| 94 | + true /* allow move */); |
| 95 | + if (results != NULL) { |
| 96 | + self->results = results; |
| 97 | + self->max_results = self->total_results; |
| 98 | + } else { |
| 99 | + if (self->max_results == 0) { |
| 100 | + // No room for any results should error. |
| 101 | + mp_raise_msg(&mp_type_MemoryError, translate("Failed to allocate wifi scan memory")); |
| 102 | + } |
| 103 | + // Unable to allocate more results, so load what we can. |
| 104 | + self->total_results = self->max_results; |
| 105 | + } |
| 106 | + } |
| 107 | + esp_wifi_scan_get_ap_records(&self->total_results, self->results); |
| 108 | + self->scanning = false; |
| 109 | + } |
| 110 | + |
| 111 | + wifi_network_obj_t *entry = m_new_obj(wifi_network_obj_t); |
| 112 | + entry->base.type = &wifi_network_type; |
| 113 | + memcpy(&entry->record, &self->results[self->current_result], sizeof(wifi_ap_record_t)); |
| 114 | + self->current_result++; |
| 115 | + |
| 116 | + // If we're returning our last network then start the next channel scan or |
| 117 | + // be done. |
| 118 | + if (self->current_result >= self->total_results) { |
| 119 | + wifi_scannednetworks_scan_next_channel(self); |
| 120 | + self->total_results = 0; |
| 121 | + self->current_result = 0; |
| 122 | + } |
| 123 | + |
| 124 | + return MP_OBJ_FROM_PTR(entry); |
| 125 | +} |
| 126 | + |
| 127 | +// We don't do a linear scan so that we look at a variety of spectrum up front. |
| 128 | +static uint8_t scan_pattern[] = {6, 1, 11, 3, 9, 13, 2, 4, 8, 12, 5, 7, 10, 14}; |
| 129 | + |
| 130 | +void wifi_scannednetworks_scan_next_channel(wifi_scannednetworks_obj_t *self) { |
| 131 | + wifi_scan_config_t config; |
| 132 | + uint8_t next_channel = sizeof(scan_pattern); |
| 133 | + while (self->current_channel_index < sizeof(scan_pattern)) { |
| 134 | + next_channel = scan_pattern[self->current_channel_index]; |
| 135 | + self->current_channel_index++; |
| 136 | + if (self->start_channel <= next_channel && next_channel <= self->end_channel) { |
| 137 | + break; |
| 138 | + } |
| 139 | + } |
| 140 | + if (next_channel == sizeof(scan_pattern) || |
| 141 | + esp_wifi_scan_start(&config, false) != ESP_OK) { |
| 142 | + wifi_scannednetworks_done(self); |
| 143 | + } else { |
| 144 | + self->scanning = true; |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +void wifi_scannednetworks_deinit(wifi_scannednetworks_obj_t* self) { |
| 149 | + // if a scan is active, make sure and clean up the idf's buffer of results. |
| 150 | + if (self->scanning) { |
| 151 | + esp_wifi_scan_stop(); |
| 152 | + if (wifi_scannednetworks_wait_for_scan(self)) { |
| 153 | + uint16_t number = 0; |
| 154 | + esp_wifi_scan_get_ap_records(&number, NULL); |
| 155 | + self->scanning = false; |
| 156 | + } |
| 157 | + } |
| 158 | + wifi_scannednetworks_done(self); |
| 159 | +} |
0 commit comments