blob: 79611cd32878bce2b49db408af3e36a551c3c45e [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
56 void OnDataReady(int fd);
57
Andre Eisenbach89ba5282016-10-13 15:45:02 -070058 void *lib_handle_;
59 bt_vendor_interface_t *lib_interface_;
60 AsyncFdWatcher fd_watcher_;
61 int uart_fd_;
62 PacketReadCallback packet_read_cb_;
Andre Eisenbach9041d972017-01-17 18:23:12 -080063 InitializeCompleteCallback initialize_complete_cb_;
Andre Eisenbach89ba5282016-10-13 15:45:02 -070064
Andre Eisenbach9041d972017-01-17 18:23:12 -080065 enum HciParserState { HCI_IDLE, HCI_TYPE_READY, HCI_PAYLOAD };
Andre Eisenbach89ba5282016-10-13 15:45:02 -070066 HciParserState hci_parser_state_{HCI_IDLE};
67 HciPacketType hci_packet_type_{HCI_PACKET_TYPE_UNKNOWN};
Myles Watson71390182017-01-24 13:34:59 -080068 uint8_t hci_packet_preamble_[HCI_PREAMBLE_SIZE_MAX];
Andre Eisenbach89ba5282016-10-13 15:45:02 -070069 hidl_vec<uint8_t> hci_packet_;
70 size_t hci_packet_bytes_remaining_;
71 size_t hci_packet_bytes_read_;
Andre Eisenbach9041d972017-01-17 18:23:12 -080072
73 FirmwareStartupTimer *firmware_startup_timer_;
Andre Eisenbach89ba5282016-10-13 15:45:02 -070074};
75
Andre Eisenbach9041d972017-01-17 18:23:12 -080076} // namespace implementation
77} // namespace V1_0
78} // namespace bluetooth
79} // namespace hardware
80} // namespace android