1 //
2 // Copyright 2017 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 "h4_protocol.h"
18
19 #define LOG_TAG "android.hardware.bluetooth-hci-h4"
20 #include <android-base/logging.h>
21 #include <assert.h>
22 #include <fcntl.h>
23 #include <sys/uio.h>
24 #include <utils/Log.h>
25
26 namespace android {
27 namespace hardware {
28 namespace bluetooth {
29 namespace hci {
30
Send(uint8_t type,const uint8_t * data,size_t length)31 size_t H4Protocol::Send(uint8_t type, const uint8_t* data, size_t length) {
32 /* For HCI communication over USB dongle, multiple write results in
33 * response timeout as driver expect type + data at once to process
34 * the command, so using "writev"(for atomicity) here.
35 */
36 struct iovec iov[2];
37 ssize_t ret = 0;
38 iov[0].iov_base = &type;
39 iov[0].iov_len = sizeof(type);
40 iov[1].iov_base = (void *)data;
41 iov[1].iov_len = length;
42 while (1) {
43 ret = TEMP_FAILURE_RETRY(writev(uart_fd_, iov, 2));
44 if (ret == -1) {
45 if (errno == EAGAIN) {
46 ALOGE("%s error writing to UART (%s)", __func__, strerror(errno));
47 continue;
48 }
49 } else if (ret == 0) {
50 // Nothing written :(
51 ALOGE("%s zero bytes written - something went wrong...", __func__);
52 break;
53 }
54 break;
55 }
56 return ret;
57 }
58
OnPacketReady()59 void H4Protocol::OnPacketReady() {
60 switch (hci_packet_type_) {
61 case HCI_PACKET_TYPE_EVENT:
62 event_cb_(hci_packetizer_.GetPacket());
63 break;
64 case HCI_PACKET_TYPE_ACL_DATA:
65 acl_cb_(hci_packetizer_.GetPacket());
66 break;
67 case HCI_PACKET_TYPE_SCO_DATA:
68 sco_cb_(hci_packetizer_.GetPacket());
69 break;
70 case HCI_PACKET_TYPE_ISO_DATA:
71 iso_cb_(hci_packetizer_.GetPacket());
72 break;
73 default: {
74 bool bad_packet_type = true;
75 CHECK(!bad_packet_type);
76 }
77 }
78 // Get ready for the next type byte.
79 hci_packet_type_ = HCI_PACKET_TYPE_UNKNOWN;
80 }
81
OnDataReady(int fd)82 void H4Protocol::OnDataReady(int fd) {
83 if (hci_packet_type_ == HCI_PACKET_TYPE_UNKNOWN) {
84 /**
85 * read full buffer. ACL max length is 2 bytes, and SCO max length is 2
86 * byte. so taking 64K as buffer length.
87 * Question : Why to read in single chunk rather than multiple reads,
88 * which can give parameter length arriving in response ?
89 * Answer: The multiple reads does not work with BT USB dongle. At least
90 * with Bluetooth 2.0 supported USB dongle. After first read, either
91 * firmware/kernel (do not know who is responsible - inputs ??) driver
92 * discard the whole message and successive read results in forever
93 * blocking loop. - Is there any other way to make it work with multiple
94 * reads, do not know yet (it can eliminate need of this function) ?
95 * Reading in single shot gives expected response.
96 */
97 const size_t max_plen = 64*1024;
98 hidl_vec<uint8_t> tpkt;
99 tpkt.resize(max_plen);
100 size_t bytes_read = TEMP_FAILURE_RETRY(read(fd, tpkt.data(), max_plen));
101 hci_packet_type_ = static_cast<HciPacketType>(tpkt.data()[0]);
102 hci_packetizer_.CbHciPacket(tpkt.data()+1, bytes_read-1);
103 }
104 }
105
106 } // namespace hci
107 } // namespace bluetooth
108 } // namespace hardware
109 } // namespace android
110