blob: 20b30aef70a5aeb67c8ce248530ffcdf4cdaa2dc [file] [log] [blame]
Andre Eisenbach89ba5282016-10-13 15:45:02 -07001//
2// Copyright 2016 The Android Open Source Project
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#include "vendor_interface.h"
18
19#define LOG_TAG "android.hardware.bluetooth@1.0-impl"
20#include <android-base/logging.h>
Myles Watson6a7d6222016-10-13 15:45:02 -070021#include <cutils/properties.h>
Andre Eisenbach89ba5282016-10-13 15:45:02 -070022#include <utils/Log.h>
23
24#include <dlfcn.h>
Myles Watson6a7d6222016-10-13 15:45:02 -070025#include <fcntl.h>
26
27#include "bluetooth_address.h"
Andre Eisenbach89ba5282016-10-13 15:45:02 -070028
29static const char* VENDOR_LIBRARY_NAME = "libbt-vendor.so";
30static const char* VENDOR_LIBRARY_SYMBOL_NAME =
31 "BLUETOOTH_VENDOR_LIB_INTERFACE";
32
33static const int INVALID_FD = -1;
34
35namespace {
36
37using android::hardware::bluetooth::V1_0::implementation::VendorInterface;
Andre Eisenbach9041d972017-01-17 18:23:12 -080038using android::hardware::hidl_vec;
Andre Eisenbach89ba5282016-10-13 15:45:02 -070039
40tINT_CMD_CBACK internal_command_cb;
41VendorInterface* g_vendor_interface = nullptr;
42
43const size_t preamble_size_for_type[] = {
44 0, HCI_COMMAND_PREAMBLE_SIZE, HCI_ACL_PREAMBLE_SIZE, HCI_SCO_PREAMBLE_SIZE,
45 HCI_EVENT_PREAMBLE_SIZE};
46const size_t packet_length_offset_for_type[] = {
47 0, HCI_LENGTH_OFFSET_CMD, HCI_LENGTH_OFFSET_ACL, HCI_LENGTH_OFFSET_SCO,
48 HCI_LENGTH_OFFSET_EVT};
49
Andre Eisenbach9041d972017-01-17 18:23:12 -080050size_t HciGetPacketLengthForType(HciPacketType type,
51 const hidl_vec<uint8_t>& packet) {
Andre Eisenbach89ba5282016-10-13 15:45:02 -070052 size_t offset = packet_length_offset_for_type[type];
Andre Eisenbach9041d972017-01-17 18:23:12 -080053 if (type != HCI_PACKET_TYPE_ACL_DATA) return packet[offset];
54 return (((packet[offset + 1]) << 8) | packet[offset]);
Andre Eisenbach89ba5282016-10-13 15:45:02 -070055}
56
Andre Eisenbach9041d972017-01-17 18:23:12 -080057HC_BT_HDR* WrapPacketAndCopy(uint16_t event, const hidl_vec<uint8_t>& data) {
Andre Eisenbach89ba5282016-10-13 15:45:02 -070058 size_t packet_size = data.size() + sizeof(HC_BT_HDR);
59 HC_BT_HDR* packet = reinterpret_cast<HC_BT_HDR*>(new uint8_t[packet_size]);
60 packet->offset = 0;
61 packet->len = data.size();
62 packet->layer_specific = 0;
63 packet->event = event;
Myles Watson6a7d6222016-10-13 15:45:02 -070064 // TODO(eisenbach): Avoid copy here; if BT_HDR->data can be ensured to
Andre Eisenbach89ba5282016-10-13 15:45:02 -070065 // be the only way the data is accessed, a pointer could be passed here...
66 memcpy(packet->data, data.data(), data.size());
67 return packet;
68}
69
70uint8_t transmit_cb(uint16_t opcode, void* buffer, tINT_CMD_CBACK callback) {
71 ALOGV("%s opcode: 0x%04x, ptr: %p", __func__, opcode, buffer);
Andre Eisenbach89ba5282016-10-13 15:45:02 -070072 internal_command_cb = callback;
73 uint8_t type = HCI_PACKET_TYPE_COMMAND;
Andre Eisenbach9041d972017-01-17 18:23:12 -080074 VendorInterface::get()->Send(&type, 1);
75 HC_BT_HDR* bt_hdr = reinterpret_cast<HC_BT_HDR*>(buffer);
76 VendorInterface::get()->Send(bt_hdr->data, bt_hdr->len);
Andre Eisenbach89ba5282016-10-13 15:45:02 -070077 return true;
78}
79
80void firmware_config_cb(bt_vendor_op_result_t result) {
Andre Eisenbach9041d972017-01-17 18:23:12 -080081 ALOGV("%s result: %d", __func__, result);
Andre Eisenbach89ba5282016-10-13 15:45:02 -070082 VendorInterface::get()->OnFirmwareConfigured(result);
83}
84
85void sco_config_cb(bt_vendor_op_result_t result) {
86 ALOGD("%s result: %d", __func__, result);
87}
88
89void low_power_mode_cb(bt_vendor_op_result_t result) {
90 ALOGD("%s result: %d", __func__, result);
91}
92
93void sco_audiostate_cb(bt_vendor_op_result_t result) {
94 ALOGD("%s result: %d", __func__, result);
95}
96
97void* buffer_alloc_cb(int size) {
98 void* p = new uint8_t[size];
99 ALOGV("%s pts: %p, size: %d", __func__, p, size);
100 return p;
101}
102
103void buffer_free_cb(void* buffer) {
104 ALOGV("%s ptr: %p", __func__, buffer);
105 delete[] reinterpret_cast<uint8_t*>(buffer);
106}
107
108void epilog_cb(bt_vendor_op_result_t result) {
109 ALOGD("%s result: %d", __func__, result);
110}
111
112void a2dp_offload_cb(bt_vendor_op_result_t result, bt_vendor_opcode_t op,
113 uint8_t av_handle) {
114 ALOGD("%s result: %d, op: %d, handle: %d", __func__, result, op, av_handle);
115}
116
117const bt_vendor_callbacks_t lib_callbacks = {
118 sizeof(lib_callbacks), firmware_config_cb, sco_config_cb,
119 low_power_mode_cb, sco_audiostate_cb, buffer_alloc_cb,
120 buffer_free_cb, transmit_cb, epilog_cb,
121 a2dp_offload_cb};
122
123} // namespace
124
125namespace android {
126namespace hardware {
127namespace bluetooth {
128namespace V1_0 {
129namespace implementation {
130
Andre Eisenbach9041d972017-01-17 18:23:12 -0800131class FirmwareStartupTimer {
132 public:
133 FirmwareStartupTimer() : start_time_(std::chrono::steady_clock::now()) {}
134
135 ~FirmwareStartupTimer() {
136 std::chrono::duration<double> duration =
137 std::chrono::steady_clock::now() - start_time_;
138 double s = duration.count();
139 if (s == 0) return;
140 ALOGD("Firmware configured in %.3fs", s);
141 }
142
143 private:
144 std::chrono::steady_clock::time_point start_time_;
145};
146
147bool VendorInterface::Initialize(
148 InitializeCompleteCallback initialize_complete_cb,
149 PacketReadCallback packet_read_cb) {
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700150 assert(!g_vendor_interface);
151 g_vendor_interface = new VendorInterface();
Andre Eisenbach9041d972017-01-17 18:23:12 -0800152 return g_vendor_interface->Open(initialize_complete_cb, packet_read_cb);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700153}
154
155void VendorInterface::Shutdown() {
156 CHECK(g_vendor_interface);
157 g_vendor_interface->Close();
158 delete g_vendor_interface;
159 g_vendor_interface = nullptr;
160}
161
162VendorInterface* VendorInterface::get() { return g_vendor_interface; }
163
Andre Eisenbach9041d972017-01-17 18:23:12 -0800164bool VendorInterface::Open(InitializeCompleteCallback initialize_complete_cb,
165 PacketReadCallback packet_read_cb) {
166 initialize_complete_cb_ = initialize_complete_cb;
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700167 packet_read_cb_ = packet_read_cb;
168
169 // Initialize vendor interface
170
171 lib_handle_ = dlopen(VENDOR_LIBRARY_NAME, RTLD_NOW);
172 if (!lib_handle_) {
173 ALOGE("%s unable to open %s (%s)", __func__, VENDOR_LIBRARY_NAME,
174 dlerror());
175 return false;
176 }
177
178 lib_interface_ = reinterpret_cast<bt_vendor_interface_t*>(
179 dlsym(lib_handle_, VENDOR_LIBRARY_SYMBOL_NAME));
180 if (!lib_interface_) {
181 ALOGE("%s unable to find symbol %s in %s (%s)", __func__,
182 VENDOR_LIBRARY_SYMBOL_NAME, VENDOR_LIBRARY_NAME, dlerror());
183 return false;
184 }
185
Myles Watson6a7d6222016-10-13 15:45:02 -0700186 // Get the local BD address
187
188 uint8_t local_bda[BluetoothAddress::kBytes];
189 CHECK(BluetoothAddress::get_local_address(local_bda));
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700190 int status = lib_interface_->init(&lib_callbacks, (unsigned char*)local_bda);
191 if (status) {
192 ALOGE("%s unable to initialize vendor library: %d", __func__, status);
193 return false;
194 }
195
196 ALOGD("%s vendor library loaded", __func__);
197
198 // Power cycle chip
199
200 int power_state = BT_VND_PWR_OFF;
201 lib_interface_->op(BT_VND_OP_POWER_CTRL, &power_state);
202 power_state = BT_VND_PWR_ON;
203 lib_interface_->op(BT_VND_OP_POWER_CTRL, &power_state);
204
205 // Get the UART socket
206
207 int fd_list[CH_MAX] = {0};
208 int fd_count = lib_interface_->op(BT_VND_OP_USERIAL_OPEN, &fd_list);
209
210 if (fd_count != 1) {
211 ALOGE("%s fd count %d != 1; we can't handle this currently...", __func__,
212 fd_count);
213 return false;
214 }
215
216 uart_fd_ = fd_list[0];
217 if (uart_fd_ == INVALID_FD) {
218 ALOGE("%s unable to determine UART fd", __func__);
219 return false;
220 }
221
222 ALOGD("%s UART fd: %d", __func__, uart_fd_);
223
224 fd_watcher_.WatchFdForNonBlockingReads(uart_fd_,
225 [this](int fd) { OnDataReady(fd); });
226
227 // Start configuring the firmware
Andre Eisenbach9041d972017-01-17 18:23:12 -0800228 firmware_startup_timer_ = new FirmwareStartupTimer();
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700229 lib_interface_->op(BT_VND_OP_FW_CFG, nullptr);
230
231 return true;
232}
233
234void VendorInterface::Close() {
235 fd_watcher_.StopWatchingFileDescriptor();
236
237 if (lib_interface_ != nullptr) {
238 lib_interface_->op(BT_VND_OP_USERIAL_CLOSE, nullptr);
239 uart_fd_ = INVALID_FD;
240 int power_state = BT_VND_PWR_OFF;
241 lib_interface_->op(BT_VND_OP_POWER_CTRL, &power_state);
242 }
243
244 if (lib_handle_ != nullptr) {
245 dlclose(lib_handle_);
246 lib_handle_ = nullptr;
247 }
248
Andre Eisenbach9041d972017-01-17 18:23:12 -0800249 if (firmware_startup_timer_ != nullptr) {
250 delete firmware_startup_timer_;
251 firmware_startup_timer_ = nullptr;
252 }
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700253}
254
255size_t VendorInterface::Send(const uint8_t* data, size_t length) {
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700256 if (uart_fd_ == INVALID_FD) return 0;
257
258 size_t transmitted_length = 0;
259 while (length > 0) {
260 ssize_t ret =
261 TEMP_FAILURE_RETRY(write(uart_fd_, data + transmitted_length, length));
262
263 if (ret == -1) {
264 if (errno == EAGAIN) continue;
265 ALOGE("%s error writing to UART (%s)", __func__, strerror(errno));
266 break;
267
268 } else if (ret == 0) {
269 // Nothing written :(
270 ALOGE("%s zero bytes written - something went wrong...", __func__);
271 break;
272 }
273
274 transmitted_length += ret;
275 length -= ret;
276 }
277
278 return transmitted_length;
279}
280
281void VendorInterface::OnFirmwareConfigured(uint8_t result) {
Andre Eisenbach9041d972017-01-17 18:23:12 -0800282 ALOGD("%s result: %d", __func__, result);
283 internal_command_cb = nullptr;
284
285 if (firmware_startup_timer_ != nullptr) {
286 delete firmware_startup_timer_;
287 firmware_startup_timer_ = nullptr;
288 }
289
290 if (initialize_complete_cb_ != nullptr) {
291 initialize_complete_cb_(result == 0);
292 initialize_complete_cb_ = nullptr;
293 }
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700294}
295
296void VendorInterface::OnDataReady(int fd) {
297 switch (hci_parser_state_) {
298 case HCI_IDLE: {
299 uint8_t buffer[1] = {0};
300 size_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, 1));
301 CHECK(bytes_read == 1);
302 hci_packet_type_ = static_cast<HciPacketType>(buffer[0]);
303 // TODO(eisenbach): Check for workaround(s)
304 CHECK(hci_packet_type_ >= HCI_PACKET_TYPE_ACL_DATA &&
305 hci_packet_type_ <= HCI_PACKET_TYPE_EVENT)
306 << "buffer[0] = " << buffer[0];
307 hci_parser_state_ = HCI_TYPE_READY;
308 hci_packet_.resize(HCI_PREAMBLE_SIZE_MAX);
309 hci_packet_bytes_remaining_ = preamble_size_for_type[hci_packet_type_];
310 hci_packet_bytes_read_ = 0;
311 break;
312 }
313
314 case HCI_TYPE_READY: {
315 size_t bytes_read = TEMP_FAILURE_RETRY(
316 read(fd, hci_packet_.data() + hci_packet_bytes_read_,
317 hci_packet_bytes_remaining_));
318 CHECK(bytes_read > 0);
319 hci_packet_bytes_remaining_ -= bytes_read;
320 hci_packet_bytes_read_ += bytes_read;
321 if (hci_packet_bytes_remaining_ == 0) {
322 size_t packet_length =
323 HciGetPacketLengthForType(hci_packet_type_, hci_packet_);
324 hci_packet_.resize(preamble_size_for_type[hci_packet_type_] +
325 packet_length);
326 hci_packet_bytes_remaining_ = packet_length;
327 hci_parser_state_ = HCI_PAYLOAD;
328 hci_packet_bytes_read_ = 0;
329 }
330 break;
331 }
332
333 case HCI_PAYLOAD: {
334 size_t bytes_read = TEMP_FAILURE_RETRY(
335 read(fd,
336 hci_packet_.data() + preamble_size_for_type[hci_packet_type_] +
337 hci_packet_bytes_read_,
338 hci_packet_bytes_remaining_));
339 hci_packet_bytes_remaining_ -= bytes_read;
340 hci_packet_bytes_read_ += bytes_read;
341 if (hci_packet_bytes_remaining_ == 0) {
Andre Eisenbach9041d972017-01-17 18:23:12 -0800342 if (internal_command_cb != nullptr) {
343 HC_BT_HDR* bt_hdr =
344 WrapPacketAndCopy(HCI_PACKET_TYPE_EVENT, hci_packet_);
345 internal_command_cb(bt_hdr);
346 } else if (packet_read_cb_ != nullptr &&
347 initialize_complete_cb_ == nullptr) {
348 packet_read_cb_(hci_packet_type_, hci_packet_);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700349 } else {
Andre Eisenbach9041d972017-01-17 18:23:12 -0800350 ALOGE(
351 "%s HCI_PAYLOAD received without packet_read_cb or pending init.",
352 __func__);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700353 }
354 hci_parser_state_ = HCI_IDLE;
355 }
356 break;
357 }
358 }
359}
360
361} // namespace implementation
362} // namespace V1_0
363} // namespace bluetooth
364} // namespace hardware
365} // namespace android