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

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

Issue 10546010: Implement support for the OOB Pairing APIs. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase 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
« no previous file with comments | « chromeos/dbus/bluetooth_out_of_band_client.h ('k') | chromeos/dbus/dbus_thread_manager.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chromeos/dbus/bluetooth_out_of_band_client.h"
6
7 #include <map>
8 #include <string>
9
10 #include "base/bind.h"
11 #include "base/logging.h"
12 #include "chromeos/dbus/bluetooth_adapter_client.h"
13 #include "dbus/bus.h"
14 #include "dbus/message.h"
15 #include "dbus/object_path.h"
16 #include "dbus/object_proxy.h"
17 #include "third_party/cros_system_api/dbus/service_constants.h"
18
19 namespace chromeos {
20
21 // The BluetoothOutOfBandClient implementation used in production.
22 class BluetoothOutOfBandClientImpl: public BluetoothOutOfBandClient {
23 public:
24 explicit BluetoothOutOfBandClientImpl(dbus::Bus* bus)
25 : weak_ptr_factory_(this),
26 bus_(bus) {}
27
28 virtual ~BluetoothOutOfBandClientImpl() {}
29
30 // BluetoothOutOfBandClient override.
31 virtual void ReadLocalData(
32 const dbus::ObjectPath& object_path,
33 const DataCallback& callback) OVERRIDE {
34 dbus::MethodCall method_call(
35 bluetooth_outofband::kBluetoothOutOfBandInterface,
36 bluetooth_outofband::kReadLocalData);
37
38 dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path);
39
40 object_proxy->CallMethod(
41 &method_call,
42 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
43 base::Bind(&BluetoothOutOfBandClientImpl::OnReadLocalData,
44 weak_ptr_factory_.GetWeakPtr(), callback));
45 }
46
47 // BluetoothOutOfBandClient override.
48 virtual void AddRemoteData(
49 const dbus::ObjectPath& object_path,
50 const std::string& address,
51 const BluetoothOutOfBandPairingData& data,
52 const SuccessCallback& callback) OVERRIDE {
53 dbus::MethodCall method_call(
54 bluetooth_outofband::kBluetoothOutOfBandInterface,
55 bluetooth_outofband::kAddRemoteData);
56
57 dbus::MessageWriter writer(&method_call);
58 writer.AppendString(address);
59 writer.AppendArrayOfBytes(data.hash, kBluetoothOutOfBandPairingDataSize);
60 writer.AppendArrayOfBytes(data.randomizer,
61 kBluetoothOutOfBandPairingDataSize);
62
63 dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path);
64
65 object_proxy->CallMethod(
66 &method_call,
67 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
68 base::Bind(&BluetoothOutOfBandClientImpl::ResponseToSuccessCallback,
69 weak_ptr_factory_.GetWeakPtr(), callback));
70 }
71
72 // BluetoothOutOfBandClient override.
73 virtual void RemoveRemoteData(
74 const dbus::ObjectPath& object_path,
75 const std::string& address,
76 const SuccessCallback& callback) OVERRIDE {
77 dbus::MethodCall method_call(
78 bluetooth_outofband::kBluetoothOutOfBandInterface,
79 bluetooth_outofband::kRemoveRemoteData);
80
81 dbus::MessageWriter writer(&method_call);
82 writer.AppendString(address);
83
84 dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path);
85
86 object_proxy->CallMethod(
87 &method_call,
88 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
89 base::Bind(&BluetoothOutOfBandClientImpl::ResponseToSuccessCallback,
90 weak_ptr_factory_.GetWeakPtr(), callback));
91 }
92
93 private:
94 // We maintain a collection of dbus object proxies for each binding.
95 typedef std::map<const dbus::ObjectPath, dbus::ObjectProxy*> ObjectMap;
96 ObjectMap object_map_;
97
98 // Returns a pointer to the object proxy for |object_path|, creating
99 // it if necessary. This is cached in the ObjectMap.
100 dbus::ObjectProxy* GetObjectProxy(const dbus::ObjectPath& object_path) {
101 ObjectMap::iterator iter = object_map_.find(object_path);
102 if (iter != object_map_.end())
103 return iter->second;
104
105 DCHECK(bus_);
106 dbus::ObjectProxy* object_proxy = bus_->GetObjectProxy(
107 bluetooth_outofband::kBluetoothOutOfBandServiceName, object_path);
108
109 object_map_[object_path] = object_proxy;
110 return object_proxy;
111 }
112
113 // Called when a response from ReadLocalOutOfBandPairingData() is received.
114 void OnReadLocalData(const DataCallback& callback,
115 dbus::Response* response) {
116 bool success = false;
117 BluetoothOutOfBandPairingData data;
118 if (response != NULL) {
119 dbus::MessageReader reader(response);
120 uint8_t* bytes = NULL;
121 size_t length = kBluetoothOutOfBandPairingDataSize;
122 if (reader.PopArrayOfBytes(&bytes, &length)) {
123 if (length == kBluetoothOutOfBandPairingDataSize) {
124 memcpy(&data.hash, bytes, length);
125 if (reader.PopArrayOfBytes(&bytes, &length)) {
126 if (length == kBluetoothOutOfBandPairingDataSize) {
127 memcpy(&data.randomizer, bytes, length);
128 success = true;
129 }
130 }
131 }
132 }
133 }
134 callback.Run(data, success);
135 }
136
137 // Translates a dbus::Response to a SuccessCallback by assuming success if
138 // |response| is not NULL.
139 void ResponseToSuccessCallback(const SuccessCallback& callback,
140 dbus::Response* response) {
141 callback.Run(response != NULL);
142 }
143
144 // Weak pointer factory for generating 'this' pointers that might live longer
145 // than we do.
146 base::WeakPtrFactory<BluetoothOutOfBandClientImpl> weak_ptr_factory_;
147
148 dbus::Bus* bus_;
149
150 DISALLOW_COPY_AND_ASSIGN(BluetoothOutOfBandClientImpl);
151 };
152
153 // The BluetoothOutOfBandClient implementation used on Linux desktop, which does
154 // nothing.
155 class BluetoothOutOfBandClientStubImpl : public BluetoothOutOfBandClient {
156 public:
157 // BluetoothOutOfBandClient override.
158 virtual void ReadLocalData(
159 const dbus::ObjectPath& object_path,
160 const DataCallback& callback) OVERRIDE {
161 VLOG(1) << "ReadLocalData: " << object_path.value();
162 BluetoothOutOfBandPairingData data;
163 callback.Run(data, false);
164 }
165
166 // BluetoothOutOfBandClient override.
167 virtual void AddRemoteData(
168 const dbus::ObjectPath& object_path,
169 const std::string& address,
170 const BluetoothOutOfBandPairingData& data,
171 const SuccessCallback& callback) OVERRIDE {
172 VLOG(1) << "AddRemoteData: " << object_path.value();
173 callback.Run(false);
174 }
175
176 // BluetoothOutOfBandClient override.
177 virtual void RemoveRemoteData(
178 const dbus::ObjectPath& object_path,
179 const std::string& address,
180 const SuccessCallback& callback) OVERRIDE {
181 VLOG(1) << "RemoveRemoteData: " << object_path.value();
182 callback.Run(false);
183 }
184 };
185
186 BluetoothOutOfBandClient::BluetoothOutOfBandClient() {}
187
188 BluetoothOutOfBandClient::~BluetoothOutOfBandClient() {}
189
190 BluetoothOutOfBandClient* BluetoothOutOfBandClient::Create(
191 DBusClientImplementationType type,
192 dbus::Bus* bus) {
193 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION)
194 return new BluetoothOutOfBandClientImpl(bus);
195 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type);
196 return new BluetoothOutOfBandClientStubImpl();
197 }
198
199 } // namespace chromeos
OLDNEW
« no previous file with comments | « chromeos/dbus/bluetooth_out_of_band_client.h ('k') | chromeos/dbus/dbus_thread_manager.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698