blob: 97ba7aa76a8a81407b487109f710961a1485e3e2 [file] [log] [blame]
Myles Watsone4501e52022-09-30 06:20:50 -07001/*
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
29namespace android::hardware::bluetooth::hci {
30
31H4Protocol::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
43size_t H4Protocol::Send(PacketType type, const std::vector<uint8_t>& vector) {
44 return Send(type, vector.data(), vector.size());
45}
46
47size_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 if (errno == EAGAIN) {
62 ALOGE("%s error writing to UART (%s)", __func__, strerror(errno));
63 continue;
64 }
65 } else if (ret == 0) {
66 // Nothing written :(
67 ALOGE("%s zero bytes written - something went wrong...", __func__);
68 break;
69 }
70 break;
71 }
72 return ret;
73}
74
75size_t H4Protocol::OnPacketReady(const std::vector<uint8_t>& packet) {
76 switch (hci_packet_type_) {
77 case PacketType::COMMAND:
78 cmd_cb_(packet);
79 break;
80 case PacketType::ACL_DATA:
81 acl_cb_(packet);
82 break;
83 case PacketType::SCO_DATA:
84 sco_cb_(packet);
85 break;
86 case PacketType::EVENT:
87 event_cb_(packet);
88 break;
89 case PacketType::ISO_DATA:
90 iso_cb_(packet);
91 break;
92 default: {
93 LOG_ALWAYS_FATAL("Bad packet type 0x%x",
94 static_cast<int>(hci_packet_type_));
95 }
96 }
97 return packet.size();
98}
99
100void H4Protocol::SendDataToPacketizer(uint8_t* buffer, size_t length) {
101 std::vector<uint8_t> input_buffer{buffer, buffer + length};
102 size_t buffer_offset = 0;
103 while (buffer_offset < input_buffer.size()) {
104 if (hci_packet_type_ == PacketType::UNKNOWN) {
105 hci_packet_type_ =
106 static_cast<PacketType>(input_buffer.data()[buffer_offset]);
107 buffer_offset += 1;
108 } else {
109 bool packet_ready = hci_packetizer_.OnDataReady(
110 hci_packet_type_, input_buffer, buffer_offset);
111 if (packet_ready) {
112 // Call packet callback and move offset.
113 buffer_offset += OnPacketReady(hci_packetizer_.GetPacket());
114 // Get ready for the next type byte.
115 hci_packet_type_ = PacketType::UNKNOWN;
116 } else {
117 // The data was consumed, but there wasn't a packet.
118 buffer_offset = input_buffer.size();
119 }
120 }
121 }
122}
123
124void H4Protocol::OnDataReady() {
125 if (disconnected_) {
126 return;
127 }
128 uint8_t buffer[kMaxPacketLength];
129 ssize_t bytes_read =
130 TEMP_FAILURE_RETRY(read(uart_fd_, buffer, kMaxPacketLength));
131 if (bytes_read == 0) {
132 ALOGI("No bytes read, calling the disconnect callback");
133 disconnected_ = true;
134 disconnect_cb_();
135 return;
136 }
137 if (bytes_read < 0) {
138 ALOGW("error reading from UART (%s)", strerror(errno));
139 return;
140 }
141 SendDataToPacketizer(buffer, bytes_read);
142}
143
144} // namespace android::hardware::bluetooth::hci