OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chromeos/dbus/fake_bluetooth_adapter_client.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/logging.h" | |
9 #include "base/message_loop.h" | |
10 #include "base/time.h" | |
11 #include "chromeos/dbus/dbus_thread_manager.h" | |
12 #include "chromeos/dbus/fake_bluetooth_device_client.h" | |
13 #include "dbus/object_path.h" | |
14 #include "third_party/cros_system_api/dbus/service_constants.h" | |
15 | |
16 namespace { | |
17 | |
18 // Amount of time to wait after a command before performing it. | |
19 const int kCommandEffectsTimeMs = 500; | |
20 | |
21 } | |
22 | |
23 namespace chromeos { | |
24 | |
25 const dbus::ObjectPath FakeBluetoothAdapterClient::kAdapterPath( | |
Haruki Sato
2013/04/16 04:59:45
could you avoid having ObjectPath and use const ch
keybuk
2013/04/16 23:19:13
Done.
| |
26 "/fake/hci0"); | |
27 const char FakeBluetoothAdapterClient::kAdapterName[] = | |
28 "Fake Adapter"; | |
29 const char FakeBluetoothAdapterClient::kAdapterAddress[] = | |
30 "01:1a:2b:1a:2b:03"; | |
31 | |
32 const dbus::ObjectPath FakeBluetoothAdapterClient::kSecondAdapterPath( | |
Haruki Sato
2013/04/16 04:59:45
ditto.
keybuk
2013/04/16 23:19:13
Done.
| |
33 "/fake/hci1"); | |
34 const char FakeBluetoothAdapterClient::kSecondAdapterName[] = | |
35 "Second Fake Adapter"; | |
36 const char FakeBluetoothAdapterClient::kSecondAdapterAddress[] = | |
37 "00:de:51:10:01:00"; | |
38 | |
39 FakeBluetoothAdapterClient::Properties::Properties( | |
40 const PropertyChangedCallback& callback) | |
41 : ExperimentalBluetoothAdapterClient::Properties( | |
42 NULL, | |
43 bluetooth_adapter::kExperimentalBluetoothAdapterInterface, | |
44 callback) { | |
45 } | |
46 | |
47 FakeBluetoothAdapterClient::Properties::~Properties() { | |
48 } | |
49 | |
50 void FakeBluetoothAdapterClient::Properties::Get( | |
51 dbus::PropertyBase* property, | |
52 dbus::PropertySet::GetCallback callback) { | |
53 VLOG(1) << "Get " << property->name(); | |
54 callback.Run(false); | |
55 } | |
56 | |
57 void FakeBluetoothAdapterClient::Properties::GetAll() { | |
58 VLOG(1) << "GetAll"; | |
59 } | |
60 | |
61 void FakeBluetoothAdapterClient::Properties::Set( | |
62 dbus::PropertyBase *property, | |
63 dbus::PropertySet::SetCallback callback) { | |
64 VLOG(1) << "Set " << property->name(); | |
65 if (property->name() == powered.name() || property->name() == alias.name()) { | |
66 callback.Run(true); | |
67 property->ReplaceValueWithSetValue(); | |
68 NotifyPropertyChanged(property->name()); | |
69 } else { | |
70 callback.Run(false); | |
71 } | |
72 } | |
73 | |
74 | |
75 FakeBluetoothAdapterClient::FakeBluetoothAdapterClient() | |
76 : visible_(true), | |
77 second_visible_(false), | |
78 discovering_count_(0) { | |
79 properties_.reset(new Properties(base::Bind( | |
80 &FakeBluetoothAdapterClient::OnPropertyChanged, | |
81 base::Unretained(this)))); | |
82 | |
83 properties_->address.ReplaceValue(kAdapterAddress); | |
84 properties_->name.ReplaceValue("Fake Adapter (Name)"); | |
85 properties_->alias.ReplaceValue(kAdapterName); | |
86 properties_->pairable.ReplaceValue(true); | |
87 | |
88 second_properties_.reset(new Properties(base::Bind( | |
89 &FakeBluetoothAdapterClient::OnPropertyChanged, | |
90 base::Unretained(this)))); | |
91 | |
92 second_properties_->address.ReplaceValue(kSecondAdapterAddress); | |
93 second_properties_->name.ReplaceValue("Second Fake Adapter (Name)"); | |
94 second_properties_->alias.ReplaceValue(kSecondAdapterName); | |
95 second_properties_->pairable.ReplaceValue(true); | |
96 } | |
97 | |
98 FakeBluetoothAdapterClient::~FakeBluetoothAdapterClient() { | |
99 } | |
100 | |
101 void FakeBluetoothAdapterClient::AddObserver(Observer* observer) { | |
102 observers_.AddObserver(observer); | |
103 } | |
104 | |
105 void FakeBluetoothAdapterClient::RemoveObserver(Observer* observer) { | |
106 observers_.RemoveObserver(observer); | |
107 } | |
108 | |
109 std::vector<dbus::ObjectPath> FakeBluetoothAdapterClient::GetAdapters() { | |
110 std::vector<dbus::ObjectPath> object_paths; | |
111 if (visible_) | |
112 object_paths.push_back(kAdapterPath); | |
113 if (second_visible_) | |
114 object_paths.push_back(kSecondAdapterPath); | |
115 return object_paths; | |
116 } | |
117 | |
118 FakeBluetoothAdapterClient::Properties* | |
119 FakeBluetoothAdapterClient::GetProperties(const dbus::ObjectPath& object_path) { | |
120 if (object_path == kAdapterPath) | |
121 return properties_.get(); | |
122 else if (object_path == kSecondAdapterPath) | |
123 return second_properties_.get(); | |
124 else | |
125 return NULL; | |
126 } | |
127 | |
128 void FakeBluetoothAdapterClient::StartDiscovery( | |
129 const dbus::ObjectPath& object_path, | |
130 const base::Closure& callback, | |
131 const ErrorCallback& error_callback) { | |
132 if (object_path != kAdapterPath) { | |
133 error_callback.Run(kNoResponseError, ""); | |
134 return; | |
135 } | |
136 | |
137 ++discovering_count_; | |
138 VLOG(1) << "StartDiscovery: " << object_path.value() << ", " | |
139 << "count is now " << discovering_count_; | |
140 callback.Run(); | |
141 | |
142 if (discovering_count_ == 1) { | |
143 properties_->discovering.ReplaceValue(true); | |
144 properties_->NotifyPropertyChanged(properties_->discovering.name()); | |
145 | |
146 FakeBluetoothDeviceClient* device_client = | |
147 static_cast<FakeBluetoothDeviceClient*>( | |
148 DBusThreadManager::Get()->GetExperimentalBluetoothDeviceClient()); | |
149 device_client->BeginDiscoverySimulation(kAdapterPath); | |
150 } | |
151 } | |
152 | |
153 void FakeBluetoothAdapterClient::StopDiscovery( | |
154 const dbus::ObjectPath& object_path, | |
155 const base::Closure& callback, | |
156 const ErrorCallback& error_callback) { | |
157 if (object_path != kAdapterPath) { | |
158 error_callback.Run(kNoResponseError, ""); | |
159 return; | |
160 } | |
161 | |
162 if (!discovering_count_) { | |
163 LOG(WARNING) << "StopDiscovery called when not discovering"; | |
164 error_callback.Run(kNoResponseError, ""); | |
165 return; | |
166 } | |
167 | |
168 --discovering_count_; | |
169 VLOG(1) << "StopDiscovery: " << object_path.value() << ", " | |
170 << "count is now " << discovering_count_; | |
171 callback.Run(); | |
172 | |
173 if (discovering_count_ == 0) { | |
174 FakeBluetoothDeviceClient* device_client = | |
175 static_cast<FakeBluetoothDeviceClient*>( | |
176 DBusThreadManager::Get()->GetExperimentalBluetoothDeviceClient()); | |
177 device_client->EndDiscoverySimulation(kAdapterPath); | |
178 | |
179 properties_->discovering.ReplaceValue(false); | |
180 properties_->NotifyPropertyChanged(properties_->discovering.name()); | |
181 } | |
182 } | |
183 | |
184 void FakeBluetoothAdapterClient::RemoveDevice( | |
185 const dbus::ObjectPath& object_path, | |
186 const dbus::ObjectPath& device_path, | |
187 const base::Closure& callback, | |
188 const ErrorCallback& error_callback) { | |
189 if (object_path != kAdapterPath) { | |
190 error_callback.Run(kNoResponseError, ""); | |
191 return; | |
192 } | |
193 | |
194 VLOG(1) << "RemoveDevice: " << object_path.value() | |
195 << " " << device_path.value(); | |
196 callback.Run(); | |
197 | |
198 FakeBluetoothDeviceClient* device_client = | |
199 static_cast<FakeBluetoothDeviceClient*>( | |
200 DBusThreadManager::Get()->GetExperimentalBluetoothDeviceClient()); | |
201 device_client->RemoveDevice(kAdapterPath, device_path); | |
202 } | |
203 | |
204 void FakeBluetoothAdapterClient::SetVisible( | |
205 bool visible) { | |
206 if (visible && !visible_) { | |
207 // Adapter becoming visible | |
208 visible_ = visible; | |
209 | |
210 FOR_EACH_OBSERVER(ExperimentalBluetoothAdapterClient::Observer, observers_, | |
211 AdapterAdded(kAdapterPath)); | |
212 | |
213 } else if (visible_ && !visible) { | |
214 // Adapter becoming invisible | |
215 visible_ = visible; | |
216 | |
217 FOR_EACH_OBSERVER(ExperimentalBluetoothAdapterClient::Observer, observers_, | |
218 AdapterRemoved(kAdapterPath)); | |
219 } | |
220 } | |
221 | |
222 void FakeBluetoothAdapterClient::SetSecondVisible( | |
223 bool visible) { | |
224 if (visible && !second_visible_) { | |
225 // Second adapter becoming visible | |
226 second_visible_ = visible; | |
227 | |
228 FOR_EACH_OBSERVER(ExperimentalBluetoothAdapterClient::Observer, observers_, | |
229 AdapterAdded(kSecondAdapterPath)); | |
230 | |
231 } else if (second_visible_ && !visible) { | |
232 // Second adapter becoming invisible | |
233 second_visible_ = visible; | |
234 | |
235 FOR_EACH_OBSERVER(ExperimentalBluetoothAdapterClient::Observer, observers_, | |
236 AdapterRemoved(kSecondAdapterPath)); | |
237 } | |
238 } | |
239 | |
240 void FakeBluetoothAdapterClient::OnPropertyChanged( | |
241 const std::string& property_name) { | |
242 if (property_name == properties_->powered.name() && | |
243 !properties_->powered.value()) { | |
244 VLOG(1) << "Adapter powered off"; | |
245 | |
246 if (discovering_count_) { | |
247 discovering_count_ = 0; | |
248 properties_->discovering.ReplaceValue(false); | |
249 properties_->NotifyPropertyChanged(properties_->discovering.name()); | |
250 } | |
251 } | |
252 | |
253 FOR_EACH_OBSERVER(ExperimentalBluetoothAdapterClient::Observer, observers_, | |
254 AdapterPropertyChanged(kAdapterPath, property_name)); | |
255 } | |
256 | |
257 } // namespace chromeos | |
OLD | NEW |