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.
17 #define LOG_TAG "bt_vendor"
19 #include "hci/include/bt_vendor_lib.h"
20 #include "vendor_libs/test_vendor_lib/include/bredr_controller.h"
21 #include "vendor_libs/test_vendor_lib/include/hci_handler.h"
22 #include "vendor_libs/test_vendor_lib/include/hci_transport.h"
27 #include "osi/include/log.h"
34 bt_vendor_callbacks_t* vendor_callbacks;
36 // Wrapper for kicking off HciTransport listening on its own thread.
37 void* ListenEntryPoint(void* context) {
38 while ((static_cast<test_vendor_lib::HciTransport*>(context))->Listen());
39 LOG_INFO(LOG_TAG, "HciTransport stopped listening.");
44 namespace test_vendor_lib {
46 // Initializes event handler for test device. |p_cb| are the callbacks to be
47 // used by event handler. |local_bdaddr| points to the address of the Bluetooth
48 // device. Returns 0 on success, -1 on error.
49 static int TestVendorInitialize(const bt_vendor_callbacks_t* p_cb,
50 unsigned char* /* local_bdaddr */) {
51 LOG_INFO(LOG_TAG, "Initializing test controller.");
53 // TODO(dennischeng): use CHECK and DCHECK when libbase is imported.
55 vendor_callbacks = const_cast<bt_vendor_callbacks_t*>(p_cb);
57 // Initialize global objects. The order of initialization does not matter,
58 // however all global objects should be initialized before any other work is
59 // done by the vendor library.
60 test_vendor_lib::HciTransport::Initialize();
61 test_vendor_lib::HciHandler::Initialize();
62 test_vendor_lib::BREDRController::Initialize();
64 // Create the connection to be used for communication between the HCI and
65 // the HciTransport object.
66 HciTransport* transporter = HciTransport::Get();
67 if (!transporter->Connect()) {
68 LOG_ERROR(LOG_TAG, "Error connecting HciTransport object to HCI.");
72 // Start HciTransport listening on its own thread.
73 pthread_t transporter_thread;
74 pthread_create(&transporter_thread, NULL, &ListenEntryPoint, transporter);
78 // Vendor specific operations. |opcode| is the opcode for Bluedroid's vendor op
79 // definitions. |param| points to operation specific arguments. Return value is
80 // dependent on the operation invoked, or -1 on error.
81 static int TestVendorOp(bt_vendor_opcode_t opcode, void* param) {
82 LOG_INFO(LOG_TAG, "Opcode received in vendor library: %d", opcode);
84 HciTransport* transporter = HciTransport::Get();
86 LOG_ERROR(LOG_TAG, "HciTransport was not initialized");
91 case BT_VND_OP_POWER_CTRL: {
92 LOG_INFO(LOG_TAG, "Doing op: BT_VND_OP_POWER_CTRL");
93 int* state = static_cast<int*>(param);
94 if (*state == BT_VND_PWR_OFF) {
95 LOG_INFO(LOG_TAG, "Turning Bluetooth off.");
96 } else if (*state == BT_VND_PWR_ON) {
97 LOG_INFO(LOG_TAG, "Turning Bluetooth on.");
102 // Give the HCI its fd to communicate with the HciTransport.
103 case BT_VND_OP_USERIAL_OPEN: {
104 LOG_INFO(LOG_TAG, "Doing op: BT_VND_OP_USERIAL_OPEN");
105 int* transporter_fd = static_cast<int*>(param);
106 transporter_fd[0] = transporter->GetHciFd();
107 LOG_INFO(LOG_TAG, "Setting HCI's fd to: %d", transporter_fd[0]);
111 // Close the HCI's file descriptor.
112 case BT_VND_OP_USERIAL_CLOSE:
113 LOG_INFO(LOG_TAG, "Doing op: BT_VND_OP_USERIAL_CLOSE");
114 LOG_INFO(LOG_TAG, "Closing HCI's fd (fd: %d)", transporter->GetHciFd());
115 close(transporter->GetHciFd());
118 case BT_VND_OP_FW_CFG:
119 LOG_INFO(LOG_TAG, "Unsupported op: BT_VND_OP_FW_CFG");
120 vendor_callbacks->fwcfg_cb(BT_VND_OP_RESULT_FAIL);
124 LOG_INFO(LOG_TAG, "Op not recognized.");
130 // Closes the vendor interface and destroys global objects.
131 static void TestVendorCleanUp(void) {
132 LOG_INFO(LOG_TAG, "Cleaning up vendor library.");
133 test_vendor_lib::BREDRController::CleanUp();
134 test_vendor_lib::HciHandler::CleanUp();
135 test_vendor_lib::HciTransport::CleanUp();
138 } // namespace test_vendor_lib
140 // Entry point of DLib.
141 const bt_vendor_interface_t BLUETOOTH_VENDOR_LIB_INTERFACE = {
142 sizeof(bt_vendor_interface_t),
143 test_vendor_lib::TestVendorInitialize,
144 test_vendor_lib::TestVendorOp,
145 test_vendor_lib::TestVendorCleanUp