aboutsummaryrefslogtreecommitdiff
path: root/buffet/dbus_command_proxy.cc
blob: b7e82c14f2915455078a40cd8de0f62a5707d73c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// 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_.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