Home | History | Annotate | Download | only in default
      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 
     21 #include <dlfcn.h>
     22 #include <errno.h>
     23 #include <fcntl.h>
     24 #include <unistd.h>
     25 #include <utils/Log.h>
     26 
     27 namespace {
     28 
     29 const 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};
     32 const 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 
     36 size_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 
     44 namespace android {
     45 namespace hardware {
     46 namespace bluetooth {
     47 namespace hci {
     48 
     49 const hidl_vec<uint8_t>& HciPacketizer::GetPacket() const {
     50   return packet_;
     51 }
     52 
     53 void HciPacketizer::OnDataReady(int fd, HciPacketType packet_type) {
     54   switch (state_) {
     55     case HCI_PREAMBLE: {
     56       ssize_t bytes_read = TEMP_FAILURE_RETRY(
     57           read(fd, preamble_ + bytes_read_,
     58                preamble_size_for_type[packet_type] - bytes_read_));
     59       if (bytes_read <= 0) {
     60         LOG_ALWAYS_FATAL_IF((bytes_read == 0),
     61                             "%s: Unexpected EOF reading the header!", __func__);
     62         LOG_ALWAYS_FATAL("%s: Read header error: %s", __func__,
     63                          strerror(errno));
     64       }
     65       bytes_read_ += bytes_read;
     66       if (bytes_read_ == preamble_size_for_type[packet_type]) {
     67         size_t packet_length =
     68             HciGetPacketLengthForType(packet_type, preamble_);
     69         packet_.resize(preamble_size_for_type[packet_type] + packet_length);
     70         memcpy(packet_.data(), preamble_, preamble_size_for_type[packet_type]);
     71         bytes_remaining_ = packet_length;
     72         state_ = HCI_PAYLOAD;
     73         bytes_read_ = 0;
     74       }
     75       break;
     76     }
     77 
     78     case HCI_PAYLOAD: {
     79       ssize_t bytes_read = TEMP_FAILURE_RETRY(read(
     80           fd,
     81           packet_.data() + preamble_size_for_type[packet_type] + bytes_read_,
     82           bytes_remaining_));
     83       if (bytes_read <= 0) {
     84         LOG_ALWAYS_FATAL_IF((bytes_read == 0),
     85                             "%s: Unexpected EOF reading the payload!",
     86                             __func__);
     87         LOG_ALWAYS_FATAL("%s: Read payload error: %s", __func__,
     88                          strerror(errno));
     89       }
     90       bytes_remaining_ -= bytes_read;
     91       bytes_read_ += bytes_read;
     92       if (bytes_remaining_ == 0) {
     93         packet_ready_cb_();
     94         state_ = HCI_PREAMBLE;
     95         bytes_read_ = 0;
     96       }
     97       break;
     98     }
     99   }
    100 }
    101 
    102 }  // namespace hci
    103 }  // namespace bluetooth
    104 }  // namespace hardware
    105 }  // namespace android
    106