OSDN Git Service

test_vendor_lib: Initial commit
[android-x86/system-bt.git] / vendor_libs / test_vendor_lib / src / bt_vendor.cc
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 #define LOG_TAG "bt_vendor"
18
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"
23
24 #include <pthread.h>
25
26 extern "C" {
27 #include "osi/include/log.h"
28
29 #include <assert.h>
30 #include <unistd.h>
31 }  // extern "C"
32
33 namespace {
34 bt_vendor_callbacks_t* vendor_callbacks;
35
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.");
40   return NULL;
41 }
42 }  // namespace
43
44 namespace test_vendor_lib {
45
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.");
52
53   // TODO(dennischeng): use CHECK and DCHECK when libbase is imported.
54   assert(p_cb);
55   vendor_callbacks = const_cast<bt_vendor_callbacks_t*>(p_cb);
56
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();
63
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.");
69     return -1;
70   }
71
72   // Start HciTransport listening on its own thread.
73   pthread_t transporter_thread;
74   pthread_create(&transporter_thread, NULL, &ListenEntryPoint, transporter);
75   return 0;
76 }
77
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);
83
84   HciTransport* transporter = HciTransport::Get();
85   if (!transporter) {
86     LOG_ERROR(LOG_TAG, "HciTransport was not initialized");
87     return -1;
88   }
89
90   switch (opcode) {
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.");
98       }
99       return 0;
100     }
101
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]);
108       return 1;
109     }
110
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());
116       return 1;
117
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);
121       return -1;
122
123     default:
124       LOG_INFO(LOG_TAG, "Op not recognized.");
125       return -1;
126   }
127   return 0;
128 }
129
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();
136 }
137
138 }  // namespace test_vendor_lib
139
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
146 };