Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(127)

Side by Side Diff: device/bluetooth/dbus/fake_bluetooth_media_transport_client.cc

Issue 1347193004: Refactor DBusThreadManager to split away BT clients. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chromeos/dbus/fake_bluetooth_media_transport_client.h" 5 #include "device/bluetooth/dbus/fake_bluetooth_media_transport_client.h"
6 6
7 #include <unistd.h> 7 #include <unistd.h>
8 #include <sys/socket.h> 8 #include <sys/socket.h>
9 9
10 #include <sstream> 10 #include <sstream>
11 11
12 #include "base/bind.h" 12 #include "base/bind.h"
13 #include "base/stl_util.h" 13 #include "base/stl_util.h"
14 #include "chromeos/dbus/bluetooth_media_client.h"
15 #include "chromeos/dbus/dbus_thread_manager.h"
16 #include "chromeos/dbus/fake_bluetooth_adapter_client.h"
17 #include "chromeos/dbus/fake_bluetooth_media_client.h"
18 #include "chromeos/dbus/fake_bluetooth_media_endpoint_service_provider.h"
19 #include "dbus/file_descriptor.h" 14 #include "dbus/file_descriptor.h"
15 #include "device/bluetooth/dbus/bluetooth_media_client.h"
16 #include "device/bluetooth/dbus/bluez_dbus_manager.h"
17 #include "device/bluetooth/dbus/fake_bluetooth_adapter_client.h"
18 #include "device/bluetooth/dbus/fake_bluetooth_media_client.h"
19 #include "device/bluetooth/dbus/fake_bluetooth_media_endpoint_service_provider.h "
20 20
21 using dbus::ObjectPath; 21 using dbus::ObjectPath;
22 22
23 namespace { 23 namespace {
24 24
25 // TODO(mcchou): Remove this constants once it is in cros_system_api. 25 // TODO(mcchou): Remove this constants once it is in cros_system_api.
26 const char kBluetoothMediaTransportInterface[] = "org.bluez.MediaTransport1"; 26 const char kBluetoothMediaTransportInterface[] = "org.bluez.MediaTransport1";
27 const char kNotImplemented[] = "org.bluez.NotImplemented"; 27 const char kNotImplemented[] = "org.bluez.NotImplemented";
28 const char kNotAuthorized[] = "org.bluez.NotAuthorized"; 28 const char kNotAuthorized[] = "org.bluez.NotAuthorized";
29 const char kFailed[] = "org.bluez.Failed"; 29 const char kFailed[] = "org.bluez.Failed";
30 const char kNotAvailable[] = "org.bluez.NotAvailable"; 30 const char kNotAvailable[] = "org.bluez.NotAvailable";
31 31
32 const int kInvalidFd = -1; 32 const int kInvalidFd = -1;
33 33
34 ObjectPath GenerateTransportPath() { 34 ObjectPath GenerateTransportPath() {
35 static unsigned int sequence_number = 0; 35 static unsigned int sequence_number = 0;
36 ++sequence_number; 36 ++sequence_number;
37 std::stringstream path; 37 std::stringstream path;
38 path << chromeos::FakeBluetoothAdapterClient::kAdapterPath 38 path << bluez::FakeBluetoothAdapterClient::kAdapterPath
39 << chromeos::FakeBluetoothMediaTransportClient::kTransportDevicePath 39 << bluez::FakeBluetoothMediaTransportClient::kTransportDevicePath
40 << "/fd" << sequence_number; 40 << "/fd" << sequence_number;
41 return ObjectPath(path.str()); 41 return ObjectPath(path.str());
42 } 42 }
43 43
44 } // namespace 44 } // namespace
45 45
46 namespace chromeos { 46 namespace bluez {
47 47
48 // static 48 // static
49 const char FakeBluetoothMediaTransportClient::kTransportDevicePath[] = 49 const char FakeBluetoothMediaTransportClient::kTransportDevicePath[] =
50 "/fake_audio_source"; 50 "/fake_audio_source";
51 const uint8_t FakeBluetoothMediaTransportClient::kTransportCodec = 0x00; 51 const uint8_t FakeBluetoothMediaTransportClient::kTransportCodec = 0x00;
52 const std::vector<uint8_t> 52 const std::vector<uint8_t>
53 FakeBluetoothMediaTransportClient::kTransportConfiguration = { 53 FakeBluetoothMediaTransportClient::kTransportConfiguration = {0x21, 0x15,
54 0x21, 0x15, 0x33, 0x2C}; 54 0x33, 0x2C};
55 const uint16_t FakeBluetoothMediaTransportClient::kTransportDelay = 5; 55 const uint16_t FakeBluetoothMediaTransportClient::kTransportDelay = 5;
56 const uint16_t FakeBluetoothMediaTransportClient::kTransportVolume = 50; 56 const uint16_t FakeBluetoothMediaTransportClient::kTransportVolume = 50;
57 const uint16_t FakeBluetoothMediaTransportClient::kDefaultReadMtu = 20; 57 const uint16_t FakeBluetoothMediaTransportClient::kDefaultReadMtu = 20;
58 const uint16_t FakeBluetoothMediaTransportClient::kDefaultWriteMtu = 25; 58 const uint16_t FakeBluetoothMediaTransportClient::kDefaultWriteMtu = 25;
59 59
60 FakeBluetoothMediaTransportClient::Properties::Properties( 60 FakeBluetoothMediaTransportClient::Properties::Properties(
61 const PropertyChangedCallback& callback) 61 const PropertyChangedCallback& callback)
62 : BluetoothMediaTransportClient::Properties( 62 : BluetoothMediaTransportClient::Properties(
63 nullptr, 63 nullptr,
64 kBluetoothMediaTransportInterface, 64 kBluetoothMediaTransportInterface,
65 callback) { 65 callback) {}
66 }
67 66
68 FakeBluetoothMediaTransportClient::Properties::~Properties() { 67 FakeBluetoothMediaTransportClient::Properties::~Properties() {}
69 }
70 68
71 void FakeBluetoothMediaTransportClient::Properties::Get( 69 void FakeBluetoothMediaTransportClient::Properties::Get(
72 dbus::PropertyBase* property, 70 dbus::PropertyBase* property,
73 dbus::PropertySet::GetCallback callback) { 71 dbus::PropertySet::GetCallback callback) {
74 VLOG(1) << "Get " << property->name(); 72 VLOG(1) << "Get " << property->name();
75 callback.Run(false); 73 callback.Run(false);
76 } 74 }
77 75
78 void FakeBluetoothMediaTransportClient::Properties::GetAll() { 76 void FakeBluetoothMediaTransportClient::Properties::GetAll() {
79 VLOG(1) << "GetAll called."; 77 VLOG(1) << "GetAll called.";
80 } 78 }
81 79
82 void FakeBluetoothMediaTransportClient::Properties::Set( 80 void FakeBluetoothMediaTransportClient::Properties::Set(
83 dbus::PropertyBase* property, 81 dbus::PropertyBase* property,
84 dbus::PropertySet::SetCallback callback) { 82 dbus::PropertySet::SetCallback callback) {
85 VLOG(1) << "Set " << property->name(); 83 VLOG(1) << "Set " << property->name();
86 callback.Run(false); 84 callback.Run(false);
87 } 85 }
88 86
89 FakeBluetoothMediaTransportClient::Transport::Transport( 87 FakeBluetoothMediaTransportClient::Transport::Transport(
90 const ObjectPath& transport_path, 88 const ObjectPath& transport_path,
91 Properties* transport_properties) 89 Properties* transport_properties)
92 : path(transport_path) { 90 : path(transport_path) {
93 properties.reset(transport_properties); 91 properties.reset(transport_properties);
94 } 92 }
95 93
96 FakeBluetoothMediaTransportClient::Transport::~Transport() { 94 FakeBluetoothMediaTransportClient::Transport::~Transport() {}
97 }
98 95
99 FakeBluetoothMediaTransportClient::FakeBluetoothMediaTransportClient() { 96 FakeBluetoothMediaTransportClient::FakeBluetoothMediaTransportClient() {}
100 }
101 97
102 FakeBluetoothMediaTransportClient::~FakeBluetoothMediaTransportClient() { 98 FakeBluetoothMediaTransportClient::~FakeBluetoothMediaTransportClient() {
103 STLDeleteValues(&endpoint_to_transport_map_); 99 STLDeleteValues(&endpoint_to_transport_map_);
104 } 100 }
105 101
106 // DBusClient override. 102 // DBusClient override.
107 void FakeBluetoothMediaTransportClient::Init(dbus::Bus* bus) { 103 void FakeBluetoothMediaTransportClient::Init(dbus::Bus* bus) {}
108 }
109 104
110 void FakeBluetoothMediaTransportClient::AddObserver( 105 void FakeBluetoothMediaTransportClient::AddObserver(
111 BluetoothMediaTransportClient::Observer* observer) { 106 BluetoothMediaTransportClient::Observer* observer) {
112 observers_.AddObserver(observer); 107 observers_.AddObserver(observer);
113 } 108 }
114 109
115 void FakeBluetoothMediaTransportClient::RemoveObserver( 110 void FakeBluetoothMediaTransportClient::RemoveObserver(
116 BluetoothMediaTransportClient::Observer* observer) { 111 BluetoothMediaTransportClient::Observer* observer) {
117 observers_.RemoveObserver(observer); 112 observers_.RemoveObserver(observer);
118 } 113 }
(...skipping 28 matching lines...) Expand all
147 const ObjectPath& object_path, 142 const ObjectPath& object_path,
148 const base::Closure& callback, 143 const base::Closure& callback,
149 const ErrorCallback& error_callback) { 144 const ErrorCallback& error_callback) {
150 error_callback.Run(kNotImplemented, ""); 145 error_callback.Run(kNotImplemented, "");
151 } 146 }
152 147
153 void FakeBluetoothMediaTransportClient::SetValid( 148 void FakeBluetoothMediaTransportClient::SetValid(
154 FakeBluetoothMediaEndpointServiceProvider* endpoint, 149 FakeBluetoothMediaEndpointServiceProvider* endpoint,
155 bool valid) { 150 bool valid) {
156 FakeBluetoothMediaClient* media = static_cast<FakeBluetoothMediaClient*>( 151 FakeBluetoothMediaClient* media = static_cast<FakeBluetoothMediaClient*>(
157 DBusThreadManager::Get()->GetBluetoothMediaClient()); 152 bluez::BluezDBusManager::Get()->GetBluetoothMediaClient());
158 DCHECK(media); 153 DCHECK(media);
159 154
160 ObjectPath endpoint_path(endpoint->object_path()); 155 ObjectPath endpoint_path(endpoint->object_path());
161 if (!media->IsRegistered(endpoint_path)) 156 if (!media->IsRegistered(endpoint_path))
162 return; 157 return;
163 158
164 if (valid) { 159 if (valid) {
165 ObjectPath transport_path = GenerateTransportPath(); 160 ObjectPath transport_path = GenerateTransportPath();
166 VLOG(1) << "New transport, " << transport_path.value() 161 VLOG(1) << "New transport, " << transport_path.value()
167 << " is created for endpoint " << endpoint_path.value(); 162 << " is created for endpoint " << endpoint_path.value();
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 void FakeBluetoothMediaTransportClient::SetState( 198 void FakeBluetoothMediaTransportClient::SetState(
204 const ObjectPath& endpoint_path, 199 const ObjectPath& endpoint_path,
205 const std::string& state) { 200 const std::string& state) {
206 VLOG(1) << "SetState - state: " << state; 201 VLOG(1) << "SetState - state: " << state;
207 202
208 Transport* transport = GetTransport(endpoint_path); 203 Transport* transport = GetTransport(endpoint_path);
209 if (!transport) 204 if (!transport)
210 return; 205 return;
211 206
212 transport->properties->state.ReplaceValue(state); 207 transport->properties->state.ReplaceValue(state);
213 FOR_EACH_OBSERVER(BluetoothMediaTransportClient::Observer, observers_, 208 FOR_EACH_OBSERVER(
214 MediaTransportPropertyChanged( 209 BluetoothMediaTransportClient::Observer, observers_,
215 transport->path, 210 MediaTransportPropertyChanged(
216 BluetoothMediaTransportClient::kStateProperty)); 211 transport->path, BluetoothMediaTransportClient::kStateProperty));
217 } 212 }
218 213
219 void FakeBluetoothMediaTransportClient::SetVolume( 214 void FakeBluetoothMediaTransportClient::SetVolume(
220 const ObjectPath& endpoint_path, 215 const ObjectPath& endpoint_path,
221 const uint16_t& volume) { 216 const uint16_t& volume) {
222 Transport* transport = GetTransport(endpoint_path); 217 Transport* transport = GetTransport(endpoint_path);
223 if (!transport) 218 if (!transport)
224 return; 219 return;
225 220
226 transport->properties->volume.ReplaceValue(volume); 221 transport->properties->volume.ReplaceValue(volume);
227 FOR_EACH_OBSERVER(BluetoothMediaTransportClient::Observer, observers_, 222 FOR_EACH_OBSERVER(
228 MediaTransportPropertyChanged( 223 BluetoothMediaTransportClient::Observer, observers_,
229 transport->path, 224 MediaTransportPropertyChanged(
230 BluetoothMediaTransportClient::kVolumeProperty)); 225 transport->path, BluetoothMediaTransportClient::kVolumeProperty));
231 } 226 }
232 227
233 void FakeBluetoothMediaTransportClient::WriteData( 228 void FakeBluetoothMediaTransportClient::WriteData(
234 const ObjectPath& endpoint_path, const std::vector<char>& bytes) { 229 const ObjectPath& endpoint_path,
230 const std::vector<char>& bytes) {
235 VLOG(1) << "WriteData - write " << bytes.size() << " bytes"; 231 VLOG(1) << "WriteData - write " << bytes.size() << " bytes";
236 232
237 Transport* transport = GetTransport(endpoint_path); 233 Transport* transport = GetTransport(endpoint_path);
238 234
239 if (!transport || transport->properties->state.value() != "active") { 235 if (!transport || transport->properties->state.value() != "active") {
240 VLOG(1) << "WriteData - write operation rejected, since the state isn't " 236 VLOG(1) << "WriteData - write operation rejected, since the state isn't "
241 "active for endpoint: " << endpoint_path.value(); 237 "active for endpoint: "
238 << endpoint_path.value();
242 return; 239 return;
243 } 240 }
244 241
245 if (!transport->input_fd.get()) { 242 if (!transport->input_fd.get()) {
246 VLOG(1) << "WriteData - invalid input file descriptor"; 243 VLOG(1) << "WriteData - invalid input file descriptor";
247 return; 244 return;
248 } 245 }
249 246
250 ssize_t written_len = 247 ssize_t written_len =
251 write(transport->input_fd->GetPlatformFile(), bytes.data(), bytes.size()); 248 write(transport->input_fd->GetPlatformFile(), bytes.data(), bytes.size());
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
316 return; 313 return;
317 } 314 }
318 DCHECK((fds[0] > kInvalidFd) && (fds[1] > kInvalidFd)); 315 DCHECK((fds[0] > kInvalidFd) && (fds[1] > kInvalidFd));
319 transport->input_fd.reset(new base::File(fds[0])); 316 transport->input_fd.reset(new base::File(fds[0]));
320 317
321 dbus::FileDescriptor out_fd(fds[1]); 318 dbus::FileDescriptor out_fd(fds[1]);
322 callback.Run(&out_fd, kDefaultReadMtu, kDefaultWriteMtu); 319 callback.Run(&out_fd, kDefaultReadMtu, kDefaultWriteMtu);
323 SetState(endpoint_path, "active"); 320 SetState(endpoint_path, "active");
324 } 321 }
325 322
326 } // namespace chromeos 323 } // namespace bluez
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698