1 /*
2  * Copyright 2019 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 <vector>
20 
21 #include "module.h"
22 
23 namespace bluetooth {
24 namespace hal {
25 
26 using HciPacket = std::vector<uint8_t>;
27 
28 enum class Status : int32_t { SUCCESS, TRANSPORT_ERROR, INITIALIZATION_ERROR, UNKNOWN };
29 
30 // Mirrors hardware/interfaces/bluetooth/1.0/IBluetoothHciCallbacks.hal in Android, but moved initializationComplete
31 // callback to BluetoothInitializationCompleteCallback
32 
33 // The interface from the Bluetooth Controller to the stack
34 class HciHalCallbacks {
35  public:
36   virtual ~HciHalCallbacks() = default;
37 
38   // This function is invoked when an HCI event is received from the
39   // Bluetooth controller to be forwarded to the Bluetooth stack
40   // @param event is the HCI event to be sent to the Bluetooth stack
41   virtual void hciEventReceived(HciPacket event) = 0;
42 
43   // Send an ACL data packet from the controller to the host
44   // @param data the ACL HCI packet to be passed to the host stack
45   virtual void aclDataReceived(HciPacket data) = 0;
46 
47   // Send a SCO data packet from the controller to the host
48   // @param data the SCO HCI packet to be passed to the host stack
49   virtual void scoDataReceived(HciPacket data) = 0;
50 
51   // Send an ISO data packet from the controller to the host
52   // @param data the ISO HCI packet to be passed to the host stack
53   virtual void isoDataReceived(HciPacket data) = 0;
54 };
55 
56 // Mirrors hardware/interfaces/bluetooth/1.0/IBluetoothHci.hal in Android
57 // The Host Controller Interface (HCI) is the layer defined by the Bluetooth
58 // specification between the software that runs on the host and the Bluetooth
59 // controller chip. This boundary is the natural choice for a Hardware
60 // Abstraction Layer (HAL). Dealing only in HCI packets and events simplifies
61 // the stack and abstracts away power management, initialization, and other
62 // implementation-specific details related to the hardware.
63 // LINT.IfChange
64 class HciHal : public ::bluetooth::Module {
65  public:
66   static const ModuleFactory Factory;
67 
68   virtual ~HciHal() = default;
69 
70   // Register the callback for incoming packets. All incoming packets are dropped before
71   // this callback is registered. Callback can only be registered once.
72   //
73   // @param callback implements BluetoothHciHalCallbacks which will
74   //    receive callbacks when incoming HCI packets are received
75   //    from the controller to be sent to the host.
76   virtual void registerIncomingPacketCallback(HciHalCallbacks* callback) = 0;
77 
78   // Unregister the callback for incoming packets. Drop all further incoming packets.
79   virtual void unregisterIncomingPacketCallback() = 0;
80 
81   // Send an HCI command (as specified in the Bluetooth Specification
82   // V4.2, Vol 2, Part 5, Section 5.4.1) to the Bluetooth controller.
83   // Commands must be executed in order.
84   virtual void sendHciCommand(HciPacket command) = 0;
85 
86   // Send an HCI ACL data packet (as specified in the Bluetooth Specification
87   // V4.2, Vol 2, Part 5, Section 5.4.2) to the Bluetooth controller.
88   // Packets must be processed in order.
89   virtual void sendAclData(HciPacket data) = 0;
90 
91   // Send an SCO data packet (as specified in the Bluetooth Specification
92   // V4.2, Vol 2, Part 5, Section 5.4.3) to the Bluetooth controller.
93   // Packets must be processed in order.
94   virtual void sendScoData(HciPacket data) = 0;
95 
96   // Send an HCI ISO data packet (as specified in the Bluetooth Specification
97   // V5.2, Vol 4, Part E, Section 5.4.5) to the Bluetooth controller.
98   // Packets must be processed in order.
99   virtual void sendIsoData(HciPacket data) = 0;
100 };
101 // LINT.ThenChange(fuzz/fuzz_hci_hal.h)
102 
103 }  // namespace hal
104 }  // namespace bluetooth
105