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 #pragma once 18 19 #include <hidl/HidlSupport.h> 20 21 #include "async_fd_watcher.h" 22 #include "hci_internals.h" 23 #include "hci_packetizer.h" 24 25 namespace android { 26 namespace hardware { 27 namespace bluetooth { 28 namespace hci { 29 30 using ::android::hardware::hidl_vec; 31 using PacketReadCallback = std::function<void(const hidl_vec<uint8_t>&)>; 32 using OnDisconnectCallback = std::function<void()>; 33 34 class H4Protocol { 35 public: 36 H4Protocol(int fd, PacketReadCallback event_cb, PacketReadCallback acl_cb, 37 PacketReadCallback sco_cb, PacketReadCallback iso_cb, 38 OnDisconnectCallback disconnect_cb); 39 ~H4Protocol()40 virtual ~H4Protocol() {} 41 42 size_t Send(uint8_t type, const uint8_t* data, size_t length); 43 44 void OnDataReady(int fd); 45 46 private: 47 int uart_fd_; 48 bool disconnected_{false}; 49 50 size_t on_packet_ready(const hidl_vec<uint8_t>& packet); 51 void send_data_to_packetizer(uint8_t* buffer, size_t length); 52 53 PacketReadCallback event_cb_; 54 PacketReadCallback acl_cb_; 55 PacketReadCallback sco_cb_; 56 PacketReadCallback iso_cb_; 57 OnDisconnectCallback disconnect_cb_; 58 59 HciPacketType hci_packet_type_{HCI_PACKET_TYPE_UNKNOWN}; 60 HciPacketizer hci_packetizer_; 61 62 /** 63 * Question : Why read in single chunk rather than multiple reads? 64 * Answer: Using multiple reads does not work with some BT USB dongles. 65 * Reading in single shot gives expected response. 66 * ACL max length is 2 bytes, so using 64K as the buffer length. 67 */ 68 static constexpr size_t kMaxPacketLength = 64 * 1024; 69 }; 70 71 } // namespace hci 72 } // namespace bluetooth 73 } // namespace hardware 74 } // namespace android 75