blob: 905e1a6ac977bc977ef561a27fecd61e81d7f2da [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;
38
39tINT_CMD_CBACK internal_command_cb;
40VendorInterface* g_vendor_interface = nullptr;
41
42const size_t preamble_size_for_type[] = {
43 0, HCI_COMMAND_PREAMBLE_SIZE, HCI_ACL_PREAMBLE_SIZE, HCI_SCO_PREAMBLE_SIZE,
44 HCI_EVENT_PREAMBLE_SIZE};
45const size_t packet_length_offset_for_type[] = {
46 0, HCI_LENGTH_OFFSET_CMD, HCI_LENGTH_OFFSET_ACL, HCI_LENGTH_OFFSET_SCO,
47 HCI_LENGTH_OFFSET_EVT};
48
49size_t HciGetPacketLengthForType(
50 HciPacketType type, const android::hardware::hidl_vec<uint8_t>& packet) {
51 size_t offset = packet_length_offset_for_type[type];
52 if (type == HCI_PACKET_TYPE_ACL_DATA) {
53 return (((packet[offset + 1]) << 8) | packet[offset]);
54 }
55 return packet[offset];
56}
57
58HC_BT_HDR* WrapPacketAndCopy(uint16_t event,
59 const android::hardware::hidl_vec<uint8_t>& data) {
60 size_t packet_size = data.size() + sizeof(HC_BT_HDR);
61 HC_BT_HDR* packet = reinterpret_cast<HC_BT_HDR*>(new uint8_t[packet_size]);
62 packet->offset = 0;
63 packet->len = data.size();
64 packet->layer_specific = 0;
65 packet->event = event;
Myles Watson6a7d6222016-10-13 15:45:02 -070066 // TODO(eisenbach): Avoid copy here; if BT_HDR->data can be ensured to
Andre Eisenbach89ba5282016-10-13 15:45:02 -070067 // be the only way the data is accessed, a pointer could be passed here...
68 memcpy(packet->data, data.data(), data.size());
69 return packet;
70}
71
72uint8_t transmit_cb(uint16_t opcode, void* buffer, tINT_CMD_CBACK callback) {
73 ALOGV("%s opcode: 0x%04x, ptr: %p", __func__, opcode, buffer);
74 HC_BT_HDR* bt_hdr = reinterpret_cast<HC_BT_HDR*>(buffer);
75
76 internal_command_cb = callback;
77 uint8_t type = HCI_PACKET_TYPE_COMMAND;
78 VendorInterface::get()->SendPrivate(&type, 1);
79 VendorInterface::get()->SendPrivate(bt_hdr->data, bt_hdr->len);
80 return true;
81}
82
83void firmware_config_cb(bt_vendor_op_result_t result) {
84 ALOGD("%s result: %d", __func__, result);
85 VendorInterface::get()->OnFirmwareConfigured(result);
86}
87
88void sco_config_cb(bt_vendor_op_result_t result) {
89 ALOGD("%s result: %d", __func__, result);
90}
91
92void low_power_mode_cb(bt_vendor_op_result_t result) {
93 ALOGD("%s result: %d", __func__, result);
94}
95
96void sco_audiostate_cb(bt_vendor_op_result_t result) {
97 ALOGD("%s result: %d", __func__, result);
98}
99
100void* buffer_alloc_cb(int size) {
101 void* p = new uint8_t[size];
102 ALOGV("%s pts: %p, size: %d", __func__, p, size);
103 return p;
104}
105
106void buffer_free_cb(void* buffer) {
107 ALOGV("%s ptr: %p", __func__, buffer);
108 delete[] reinterpret_cast<uint8_t*>(buffer);
109}
110
111void epilog_cb(bt_vendor_op_result_t result) {
112 ALOGD("%s result: %d", __func__, result);
113}
114
115void a2dp_offload_cb(bt_vendor_op_result_t result, bt_vendor_opcode_t op,
116 uint8_t av_handle) {
117 ALOGD("%s result: %d, op: %d, handle: %d", __func__, result, op, av_handle);
118}
119
120const bt_vendor_callbacks_t lib_callbacks = {
121 sizeof(lib_callbacks), firmware_config_cb, sco_config_cb,
122 low_power_mode_cb, sco_audiostate_cb, buffer_alloc_cb,
123 buffer_free_cb, transmit_cb, epilog_cb,
124 a2dp_offload_cb};
125
126} // namespace
127
128namespace android {
129namespace hardware {
130namespace bluetooth {
131namespace V1_0 {
132namespace implementation {
133
134bool VendorInterface::Initialize(PacketReadCallback packet_read_cb) {
135 assert(!g_vendor_interface);
136 g_vendor_interface = new VendorInterface();
137 return g_vendor_interface->Open(packet_read_cb);
138}
139
140void VendorInterface::Shutdown() {
141 CHECK(g_vendor_interface);
142 g_vendor_interface->Close();
143 delete g_vendor_interface;
144 g_vendor_interface = nullptr;
145}
146
147VendorInterface* VendorInterface::get() { return g_vendor_interface; }
148
149bool VendorInterface::Open(PacketReadCallback packet_read_cb) {
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700150 firmware_configured_ = false;
151 packet_read_cb_ = packet_read_cb;
152
153 // Initialize vendor interface
154
155 lib_handle_ = dlopen(VENDOR_LIBRARY_NAME, RTLD_NOW);
156 if (!lib_handle_) {
157 ALOGE("%s unable to open %s (%s)", __func__, VENDOR_LIBRARY_NAME,
158 dlerror());
159 return false;
160 }
161
162 lib_interface_ = reinterpret_cast<bt_vendor_interface_t*>(
163 dlsym(lib_handle_, VENDOR_LIBRARY_SYMBOL_NAME));
164 if (!lib_interface_) {
165 ALOGE("%s unable to find symbol %s in %s (%s)", __func__,
166 VENDOR_LIBRARY_SYMBOL_NAME, VENDOR_LIBRARY_NAME, dlerror());
167 return false;
168 }
169
Myles Watson6a7d6222016-10-13 15:45:02 -0700170 // Get the local BD address
171
172 uint8_t local_bda[BluetoothAddress::kBytes];
173 CHECK(BluetoothAddress::get_local_address(local_bda));
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700174 int status = lib_interface_->init(&lib_callbacks, (unsigned char*)local_bda);
175 if (status) {
176 ALOGE("%s unable to initialize vendor library: %d", __func__, status);
177 return false;
178 }
179
180 ALOGD("%s vendor library loaded", __func__);
181
182 // Power cycle chip
183
184 int power_state = BT_VND_PWR_OFF;
185 lib_interface_->op(BT_VND_OP_POWER_CTRL, &power_state);
186 power_state = BT_VND_PWR_ON;
187 lib_interface_->op(BT_VND_OP_POWER_CTRL, &power_state);
188
189 // Get the UART socket
190
191 int fd_list[CH_MAX] = {0};
192 int fd_count = lib_interface_->op(BT_VND_OP_USERIAL_OPEN, &fd_list);
193
194 if (fd_count != 1) {
195 ALOGE("%s fd count %d != 1; we can't handle this currently...", __func__,
196 fd_count);
197 return false;
198 }
199
200 uart_fd_ = fd_list[0];
201 if (uart_fd_ == INVALID_FD) {
202 ALOGE("%s unable to determine UART fd", __func__);
203 return false;
204 }
205
206 ALOGD("%s UART fd: %d", __func__, uart_fd_);
207
208 fd_watcher_.WatchFdForNonBlockingReads(uart_fd_,
209 [this](int fd) { OnDataReady(fd); });
210
211 // Start configuring the firmware
212 lib_interface_->op(BT_VND_OP_FW_CFG, nullptr);
213
214 return true;
215}
216
217void VendorInterface::Close() {
218 fd_watcher_.StopWatchingFileDescriptor();
219
220 if (lib_interface_ != nullptr) {
221 lib_interface_->op(BT_VND_OP_USERIAL_CLOSE, nullptr);
222 uart_fd_ = INVALID_FD;
223 int power_state = BT_VND_PWR_OFF;
224 lib_interface_->op(BT_VND_OP_POWER_CTRL, &power_state);
225 }
226
227 if (lib_handle_ != nullptr) {
228 dlclose(lib_handle_);
229 lib_handle_ = nullptr;
230 }
231
232 firmware_configured_ = false;
233}
234
235size_t VendorInterface::Send(const uint8_t* data, size_t length) {
236 if (firmware_configured_ && queued_data_.size() == 0)
237 return SendPrivate(data, length);
238
239 if (!firmware_configured_) {
240 ALOGI("%s queueing command", __func__);
241 queued_data_.resize(queued_data_.size() + length);
242 uint8_t* append_ptr = &queued_data_[queued_data_.size() - length];
243 memcpy(append_ptr, data, length);
244 return length;
245 }
246
247 ALOGI("%s sending queued command", __func__);
248 SendPrivate(queued_data_.data(), queued_data_.size());
249 queued_data_.resize(0);
250
251 ALOGI("%s done sending queued command", __func__);
252
253 return SendPrivate(data, length);
254}
255
256size_t VendorInterface::SendPrivate(const uint8_t* data, size_t length) {
257 if (uart_fd_ == INVALID_FD) return 0;
258
259 size_t transmitted_length = 0;
260 while (length > 0) {
261 ssize_t ret =
262 TEMP_FAILURE_RETRY(write(uart_fd_, data + transmitted_length, length));
263
264 if (ret == -1) {
265 if (errno == EAGAIN) continue;
266 ALOGE("%s error writing to UART (%s)", __func__, strerror(errno));
267 break;
268
269 } else if (ret == 0) {
270 // Nothing written :(
271 ALOGE("%s zero bytes written - something went wrong...", __func__);
272 break;
273 }
274
275 transmitted_length += ret;
276 length -= ret;
277 }
278
279 return transmitted_length;
280}
281
282void VendorInterface::OnFirmwareConfigured(uint8_t result) {
283 ALOGI("%s: result = %d", __func__, result);
284 firmware_configured_ = true;
285 VendorInterface::get()->Send(NULL, 0);
286}
287
288void VendorInterface::OnDataReady(int fd) {
289 switch (hci_parser_state_) {
290 case HCI_IDLE: {
291 uint8_t buffer[1] = {0};
292 size_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, 1));
293 CHECK(bytes_read == 1);
294 hci_packet_type_ = static_cast<HciPacketType>(buffer[0]);
295 // TODO(eisenbach): Check for workaround(s)
296 CHECK(hci_packet_type_ >= HCI_PACKET_TYPE_ACL_DATA &&
297 hci_packet_type_ <= HCI_PACKET_TYPE_EVENT)
298 << "buffer[0] = " << buffer[0];
299 hci_parser_state_ = HCI_TYPE_READY;
300 hci_packet_.resize(HCI_PREAMBLE_SIZE_MAX);
301 hci_packet_bytes_remaining_ = preamble_size_for_type[hci_packet_type_];
302 hci_packet_bytes_read_ = 0;
303 break;
304 }
305
306 case HCI_TYPE_READY: {
307 size_t bytes_read = TEMP_FAILURE_RETRY(
308 read(fd, hci_packet_.data() + hci_packet_bytes_read_,
309 hci_packet_bytes_remaining_));
310 CHECK(bytes_read > 0);
311 hci_packet_bytes_remaining_ -= bytes_read;
312 hci_packet_bytes_read_ += bytes_read;
313 if (hci_packet_bytes_remaining_ == 0) {
314 size_t packet_length =
315 HciGetPacketLengthForType(hci_packet_type_, hci_packet_);
316 hci_packet_.resize(preamble_size_for_type[hci_packet_type_] +
317 packet_length);
318 hci_packet_bytes_remaining_ = packet_length;
319 hci_parser_state_ = HCI_PAYLOAD;
320 hci_packet_bytes_read_ = 0;
321 }
322 break;
323 }
324
325 case HCI_PAYLOAD: {
326 size_t bytes_read = TEMP_FAILURE_RETRY(
327 read(fd,
328 hci_packet_.data() + preamble_size_for_type[hci_packet_type_] +
329 hci_packet_bytes_read_,
330 hci_packet_bytes_remaining_));
331 hci_packet_bytes_remaining_ -= bytes_read;
332 hci_packet_bytes_read_ += bytes_read;
333 if (hci_packet_bytes_remaining_ == 0) {
334 if (firmware_configured_) {
335 if (packet_read_cb_ != nullptr) {
336 packet_read_cb_(hci_packet_type_, hci_packet_);
337 }
338 } else {
339 if (internal_command_cb != nullptr) {
340 HC_BT_HDR* bt_hdr =
341 WrapPacketAndCopy(HCI_PACKET_TYPE_EVENT, hci_packet_);
342 internal_command_cb(bt_hdr);
343 }
344 }
345 hci_parser_state_ = HCI_IDLE;
346 }
347 break;
348 }
349 }
350}
351
352} // namespace implementation
353} // namespace V1_0
354} // namespace bluetooth
355} // namespace hardware
356} // namespace android