1 //
2 // Copyright 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()26 int FakeHALEnable() {
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() { return BT_STATUS_SUCCESS; }
35
FakeHALSetAdapterProperty(const bt_property_t *)36 int FakeHALSetAdapterProperty(const bt_property_t* /* property */) {
37 LOG(INFO) << __func__;
38 return (g_hal_manager.set_property_succeed ? BT_STATUS_SUCCESS
39 : BT_STATUS_FAIL);
40 }
41
42 bt_interface_t fake_bt_iface = {
43 sizeof(bt_interface_t),
44 nullptr, /* init */
45 FakeHALEnable,
46 FakeHALDisable,
47 nullptr, /* cleanup */
48 FakeHALGetAdapterProperties,
49 nullptr, /* get_adapter_property */
50 FakeHALSetAdapterProperty,
51 nullptr, /* get_remote_device_properties */
52 nullptr, /* get_remote_device_property */
53 nullptr, /* set_remote_device_property */
54 nullptr, /* get_remote_service_record */
55 nullptr, /* get_remote_services */
56 nullptr, /* start_discovery */
57 nullptr, /* cancel_discovery */
58 nullptr, /* create_bond */
59 nullptr, /* create_bond_out_of_band */
60 nullptr, /* remove_bond */
61 nullptr, /* cancel_bond */
62 nullptr, /* get_connection_state */
63 nullptr, /* pin_reply */
64 nullptr, /* ssp_reply */
65 nullptr, /* get_profile_interface */
66 nullptr, /* dut_mode_configure */
67 nullptr, /* dut_more_send */
68 nullptr, /* le_test_mode */
69 nullptr, /* set_os_callouts */
70 nullptr, /* read_energy_info */
71 nullptr, /* dump */
72 nullptr, /* dumpMetrics */
73 nullptr, /* config clear */
74 nullptr, /* interop_database_clear */
75 nullptr, /* interop_database_add */
76 nullptr, /* get_avrcp_service */
77 nullptr, /* obfuscate_address */
78 nullptr, /* get_metric_id */
79 nullptr, /* set_dynamic_audio_buffer_size */
80 nullptr, /* generate_local_oob_data */
81 };
82
83 } // namespace
84
85 // static
GetManager()86 FakeBluetoothInterface::Manager* FakeBluetoothInterface::GetManager() {
87 return &g_hal_manager;
88 }
89
Manager()90 FakeBluetoothInterface::Manager::Manager()
91 : enable_succeed(false),
92 disable_succeed(false),
93 set_property_succeed(false) {}
94
NotifyAdapterStateChanged(bt_state_t state)95 void FakeBluetoothInterface::NotifyAdapterStateChanged(bt_state_t state) {
96 for (auto& observer : observers_) {
97 observer.AdapterStateChangedCallback(state);
98 }
99 }
100
NotifyAdapterPropertiesChanged(int num_properties,bt_property_t * properties)101 void FakeBluetoothInterface::NotifyAdapterPropertiesChanged(
102 int num_properties, bt_property_t* properties) {
103 for (auto& observer : observers_) {
104 observer.AdapterPropertiesCallback(BT_STATUS_SUCCESS, num_properties,
105 properties);
106 }
107 }
108
NotifyAdapterNamePropertyChanged(const std::string & name)109 void FakeBluetoothInterface::NotifyAdapterNamePropertyChanged(
110 const std::string& name) {
111 bt_bdname_t hal_name;
112 strncpy(reinterpret_cast<char*>(hal_name.name), name.c_str(),
113 std::min(sizeof(hal_name) - 1, name.length()));
114 reinterpret_cast<char*>(hal_name.name)[name.length()] = '\0';
115
116 bt_property_t property;
117 property.len = sizeof(hal_name);
118 property.val = &hal_name;
119 property.type = BT_PROPERTY_BDNAME;
120
121 NotifyAdapterPropertiesChanged(1, &property);
122 }
123
NotifyAdapterAddressPropertyChanged(const RawAddress * address)124 void FakeBluetoothInterface::NotifyAdapterAddressPropertyChanged(
125 const RawAddress* address) {
126 bt_property_t property;
127 property.len = RawAddress::kLength;
128 property.val = (void*)address->address;
129 property.type = BT_PROPERTY_BDADDR;
130
131 NotifyAdapterPropertiesChanged(1, &property);
132 }
133
NotifyAdapterLocalLeFeaturesPropertyChanged(const bt_local_le_features_t * features)134 void FakeBluetoothInterface::NotifyAdapterLocalLeFeaturesPropertyChanged(
135 const bt_local_le_features_t* features) {
136 bt_property_t property;
137 property.len = sizeof(*features);
138 property.val = (void*)features;
139 property.type = BT_PROPERTY_LOCAL_LE_FEATURES;
140
141 NotifyAdapterPropertiesChanged(1, &property);
142 }
143
NotifyAclStateChangedCallback(bt_status_t status,const RawAddress & remote_bdaddr,bt_acl_state_t state,bt_hci_error_code_t hci_reason)144 void FakeBluetoothInterface::NotifyAclStateChangedCallback(
145 bt_status_t status, const RawAddress& remote_bdaddr, bt_acl_state_t state,
146 bt_hci_error_code_t hci_reason) {
147 for (auto& observer : observers_) {
148 observer.AclStateChangedCallback(status, remote_bdaddr, state, hci_reason);
149 }
150 }
151
AddObserver(Observer * observer)152 void FakeBluetoothInterface::AddObserver(Observer* observer) {
153 observers_.AddObserver(observer);
154 }
155
RemoveObserver(Observer * observer)156 void FakeBluetoothInterface::RemoveObserver(Observer* observer) {
157 observers_.RemoveObserver(observer);
158 }
159
GetHALInterface() const160 const bt_interface_t* FakeBluetoothInterface::GetHALInterface() const {
161 return &fake_bt_iface;
162 }
163
GetHALCallbacks() const164 bt_callbacks_t* FakeBluetoothInterface::GetHALCallbacks() const {
165 return nullptr;
166 }
167
168 } // namespace hal
169 } // namespace bluetooth
170