Update ledflasher to use weaved's client library
Do not use weave'd D-Bus proxies directly. Use the new client library.
Change-Id: I3486689e565517b7fd66ee1f4bd441ab0369eab5
diff --git a/src/ledflasher/Android.mk b/src/ledflasher/Android.mk
index 857a44e..97c331c 100644
--- a/src/ledflasher/Android.mk
+++ b/src/ledflasher/Android.mk
@@ -36,7 +36,7 @@
libchromeos-stream \
libdbus \
libledservice-client \
- libweaved-client \
+ libweaved \
LOCAL_C_INCLUDES := external/gtest/include
LOCAL_CFLAGS := -Wall -Werror -Wno-sign-promo
diff --git a/src/ledflasher/ledflasher.cc b/src/ledflasher/ledflasher.cc
index 1d7d58c..679c04a 100644
--- a/src/ledflasher/ledflasher.cc
+++ b/src/ledflasher/ledflasher.cc
@@ -22,31 +22,13 @@
#include <base/macros.h>
#include <chromeos/daemons/dbus_daemon.h>
#include <chromeos/syslog_logging.h>
+#include <libweaved/device.h>
#include "animation.h"
-#include "buffet/dbus-proxies.h"
#include "ledservice/dbus-proxies.h"
-using com::android::Weave::CommandProxy;
-using com::android::Weave::ManagerProxy;
using com::android::LEDService::ServiceProxy;
-namespace {
-// Helper function to get a command parameter of particular type T from the
-// command parameter list. Returns default value for type T (e.g. 0 for int or
-// or "" for std::string) if the parameter with the given name is not found or
-// is of incorrect type.
-template <typename T>
-T GetParameter(const chromeos::VariantDictionary& parameters,
- const std::string& name) {
- T value{};
- auto p = parameters.find(name);
- if (p != parameters.end())
- p->second.GetValue<T>(&value);
- return value;
-}
-} // anonymous namespace
-
class Daemon final : public chromeos::DBusDaemon {
public:
Daemon() = default;
@@ -58,22 +40,16 @@
void OnLEDServiceConnected(ServiceProxy* service);
void OnLEDServiceDisconnected(const dbus::ObjectPath& object_path);
- // Main callback invoked when new command is added to Buffet's command queue.
- void OnCommand(CommandProxy* command);
-
// Particular command handlers for various commands.
- void OnSet(CommandProxy* command);
- void OnToggle(CommandProxy* command);
- void OnAnimate(CommandProxy* command);
+ void OnSet(const std::weak_ptr<weaved::Command>& cmd);
+ void OnToggle(const std::weak_ptr<weaved::Command>& cmd);
+ void OnAnimate(const std::weak_ptr<weaved::Command>& cmd);
// Helper methods to propagate device state changes to Buffet and hence to
// the cloud server or local clients.
- void NotifyDeviceStateChanged();
- void UpdateDeviceState(ManagerProxy* manager);
+ void UpdateDeviceState();
- // Weaved's D-Bus object manager class that is used to communicate with Weaved
- // and receive Weave commands from local clients or GCD server.
- std::unique_ptr<com::android::Weave::ObjectManagerProxy> weaved_object_mgr_;
+ std::unique_ptr<weaved::Device> device_;
// LEDService's D-Bus object manager.
std::unique_ptr<com::android::LEDService::ObjectManagerProxy> led_object_mgr_;
@@ -95,11 +71,17 @@
if (return_code != EX_OK)
return return_code;
- weaved_object_mgr_.reset(new com::android::Weave::ObjectManagerProxy{bus_});
- weaved_object_mgr_->SetCommandAddedCallback(
- base::Bind(&Daemon::OnCommand, base::Unretained(this)));
- weaved_object_mgr_->SetManagerAddedCallback(
- base::Bind(&Daemon::UpdateDeviceState, base::Unretained(this)));
+ device_ = weaved::Device::CreateInstance(
+ bus_, base::Bind(&Daemon::UpdateDeviceState, base::Unretained(this)));
+ device_->AddCommandHandler(
+ "_ledflasher._set",
+ base::Bind(&Daemon::OnSet, base::Unretained(this)));
+ device_->AddCommandHandler(
+ "_ledflasher._toggle",
+ base::Bind(&Daemon::OnToggle, base::Unretained(this)));
+ device_->AddCommandHandler(
+ "_ledflasher._animate",
+ base::Bind(&Daemon::OnAnimate, base::Unretained(this)));
led_object_mgr_.reset(new com::android::LEDService::ObjectManagerProxy{bus_});
led_object_mgr_->SetServiceAddedCallback(
@@ -113,7 +95,7 @@
void Daemon::OnLEDServiceConnected(ServiceProxy* service) {
led_service_ = service;
- NotifyDeviceStateChanged();
+ UpdateDeviceState();
}
void Daemon::OnLEDServiceDisconnected(const dbus::ObjectPath& object_path) {
@@ -121,28 +103,19 @@
led_service_ = nullptr;
}
-void Daemon::OnCommand(CommandProxy* command) {
- if (command->state() != "queued")
+void Daemon::OnSet(const std::weak_ptr<weaved::Command>& cmd) {
+ auto command = cmd.lock();
+ if (!command)
return;
- LOG(INFO) << "Command: " << command->name();
- if (command->name() == "_ledflasher._set") {
- OnSet(command);
- } else if (command->name() == "_ledflasher._toggle") {
- OnToggle(command);
- } else if (command->name() == "_ledflasher._animate") {
- OnAnimate(command);
- }
-}
-
-void Daemon::OnSet(CommandProxy* command) {
- int index = GetParameter<int>(command->parameters(), "_led");
- CHECK(index > 0);
- bool on = GetParameter<bool>(command->parameters(), "_on");
if (!led_service_) {
CHECK(command->Abort("system_error", "ledservice unavailable", nullptr));
return;
}
+
+ int index = command->GetParameter<int>("_led");
+ CHECK_GT(index, 0);
+ bool on = command->GetParameter<bool>("_on");
chromeos::ErrorPtr error;
if (!led_service_->SetLED(index - 1, on, &error)) {
CHECK(command->Abort(error->GetCode(), error->GetMessage(), nullptr));
@@ -150,19 +123,24 @@
}
animation_.reset();
status_ = "idle";
- NotifyDeviceStateChanged();
+ UpdateDeviceState();
CHECK(command->Complete({}, nullptr));
}
-void Daemon::OnToggle(CommandProxy* command) {
- int index = GetParameter<int>(command->parameters(), "_led");
- CHECK(index > 0);
- index--;
- bool on = false;
+void Daemon::OnToggle(const std::weak_ptr<weaved::Command>& cmd) {
+ auto command = cmd.lock();
+ if (!command)
+ return;
+
if (!led_service_) {
CHECK(command->Abort("system_error", "ledservice unavailable", nullptr));
return;
}
+
+ int index = command->GetParameter<int>("_led");
+ CHECK_GT(index, 0);
+ index--;
+ bool on = false;
chromeos::ErrorPtr error;
if(!led_service_->GetLED(index, &on, &error) ||
!led_service_->SetLED(index, !on, &error)) {
@@ -171,16 +149,22 @@
}
animation_.reset();
status_ = "idle";
- NotifyDeviceStateChanged();
+ UpdateDeviceState();
CHECK(command->Complete({}, nullptr));
}
-void Daemon::OnAnimate(CommandProxy* command) {
- if (!led_service_)
+void Daemon::OnAnimate(const std::weak_ptr<weaved::Command>& cmd) {
+ auto command = cmd.lock();
+ if (!command)
return;
- double duration = GetParameter<double>(command->parameters(), "_duration");
- std::string type = GetParameter<std::string>(command->parameters(), "_type");
+ if (!led_service_) {
+ CHECK(command->Abort("system_error", "ledservice unavailable", nullptr));
+ return;
+ }
+
+ double duration = command->GetParameter<double>("_duration");
+ std::string type = command->GetParameter<std::string>("_type");
animation_ = Animation::Create(led_service_, type,
base::TimeDelta::FromSecondsD(duration));
if (animation_) {
@@ -189,11 +173,11 @@
} else {
status_ = "idle";
}
- NotifyDeviceStateChanged();
+ UpdateDeviceState();
CHECK(command->Complete({}, nullptr));
}
-void Daemon::UpdateDeviceState(ManagerProxy* manager) {
+void Daemon::UpdateDeviceState() {
if (!led_service_)
return;
@@ -205,13 +189,7 @@
{"_ledflasher._status", status_},
{"_ledflasher._leds", leds},
};
- CHECK(manager->UpdateState(state_change, nullptr));
-}
-
-void Daemon::NotifyDeviceStateChanged() {
- ManagerProxy* manager = weaved_object_mgr_->GetManagerProxy();
- if (manager)
- UpdateDeviceState(manager);
+ CHECK(device_->SetStateProperties(state_change, nullptr));
}
int main(int argc, char* argv[]) {