OSDN Git Service

resolved conflicts for b8cc54d1 to mnc-dr-dev-plus-aosp
[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 "vendor_manager.h"
20
21 #include "base/logging.h"
22
23 extern "C" {
24 #include "osi/include/log.h"
25
26 #include <unistd.h>
27 }  // extern "C"
28
29 namespace test_vendor_lib {
30
31 // Initializes vendor manager for test controller. |p_cb| are the callbacks to
32 // be in TestVendorOp(). |local_bdaddr| points to the address of the Bluetooth
33 // device. Returns 0 on success, -1 on error.
34 static int TestVendorInitialize(const bt_vendor_callbacks_t* p_cb,
35                                 unsigned char* /* local_bdaddr */) {
36   LOG_INFO(LOG_TAG, "Initializing test controller.");
37   CHECK(p_cb);
38
39   VendorManager::Initialize();
40   VendorManager* manager = VendorManager::Get();
41   manager->SetVendorCallbacks(*(const_cast<bt_vendor_callbacks_t*>(p_cb)));
42   return manager->Run() ? 0 : -1;
43 }
44
45 // Vendor specific operations. |opcode| is the opcode for Bluedroid's vendor op
46 // definitions. |param| points to operation specific arguments. Return value is
47 // dependent on the operation invoked, or -1 on error.
48 static int TestVendorOp(bt_vendor_opcode_t opcode, void* param) {
49   LOG_INFO(LOG_TAG, "Opcode received in vendor library: %d", opcode);
50
51   VendorManager* manager = VendorManager::Get();
52   CHECK(manager);
53
54   switch (opcode) {
55     case BT_VND_OP_POWER_CTRL: {
56       LOG_INFO(LOG_TAG, "Doing op: BT_VND_OP_POWER_CTRL");
57       int* state = static_cast<int*>(param);
58       if (*state == BT_VND_PWR_OFF)
59         LOG_INFO(LOG_TAG, "Turning Bluetooth off.");
60       else if (*state == BT_VND_PWR_ON)
61         LOG_INFO(LOG_TAG, "Turning Bluetooth on.");
62       return 0;
63     }
64
65     // Give the HCI its fd to communicate with the HciTransport.
66     case BT_VND_OP_USERIAL_OPEN: {
67       LOG_INFO(LOG_TAG, "Doing op: BT_VND_OP_USERIAL_OPEN");
68       int* fd_list = static_cast<int*>(param);
69       fd_list[0] = manager->GetHciFd();
70       LOG_INFO(LOG_TAG, "Setting HCI's fd to: %d", fd_list[0]);
71       return 1;
72     }
73
74     // Close the HCI's file descriptor.
75     case BT_VND_OP_USERIAL_CLOSE:
76       LOG_INFO(LOG_TAG, "Doing op: BT_VND_OP_USERIAL_CLOSE");
77       LOG_INFO(LOG_TAG, "Closing HCI's fd (fd: %d)", manager->GetHciFd());
78       manager->CloseHciFd();
79       return 1;
80
81     case BT_VND_OP_FW_CFG:
82       LOG_INFO(LOG_TAG, "Unsupported op: BT_VND_OP_FW_CFG");
83       manager->GetVendorCallbacks().fwcfg_cb(BT_VND_OP_RESULT_FAIL);
84       return -1;
85
86     default:
87       LOG_INFO(LOG_TAG, "Op not recognized.");
88       return -1;
89   }
90   return 0;
91 }
92
93 // Closes the vendor interface and cleans up the global vendor manager object.
94 static void TestVendorCleanUp(void) {
95   LOG_INFO(LOG_TAG, "Cleaning up vendor library.");
96   VendorManager::CleanUp();
97 }
98
99 }  // namespace test_vendor_lib
100
101 // Entry point of DLib.
102 const bt_vendor_interface_t BLUETOOTH_VENDOR_LIB_INTERFACE = {
103   sizeof(bt_vendor_interface_t),
104   test_vendor_lib::TestVendorInitialize,
105   test_vendor_lib::TestVendorOp,
106   test_vendor_lib::TestVendorCleanUp
107 };