summaryrefslogtreecommitdiffstats
path: root/chromeos
diff options
context:
space:
mode:
authorarmansito@chromium.org <armansito@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-12-08 07:18:54 +0000
committerarmansito@chromium.org <armansito@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-12-08 07:18:54 +0000
commit42716047fd8604517a73070401470fada6971b23 (patch)
treef17388c8038fb91984dc2c727ae58649862f96de /chromeos
parent6fe561067c842ec9d884f1b3356b4218af9a0dfa (diff)
downloadchromium_src-42716047fd8604517a73070401470fada6971b23.zip
chromium_src-42716047fd8604517a73070401470fada6971b23.tar.gz
chromium_src-42716047fd8604517a73070401470fada6971b23.tar.bz2
nfc: Implement NfcAdapterChromeOS.
Added the basic implementation of NfcAdapterChromeOS, with no tag/peer logic. BUG=316471 TEST=device_unittests Review URL: https://codereview.chromium.org/100393011 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@239388 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chromeos')
-rw-r--r--chromeos/dbus/fake_nfc_adapter_client.cc135
-rw-r--r--chromeos/dbus/fake_nfc_adapter_client.h31
-rw-r--r--chromeos/dbus/nfc_adapter_client.cc6
-rw-r--r--chromeos/dbus/nfc_adapter_client.h3
-rw-r--r--chromeos/dbus/nfc_client_helpers.cc10
-rw-r--r--chromeos/dbus/nfc_client_helpers.h4
6 files changed, 178 insertions, 11 deletions
diff --git a/chromeos/dbus/fake_nfc_adapter_client.cc b/chromeos/dbus/fake_nfc_adapter_client.cc
index f9b1688..ddc673a 100644
--- a/chromeos/dbus/fake_nfc_adapter_client.cc
+++ b/chromeos/dbus/fake_nfc_adapter_client.cc
@@ -5,14 +5,15 @@
#include "chromeos/dbus/fake_nfc_adapter_client.h"
#include "base/logging.h"
+#include "dbus/message.h"
#include "dbus/object_path.h"
-
-// TODO(armansito): For now, this class doesn't do anything. Implement fake
-// behavior in conjunction with unit tests while implementing the src/device
-// layer.
+#include "third_party/cros_system_api/dbus/service_constants.h"
namespace chromeos {
+const char FakeNfcAdapterClient::kAdapterPath0[] = "/fake/nfc0";
+const char FakeNfcAdapterClient::kAdapterPath1[] = "/fake/nfc1";
+
FakeNfcAdapterClient::Properties::Properties(
const PropertyChangedCallback& callback)
: NfcAdapterClient::Properties(NULL, callback) {
@@ -36,11 +37,56 @@ void FakeNfcAdapterClient::Properties::Set(
dbus::PropertyBase* property,
dbus::PropertySet::SetCallback callback) {
VLOG(1) << "Set " << property->name();
- callback.Run(false);
+ if (property->name() != powered.name()) {
+ callback.Run(false);
+ return;
+ }
+
+ // Cannot set the power if currently polling.
+ if (polling.value()) {
+ callback.Run(false);
+ return;
+ }
+
+ // Obtain the cached "set value" and send a property changed signal only if
+ // its value is different from the current value of the property.
+ scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
+ dbus::MessageWriter writer(response.get());
+ property->AppendSetValueToWriter(&writer);
+ dbus::MessageReader reader(response.get());
+ bool set_value = false;
+ if (!reader.PopVariantOfBool(&set_value) || set_value == powered.value()) {
+ LOG(WARNING) << "Property has not changed.";
+ callback.Run(false);
+ return;
+ }
+ property->ReplaceValueWithSetValue();
+ callback.Run(true);
}
-FakeNfcAdapterClient::FakeNfcAdapterClient() {
+FakeNfcAdapterClient::FakeNfcAdapterClient()
+ : present_(true),
+ second_present_(false) {
VLOG(1) << "Creating FakeNfcAdapterClient";
+
+ std::vector<std::string> protocols;
+ protocols.push_back(nfc_common::kProtocolFelica);
+ protocols.push_back(nfc_common::kProtocolMifare);
+ protocols.push_back(nfc_common::kProtocolJewel);
+ protocols.push_back(nfc_common::kProtocolIsoDep);
+ protocols.push_back(nfc_common::kProtocolNfcDep);
+
+ properties_.reset(new Properties(base::Bind(
+ &FakeNfcAdapterClient::OnPropertyChanged,
+ base::Unretained(this),
+ dbus::ObjectPath(kAdapterPath0))));
+ properties_->protocols.ReplaceValue(protocols);
+
+ second_properties_.reset(new Properties(base::Bind(
+ &FakeNfcAdapterClient::OnPropertyChanged,
+ base::Unretained(this),
+ dbus::ObjectPath(kAdapterPath1))));
+ second_properties_->protocols.ReplaceValue(protocols);
}
FakeNfcAdapterClient::~FakeNfcAdapterClient() {
@@ -50,13 +96,28 @@ void FakeNfcAdapterClient::Init(dbus::Bus* bus) {
}
void FakeNfcAdapterClient::AddObserver(Observer* observer) {
+ observers_.AddObserver(observer);
}
void FakeNfcAdapterClient::RemoveObserver(Observer* observer) {
+ observers_.RemoveObserver(observer);
+}
+
+std::vector<dbus::ObjectPath> FakeNfcAdapterClient::GetAdapters() {
+ std::vector<dbus::ObjectPath> object_paths;
+ if (present_)
+ object_paths.push_back(dbus::ObjectPath(kAdapterPath0));
+ if (second_present_)
+ object_paths.push_back(dbus::ObjectPath(kAdapterPath1));
+ return object_paths;
}
FakeNfcAdapterClient::Properties*
FakeNfcAdapterClient::GetProperties(const dbus::ObjectPath& object_path) {
+ if (object_path == dbus::ObjectPath(kAdapterPath0))
+ return properties_.get();
+ if (object_path == dbus::ObjectPath(kAdapterPath1))
+ return second_properties_.get();
return NULL;
}
@@ -65,14 +126,72 @@ void FakeNfcAdapterClient::StartPollLoop(
const std::string& mode,
const base::Closure& callback,
const nfc_client_helpers::ErrorCallback& error_callback) {
- VLOG(1) << "FakeNfcAdapterClient::StartPollLoop called. Nothing happened.";
+ VLOG(1) << "FakeNfcAdapterClient::StartPollLoop";
+ if (object_path != dbus::ObjectPath(kAdapterPath0)) {
+ error_callback.Run(nfc_client_helpers::kNoResponseError, "");
+ return;
+ }
+ if (!properties_->powered.value()) {
+ error_callback.Run("org.neard.Error.Failed", "Adapter not powered.");
+ return;
+ }
+ if (properties_->polling.value()) {
+ error_callback.Run("org.neard.Error.Failed", "Already polling.");
+ return;
+ }
+ properties_->polling.ReplaceValue(true);
+ properties_->mode.ReplaceValue(mode);
+ // TODO(armansito): Initiate fake device/tag simulation here.
}
void FakeNfcAdapterClient::StopPollLoop(
const dbus::ObjectPath& object_path,
const base::Closure& callback,
const nfc_client_helpers::ErrorCallback& error_callback) {
- VLOG(1) << "FakeNfcAdapterClient::StopPollLoop called. Nothing happened.";
+ VLOG(1) << "FakeNfcAdapterClient::StopPollLoop.";
+ if (object_path != dbus::ObjectPath(kAdapterPath0)) {
+ error_callback.Run(nfc_client_helpers::kNoResponseError, "");
+ return;
+ }
+ if (!properties_->polling.value()) {
+ error_callback.Run("org.neard.Error.Failed", "Not polling.");
+ return;
+ }
+ // TODO(armansito): End fake device/tag simulation here.
+ properties_->polling.ReplaceValue(false);
+}
+
+void FakeNfcAdapterClient::SetAdapterPresent(bool present) {
+ if (present == present_)
+ return;
+ present_ = present;
+ if (present_) {
+ FOR_EACH_OBSERVER(NfcAdapterClient::Observer, observers_,
+ AdapterAdded(dbus::ObjectPath(kAdapterPath0)));
+ } else {
+ FOR_EACH_OBSERVER(NfcAdapterClient::Observer, observers_,
+ AdapterRemoved(dbus::ObjectPath(kAdapterPath0)));
+ }
+}
+
+void FakeNfcAdapterClient::SetSecondAdapterPresent(bool present) {
+ if (present == second_present_)
+ return;
+ second_present_ = present;
+ if (present_) {
+ FOR_EACH_OBSERVER(NfcAdapterClient::Observer, observers_,
+ AdapterAdded(dbus::ObjectPath(kAdapterPath1)));
+ } else {
+ FOR_EACH_OBSERVER(NfcAdapterClient::Observer, observers_,
+ AdapterRemoved(dbus::ObjectPath(kAdapterPath1)));
+ }
+}
+
+void FakeNfcAdapterClient::OnPropertyChanged(
+ const dbus::ObjectPath& object_path,
+ const std::string& property_name) {
+ FOR_EACH_OBSERVER(NfcAdapterClient::Observer, observers_,
+ AdapterPropertyChanged(object_path, property_name));
}
} // namespace chromeos
diff --git a/chromeos/dbus/fake_nfc_adapter_client.h b/chromeos/dbus/fake_nfc_adapter_client.h
index 7c23fc3..268b506 100644
--- a/chromeos/dbus/fake_nfc_adapter_client.h
+++ b/chromeos/dbus/fake_nfc_adapter_client.h
@@ -5,6 +5,10 @@
#ifndef CHROMEOS_DBUS_FAKE_NFC_ADAPTER_CLIENT_H_
#define CHROMEOS_DBUS_FAKE_NFC_ADAPTER_CLIENT_H_
+#include <string>
+
+#include "base/memory/scoped_ptr.h"
+#include "base/observer_list.h"
#include "chromeos/chromeos_export.h"
#include "chromeos/dbus/nfc_adapter_client.h"
#include "chromeos/dbus/nfc_client_helpers.h"
@@ -13,9 +17,6 @@ namespace chromeos {
// FakeNfcAdapterClient simulates the behavior of the NFC adapter objects
// and is used both in test cases in place of a mock and on the Linux desktop.
-// TODO(armansito): For now, this doesn't do anything. Implement fake
-// behavior in conjunction with unit tests while implementing the src/device
-// layer.
class CHROMEOS_EXPORT FakeNfcAdapterClient : public NfcAdapterClient {
public:
struct Properties : public NfcAdapterClient::Properties {
@@ -37,6 +38,7 @@ class CHROMEOS_EXPORT FakeNfcAdapterClient : public NfcAdapterClient {
virtual void Init(dbus::Bus* bus) OVERRIDE;
virtual void AddObserver(Observer* observer) OVERRIDE;
virtual void RemoveObserver(Observer* observer) OVERRIDE;
+ virtual std::vector<dbus::ObjectPath> GetAdapters() OVERRIDE;
virtual Properties* GetProperties(
const dbus::ObjectPath& object_path) OVERRIDE;
virtual void StartPollLoop(
@@ -49,7 +51,30 @@ class CHROMEOS_EXPORT FakeNfcAdapterClient : public NfcAdapterClient {
const base::Closure& callback,
const nfc_client_helpers::ErrorCallback& error_callback) OVERRIDE;
+ // Sets the adapter as |present|.
+ void SetAdapterPresent(bool present);
+ void SetSecondAdapterPresent(bool present);
+
+ // The object paths for the adapters that are being emulated.
+ static const char kAdapterPath0[];
+ static const char kAdapterPath1[];
+
private:
+ // Property changed callback passed when we create Properties* structures.
+ void OnPropertyChanged(const dbus::ObjectPath& object_path,
+ const std::string& property_name);
+
+ // List of observers interested in event notifications from us.
+ ObserverList<Observer> observers_;
+
+ // Fake properties that are returned for the emulated adapters.
+ scoped_ptr<Properties> properties_;
+ scoped_ptr<Properties> second_properties_;
+
+ // Whether the adapter and second adapter is present or not.
+ bool present_;
+ bool second_present_;
+
DISALLOW_COPY_AND_ASSIGN(FakeNfcAdapterClient);
};
diff --git a/chromeos/dbus/nfc_adapter_client.cc b/chromeos/dbus/nfc_adapter_client.cc
index 9627dda..34fca6a 100644
--- a/chromeos/dbus/nfc_adapter_client.cc
+++ b/chromeos/dbus/nfc_adapter_client.cc
@@ -65,6 +65,11 @@ class NfcAdapterClientImpl
}
// NfcAdapterClient override.
+ virtual std::vector<dbus::ObjectPath> GetAdapters() OVERRIDE {
+ return object_map_->GetObjectPaths();
+ }
+
+ // NfcAdapterClient override.
virtual Properties* GetProperties(const dbus::ObjectPath& object_path)
OVERRIDE {
return static_cast<Properties*>(
@@ -170,6 +175,7 @@ class NfcAdapterClientImpl
AdapterAdded(object_path));
}
+ // nfc_client_helpers::DBusObjectMap::Delegate override.
virtual void ObjectRemoved(const dbus::ObjectPath& object_path) OVERRIDE {
FOR_EACH_OBSERVER(NfcAdapterClient::Observer, observers_,
AdapterRemoved(object_path));
diff --git a/chromeos/dbus/nfc_adapter_client.h b/chromeos/dbus/nfc_adapter_client.h
index 1d3dc1f..6da4614 100644
--- a/chromeos/dbus/nfc_adapter_client.h
+++ b/chromeos/dbus/nfc_adapter_client.h
@@ -85,6 +85,9 @@ class CHROMEOS_EXPORT NfcAdapterClient : public DBusClient {
virtual void AddObserver(Observer* observer) = 0;
virtual void RemoveObserver(Observer* observer) = 0;
+ // Returns the list of adapter object paths known to the system.
+ virtual std::vector<dbus::ObjectPath> GetAdapters() = 0;
+
// Obtains the properties for the adapter with object path |object_path|, any
// values should be copied if needed. A NULL pointer will be returned, if no
// adapter with the given object path is known to exist.
diff --git a/chromeos/dbus/nfc_client_helpers.cc b/chromeos/dbus/nfc_client_helpers.cc
index c442bc0..8dedc59 100644
--- a/chromeos/dbus/nfc_client_helpers.cc
+++ b/chromeos/dbus/nfc_client_helpers.cc
@@ -201,6 +201,16 @@ void DBusObjectMap::RefreshAllProperties() {
}
}
+ObjectPathVector DBusObjectMap::GetObjectPaths() {
+ std::vector<dbus::ObjectPath> object_paths;
+ for (ObjectMap::const_iterator iter = object_map_.begin();
+ iter != object_map_.end(); ++iter) {
+ const dbus::ObjectPath& object_path = iter->first;
+ object_paths.push_back(object_path);
+ }
+ return object_paths;
+}
+
DBusObjectMap::ObjectPropertyPair DBusObjectMap::GetObjectPropertyPair(
const dbus::ObjectPath& object_path) {
ObjectMap::iterator iter = object_map_.find(object_path);
diff --git a/chromeos/dbus/nfc_client_helpers.h b/chromeos/dbus/nfc_client_helpers.h
index e2e8b06..be38a72 100644
--- a/chromeos/dbus/nfc_client_helpers.h
+++ b/chromeos/dbus/nfc_client_helpers.h
@@ -129,6 +129,10 @@ class CHROMEOS_EXPORT DBusObjectMap {
// that are managed by this instance.
void RefreshAllProperties();
+ // Returns a list containing the object paths of all remote objects that are
+ // managed by this instance.
+ ObjectPathVector GetObjectPaths();
+
private:
typedef std::pair<dbus::ObjectProxy*, NfcPropertySet*> ObjectPropertyPair;
typedef std::map<dbus::ObjectPath, ObjectPropertyPair> ObjectMap;