blob: 1489b7904004b5c955b125d2206a523c4b52a7f0 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
#pragma once
#include <array>
#include <atomic>
#include <map>
#include <mutex>
#include <optional>
#include <string>
#include <vector>
#include <freertos/FreeRTOS.h>
#include <freertos/stream_buffer.h>
#include "bluetooth_types.hpp"
#include "esp_a2dp_api.h"
#include "esp_avrc_api.h"
#include "esp_gap_bt_api.h"
#include "nvs.hpp"
#include "tinyfsm.hpp"
#include "tinyfsm/include/tinyfsm.hpp"
namespace drivers {
/*
* A handle used to interact with the bluetooth state machine.
*/
class Bluetooth {
public:
Bluetooth(NvsStorage& storage);
auto Enable() -> bool;
auto Disable() -> void;
auto IsEnabled() -> bool;
auto KnownDevices() -> std::vector<bluetooth::Device>;
auto SetPreferredDevice(const bluetooth::mac_addr_t& mac) -> void;
auto SetSource(StreamBufferHandle_t) -> void;
auto SetEventHandler(std::function<void(bluetooth::Event)> cb) -> void;
};
namespace bluetooth {
namespace events {
struct Enable : public tinyfsm::Event {};
struct Disable : public tinyfsm::Event {};
struct PreferredDeviceChanged : public tinyfsm::Event {};
struct SourceChanged : public tinyfsm::Event {};
namespace internal {
struct Gap : public tinyfsm::Event {
esp_bt_gap_cb_event_t type;
esp_bt_gap_cb_param_t* param;
};
struct A2dp : public tinyfsm::Event {
esp_a2d_cb_event_t type;
esp_a2d_cb_param_t* param;
};
struct Avrc : public tinyfsm::Event {
esp_avrc_ct_cb_event_t type;
esp_avrc_ct_cb_param_t* param;
};
} // namespace internal
} // namespace events
class BluetoothState : public tinyfsm::Fsm<BluetoothState> {
public:
static auto Init(NvsStorage& storage) -> void;
static auto devices() -> std::vector<Device>;
static auto preferred_device() -> std::optional<mac_addr_t>;
static auto preferred_device(const mac_addr_t&) -> void;
static auto source() -> StreamBufferHandle_t;
static auto source(StreamBufferHandle_t) -> void;
static auto event_handler(std::function<void(Event)>) -> void;
virtual ~BluetoothState(){};
virtual void entry() {}
virtual void exit() {}
virtual void react(const events::Enable& ev){};
virtual void react(const events::Disable& ev) = 0;
virtual void react(const events::PreferredDeviceChanged& ev){};
virtual void react(const events::SourceChanged& ev){};
virtual void react(const events::internal::Gap& ev) = 0;
virtual void react(const events::internal::A2dp& ev){};
virtual void react(const events::internal::Avrc& ev){};
protected:
static NvsStorage* sStorage_;
static std::mutex sDevicesMutex_;
static std::map<mac_addr_t, Device> sDevices_;
static std::optional<mac_addr_t> sPreferredDevice_;
static mac_addr_t sCurrentDevice_;
static std::atomic<StreamBufferHandle_t> sSource_;
static std::function<void(Event)> sEventHandler_;
};
class Disabled : public BluetoothState {
public:
void entry() override;
void react(const events::Enable& ev) override;
void react(const events::Disable& ev) override{};
void react(const events::internal::Gap& ev) override {}
void react(const events::internal::A2dp& ev) override {}
using BluetoothState::react;
};
class Scanning : public BluetoothState {
public:
void entry() override;
void exit() override;
void react(const events::Disable& ev) override;
void react(const events::PreferredDeviceChanged& ev) override;
void react(const events::internal::Gap& ev) override;
using BluetoothState::react;
private:
auto OnDeviceDiscovered(esp_bt_gap_cb_param_t*) -> void;
};
class Connecting : public BluetoothState {
public:
void entry() override;
void exit() override;
void react(const events::PreferredDeviceChanged& ev) override;
void react(const events::Disable& ev) override;
void react(const events::internal::Gap& ev) override;
void react(const events::internal::A2dp& ev) override;
using BluetoothState::react;
};
class Connected : public BluetoothState {
public:
void entry() override;
void exit() override;
void react(const events::PreferredDeviceChanged& ev) override;
void react(const events::SourceChanged& ev) override;
void react(const events::Disable& ev) override;
void react(const events::internal::Gap& ev) override;
void react(const events::internal::A2dp& ev) override;
void react(const events::internal::Avrc& ev) override;
using BluetoothState::react;
};
} // namespace bluetooth
} // namespace drivers
|