blob: 133c51bff338e3c30b18c2a2f7a36cb693c1d6ec [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;
32using PacketReadCallback = std::function<void(HciPacketType, const hidl_vec<uint8_t>&)>;
33
34class VendorInterface {
35 public:
36 static bool Initialize(PacketReadCallback packet_read_cb);
37 static void Shutdown();
38 static VendorInterface* get();
39
40 size_t Send(const uint8_t *data, size_t length);
41
42 void OnFirmwareConfigured(uint8_t result);
43
44 // Actually send the data.
45 size_t SendPrivate(const uint8_t *data, size_t length);
46
47 private:
48 VendorInterface() { queued_data_.resize(0); }
49 virtual ~VendorInterface() = default;
50
51 bool Open(PacketReadCallback packet_read_cb);
52 void Close();
53
54 void OnDataReady(int fd);
55
56 // Queue data from Send() until the interface is ready.
57 hidl_vec<uint8_t> queued_data_;
58
59 void *lib_handle_;
60 bt_vendor_interface_t *lib_interface_;
61 AsyncFdWatcher fd_watcher_;
62 int uart_fd_;
63 PacketReadCallback packet_read_cb_;
64 bool firmware_configured_;
65
66 enum HciParserState {
67 HCI_IDLE,
68 HCI_TYPE_READY,
69 HCI_PAYLOAD
70 };
71 HciParserState hci_parser_state_{HCI_IDLE};
72 HciPacketType hci_packet_type_{HCI_PACKET_TYPE_UNKNOWN};
73 hidl_vec<uint8_t> hci_packet_;
74 size_t hci_packet_bytes_remaining_;
75 size_t hci_packet_bytes_read_;
76};
77
78} // namespace implementation
79} // namespace V1_0
80} // namespace bluetooth
81} // namespace hardware
82} // namespace android