summaryrefslogtreecommitdiff
path: root/src/drivers/bluetooth.cpp
blob: f9ab4e95fb9dfb2cab1186bee9122d16cc82167e (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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#include "bluetooth.hpp"

#include <stdint.h>

#include <atomic>
#include <ostream>
#include <sstream>

#include "esp_a2dp_api.h"
#include "esp_avrc_api.h"
#include "esp_bt.h"
#include "esp_bt_device.h"
#include "esp_bt_main.h"
#include "esp_gap_bt_api.h"
#include "esp_log.h"
#include "esp_mac.h"
#include "tinyfsm/include/tinyfsm.hpp"

namespace drivers {

static constexpr char kTag[] = "bluetooth";

static std::atomic<StreamBufferHandle_t> sStream;

auto gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t* param) -> void {
  tinyfsm::FsmList<bluetooth::BluetoothState>::dispatch(
      bluetooth::events::internal::Gap{.type = event, .param = param});
}

auto avrcp_cb(esp_avrc_ct_cb_event_t event, esp_avrc_ct_cb_param_t* param)
    -> void {
  tinyfsm::FsmList<bluetooth::BluetoothState>::dispatch(
      bluetooth::events::internal::Avrc{.type = event, .param = param});
}

auto a2dp_cb(esp_a2d_cb_event_t event, esp_a2d_cb_param_t* param) -> void {
  tinyfsm::FsmList<bluetooth::BluetoothState>::dispatch(
      bluetooth::events::internal::A2dp{.type = event, .param = param});
}

auto a2dp_data_cb(uint8_t* buf, int32_t buf_size) -> int32_t {
  if (buf == nullptr || buf_size <= 0) {
    return 0;
  }
  StreamBufferHandle_t stream = sStream.load();
  if (stream == nullptr) {
    return 0;
  }
  return xStreamBufferReceive(stream, buf, buf_size, 0);
}

Bluetooth::Bluetooth() {
  tinyfsm::FsmList<bluetooth::BluetoothState>::start();
}

auto Bluetooth::Enable() -> bool {
  tinyfsm::FsmList<bluetooth::BluetoothState>::dispatch(
      bluetooth::events::Enable{});

  return !bluetooth::BluetoothState::is_in_state<bluetooth::Disabled>();
}

auto Bluetooth::Disable() -> void {
  tinyfsm::FsmList<bluetooth::BluetoothState>::dispatch(
      bluetooth::events::Disable{});
}

auto DeviceName() -> std::string {
  uint8_t mac[8]{0};
  esp_efuse_mac_get_default(mac);
  std::ostringstream name;
  name << "TANGARA " << std::hex << mac[0] << mac[1];
  return name.str();
}

namespace bluetooth {

static bool sIsFirstEntry = true;

void Disabled::entry() {
  if (sIsFirstEntry) {
    // We only use BT Classic, to claw back ~60KiB from the BLE firmware.
    esp_bt_controller_mem_release(ESP_BT_MODE_BLE);
    sIsFirstEntry = false;
    return;
  }

  esp_bluedroid_disable();
  esp_bluedroid_deinit();
  esp_bt_controller_disable();
}

void Disabled::react(const events::Enable&) {
  esp_bt_controller_config_t config = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
  if (esp_bt_controller_init(&config) != ESP_OK) {
    ESP_LOGE(kTag, "initialize controller failed");
    return;
  }

  if (esp_bt_controller_enable(ESP_BT_MODE_CLASSIC_BT) != ESP_OK) {
    ESP_LOGE(kTag, "enable controller failed");
    return;
  }

  if (esp_bluedroid_init() != ESP_OK) {
    ESP_LOGE(kTag, "initialize bluedroid failed");
    return;
  }

  if (esp_bluedroid_enable() != ESP_OK) {
    ESP_LOGE(kTag, "enable bluedroid failed");
    return;
  }

  // Enable Secure Simple Pairing
  esp_bt_sp_param_t param_type = ESP_BT_SP_IOCAP_MODE;
  esp_bt_io_cap_t iocap = ESP_BT_IO_CAP_IO;
  esp_bt_gap_set_security_param(param_type, &iocap, sizeof(uint8_t));

  // Set a reasonable name for the device.
  std::string name = DeviceName();
  esp_bt_dev_set_device_name(name.c_str());

  // Initialise GAP. This controls advertising our device, and scanning for
  // other devices.
  esp_bt_gap_register_callback(gap_cb);

  // Initialise AVRCP. This handles playback controls; play/pause/volume/etc.
  // esp_avrc_ct_init();
  // esp_avrc_ct_register_callback(avrcp_cb);

  // Initialise A2DP. This handles streaming audio. Currently ESP-IDF's SBC
  // encoder only supports 2 channels of interleaved 16 bit samples, at
  // 44.1kHz, so there is no additional configuration to be done for the
  // stream itself.
  esp_a2d_source_init();
  esp_a2d_register_callback(a2dp_cb);
  esp_a2d_source_register_data_callback(a2dp_data_cb);

  // Don't let anyone interact with us before we're ready.
  esp_bt_gap_set_scan_mode(ESP_BT_NON_CONNECTABLE, ESP_BT_NON_DISCOVERABLE);

  transit<Scanning>();
}

static constexpr uint8_t kDiscoveryTimeSeconds = 10;
static constexpr uint8_t kDiscoveryMaxResults = 0;

void Scanning::entry() {
  ESP_LOGI(kTag, "scanning for devices");
  esp_bt_gap_start_discovery(ESP_BT_INQ_MODE_GENERAL_INQUIRY,
                             kDiscoveryTimeSeconds, kDiscoveryMaxResults);
}

void Scanning::exit() {
  esp_bt_gap_cancel_discovery();
}

auto OnDeviceDiscovered(esp_bt_gap_cb_param_t* param) -> void {
  ESP_LOGI(kTag, "device discovered");
}

void Scanning::react(const events::internal::Gap& ev) {
  switch (ev.type) {
    case ESP_BT_GAP_DISC_RES_EVT:
      OnDeviceDiscovered(ev.param);
      break;
    case ESP_BT_GAP_DISC_STATE_CHANGED_EVT:
      if (ev.param->disc_st_chg.state == ESP_BT_GAP_DISCOVERY_STOPPED) {
        ESP_LOGI(kTag, "still scanning");
        esp_bt_gap_start_discovery(ESP_BT_INQ_MODE_GENERAL_INQUIRY,
                                   kDiscoveryTimeSeconds, kDiscoveryMaxResults);
      }
      break;
    case ESP_BT_GAP_MODE_CHG_EVT:
      // todo: mode change. is this important?
      ESP_LOGI(kTag, "GAP mode changed");
      break;
    default:
      ESP_LOGW(kTag, "unhandled GAP event: %u", ev.type);
  }
}

void Connecting::entry() {
  ESP_LOGI(kTag, "connecting to device");
  esp_a2d_source_connect(nullptr);
}

void Connecting::exit() {}

void Connecting::react(const events::internal::Gap& ev) {
  switch (ev.type) {
    case ESP_BT_GAP_AUTH_CMPL_EVT:
      // todo: auth completed. check if we succeeded.
      break;
    case ESP_BT_GAP_PIN_REQ_EVT:
      // todo: device needs a pin to connect.
      break;
    case ESP_BT_GAP_CFM_REQ_EVT:
      // todo: device needs user to click okay.
      break;
    case ESP_BT_GAP_KEY_NOTIF_EVT:
      // todo: device is telling us a password?
      break;
    case ESP_BT_GAP_KEY_REQ_EVT:
      // todo: device needs a password
      break;
    case ESP_BT_GAP_MODE_CHG_EVT:
      // todo: mode change. is this important?
      break;
    default:
      ESP_LOGW(kTag, "unhandled GAP event: %u", ev.type);
  }
}

void Connecting::react(const events::internal::A2dp& ev) {
  switch (ev.type) {
    case ESP_A2D_CONNECTION_STATE_EVT:
      // todo: connection state changed. we might be connected!
      break;
    default:
      ESP_LOGW(kTag, "unhandled A2DP event: %u", ev.type);
  }
}

void Connected::react(const events::internal::A2dp& ev) {
  switch (ev.type) {
    case ESP_A2D_CONNECTION_STATE_EVT:
      // todo: connection state changed. we might have dropped
      break;
    case ESP_A2D_AUDIO_STATE_EVT:
      // todo: audio state changed. who knows, dude.
      break;
    default:
      ESP_LOGW(kTag, "unhandled A2DP event: %u", ev.type);
  }
}

void Connected::react(const events::internal::Avrc& ev) {
  switch (ev.type) {
    case ESP_AVRC_CT_CONNECTION_STATE_EVT:
      // todo: avrc connected. send our capabilities.
    default:
      ESP_LOGW(kTag, "unhandled AVRC event: %u", ev.type);
  }
}

}  // namespace bluetooth

}  // namespace drivers

FSM_INITIAL_STATE(drivers::bluetooth::BluetoothState,
                  drivers::bluetooth::Disabled)