blob: 2008b000934cb46a9b957dc9d8b705ba42673091 [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>
Zach Johnson917efb12017-02-26 23:46:05 -080025
26namespace android {
27namespace hardware {
28namespace bluetooth {
29namespace hci {
30
31size_t H4Protocol::Send(uint8_t type, const uint8_t* data, size_t length) {
Peng Qi92afd742017-06-19 18:01:07 +080032 struct iovec iov[] = {{&type, sizeof(type)},
33 {const_cast<uint8_t*>(data), length}};
34 ssize_t ret = 0;
35 do {
36 ret = TEMP_FAILURE_RETRY(writev(uart_fd_, iov, sizeof(iov) / sizeof(iov[0])));
37 } while (-1 == ret && EAGAIN == errno);
38
39 if (ret == -1) {
40 ALOGE("%s error writing to UART (%s)", __func__, strerror(errno));
Myles Watson3e272a72017-06-26 13:09:11 -070041 } else if (ret < static_cast<ssize_t>(length + 1)) {
42 ALOGE("%s: %d / %d bytes written - something went wrong...", __func__,
43 static_cast<int>(ret), static_cast<int>(length + 1));
Zach Johnson917efb12017-02-26 23:46:05 -080044 }
Peng Qi92afd742017-06-19 18:01:07 +080045 return ret;
Zach Johnson917efb12017-02-26 23:46:05 -080046}
47
48void H4Protocol::OnPacketReady() {
49 switch (hci_packet_type_) {
50 case HCI_PACKET_TYPE_EVENT:
51 event_cb_(hci_packetizer_.GetPacket());
52 break;
53 case HCI_PACKET_TYPE_ACL_DATA:
54 acl_cb_(hci_packetizer_.GetPacket());
55 break;
56 case HCI_PACKET_TYPE_SCO_DATA:
57 sco_cb_(hci_packetizer_.GetPacket());
58 break;
Myles Watson3e272a72017-06-26 13:09:11 -070059 default:
60 LOG_ALWAYS_FATAL("%s: Unimplemented packet type %d", __func__,
61 static_cast<int>(hci_packet_type_));
Zach Johnson917efb12017-02-26 23:46:05 -080062 }
63 // Get ready for the next type byte.
64 hci_packet_type_ = HCI_PACKET_TYPE_UNKNOWN;
65}
66
67void H4Protocol::OnDataReady(int fd) {
68 if (hci_packet_type_ == HCI_PACKET_TYPE_UNKNOWN) {
69 uint8_t buffer[1] = {0};
Myles Watson3e272a72017-06-26 13:09:11 -070070 ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, 1));
71 if (bytes_read != 1) {
72 if (bytes_read == 0) {
73 LOG_ALWAYS_FATAL("%s: Unexpected EOF reading the packet type!",
74 __func__);
75 } else if (bytes_read < 0) {
76 LOG_ALWAYS_FATAL("%s: Read packet type error: %s", __func__,
77 strerror(errno));
78 } else {
79 LOG_ALWAYS_FATAL("%s: More bytes read than expected (%u)!", __func__,
80 static_cast<unsigned int>(bytes_read));
81 }
82 }
Zach Johnson917efb12017-02-26 23:46:05 -080083 hci_packet_type_ = static_cast<HciPacketType>(buffer[0]);
84 } else {
85 hci_packetizer_.OnDataReady(fd, hci_packet_type_);
86 }
87}
88
89} // namespace hci
90} // namespace bluetooth
91} // namespace hardware
92} // namespace android