105 lines
3.0 KiB
C++
105 lines
3.0 KiB
C++
#pragma once
|
|
|
|
#include "core/task_stack_budget.h"
|
|
#include "friend/friend_store.h"
|
|
#include "platform/borla_s3/ble_friend_exchange.h"
|
|
#include "ports/application_observer.h"
|
|
#include "ports/device_platform.h"
|
|
#include "ports/device_profile_sink.h"
|
|
#include "ports/event_publisher.h"
|
|
#include "ports/friend_service.h"
|
|
#include "ports/realtime_protocol.h"
|
|
#include "ports/connectivity_manager.h"
|
|
#include <array>
|
|
#include <atomic>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
|
|
#include <freertos/FreeRTOS.h>
|
|
#include <freertos/queue.h>
|
|
#include <freertos/task.h>
|
|
|
|
namespace borla {
|
|
|
|
class FriendService final
|
|
: public IFriendService,
|
|
public IBleFriendExchangeObserver,
|
|
public IDeviceProfileSink,
|
|
public IApplicationObserver {
|
|
public:
|
|
FriendService(
|
|
BleFriendExchange& exchange,
|
|
IFriendStore& store,
|
|
IRealtimeProtocol& realtime,
|
|
IConnectivityManager& connectivity);//2026-9-7 ckkj add
|
|
|
|
bool Initialize(
|
|
const DeviceIdentity& identity,
|
|
IEventPublisher& publisher);
|
|
void Start() override;
|
|
void Stop() override;
|
|
void SetDeviceName(const char* device_name) override;
|
|
void OnBleFriendMatched(const FriendPeer& peer) override;
|
|
void OnBleFriendExchangeStopped(
|
|
bool paired,
|
|
uint32_t error_id) override;
|
|
void OnApplicationEvent(
|
|
const AppEvent& event,
|
|
const RuntimeState& state) override;
|
|
|
|
private:
|
|
enum class CommandType : uint8_t {
|
|
kStart,
|
|
kStop,
|
|
kMatched,
|
|
kExchangeStopped,
|
|
kSetDeviceName,
|
|
kUploadPending,
|
|
};
|
|
|
|
struct Command {
|
|
CommandType type = CommandType::kStart;
|
|
FriendPeer peer{};
|
|
std::array<char, kFriendNameCapacity> device_name{};
|
|
uint32_t error_id = 0;
|
|
bool paired = false;
|
|
};
|
|
|
|
static constexpr size_t kQueueDepth = 10;
|
|
static constexpr UBaseType_t kTaskPriority = 5;
|
|
|
|
static void TaskEntry(void* context);
|
|
[[noreturn]] void Run();
|
|
bool Post(const Command& command);
|
|
void Handle(const Command& command);
|
|
void HandleStart();
|
|
void HandleMatched(const FriendPeer& peer);
|
|
void HandleSetDeviceName(const char* device_name);
|
|
void UploadPending();
|
|
const char* LocalName();
|
|
|
|
BleFriendExchange& exchange_;
|
|
IFriendStore& store_;
|
|
IRealtimeProtocol& realtime_;
|
|
IConnectivityManager& connectivity_;//2026-9-7 ckkj add
|
|
IEventPublisher* publisher_ = nullptr;
|
|
FriendStoreSnapshot snapshot_{};
|
|
std::array<char, kFriendMacCapacity> local_mac_{};
|
|
std::array<char, kFriendAdvertisingNameCapacity>
|
|
fallback_name_{};
|
|
|
|
QueueHandle_t queue_ = nullptr;
|
|
StaticQueue_t queue_control_{};
|
|
alignas(Command)
|
|
std::array<uint8_t, kQueueDepth * sizeof(Command)>
|
|
queue_storage_{};
|
|
TaskHandle_t task_ = nullptr;
|
|
StaticTask_t task_control_{};
|
|
std::array<
|
|
StackType_t,
|
|
task_stack_budget::kFriendServiceBytes> task_stack_{};
|
|
std::atomic<bool> initialized_{false};
|
|
};
|
|
|
|
} // namespace borla
|