blob: 73ff2eb3777a7e21d435b917441af5f6438d90a0 [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;
Myles Watson6a7d6222016-10-13 15:45:02 -070032using PacketReadCallback =
33 std::function<void(HciPacketType, const hidl_vec<uint8_t> &)>;
Andre Eisenbach89ba5282016-10-13 15:45:02 -070034
35class 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