blob: 00ddb0c01bf07f4de2b1848e33450a02390cdc0e (
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
#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 <stdint.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 IsConnected() -> bool;
auto ConnectedDevice() -> std::optional<bluetooth::Device>;
/*
* Sets whether or not the bluetooth stack is allowed to actively scan for
* new devices.
*/
auto SetDeviceDiscovery(bool) -> void;
auto IsDiscovering() -> bool;
auto KnownDevices() -> std::vector<bluetooth::Device>;
auto SetPreferredDevice(std::optional<bluetooth::MacAndName> dev) -> void;
auto PreferredDevice() -> std::optional<bluetooth::MacAndName>;
auto SetSource(StreamBufferHandle_t) -> void;
auto SetVolume(uint8_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 {};
struct DiscoveryChanged : public tinyfsm::Event {};
struct DeviceDiscovered : public tinyfsm::Event {
const Device& device;
};
struct ChangeVolume : public tinyfsm::Event {
const uint8_t volume;
};
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
/*
* Utility for managing scanning, independent of the current connection state.
*/
class Scanner {
public:
Scanner();
auto ScanContinuously() -> void;
auto ScanOnce() -> void;
auto StopScanning() -> void;
auto StopScanningNow() -> void;
auto HandleGapEvent(const events::internal::Gap&) -> void;
private:
bool enabled_;
bool is_discovering_;
auto HandleDeviceDiscovery(const esp_bt_gap_cb_param_t& param) -> void;
};
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<bluetooth::MacAndName>;
static auto preferred_device(std::optional<bluetooth::MacAndName>) -> void;
static auto scanning() -> bool;
static auto discovery() -> bool;
static auto discovery(bool) -> 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::DiscoveryChanged&);
virtual void react(const events::ChangeVolume&) {}
virtual void react(const events::DeviceDiscovered&);
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 Scanner* sScanner_;
static std::mutex sDevicesMutex_;
static std::map<mac_addr_t, Device> sDevices_;
static std::optional<bluetooth::MacAndName> sPreferredDevice_;
static std::optional<bluetooth::MacAndName> sConnectingDevice_;
static bool sIsDiscoveryAllowed_;
static std::atomic<StreamBufferHandle_t> sSource_;
static std::function<void(Event)> sEventHandler_;
};
class Disabled : public BluetoothState {
public:
void entry() override;
void exit() 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 {}
void react(const events::DiscoveryChanged& ev) override{};
using BluetoothState::react;
};
class Idle : public BluetoothState {
public:
void entry() 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;
};
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::ChangeVolume&) 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;
private:
uint8_t transaction_num_;
mac_addr_t connected_to_;
};
} // namespace bluetooth
} // namespace drivers
|