Arman Uguray | 2117e52 | 2015-08-14 17:23:47 -0700 | [diff] [blame] | 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 | |
Ajay Panicker | 7b266be | 2016-03-17 17:09:24 -0700 | [diff] [blame] | 26 | int FakeHALEnable(bool start_restricted) { |
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 */ |
| 72 | nullptr, /* config clear */ |
| 73 | nullptr, /* interop_database_clear */ |
| 74 | nullptr /* interop_database_add */ |
Arman Uguray | 2117e52 | 2015-08-14 17:23:47 -0700 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | } // namespace |
| 78 | |
| 79 | // static |
| 80 | FakeBluetoothInterface::Manager* FakeBluetoothInterface::GetManager() { |
| 81 | return &g_hal_manager; |
| 82 | } |
| 83 | |
| 84 | FakeBluetoothInterface::Manager::Manager() |
| 85 | : enable_succeed(false), |
| 86 | disable_succeed(false), |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 87 | set_property_succeed(false) {} |
Arman Uguray | 2117e52 | 2015-08-14 17:23:47 -0700 | [diff] [blame] | 88 | |
| 89 | void FakeBluetoothInterface::NotifyAdapterStateChanged(bt_state_t state) { |
| 90 | FOR_EACH_OBSERVER(Observer, observers_, AdapterStateChangedCallback(state)); |
| 91 | } |
| 92 | |
Arman Uguray | 03b1f0f | 2015-08-17 17:23:42 -0700 | [diff] [blame] | 93 | void FakeBluetoothInterface::NotifyAdapterPropertiesChanged( |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 94 | int num_properties, bt_property_t* properties) { |
Arman Uguray | 03b1f0f | 2015-08-17 17:23:42 -0700 | [diff] [blame] | 95 | FOR_EACH_OBSERVER( |
| 96 | Observer, observers_, |
| 97 | AdapterPropertiesCallback(BT_STATUS_SUCCESS, num_properties, properties)); |
| 98 | } |
| 99 | |
| 100 | void FakeBluetoothInterface::NotifyAdapterNamePropertyChanged( |
| 101 | const std::string& name) { |
| 102 | bt_bdname_t hal_name; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 103 | strncpy(reinterpret_cast<char*>(hal_name.name), name.c_str(), |
| 104 | std::min(sizeof(hal_name) - 1, name.length())); |
Arman Uguray | 03b1f0f | 2015-08-17 17:23:42 -0700 | [diff] [blame] | 105 | reinterpret_cast<char*>(hal_name.name)[name.length()] = '\0'; |
| 106 | |
| 107 | bt_property_t property; |
| 108 | property.len = sizeof(hal_name); |
| 109 | property.val = &hal_name; |
| 110 | property.type = BT_PROPERTY_BDNAME; |
| 111 | |
| 112 | NotifyAdapterPropertiesChanged(1, &property); |
| 113 | } |
| 114 | |
| 115 | void FakeBluetoothInterface::NotifyAdapterAddressPropertyChanged( |
Jakub Pawlowski | a484a88 | 2017-06-24 17:30:18 -0700 | [diff] [blame^] | 116 | const RawAddress* address) { |
Arman Uguray | 03b1f0f | 2015-08-17 17:23:42 -0700 | [diff] [blame] | 117 | bt_property_t property; |
Jakub Pawlowski | a484a88 | 2017-06-24 17:30:18 -0700 | [diff] [blame^] | 118 | property.len = sizeof(RawAddress); |
Arman Uguray | 03b1f0f | 2015-08-17 17:23:42 -0700 | [diff] [blame] | 119 | property.val = (void*)address; |
| 120 | property.type = BT_PROPERTY_BDADDR; |
| 121 | |
| 122 | NotifyAdapterPropertiesChanged(1, &property); |
| 123 | } |
| 124 | |
Arman Uguray | 10b54c4 | 2015-08-21 14:59:57 -0700 | [diff] [blame] | 125 | void FakeBluetoothInterface::NotifyAdapterLocalLeFeaturesPropertyChanged( |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 126 | const bt_local_le_features_t* features) { |
Arman Uguray | 10b54c4 | 2015-08-21 14:59:57 -0700 | [diff] [blame] | 127 | bt_property_t property; |
| 128 | property.len = sizeof(*features); |
| 129 | property.val = (void*)features; |
| 130 | property.type = BT_PROPERTY_LOCAL_LE_FEATURES; |
| 131 | |
| 132 | NotifyAdapterPropertiesChanged(1, &property); |
| 133 | } |
| 134 | |
Arman Uguray | 0f29c00 | 2015-11-13 15:05:48 -0800 | [diff] [blame] | 135 | void FakeBluetoothInterface::NotifyAclStateChangedCallback( |
Jakub Pawlowski | a484a88 | 2017-06-24 17:30:18 -0700 | [diff] [blame^] | 136 | bt_status_t status, const RawAddress& remote_bdaddr, bt_acl_state_t state) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 137 | FOR_EACH_OBSERVER(Observer, observers_, |
| 138 | AclStateChangedCallback(status, remote_bdaddr, state)); |
Arman Uguray | 0f29c00 | 2015-11-13 15:05:48 -0800 | [diff] [blame] | 139 | } |
| 140 | |
Arman Uguray | 2117e52 | 2015-08-14 17:23:47 -0700 | [diff] [blame] | 141 | void FakeBluetoothInterface::AddObserver(Observer* observer) { |
| 142 | observers_.AddObserver(observer); |
| 143 | } |
| 144 | |
| 145 | void FakeBluetoothInterface::RemoveObserver(Observer* observer) { |
| 146 | observers_.RemoveObserver(observer); |
| 147 | } |
| 148 | |
| 149 | const bt_interface_t* FakeBluetoothInterface::GetHALInterface() const { |
| 150 | return &fake_bt_iface; |
| 151 | } |
| 152 | |
Pavlin Radoslavov | a329205 | 2017-04-13 14:33:30 -0700 | [diff] [blame] | 153 | bt_callbacks_t* FakeBluetoothInterface::GetHALCallbacks() const { |
| 154 | return nullptr; |
| 155 | } |
| 156 | |
Arman Uguray | 2117e52 | 2015-08-14 17:23:47 -0700 | [diff] [blame] | 157 | const bluetooth_device_t* FakeBluetoothInterface::GetHALAdapter() const { |
| 158 | // TODO(armansito): Do something meaningful here to simulate test behavior. |
| 159 | return nullptr; |
| 160 | } |
| 161 | |
| 162 | } // namespace hal |
| 163 | } // namespace bluetooth |