2 // Copyright 2015 The Android Open Source Project
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "vendor_libs/test_vendor_lib/include/command_packet.h"
20 #include "vendor_libs/test_vendor_lib/include/packet.h"
24 #include <unordered_map>
27 #include <base/macros.h>
29 namespace test_vendor_lib {
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.
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();
46 static void Initialize();
48 static void CleanUp();
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);
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(
62 std::function<void(const std::vector<std::uint8_t> args)> callback);
65 // There will only be a single global instance of this class.
66 HciHandler() = default;
68 // The destructor can only be indirectly accessed through the static
69 // CleanUp() method that destructs the global handler.
70 ~HciHandler() = default;
72 // Sets the command and data callbacks for when packets are received from the
74 void RegisterTransportCallbacks();
76 // Disallow any copies of the singleton to be made.
77 DISALLOW_COPY_AND_ASSIGN(HciHandler);
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)> >
86 } // namespace test_vendor_lib