• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "hci_packetizer.h"
18 
19 #define LOG_TAG "android.hardware.bluetooth.hci_packetizer"
20 #include <android-base/logging.h>
21 #include <utils/Log.h>
22 
23 #include <dlfcn.h>
24 #include <fcntl.h>
25 
26 namespace {
27 
28 const 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};
31 const 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 
HciGetPacketLengthForType(HciPacketType type,const uint8_t * preamble)35 size_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 
43 namespace android {
44 namespace hardware {
45 namespace bluetooth {
46 namespace hci {
47 
GetPacket() const48 const hidl_vec<uint8_t>& HciPacketizer::GetPacket() const { return packet_; }
49 
CbHciPacket(uint8_t * data,size_t len)50 void HciPacketizer::CbHciPacket(uint8_t *data, size_t len) {
51     packet_.setToExternal(data, len);
52     packet_ready_cb_();
53 }
54 
OnDataReady(int fd,HciPacketType packet_type)55 void HciPacketizer::OnDataReady(int fd, HciPacketType packet_type) {
56   switch (state_) {
57     case HCI_PREAMBLE: {
58       size_t bytes_read = TEMP_FAILURE_RETRY(
59           read(fd, preamble_ + bytes_read_,
60                preamble_size_for_type[packet_type] - bytes_read_));
61       CHECK(bytes_read > 0);
62       bytes_read_ += bytes_read;
63       if (bytes_read_ == preamble_size_for_type[packet_type]) {
64         size_t packet_length =
65             HciGetPacketLengthForType(packet_type, preamble_);
66         packet_.resize(preamble_size_for_type[packet_type] + packet_length);
67         memcpy(packet_.data(), preamble_, preamble_size_for_type[packet_type]);
68         bytes_remaining_ = packet_length;
69         state_ = HCI_PAYLOAD;
70         bytes_read_ = 0;
71       }
72       break;
73     }
74 
75     case HCI_PAYLOAD: {
76       size_t bytes_read = TEMP_FAILURE_RETRY(read(
77           fd,
78           packet_.data() + preamble_size_for_type[packet_type] + bytes_read_,
79           bytes_remaining_));
80       CHECK(bytes_read > 0);
81       bytes_remaining_ -= bytes_read;
82       bytes_read_ += bytes_read;
83       if (bytes_remaining_ == 0) {
84         packet_ready_cb_();
85         state_ = HCI_PREAMBLE;
86         bytes_read_ = 0;
87       }
88       break;
89     }
90   }
91 }
92 
93 }  // namespace hci
94 }  // namespace bluetooth
95 }  // namespace hardware
96 }  // namespace android
97