OSDN Git Service

Prevent integer overflows during GATT signing am: c8fd18625f am: 7da957c2c0
[android-x86/system-bt.git] / service / hal / fake_bluetooth_interface.cpp
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
26 int FakeHALEnable() {
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
34 int FakeHALGetAdapterProperties() {
35   return BT_STATUS_SUCCESS;
36 }
37
38 int 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
44 bt_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 */
61   nullptr, /* remove_bond */
62   nullptr, /* cancel_bond */
63   nullptr, /* get_connection_state */
64   nullptr, /* pin_reply */
65   nullptr, /* ssp_reply */
66   nullptr, /* get_profile_interface */
67   nullptr, /* dut_mode_configure */
68   nullptr, /* dut_more_send */
69   nullptr, /* le_test_mode */
70   nullptr, /* config_hci_snoop_log */
71   nullptr, /* set_os_callouts */
72   nullptr, /* read_energy_info */
73   nullptr, /* dump */
74 };
75
76 }  // namespace
77
78 // static
79 FakeBluetoothInterface::Manager* FakeBluetoothInterface::GetManager() {
80   return &g_hal_manager;
81 }
82
83 FakeBluetoothInterface::Manager::Manager()
84     : enable_succeed(false),
85       disable_succeed(false),
86       set_property_succeed(false) {
87 }
88
89 void FakeBluetoothInterface::NotifyAdapterStateChanged(bt_state_t state) {
90   FOR_EACH_OBSERVER(Observer, observers_, AdapterStateChangedCallback(state));
91 }
92
93 void FakeBluetoothInterface::NotifyAdapterPropertiesChanged(
94     int num_properties,
95     bt_property_t* properties) {
96   FOR_EACH_OBSERVER(
97       Observer, observers_,
98       AdapterPropertiesCallback(BT_STATUS_SUCCESS, num_properties, properties));
99 }
100
101 void FakeBluetoothInterface::NotifyAdapterNamePropertyChanged(
102     const std::string& name) {
103   bt_bdname_t hal_name;
104   strncpy(reinterpret_cast<char*>(hal_name.name),
105           name.c_str(),
106           std::min(sizeof(hal_name)-1, name.length()));
107   reinterpret_cast<char*>(hal_name.name)[name.length()] = '\0';
108
109   bt_property_t property;
110   property.len = sizeof(hal_name);
111   property.val = &hal_name;
112   property.type = BT_PROPERTY_BDNAME;
113
114   NotifyAdapterPropertiesChanged(1, &property);
115 }
116
117 void FakeBluetoothInterface::NotifyAdapterAddressPropertyChanged(
118     const bt_bdaddr_t* address) {
119   bt_property_t property;
120   property.len = sizeof(bt_bdaddr_t);
121   property.val = (void*)address;
122   property.type = BT_PROPERTY_BDADDR;
123
124   NotifyAdapterPropertiesChanged(1, &property);
125 }
126
127 void FakeBluetoothInterface::NotifyAdapterLocalLeFeaturesPropertyChanged(
128       const bt_local_le_features_t* features) {
129   bt_property_t property;
130   property.len = sizeof(*features);
131   property.val = (void*)features;
132   property.type = BT_PROPERTY_LOCAL_LE_FEATURES;
133
134   NotifyAdapterPropertiesChanged(1, &property);
135 }
136
137 void FakeBluetoothInterface::NotifyAclStateChangedCallback(
138     bt_status_t status,
139     const bt_bdaddr_t& remote_bdaddr,
140     bt_acl_state_t state) {
141   FOR_EACH_OBSERVER(
142       Observer, observers_,
143       AclStateChangedCallback(status, remote_bdaddr, state));
144 }
145
146 void FakeBluetoothInterface::AddObserver(Observer* observer) {
147   observers_.AddObserver(observer);
148 }
149
150 void FakeBluetoothInterface::RemoveObserver(Observer* observer) {
151   observers_.RemoveObserver(observer);
152 }
153
154 const bt_interface_t* FakeBluetoothInterface::GetHALInterface() const {
155   return &fake_bt_iface;
156 }
157
158 const bluetooth_device_t* FakeBluetoothInterface::GetHALAdapter() const {
159   // TODO(armansito): Do something meaningful here to simulate test behavior.
160   return nullptr;
161 }
162
163 }  // namespace hal
164 }  // namespace bluetooth