|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2020 Scott Shawcroft for Adafruit Industries |
| 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 <string.h> |
| 28 | + |
| 29 | +#include "shared-bindings/wifi/Network.h" |
| 30 | +#include "shared-bindings/wifi/AuthMode.h" |
| 31 | + |
| 32 | +mp_obj_t common_hal_wifi_network_get_ssid(wifi_network_obj_t *self) { |
| 33 | + const char *cstr = (const char *)self->record.ssid; |
| 34 | + return mp_obj_new_str(cstr, self->record.ssid_len); |
| 35 | +} |
| 36 | + |
| 37 | +#define MAC_ADDRESS_LENGTH 6 |
| 38 | + |
| 39 | +mp_obj_t common_hal_wifi_network_get_bssid(wifi_network_obj_t *self) { |
| 40 | + return mp_obj_new_bytes(self->record.bssid, MAC_ADDRESS_LENGTH); |
| 41 | +} |
| 42 | + |
| 43 | +mp_obj_t common_hal_wifi_network_get_rssi(wifi_network_obj_t *self) { |
| 44 | + return mp_obj_new_int(self->record.rssi); |
| 45 | +} |
| 46 | + |
| 47 | +mp_obj_t common_hal_wifi_network_get_channel(wifi_network_obj_t *self) { |
| 48 | + return mp_obj_new_int(self->record.channel); |
| 49 | +} |
| 50 | + |
| 51 | +mp_obj_t common_hal_wifi_network_get_country(wifi_network_obj_t *self) { |
| 52 | + return (mp_obj_t)MP_QSTR_; |
| 53 | +} |
| 54 | + |
| 55 | +mp_obj_t common_hal_wifi_network_get_authmode(wifi_network_obj_t *self) { |
| 56 | + uint8_t authmode_mask = 0; |
| 57 | + if (self->record.auth_mode == 0) { |
| 58 | + authmode_mask = (1 << AUTHMODE_OPEN); |
| 59 | + } |
| 60 | + if (self->record.auth_mode & 1) { |
| 61 | + authmode_mask |= (1 << AUTHMODE_PSK); |
| 62 | + } |
| 63 | + ; |
| 64 | + if (self->record.auth_mode & 2) { |
| 65 | + authmode_mask |= (1 << AUTHMODE_WPA); |
| 66 | + } |
| 67 | + ; |
| 68 | + if (self->record.auth_mode & 4) { |
| 69 | + authmode_mask |= (1 << AUTHMODE_WPA2); |
| 70 | + } |
| 71 | + ; |
| 72 | + mp_obj_t authmode_list = mp_obj_new_list(0, NULL); |
| 73 | + if (authmode_mask != 0) { |
| 74 | + for (uint8_t i = 0; i < 8; i++) { |
| 75 | + if ((authmode_mask >> i) & 1) { |
| 76 | + mp_obj_list_append(authmode_list, cp_enum_find(&wifi_authmode_type, i)); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + return authmode_list; |
| 81 | +} |
0 commit comments