blob: f1d03adc3294e46defe194c7f1d795c91e181661 [file] [log] [blame]
Arman Uguray2117e522015-08-14 17:23:47 -07001//
Jakub Pawlowski5b790fe2017-09-18 09:00:20 -07002// Copyright 2015 Google, Inc.
Arman Uguray2117e522015-08-14 17:23:47 -07003//
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
19namespace bluetooth {
20namespace hal {
21
22namespace {
23
24FakeBluetoothInterface::Manager g_hal_manager;
25
Martin Brabham1a88ace2019-05-10 12:42:15 -070026int FakeHALEnable() {
Arman Uguray2117e522015-08-14 17:23:47 -070027 return g_hal_manager.enable_succeed ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
28}
29
30int FakeHALDisable() {
31 return g_hal_manager.disable_succeed ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
32}
33
Myles Watson911d1ae2016-11-28 16:44:40 -080034int FakeHALGetAdapterProperties() { return BT_STATUS_SUCCESS; }
Arman Uguray2117e522015-08-14 17:23:47 -070035
36int FakeHALSetAdapterProperty(const bt_property_t* /* property */) {
37 LOG(INFO) << __func__;
Myles Watson911d1ae2016-11-28 16:44:40 -080038 return (g_hal_manager.set_property_succeed ? BT_STATUS_SUCCESS
39 : BT_STATUS_FAIL);
Arman Uguray2117e522015-08-14 17:23:47 -070040}
41
42bt_interface_t fake_bt_iface = {
Myles Watson911d1ae2016-11-28 16:44:40 -080043 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 Watson911d1ae2016-11-28 16:44:40 -080069 nullptr, /* set_os_callouts */
70 nullptr, /* read_energy_info */
71 nullptr, /* dump */
Jack He9af00782018-01-31 16:51:26 -080072 nullptr, /* dumpMetrics */
Myles Watson911d1ae2016-11-28 16:44:40 -080073 nullptr, /* config clear */
74 nullptr, /* interop_database_clear */
Ajay Panicker66401792018-03-14 23:45:54 -070075 nullptr, /* interop_database_add */
76 nullptr, /* get_avrcp_service */
Jack He78b69d92018-11-16 02:59:43 -080077 nullptr, /* obfuscate_address */
Arman Uguray2117e522015-08-14 17:23:47 -070078};
79
80} // namespace
81
82// static
83FakeBluetoothInterface::Manager* FakeBluetoothInterface::GetManager() {
84 return &g_hal_manager;
85}
86
87FakeBluetoothInterface::Manager::Manager()
88 : enable_succeed(false),
89 disable_succeed(false),
Myles Watson911d1ae2016-11-28 16:44:40 -080090 set_property_succeed(false) {}
Arman Uguray2117e522015-08-14 17:23:47 -070091
92void FakeBluetoothInterface::NotifyAdapterStateChanged(bt_state_t state) {
Hidehiko Abeaeca7aa2017-12-13 18:57:10 +090093 for (auto& observer : observers_) {
94 observer.AdapterStateChangedCallback(state);
95 }
Arman Uguray2117e522015-08-14 17:23:47 -070096}
97
Arman Uguray03b1f0f2015-08-17 17:23:42 -070098void FakeBluetoothInterface::NotifyAdapterPropertiesChanged(
Myles Watson911d1ae2016-11-28 16:44:40 -080099 int num_properties, bt_property_t* properties) {
Hidehiko Abeaeca7aa2017-12-13 18:57:10 +0900100 for (auto& observer : observers_) {
101 observer.AdapterPropertiesCallback(BT_STATUS_SUCCESS, num_properties,
102 properties);
103 }
Arman Uguray03b1f0f2015-08-17 17:23:42 -0700104}
105
106void FakeBluetoothInterface::NotifyAdapterNamePropertyChanged(
107 const std::string& name) {
108 bt_bdname_t hal_name;
Myles Watson911d1ae2016-11-28 16:44:40 -0800109 strncpy(reinterpret_cast<char*>(hal_name.name), name.c_str(),
110 std::min(sizeof(hal_name) - 1, name.length()));
Arman Uguray03b1f0f2015-08-17 17:23:42 -0700111 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
121void FakeBluetoothInterface::NotifyAdapterAddressPropertyChanged(
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700122 const RawAddress* address) {
Arman Uguray03b1f0f2015-08-17 17:23:42 -0700123 bt_property_t property;
Jakub Pawlowski2e05f0d2017-08-16 06:41:02 -0700124 property.len = RawAddress::kLength;
125 property.val = (void*)address->address;
Arman Uguray03b1f0f2015-08-17 17:23:42 -0700126 property.type = BT_PROPERTY_BDADDR;
127
128 NotifyAdapterPropertiesChanged(1, &property);
129}
130
Arman Uguray10b54c42015-08-21 14:59:57 -0700131void FakeBluetoothInterface::NotifyAdapterLocalLeFeaturesPropertyChanged(
Myles Watson911d1ae2016-11-28 16:44:40 -0800132 const bt_local_le_features_t* features) {
Arman Uguray10b54c42015-08-21 14:59:57 -0700133 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 Uguray0f29c002015-11-13 15:05:48 -0800141void FakeBluetoothInterface::NotifyAclStateChangedCallback(
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700142 bt_status_t status, const RawAddress& remote_bdaddr, bt_acl_state_t state) {
Hidehiko Abeaeca7aa2017-12-13 18:57:10 +0900143 for (auto& observer : observers_) {
144 observer.AclStateChangedCallback(status, remote_bdaddr, state);
145 }
Arman Uguray0f29c002015-11-13 15:05:48 -0800146}
147
Arman Uguray2117e522015-08-14 17:23:47 -0700148void FakeBluetoothInterface::AddObserver(Observer* observer) {
149 observers_.AddObserver(observer);
150}
151
152void FakeBluetoothInterface::RemoveObserver(Observer* observer) {
153 observers_.RemoveObserver(observer);
154}
155
156const bt_interface_t* FakeBluetoothInterface::GetHALInterface() const {
157 return &fake_bt_iface;
158}
159
Pavlin Radoslavova3292052017-04-13 14:33:30 -0700160bt_callbacks_t* FakeBluetoothInterface::GetHALCallbacks() const {
161 return nullptr;
162}
163
Arman Uguray2117e522015-08-14 17:23:47 -0700164} // namespace hal
165} // namespace bluetooth