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
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <log/log.h>
24 #include <sys/uio.h>
25 #include <unistd.h>
26
27 namespace android {
28 namespace hardware {
29 namespace bluetooth {
30 namespace hci {
31
Send(uint8_t type,const uint8_t * data,size_t length)32 size_t H4Protocol::Send(uint8_t type, const uint8_t* data, size_t length) {
33 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;
40 }
41 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;
79 }
80
OnPacketReady()81 void 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;
92 case HCI_PACKET_TYPE_ISO_DATA:
93 iso_cb_(hci_packetizer_.GetPacket());
94 break;
95 default:
96 LOG_ALWAYS_FATAL("%s: Unimplemented packet type %d", __func__,
97 static_cast<int>(hci_packet_type_));
98 }
99 // Get ready for the next type byte.
100 hci_packet_type_ = HCI_PACKET_TYPE_UNKNOWN;
101 }
102
OnDataReady(int fd)103 void H4Protocol::OnDataReady(int fd) {
104 if (hci_packet_type_ == HCI_PACKET_TYPE_UNKNOWN) {
105 uint8_t buffer[1] = {0};
106 ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, 1));
107 if (bytes_read != 1) {
108 if (bytes_read == 0) {
109 // 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;
113 } 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 }
121 hci_packet_type_ = static_cast<HciPacketType>(buffer[0]);
122 if (hci_packet_type_ != HCI_PACKET_TYPE_ACL_DATA &&
123 hci_packet_type_ != HCI_PACKET_TYPE_SCO_DATA &&
124 hci_packet_type_ != HCI_PACKET_TYPE_ISO_DATA &&
125 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 }
129 } else {
130 hci_packetizer_.OnDataReady(fd, hci_packet_type_);
131 }
132 }
133
134 } // namespace hci
135 } // namespace bluetooth
136 } // namespace hardware
137 } // namespace android
138