2025-09-27 12:27:58 -07:00

80 lines
1.9 KiB
C++

/// © MiroZ 2024
#ifndef __WIFI_H__
#define __WIFI_H__
#include <WiFi.h>
#include <esp_timer.h>
#include "Settings.h"
#include "TaskMgr.h"
#define _USE_TIMER
class Wifi
{
public:
typedef enum
{
NOT_CONNECTED,
PENDING,
NOT_PROVISIONED,
CONNECTED,
PROVISIONING
} WIFI_STATUS;
protected:
uint8_t m_ignored[SETTINGS_NUM_WIFI_ENTRIES];
WIFI_STATUS m_wifi_status = WIFI_STATUS::NOT_CONNECTED;
#ifdef USE_TIMER
esp_timer_handle_t m_timer;
static void timerCallback(void* arg);
#endif
TaskHandle_t m_task = nullptr;
void task();
WIFI_STATUS startConnecting();
WIFI_STATUS connectTo(int index);
int scan();
bool m_pause = false;
public:
Wifi();
void startProvisioning();
void stopProvisioning();
bool isProvisioningTimedOut();
void resetProvisioningTimer();
void performBootupCalibration();
bool shouldPerformBootupCalibration();
protected:
void wifi_event(arduino_event_id_t event, arduino_event_info_t info);
unsigned long m_provision_start_time = 0;
static const unsigned long PROVISION_TIMEOUT_MS = 600000; // 10 minutes
bool m_provision_timeout = false;
public:
int start();
void pause(bool pause);
WIFI_STATUS status() { return m_wifi_status; }
WIFI_STATUS waitForConnection();
private:
// Single source of truth for fallback networks
static const char* FALLBACK_NETWORKS[];
static const char* FALLBACK_PASSWORDS[];
static const int NUM_FALLBACK_NETWORKS;
WIFI_STATUS connectToDefault();
WIFI_STATUS connectToFallbackNetwork(const char* ssid, const char* password);
unsigned long m_last_recalibration_time = 0;
static const unsigned long RECALIBRATION_COOLDOWN_MS = 3600000; // 1 hour
bool m_bootup_calibration_done = false;
bool shouldRecalibrateRadio(int failed_ssid_index);
void performRadioRecalibration();
bool isSSIDVisible(const char* ssid);
};
#endif /* __WIFI_H__ */