ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / chromeos / dbus / fake_bluetooth_media_transport_client.cc
blobd503aa04d6b36894868e7ad9ae8cd296f1976637
1 // Copyright 2014 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.
5 #include "chromeos/dbus/fake_bluetooth_media_transport_client.h"
7 #include <sstream>
9 #include "base/bind.h"
10 #include "base/stl_util.h"
11 #include "chromeos/dbus/bluetooth_media_client.h"
12 #include "chromeos/dbus/dbus_thread_manager.h"
13 #include "chromeos/dbus/fake_bluetooth_adapter_client.h"
14 #include "chromeos/dbus/fake_bluetooth_media_client.h"
15 #include "chromeos/dbus/fake_bluetooth_media_endpoint_service_provider.h"
17 using dbus::ObjectPath;
19 namespace {
21 // TODO(mcchou): Remove this constants once it is in cros_system_api.
22 const char kBluetoothMediaTransportInterface[] = "org.bluez.MediaTransport1";
23 const char kNotImplemented[] = "org.bluez.NotImplemented";
25 ObjectPath GenerateTransportPath() {
26 static unsigned int sequence_number = 0;
27 ++sequence_number;
28 std::stringstream path;
29 path << chromeos::FakeBluetoothAdapterClient::kAdapterPath
30 << chromeos::FakeBluetoothMediaTransportClient::kTransportDevicePath
31 << "/fd" << sequence_number;
32 return ObjectPath(path.str());
35 } // namespace
37 namespace chromeos {
39 // static
40 const char FakeBluetoothMediaTransportClient::kTransportDevicePath[] =
41 "/fake_audio_source";
42 const uint8_t FakeBluetoothMediaTransportClient::kTransportCodec = 0x00;
43 const std::vector<uint8_t>
44 FakeBluetoothMediaTransportClient::kTransportConfiguration = {
45 0x21, 0x15, 0x33, 0x2C};
46 const uint16_t FakeBluetoothMediaTransportClient::kTransportDelay = 5;
47 const uint16_t FakeBluetoothMediaTransportClient::kTransportVolume = 50;
49 FakeBluetoothMediaTransportClient::Properties::Properties(
50 const PropertyChangedCallback& callback)
51 : BluetoothMediaTransportClient::Properties(
52 nullptr,
53 kBluetoothMediaTransportInterface,
54 callback) {
57 FakeBluetoothMediaTransportClient::Properties::~Properties() {
60 void FakeBluetoothMediaTransportClient::Properties::Get(
61 dbus::PropertyBase* property,
62 dbus::PropertySet::GetCallback callback) {
63 VLOG(1) << "Get " << property->name();
64 callback.Run(false);
67 void FakeBluetoothMediaTransportClient::Properties::GetAll() {
68 VLOG(1) << "GetAll called.";
71 void FakeBluetoothMediaTransportClient::Properties::Set(
72 dbus::PropertyBase* property,
73 dbus::PropertySet::SetCallback callback) {
74 VLOG(1) << "Set " << property->name();
75 callback.Run(false);
78 FakeBluetoothMediaTransportClient::Transport::Transport(
79 const ObjectPath& transport_path,
80 Properties* transport_properties)
81 : path(transport_path) {
82 properties.reset(transport_properties);
85 FakeBluetoothMediaTransportClient::Transport::~Transport() {
88 FakeBluetoothMediaTransportClient::FakeBluetoothMediaTransportClient() {
91 FakeBluetoothMediaTransportClient::~FakeBluetoothMediaTransportClient() {
92 for (auto& it : endpoint_to_transport_map_)
93 delete it.second;
94 endpoint_to_transport_map_.clear();
97 // DBusClient override.
98 void FakeBluetoothMediaTransportClient::Init(dbus::Bus* bus) {
101 void FakeBluetoothMediaTransportClient::AddObserver(
102 BluetoothMediaTransportClient::Observer* observer) {
103 observers_.AddObserver(observer);
106 void FakeBluetoothMediaTransportClient::RemoveObserver(
107 BluetoothMediaTransportClient::Observer* observer) {
108 observers_.RemoveObserver(observer);
111 FakeBluetoothMediaTransportClient::Properties*
112 FakeBluetoothMediaTransportClient::GetProperties(
113 const ObjectPath& object_path) {
114 ObjectPath endpoint_path = GetEndpointPath(object_path);
115 if (!endpoint_path.IsValid() ||
116 !ContainsKey(endpoint_to_transport_map_, endpoint_path))
117 return nullptr;
118 return endpoint_to_transport_map_[endpoint_path]->properties.get();
121 void FakeBluetoothMediaTransportClient::Acquire(
122 const ObjectPath& object_path,
123 const AcquireCallback& callback,
124 const ErrorCallback& error_callback) {
125 error_callback.Run(kNotImplemented, "");
128 void FakeBluetoothMediaTransportClient::TryAcquire(
129 const ObjectPath& object_path,
130 const AcquireCallback& callback,
131 const ErrorCallback& error_callback) {
132 error_callback.Run(kNotImplemented, "");
135 void FakeBluetoothMediaTransportClient::Release(
136 const ObjectPath& object_path,
137 const base::Closure& callback,
138 const ErrorCallback& error_callback) {
139 error_callback.Run(kNotImplemented, "");
142 void FakeBluetoothMediaTransportClient::SetValid(
143 FakeBluetoothMediaEndpointServiceProvider* endpoint,
144 bool valid) {
145 FakeBluetoothMediaClient* media = static_cast<FakeBluetoothMediaClient*>(
146 DBusThreadManager::Get()->GetBluetoothMediaClient());
147 DCHECK(media);
149 ObjectPath endpoint_path(endpoint->object_path());
150 if (!media->IsRegistered(endpoint_path))
151 return;
153 if (valid) {
154 ObjectPath transport_path = GenerateTransportPath();
155 VLOG(1) << "New transport, " << transport_path.value()
156 << " is created for endpoint " << endpoint_path.value();
158 // Sets the fake property set with default values.
159 scoped_ptr<Properties> properties(new Properties(
160 base::Bind(&FakeBluetoothMediaTransportClient::OnPropertyChanged,
161 base::Unretained(this))));
162 properties->device.ReplaceValue(ObjectPath(kTransportDevicePath));
163 properties->uuid.ReplaceValue(
164 BluetoothMediaClient::kBluetoothAudioSinkUUID);
165 properties->codec.ReplaceValue(kTransportCodec);
166 properties->configuration.ReplaceValue(kTransportConfiguration);
167 properties->state.ReplaceValue(BluetoothMediaTransportClient::kStateIdle);
168 properties->delay.ReplaceValue(kTransportDelay);
169 properties->volume.ReplaceValue(kTransportVolume);
171 endpoint_to_transport_map_[endpoint_path] =
172 new Transport(transport_path, properties.release());
173 transport_to_endpoint_map_[transport_path] = endpoint_path;
174 return;
177 if (!ContainsKey(endpoint_to_transport_map_, endpoint_path))
178 return;
180 // Notifies observers about the state change of the transport.
181 FOR_EACH_OBSERVER(BluetoothMediaTransportClient::Observer, observers_,
182 MediaTransportRemoved(GetTransportPath(endpoint_path)));
184 endpoint->ClearConfiguration(GetTransportPath(endpoint_path));
185 transport_to_endpoint_map_.erase(GetTransportPath(endpoint_path));
186 delete endpoint_to_transport_map_[endpoint_path];
187 endpoint_to_transport_map_.erase(endpoint_path);
190 void FakeBluetoothMediaTransportClient::SetState(
191 const dbus::ObjectPath& endpoint_path,
192 const std::string& state) {
193 if (!ContainsKey(endpoint_to_transport_map_, endpoint_path))
194 return;
196 endpoint_to_transport_map_[endpoint_path]
197 ->properties->state.ReplaceValue(state);
198 FOR_EACH_OBSERVER(BluetoothMediaTransportClient::Observer, observers_,
199 MediaTransportPropertyChanged(
200 GetTransportPath(endpoint_path),
201 BluetoothMediaTransportClient::kStateProperty));
204 void FakeBluetoothMediaTransportClient::SetVolume(
205 const dbus::ObjectPath& endpoint_path,
206 const uint16_t& volume) {
207 if (!ContainsKey(endpoint_to_transport_map_, endpoint_path))
208 return;
210 endpoint_to_transport_map_[endpoint_path]->properties->volume.ReplaceValue(
211 volume);
212 FOR_EACH_OBSERVER(BluetoothMediaTransportClient::Observer, observers_,
213 MediaTransportPropertyChanged(
214 GetTransportPath(endpoint_path),
215 BluetoothMediaTransportClient::kVolumeProperty));
218 ObjectPath FakeBluetoothMediaTransportClient::GetTransportPath(
219 const ObjectPath& endpoint_path) {
220 if (ContainsKey(endpoint_to_transport_map_, endpoint_path))
221 return endpoint_to_transport_map_[endpoint_path]->path;
222 return ObjectPath("");
225 ObjectPath FakeBluetoothMediaTransportClient::GetEndpointPath(
226 const ObjectPath& transport_path) {
227 if (ContainsKey(transport_to_endpoint_map_, transport_path))
228 return transport_to_endpoint_map_[transport_path];
229 return ObjectPath("");
232 void FakeBluetoothMediaTransportClient::OnPropertyChanged(
233 const std::string& property_name) {
234 VLOG(1) << "Property " << property_name << " changed";
237 } // namespace chromeos