Home | History | Annotate | Download | only in interface
      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 "h4_protocol.h"
     18 
     19 #define LOG_TAG "android.hardware.bluetooth-hci-h4"
     20 #include <android-base/logging.h>
     21 #include <assert.h>
     22 #include <fcntl.h>
     23 #include <sys/uio.h>
     24 #include <utils/Log.h>
     25 
     26 namespace android {
     27 namespace hardware {
     28 namespace bluetooth {
     29 namespace hci {
     30 
     31 size_t H4Protocol::Send(uint8_t type, const uint8_t* data, size_t length) {
     32     /* For HCI communication over USB dongle, multiple write results in
     33      * response timeout as driver expect type + data at once to process
     34      * the command, so using "writev"(for atomicity) here.
     35      */
     36     struct iovec iov[2];
     37     ssize_t ret = 0;
     38     iov[0].iov_base = &type;
     39     iov[0].iov_len = sizeof(type);
     40     iov[1].iov_base = (void *)data;
     41     iov[1].iov_len = length;
     42     while (1) {
     43         ret = TEMP_FAILURE_RETRY(writev(uart_fd_, iov, 2));
     44         if (ret == -1) {
     45             if (errno == EAGAIN) {
     46                 ALOGE("%s error writing to UART (%s)", __func__, strerror(errno));
     47                 continue;
     48             }
     49         } else if (ret == 0) {
     50             // Nothing written :(
     51             ALOGE("%s zero bytes written - something went wrong...", __func__);
     52             break;
     53         }
     54         break;
     55     }
     56     return ret;
     57 }
     58 
     59 void H4Protocol::OnPacketReady() {
     60   switch (hci_packet_type_) {
     61     case HCI_PACKET_TYPE_EVENT:
     62       event_cb_(hci_packetizer_.GetPacket());
     63       break;
     64     case HCI_PACKET_TYPE_ACL_DATA:
     65       acl_cb_(hci_packetizer_.GetPacket());
     66       break;
     67     case HCI_PACKET_TYPE_SCO_DATA:
     68       sco_cb_(hci_packetizer_.GetPacket());
     69       break;
     70     default: {
     71       bool bad_packet_type = true;
     72       CHECK(!bad_packet_type);
     73     }
     74   }
     75   // Get ready for the next type byte.
     76   hci_packet_type_ = HCI_PACKET_TYPE_UNKNOWN;
     77 }
     78 
     79 void H4Protocol::OnDataReady(int fd) {
     80     if (hci_packet_type_ == HCI_PACKET_TYPE_UNKNOWN) {
     81         /**
     82          * read full buffer. ACL max length is 2 bytes, and SCO max length is 2
     83          * byte. so taking 64K as buffer length.
     84          * Question : Why to read in single chunk rather than multiple reads,
     85          * which can give parameter length arriving in response ?
     86          * Answer: The multiple reads does not work with BT USB dongle. At least
     87          * with Bluetooth 2.0 supported USB dongle. After first read, either
     88          * firmware/kernel (do not know who is responsible - inputs ??) driver
     89          * discard the whole message and successive read results in forever
     90          * blocking loop. - Is there any other way to make it work with multiple
     91          * reads, do not know yet (it can eliminate need of this function) ?
     92          * Reading in single shot gives expected response.
     93          */
     94         const size_t max_plen = 64*1024;
     95         hidl_vec<uint8_t> tpkt;
     96         tpkt.resize(max_plen);
     97         size_t bytes_read = TEMP_FAILURE_RETRY(read(fd, tpkt.data(), max_plen));
     98         hci_packet_type_ = static_cast<HciPacketType>(tpkt.data()[0]);
     99         hci_packetizer_.CbHciPacket(tpkt.data()+1, bytes_read-1);
    100     }
    101 }
    102 
    103 }  // namespace hci
    104 }  // namespace bluetooth
    105 }  // namespace hardware
    106 }  // namespace android
    107