108 lines
3.3 KiB
C++
108 lines
3.3 KiB
C++
#pragma once
|
|
|
|
#include "connectivity/connectivity_policy.h"
|
|
#include "connectivity/wifi_credential_store.h"
|
|
#include "connectivity/wifi_transport.h"
|
|
#include "core/task_stack_budget.h"
|
|
#include "ports/connectivity_manager.h"
|
|
#include "ports/event_publisher.h"
|
|
#include "ports/remote_behavior_config.h"
|
|
|
|
#include <array>
|
|
#include <atomic>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
|
|
#include <esp_timer.h>
|
|
#include <freertos/FreeRTOS.h>
|
|
#include <freertos/portmacro.h>
|
|
#include <freertos/queue.h>
|
|
#include <freertos/semphr.h>
|
|
#include <freertos/task.h>
|
|
|
|
namespace borla {
|
|
|
|
class ConnectivityManager final : public IConnectivityManager,
|
|
public IWifiTransportObserver {
|
|
public:
|
|
ConnectivityManager(IWifiTransport& transport,
|
|
IWifiCredentialStore& credentials, ReconnectBackoffConfig retry_config);
|
|
|
|
bool Initialize();
|
|
void SetEventPublisher(IEventPublisher* publisher)
|
|
{
|
|
publisher_ = publisher;
|
|
}
|
|
void SetRemoteBehaviorConfig(IRemoteBehaviorConfig* config)
|
|
{
|
|
remote_behavior_config_ = config;
|
|
}
|
|
|
|
void Start() override;
|
|
void Stop() override;
|
|
void SwitchNetwork() override;
|
|
// 2026-9-7 ckkj add: BLE 好友模式前后静默挂起/恢复网络,释放 WiFi 驱动内存。
|
|
void SuspendForBle() override;
|
|
void ResumeAfterBle() override;//////////
|
|
bool IsOnline() const override { return online_.load(); }
|
|
ConnectivityDiagnostics GetDiagnostics() const override;
|
|
|
|
void OnWifiTransportEvent(const WifiTransportEvent& event) override;
|
|
|
|
private:
|
|
enum class ManagerEventType : uint8_t {
|
|
kStart,
|
|
kStop,
|
|
kTransportReady,
|
|
kTransportStartFailed,
|
|
kGotIp,
|
|
kDisconnected,
|
|
kRetryElapsed,
|
|
kRetryNow,
|
|
};
|
|
|
|
struct ManagerEvent {
|
|
ManagerEventType type = ManagerEventType::kStart;
|
|
uint16_t disconnect_reason = 0;
|
|
};
|
|
|
|
static constexpr size_t kQueueDepth = 24;
|
|
static constexpr UBaseType_t kTaskPriority = 4;
|
|
|
|
static void TaskEntry(void* context);
|
|
static void RetryTimerCallback(void* context);
|
|
|
|
bool Post(const ManagerEvent& event);
|
|
[[noreturn]] void Run();
|
|
void Handle(const ManagerEvent& event);
|
|
void Execute(const ConnectivityPolicyResult& result);
|
|
void Execute(const ConnectivityAction& action);
|
|
void RefreshDiagnostics();
|
|
void PublishAppEvent(const AppEvent& event);
|
|
|
|
IWifiTransport& transport_;
|
|
IWifiCredentialStore& credential_store_;
|
|
ConnectivityPolicy policy_;
|
|
IEventPublisher* publisher_ = nullptr;
|
|
IRemoteBehaviorConfig* remote_behavior_config_ = nullptr;
|
|
std::array<WifiCredential, kMaxSavedWifiCredentials> credentials_ {};
|
|
size_t credential_count_ = 0;
|
|
|
|
QueueHandle_t queue_ = nullptr;
|
|
StaticQueue_t queue_control_ {};
|
|
alignas(ManagerEvent) std::array<uint8_t,
|
|
kQueueDepth * sizeof(ManagerEvent)> queue_storage_ {};
|
|
TaskHandle_t task_ = nullptr;
|
|
SemaphoreHandle_t stop_completed_ = nullptr;
|
|
StaticSemaphore_t stop_completed_control_ {};
|
|
esp_timer_handle_t retry_timer_ = nullptr;
|
|
bool initialized_ = false;
|
|
|
|
std::atomic<bool> online_ { false };
|
|
std::atomic<bool> ble_suspended_ { false };//2026-9-7 ckkj add
|
|
mutable portMUX_TYPE diagnostics_lock_ = portMUX_INITIALIZER_UNLOCKED;
|
|
ConnectivityDiagnostics diagnostics_ {};
|
|
};
|
|
|
|
} // namespace borla
|