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; |
Andre Eisenbach | 9041d97 | 2017-01-17 18:23:12 -0800 | [diff] [blame] | 32 | using InitializeCompleteCallback = std::function<void(bool success)>; |
Myles Watson | 6a7d622 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 33 | using PacketReadCallback = |
| 34 | std::function<void(HciPacketType, const hidl_vec<uint8_t> &)>; |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 35 | |
Andre Eisenbach | 9041d97 | 2017-01-17 18:23:12 -0800 | [diff] [blame] | 36 | class FirmwareStartupTimer; |
| 37 | |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 38 | class VendorInterface { |
| 39 | public: |
Andre Eisenbach | 9041d97 | 2017-01-17 18:23:12 -0800 | [diff] [blame] | 40 | static bool Initialize(InitializeCompleteCallback initialize_complete_cb, |
| 41 | PacketReadCallback packet_read_cb); |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 42 | static void Shutdown(); |
Andre Eisenbach | 9041d97 | 2017-01-17 18:23:12 -0800 | [diff] [blame] | 43 | static VendorInterface *get(); |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 44 | |
Myles Watson | df765ea | 2017-01-30 09:07:37 -0800 | [diff] [blame] | 45 | size_t Send(uint8_t type, const uint8_t *data, size_t length); |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 46 | |
| 47 | void OnFirmwareConfigured(uint8_t result); |
| 48 | |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 49 | private: |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 50 | virtual ~VendorInterface() = default; |
| 51 | |
Myles Watson | a7d33b3 | 2017-01-24 09:09:58 -0800 | [diff] [blame] | 52 | bool Open(InitializeCompleteCallback initialize_complete_cb, |
| 53 | PacketReadCallback packet_read_cb); |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 54 | void Close(); |
| 55 | |
Myles Watson | beb13b4 | 2017-01-26 10:47:27 -0800 | [diff] [blame] | 56 | void OnTimeout(); |
| 57 | |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 58 | void OnDataReady(int fd); |
| 59 | |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 60 | void *lib_handle_; |
| 61 | bt_vendor_interface_t *lib_interface_; |
| 62 | AsyncFdWatcher fd_watcher_; |
| 63 | int uart_fd_; |
| 64 | PacketReadCallback packet_read_cb_; |
Andre Eisenbach | 9041d97 | 2017-01-17 18:23:12 -0800 | [diff] [blame] | 65 | InitializeCompleteCallback initialize_complete_cb_; |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 66 | |
Andre Eisenbach | 9041d97 | 2017-01-17 18:23:12 -0800 | [diff] [blame] | 67 | enum HciParserState { HCI_IDLE, HCI_TYPE_READY, HCI_PAYLOAD }; |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 68 | HciParserState hci_parser_state_{HCI_IDLE}; |
| 69 | HciPacketType hci_packet_type_{HCI_PACKET_TYPE_UNKNOWN}; |
Myles Watson | 7139018 | 2017-01-24 13:34:59 -0800 | [diff] [blame] | 70 | uint8_t hci_packet_preamble_[HCI_PREAMBLE_SIZE_MAX]; |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 71 | hidl_vec<uint8_t> hci_packet_; |
| 72 | size_t hci_packet_bytes_remaining_; |
| 73 | size_t hci_packet_bytes_read_; |
Andre Eisenbach | 9041d97 | 2017-01-17 18:23:12 -0800 | [diff] [blame] | 74 | |
| 75 | FirmwareStartupTimer *firmware_startup_timer_; |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 76 | }; |
| 77 | |
Andre Eisenbach | 9041d97 | 2017-01-17 18:23:12 -0800 | [diff] [blame] | 78 | } // namespace implementation |
| 79 | } // namespace V1_0 |
| 80 | } // namespace bluetooth |
| 81 | } // namespace hardware |
| 82 | } // namespace android |