OSDN Git Service

test_vendor_lib: Initial commit
[android-x86/system-bt.git] / vendor_libs / test_vendor_lib / include / hci_handler.h
1 //
2 // Copyright 2015 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 "vendor_libs/test_vendor_lib/include/command_packet.h"
20 #include "vendor_libs/test_vendor_lib/include/packet.h"
21
22 #include <cstdint>
23 #include <memory>
24 #include <unordered_map>
25 #include <vector>
26
27 #include <base/macros.h>
28
29 namespace test_vendor_lib {
30
31 // Dispatches packets to the appropriate controller handler. These handlers
32 // must be registered by controller objects in order for commands to be
33 // processed. Unregistered commands will perform no operations. Exposes two
34 // callbacks, HandleCommand() and HandleData(), to be registered with a listener
35 // object and called when commands and data are sent by the host.
36 class HciHandler {
37  public:
38   // Functions that operate on the global handler instance. Initialize()
39   // is called by the vendor library's Init() function to create the global
40   // handler and must be called before Get() and CleanUp().
41   // CleanUp() should be called when a call to TestVendorCleanUp() is made
42   // since the global handler should live throughout the entire time the test
43   // vendor library is in use.
44   static HciHandler* Get();
45
46   static void Initialize();
47
48   static void CleanUp();
49
50   // Callback to be fired when a command packet is received from the HCI. Takes
51   // ownership of the packet and dispatches work to the controller through the
52   // callback registered with the command's opcode. After the controller
53   // finishes processing the command and the callback returns, the command
54   // packet is destroyed.
55   void HandleCommand(std::unique_ptr<CommandPacket> command);
56
57   // Creates the mapping from the opcode to the method |callback|.
58   // |callback|, which is provided by the controller, will be fired when its
59   // command opcode is received from the HCI.
60   void RegisterControllerCallback(
61       std::uint16_t opcode,
62       std::function<void(const std::vector<std::uint8_t> args)> callback);
63
64  private:
65   // There will only be a single global instance of this class.
66   HciHandler() = default;
67
68   // The destructor can only be indirectly accessed through the static
69   // CleanUp() method that destructs the global handler.
70   ~HciHandler() = default;
71
72   // Sets the command and data callbacks for when packets are received from the
73   // HCI.
74   void RegisterTransportCallbacks();
75
76   // Disallow any copies of the singleton to be made.
77   DISALLOW_COPY_AND_ASSIGN(HciHandler);
78
79   // Controller callbacks to be executed in handlers and registered in
80   // RegisterControllerCallback().
81   std::unordered_map<std::uint16_t,
82                      std::function<void(const std::vector<std::uint8_t> args)> >
83       callbacks_;
84 };
85
86 }  // namespace test_vendor_lib