summaryrefslogtreecommitdiffstats
path: root/chromium/device/nfc/nfc_adapter_chromeos.cc
blob: 374e8ac83e94901efc4e87b28e7899f58c846e73 (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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
// Copyright 2013 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 "device/nfc/nfc_adapter_chromeos.h"

#include <vector>

#include "base/callback.h"
#include "base/logging.h"
#include "chromeos/dbus/dbus_thread_manager.h"
#include "device/nfc/nfc_peer_chromeos.h"
#include "device/nfc/nfc_tag_chromeos.h"
#include "third_party/cros_system_api/dbus/service_constants.h"

namespace chromeos {

namespace {

typedef std::vector<dbus::ObjectPath> ObjectPathVector;

}  // namespace

NfcAdapterChromeOS::NfcAdapterChromeOS()
    : weak_ptr_factory_(this) {
  DBusThreadManager::Get()->GetNfcAdapterClient()->AddObserver(this);
  DBusThreadManager::Get()->GetNfcDeviceClient()->AddObserver(this);
  DBusThreadManager::Get()->GetNfcTagClient()->AddObserver(this);

  const ObjectPathVector& object_paths =
      DBusThreadManager::Get()->GetNfcAdapterClient()->GetAdapters();
  if (!object_paths.empty()) {
    VLOG(1) << object_paths.size() << " NFC adapter(s) available.";
    SetAdapter(object_paths[0]);
  }
}

NfcAdapterChromeOS::~NfcAdapterChromeOS() {
  DBusThreadManager::Get()->GetNfcAdapterClient()->RemoveObserver(this);
  DBusThreadManager::Get()->GetNfcDeviceClient()->RemoveObserver(this);
  DBusThreadManager::Get()->GetNfcTagClient()->RemoveObserver(this);
}

void NfcAdapterChromeOS::AddObserver(NfcAdapter::Observer* observer) {
  DCHECK(observer);
  observers_.AddObserver(observer);
}

void NfcAdapterChromeOS::RemoveObserver(NfcAdapter::Observer* observer) {
  DCHECK(observer);
  observers_.RemoveObserver(observer);
}

bool NfcAdapterChromeOS::IsPresent() const {
  return !object_path_.value().empty();
}

bool NfcAdapterChromeOS::IsPowered() const {
  if (!IsPresent())
    return false;
  return DBusThreadManager::Get()->GetNfcAdapterClient()->
      GetProperties(object_path_)->powered.value();
}

bool NfcAdapterChromeOS::IsPolling() const {
  if (!IsPresent())
    return false;
  return DBusThreadManager::Get()->GetNfcAdapterClient()->
      GetProperties(object_path_)->polling.value();
}

bool NfcAdapterChromeOS::IsInitialized() const {
  return true;
}

void NfcAdapterChromeOS::SetPowered(bool powered,
                                    const base::Closure& callback,
                                    const ErrorCallback& error_callback) {
  if (!IsPresent()) {
    LOG(WARNING) << "Adapter not present. Cannot power up the antenna.";
    error_callback.Run();
    return;
  }
  DBusThreadManager::Get()->GetNfcAdapterClient()->
      GetProperties(object_path_)->powered.Set(
          powered,
          base::Bind(&NfcAdapterChromeOS::OnSetPowered,
                     weak_ptr_factory_.GetWeakPtr(),
                     callback,
                     error_callback));
}

void NfcAdapterChromeOS::StartPolling(const base::Closure& callback,
                                      const ErrorCallback& error_callback) {
  // Always poll in "Initiator" mode.
  DBusThreadManager::Get()->GetNfcAdapterClient()->
      StartPollLoop(object_path_,
                    nfc_adapter::kModeInitiator,
                    base::Bind(&NfcAdapterChromeOS::OnStartPolling,
                               weak_ptr_factory_.GetWeakPtr(),
                               callback),
                    base::Bind(&NfcAdapterChromeOS::OnStartPollingError,
                               weak_ptr_factory_.GetWeakPtr(),
                               error_callback));
}

void NfcAdapterChromeOS::StopPolling(const base::Closure& callback,
                                     const ErrorCallback& error_callback) {
  DBusThreadManager::Get()->GetNfcAdapterClient()->
      StopPollLoop(object_path_,
                   base::Bind(&NfcAdapterChromeOS::OnStopPolling,
                              weak_ptr_factory_.GetWeakPtr(),
                              callback),
                   base::Bind(&NfcAdapterChromeOS::OnStopPollingError,
                              weak_ptr_factory_.GetWeakPtr(),
                              error_callback));
}

void NfcAdapterChromeOS::AdapterAdded(const dbus::ObjectPath& object_path) {
  // Set the adapter to the newly added adapter only if no adapter is present.
  if (!IsPresent())
    SetAdapter(object_path);
}

void NfcAdapterChromeOS::AdapterRemoved(const dbus::ObjectPath& object_path) {
  if (object_path != object_path_)
    return;

  // The current adapter was removed, so mark us as not present and clean up
  // peers and tags.
  RemoveAdapter();

  // There may still be other adapters present on the system. Set the next
  // available adapter as the current one.
  const ObjectPathVector& object_paths =
      DBusThreadManager::Get()->GetNfcAdapterClient()->GetAdapters();
  for (ObjectPathVector::const_iterator iter =
          object_paths.begin();
       iter != object_paths.end(); ++iter) {
    // The removed object will still be available until the call to
    // AdapterRemoved returns. Make sure that we are not re-adding the
    // removed adapter.
    if (*iter == object_path)
      continue;
    SetAdapter(*iter);
  }
}

void NfcAdapterChromeOS::AdapterPropertyChanged(
    const dbus::ObjectPath& object_path,
    const std::string& property_name) {
  if (object_path != object_path_)
    return;
  NfcAdapterClient::Properties* properties =
      DBusThreadManager::Get()->GetNfcAdapterClient()->
          GetProperties(object_path_);
  if (property_name == properties->powered.name())
    PoweredChanged(properties->powered.value());
  else if (property_name == properties->polling.name())
    PollingChanged(properties->polling.value());
}

void NfcAdapterChromeOS::DeviceAdded(const dbus::ObjectPath& object_path) {
  if (!IsPresent())
    return;

  if (GetPeer(object_path.value()))
    return;

  VLOG(1) << "NFC device found: " << object_path.value();

  // Check to see if the device belongs to this adapter.
  const ObjectPathVector& devices =
      DBusThreadManager::Get()->GetNfcDeviceClient()->
          GetDevicesForAdapter(object_path_);
  bool device_found = false;
  for (ObjectPathVector::const_iterator iter = devices.begin();
       iter != devices.end(); ++iter) {
    if (*iter == object_path) {
      device_found = true;
      break;
    }
  }
  if (!device_found) {
    VLOG(1) << "Found peer device does not belong to the current adapter.";
    return;
  }

  // Create the peer object.
  NfcPeerChromeOS* peer_chromeos = new NfcPeerChromeOS(object_path);
  SetPeer(object_path.value(), peer_chromeos);
  FOR_EACH_OBSERVER(NfcAdapter::Observer, observers_,
                    PeerFound(this, peer_chromeos));
}

void NfcAdapterChromeOS::DeviceRemoved(const dbus::ObjectPath& object_path) {
  VLOG(1) << "NFC device lost: " << object_path.value();
  device::NfcPeer* peer = RemovePeer(object_path.value());
  if (!peer) {
    VLOG(1) << "Removed peer device does not belong to the current adapter.";
    return;
  }
  FOR_EACH_OBSERVER(NfcAdapter::Observer, observers_, PeerLost(this, peer));
  delete peer;
}

void NfcAdapterChromeOS::TagAdded(const dbus::ObjectPath& object_path) {
  if (!IsPresent())
    return;

  if (GetTag(object_path.value()))
    return;

  VLOG(1) << "NFC tag found: " << object_path.value();

  // Check to see if the tag belongs to this adapter.
  const std::vector<dbus::ObjectPath>& tags =
      DBusThreadManager::Get()->GetNfcTagClient()->
          GetTagsForAdapter(object_path_);
  bool tag_found = false;
  for (std::vector<dbus::ObjectPath>::const_iterator iter = tags.begin();
       iter != tags.end(); ++iter) {
    if (*iter == object_path) {
      tag_found = true;
      break;
    }
  }
  if (!tag_found) {
    VLOG(1) << "Found tag does not belong to the current adapter.";
    return;
  }

  // Create the tag object.
  NfcTagChromeOS* tag_chromeos = new NfcTagChromeOS(object_path);
  SetTag(object_path.value(), tag_chromeos);
  FOR_EACH_OBSERVER(NfcAdapter::Observer, observers_,
                    TagFound(this, tag_chromeos));
}

void NfcAdapterChromeOS::TagRemoved(const dbus::ObjectPath& object_path) {
  VLOG(1) << "NFC tag lost : " << object_path.value();
  device::NfcTag* tag = RemoveTag(object_path.value());
  if (!tag) {
    VLOG(1) << "Removed tag does not belong to the current adapter.";
    return;
  }
  FOR_EACH_OBSERVER(NfcAdapter::Observer, observers_, TagLost(this, tag));
  delete tag;
}

void NfcAdapterChromeOS::SetAdapter(const dbus::ObjectPath& object_path) {
  DCHECK(!IsPresent());
  object_path_ = object_path;
  VLOG(1) << "Using NFC adapter: " << object_path.value();

  NfcAdapterClient::Properties* properties =
      DBusThreadManager::Get()->GetNfcAdapterClient()->
          GetProperties(object_path_);
  PresentChanged(true);
  if (properties->powered.value())
    PoweredChanged(true);
  if (properties->polling.value())
    PollingChanged(true);

  // Create peer objects for peers that were added before the adapter was set.
  const ObjectPathVector& devices =
      DBusThreadManager::Get()->GetNfcDeviceClient()->
          GetDevicesForAdapter(object_path_);
  for (ObjectPathVector::const_iterator iter = devices.begin();
       iter != devices.end(); ++iter) {
    const dbus::ObjectPath& object_path = *iter;
    if (GetPeer(object_path.value()))
      continue;
    NfcPeerChromeOS* peer_chromeos = new NfcPeerChromeOS(object_path);
    SetPeer(object_path.value(), peer_chromeos);
    FOR_EACH_OBSERVER(NfcAdapter::Observer, observers_,
                      PeerFound(this, peer_chromeos));
  }

  // Create tag objects for tags that were added before the adapter was set.
  const std::vector<dbus::ObjectPath>& tags =
      DBusThreadManager::Get()->GetNfcTagClient()->
          GetTagsForAdapter(object_path_);
  for (std::vector<dbus::ObjectPath>::const_iterator iter = tags.begin();
       iter != tags.end(); ++iter) {
    const dbus::ObjectPath& object_path = *iter;
    if (GetTag(object_path.value()))
      continue;
    NfcTagChromeOS* tag_chromeos = new NfcTagChromeOS(object_path);
    SetTag(object_path.value(), tag_chromeos);
    FOR_EACH_OBSERVER(NfcAdapter::Observer, observers_,
                      TagFound(this, tag_chromeos));
  }
}

void NfcAdapterChromeOS::RemoveAdapter() {
  DCHECK(IsPresent());
  VLOG(1) << "NFC adapter removed: " << object_path_.value();

  NfcAdapterClient::Properties* properties =
      DBusThreadManager::Get()->GetNfcAdapterClient()->
          GetProperties(object_path_);
  if (properties->powered.value())
    PoweredChanged(false);
  if (properties->polling.value())
    PollingChanged(false);

  // Copy the tags and peers here and clear the original containers so that
  // GetPeers and GetTags return no values during the *Removed observer calls.
  PeerList peers;
  TagList tags;
  GetPeers(&peers);
  GetTags(&tags);
  ClearPeers();
  ClearTags();

  for (PeerList::iterator iter = peers.begin();
       iter != peers.end(); ++iter) {
    device::NfcPeer* peer = *iter;
    FOR_EACH_OBSERVER(NfcAdapter::Observer, observers_,
                      PeerLost(this, peer));
    delete peer;
  }
  for (TagList::iterator iter = tags.begin();
       iter != tags.end(); ++iter) {
    device::NfcTag* tag = *iter;
    FOR_EACH_OBSERVER(NfcAdapter::Observer, observers_,
                      TagLost(this, tag));
    delete tag;
  }

  object_path_ = dbus::ObjectPath("");
  PresentChanged(false);
}

void NfcAdapterChromeOS::PoweredChanged(bool powered) {
  FOR_EACH_OBSERVER(NfcAdapter::Observer, observers_,
                    AdapterPoweredChanged(this, powered));
}

void NfcAdapterChromeOS::PollingChanged(bool polling) {
  FOR_EACH_OBSERVER(NfcAdapter::Observer, observers_,
                    AdapterPollingChanged(this, polling));
}

void NfcAdapterChromeOS::PresentChanged(bool present) {
  FOR_EACH_OBSERVER(NfcAdapter::Observer, observers_,
                    AdapterPresentChanged(this, present));
}

void NfcAdapterChromeOS::OnSetPowered(const base::Closure& callback,
                                      const ErrorCallback& error_callback,
                                      bool success) {
  VLOG(1) << "NfcAdapterChromeOS::OnSetPowered result: " << success;
  if (success) {
    // TODO(armansito): There is a bug in neard 0.13 that causes it not to emit
    // a signal when the "Powered" property changes. Sync the properties here,
    // but remove it in neard 0.14.
    if (IsPresent()) {
      DBusThreadManager::Get()->GetNfcAdapterClient()->
          GetProperties(object_path_)->GetAll();
    }
    callback.Run();
  } else {
    LOG(ERROR) << "Failed to power up the NFC antenna radio.";
    error_callback.Run();
  }
}

void NfcAdapterChromeOS::OnStartPolling(const base::Closure& callback) {
  callback.Run();
}

void NfcAdapterChromeOS::OnStartPollingError(
    const ErrorCallback& error_callback,
    const std::string& error_name,
    const std::string& error_message) {
  LOG(ERROR) << object_path_.value() << ": Failed to start polling: "
             << error_name << ": " << error_message;
  error_callback.Run();
}

void NfcAdapterChromeOS::OnStopPolling(const base::Closure& callback) {
  callback.Run();
}

void NfcAdapterChromeOS::OnStopPollingError(
    const ErrorCallback& error_callback,
    const std::string& error_name,
    const std::string& error_message) {
  LOG(ERROR) << object_path_.value() << ": Failed to stop polling: "
             << error_name << ": " << error_message;
  error_callback.Run();
}

}  // namespace chromeos