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

Side by Side Diff: chromeos/dbus/bluetooth_adapter_client.cc

Issue 10546010: Implement support for the OOB Pairing APIs. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 6 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/bluetooth_adapter_client.h" 5 #include "chromeos/dbus/bluetooth_adapter_client.h"
6 6
7 #include <map> 7 #include <map>
8 #include <utility>
8 9
9 #include "base/bind.h" 10 #include "base/bind.h"
10 #include "base/logging.h" 11 #include "base/logging.h"
11 #include "base/stl_util.h" 12 #include "base/stl_util.h"
12 #include "chromeos/dbus/bluetooth_device_client.h" 13 #include "chromeos/dbus/bluetooth_device_client.h"
13 #include "chromeos/dbus/bluetooth_manager_client.h" 14 #include "chromeos/dbus/bluetooth_manager_client.h"
15 #include "chromeos/dbus/bluetooth_out_of_band_pairing_data.h"
14 #include "chromeos/dbus/bluetooth_property.h" 16 #include "chromeos/dbus/bluetooth_property.h"
15 #include "dbus/bus.h" 17 #include "dbus/bus.h"
16 #include "dbus/message.h" 18 #include "dbus/message.h"
17 #include "dbus/object_path.h" 19 #include "dbus/object_path.h"
18 #include "dbus/object_proxy.h" 20 #include "dbus/object_proxy.h"
19 #include "third_party/cros_system_api/dbus/service_constants.h" 21 #include "third_party/cros_system_api/dbus/service_constants.h"
20 22
21 namespace chromeos { 23 namespace chromeos {
22 24
23 BluetoothAdapterClient::Properties::Properties(dbus::ObjectProxy* object_proxy, 25 BluetoothAdapterClient::Properties::Properties(dbus::ObjectProxy* object_proxy,
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after
291 293
292 dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path); 294 dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path);
293 295
294 object_proxy->CallMethod( 296 object_proxy->CallMethod(
295 &method_call, 297 &method_call,
296 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, 298 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
297 base::Bind(&BluetoothAdapterClientImpl::OnCreateDevice, 299 base::Bind(&BluetoothAdapterClientImpl::OnCreateDevice,
298 weak_ptr_factory_.GetWeakPtr(), object_path, callback)); 300 weak_ptr_factory_.GetWeakPtr(), object_path, callback));
299 } 301 }
300 302
303 // BluetoothAdapterClient override.
304 virtual void ReadLocalOutOfBandPairingData(
305 const dbus::ObjectPath& object_path,
306 const OutOfBandPairingDataCallback& callback) {
307 dbus::MethodCall method_call(
308 bluetooth_outofband::kBluetoothOutOfBandInterface,
309 bluetooth_outofband::kReadLocalData);
310
311 dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path);
312
313 object_proxy->CallMethod(
314 &method_call,
315 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
316 base::Bind(&BluetoothAdapterClientImpl::OnReadLocalData,
317 weak_ptr_factory_.GetWeakPtr(), object_path, callback));
318 }
319
320 // BluetoothAdapterClient override.
321 virtual void AddRemoteOutOfBandPairingData(
322 const dbus::ObjectPath& object_path,
323 const std::string& address,
324 const OutOfBandPairingData& data,
325 const AdapterCallback& callback) {
326 dbus::MethodCall method_call(
327 bluetooth_outofband::kBluetoothOutOfBandInterface,
328 bluetooth_outofband::kAddRemoteData);
329
330 dbus::MessageWriter writer(&method_call);
331 writer.AppendString(address);
332 writer.AppendArrayOfBytes(data.hash, kOutOfBandPairingDataSize);
333 writer.AppendArrayOfBytes(data.randomizer, kOutOfBandPairingDataSize);
334
335 dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path);
336
337 object_proxy->CallMethod(
338 &method_call,
339 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
340 base::Bind(&BluetoothAdapterClientImpl::ResponseToAdapterCallback,
341 weak_ptr_factory_.GetWeakPtr(), object_path, callback));
342 }
343
344 virtual void RemoveRemoteOutOfBandPairingData(
345 const dbus::ObjectPath& object_path,
346 const std::string& address,
347 const AdapterCallback& callback) {
348 dbus::MethodCall method_call(
349 bluetooth_outofband::kBluetoothOutOfBandInterface,
350 bluetooth_outofband::kRemoveRemoteData);
351
352 dbus::MessageWriter writer(&method_call);
353 writer.AppendString(address);
354
355 dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path);
356
357 object_proxy->CallMethod(
358 &method_call,
359 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
360 base::Bind(&BluetoothAdapterClientImpl::ResponseToAdapterCallback,
361 weak_ptr_factory_.GetWeakPtr(), object_path, callback));
362 }
363
301 private: 364 private:
302 // We maintain a collection of dbus object proxies and properties structures 365 // We maintain a collection of dbus object proxies and properties structures
303 // for each adapter. 366 // for each adapter.
304 typedef std::pair<dbus::ObjectProxy*, Properties*> Object; 367 typedef std::pair<dbus::ObjectProxy*, Properties*> Object;
305 typedef std::map<const dbus::ObjectPath, Object> ObjectMap; 368 typedef std::map<const dbus::ObjectPath, Object> ObjectMap;
306 ObjectMap object_map_; 369 ObjectMap object_map_;
307 370
308 // BluetoothManagerClient::Observer override. 371 // BluetoothManagerClient::Observer override.
309 virtual void AdapterAdded(const dbus::ObjectPath& object_path) OVERRIDE { 372 virtual void AdapterAdded(const dbus::ObjectPath& object_path) OVERRIDE {
310 } 373 }
(...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after
600 success = true; 663 success = true;
601 } 664 }
602 } else { 665 } else {
603 LOG(WARNING) << "Failed to create device."; 666 LOG(WARNING) << "Failed to create device.";
604 } 667 }
605 668
606 // Notify client. 669 // Notify client.
607 callback.Run(device_path, success); 670 callback.Run(device_path, success);
608 } 671 }
609 672
673 // Called when a response from ReadLocalOutOfBandPairingData() is received.
674 void OnReadLocalData(const dbus::ObjectPath& object_path,
675 const OutOfBandPairingDataCallback& callback,
676 dbus::Response* response) {
677 bool success = false;
678 OutOfBandPairingData data;
679 if (response != NULL) {
680 dbus::MessageReader reader(response);
681 uint8_t* bytes;
682 size_t length = kOutOfBandPairingDataSize;
683 if (reader.PopArrayOfBytes(&bytes, &length)) {
684 if (length == kOutOfBandPairingDataSize) {
685 memcpy(&data.hash, bytes, length);
686 if (reader.PopArrayOfBytes(&bytes, &length)) {
687 if (length == kOutOfBandPairingDataSize) {
688 memcpy(&data.randomizer, bytes, length);
689 success = true;
690 }
691 }
692 }
693 }
694 }
695 callback.Run(data, success);
696 }
697
698 // Translates a dbus::Response to an AdapterCallback by calling |callback|
699 // with |object_path| and assuming success if |response| is not NULL.
700 void ResponseToAdapterCallback(const dbus::ObjectPath& object_path,
701 const AdapterCallback& callback,
702 dbus::Response* response) {
703 callback.Run(object_path, response != NULL);
704 }
705
610 // Called when a response for CreatePairedDevice() is received. 706 // Called when a response for CreatePairedDevice() is received.
611 void OnCreatePairedDevice(const dbus::ObjectPath& object_path, 707 void OnCreatePairedDevice(const dbus::ObjectPath& object_path,
612 const DeviceCallback& callback, 708 const DeviceCallback& callback,
613 dbus::Response* response) { 709 dbus::Response* response) {
614 // Parse response. 710 // Parse response.
615 bool success = false; 711 bool success = false;
616 dbus::ObjectPath device_path; 712 dbus::ObjectPath device_path;
617 if (response != NULL) { 713 if (response != NULL) {
618 dbus::MessageReader reader(response); 714 dbus::MessageReader reader(response);
619 if (!reader.PopObjectPath(&device_path)) { 715 if (!reader.PopObjectPath(&device_path)) {
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
781 } 877 }
782 878
783 // BluetoothAdapterClient override. 879 // BluetoothAdapterClient override.
784 virtual void UnregisterAgent(const dbus::ObjectPath& object_path, 880 virtual void UnregisterAgent(const dbus::ObjectPath& object_path,
785 const dbus::ObjectPath& agent_path, 881 const dbus::ObjectPath& agent_path,
786 const AdapterCallback& callback) OVERRIDE { 882 const AdapterCallback& callback) OVERRIDE {
787 VLOG(1) << "UnregisterAgent: " << object_path.value() 883 VLOG(1) << "UnregisterAgent: " << object_path.value()
788 << " " << agent_path.value(); 884 << " " << agent_path.value();
789 callback.Run(object_path, false); 885 callback.Run(object_path, false);
790 } 886 }
887
888 virtual void ReadLocalOutOfBandPairingData(
889 const dbus::ObjectPath& object_path,
890 const OutOfBandPairingDataCallback& callback) {
891 VLOG(1) << "ReadLocalOutOfBandPairingData: " << object_path.value();
892 OutOfBandPairingData data;
893 callback.Run(data, false);
894 }
895
896 virtual void AddRemoteOutOfBandPairingData(
897 const dbus::ObjectPath& object_path,
898 const std::string& address,
899 const OutOfBandPairingData& data,
900 const AdapterCallback& callback) {
901 VLOG(1) << "AddRemoteData: " << object_path.value();
902 callback.Run(object_path, false);
903 }
904
905 virtual void RemoveRemoteOutOfBandPairingData(
906 const dbus::ObjectPath& object_path,
907 const std::string& address,
908 const AdapterCallback& callback) {
909 VLOG(1) << "RemoveRemoteData: " << object_path.value();
910 callback.Run(object_path, false);
911 }
791 }; 912 };
792 913
793 BluetoothAdapterClient::BluetoothAdapterClient() { 914 BluetoothAdapterClient::BluetoothAdapterClient() {
794 } 915 }
795 916
796 BluetoothAdapterClient::~BluetoothAdapterClient() { 917 BluetoothAdapterClient::~BluetoothAdapterClient() {
797 } 918 }
798 919
799 BluetoothAdapterClient* BluetoothAdapterClient::Create( 920 BluetoothAdapterClient* BluetoothAdapterClient::Create(
800 DBusClientImplementationType type, 921 DBusClientImplementationType type,
801 dbus::Bus* bus, 922 dbus::Bus* bus,
802 BluetoothManagerClient* manager_client) { 923 BluetoothManagerClient* manager_client) {
803 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) 924 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION)
804 return new BluetoothAdapterClientImpl(bus, manager_client); 925 return new BluetoothAdapterClientImpl(bus, manager_client);
805 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); 926 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type);
806 return new BluetoothAdapterClientStubImpl(); 927 return new BluetoothAdapterClientStubImpl();
807 } 928 }
808 929
809 } // namespace chromeos 930 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698