blob: 109ca5c60d623bc55e6a38196429aa37fe719485 [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) {
Sarveshwar Bajajfd3514a2024-11-12 01:07:59 +053048 struct iovec iov_array[] = {{&type, sizeof(type)},
49 {const_cast<uint8_t*>(data), length}};
50 size_t iovcnt = sizeof(iov_array) / sizeof(iov_array[0]);
51 struct iovec* iov = iov_array;
52 size_t total_bytes = sizeof(type) + length;
53 size_t remaining_bytes = total_bytes;
54 size_t bytes_written = 0;
55
56 while (remaining_bytes > 0) {
57 ssize_t ret = TEMP_FAILURE_RETRY(writev(uart_fd_, iov, iovcnt));
Myles Watsone4501e52022-09-30 06:20:50 -070058 if (ret == -1) {
Sarveshwar Bajajfd3514a2024-11-12 01:07:59 +053059 if (errno == EAGAIN) continue;
60 ALOGE("Error writing to UART (%s)", strerror(errno));
61 break;
Myles Watsone4501e52022-09-30 06:20:50 -070062 } else if (ret == 0) {
63 // Nothing written :(
64 ALOGE("%s zero bytes written - something went wrong...", __func__);
65 break;
Sarveshwar Bajajfd3514a2024-11-12 01:07:59 +053066 } else if (ret == remaining_bytes) {
67 // Everything written
68 bytes_written += ret;
69 break;
Myles Watsone4501e52022-09-30 06:20:50 -070070 }
Sarveshwar Bajajfd3514a2024-11-12 01:07:59 +053071
72 // Updating counters for partial writes
73 bytes_written += ret;
74 remaining_bytes -= ret;
75
76 ALOGW("%s: %zu/%zu bytes written - retrying remaining %zu bytes", __func__,
77 bytes_written, total_bytes, remaining_bytes);
78
79 // Remove fully written iovs from the list
80 while (ret >= iov->iov_len) {
81 ret -= iov->iov_len;
82 ++iov;
83 --iovcnt;
84 }
85 // Adjust the iov to point to the remaining data that needs to be written
86 if (ret > 0) {
87 iov->iov_base = static_cast<uint8_t*>(iov->iov_base) + ret;
88 iov->iov_len -= ret;
89 }
Myles Watsone4501e52022-09-30 06:20:50 -070090 }
Sarveshwar Bajajfd3514a2024-11-12 01:07:59 +053091 return total_bytes - remaining_bytes;
Myles Watsone4501e52022-09-30 06:20:50 -070092}
93
94size_t H4Protocol::OnPacketReady(const std::vector<uint8_t>& packet) {
95 switch (hci_packet_type_) {
96 case PacketType::COMMAND:
97 cmd_cb_(packet);
98 break;
99 case PacketType::ACL_DATA:
100 acl_cb_(packet);
101 break;
102 case PacketType::SCO_DATA:
103 sco_cb_(packet);
104 break;
105 case PacketType::EVENT:
106 event_cb_(packet);
107 break;
108 case PacketType::ISO_DATA:
109 iso_cb_(packet);
110 break;
111 default: {
112 LOG_ALWAYS_FATAL("Bad packet type 0x%x",
113 static_cast<int>(hci_packet_type_));
114 }
115 }
116 return packet.size();
117}
118
119void H4Protocol::SendDataToPacketizer(uint8_t* buffer, size_t length) {
120 std::vector<uint8_t> input_buffer{buffer, buffer + length};
121 size_t buffer_offset = 0;
122 while (buffer_offset < input_buffer.size()) {
123 if (hci_packet_type_ == PacketType::UNKNOWN) {
124 hci_packet_type_ =
125 static_cast<PacketType>(input_buffer.data()[buffer_offset]);
126 buffer_offset += 1;
127 } else {
128 bool packet_ready = hci_packetizer_.OnDataReady(
Henri Chataing28453032023-04-19 18:14:03 +0000129 hci_packet_type_, input_buffer, &buffer_offset);
Myles Watsone4501e52022-09-30 06:20:50 -0700130 if (packet_ready) {
Henri Chataing28453032023-04-19 18:14:03 +0000131 // Call packet callback.
132 OnPacketReady(hci_packetizer_.GetPacket());
Myles Watsone4501e52022-09-30 06:20:50 -0700133 // Get ready for the next type byte.
134 hci_packet_type_ = PacketType::UNKNOWN;
Myles Watsone4501e52022-09-30 06:20:50 -0700135 }
136 }
137 }
138}
139
140void H4Protocol::OnDataReady() {
141 if (disconnected_) {
142 return;
143 }
144 uint8_t buffer[kMaxPacketLength];
145 ssize_t bytes_read =
146 TEMP_FAILURE_RETRY(read(uart_fd_, buffer, kMaxPacketLength));
147 if (bytes_read == 0) {
148 ALOGI("No bytes read, calling the disconnect callback");
149 disconnected_ = true;
150 disconnect_cb_();
151 return;
152 }
153 if (bytes_read < 0) {
154 ALOGW("error reading from UART (%s)", strerror(errno));
155 return;
156 }
157 SendDataToPacketizer(buffer, bytes_read);
158}
159
160} // namespace android::hardware::bluetooth::hci