blob: 2da1254531e2bff9d279ed1bb641a74dbef37694 [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>
Myles Watson3e272a72017-06-26 13:09:11 -070024#include <utils/Log.h>
Myles Watson274a3812017-02-23 06:29:08 -080025
26namespace {
27
28const size_t preamble_size_for_type[] = {
29 0, HCI_COMMAND_PREAMBLE_SIZE, HCI_ACL_PREAMBLE_SIZE, HCI_SCO_PREAMBLE_SIZE,
30 HCI_EVENT_PREAMBLE_SIZE};
31const size_t packet_length_offset_for_type[] = {
32 0, HCI_LENGTH_OFFSET_CMD, HCI_LENGTH_OFFSET_ACL, HCI_LENGTH_OFFSET_SCO,
33 HCI_LENGTH_OFFSET_EVT};
34
35size_t HciGetPacketLengthForType(HciPacketType type, const uint8_t* preamble) {
36 size_t offset = packet_length_offset_for_type[type];
37 if (type != HCI_PACKET_TYPE_ACL_DATA) return preamble[offset];
38 return (((preamble[offset + 1]) << 8) | preamble[offset]);
39}
40
41} // namespace
42
43namespace android {
44namespace hardware {
45namespace bluetooth {
46namespace hci {
47
Myles Watson3e272a72017-06-26 13:09:11 -070048const hidl_vec<uint8_t>& HciPacketizer::GetPacket() const {
49 return packet_;
50}
Myles Watson274a3812017-02-23 06:29:08 -080051
Zach Johnson917efb12017-02-26 23:46:05 -080052void HciPacketizer::OnDataReady(int fd, HciPacketType packet_type) {
53 switch (state_) {
54 case HCI_PREAMBLE: {
Myles Watson3e272a72017-06-26 13:09:11 -070055 ssize_t bytes_read = TEMP_FAILURE_RETRY(
Zach Johnson917efb12017-02-26 23:46:05 -080056 read(fd, preamble_ + bytes_read_,
57 preamble_size_for_type[packet_type] - bytes_read_));
Myles Watson3e272a72017-06-26 13:09:11 -070058 if (bytes_read <= 0) {
59 LOG_ALWAYS_FATAL_IF((bytes_read == 0),
60 "%s: Unexpected EOF reading the header!", __func__);
61 LOG_ALWAYS_FATAL("%s: Read header error: %s", __func__,
62 strerror(errno));
63 }
Zach Johnson917efb12017-02-26 23:46:05 -080064 bytes_read_ += bytes_read;
65 if (bytes_read_ == preamble_size_for_type[packet_type]) {
Myles Watson274a3812017-02-23 06:29:08 -080066 size_t packet_length =
Zach Johnson917efb12017-02-26 23:46:05 -080067 HciGetPacketLengthForType(packet_type, preamble_);
68 packet_.resize(preamble_size_for_type[packet_type] + packet_length);
69 memcpy(packet_.data(), preamble_, preamble_size_for_type[packet_type]);
70 bytes_remaining_ = packet_length;
71 state_ = HCI_PAYLOAD;
72 bytes_read_ = 0;
Myles Watson274a3812017-02-23 06:29:08 -080073 }
74 break;
75 }
76
77 case HCI_PAYLOAD: {
Myles Watson3e272a72017-06-26 13:09:11 -070078 ssize_t bytes_read = TEMP_FAILURE_RETRY(read(
Zach Johnson917efb12017-02-26 23:46:05 -080079 fd,
80 packet_.data() + preamble_size_for_type[packet_type] + bytes_read_,
81 bytes_remaining_));
Myles Watson3e272a72017-06-26 13:09:11 -070082 if (bytes_read <= 0) {
83 LOG_ALWAYS_FATAL_IF((bytes_read == 0),
84 "%s: Unexpected EOF reading the payload!",
85 __func__);
86 LOG_ALWAYS_FATAL("%s: Read payload error: %s", __func__,
87 strerror(errno));
88 }
Zach Johnson917efb12017-02-26 23:46:05 -080089 bytes_remaining_ -= bytes_read;
90 bytes_read_ += bytes_read;
91 if (bytes_remaining_ == 0) {
92 packet_ready_cb_();
93 state_ = HCI_PREAMBLE;
94 bytes_read_ = 0;
Myles Watson274a3812017-02-23 06:29:08 -080095 }
96 break;
97 }
98 }
99}
100
101} // namespace hci
102} // namespace bluetooth
103} // namespace hardware
104} // namespace android