blob: 98357f573d84605cfdcc50a75ae2a2e2801e8996 [file] [log] [blame]
Andre Eisenbach89ba5282016-10-13 15:45:02 -07001//
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
25namespace android {
26namespace hardware {
27namespace bluetooth {
28namespace V1_0 {
29namespace implementation {
30
31using ::android::hardware::hidl_vec;
Andre Eisenbach9041d972017-01-17 18:23:12 -080032using InitializeCompleteCallback = std::function<void(bool success)>;
Myles Watson6a7d6222016-10-13 15:45:02 -070033using PacketReadCallback =
34 std::function<void(HciPacketType, const hidl_vec<uint8_t> &)>;
Andre Eisenbach89ba5282016-10-13 15:45:02 -070035
Andre Eisenbach9041d972017-01-17 18:23:12 -080036class FirmwareStartupTimer;
37
Andre Eisenbach89ba5282016-10-13 15:45:02 -070038class VendorInterface {
39 public:
Andre Eisenbach9041d972017-01-17 18:23:12 -080040 static bool Initialize(InitializeCompleteCallback initialize_complete_cb,
41 PacketReadCallback packet_read_cb);
Andre Eisenbach89ba5282016-10-13 15:45:02 -070042 static void Shutdown();
Andre Eisenbach9041d972017-01-17 18:23:12 -080043 static VendorInterface *get();
Andre Eisenbach89ba5282016-10-13 15:45:02 -070044
Myles Watsondf765ea2017-01-30 09:07:37 -080045 size_t Send(uint8_t type, const uint8_t *data, size_t length);
Andre Eisenbach89ba5282016-10-13 15:45:02 -070046
47 void OnFirmwareConfigured(uint8_t result);
48
Andre Eisenbach89ba5282016-10-13 15:45:02 -070049 private:
Andre Eisenbach89ba5282016-10-13 15:45:02 -070050 virtual ~VendorInterface() = default;
51
Myles Watsona7d33b32017-01-24 09:09:58 -080052 bool Open(InitializeCompleteCallback initialize_complete_cb,
53 PacketReadCallback packet_read_cb);
Andre Eisenbach89ba5282016-10-13 15:45:02 -070054 void Close();
55
Myles Watsonbeb13b42017-01-26 10:47:27 -080056 void OnTimeout();
57
Andre Eisenbach89ba5282016-10-13 15:45:02 -070058 void OnDataReady(int fd);
59
Andre Eisenbach89ba5282016-10-13 15:45:02 -070060 void *lib_handle_;
61 bt_vendor_interface_t *lib_interface_;
Myles Watsonbe6176d2017-02-21 13:27:01 -080062 async::AsyncFdWatcher fd_watcher_;
Andre Eisenbach89ba5282016-10-13 15:45:02 -070063 int uart_fd_;
64 PacketReadCallback packet_read_cb_;
Andre Eisenbach9041d972017-01-17 18:23:12 -080065 InitializeCompleteCallback initialize_complete_cb_;
Andre Eisenbach89ba5282016-10-13 15:45:02 -070066
Andre Eisenbach9041d972017-01-17 18:23:12 -080067 enum HciParserState { HCI_IDLE, HCI_TYPE_READY, HCI_PAYLOAD };
Andre Eisenbach89ba5282016-10-13 15:45:02 -070068 HciParserState hci_parser_state_{HCI_IDLE};
69 HciPacketType hci_packet_type_{HCI_PACKET_TYPE_UNKNOWN};
Myles Watson71390182017-01-24 13:34:59 -080070 uint8_t hci_packet_preamble_[HCI_PREAMBLE_SIZE_MAX];
Andre Eisenbach89ba5282016-10-13 15:45:02 -070071 hidl_vec<uint8_t> hci_packet_;
72 size_t hci_packet_bytes_remaining_;
73 size_t hci_packet_bytes_read_;
Andre Eisenbach9041d972017-01-17 18:23:12 -080074
75 FirmwareStartupTimer *firmware_startup_timer_;
Andre Eisenbach89ba5282016-10-13 15:45:02 -070076};
77
Andre Eisenbach9041d972017-01-17 18:23:12 -080078} // namespace implementation
79} // namespace V1_0
80} // namespace bluetooth
81} // namespace hardware
82} // namespace android