Myles Watson | 6d5e772 | 2022-09-30 06:22:43 -0700 | [diff] [blame] | 1 | /* |
Antoine SOULIER | b833bc6 | 2024-11-08 00:50:36 +0000 | [diff] [blame] | 2 | * Copyright 2024 The Android Open Source Project |
Myles Watson | 6d5e772 | 2022-09-30 06:22:43 -0700 | [diff] [blame] | 3 | * |
| 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 Watson | 6d5e772 | 2022-09-30 06:22:43 -0700 | [diff] [blame] | 19 | #include <cutils/properties.h> |
| 20 | #include <fcntl.h> |
Antoine SOULIER | b833bc6 | 2024-11-08 00:50:36 +0000 | [diff] [blame] | 21 | #include <hal/ffi.h> |
Myles Watson | 6d5e772 | 2022-09-30 06:22:43 -0700 | [diff] [blame] | 22 | #include <string.h> |
Myles Watson | 6d5e772 | 2022-09-30 06:22:43 -0700 | [diff] [blame] | 23 | #include <termios.h> |
| 24 | |
Antoine SOULIER | b833bc6 | 2024-11-08 00:50:36 +0000 | [diff] [blame] | 25 | #include <future> |
| 26 | #include <memory> |
| 27 | #include <vector> |
| 28 | |
| 29 | #include "async_fd_watcher.h" |
| 30 | #include "h4_protocol.h" |
Myles Watson | 6d5e772 | 2022-09-30 06:22:43 -0700 | [diff] [blame] | 31 | #include "log/log.h" |
Antoine SOULIER | b833bc6 | 2024-11-08 00:50:36 +0000 | [diff] [blame] | 32 | #include "net_bluetooth_mgmt.h" |
Myles Watson | 6d5e772 | 2022-09-30 06:22:43 -0700 | [diff] [blame] | 33 | |
| 34 | namespace { |
| 35 | int 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 | |
| 47 | using namespace ::android::hardware::bluetooth::hci; |
| 48 | using namespace ::android::hardware::bluetooth::async; |
| 49 | |
| 50 | namespace aidl::android::hardware::bluetooth::impl { |
| 51 | |
Henri Chataing | 1267ff2 | 2023-08-09 22:38:24 +0000 | [diff] [blame] | 52 | std::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 | |
| 61 | bool starts_with(const std::string& str, const std::string& prefix) { |
| 62 | return str.compare(0, prefix.length(), prefix) == 0; |
| 63 | } |
| 64 | |
Antoine SOULIER | b833bc6 | 2024-11-08 00:50:36 +0000 | [diff] [blame] | 65 | class Hal { |
Myles Watson | 6d5e772 | 2022-09-30 06:22:43 -0700 | [diff] [blame] | 66 | public: |
Antoine SOULIER | b833bc6 | 2024-11-08 00:50:36 +0000 | [diff] [blame] | 67 | 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 Watson | 6d5e772 | 2022-09-30 06:22:43 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Antoine SOULIER | b833bc6 | 2024-11-08 00:50:36 +0000 | [diff] [blame] | 73 | static void Initialize(void* instance, |
| 74 | const struct hal_callbacks* callbacks) { |
| 75 | static_cast<Hal*>(instance)->Initialize(callbacks); |
Myles Watson | 6d5e772 | 2022-09-30 06:22:43 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Antoine SOULIER | b833bc6 | 2024-11-08 00:50:36 +0000 | [diff] [blame] | 78 | 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 Watson | 6d5e772 | 2022-09-30 06:22:43 -0700 | [diff] [blame] | 83 | } |
Antoine SOULIER | b833bc6 | 2024-11-08 00:50:36 +0000 | [diff] [blame] | 84 | |
| 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 Watson | 65b47f5 | 2023-01-26 12:59:06 -0800 | [diff] [blame] | 98 | } |
Myles Watson | 6d5e772 | 2022-09-30 06:22:43 -0700 | [diff] [blame] | 99 | |
| 100 | private: |
Antoine SOULIER | b833bc6 | 2024-11-08 00:50:36 +0000 | [diff] [blame] | 101 | 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 Watson | efa25d7 | 2022-10-03 16:27:32 -0700 | [diff] [blame] | 112 | return fd; |
Myles Watson | 6d5e772 | 2022-09-30 06:22:43 -0700 | [diff] [blame] | 113 | } |
Myles Watson | efa25d7 | 2022-10-03 16:27:32 -0700 | [diff] [blame] | 114 | |
Antoine SOULIER | b833bc6 | 2024-11-08 00:50:36 +0000 | [diff] [blame] | 115 | void reset() { |
| 116 | // Send a reset command and wait until the command complete comes back. |
Myles Watson | 65b47f5 | 2023-01-26 12:59:06 -0800 | [diff] [blame] | 117 | |
Antoine SOULIER | b833bc6 | 2024-11-08 00:50:36 +0000 | [diff] [blame] | 118 | std::vector<uint8_t> reset = {0x03, 0x0c, 0x00}; |
Myles Watson | 65b47f5 | 2023-01-26 12:59:06 -0800 | [diff] [blame] | 119 | |
Antoine SOULIER | b833bc6 | 2024-11-08 00:50:36 +0000 | [diff] [blame] | 120 | auto resetPromise = std::make_shared<std::promise<void>>(); |
| 121 | auto resetFuture = resetPromise->get_future(); |
Myles Watson | 65b47f5 | 2023-01-26 12:59:06 -0800 | [diff] [blame] | 122 | |
Antoine SOULIER | b833bc6 | 2024-11-08 00:50:36 +0000 | [diff] [blame] | 123 | 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 Watson | 65b47f5 | 2023-01-26 12:59:06 -0800 | [diff] [blame] | 164 | |
Antoine SOULIER | b833bc6 | 2024-11-08 00:50:36 +0000 | [diff] [blame] | 165 | 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 Watson | 65b47f5 | 2023-01-26 12:59:06 -0800 | [diff] [blame] | 172 | } else { |
Antoine SOULIER | b833bc6 | 2024-11-08 00:50:36 +0000 | [diff] [blame] | 173 | ALOGE("HCI Reset Response not received in one second"); |
Myles Watson | 65b47f5 | 2023-01-26 12:59:06 -0800 | [diff] [blame] | 174 | } |
Antoine SOULIER | b833bc6 | 2024-11-08 00:50:36 +0000 | [diff] [blame] | 175 | |
| 176 | resetPromise.reset(); |
Myles Watson | 65b47f5 | 2023-01-26 12:59:06 -0800 | [diff] [blame] | 177 | } |
| 178 | |
Antoine SOULIER | b833bc6 | 2024-11-08 00:50:36 +0000 | [diff] [blame] | 179 | void Initialize(const struct hal_callbacks* callbacks) { |
| 180 | ALOGI(__func__); |
Myles Watson | 65b47f5 | 2023-01-26 12:59:06 -0800 | [diff] [blame] | 181 | |
Antoine SOULIER | b833bc6 | 2024-11-08 00:50:36 +0000 | [diff] [blame] | 182 | 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 Watson | efa25d7 | 2022-10-03 16:27:32 -0700 | [diff] [blame] | 191 | |
Antoine SOULIER | b833bc6 | 2024-11-08 00:50:36 +0000 | [diff] [blame] | 192 | 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 Watson | efa25d7 | 2022-10-03 16:27:32 -0700 | [diff] [blame] | 203 | if (mFd < 0) { |
Antoine SOULIER | b833bc6 | 2024-11-08 00:50:36 +0000 | [diff] [blame] | 204 | 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.yang | 6ebe251 | 2023-05-31 13:59:04 +0800 | [diff] [blame] | 291 | mState = HalState::READY; |
Antoine SOULIER | b833bc6 | 2024-11-08 00:50:36 +0000 | [diff] [blame] | 292 | mH4 = nullptr; |
Myles Watson | efa25d7 | 2022-10-03 16:27:32 -0700 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
Antoine SOULIER | b833bc6 | 2024-11-08 00:50:36 +0000 | [diff] [blame] | 296 | 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 Chataing | 1267ff2 | 2023-08-09 22:38:24 +0000 | [diff] [blame] | 307 | } |
Myles Watson | 65b47f5 | 2023-01-26 12:59:06 -0800 | [diff] [blame] | 308 | |
Antoine SOULIER | b833bc6 | 2024-11-08 00:50:36 +0000 | [diff] [blame] | 309 | 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 Watson | efa25d7 | 2022-10-03 16:27:32 -0700 | [diff] [blame] | 313 | } |
Myles Watson | efa25d7 | 2022-10-03 16:27:32 -0700 | [diff] [blame] | 314 | |
Myles Watson | 65b47f5 | 2023-01-26 12:59:06 -0800 | [diff] [blame] | 315 | std::lock_guard<std::mutex> guard(mStateMutex); |
Antoine SOULIER | b833bc6 | 2024-11-08 00:50:36 +0000 | [diff] [blame] | 316 | if (mH4 == nullptr) { |
| 317 | ALOGE("Illegal State"); |
| 318 | return false; |
Myles Watson | 65b47f5 | 2023-01-26 12:59:06 -0800 | [diff] [blame] | 319 | } |
Antoine SOULIER | b833bc6 | 2024-11-08 00:50:36 +0000 | [diff] [blame] | 320 | |
| 321 | mH4->Send(type, v); |
| 322 | return true; |
Myles Watson | 65b47f5 | 2023-01-26 12:59:06 -0800 | [diff] [blame] | 323 | } |
| 324 | |
Antoine SOULIER | b833bc6 | 2024-11-08 00:50:36 +0000 | [diff] [blame] | 325 | 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 Watson | 65b47f5 | 2023-01-26 12:59:06 -0800 | [diff] [blame] | 331 | |
Antoine SOULIER | b833bc6 | 2024-11-08 00:50:36 +0000 | [diff] [blame] | 332 | // 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 Watson | 6d5e772 | 2022-09-30 06:22:43 -0700 | [diff] [blame] | 341 | |
| 342 | } // namespace aidl::android::hardware::bluetooth::impl |
Antoine SOULIER | b833bc6 | 2024-11-08 00:50:36 +0000 | [diff] [blame] | 343 | |
| 344 | extern "C" { |
| 345 | |
| 346 | using namespace aidl::android::hardware::bluetooth::impl; |
| 347 | |
| 348 | struct 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 | } |