blob: a0b4eaad2e127b39c4aac997552edc661a26a6e2 [file] [log] [blame]
Arman Uguray2117e522015-08-14 17:23:47 -07001//
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
19namespace bluetooth {
20namespace hal {
21
22namespace {
23
24FakeBluetoothInterface::Manager g_hal_manager;
25
26int FakeHALEnable() {
27 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
34int FakeHALGetAdapterProperties() {
35 return BT_STATUS_SUCCESS;
36}
37
38int 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
44bt_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 */
Jakub Pawlowski1a5bb5f2015-12-01 12:14:22 -080061 nullptr, /* create_bond_out_of_band */
Arman Uguray2117e522015-08-14 17:23:47 -070062 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 */
Andre Eisenbachf79d9cb2016-02-15 14:51:07 -080075 nullptr, /* config clear */
76 nullptr, /* interop_database_clear */
77 nullptr /* interop_database_add */
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),
90 set_property_succeed(false) {
91}
92
93void FakeBluetoothInterface::NotifyAdapterStateChanged(bt_state_t state) {
94 FOR_EACH_OBSERVER(Observer, observers_, AdapterStateChangedCallback(state));
95}
96
Arman Uguray03b1f0f2015-08-17 17:23:42 -070097void 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
105void 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
121void 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
Arman Uguray10b54c42015-08-21 14:59:57 -0700131void 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
Arman Uguray0f29c002015-11-13 15:05:48 -0800141void 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
Arman Uguray2117e522015-08-14 17:23:47 -0700150void FakeBluetoothInterface::AddObserver(Observer* observer) {
151 observers_.AddObserver(observer);
152}
153
154void FakeBluetoothInterface::RemoveObserver(Observer* observer) {
155 observers_.RemoveObserver(observer);
156}
157
158const bt_interface_t* FakeBluetoothInterface::GetHALInterface() const {
159 return &fake_bt_iface;
160}
161
162const 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