blob: 43abbe46bea5b0c91e5b6dceef0e19c938091571 [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) {
Peng Qi92afd742017-06-19 18:01:07 +080033 struct iovec iov[] = {{&type, sizeof(type)},
34 {const_cast<uint8_t*>(data), length}};
35 ssize_t ret = 0;
36 do {
Jack Hecaeab052018-10-23 18:13:51 -070037 ret =
38 TEMP_FAILURE_RETRY(writev(uart_fd_, iov, sizeof(iov) / sizeof(iov[0])));
Peng Qi92afd742017-06-19 18:01:07 +080039 } while (-1 == ret && EAGAIN == errno);
40
41 if (ret == -1) {
42 ALOGE("%s error writing to UART (%s)", __func__, strerror(errno));
Myles Watson3e272a72017-06-26 13:09:11 -070043 } else if (ret < static_cast<ssize_t>(length + 1)) {
44 ALOGE("%s: %d / %d bytes written - something went wrong...", __func__,
45 static_cast<int>(ret), static_cast<int>(length + 1));
Zach Johnson917efb12017-02-26 23:46:05 -080046 }
Peng Qi92afd742017-06-19 18:01:07 +080047 return ret;
Zach Johnson917efb12017-02-26 23:46:05 -080048}
49
50void H4Protocol::OnPacketReady() {
51 switch (hci_packet_type_) {
52 case HCI_PACKET_TYPE_EVENT:
53 event_cb_(hci_packetizer_.GetPacket());
54 break;
55 case HCI_PACKET_TYPE_ACL_DATA:
56 acl_cb_(hci_packetizer_.GetPacket());
57 break;
58 case HCI_PACKET_TYPE_SCO_DATA:
59 sco_cb_(hci_packetizer_.GetPacket());
60 break;
Jakub Pawlowski13b4d312019-11-05 12:27:29 +010061 case HCI_PACKET_TYPE_ISO_DATA:
62 iso_cb_(hci_packetizer_.GetPacket());
63 break;
Myles Watson3e272a72017-06-26 13:09:11 -070064 default:
65 LOG_ALWAYS_FATAL("%s: Unimplemented packet type %d", __func__,
66 static_cast<int>(hci_packet_type_));
Zach Johnson917efb12017-02-26 23:46:05 -080067 }
68 // Get ready for the next type byte.
69 hci_packet_type_ = HCI_PACKET_TYPE_UNKNOWN;
70}
71
72void H4Protocol::OnDataReady(int fd) {
73 if (hci_packet_type_ == HCI_PACKET_TYPE_UNKNOWN) {
74 uint8_t buffer[1] = {0};
Myles Watson3e272a72017-06-26 13:09:11 -070075 ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, 1));
76 if (bytes_read != 1) {
77 if (bytes_read == 0) {
Myles Watson0d63f8a2018-01-08 16:49:13 -080078 // This is only expected if the UART got closed when shutting down.
79 ALOGE("%s: Unexpected EOF reading the packet type!", __func__);
80 sleep(5); // Expect to be shut down within 5 seconds.
81 return;
Myles Watson3e272a72017-06-26 13:09:11 -070082 } else if (bytes_read < 0) {
83 LOG_ALWAYS_FATAL("%s: Read packet type error: %s", __func__,
84 strerror(errno));
85 } else {
86 LOG_ALWAYS_FATAL("%s: More bytes read than expected (%u)!", __func__,
87 static_cast<unsigned int>(bytes_read));
88 }
89 }
Zach Johnson917efb12017-02-26 23:46:05 -080090 hci_packet_type_ = static_cast<HciPacketType>(buffer[0]);
Myles Watson521621d2017-08-11 16:47:28 -070091 if (hci_packet_type_ != HCI_PACKET_TYPE_ACL_DATA &&
92 hci_packet_type_ != HCI_PACKET_TYPE_SCO_DATA &&
Jakub Pawlowskidb779b42020-11-23 14:07:54 +010093 hci_packet_type_ != HCI_PACKET_TYPE_ISO_DATA &&
Myles Watson521621d2017-08-11 16:47:28 -070094 hci_packet_type_ != HCI_PACKET_TYPE_EVENT) {
95 LOG_ALWAYS_FATAL("%s: Unimplemented packet type %d", __func__,
96 static_cast<int>(hci_packet_type_));
97 }
Zach Johnson917efb12017-02-26 23:46:05 -080098 } else {
99 hci_packetizer_.OnDataReady(fd, hci_packet_type_);
100 }
101}
102
103} // namespace hci
104} // namespace bluetooth
105} // namespace hardware
106} // namespace android