blob: 33238dab53f1522db0ba62b135022d07622ed4c0 [file] [log] [blame]
Zach Johnson917efb12017-02-26 23:46:05 -08001//
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"
Myles Watson3e272a72017-06-26 13:09:11 -070020
Peng Qi92afd742017-06-19 18:01:07 +080021#include <errno.h>
Zach Johnson917efb12017-02-26 23:46:05 -080022#include <fcntl.h>
Peng Qi92afd742017-06-19 18:01:07 +080023#include <log/log.h>
24#include <sys/uio.h>
Jiyong Parkcb198f72017-07-10 13:27:05 +090025#include <unistd.h>
Zach Johnson917efb12017-02-26 23:46:05 -080026
27namespace android {
28namespace hardware {
29namespace bluetooth {
30namespace hci {
31
32size_t H4Protocol::Send(uint8_t type, const uint8_t* data, size_t length) {
Sukesh Srikakula1e2b2342021-10-15 17:17:48 -070033 struct iovec iov_array[] = {{&type, sizeof(type)},
34 {const_cast<uint8_t*>(data), length}};
35 struct iovec* iov = iov_array;
36 int iovcnt = sizeof(iov_array) / sizeof(iov_array[0]);
37 size_t total_bytes = 0;
38 for (int i = 0; i < iovcnt; i++) {
39 total_bytes += iov_array[i].iov_len;
Zach Johnson917efb12017-02-26 23:46:05 -080040 }
Sukesh Srikakula1e2b2342021-10-15 17:17:48 -070041 size_t bytes_written = 0;
42 size_t remaining_bytes = total_bytes;
43
44 while (remaining_bytes > 0) {
45 ssize_t ret = TEMP_FAILURE_RETRY(writev(uart_fd_, iov, iovcnt));
46 if (ret == -1) {
47 if (errno == EAGAIN) continue;
48 ALOGE("%s error writing to UART (%s)", __func__, strerror(errno));
49 break;
50 } else if (ret == 0) {
51 // Nothing written
52 ALOGE("%s zero bytes written - something went wrong...", __func__);
53 break;
54 } else if (ret == remaining_bytes) {
55 // Everything written
56 bytes_written += ret;
57 break;
58 }
59
60 bytes_written += ret;
61 remaining_bytes -= ret;
62 ALOGW("%s: %d/%d bytes written - retrying remaining %d bytes", __func__,
63 static_cast<int>(bytes_written), static_cast<int>(total_bytes),
64 static_cast<int>(remaining_bytes));
65
66 // Remove iovs which are written from the list
67 while (ret >= iov->iov_len) {
68 ret -= iov->iov_len;
69 ++iov;
70 --iovcnt;
71 }
72 // Adjust the iov to point to the remaining data which needs to be written
73 if (ret) {
74 iov->iov_base = static_cast<uint8_t*>(iov->iov_base) + ret;
75 iov->iov_len -= ret;
76 }
77 }
78 return bytes_written;
Zach Johnson917efb12017-02-26 23:46:05 -080079}
80
81void H4Protocol::OnPacketReady() {
82 switch (hci_packet_type_) {
83 case HCI_PACKET_TYPE_EVENT:
84 event_cb_(hci_packetizer_.GetPacket());
85 break;
86 case HCI_PACKET_TYPE_ACL_DATA:
87 acl_cb_(hci_packetizer_.GetPacket());
88 break;
89 case HCI_PACKET_TYPE_SCO_DATA:
90 sco_cb_(hci_packetizer_.GetPacket());
91 break;
Jakub Pawlowski13b4d312019-11-05 12:27:29 +010092 case HCI_PACKET_TYPE_ISO_DATA:
93 iso_cb_(hci_packetizer_.GetPacket());
94 break;
Myles Watson3e272a72017-06-26 13:09:11 -070095 default:
96 LOG_ALWAYS_FATAL("%s: Unimplemented packet type %d", __func__,
97 static_cast<int>(hci_packet_type_));
Zach Johnson917efb12017-02-26 23:46:05 -080098 }
99 // Get ready for the next type byte.
100 hci_packet_type_ = HCI_PACKET_TYPE_UNKNOWN;
101}
102
103void H4Protocol::OnDataReady(int fd) {
104 if (hci_packet_type_ == HCI_PACKET_TYPE_UNKNOWN) {
105 uint8_t buffer[1] = {0};
Myles Watson3e272a72017-06-26 13:09:11 -0700106 ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, 1));
107 if (bytes_read != 1) {
108 if (bytes_read == 0) {
Myles Watson0d63f8a2018-01-08 16:49:13 -0800109 // This is only expected if the UART got closed when shutting down.
110 ALOGE("%s: Unexpected EOF reading the packet type!", __func__);
111 sleep(5); // Expect to be shut down within 5 seconds.
112 return;
Myles Watson3e272a72017-06-26 13:09:11 -0700113 } else if (bytes_read < 0) {
114 LOG_ALWAYS_FATAL("%s: Read packet type error: %s", __func__,
115 strerror(errno));
116 } else {
117 LOG_ALWAYS_FATAL("%s: More bytes read than expected (%u)!", __func__,
118 static_cast<unsigned int>(bytes_read));
119 }
120 }
Zach Johnson917efb12017-02-26 23:46:05 -0800121 hci_packet_type_ = static_cast<HciPacketType>(buffer[0]);
Myles Watson521621d2017-08-11 16:47:28 -0700122 if (hci_packet_type_ != HCI_PACKET_TYPE_ACL_DATA &&
123 hci_packet_type_ != HCI_PACKET_TYPE_SCO_DATA &&
Jakub Pawlowskidb779b42020-11-23 14:07:54 +0100124 hci_packet_type_ != HCI_PACKET_TYPE_ISO_DATA &&
Myles Watson521621d2017-08-11 16:47:28 -0700125 hci_packet_type_ != HCI_PACKET_TYPE_EVENT) {
126 LOG_ALWAYS_FATAL("%s: Unimplemented packet type %d", __func__,
127 static_cast<int>(hci_packet_type_));
128 }
Zach Johnson917efb12017-02-26 23:46:05 -0800129 } else {
130 hci_packetizer_.OnDataReady(fd, hci_packet_type_);
131 }
132}
133
134} // namespace hci
135} // namespace bluetooth
136} // namespace hardware
137} // namespace android