Alex Vakulenko | b511e56 | 2014-09-12 10:27:23 -0700 | [diff] [blame] | 1 | // Copyright 2014 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | // This is a sample daemon that "handles" Buffet commands. |
| 6 | // It just prints the information about the command received to stdout and |
| 7 | // marks the command as processed. |
| 8 | |
| 9 | #include <string> |
| 10 | #include <sysexits.h> |
| 11 | |
| 12 | #include <base/bind.h> |
| 13 | #include <base/command_line.h> |
| 14 | #include <base/format_macros.h> |
Vitaly Buka | e25f6fa | 2015-04-29 12:16:58 -0700 | [diff] [blame] | 15 | #include <base/json/json_writer.h> |
| 16 | #include <base/values.h> |
Alex Vakulenko | b511e56 | 2014-09-12 10:27:23 -0700 | [diff] [blame] | 17 | #include <chromeos/daemons/dbus_daemon.h> |
| 18 | #include <chromeos/map_utils.h> |
| 19 | #include <chromeos/strings/string_utils.h> |
| 20 | #include <chromeos/syslog_logging.h> |
| 21 | |
Alex Vakulenko | 9485896 | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 22 | #include "buffet/dbus-proxies.h" |
Alex Vakulenko | b511e56 | 2014-09-12 10:27:23 -0700 | [diff] [blame] | 23 | |
Alex Vakulenko | 62c1b3a | 2015-04-13 16:05:10 -0700 | [diff] [blame] | 24 | namespace { |
Vitaly Buka | e25f6fa | 2015-04-29 12:16:58 -0700 | [diff] [blame] | 25 | |
| 26 | std::unique_ptr<base::DictionaryValue> DictionaryToJson( |
| 27 | const chromeos::VariantDictionary& dictionary); |
| 28 | |
| 29 | std::unique_ptr<base::Value> AnyToJson(const chromeos::Any& value) { |
| 30 | if (value.IsTypeCompatible<chromeos::VariantDictionary>()) |
| 31 | return DictionaryToJson(value.Get<chromeos::VariantDictionary>()); |
| 32 | |
| 33 | if (value.IsTypeCompatible<std::string>()) { |
| 34 | return std::unique_ptr<base::Value>{ |
| 35 | new base::StringValue(value.Get<std::string>())}; |
| 36 | } |
| 37 | |
| 38 | if (value.IsTypeCompatible<double>()) { |
| 39 | return std::unique_ptr<base::Value>{ |
| 40 | new base::FundamentalValue(value.Get<double>())}; |
| 41 | } |
| 42 | |
| 43 | if (value.IsTypeCompatible<bool>()) { |
| 44 | return std::unique_ptr<base::Value>{ |
| 45 | new base::FundamentalValue(value.Get<bool>())}; |
| 46 | } |
| 47 | |
| 48 | if (value.IsTypeCompatible<int>()) { |
| 49 | return std::unique_ptr<base::Value>{ |
| 50 | new base::FundamentalValue(value.Get<int>())}; |
| 51 | } |
| 52 | |
| 53 | LOG(FATAL) << "Unsupported type:" << value.GetType().name(); |
| 54 | return {}; |
| 55 | } |
| 56 | |
| 57 | std::unique_ptr<base::DictionaryValue> DictionaryToJson( |
| 58 | const chromeos::VariantDictionary& dictionary) { |
| 59 | std::unique_ptr<base::DictionaryValue> result{new base::DictionaryValue}; |
| 60 | for (const auto& it : dictionary) |
| 61 | result->Set(it.first, AnyToJson(it.second).release()); |
| 62 | return result; |
| 63 | } |
| 64 | |
| 65 | std::string DictionaryToString(const chromeos::VariantDictionary& dictionary) { |
| 66 | std::unique_ptr<base::DictionaryValue> json{DictionaryToJson(dictionary)}; |
| 67 | std::string str; |
Alex Vakulenko | ab4e4ff | 2015-06-15 12:53:22 -0700 | [diff] [blame] | 68 | base::JSONWriter::Write(*json, &str); |
Vitaly Buka | e25f6fa | 2015-04-29 12:16:58 -0700 | [diff] [blame] | 69 | return str; |
| 70 | } |
| 71 | |
Alex Vakulenko | 62c1b3a | 2015-04-13 16:05:10 -0700 | [diff] [blame] | 72 | } // anonymous namespace |
| 73 | |
Alex Vakulenko | 1e3a66b | 2015-05-22 15:48:53 -0700 | [diff] [blame] | 74 | class Daemon final : public chromeos::DBusDaemon { |
Alex Vakulenko | b511e56 | 2014-09-12 10:27:23 -0700 | [diff] [blame] | 75 | public: |
| 76 | Daemon() = default; |
| 77 | |
| 78 | protected: |
| 79 | int OnInit() override; |
| 80 | void OnShutdown(int* return_code) override; |
| 81 | |
| 82 | private: |
Alex Vakulenko | 63bdf08 | 2015-08-21 09:27:12 -0700 | [diff] [blame] | 83 | std::unique_ptr<com::android::Weave::ObjectManagerProxy> object_manager_; |
Alex Vakulenko | b511e56 | 2014-09-12 10:27:23 -0700 | [diff] [blame] | 84 | |
Alex Vakulenko | 63bdf08 | 2015-08-21 09:27:12 -0700 | [diff] [blame] | 85 | void OnBuffetCommand(com::android::Weave::CommandProxy* command); |
Alex Vakulenko | 9485896 | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 86 | void OnBuffetCommandRemoved(const dbus::ObjectPath& object_path); |
Alex Vakulenko | 63bdf08 | 2015-08-21 09:27:12 -0700 | [diff] [blame] | 87 | void OnPropertyChange(com::android::Weave::CommandProxy* command, |
Alex Vakulenko | 9485896 | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 88 | const std::string& property_name); |
Alex Vakulenko | 63bdf08 | 2015-08-21 09:27:12 -0700 | [diff] [blame] | 89 | void OnCommandProgress(com::android::Weave::CommandProxy* command, |
Alex Vakulenko | 9485896 | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 90 | int progress); |
Alex Vakulenko | b511e56 | 2014-09-12 10:27:23 -0700 | [diff] [blame] | 91 | |
| 92 | DISALLOW_COPY_AND_ASSIGN(Daemon); |
| 93 | }; |
| 94 | |
| 95 | int Daemon::OnInit() { |
| 96 | int return_code = chromeos::DBusDaemon::OnInit(); |
| 97 | if (return_code != EX_OK) |
| 98 | return return_code; |
| 99 | |
Alex Vakulenko | 63bdf08 | 2015-08-21 09:27:12 -0700 | [diff] [blame] | 100 | object_manager_.reset(new com::android::Weave::ObjectManagerProxy{bus_}); |
Alex Vakulenko | 9485896 | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 101 | object_manager_->SetCommandAddedCallback( |
| 102 | base::Bind(&Daemon::OnBuffetCommand, base::Unretained(this))); |
| 103 | object_manager_->SetCommandRemovedCallback( |
| 104 | base::Bind(&Daemon::OnBuffetCommandRemoved, base::Unretained(this))); |
Alex Vakulenko | b511e56 | 2014-09-12 10:27:23 -0700 | [diff] [blame] | 105 | |
| 106 | printf("Waiting for commands...\n"); |
| 107 | return EX_OK; |
| 108 | } |
| 109 | |
| 110 | void Daemon::OnShutdown(int* return_code) { |
| 111 | printf("Shutting down...\n"); |
| 112 | } |
| 113 | |
Alex Vakulenko | 63bdf08 | 2015-08-21 09:27:12 -0700 | [diff] [blame] | 114 | void Daemon::OnPropertyChange(com::android::Weave::CommandProxy* command, |
Alex Vakulenko | 9485896 | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 115 | const std::string& property_name) { |
| 116 | printf("Notification: property '%s' on command '%s' changed.\n", |
| 117 | property_name.c_str(), command->id().c_str()); |
Vitaly Buka | e25f6fa | 2015-04-29 12:16:58 -0700 | [diff] [blame] | 118 | printf(" Current command status: '%s'\n", command->status().c_str()); |
| 119 | std::string progress = DictionaryToString(command->progress()); |
| 120 | printf(" Current command progress: %s\n", progress.c_str()); |
| 121 | std::string results = DictionaryToString(command->results()); |
| 122 | printf(" Current command results: %s\n", results.c_str()); |
Alex Vakulenko | 9485896 | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Alex Vakulenko | 63bdf08 | 2015-08-21 09:27:12 -0700 | [diff] [blame] | 125 | void Daemon::OnBuffetCommand(com::android::Weave::CommandProxy* command) { |
Alex Vakulenko | 62c1b3a | 2015-04-13 16:05:10 -0700 | [diff] [blame] | 126 | // "Handle" only commands that belong to this daemon's category. |
Alex Vakulenko | 2915a7b | 2015-10-07 17:04:00 -0700 | [diff] [blame^] | 127 | if (command->status() == "done") |
Alex Vakulenko | 62c1b3a | 2015-04-13 16:05:10 -0700 | [diff] [blame] | 128 | return; |
| 129 | |
Alex Vakulenko | 9485896 | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 130 | command->SetPropertyChangedCallback(base::Bind(&Daemon::OnPropertyChange, |
| 131 | base::Unretained(this))); |
| 132 | printf("++++++++++++++++++++++++++++++++++++++++++++++++\n"); |
| 133 | printf("Command received: %s\n", command->name().c_str()); |
| 134 | printf("DBus Object Path: %s\n", command->GetObjectPath().value().c_str()); |
Alex Vakulenko | 9485896 | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 135 | printf(" ID: %s\n", command->id().c_str()); |
| 136 | printf(" status: %s\n", command->status().c_str()); |
Alex Vakulenko | d419044 | 2015-04-20 12:33:52 -0700 | [diff] [blame] | 137 | printf(" origin: %s\n", command->origin().c_str()); |
Vitaly Buka | e25f6fa | 2015-04-29 12:16:58 -0700 | [diff] [blame] | 138 | std::string param_names = DictionaryToString(command->parameters()); |
| 139 | printf(" parameters: %s\n", param_names.c_str()); |
Alex Vakulenko | 9485896 | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 140 | OnCommandProgress(command, 0); |
| 141 | } |
| 142 | |
Alex Vakulenko | 63bdf08 | 2015-08-21 09:27:12 -0700 | [diff] [blame] | 143 | void Daemon::OnCommandProgress(com::android::Weave::CommandProxy* command, |
Alex Vakulenko | 9485896 | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 144 | int progress) { |
Vitaly Buka | e25f6fa | 2015-04-29 12:16:58 -0700 | [diff] [blame] | 145 | printf("Updating command '%s' progress to %d%%\n", command->id().c_str(), |
| 146 | progress); |
| 147 | auto new_progress = command->progress(); |
| 148 | new_progress["progress"] = progress; |
| 149 | command->SetProgress(new_progress, nullptr); |
| 150 | |
Alex Vakulenko | 9485896 | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 151 | if (progress >= 100) { |
| 152 | command->Done(nullptr); |
| 153 | } else { |
Alex Vakulenko | 9485896 | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 154 | base::MessageLoop::current()->PostDelayedTask( |
Vitaly Buka | e25f6fa | 2015-04-29 12:16:58 -0700 | [diff] [blame] | 155 | FROM_HERE, base::Bind(&Daemon::OnCommandProgress, |
| 156 | base::Unretained(this), command, progress + 10), |
Alex Vakulenko | 9485896 | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 157 | base::TimeDelta::FromSeconds(1)); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | void Daemon::OnBuffetCommandRemoved(const dbus::ObjectPath& object_path) { |
| 162 | printf("------------------------------------------------\n"); |
| 163 | printf("Command removed\n"); |
| 164 | printf("DBus Object Path: %s\n", object_path.value().c_str()); |
Alex Vakulenko | b511e56 | 2014-09-12 10:27:23 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | int main(int argc, char* argv[]) { |
Alex Vakulenko | 18862e4 | 2015-04-02 14:31:10 -0700 | [diff] [blame] | 168 | base::CommandLine::Init(argc, argv); |
Alex Vakulenko | b511e56 | 2014-09-12 10:27:23 -0700 | [diff] [blame] | 169 | chromeos::InitLog(chromeos::kLogToSyslog | |
| 170 | chromeos::kLogToStderr | |
| 171 | chromeos::kLogHeader); |
| 172 | Daemon daemon; |
| 173 | return daemon.Run(); |
| 174 | } |