blob: 7cb3a113f13096c33a9cbd46795f8e31ead92d4f [file] [log] [blame]
Myles Watson274a3812017-02-23 06:29:08 -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 "hci_packetizer.h"
18
19#define LOG_TAG "android.hardware.bluetooth.hci_packetizer"
Myles Watson274a3812017-02-23 06:29:08 -080020
21#include <dlfcn.h>
Myles Watson3e272a72017-06-26 13:09:11 -070022#include <errno.h>
Myles Watson274a3812017-02-23 06:29:08 -080023#include <fcntl.h>
Jiyong Parkcb198f72017-07-10 13:27:05 +090024#include <unistd.h>
Myles Watson3e272a72017-06-26 13:09:11 -070025#include <utils/Log.h>
Myles Watson274a3812017-02-23 06:29:08 -080026
27namespace {
28
29const size_t preamble_size_for_type[] = {
30 0, HCI_COMMAND_PREAMBLE_SIZE, HCI_ACL_PREAMBLE_SIZE, HCI_SCO_PREAMBLE_SIZE,
31 HCI_EVENT_PREAMBLE_SIZE};
32const size_t packet_length_offset_for_type[] = {
33 0, HCI_LENGTH_OFFSET_CMD, HCI_LENGTH_OFFSET_ACL, HCI_LENGTH_OFFSET_SCO,
34 HCI_LENGTH_OFFSET_EVT};
35
36size_t HciGetPacketLengthForType(HciPacketType type, const uint8_t* preamble) {
37 size_t offset = packet_length_offset_for_type[type];
38 if (type != HCI_PACKET_TYPE_ACL_DATA) return preamble[offset];
39 return (((preamble[offset + 1]) << 8) | preamble[offset]);
40}
41
42} // namespace
43
44namespace android {
45namespace hardware {
46namespace bluetooth {
47namespace hci {
48
Jack Hecaeab052018-10-23 18:13:51 -070049const hidl_vec<uint8_t>& HciPacketizer::GetPacket() const { return packet_; }
Myles Watson274a3812017-02-23 06:29:08 -080050
Zach Johnson917efb12017-02-26 23:46:05 -080051void HciPacketizer::OnDataReady(int fd, HciPacketType packet_type) {
52 switch (state_) {
53 case HCI_PREAMBLE: {
Myles Watson3e272a72017-06-26 13:09:11 -070054 ssize_t bytes_read = TEMP_FAILURE_RETRY(
Zach Johnson917efb12017-02-26 23:46:05 -080055 read(fd, preamble_ + bytes_read_,
56 preamble_size_for_type[packet_type] - bytes_read_));
Myles Watson0d63f8a2018-01-08 16:49:13 -080057 if (bytes_read == 0) {
58 // This is only expected if the UART got closed when shutting down.
59 ALOGE("%s: Unexpected EOF reading the header!", __func__);
60 sleep(5); // Expect to be shut down within 5 seconds.
61 return;
62 }
63 if (bytes_read < 0) {
Myles Watson3e272a72017-06-26 13:09:11 -070064 LOG_ALWAYS_FATAL("%s: Read header error: %s", __func__,
65 strerror(errno));
66 }
Zach Johnson917efb12017-02-26 23:46:05 -080067 bytes_read_ += bytes_read;
68 if (bytes_read_ == preamble_size_for_type[packet_type]) {
Myles Watson274a3812017-02-23 06:29:08 -080069 size_t packet_length =
Zach Johnson917efb12017-02-26 23:46:05 -080070 HciGetPacketLengthForType(packet_type, preamble_);
71 packet_.resize(preamble_size_for_type[packet_type] + packet_length);
72 memcpy(packet_.data(), preamble_, preamble_size_for_type[packet_type]);
73 bytes_remaining_ = packet_length;
74 state_ = HCI_PAYLOAD;
75 bytes_read_ = 0;
Myles Watson274a3812017-02-23 06:29:08 -080076 }
77 break;
78 }
79
80 case HCI_PAYLOAD: {
Myles Watson3e272a72017-06-26 13:09:11 -070081 ssize_t bytes_read = TEMP_FAILURE_RETRY(read(
Zach Johnson917efb12017-02-26 23:46:05 -080082 fd,
83 packet_.data() + preamble_size_for_type[packet_type] + bytes_read_,
84 bytes_remaining_));
Myles Watson0d63f8a2018-01-08 16:49:13 -080085 if (bytes_read == 0) {
86 // This is only expected if the UART got closed when shutting down.
87 ALOGE("%s: Unexpected EOF reading the payload!", __func__);
88 sleep(5); // Expect to be shut down within 5 seconds.
89 return;
90 }
91 if (bytes_read < 0) {
Myles Watson3e272a72017-06-26 13:09:11 -070092 LOG_ALWAYS_FATAL("%s: Read payload error: %s", __func__,
93 strerror(errno));
94 }
Zach Johnson917efb12017-02-26 23:46:05 -080095 bytes_remaining_ -= bytes_read;
96 bytes_read_ += bytes_read;
97 if (bytes_remaining_ == 0) {
98 packet_ready_cb_();
99 state_ = HCI_PREAMBLE;
100 bytes_read_ = 0;
Myles Watson274a3812017-02-23 06:29:08 -0800101 }
102 break;
103 }
104 }
105}
106
107} // namespace hci
108} // namespace bluetooth
109} // namespace hardware
110} // namespace android