Arman Uguray | 2117e52 | 2015-08-14 17:23:47 -0700 | [diff] [blame] | 1 | // |
Jakub Pawlowski | 5b790fe | 2017-09-18 09:00:20 -0700 | [diff] [blame] | 2 | // Copyright 2015 Google, Inc. |
Arman Uguray | 2117e52 | 2015-08-14 17:23:47 -0700 | [diff] [blame] | 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 | |
Martin Brabham | 1a88ace | 2019-05-10 12:42:15 -0700 | [diff] [blame] | 26 | int FakeHALEnable() { |
Arman Uguray | 2117e52 | 2015-08-14 17:23:47 -0700 | [diff] [blame] | 27 | return g_hal_manager.enable_succeed ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; |
| 28 | } |
| 29 | |
| 30 | int FakeHALDisable() { |
| 31 | return g_hal_manager.disable_succeed ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; |
| 32 | } |
| 33 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 34 | int FakeHALGetAdapterProperties() { return BT_STATUS_SUCCESS; } |
Arman Uguray | 2117e52 | 2015-08-14 17:23:47 -0700 | [diff] [blame] | 35 | |
| 36 | int FakeHALSetAdapterProperty(const bt_property_t* /* property */) { |
| 37 | LOG(INFO) << __func__; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 38 | return (g_hal_manager.set_property_succeed ? BT_STATUS_SUCCESS |
| 39 | : BT_STATUS_FAIL); |
Arman Uguray | 2117e52 | 2015-08-14 17:23:47 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | bt_interface_t fake_bt_iface = { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 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 */ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 69 | nullptr, /* set_os_callouts */ |
| 70 | nullptr, /* read_energy_info */ |
| 71 | nullptr, /* dump */ |
Jack He | 9af0078 | 2018-01-31 16:51:26 -0800 | [diff] [blame] | 72 | nullptr, /* dumpMetrics */ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 73 | nullptr, /* config clear */ |
| 74 | nullptr, /* interop_database_clear */ |
Ajay Panicker | 6640179 | 2018-03-14 23:45:54 -0700 | [diff] [blame] | 75 | nullptr, /* interop_database_add */ |
| 76 | nullptr, /* get_avrcp_service */ |
Jack He | 78b69d9 | 2018-11-16 02:59:43 -0800 | [diff] [blame] | 77 | nullptr, /* obfuscate_address */ |
Arman Uguray | 2117e52 | 2015-08-14 17:23:47 -0700 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | } // namespace |
| 81 | |
| 82 | // static |
| 83 | FakeBluetoothInterface::Manager* FakeBluetoothInterface::GetManager() { |
| 84 | return &g_hal_manager; |
| 85 | } |
| 86 | |
| 87 | FakeBluetoothInterface::Manager::Manager() |
| 88 | : enable_succeed(false), |
| 89 | disable_succeed(false), |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 90 | set_property_succeed(false) {} |
Arman Uguray | 2117e52 | 2015-08-14 17:23:47 -0700 | [diff] [blame] | 91 | |
| 92 | void FakeBluetoothInterface::NotifyAdapterStateChanged(bt_state_t state) { |
Hidehiko Abe | aeca7aa | 2017-12-13 18:57:10 +0900 | [diff] [blame] | 93 | for (auto& observer : observers_) { |
| 94 | observer.AdapterStateChangedCallback(state); |
| 95 | } |
Arman Uguray | 2117e52 | 2015-08-14 17:23:47 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Arman Uguray | 03b1f0f | 2015-08-17 17:23:42 -0700 | [diff] [blame] | 98 | void FakeBluetoothInterface::NotifyAdapterPropertiesChanged( |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 99 | int num_properties, bt_property_t* properties) { |
Hidehiko Abe | aeca7aa | 2017-12-13 18:57:10 +0900 | [diff] [blame] | 100 | for (auto& observer : observers_) { |
| 101 | observer.AdapterPropertiesCallback(BT_STATUS_SUCCESS, num_properties, |
| 102 | properties); |
| 103 | } |
Arman Uguray | 03b1f0f | 2015-08-17 17:23:42 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | void FakeBluetoothInterface::NotifyAdapterNamePropertyChanged( |
| 107 | const std::string& name) { |
| 108 | bt_bdname_t hal_name; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 109 | strncpy(reinterpret_cast<char*>(hal_name.name), name.c_str(), |
| 110 | std::min(sizeof(hal_name) - 1, name.length())); |
Arman Uguray | 03b1f0f | 2015-08-17 17:23:42 -0700 | [diff] [blame] | 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 | |
| 121 | void FakeBluetoothInterface::NotifyAdapterAddressPropertyChanged( |
Jakub Pawlowski | a484a88 | 2017-06-24 17:30:18 -0700 | [diff] [blame] | 122 | const RawAddress* address) { |
Arman Uguray | 03b1f0f | 2015-08-17 17:23:42 -0700 | [diff] [blame] | 123 | bt_property_t property; |
Jakub Pawlowski | 2e05f0d | 2017-08-16 06:41:02 -0700 | [diff] [blame] | 124 | property.len = RawAddress::kLength; |
| 125 | property.val = (void*)address->address; |
Arman Uguray | 03b1f0f | 2015-08-17 17:23:42 -0700 | [diff] [blame] | 126 | property.type = BT_PROPERTY_BDADDR; |
| 127 | |
| 128 | NotifyAdapterPropertiesChanged(1, &property); |
| 129 | } |
| 130 | |
Arman Uguray | 10b54c4 | 2015-08-21 14:59:57 -0700 | [diff] [blame] | 131 | void FakeBluetoothInterface::NotifyAdapterLocalLeFeaturesPropertyChanged( |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 132 | const bt_local_le_features_t* features) { |
Arman Uguray | 10b54c4 | 2015-08-21 14:59:57 -0700 | [diff] [blame] | 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 | |
Arman Uguray | 0f29c00 | 2015-11-13 15:05:48 -0800 | [diff] [blame] | 141 | void FakeBluetoothInterface::NotifyAclStateChangedCallback( |
Jakub Pawlowski | a484a88 | 2017-06-24 17:30:18 -0700 | [diff] [blame] | 142 | bt_status_t status, const RawAddress& remote_bdaddr, bt_acl_state_t state) { |
Hidehiko Abe | aeca7aa | 2017-12-13 18:57:10 +0900 | [diff] [blame] | 143 | for (auto& observer : observers_) { |
| 144 | observer.AclStateChangedCallback(status, remote_bdaddr, state); |
| 145 | } |
Arman Uguray | 0f29c00 | 2015-11-13 15:05:48 -0800 | [diff] [blame] | 146 | } |
| 147 | |
Arman Uguray | 2117e52 | 2015-08-14 17:23:47 -0700 | [diff] [blame] | 148 | void FakeBluetoothInterface::AddObserver(Observer* observer) { |
| 149 | observers_.AddObserver(observer); |
| 150 | } |
| 151 | |
| 152 | void FakeBluetoothInterface::RemoveObserver(Observer* observer) { |
| 153 | observers_.RemoveObserver(observer); |
| 154 | } |
| 155 | |
| 156 | const bt_interface_t* FakeBluetoothInterface::GetHALInterface() const { |
| 157 | return &fake_bt_iface; |
| 158 | } |
| 159 | |
Pavlin Radoslavov | a329205 | 2017-04-13 14:33:30 -0700 | [diff] [blame] | 160 | bt_callbacks_t* FakeBluetoothInterface::GetHALCallbacks() const { |
| 161 | return nullptr; |
| 162 | } |
| 163 | |
Arman Uguray | 2117e52 | 2015-08-14 17:23:47 -0700 | [diff] [blame] | 164 | } // namespace hal |
| 165 | } // namespace bluetooth |