|
| 1 | +/* |
| 2 | + * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2015 Paul Sokolovsky |
| 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 | + |
| 27 | +#include <stdio.h> |
| 28 | +#include <string.h> |
| 29 | +#include <stdbool.h> |
| 30 | + |
| 31 | +#include "py/nlr.h" |
| 32 | +#include "py/obj.h" |
| 33 | +#include "py/gc.h" |
| 34 | +#include "py/runtime.h" |
| 35 | +#include MICROPY_HAL_H |
| 36 | +#include "queue.h" |
| 37 | +#include "user_interface.h" |
| 38 | + |
| 39 | +STATIC mp_obj_t scan_cb_obj; |
| 40 | + |
| 41 | +STATIC void esp_scan_cb(scaninfo *si, STATUS status) { |
| 42 | + //printf("in pyb_scan_cb: %d, si=%p, si->pbss=%p\n", status, si, si->pbss); |
| 43 | + struct bss_info *bs; |
| 44 | + if (si->pbss) { |
| 45 | + STAILQ_FOREACH(bs, si->pbss, next) { |
| 46 | + mp_obj_tuple_t *t = mp_obj_new_tuple(6, NULL); |
| 47 | + t->items[0] = mp_obj_new_bytes(bs->ssid, strlen((char*)bs->ssid)); |
| 48 | + t->items[1] = mp_obj_new_bytes(bs->bssid, sizeof(bs->bssid)); |
| 49 | + t->items[2] = MP_OBJ_NEW_SMALL_INT(bs->channel); |
| 50 | + t->items[3] = MP_OBJ_NEW_SMALL_INT(bs->rssi); |
| 51 | + t->items[4] = MP_OBJ_NEW_SMALL_INT(bs->authmode); |
| 52 | + t->items[5] = MP_OBJ_NEW_SMALL_INT(bs->is_hidden); |
| 53 | + mp_call_function_1(scan_cb_obj, t); |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +STATIC mp_obj_t esp_scan(mp_obj_t cb_in) { |
| 59 | + scan_cb_obj = cb_in; |
| 60 | + wifi_set_opmode(STATION_MODE); |
| 61 | + wifi_station_scan(NULL, (scan_done_cb_t)esp_scan_cb); |
| 62 | + return mp_const_none; |
| 63 | +} |
| 64 | +STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp_scan_obj, esp_scan); |
| 65 | + |
| 66 | +STATIC const mp_map_elem_t esp_module_globals_table[] = { |
| 67 | + { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_esp) }, |
| 68 | + |
| 69 | + { MP_OBJ_NEW_QSTR(MP_QSTR_scan), (mp_obj_t)&esp_scan_obj }, |
| 70 | +}; |
| 71 | + |
| 72 | +STATIC MP_DEFINE_CONST_DICT(esp_module_globals, esp_module_globals_table); |
| 73 | + |
| 74 | +const mp_obj_module_t esp_module = { |
| 75 | + .base = { &mp_type_module }, |
| 76 | + .name = MP_QSTR_esp, |
| 77 | + .globals = (mp_obj_dict_t*)&esp_module_globals, |
| 78 | +}; |
0 commit comments