blob: fb46bcf9906622b889d0e23d99d36c5046d44022 [file] [log] [blame]
/*
* Copyright 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <string>
#include <sysexits.h>
#include <base/bind.h>
#include <base/command_line.h>
#include <base/macros.h>
#include <base/memory/weak_ptr.h>
#include <brillo/binder_watcher.h>
#include <brillo/daemons/dbus_daemon.h>
#include <brillo/syslog_logging.h>
#include <libweaved/service.h>
#include "animation.h"
#include "ledservice/dbus-proxies.h"
using com::android::LEDService::ServiceProxyInterface;
namespace {
const char kWeaveComponent[] = "ledflasher";
} // anonymous namespace
class Daemon final : public brillo::DBusDaemon {
public:
Daemon() = default;
protected:
int OnInit() override;
private:
void OnWeaveServiceConnected(const std::weak_ptr<weaved::Service>& service);
void OnLEDServiceConnected(ServiceProxyInterface* service);
void OnLEDServiceDisconnected(const dbus::ObjectPath& object_path);
// Particular command handlers for various commands.
void OnSet(std::unique_ptr<weaved::Command> command);
void OnToggle(std::unique_ptr<weaved::Command> command);
void OnAnimate(std::unique_ptr<weaved::Command> command);
// Helper methods to propagate device state changes to Buffet and hence to
// the cloud server or local clients.
void UpdateDeviceState();
std::weak_ptr<weaved::Service> service_;
// LEDService's D-Bus object manager.
std::unique_ptr<com::android::LEDService::ObjectManagerProxy> led_object_mgr_;
// Device state variables.
std::string status_{"idle"};
// LED service interface.
ServiceProxyInterface* led_service_{nullptr};
// Current animation;
std::unique_ptr<Animation> animation_;
brillo::BinderWatcher binder_watcher_;
std::unique_ptr<weaved::Service::Subscription> weave_service_subscription_;
base::WeakPtrFactory<Daemon> weak_ptr_factory_{this};
DISALLOW_COPY_AND_ASSIGN(Daemon);
};
int Daemon::OnInit() {
int return_code = brillo::DBusDaemon::OnInit();
if (return_code != EX_OK)
return return_code;
if (!binder_watcher_.Init())
return EX_OSERR;
weave_service_subscription_ = weaved::Service::Connect(
brillo::MessageLoop::current(),
base::Bind(&Daemon::OnWeaveServiceConnected,
weak_ptr_factory_.GetWeakPtr()));
led_object_mgr_.reset(new com::android::LEDService::ObjectManagerProxy{bus_});
led_object_mgr_->SetServiceAddedCallback(
base::Bind(&Daemon::OnLEDServiceConnected, base::Unretained(this)));
led_object_mgr_->SetServiceRemovedCallback(
base::Bind(&Daemon::OnLEDServiceDisconnected, base::Unretained(this)));
LOG(INFO) << "Waiting for commands...";
return EX_OK;
}
void Daemon::OnWeaveServiceConnected(
const std::weak_ptr<weaved::Service>& service) {
LOG(INFO) << "Daemon::OnWeaveServiceConnected";
service_ = service;
auto weave_service = service.lock();
if (!weave_service)
return;
weave_service->AddComponent(kWeaveComponent, {"_ledflasher"}, nullptr);
weave_service->AddCommandHandler(
kWeaveComponent,
"_ledflasher.set",
base::Bind(&Daemon::OnSet, weak_ptr_factory_.GetWeakPtr()));
weave_service->AddCommandHandler(
kWeaveComponent,
"_ledflasher.toggle",
base::Bind(&Daemon::OnToggle, weak_ptr_factory_.GetWeakPtr()));
weave_service->AddCommandHandler(
kWeaveComponent,
"_ledflasher.animate",
base::Bind(&Daemon::OnAnimate, weak_ptr_factory_.GetWeakPtr()));
UpdateDeviceState();
}
void Daemon::OnLEDServiceConnected(ServiceProxyInterface* service) {
led_service_ = service;
UpdateDeviceState();
}
void Daemon::OnLEDServiceDisconnected(const dbus::ObjectPath& /*object_path*/) {
animation_.reset();
led_service_ = nullptr;
}
void Daemon::OnSet(std::unique_ptr<weaved::Command> command) {
if (!led_service_) {
command->Abort("system_error", "ledservice unavailable", nullptr);
return;
}
int index = command->GetParameter<int>("led");
if(index < 1 || index > 4) {
command->Abort("invalid_parameter", "Invalid parameter value", nullptr);
return;
}
bool on = command->GetParameter<bool>("on");
brillo::ErrorPtr error;
if (!led_service_->SetLED(index - 1, on, &error)) {
command->Abort(error->GetCode(), error->GetMessage(), nullptr);
return;
}
animation_.reset();
status_ = "idle";
UpdateDeviceState();
command->Complete({}, nullptr);
}
void Daemon::OnToggle(std::unique_ptr<weaved::Command> command) {
if (!led_service_) {
command->Abort("system_error", "ledservice unavailable", nullptr);
return;
}
int index = command->GetParameter<int>("led");
if(index < 1 || index > 4) {
command->Abort("invalid_parameter", "Invalid parameter value", nullptr);
return;
}
index--;
bool on = false;
brillo::ErrorPtr error;
if(!led_service_->GetLED(index, &on, &error) ||
!led_service_->SetLED(index, !on, &error)) {
command->Abort(error->GetCode(), error->GetMessage(), nullptr);
return;
}
animation_.reset();
status_ = "idle";
UpdateDeviceState();
command->Complete({}, nullptr);
}
void Daemon::OnAnimate(std::unique_ptr<weaved::Command> command) {
if (!led_service_) {
command->Abort("system_error", "ledservice unavailable", nullptr);
return;
}
double duration = command->GetParameter<double>("duration");
if(duration <= 0.0) {
command->Abort("invalid_parameter", "Invalid parameter value", nullptr);
return;
}
std::string type = command->GetParameter<std::string>("type");
animation_ = Animation::Create(led_service_, type,
base::TimeDelta::FromSecondsD(duration));
if (animation_) {
status_ = "animating";
animation_->Start();
} else {
status_ = "idle";
}
UpdateDeviceState();
command->Complete({}, nullptr);
}
void Daemon::UpdateDeviceState() {
if (!led_service_)
return;
std::vector<bool> leds;
if (!led_service_->GetAllLEDs(&leds, nullptr))
return;
auto weave_service = service_.lock();
if (!weave_service)
return;
brillo::VariantDictionary state_change{
{"_ledflasher.status", status_},
{"_ledflasher.leds", leds},
};
// TODO: Come up with a design for ledflasher.cpp such that this call never
// fails.
weave_service->SetStateProperties(kWeaveComponent, state_change, nullptr);
}
int main(int argc, char* argv[]) {
base::CommandLine::Init(argc, argv);
brillo::InitLog(brillo::kLogToSyslog | brillo::kLogHeader);
Daemon daemon;
return daemon.Run();
}