1 //
2 //  Copyright (C) 2015 Google, Inc.
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 #include "service/hal/fake_bluetooth_interface.h"
18 
19 namespace bluetooth {
20 namespace hal {
21 
22 namespace {
23 
24 FakeBluetoothInterface::Manager g_hal_manager;
25 
FakeHALEnable(bool start_restricted)26 int FakeHALEnable(bool start_restricted) {
27   return g_hal_manager.enable_succeed ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
28 }
29 
FakeHALDisable()30 int FakeHALDisable() {
31   return g_hal_manager.disable_succeed ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
32 }
33 
FakeHALGetAdapterProperties()34 int FakeHALGetAdapterProperties() {
35   return BT_STATUS_SUCCESS;
36 }
37 
FakeHALSetAdapterProperty(const bt_property_t *)38 int FakeHALSetAdapterProperty(const bt_property_t* /* property */) {
39   LOG(INFO) << __func__;
40   return (g_hal_manager.set_property_succeed ? BT_STATUS_SUCCESS :
41           BT_STATUS_FAIL);
42 }
43 
44 bt_interface_t fake_bt_iface = {
45   sizeof(bt_interface_t),
46   nullptr, /* init */
47   FakeHALEnable,
48   FakeHALDisable,
49   nullptr, /* cleanup */
50   FakeHALGetAdapterProperties,
51   nullptr, /* get_adapter_property */
52   FakeHALSetAdapterProperty,
53   nullptr, /* get_remote_device_properties */
54   nullptr, /* get_remote_device_property */
55   nullptr, /* set_remote_device_property */
56   nullptr, /* get_remote_service_record */
57   nullptr, /* get_remote_services */
58   nullptr, /* start_discovery */
59   nullptr, /* cancel_discovery */
60   nullptr, /* create_bond */
61   nullptr, /* create_bond_out_of_band */
62   nullptr, /* remove_bond */
63   nullptr, /* cancel_bond */
64   nullptr, /* get_connection_state */
65   nullptr, /* pin_reply */
66   nullptr, /* ssp_reply */
67   nullptr, /* get_profile_interface */
68   nullptr, /* dut_mode_configure */
69   nullptr, /* dut_more_send */
70   nullptr, /* le_test_mode */
71   nullptr, /* config_hci_snoop_log */
72   nullptr, /* set_os_callouts */
73   nullptr, /* read_energy_info */
74   nullptr, /* dump */
75   nullptr, /* config clear */
76   nullptr, /* interop_database_clear */
77   nullptr  /* interop_database_add */
78 };
79 
80 }  // namespace
81 
82 // static
GetManager()83 FakeBluetoothInterface::Manager* FakeBluetoothInterface::GetManager() {
84   return &g_hal_manager;
85 }
86 
Manager()87 FakeBluetoothInterface::Manager::Manager()
88     : enable_succeed(false),
89       disable_succeed(false),
90       set_property_succeed(false) {
91 }
92 
NotifyAdapterStateChanged(bt_state_t state)93 void FakeBluetoothInterface::NotifyAdapterStateChanged(bt_state_t state) {
94   FOR_EACH_OBSERVER(Observer, observers_, AdapterStateChangedCallback(state));
95 }
96 
NotifyAdapterPropertiesChanged(int num_properties,bt_property_t * properties)97 void FakeBluetoothInterface::NotifyAdapterPropertiesChanged(
98     int num_properties,
99     bt_property_t* properties) {
100   FOR_EACH_OBSERVER(
101       Observer, observers_,
102       AdapterPropertiesCallback(BT_STATUS_SUCCESS, num_properties, properties));
103 }
104 
NotifyAdapterNamePropertyChanged(const std::string & name)105 void FakeBluetoothInterface::NotifyAdapterNamePropertyChanged(
106     const std::string& name) {
107   bt_bdname_t hal_name;
108   strncpy(reinterpret_cast<char*>(hal_name.name),
109           name.c_str(),
110           std::min(sizeof(hal_name)-1, name.length()));
111   reinterpret_cast<char*>(hal_name.name)[name.length()] = '\0';
112 
113   bt_property_t property;
114   property.len = sizeof(hal_name);
115   property.val = &hal_name;
116   property.type = BT_PROPERTY_BDNAME;
117 
118   NotifyAdapterPropertiesChanged(1, &property);
119 }
120 
NotifyAdapterAddressPropertyChanged(const bt_bdaddr_t * address)121 void FakeBluetoothInterface::NotifyAdapterAddressPropertyChanged(
122     const bt_bdaddr_t* address) {
123   bt_property_t property;
124   property.len = sizeof(bt_bdaddr_t);
125   property.val = (void*)address;
126   property.type = BT_PROPERTY_BDADDR;
127 
128   NotifyAdapterPropertiesChanged(1, &property);
129 }
130 
NotifyAdapterLocalLeFeaturesPropertyChanged(const bt_local_le_features_t * features)131 void FakeBluetoothInterface::NotifyAdapterLocalLeFeaturesPropertyChanged(
132       const bt_local_le_features_t* features) {
133   bt_property_t property;
134   property.len = sizeof(*features);
135   property.val = (void*)features;
136   property.type = BT_PROPERTY_LOCAL_LE_FEATURES;
137 
138   NotifyAdapterPropertiesChanged(1, &property);
139 }
140 
NotifyAclStateChangedCallback(bt_status_t status,const bt_bdaddr_t & remote_bdaddr,bt_acl_state_t state)141 void FakeBluetoothInterface::NotifyAclStateChangedCallback(
142     bt_status_t status,
143     const bt_bdaddr_t& remote_bdaddr,
144     bt_acl_state_t state) {
145   FOR_EACH_OBSERVER(
146       Observer, observers_,
147       AclStateChangedCallback(status, remote_bdaddr, state));
148 }
149 
AddObserver(Observer * observer)150 void FakeBluetoothInterface::AddObserver(Observer* observer) {
151   observers_.AddObserver(observer);
152 }
153 
RemoveObserver(Observer * observer)154 void FakeBluetoothInterface::RemoveObserver(Observer* observer) {
155   observers_.RemoveObserver(observer);
156 }
157 
GetHALInterface() const158 const bt_interface_t* FakeBluetoothInterface::GetHALInterface() const {
159   return &fake_bt_iface;
160 }
161 
GetHALAdapter() const162 const bluetooth_device_t* FakeBluetoothInterface::GetHALAdapter() const {
163   // TODO(armansito): Do something meaningful here to simulate test behavior.
164   return nullptr;
165 }
166 
167 }  // namespace hal
168 }  // namespace bluetooth
169