189 lines
5.6 KiB
C++
189 lines
5.6 KiB
C++
#include "dual_network_board.h"
|
|
#include "application.h"
|
|
|
|
#include <esp_log.h>
|
|
#include <esp_system.h>
|
|
#include <wifi_station.h>
|
|
#include "boards/borla_s3/config.h"
|
|
static const char *TAG = "DualNetworkBoard";
|
|
|
|
DualNetworkBoard::DualNetworkBoard(gpio_num_t ml307_tx_pin, gpio_num_t ml307_rx_pin,
|
|
gpio_num_t ml307_dtr_pin)
|
|
: ml307_tx_pin_(ml307_tx_pin),
|
|
ml307_rx_pin_(ml307_rx_pin),
|
|
ml307_dtr_pin_(ml307_dtr_pin)
|
|
{
|
|
// Check if previous boot flagged us to use 4G
|
|
Settings settings("network", true);
|
|
int use_4g = settings.GetInt("use_4g", 0);
|
|
// use_4g=1;
|
|
if (use_4g)
|
|
{
|
|
network_type_ = NetworkType::ML307;
|
|
// Clear flag immediately — next reboot will try WiFi again
|
|
settings.SetInt("use_4g", 0);
|
|
ESP_LOGI(TAG, "Boot mode: 4G (WiFi failed last boot, will retry WiFi on next reboot)");
|
|
current_board_ = std::make_unique<Ml307Board>(ml307_tx_pin_, ml307_rx_pin_, ml307_dtr_pin_);
|
|
}
|
|
else
|
|
{
|
|
network_type_ = NetworkType::WIFI;
|
|
ESP_LOGI(TAG, "Boot mode: WiFi (default)");
|
|
current_board_ = std::make_unique<WifiBoard>();
|
|
}
|
|
}
|
|
|
|
bool DualNetworkBoard::IsWifiConfigMode() const
|
|
{
|
|
if (network_type_ != NetworkType::WIFI || !current_board_)
|
|
return false;
|
|
auto *wifi_board = static_cast<WifiBoard *>(current_board_.get());
|
|
return wifi_board->IsWifiConfigMode();
|
|
}
|
|
|
|
void DualNetworkBoard::SwitchNetworkType()
|
|
{
|
|
if (network_type_ == NetworkType::WIFI)
|
|
{
|
|
ESP_LOGW(TAG, "Switching WiFi -> 4G, rebooting...");
|
|
Settings("network", true).SetInt("use_4g", 1);
|
|
// Keep PERP stable during reboot. Board boot logic handles the
|
|
// required ML307 cold boot in 4G mode.
|
|
}
|
|
else
|
|
{
|
|
ESP_LOGW(TAG, "Switching 4G -> WiFi, rebooting...");
|
|
// use_4g already cleared on boot, just restart
|
|
}
|
|
vTaskDelay(pdMS_TO_TICKS(500));
|
|
esp_restart();
|
|
}
|
|
|
|
std::string DualNetworkBoard::GetBoardType() const
|
|
{
|
|
return current_board_->GetBoardType();
|
|
}
|
|
|
|
void DualNetworkBoard::StartNetwork()
|
|
{
|
|
if (network_type_ == NetworkType::WIFI)
|
|
{
|
|
ESP_LOGI(TAG, "Starting network: WiFi");
|
|
|
|
auto *wifi_board = static_cast<WifiBoard *>(current_board_.get());
|
|
wifi_board->SetSkipBleOnTimeout(true);
|
|
current_board_->StartNetwork();
|
|
|
|
if (current_board_->IsNetworkConnected())
|
|
{
|
|
ESP_LOGI(TAG, "WiFi connected, starting WiFi monitor");
|
|
StartWifiMonitor();
|
|
return;
|
|
}
|
|
|
|
// WiFi failed — set one-time flag and reboot into 4G
|
|
ESP_LOGW(TAG, "WiFi failed, rebooting into 4G");
|
|
Settings("network", true).SetInt("use_4g", 1);
|
|
vTaskDelay(pdMS_TO_TICKS(500));
|
|
esp_restart();
|
|
}
|
|
else
|
|
{
|
|
ESP_LOGI(TAG, "Starting network: 4G");
|
|
current_board_->StartNetwork();
|
|
}
|
|
}
|
|
|
|
NetworkInterface *DualNetworkBoard::GetNetwork()
|
|
{
|
|
return current_board_->GetNetwork();
|
|
}
|
|
|
|
bool DualNetworkBoard::IsNetworkConnected() const
|
|
{
|
|
return current_board_->IsNetworkConnected();
|
|
}
|
|
|
|
void DualNetworkBoard::SetPowerSaveMode(bool enabled)
|
|
{
|
|
current_board_->SetPowerSaveMode(enabled);
|
|
}
|
|
|
|
// === WiFi monitor: reboot and switch to 4G on persistent WiFi failure ===
|
|
|
|
void DualNetworkBoard::StartWifiMonitor()
|
|
{
|
|
xTaskCreate(WifiMonitorTaskFunc, "wifi_mon", 3072, this, 1, &wifi_monitor_task_);
|
|
ESP_LOGI(TAG, "WiFi monitor started (failover timeout=%ds, RSSI threshold=%ddBm)",
|
|
WIFI_FAILOVER_TIMEOUT_MS / 1000, WIFI_RSSI_THRESHOLD);
|
|
}
|
|
|
|
void DualNetworkBoard::WifiMonitorTaskFunc(void * /*arg*/)
|
|
{
|
|
auto &station = WifiStation::GetInstance();
|
|
|
|
int disconnect_elapsed_ms = 0;
|
|
int weak_signal_count = 0;
|
|
|
|
// Wait for initial WiFi connection to stabilize
|
|
vTaskDelay(pdMS_TO_TICKS(10000));
|
|
|
|
while (true)
|
|
{
|
|
vTaskDelay(pdMS_TO_TICKS(WIFI_CHECK_INTERVAL_MS));
|
|
|
|
// Don't failover during shutdown
|
|
if (Application::GetInstance().shutting_down_.load())
|
|
{
|
|
continue;
|
|
}
|
|
|
|
bool connected = station.IsConnected();
|
|
|
|
if (!connected)
|
|
{
|
|
disconnect_elapsed_ms += WIFI_CHECK_INTERVAL_MS;
|
|
weak_signal_count = 0;
|
|
|
|
ESP_LOGW(TAG, "WiFi disconnected for %ds / %ds",
|
|
disconnect_elapsed_ms / 1000, WIFI_FAILOVER_TIMEOUT_MS / 1000);
|
|
|
|
if (disconnect_elapsed_ms >= WIFI_FAILOVER_TIMEOUT_MS)
|
|
{
|
|
ESP_LOGE(TAG, "WiFi lost — switching to 4G via reboot");
|
|
Settings("network", true).SetInt("use_4g", 1);
|
|
vTaskDelay(pdMS_TO_TICKS(500));
|
|
esp_restart();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
disconnect_elapsed_ms = 0;
|
|
|
|
int8_t rssi = station.GetRssi();
|
|
if (rssi < WIFI_RSSI_THRESHOLD && rssi != 0)
|
|
{
|
|
weak_signal_count++;
|
|
ESP_LOGW(TAG, "WiFi signal weak: RSSI=%d dBm (%d/%d)",
|
|
rssi, weak_signal_count, WIFI_WEAK_SIGNAL_COUNT);
|
|
|
|
if (weak_signal_count >= WIFI_WEAK_SIGNAL_COUNT)
|
|
{
|
|
ESP_LOGE(TAG, "WiFi signal persistently weak — switching to 4G via reboot");
|
|
Settings("network", true).SetInt("use_4g", 1);
|
|
vTaskDelay(pdMS_TO_TICKS(500));
|
|
esp_restart();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (weak_signal_count > 0)
|
|
{
|
|
ESP_LOGI(TAG, "WiFi signal recovered: RSSI=%d dBm", rssi);
|
|
}
|
|
weak_signal_count = 0;
|
|
}
|
|
}
|
|
}
|
|
}
|