|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2021 microDev |
| 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 "py/mpstate.h" |
| 30 | +#include "py/runtime.h" |
| 31 | + |
| 32 | +#include "shared-bindings/wifi/Monitor.h" |
| 33 | +#include "shared-bindings/wifi/Packet.h" |
| 34 | + |
| 35 | +#include "esp_log.h" |
| 36 | + |
| 37 | +#define MONITOR_PAYLOAD_FCS_LEN (4) |
| 38 | +#define MONITOR_QUEUE_TIMEOUT_TICK (0) |
| 39 | + |
| 40 | +typedef struct { |
| 41 | + void *payload; |
| 42 | + unsigned channel; |
| 43 | + uint32_t length; |
| 44 | + signed rssi; |
| 45 | +} monitor_packet_t; |
| 46 | + |
| 47 | +static const char *TAG = "monitor"; |
| 48 | + |
| 49 | +static void wifi_monitor_cb(void *recv_buf, wifi_promiscuous_pkt_type_t type) { |
| 50 | + wifi_promiscuous_pkt_t *pkt = (wifi_promiscuous_pkt_t *)recv_buf; |
| 51 | + |
| 52 | + // prepare packet |
| 53 | + monitor_packet_t packet = { |
| 54 | + .channel = pkt->rx_ctrl.channel, |
| 55 | + .length = pkt->rx_ctrl.sig_len, |
| 56 | + .rssi = pkt->rx_ctrl.rssi, |
| 57 | + }; |
| 58 | + |
| 59 | + // for now, the monitor only dumps the length of the MISC type frame |
| 60 | + if (type != WIFI_PKT_MISC && !pkt->rx_ctrl.rx_state) { |
| 61 | + packet.length -= MONITOR_PAYLOAD_FCS_LEN; |
| 62 | + packet.payload = malloc(packet.length); |
| 63 | + if (packet.payload) { |
| 64 | + memcpy(packet.payload, pkt->payload, packet.length); |
| 65 | + wifi_monitor_obj_t *self = MP_STATE_VM(wifi_monitor_singleton); |
| 66 | + if (self->queue) { |
| 67 | + // send packet |
| 68 | + if (xQueueSendFromISR(self->queue, &packet, NULL) != pdTRUE) { |
| 69 | + self->lost++; |
| 70 | + free(packet.payload); |
| 71 | + ESP_LOGE(TAG, "packet queue full"); |
| 72 | + } |
| 73 | + } |
| 74 | + } else { |
| 75 | + ESP_LOGE(TAG, "not enough memory for packet"); |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +void common_hal_wifi_monitor_construct(wifi_monitor_obj_t *self, uint8_t channel, size_t queue) { |
| 81 | + const compressed_string_t *monitor_mode_init_error = translate("monitor init failed"); |
| 82 | + |
| 83 | + self->queue = xQueueCreate(queue, sizeof(monitor_packet_t)); |
| 84 | + if (!self->queue) { |
| 85 | + mp_raise_RuntimeError(monitor_mode_init_error); |
| 86 | + } |
| 87 | + |
| 88 | + // start wifi promicuous mode |
| 89 | + wifi_promiscuous_filter_t wifi_filter = { |
| 90 | + .filter_mask = WIFI_PROMIS_FILTER_MASK_MGMT, |
| 91 | + }; |
| 92 | + esp_wifi_set_promiscuous_filter(&wifi_filter); |
| 93 | + esp_wifi_set_promiscuous_rx_cb(wifi_monitor_cb); |
| 94 | + if (esp_wifi_set_promiscuous(true) != ESP_OK) { |
| 95 | + mp_raise_RuntimeError(monitor_mode_init_error); |
| 96 | + } |
| 97 | + esp_wifi_set_channel(channel, WIFI_SECOND_CHAN_NONE); |
| 98 | + |
| 99 | + self->channel = channel; |
| 100 | + self->queue_length = queue; |
| 101 | +} |
| 102 | + |
| 103 | +bool common_hal_wifi_monitor_deinited(void) { |
| 104 | + bool enabled; |
| 105 | + return (esp_wifi_get_promiscuous(&enabled) == ESP_ERR_WIFI_NOT_INIT) ? true : !enabled; |
| 106 | +} |
| 107 | + |
| 108 | +void common_hal_wifi_monitor_deinit(wifi_monitor_obj_t *self) { |
| 109 | + if (common_hal_wifi_monitor_deinited()) { |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + // disable wifi promiscuous mode |
| 114 | + esp_wifi_set_promiscuous(false); |
| 115 | + |
| 116 | + // make sure to free all resources in the left items |
| 117 | + UBaseType_t left_items = uxQueueMessagesWaiting(self->queue); |
| 118 | + monitor_packet_t packet; |
| 119 | + while (left_items--) { |
| 120 | + xQueueReceive(self->queue, &packet, MONITOR_QUEUE_TIMEOUT_TICK); |
| 121 | + free(packet.payload); |
| 122 | + } |
| 123 | + vQueueDelete(self->queue); |
| 124 | + self->queue = NULL; |
| 125 | +} |
| 126 | + |
| 127 | +void common_hal_wifi_monitor_set_channel(wifi_monitor_obj_t *self, uint8_t channel) { |
| 128 | + self->channel = channel; |
| 129 | + esp_wifi_set_channel(channel, WIFI_SECOND_CHAN_NONE); |
| 130 | +} |
| 131 | + |
| 132 | +mp_obj_t common_hal_wifi_monitor_get_channel(wifi_monitor_obj_t *self) { |
| 133 | + return MP_OBJ_NEW_SMALL_INT(self->channel); |
| 134 | +} |
| 135 | + |
| 136 | +mp_obj_t common_hal_wifi_monitor_get_queue(wifi_monitor_obj_t *self) { |
| 137 | + return mp_obj_new_int_from_uint(self->queue_length); |
| 138 | +} |
| 139 | + |
| 140 | +mp_obj_t common_hal_wifi_monitor_get_lost(wifi_monitor_obj_t *self) { |
| 141 | + size_t lost = self->lost; |
| 142 | + self->lost = 0; |
| 143 | + return mp_obj_new_int_from_uint(lost); |
| 144 | +} |
| 145 | + |
| 146 | +mp_obj_t common_hal_wifi_monitor_get_queued(wifi_monitor_obj_t *self) { |
| 147 | + return mp_obj_new_int_from_uint(uxQueueMessagesWaiting(self->queue)); |
| 148 | +} |
| 149 | + |
| 150 | +mp_obj_t common_hal_wifi_monitor_get_packet(wifi_monitor_obj_t *self) { |
| 151 | + monitor_packet_t packet; |
| 152 | + |
| 153 | + if (xQueueReceive(self->queue, &packet, MONITOR_QUEUE_TIMEOUT_TICK) != pdTRUE) { |
| 154 | + return (mp_obj_t)&mp_const_empty_dict_obj; |
| 155 | + } |
| 156 | + |
| 157 | + mp_obj_dict_t *dict = MP_OBJ_TO_PTR(mp_obj_new_dict(4)); |
| 158 | + |
| 159 | + mp_obj_dict_store(dict, cp_enum_find(&wifi_packet_type, PACKET_CH), MP_OBJ_NEW_SMALL_INT(packet.channel)); |
| 160 | + |
| 161 | + mp_obj_dict_store(dict, cp_enum_find(&wifi_packet_type, PACKET_LEN), MP_OBJ_NEW_SMALL_INT(packet.length)); |
| 162 | + |
| 163 | + mp_obj_dict_store(dict, cp_enum_find(&wifi_packet_type, PACKET_RAW), mp_obj_new_bytes(packet.payload, packet.length)); |
| 164 | + free(packet.payload); |
| 165 | + |
| 166 | + mp_obj_dict_store(dict, cp_enum_find(&wifi_packet_type, PACKET_RSSI), MP_OBJ_NEW_SMALL_INT(packet.rssi)); |
| 167 | + |
| 168 | + return MP_OBJ_FROM_PTR(dict); |
| 169 | +} |
0 commit comments