1 /*
2 * Copyright 2022 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
21 #include <assert.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <string.h>
25 #include <sys/uio.h>
26
27 #include "log/log.h"
28
29 namespace android::hardware::bluetooth::hci {
30
H4Protocol(int fd,PacketReadCallback cmd_cb,PacketReadCallback acl_cb,PacketReadCallback sco_cb,PacketReadCallback event_cb,PacketReadCallback iso_cb,DisconnectCallback disconnect_cb)31 H4Protocol::H4Protocol(int fd, PacketReadCallback cmd_cb,
32 PacketReadCallback acl_cb, PacketReadCallback sco_cb,
33 PacketReadCallback event_cb, PacketReadCallback iso_cb,
34 DisconnectCallback disconnect_cb)
35 : uart_fd_(fd),
36 cmd_cb_(std::move(cmd_cb)),
37 acl_cb_(std::move(acl_cb)),
38 sco_cb_(std::move(sco_cb)),
39 event_cb_(std::move(event_cb)),
40 iso_cb_(std::move(iso_cb)),
41 disconnect_cb_(std::move(disconnect_cb)) {}
42
Send(PacketType type,const std::vector<uint8_t> & vector)43 size_t H4Protocol::Send(PacketType type, const std::vector<uint8_t>& vector) {
44 return Send(type, vector.data(), vector.size());
45 }
46
Send(PacketType type,const uint8_t * data,size_t length)47 size_t H4Protocol::Send(PacketType type, const uint8_t* data, size_t length) {
48 /* For HCI communication over USB dongle, multiple write results in
49 * response timeout as driver expect type + data at once to process
50 * the command, so using "writev"(for atomicity) here.
51 */
52 struct iovec iov[2];
53 ssize_t ret = 0;
54 iov[0].iov_base = &type;
55 iov[0].iov_len = sizeof(type);
56 iov[1].iov_base = (void*)data;
57 iov[1].iov_len = length;
58 while (1) {
59 ret = TEMP_FAILURE_RETRY(writev(uart_fd_, iov, 2));
60 if (ret == -1) {
61 LOG_ALWAYS_FATAL("%s error writing to UART (%s)", __func__,
62 strerror(errno));
63 } else if (ret == 0) {
64 // Nothing written :(
65 ALOGE("%s zero bytes written - something went wrong...", __func__);
66 break;
67 }
68 break;
69 }
70 return ret;
71 }
72
OnPacketReady(const std::vector<uint8_t> & packet)73 size_t H4Protocol::OnPacketReady(const std::vector<uint8_t>& packet) {
74 switch (hci_packet_type_) {
75 case PacketType::COMMAND:
76 cmd_cb_(packet);
77 break;
78 case PacketType::ACL_DATA:
79 acl_cb_(packet);
80 break;
81 case PacketType::SCO_DATA:
82 sco_cb_(packet);
83 break;
84 case PacketType::EVENT:
85 event_cb_(packet);
86 break;
87 case PacketType::ISO_DATA:
88 iso_cb_(packet);
89 break;
90 default: {
91 LOG_ALWAYS_FATAL("Bad packet type 0x%x",
92 static_cast<int>(hci_packet_type_));
93 }
94 }
95 return packet.size();
96 }
97
SendDataToPacketizer(uint8_t * buffer,size_t length)98 void H4Protocol::SendDataToPacketizer(uint8_t* buffer, size_t length) {
99 std::vector<uint8_t> input_buffer{buffer, buffer + length};
100 size_t buffer_offset = 0;
101 while (buffer_offset < input_buffer.size()) {
102 if (hci_packet_type_ == PacketType::UNKNOWN) {
103 hci_packet_type_ =
104 static_cast<PacketType>(input_buffer.data()[buffer_offset]);
105 buffer_offset += 1;
106 } else {
107 bool packet_ready = hci_packetizer_.OnDataReady(
108 hci_packet_type_, input_buffer, &buffer_offset);
109 if (packet_ready) {
110 // Call packet callback.
111 OnPacketReady(hci_packetizer_.GetPacket());
112 // Get ready for the next type byte.
113 hci_packet_type_ = PacketType::UNKNOWN;
114 }
115 }
116 }
117 }
118
OnDataReady()119 void H4Protocol::OnDataReady() {
120 if (disconnected_) {
121 return;
122 }
123 uint8_t buffer[kMaxPacketLength];
124 ssize_t bytes_read =
125 TEMP_FAILURE_RETRY(read(uart_fd_, buffer, kMaxPacketLength));
126 if (bytes_read == 0) {
127 ALOGI("No bytes read, calling the disconnect callback");
128 disconnected_ = true;
129 disconnect_cb_();
130 return;
131 }
132 if (bytes_read < 0) {
133 ALOGW("error reading from UART (%s)", strerror(errno));
134 return;
135 }
136 SendDataToPacketizer(buffer, bytes_read);
137 }
138
139 } // namespace android::hardware::bluetooth::hci
140