summaryrefslogtreecommitdiffstats
path: root/chromeos/dbus/bluetooth_out_of_band_client.cc
blob: 457cf82515b0c7b9c69bd6c21b2f7aee34840c3d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/dbus/bluetooth_out_of_band_client.h"

#include <map>
#include <string>

#include "base/bind.h"
#include "base/logging.h"
#include "chromeos/dbus/bluetooth_adapter_client.h"
#include "dbus/bus.h"
#include "dbus/message.h"
#include "dbus/object_path.h"
#include "dbus/object_proxy.h"
#include "device/bluetooth/bluetooth_out_of_band_pairing_data.h"
#include "third_party/cros_system_api/dbus/service_constants.h"

namespace chromeos {

// The BluetoothOutOfBandClient implementation used in production.
class BluetoothOutOfBandClientImpl: public BluetoothOutOfBandClient {
 public:
  explicit BluetoothOutOfBandClientImpl(dbus::Bus* bus)
      : bus_(bus),
        weak_ptr_factory_(this) {}

  virtual ~BluetoothOutOfBandClientImpl() {}

  // BluetoothOutOfBandClient override.
  virtual void ReadLocalData(
      const dbus::ObjectPath& object_path,
      const DataCallback& callback) OVERRIDE {
    dbus::MethodCall method_call(
        bluetooth_outofband::kBluetoothOutOfBandInterface,
        bluetooth_outofband::kReadLocalData);

    dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path);

    object_proxy->CallMethod(
        &method_call,
        dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
        base::Bind(&BluetoothOutOfBandClientImpl::OnReadLocalData,
                   weak_ptr_factory_.GetWeakPtr(), callback));
  }

  // BluetoothOutOfBandClient override.
  virtual void AddRemoteData(
      const dbus::ObjectPath& object_path,
      const std::string& address,
      const device::BluetoothOutOfBandPairingData& data,
      const SuccessCallback& callback) OVERRIDE {
    dbus::MethodCall method_call(
        bluetooth_outofband::kBluetoothOutOfBandInterface,
        bluetooth_outofband::kAddRemoteData);

    dbus::MessageWriter writer(&method_call);
    writer.AppendString(address);
    writer.AppendArrayOfBytes(
        data.hash, device::kBluetoothOutOfBandPairingDataSize);
    writer.AppendArrayOfBytes(
        data.randomizer, device::kBluetoothOutOfBandPairingDataSize);

    dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path);

    object_proxy->CallMethod(
        &method_call,
        dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
        base::Bind(&BluetoothOutOfBandClientImpl::ResponseToSuccessCallback,
                   weak_ptr_factory_.GetWeakPtr(), callback));
  }

  // BluetoothOutOfBandClient override.
  virtual void RemoveRemoteData(
      const dbus::ObjectPath& object_path,
      const std::string& address,
      const SuccessCallback& callback) OVERRIDE {
    dbus::MethodCall method_call(
        bluetooth_outofband::kBluetoothOutOfBandInterface,
        bluetooth_outofband::kRemoveRemoteData);

    dbus::MessageWriter writer(&method_call);
    writer.AppendString(address);

    dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path);

    object_proxy->CallMethod(
        &method_call,
        dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
        base::Bind(&BluetoothOutOfBandClientImpl::ResponseToSuccessCallback,
                   weak_ptr_factory_.GetWeakPtr(), callback));
  }

 private:
  // We maintain a collection of dbus object proxies for each binding.
  typedef std::map<const dbus::ObjectPath, dbus::ObjectProxy*> ObjectMap;
  ObjectMap object_map_;

  // Returns a pointer to the object proxy for |object_path|, creating
  // it if necessary.  This is cached in the ObjectMap.
  dbus::ObjectProxy* GetObjectProxy(const dbus::ObjectPath& object_path) {
    ObjectMap::iterator iter = object_map_.find(object_path);
    if (iter != object_map_.end())
      return iter->second;

    DCHECK(bus_);
    dbus::ObjectProxy* object_proxy = bus_->GetObjectProxy(
        bluetooth_outofband::kBluetoothOutOfBandServiceName, object_path);

    object_map_[object_path] = object_proxy;
    return object_proxy;
  }

  // Called when a response from ReadLocalOutOfBandPairingData() is received.
  void OnReadLocalData(const DataCallback& callback,
                       dbus::Response* response) {
    bool success = false;
    device::BluetoothOutOfBandPairingData data;
    if (response != NULL) {
      dbus::MessageReader reader(response);
      uint8_t* bytes = NULL;
      size_t length = device::kBluetoothOutOfBandPairingDataSize;
      if (reader.PopArrayOfBytes(&bytes, &length)) {
        if (length == device::kBluetoothOutOfBandPairingDataSize) {
          memcpy(&data.hash, bytes, length);
          if (reader.PopArrayOfBytes(&bytes, &length)) {
            if (length == device::kBluetoothOutOfBandPairingDataSize) {
              memcpy(&data.randomizer, bytes, length);
              success = true;
            }
          }
        }
      }
    }
    callback.Run(data, success);
  }

  // Translates a dbus::Response to a SuccessCallback by assuming success if
  // |response| is not NULL.
  void ResponseToSuccessCallback(const SuccessCallback& callback,
                                 dbus::Response* response) {
    callback.Run(response != NULL);
  }

  dbus::Bus* bus_;

  // Weak pointer factory for generating 'this' pointers that might live longer
  // than we do.
  // Note: This should remain the last member so it'll be destroyed and
  // invalidate its weak pointers before any other members are destroyed.
  base::WeakPtrFactory<BluetoothOutOfBandClientImpl> weak_ptr_factory_;

  DISALLOW_COPY_AND_ASSIGN(BluetoothOutOfBandClientImpl);
};

// The BluetoothOutOfBandClient implementation used on Linux desktop, which does
// nothing.
class BluetoothOutOfBandClientStubImpl : public BluetoothOutOfBandClient {
 public:
  // BluetoothOutOfBandClient override.
  virtual void ReadLocalData(
      const dbus::ObjectPath& object_path,
      const DataCallback& callback) OVERRIDE {
    VLOG(1) << "ReadLocalData: " << object_path.value();
    device::BluetoothOutOfBandPairingData data;
    callback.Run(data, false);
  }

  // BluetoothOutOfBandClient override.
  virtual void AddRemoteData(
      const dbus::ObjectPath& object_path,
      const std::string& address,
      const device::BluetoothOutOfBandPairingData& data,
      const SuccessCallback& callback) OVERRIDE {
    VLOG(1) << "AddRemoteData: " << object_path.value();
    callback.Run(false);
  }

  // BluetoothOutOfBandClient override.
  virtual void RemoveRemoteData(
      const dbus::ObjectPath& object_path,
      const std::string& address,
      const SuccessCallback& callback) OVERRIDE {
    VLOG(1) << "RemoveRemoteData: " << object_path.value();
    callback.Run(false);
  }
};

BluetoothOutOfBandClient::BluetoothOutOfBandClient() {}

BluetoothOutOfBandClient::~BluetoothOutOfBandClient() {}

BluetoothOutOfBandClient* BluetoothOutOfBandClient::Create(
    DBusClientImplementationType type,
    dbus::Bus* bus) {
  if (type == REAL_DBUS_CLIENT_IMPLEMENTATION)
    return new BluetoothOutOfBandClientImpl(bus);
  DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type);
  return new BluetoothOutOfBandClientStubImpl();
}

}  // namespace chromeos