117 lines
3.5 KiB
C++
117 lines
3.5 KiB
C++
#pragma once
|
|
|
|
#include "connectivity/wifi_credential_store.h"
|
|
#include "connectivity/wifi_transport.h"
|
|
#include "core/task_stack_budget.h"
|
|
#include "platform/borla_s3/ble_provisioning_gatt.h"
|
|
#include "ports/audio_engine.h"
|
|
#include "ports/device_platform.h"
|
|
#include "ports/event_publisher.h"
|
|
#include "ports/provisioning_service.h"
|
|
|
|
#include <array>
|
|
#include <atomic>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
|
|
#include <freertos/FreeRTOS.h>
|
|
#include <freertos/event_groups.h>
|
|
#include <freertos/queue.h>
|
|
#include <freertos/task.h>
|
|
|
|
namespace borla {
|
|
|
|
class BleProvisioningService final
|
|
: public IProvisioningService,
|
|
public IBleProvisioningGattObserver,
|
|
public IWifiTransportObserver {
|
|
public:
|
|
BleProvisioningService(
|
|
IWifiTransport& wifi,
|
|
IWifiCredentialStore& credentials,
|
|
IDevicePlatform& platform,
|
|
IAudioEngine& audio);
|
|
|
|
bool Initialize();
|
|
void SetEventPublisher(IEventPublisher* publisher);
|
|
void Start() override;
|
|
void Stop() override;
|
|
|
|
void OnBleWifiScanRequested() override;
|
|
void OnBleWifiCredential(
|
|
const WifiCredential& credential) override;
|
|
void OnBleSavedWifiListRequested() override;
|
|
void OnBleSavedWifiDeleteRequested(uint8_t index) override;
|
|
void OnBleRebootRequested() override;
|
|
void OnWifiTransportEvent(
|
|
const WifiTransportEvent& event) override;
|
|
|
|
private:
|
|
enum class CommandType : uint8_t {
|
|
kStart,
|
|
kStop,
|
|
kSendScan,
|
|
kConnectCredential,
|
|
kListSavedWifi,
|
|
kDeleteSavedWifi,
|
|
kReboot,
|
|
};
|
|
|
|
struct Command {
|
|
CommandType type = CommandType::kStart;
|
|
WifiCredential credential{};
|
|
uint8_t index = 0;
|
|
};
|
|
|
|
static constexpr size_t kQueueDepth = 12;
|
|
static constexpr UBaseType_t kTaskPriority = 5;
|
|
static constexpr EventBits_t kWifiStartedBit = BIT0;
|
|
static constexpr EventBits_t kWifiConnectedBit = BIT1;
|
|
static constexpr EventBits_t kWifiDisconnectedBit = BIT2;
|
|
static constexpr EventBits_t kWifiBits =
|
|
kWifiStartedBit | kWifiConnectedBit | kWifiDisconnectedBit;
|
|
|
|
static void TaskEntry(void* context);
|
|
[[noreturn]] void Run();
|
|
bool Post(const Command& command);
|
|
void Handle(const Command& command);
|
|
void HandleStart();
|
|
void HandleConnect(WifiCredential credential);
|
|
void HandleListSavedWifi();
|
|
void HandleDeleteSavedWifi(uint8_t index);
|
|
bool RestoreSavedPassword(WifiCredential* credential);
|
|
const char* MapDisconnectError(uint16_t reason) const;
|
|
void Publish(const AppEvent& event);
|
|
|
|
IWifiTransport& wifi_;
|
|
IWifiCredentialStore& credentials_;
|
|
IDevicePlatform& platform_;
|
|
IAudioEngine& audio_;
|
|
BleProvisioningGattServer gatt_;
|
|
std::array<WifiNetwork, kMaxProvisioningScanResults>
|
|
scan_results_{};
|
|
size_t scan_count_ = 0;
|
|
|
|
QueueHandle_t queue_ = nullptr;
|
|
StaticQueue_t queue_control_{};
|
|
alignas(Command)
|
|
std::array<uint8_t, kQueueDepth * sizeof(Command)>
|
|
queue_storage_{};
|
|
EventGroupHandle_t wifi_events_ = nullptr;
|
|
StaticEventGroup_t wifi_events_control_{};
|
|
TaskHandle_t task_ = nullptr;
|
|
StaticTask_t task_control_{};
|
|
std::array<
|
|
StackType_t,
|
|
task_stack_budget::kBleProvisioningBytes>
|
|
task_stack_{};
|
|
|
|
std::atomic<IEventPublisher*> publisher_{nullptr};
|
|
std::atomic<uint16_t> disconnect_reason_{0};
|
|
std::atomic<bool> initialized_{false};
|
|
bool active_ = false;
|
|
bool connection_attempt_active_ = false;
|
|
};
|
|
|
|
} // namespace borla
|