blob: bcdb67e1927cc12be4518f5b12d0a6c0d4ef662e [file] [log] [blame]
Myles Watson6d5e7722022-09-30 06:22:43 -07001/*
Antoine SOULIERb833bc62024-11-08 00:50:36 +00002 * Copyright 2024 The Android Open Source Project
Myles Watson6d5e7722022-09-30 06:22:43 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "android.hardware.bluetooth.service.default"
18
Myles Watson6d5e7722022-09-30 06:22:43 -070019#include <cutils/properties.h>
20#include <fcntl.h>
Antoine SOULIERb833bc62024-11-08 00:50:36 +000021#include <hal/ffi.h>
Myles Watson6d5e7722022-09-30 06:22:43 -070022#include <string.h>
Myles Watson6d5e7722022-09-30 06:22:43 -070023#include <termios.h>
24
Antoine SOULIERb833bc62024-11-08 00:50:36 +000025#include <future>
26#include <memory>
27#include <vector>
28
29#include "async_fd_watcher.h"
30#include "h4_protocol.h"
Myles Watson6d5e7722022-09-30 06:22:43 -070031#include "log/log.h"
Antoine SOULIERb833bc62024-11-08 00:50:36 +000032#include "net_bluetooth_mgmt.h"
Myles Watson6d5e7722022-09-30 06:22:43 -070033
34namespace {
35int SetTerminalRaw(int fd) {
36 termios terminal_settings;
37 int rval = tcgetattr(fd, &terminal_settings);
38 if (rval < 0) {
39 return rval;
40 }
41 cfmakeraw(&terminal_settings);
42 rval = tcsetattr(fd, TCSANOW, &terminal_settings);
43 return rval;
44}
45} // namespace
46
47using namespace ::android::hardware::bluetooth::hci;
48using namespace ::android::hardware::bluetooth::async;
49
50namespace aidl::android::hardware::bluetooth::impl {
51
Henri Chataing1267ff22023-08-09 22:38:24 +000052std::optional<std::string> GetSystemProperty(const std::string& property) {
53 std::array<char, PROPERTY_VALUE_MAX> value_array{0};
54 auto value_len = property_get(property.c_str(), value_array.data(), nullptr);
55 if (value_len <= 0) {
56 return std::nullopt;
57 }
58 return std::string(value_array.data(), value_len);
59}
60
61bool starts_with(const std::string& str, const std::string& prefix) {
62 return str.compare(0, prefix.length(), prefix) == 0;
63}
64
Antoine SOULIERb833bc62024-11-08 00:50:36 +000065class Hal {
Myles Watson6d5e7722022-09-30 06:22:43 -070066 public:
Antoine SOULIERb833bc62024-11-08 00:50:36 +000067 Hal(const std::string& dev_path = "/dev/hvc5") {
68 char property_bytes[PROPERTY_VALUE_MAX];
69 property_get("vendor.ser.bt-uart", property_bytes, dev_path.c_str());
70 mDevPath = std::string(property_bytes);
Myles Watson6d5e7722022-09-30 06:22:43 -070071 }
72
Antoine SOULIERb833bc62024-11-08 00:50:36 +000073 static void Initialize(void* instance,
74 const struct hal_callbacks* callbacks) {
75 static_cast<Hal*>(instance)->Initialize(callbacks);
Myles Watson6d5e7722022-09-30 06:22:43 -070076 }
77
Antoine SOULIERb833bc62024-11-08 00:50:36 +000078 static void Close(void* instance) { static_cast<Hal*>(instance)->Close(); }
79
80 static void SendCommand(void* instance, const uint8_t* data, size_t len) {
81 static_cast<Hal*>(instance)->SendCommand(
82 std::vector<uint8_t>(data, data + len));
Myles Watson6d5e7722022-09-30 06:22:43 -070083 }
Antoine SOULIERb833bc62024-11-08 00:50:36 +000084
85 static void SendAcl(void* instance, const uint8_t* data, size_t len) {
86 static_cast<Hal*>(instance)->SendAcl(
87 std::vector<uint8_t>(data, data + len));
88 }
89
90 static void SendSco(void* instance, const uint8_t* data, size_t len) {
91 static_cast<Hal*>(instance)->SendSco(
92 std::vector<uint8_t>(data, data + len));
93 }
94
95 static void SendIso(void* instance, const uint8_t* data, size_t len) {
96 static_cast<Hal*>(instance)->SendIso(
97 std::vector<uint8_t>(data, data + len));
Myles Watson65b47f52023-01-26 12:59:06 -080098 }
Myles Watson6d5e7722022-09-30 06:22:43 -070099
100 private:
Antoine SOULIERb833bc62024-11-08 00:50:36 +0000101 int getFdFromDevPath() {
102 int fd = open(mDevPath.c_str(), O_RDWR);
103 if (fd < 0) {
104 ALOGE("Could not connect to bt: %s (%s)", mDevPath.c_str(),
105 strerror(errno));
106 return fd;
107 }
108 if (int ret = SetTerminalRaw(fd) < 0) {
109 ALOGI("Could not make %s a raw terminal %d(%s)", mDevPath.c_str(), ret,
110 strerror(errno));
111 }
Myles Watsonefa25d72022-10-03 16:27:32 -0700112 return fd;
Myles Watson6d5e7722022-09-30 06:22:43 -0700113 }
Myles Watsonefa25d72022-10-03 16:27:32 -0700114
Antoine SOULIERb833bc62024-11-08 00:50:36 +0000115 void reset() {
116 // Send a reset command and wait until the command complete comes back.
Myles Watson65b47f52023-01-26 12:59:06 -0800117
Antoine SOULIERb833bc62024-11-08 00:50:36 +0000118 std::vector<uint8_t> reset = {0x03, 0x0c, 0x00};
Myles Watson65b47f52023-01-26 12:59:06 -0800119
Antoine SOULIERb833bc62024-11-08 00:50:36 +0000120 auto resetPromise = std::make_shared<std::promise<void>>();
121 auto resetFuture = resetPromise->get_future();
Myles Watson65b47f52023-01-26 12:59:06 -0800122
Antoine SOULIERb833bc62024-11-08 00:50:36 +0000123 mH4 = std::make_shared<H4Protocol>(
124 mFd,
125 [](const std::vector<uint8_t>& raw_command) {
126 ALOGI("Discarding %d bytes with command type",
127 static_cast<int>(raw_command.size()));
128 },
129 [](const std::vector<uint8_t>& raw_acl) {
130 ALOGI("Discarding %d bytes with acl type",
131 static_cast<int>(raw_acl.size()));
132 },
133 [](const std::vector<uint8_t>& raw_sco) {
134 ALOGI("Discarding %d bytes with sco type",
135 static_cast<int>(raw_sco.size()));
136 },
137 [resetPromise](const std::vector<uint8_t>& raw_event) {
138 std::vector<uint8_t> reset_complete = {0x0e, 0x04, 0x01,
139 0x03, 0x0c, 0x00};
140 bool valid = raw_event.size() == 6 &&
141 raw_event[0] == reset_complete[0] &&
142 raw_event[1] == reset_complete[1] &&
143 // Don't compare the number of packets field.
144 raw_event[3] == reset_complete[3] &&
145 raw_event[4] == reset_complete[4] &&
146 raw_event[5] == reset_complete[5];
147 if (valid) {
148 resetPromise->set_value();
149 } else {
150 ALOGI("Discarding %d bytes with event type",
151 static_cast<int>(raw_event.size()));
152 }
153 },
154 [](const std::vector<uint8_t>& raw_iso) {
155 ALOGI("Discarding %d bytes with iso type",
156 static_cast<int>(raw_iso.size()));
157 },
158 [this]() {
159 ALOGI("HCI socket device disconnected while waiting for reset");
160 mFdWatcher.StopWatchingFileDescriptors();
161 });
162 mFdWatcher.WatchFdForNonBlockingReads(mFd,
163 [this](int) { mH4->OnDataReady(); });
Myles Watson65b47f52023-01-26 12:59:06 -0800164
Antoine SOULIERb833bc62024-11-08 00:50:36 +0000165 if (!send(PacketType::COMMAND, reset)) {
166 ALOGE("Error sending reset command");
167 }
168 auto status = resetFuture.wait_for(std::chrono::seconds(1));
169 mFdWatcher.StopWatchingFileDescriptors();
170 if (status == std::future_status::ready) {
171 ALOGI("HCI Reset successful");
Myles Watson65b47f52023-01-26 12:59:06 -0800172 } else {
Antoine SOULIERb833bc62024-11-08 00:50:36 +0000173 ALOGE("HCI Reset Response not received in one second");
Myles Watson65b47f52023-01-26 12:59:06 -0800174 }
Antoine SOULIERb833bc62024-11-08 00:50:36 +0000175
176 resetPromise.reset();
Myles Watson65b47f52023-01-26 12:59:06 -0800177 }
178
Antoine SOULIERb833bc62024-11-08 00:50:36 +0000179 void Initialize(const struct hal_callbacks* callbacks) {
180 ALOGI(__func__);
Myles Watson65b47f52023-01-26 12:59:06 -0800181
Antoine SOULIERb833bc62024-11-08 00:50:36 +0000182 HalState old_state = HalState::READY;
183 {
184 std::lock_guard<std::mutex> guard(mStateMutex);
185 if (mState != HalState::READY) {
186 old_state = mState;
187 } else {
188 mState = HalState::INITIALIZING;
189 }
190 }
Myles Watsonefa25d72022-10-03 16:27:32 -0700191
Antoine SOULIERb833bc62024-11-08 00:50:36 +0000192 if (old_state != HalState::READY) {
193 ALOGE("initialize: Unexpected State %d", static_cast<int>(old_state));
194 Close();
195 callbacks->initialization_complete(callbacks->handle,
196 STATUS_ALREADY_INITIALIZED);
197 return;
198 }
199
200 mCallbacks = std::make_unique<struct hal_callbacks>(*callbacks);
201 management_.reset(new NetBluetoothMgmt);
202 mFd = management_->openHci();
Myles Watsonefa25d72022-10-03 16:27:32 -0700203 if (mFd < 0) {
Antoine SOULIERb833bc62024-11-08 00:50:36 +0000204 management_.reset();
205
206 ALOGI("Unable to open Linux interface, trying default path.");
207 mFd = getFdFromDevPath();
208 if (mFd < 0) {
209 mState = HalState::READY;
210 mCallbacks->initialization_complete(mCallbacks->handle,
211 STATUS_UNABLE_TO_OPEN_INTERFACE);
212 return;
213 }
214 }
215
216 // TODO: HCI Reset on emulators since the bluetooth controller
217 // cannot be powered on/off during the HAL setup; and the stack
218 // might received spurious packets/events during boottime.
219 // Proper solution would be to use bt-virtio or vsock to better
220 // control the link to rootcanal and the controller lifetime.
221 const std::string kBoardProperty = "ro.product.board";
222 const std::string kCuttlefishBoard = "cutf";
223 auto board_name = GetSystemProperty(kBoardProperty);
224 if (board_name.has_value() &&
225 (starts_with(board_name.value(), "cutf") ||
226 starts_with(board_name.value(), "goldfish"))) {
227 reset();
228 }
229
230 mH4 = std::make_shared<H4Protocol>(
231 mFd,
232 [](const std::vector<uint8_t>& /* raw_command */) {
233 LOG_ALWAYS_FATAL("Unexpected command!");
234 },
235 [this](const std::vector<uint8_t>& raw_acl) {
236 mCallbacks->acl_received(mCallbacks->handle, raw_acl.data(),
237 raw_acl.size());
238 },
239 [this](const std::vector<uint8_t>& raw_sco) {
240 mCallbacks->sco_received(mCallbacks->handle, raw_sco.data(),
241 raw_sco.size());
242 },
243 [this](const std::vector<uint8_t>& raw_event) {
244 mCallbacks->event_received(mCallbacks->handle, raw_event.data(),
245 raw_event.size());
246 },
247 [this](const std::vector<uint8_t>& raw_iso) {
248 mCallbacks->iso_received(mCallbacks->handle, raw_iso.data(),
249 raw_iso.size());
250 },
251 [this]() {
252 ALOGI("HCI socket device disconnected");
253 mFdWatcher.StopWatchingFileDescriptors();
254 });
255 mFdWatcher.WatchFdForNonBlockingReads(mFd,
256 [this](int) { mH4->OnDataReady(); });
257
258 {
259 std::lock_guard<std::mutex> guard(mStateMutex);
260 mState = HalState::ONE_CLIENT;
261 }
262
263 ALOGI("initialization complete");
264 mCallbacks->initialization_complete(mCallbacks->handle, STATUS_SUCCESS);
265 }
266
267 void Close() {
268 ALOGI(__func__);
269 {
270 std::lock_guard<std::mutex> guard(mStateMutex);
271 if (mState != HalState::ONE_CLIENT) {
272 LOG_ALWAYS_FATAL_IF(mState == HalState::INITIALIZING,
273 "mState is INITIALIZING");
274 ALOGI("Already closed");
275 return;
276 }
277 mCallbacks.reset();
278 mState = HalState::CLOSING;
279 }
280
281 mFdWatcher.StopWatchingFileDescriptors();
282
283 if (management_) {
284 management_->closeHci();
285 } else {
286 ::close(mFd);
287 }
288
289 {
290 std::lock_guard<std::mutex> guard(mStateMutex);
yanggang.yang6ebe2512023-05-31 13:59:04 +0800291 mState = HalState::READY;
Antoine SOULIERb833bc62024-11-08 00:50:36 +0000292 mH4 = nullptr;
Myles Watsonefa25d72022-10-03 16:27:32 -0700293 }
294 }
295
Antoine SOULIERb833bc62024-11-08 00:50:36 +0000296 void SendCommand(const std::vector<uint8_t>& data) {
297 send(PacketType::COMMAND, data);
298 }
299 void SendAcl(const std::vector<uint8_t>& data) {
300 send(PacketType::ACL_DATA, data);
301 }
302 void SendSco(const std::vector<uint8_t>& data) {
303 send(PacketType::SCO_DATA, data);
304 }
305 void SendIso(const std::vector<uint8_t>& data) {
306 send(PacketType::ISO_DATA, data);
Henri Chataing1267ff22023-08-09 22:38:24 +0000307 }
Myles Watson65b47f52023-01-26 12:59:06 -0800308
Antoine SOULIERb833bc62024-11-08 00:50:36 +0000309 bool send(PacketType type, const std::vector<uint8_t>& v) {
310 if (v.empty()) {
311 ALOGE("Packet is empty, no data was found to be sent");
312 return false;
Myles Watsonefa25d72022-10-03 16:27:32 -0700313 }
Myles Watsonefa25d72022-10-03 16:27:32 -0700314
Myles Watson65b47f52023-01-26 12:59:06 -0800315 std::lock_guard<std::mutex> guard(mStateMutex);
Antoine SOULIERb833bc62024-11-08 00:50:36 +0000316 if (mH4 == nullptr) {
317 ALOGE("Illegal State");
318 return false;
Myles Watson65b47f52023-01-26 12:59:06 -0800319 }
Antoine SOULIERb833bc62024-11-08 00:50:36 +0000320
321 mH4->Send(type, v);
322 return true;
Myles Watson65b47f52023-01-26 12:59:06 -0800323 }
324
Antoine SOULIERb833bc62024-11-08 00:50:36 +0000325 std::unique_ptr<struct hal_callbacks> mCallbacks;
326 std::string mDevPath;
327 int mFd{-1};
328 ::android::hardware::bluetooth::async::AsyncFdWatcher mFdWatcher;
329 std::shared_ptr<::android::hardware::bluetooth::hci::H4Protocol> mH4;
330 std::unique_ptr<NetBluetoothMgmt> management_{};
Myles Watson65b47f52023-01-26 12:59:06 -0800331
Antoine SOULIERb833bc62024-11-08 00:50:36 +0000332 // Don't close twice or open before close is complete
333 std::mutex mStateMutex;
334 enum class HalState {
335 READY,
336 INITIALIZING,
337 ONE_CLIENT,
338 CLOSING,
339 } mState{HalState::READY};
340};
Myles Watson6d5e7722022-09-30 06:22:43 -0700341
342} // namespace aidl::android::hardware::bluetooth::impl
Antoine SOULIERb833bc62024-11-08 00:50:36 +0000343
344extern "C" {
345
346using namespace aidl::android::hardware::bluetooth::impl;
347
348struct hal_interface hal_new() {
349 return (struct hal_interface){
350 .handle = new Hal(),
351 .initialize = &Hal::Initialize,
352 .close = &Hal::Close,
353 .send_command = &Hal::SendCommand,
354 .send_acl = &Hal::SendAcl,
355 .send_sco = &Hal::SendSco,
356 .send_iso = &Hal::SendIso,
357 };
358}
359}