blob: 011ba01b8c3effda2a89975a8ee8127109c5b8e0 [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 "buffet/dbus_command_proxy.h"
#include <brillo/dbus/async_event_sequencer.h>
#include <brillo/dbus/exported_object_manager.h>
#include <weave/enum_to_string.h>
#include "buffet/dbus_conversion.h"
#include "buffet/weave_error_conversion.h"
using brillo::dbus_utils::AsyncEventSequencer;
using brillo::dbus_utils::ExportedObjectManager;
namespace errors {
namespace commands {
const char kDomain[] = "weaved";
const char kCommandDestroyed[] = "command_destroyed";
} // namespace commands
} // namespace errors
namespace buffet {
namespace {
bool ReportDestroyedError(brillo::ErrorPtr* error) {
brillo::Error::AddTo(error, FROM_HERE, errors::commands::kDomain,
errors::commands::kCommandDestroyed,
"Command has been destroyed");
return false;
}
} // anonymous namespace
DBusCommandProxy::DBusCommandProxy(ExportedObjectManager* object_manager,
const scoped_refptr<dbus::Bus>& bus,
const std::weak_ptr<weave::Command>& command,
std::string object_path)
: command_{command},
dbus_object_{object_manager, bus, dbus::ObjectPath{object_path}} {}
void DBusCommandProxy::RegisterAsync(
const AsyncEventSequencer::CompletionAction& completion_callback) {
auto command = command_.lock();
if (!command)
return;
dbus_adaptor_.RegisterWithDBusObject(&dbus_object_);
// Set the initial property values before registering the DBus object.
dbus_adaptor_.SetName(command->GetName());
dbus_adaptor_.SetId(command->GetID());
dbus_adaptor_.SetComponent(command->GetComponent());
dbus_adaptor_.SetState(EnumToString(command->GetState()));
dbus_adaptor_.SetProgress(
DictionaryToDBusVariantDictionary(command->GetProgress()));
dbus_adaptor_.SetOrigin(EnumToString(command->GetOrigin()));
dbus_adaptor_.SetParameters(
DictionaryToDBusVariantDictionary(command->GetParameters()));
dbus_adaptor_.SetResults(
DictionaryToDBusVariantDictionary(command->GetResults()));
// Register the command DBus object and expose its methods and properties.
dbus_object_.RegisterAsync(completion_callback);
}
bool DBusCommandProxy::SetProgress(
brillo::ErrorPtr* error,
const brillo::VariantDictionary& progress) {
auto command = command_.lock();
if (!command)
return ReportDestroyedError(error);
LOG(INFO) << "Received call to Command<" << command->GetName()
<< ">::SetProgress()";
auto dictionary = DictionaryFromDBusVariantDictionary(progress, error);
if (!dictionary)
return false;
weave::ErrorPtr weave_error;
if (!command->SetProgress(*dictionary, &weave_error)) {
ConvertError(*weave_error, error);
return false;
}
dbus_adaptor_.SetProgress(
DictionaryToDBusVariantDictionary(command->GetProgress()));
dbus_adaptor_.SetState(EnumToString(command->GetState()));
return true;
}
bool DBusCommandProxy::Complete(brillo::ErrorPtr* error,
const brillo::VariantDictionary& results) {
auto command = command_.lock();
if (!command)
return ReportDestroyedError(error);
LOG(INFO) << "Received call to Command<" << command->GetName()
<< ">::Complete()";
auto dictionary = DictionaryFromDBusVariantDictionary(results, error);
if (!dictionary)
return false;
weave::ErrorPtr weave_error;
if (!command->Complete(*dictionary, &weave_error)) {
ConvertError(*weave_error, error);
return false;
}
dbus_adaptor_.SetResults(
DictionaryToDBusVariantDictionary(command->GetResults()));
dbus_adaptor_.SetState(EnumToString(command->GetState()));
return true;
}
bool DBusCommandProxy::Abort(brillo::ErrorPtr* error,
const std::string& code,
const std::string& message) {
auto command = command_.lock();
if (!command)
return ReportDestroyedError(error);
LOG(INFO) << "Received call to Command<" << command->GetName()
<< ">::Abort()";
weave::ErrorPtr cmd_error;
weave::Error::AddTo(&cmd_error, FROM_HERE, "command_error", code, message);
weave::ErrorPtr weave_error;
if (!command->Abort(cmd_error.get(), &weave_error)) {
ConvertError(*weave_error, error);
return false;
}
dbus_adaptor_.SetState(EnumToString(command->GetState()));
return true;
}
bool DBusCommandProxy::Cancel(brillo::ErrorPtr* error) {
auto command = command_.lock();
if (!command)
return ReportDestroyedError(error);
LOG(INFO) << "Received call to Command<" << command->GetName()
<< ">::Cancel()";
weave::ErrorPtr weave_error;
if (!command->Cancel(&weave_error)) {
ConvertError(*weave_error, error);
return false;
}
dbus_adaptor_.SetState(EnumToString(command->GetState()));
return true;
}
} // namespace buffet