• 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 "h4_protocol.h"
18 
19 #define LOG_TAG "android.hardware.bluetooth-hci-h4"
20 
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <log/log.h>
24 #include <sys/uio.h>
25 #include <unistd.h>
26 
27 namespace android {
28 namespace hardware {
29 namespace bluetooth {
30 namespace hci {
31 
Send(uint8_t type,const uint8_t * data,size_t length)32 size_t H4Protocol::Send(uint8_t type, const uint8_t* data, size_t length) {
33   struct iovec iov[] = {{&type, sizeof(type)},
34                         {const_cast<uint8_t*>(data), length}};
35   ssize_t ret = 0;
36   do {
37     ret =
38         TEMP_FAILURE_RETRY(writev(uart_fd_, iov, sizeof(iov) / sizeof(iov[0])));
39   } while (-1 == ret && EAGAIN == errno);
40 
41   if (ret == -1) {
42     ALOGE("%s error writing to UART (%s)", __func__, strerror(errno));
43   } else if (ret < static_cast<ssize_t>(length + 1)) {
44     ALOGE("%s: %d / %d bytes written - something went wrong...", __func__,
45           static_cast<int>(ret), static_cast<int>(length + 1));
46   }
47   return ret;
48 }
49 
OnPacketReady()50 void H4Protocol::OnPacketReady() {
51   switch (hci_packet_type_) {
52     case HCI_PACKET_TYPE_EVENT:
53       event_cb_(hci_packetizer_.GetPacket());
54       break;
55     case HCI_PACKET_TYPE_ACL_DATA:
56       acl_cb_(hci_packetizer_.GetPacket());
57       break;
58     case HCI_PACKET_TYPE_SCO_DATA:
59       sco_cb_(hci_packetizer_.GetPacket());
60       break;
61     default:
62       LOG_ALWAYS_FATAL("%s: Unimplemented packet type %d", __func__,
63                        static_cast<int>(hci_packet_type_));
64   }
65   // Get ready for the next type byte.
66   hci_packet_type_ = HCI_PACKET_TYPE_UNKNOWN;
67 }
68 
OnDataReady(int fd)69 void H4Protocol::OnDataReady(int fd) {
70   if (hci_packet_type_ == HCI_PACKET_TYPE_UNKNOWN) {
71     uint8_t buffer[1] = {0};
72     ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, 1));
73     if (bytes_read != 1) {
74       if (bytes_read == 0) {
75         // This is only expected if the UART got closed when shutting down.
76         ALOGE("%s: Unexpected EOF reading the packet type!", __func__);
77         sleep(5);  // Expect to be shut down within 5 seconds.
78         return;
79       } else if (bytes_read < 0) {
80         LOG_ALWAYS_FATAL("%s: Read packet type error: %s", __func__,
81                          strerror(errno));
82       } else {
83         LOG_ALWAYS_FATAL("%s: More bytes read than expected (%u)!", __func__,
84                          static_cast<unsigned int>(bytes_read));
85       }
86     }
87     hci_packet_type_ = static_cast<HciPacketType>(buffer[0]);
88     if (hci_packet_type_ != HCI_PACKET_TYPE_ACL_DATA &&
89         hci_packet_type_ != HCI_PACKET_TYPE_SCO_DATA &&
90         hci_packet_type_ != HCI_PACKET_TYPE_EVENT) {
91       LOG_ALWAYS_FATAL("%s: Unimplemented packet type %d", __func__,
92                        static_cast<int>(hci_packet_type_));
93     }
94   } else {
95     hci_packetizer_.OnDataReady(fd, hci_packet_type_);
96   }
97 }
98 
99 }  // namespace hci
100 }  // namespace bluetooth
101 }  // namespace hardware
102 }  // namespace android
103