1 // Copyright 2013 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.
5 #include "chromeos/dbus/fake_nfc_tag_client.h"
7 #include "base/logging.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/time/time.h"
10 #include "chromeos/dbus/dbus_thread_manager.h"
11 #include "chromeos/dbus/fake_nfc_adapter_client.h"
12 #include "chromeos/dbus/fake_nfc_record_client.h"
13 #include "chromeos/dbus/nfc_client_helpers.h"
14 #include "dbus/object_path.h"
15 #include "third_party/cros_system_api/dbus/service_constants.h"
19 using nfc_client_helpers::ObjectPathVector
;
21 const char FakeNfcTagClient::kTagPath
[] = "/fake/tag0";
22 const int FakeNfcTagClient::kDefaultSimulationTimeoutMilliseconds
= 20000;
24 FakeNfcTagClient::Properties::Properties(
25 const PropertyChangedCallback
& callback
)
26 : NfcTagClient::Properties(NULL
, callback
) {
29 FakeNfcTagClient::Properties::~Properties() {
32 void FakeNfcTagClient::Properties::Get(
33 dbus::PropertyBase
* property
,
34 dbus::PropertySet::GetCallback callback
) {
35 VLOG(1) << "Get " << property
->name();
39 void FakeNfcTagClient::Properties::GetAll() {
43 void FakeNfcTagClient::Properties::Set(
44 dbus::PropertyBase
* property
,
45 dbus::PropertySet::SetCallback callback
) {
46 VLOG(1) << "Set " << property
->name();
50 FakeNfcTagClient::FakeNfcTagClient()
51 : pairing_started_(false),
53 simulation_timeout_(kDefaultSimulationTimeoutMilliseconds
) {
54 VLOG(1) << "Creating FakeNfcTagClient";
55 properties_
.reset(new Properties(
56 base::Bind(&FakeNfcTagClient::OnPropertyChanged
,
57 base::Unretained(this),
58 dbus::ObjectPath(kTagPath
))));
61 FakeNfcTagClient::~FakeNfcTagClient() {
64 void FakeNfcTagClient::Init(dbus::Bus
* bus
) {
67 void FakeNfcTagClient::AddObserver(Observer
* observer
) {
68 observers_
.AddObserver(observer
);
71 void FakeNfcTagClient::RemoveObserver(Observer
* observer
) {
72 observers_
.RemoveObserver(observer
);
75 std::vector
<dbus::ObjectPath
> FakeNfcTagClient::GetTagsForAdapter(
76 const dbus::ObjectPath
& adapter_path
) {
77 std::vector
<dbus::ObjectPath
> tag_paths
;
79 adapter_path
.value() == FakeNfcAdapterClient::kAdapterPath0
)
80 tag_paths
.push_back(dbus::ObjectPath(kTagPath
));
84 FakeNfcTagClient::Properties
*
85 FakeNfcTagClient::GetProperties(const dbus::ObjectPath
& object_path
) {
88 return properties_
.get();
91 void FakeNfcTagClient::Write(
92 const dbus::ObjectPath
& object_path
,
93 const base::DictionaryValue
& attributes
,
94 const base::Closure
& callback
,
95 const nfc_client_helpers::ErrorCallback
& error_callback
) {
96 VLOG(1) << "FakeNfcTagClient::Write called. Nothing happened.";
98 if (!tag_visible_
|| object_path
.value() != kTagPath
) {
99 LOG(ERROR
) << "No such tag: " << object_path
.value();
100 error_callback
.Run(nfc_error::kDoesNotExist
, "No such tag.");
104 FakeNfcRecordClient
* record_client
= static_cast<FakeNfcRecordClient
*>(
105 DBusThreadManager::Get()->GetNfcRecordClient());
106 if (!record_client
->WriteTagRecord(attributes
)) {
107 LOG(ERROR
) << "Failed to tag: " << object_path
.value();
108 error_callback
.Run(nfc_error::kFailed
, "Failed.");
116 void FakeNfcTagClient::BeginPairingSimulation(int visibility_delay
) {
117 if (pairing_started_
) {
118 VLOG(1) << "Simulation already started.";
121 DCHECK(!tag_visible_
);
122 DCHECK(visibility_delay
>= 0);
124 pairing_started_
= true;
126 base::MessageLoop::current()->PostDelayedTask(
128 base::Bind(&FakeNfcTagClient::MakeTagVisible
,
129 base::Unretained(this)),
130 base::TimeDelta::FromMilliseconds(visibility_delay
));
133 void FakeNfcTagClient::EndPairingSimulation() {
134 if (!pairing_started_
) {
135 VLOG(1) << "No simulation started.";
139 pairing_started_
= false;
143 // Remove records, if they were added.
144 if (!properties_
->records
.value().empty()) {
145 FakeNfcRecordClient
* record_client
=
146 static_cast<FakeNfcRecordClient
*>(
147 DBusThreadManager::Get()->GetNfcRecordClient());
148 record_client
->SetTagRecordsVisible(false);
151 FOR_EACH_OBSERVER(Observer
, observers_
,
152 TagRemoved(dbus::ObjectPath(kTagPath
)));
153 FakeNfcAdapterClient
* adapter_client
=
154 static_cast<FakeNfcAdapterClient
*>(
155 DBusThreadManager::Get()->GetNfcAdapterClient());
156 adapter_client
->UnsetTag(dbus::ObjectPath(kTagPath
));
157 tag_visible_
= false;
160 void FakeNfcTagClient::EnableSimulationTimeout(int simulation_timeout
) {
161 DCHECK(simulation_timeout
>= 0);
162 simulation_timeout_
= simulation_timeout
;
165 void FakeNfcTagClient::DisableSimulationTimeout() {
166 simulation_timeout_
= -1;
169 void FakeNfcTagClient::SetRecords(
170 const std::vector
<dbus::ObjectPath
>& record_paths
) {
172 VLOG(1) << "Tag not visible.";
175 properties_
->records
.ReplaceValue(record_paths
);
178 void FakeNfcTagClient::ClearRecords() {
179 ObjectPathVector records
;
183 void FakeNfcTagClient::OnPropertyChanged(const dbus::ObjectPath
& object_path
,
184 const std::string
& property_name
) {
185 FOR_EACH_OBSERVER(Observer
, observers_
,
186 TagPropertyChanged(object_path
, property_name
));
189 void FakeNfcTagClient::MakeTagVisible() {
190 if (!pairing_started_
) {
191 VLOG(1) << "Tag pairing was cancelled.";
196 // Set the tag properties.
197 FakeNfcAdapterClient
* adapter_client
=
198 static_cast<FakeNfcAdapterClient
*>(
199 DBusThreadManager::Get()->GetNfcAdapterClient());
200 adapter_client
->SetTag(dbus::ObjectPath(kTagPath
));
201 FOR_EACH_OBSERVER(Observer
, observers_
,
202 TagAdded(dbus::ObjectPath(kTagPath
)));
204 properties_
->type
.ReplaceValue(nfc_tag::kTagType2
);
205 properties_
->protocol
.ReplaceValue(nfc_common::kProtocolNfcDep
);
206 properties_
->read_only
.ReplaceValue(false);
207 FOR_EACH_OBSERVER(Observer
, observers_
,
208 TagPropertiesReceived(dbus::ObjectPath(kTagPath
)));
210 if (simulation_timeout_
>= 0) {
211 base::MessageLoop::current()->PostDelayedTask(
213 base::Bind(&FakeNfcTagClient::HandleSimulationTimeout
,
214 base::Unretained(this)),
215 base::TimeDelta::FromMilliseconds(simulation_timeout_
));
220 void FakeNfcTagClient::HandleSimulationTimeout() {
221 if (simulation_timeout_
< 0) {
222 VLOG(1) << "Simulation timeout was cancelled. Nothing to do.";
225 EndPairingSimulation();
228 } // namespace chromeos