Branch data Line data Source code
1 : : // src/channel_monitor.cpp
2 : : #include <cstdint>
3 : :
4 : : #define LOG_LOCAL_LEVEL ESP_LOG_INFO
5 : : #include "esp_log.h"
6 : :
7 : : #include "protocol_types.hpp"
8 : : #include "channel_monitor.hpp"
9 : :
10 : : static const char* TAG = "ChannelMonitor";
11 : :
12 : 8 : ChannelMonitor::ChannelMonitor(IWiFiHAL& hal_wifi, IFreeRTOSHAL& hal_freertos)
13 : 8 : : hal_wifi_(hal_wifi)
14 : 8 : , hal_freertos_(hal_freertos)
15 : : {
16 : 8 : }
17 : :
18 : 16 : ChannelMonitor::~ChannelMonitor()
19 : : {
20 : 8 : is_active_ = false;
21 : 16 : }
22 : :
23 : 8 : esp_err_t ChannelMonitor::init(uint32_t interval_ms, TaskHandle_t rx_task_handle)
24 : : {
25 [ + + ]: 8 : if (rx_task_handle == nullptr) {
26 : : return ESP_ERR_INVALID_ARG;
27 : : }
28 : 6 : rx_task_handle_ = rx_task_handle;
29 : 6 : interval_ms_ = interval_ms;
30 : 6 : last_known_channel_ = verify_wifi_channel();
31 : 6 : is_active_ = true;
32 : 6 : return ESP_OK;
33 : : }
34 : :
35 : 1 : void ChannelMonitor::deinit()
36 : : {
37 : 1 : is_active_ = false;
38 : 1 : rx_task_handle_ = nullptr;
39 : 1 : }
40 : :
41 : 9 : void ChannelMonitor::tick(int64_t now_ms)
42 : : {
43 : : // If not initialized or the interval has not passed, nothing to do here
44 [ + + + + ]: 9 : if (!is_active_ || (now_ms - last_check_ms_ < interval_ms_)) {
45 : : return;
46 : : }
47 : :
48 : : // When the interval has passed, update the last check time
49 : 3 : last_check_ms_ = now_ms;
50 : :
51 : 3 : uint8_t current_channel = verify_wifi_channel(); // Get the current WiFi channel
52 : :
53 [ + + ]: 3 : if (current_channel != last_known_channel_.load()) { // Check if the channel has changed
54 : 1 : last_known_channel_.store(current_channel); // Update the last known channel member
55 : 1 : notify_rx_task(NOTIFY_CHANNEL_CHANGED); // Notify rx_task about channel change
56 : : }
57 : : }
58 : :
59 : 9 : uint8_t ChannelMonitor::verify_wifi_channel()
60 : : {
61 : 9 : uint8_t channel;
62 : 9 : wifi_second_chan_t second_chan;
63 : 9 : esp_err_t err = hal_wifi_.wifi_get_channel(&channel, &second_chan);
64 [ + + ]: 9 : if (err != ESP_OK) {
65 : 1 : ESP_LOGE(TAG, "Failed to get WiFi channel: %s", esp_err_to_name(err));
66 : 1 : return last_known_channel_.load();
67 : : }
68 : 8 : return channel;
69 : : }
70 : :
71 : 1 : void ChannelMonitor::notify_rx_task(uint32_t notifications)
72 : : {
73 : 1 : hal_freertos_.task_notify(rx_task_handle_, notifications, eSetBits);
74 : 1 : }
|