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.
4
5 #include "chromeos/dbus/fake_nfc_adapter_client.h"
6
7 #include "base/logging.h"
8 #include "dbus/message.h"
9 #include "dbus/object_path.h"
10 #include "third_party/cros_system_api/dbus/service_constants.h"
11
12 namespace chromeos {
13
14 const char FakeNfcAdapterClient::kAdapterPath0[] = "/fake/nfc0";
15 const char FakeNfcAdapterClient::kAdapterPath1[] = "/fake/nfc1";
16
Properties(const PropertyChangedCallback & callback)17 FakeNfcAdapterClient::Properties::Properties(
18 const PropertyChangedCallback& callback)
19 : NfcAdapterClient::Properties(NULL, callback) {
20 }
21
~Properties()22 FakeNfcAdapterClient::Properties::~Properties() {
23 }
24
Get(dbus::PropertyBase * property,dbus::PropertySet::GetCallback callback)25 void FakeNfcAdapterClient::Properties::Get(
26 dbus::PropertyBase* property,
27 dbus::PropertySet::GetCallback callback) {
28 VLOG(1) << "Get " << property->name();
29 callback.Run(false);
30 }
31
GetAll()32 void FakeNfcAdapterClient::Properties::GetAll() {
33 VLOG(1) << "GetAll";
34 }
35
Set(dbus::PropertyBase * property,dbus::PropertySet::SetCallback callback)36 void FakeNfcAdapterClient::Properties::Set(
37 dbus::PropertyBase* property,
38 dbus::PropertySet::SetCallback callback) {
39 VLOG(1) << "Set " << property->name();
40 if (property->name() != powered.name()) {
41 callback.Run(false);
42 return;
43 }
44
45 // Cannot set the power if currently polling.
46 if (polling.value()) {
47 callback.Run(false);
48 return;
49 }
50
51 // Obtain the cached "set value" and send a property changed signal only if
52 // its value is different from the current value of the property.
53 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
54 dbus::MessageWriter writer(response.get());
55 property->AppendSetValueToWriter(&writer);
56 dbus::MessageReader reader(response.get());
57 bool set_value = false;
58 if (!reader.PopVariantOfBool(&set_value) || set_value == powered.value()) {
59 LOG(WARNING) << "Property has not changed.";
60 callback.Run(false);
61 return;
62 }
63 property->ReplaceValueWithSetValue();
64 callback.Run(true);
65 }
66
FakeNfcAdapterClient()67 FakeNfcAdapterClient::FakeNfcAdapterClient()
68 : present_(true),
69 second_present_(false) {
70 VLOG(1) << "Creating FakeNfcAdapterClient";
71
72 std::vector<std::string> protocols;
73 protocols.push_back(nfc_common::kProtocolFelica);
74 protocols.push_back(nfc_common::kProtocolMifare);
75 protocols.push_back(nfc_common::kProtocolJewel);
76 protocols.push_back(nfc_common::kProtocolIsoDep);
77 protocols.push_back(nfc_common::kProtocolNfcDep);
78
79 properties_.reset(new Properties(base::Bind(
80 &FakeNfcAdapterClient::OnPropertyChanged,
81 base::Unretained(this),
82 dbus::ObjectPath(kAdapterPath0))));
83 properties_->protocols.ReplaceValue(protocols);
84
85 second_properties_.reset(new Properties(base::Bind(
86 &FakeNfcAdapterClient::OnPropertyChanged,
87 base::Unretained(this),
88 dbus::ObjectPath(kAdapterPath1))));
89 second_properties_->protocols.ReplaceValue(protocols);
90 }
91
~FakeNfcAdapterClient()92 FakeNfcAdapterClient::~FakeNfcAdapterClient() {
93 }
94
Init(dbus::Bus * bus)95 void FakeNfcAdapterClient::Init(dbus::Bus* bus) {
96 }
97
AddObserver(Observer * observer)98 void FakeNfcAdapterClient::AddObserver(Observer* observer) {
99 observers_.AddObserver(observer);
100 }
101
RemoveObserver(Observer * observer)102 void FakeNfcAdapterClient::RemoveObserver(Observer* observer) {
103 observers_.RemoveObserver(observer);
104 }
105
GetAdapters()106 std::vector<dbus::ObjectPath> FakeNfcAdapterClient::GetAdapters() {
107 std::vector<dbus::ObjectPath> object_paths;
108 if (present_)
109 object_paths.push_back(dbus::ObjectPath(kAdapterPath0));
110 if (second_present_)
111 object_paths.push_back(dbus::ObjectPath(kAdapterPath1));
112 return object_paths;
113 }
114
115 FakeNfcAdapterClient::Properties*
GetProperties(const dbus::ObjectPath & object_path)116 FakeNfcAdapterClient::GetProperties(const dbus::ObjectPath& object_path) {
117 if (object_path == dbus::ObjectPath(kAdapterPath0))
118 return properties_.get();
119 if (object_path == dbus::ObjectPath(kAdapterPath1))
120 return second_properties_.get();
121 return NULL;
122 }
123
StartPollLoop(const dbus::ObjectPath & object_path,const std::string & mode,const base::Closure & callback,const nfc_client_helpers::ErrorCallback & error_callback)124 void FakeNfcAdapterClient::StartPollLoop(
125 const dbus::ObjectPath& object_path,
126 const std::string& mode,
127 const base::Closure& callback,
128 const nfc_client_helpers::ErrorCallback& error_callback) {
129 VLOG(1) << "FakeNfcAdapterClient::StartPollLoop";
130 if (object_path != dbus::ObjectPath(kAdapterPath0)) {
131 error_callback.Run(nfc_client_helpers::kNoResponseError, "");
132 return;
133 }
134 if (!properties_->powered.value()) {
135 error_callback.Run("org.neard.Error.Failed", "Adapter not powered.");
136 return;
137 }
138 if (properties_->polling.value()) {
139 error_callback.Run("org.neard.Error.Failed", "Already polling.");
140 return;
141 }
142 properties_->polling.ReplaceValue(true);
143 properties_->mode.ReplaceValue(mode);
144 // TODO(armansito): Initiate fake device/tag simulation here.
145 }
146
StopPollLoop(const dbus::ObjectPath & object_path,const base::Closure & callback,const nfc_client_helpers::ErrorCallback & error_callback)147 void FakeNfcAdapterClient::StopPollLoop(
148 const dbus::ObjectPath& object_path,
149 const base::Closure& callback,
150 const nfc_client_helpers::ErrorCallback& error_callback) {
151 VLOG(1) << "FakeNfcAdapterClient::StopPollLoop.";
152 if (object_path != dbus::ObjectPath(kAdapterPath0)) {
153 error_callback.Run(nfc_client_helpers::kNoResponseError, "");
154 return;
155 }
156 if (!properties_->polling.value()) {
157 error_callback.Run("org.neard.Error.Failed", "Not polling.");
158 return;
159 }
160 // TODO(armansito): End fake device/tag simulation here.
161 properties_->polling.ReplaceValue(false);
162 }
163
SetAdapterPresent(bool present)164 void FakeNfcAdapterClient::SetAdapterPresent(bool present) {
165 if (present == present_)
166 return;
167 present_ = present;
168 if (present_) {
169 FOR_EACH_OBSERVER(NfcAdapterClient::Observer, observers_,
170 AdapterAdded(dbus::ObjectPath(kAdapterPath0)));
171 } else {
172 FOR_EACH_OBSERVER(NfcAdapterClient::Observer, observers_,
173 AdapterRemoved(dbus::ObjectPath(kAdapterPath0)));
174 }
175 }
176
SetSecondAdapterPresent(bool present)177 void FakeNfcAdapterClient::SetSecondAdapterPresent(bool present) {
178 if (present == second_present_)
179 return;
180 second_present_ = present;
181 if (present_) {
182 FOR_EACH_OBSERVER(NfcAdapterClient::Observer, observers_,
183 AdapterAdded(dbus::ObjectPath(kAdapterPath1)));
184 } else {
185 FOR_EACH_OBSERVER(NfcAdapterClient::Observer, observers_,
186 AdapterRemoved(dbus::ObjectPath(kAdapterPath1)));
187 }
188 }
189
OnPropertyChanged(const dbus::ObjectPath & object_path,const std::string & property_name)190 void FakeNfcAdapterClient::OnPropertyChanged(
191 const dbus::ObjectPath& object_path,
192 const std::string& property_name) {
193 FOR_EACH_OBSERVER(NfcAdapterClient::Observer, observers_,
194 AdapterPropertyChanged(object_path, property_name));
195 }
196
197 } // namespace chromeos
198