Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright 2016 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 "bt_vendor_lib.h" |
| 23 | #include "hci_internals.h" |
| 24 | |
| 25 | namespace android { |
| 26 | namespace hardware { |
| 27 | namespace bluetooth { |
| 28 | namespace V1_0 { |
| 29 | namespace implementation { |
| 30 | |
| 31 | using ::android::hardware::hidl_vec; |
Myles Watson | 6a7d622 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 32 | using PacketReadCallback = |
| 33 | std::function<void(HciPacketType, const hidl_vec<uint8_t> &)>; |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 34 | |
| 35 | class VendorInterface { |
| 36 | public: |
| 37 | static bool Initialize(PacketReadCallback packet_read_cb); |
| 38 | static void Shutdown(); |
| 39 | static VendorInterface* get(); |
| 40 | |
| 41 | size_t Send(const uint8_t *data, size_t length); |
| 42 | |
| 43 | void OnFirmwareConfigured(uint8_t result); |
| 44 | |
| 45 | // Actually send the data. |
| 46 | size_t SendPrivate(const uint8_t *data, size_t length); |
| 47 | |
| 48 | private: |
| 49 | VendorInterface() { queued_data_.resize(0); } |
| 50 | virtual ~VendorInterface() = default; |
| 51 | |
| 52 | bool Open(PacketReadCallback packet_read_cb); |
| 53 | void Close(); |
| 54 | |
| 55 | void OnDataReady(int fd); |
| 56 | |
| 57 | // Queue data from Send() until the interface is ready. |
| 58 | hidl_vec<uint8_t> queued_data_; |
| 59 | |
| 60 | void *lib_handle_; |
| 61 | bt_vendor_interface_t *lib_interface_; |
| 62 | AsyncFdWatcher fd_watcher_; |
| 63 | int uart_fd_; |
| 64 | PacketReadCallback packet_read_cb_; |
| 65 | bool firmware_configured_; |
| 66 | |
| 67 | enum HciParserState { |
| 68 | HCI_IDLE, |
| 69 | HCI_TYPE_READY, |
| 70 | HCI_PAYLOAD |
| 71 | }; |
| 72 | HciParserState hci_parser_state_{HCI_IDLE}; |
| 73 | HciPacketType hci_packet_type_{HCI_PACKET_TYPE_UNKNOWN}; |
| 74 | hidl_vec<uint8_t> hci_packet_; |
| 75 | size_t hci_packet_bytes_remaining_; |
| 76 | size_t hci_packet_bytes_read_; |
| 77 | }; |
| 78 | |
| 79 | } // namespace implementation |
| 80 | } // namespace V1_0 |
| 81 | } // namespace bluetooth |
| 82 | } // namespace hardware |
| 83 | } // namespace android |