blob: dba2f4dacb48d779994abab4b6d340a12b33a576 [file] [log] [blame]
Chris Sosa45d9f102014-03-24 11:18:54 -07001// 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
Christopher Wileya4915c42014-03-27 14:45:37 -07005#include <iostream>
Chris Sosa45d9f102014-03-24 11:18:54 -07006#include <string>
7
Chris Sosaababc5c2014-04-09 15:42:01 -07008#include <base/command_line.h>
Chris Sosa45d9f102014-03-24 11:18:54 -07009#include <base/logging.h>
10#include <base/memory/scoped_ptr.h>
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070011#include <base/values.h>
Chris Sosa45d9f102014-03-24 11:18:54 -070012#include <dbus/bus.h>
13#include <dbus/object_proxy.h>
Christopher Wileya4915c42014-03-27 14:45:37 -070014#include <dbus/message.h>
Chris Sosaababc5c2014-04-09 15:42:01 -070015#include <sysexits.h>
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070016#include <dbus/values_util.h>
Chris Sosa45d9f102014-03-24 11:18:54 -070017
Christopher Wileyeb19fa02014-03-27 13:27:30 -070018#include "buffet/dbus_constants.h"
Chris Sosa45d9f102014-03-24 11:18:54 -070019#include "buffet/dbus_manager.h"
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070020#include "buffet/data_encoding.h"
Chris Sosa45d9f102014-03-24 11:18:54 -070021
Christopher Wileya4915c42014-03-27 14:45:37 -070022using namespace buffet::dbus_constants;
Chris Sosa45d9f102014-03-24 11:18:54 -070023
24namespace {
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070025static const int default_timeout_ms = 1000;
Chris Sosa45d9f102014-03-24 11:18:54 -070026
Christopher Wileya4915c42014-03-27 14:45:37 -070027dbus::ObjectProxy* GetBuffetDBusProxy(dbus::Bus *bus,
28 const std::string& object_path) {
Chris Sosa45d9f102014-03-24 11:18:54 -070029 return bus->GetObjectProxy(
Christopher Wileyeb19fa02014-03-27 13:27:30 -070030 buffet::dbus_constants::kServiceName,
Christopher Wileya4915c42014-03-27 14:45:37 -070031 dbus::ObjectPath(object_path));
Chris Sosa45d9f102014-03-24 11:18:54 -070032}
33
Christopher Wileya4915c42014-03-27 14:45:37 -070034bool CallTestMethod(dbus::ObjectProxy* proxy) {
Christopher Wileyeb19fa02014-03-27 13:27:30 -070035 dbus::MethodCall method_call(buffet::dbus_constants::kRootInterface,
36 buffet::dbus_constants::kRootTestMethod);
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070037 scoped_ptr<dbus::Response> response(
38 proxy->CallMethodAndBlock(&method_call, default_timeout_ms));
Chris Sosa45d9f102014-03-24 11:18:54 -070039 if (!response) {
Christopher Wileya4915c42014-03-27 14:45:37 -070040 std::cout << "Failed to receive a response." << std::endl;
41 return false;
Chris Sosa45d9f102014-03-24 11:18:54 -070042 }
Christopher Wileya4915c42014-03-27 14:45:37 -070043 std::cout << "Received a response." << std::endl;
44 return true;
Chris Sosa45d9f102014-03-24 11:18:54 -070045}
46
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070047bool CallManagerCheckDeviceRegistered(dbus::ObjectProxy* proxy) {
Christopher Wileya4915c42014-03-27 14:45:37 -070048 dbus::MethodCall method_call(
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070049 buffet::dbus_constants::kManagerInterface,
50 buffet::dbus_constants::kManagerCheckDeviceRegistered);
51
Christopher Wileya4915c42014-03-27 14:45:37 -070052 scoped_ptr<dbus::Response> response(
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070053 proxy->CallMethodAndBlock(&method_call, default_timeout_ms));
Christopher Wileya4915c42014-03-27 14:45:37 -070054 if (!response) {
55 std::cout << "Failed to receive a response." << std::endl;
56 return false;
Chris Sosa45d9f102014-03-24 11:18:54 -070057 }
58
Christopher Wileya4915c42014-03-27 14:45:37 -070059 dbus::MessageReader reader(response.get());
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070060 std::string device_id;
61 if (!reader.PopString(&device_id)) {
62 std::cout << "No device ID in response." << std::endl;
Christopher Wileya4915c42014-03-27 14:45:37 -070063 return false;
64 }
65
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070066 std::cout << "Device ID: "
67 << (device_id.empty() ? std::string("<unregistered>") : device_id)
68 << std::endl;
69 return true;
70}
71
72bool CallManagerGetDeviceInfo(dbus::ObjectProxy* proxy) {
73 dbus::MethodCall method_call(
74 buffet::dbus_constants::kManagerInterface,
75 buffet::dbus_constants::kManagerGetDeviceInfo);
76
77 scoped_ptr<dbus::Response> response(
78 proxy->CallMethodAndBlock(&method_call, default_timeout_ms));
79 if (!response) {
80 std::cout << "Failed to receive a response." << std::endl;
81 return false;
82 }
83
84 dbus::MessageReader reader(response.get());
85 std::string device_info;
86 if (!reader.PopString(&device_info)) {
87 std::cout << "No device info in response." << std::endl;
88 return false;
89 }
90
91 std::cout << "Device Info: "
92 << (device_info.empty() ? std::string("<unregistered>") : device_info)
93 << std::endl;
94 return true;
95}
96
97bool CallManagerStartRegisterDevice(
98 dbus::ObjectProxy* proxy,
99 const std::map<std::string, std::shared_ptr<base::Value>>& params) {
100 dbus::MethodCall method_call(
101 buffet::dbus_constants::kManagerInterface,
102 buffet::dbus_constants::kManagerStartRegisterDevice);
103 dbus::MessageWriter writer(&method_call);
104 dbus::MessageWriter dict_writer(nullptr);
105 writer.OpenArray("{sv}", &dict_writer);
106 for (auto&& pair : params) {
107 dbus::MessageWriter dict_entry_writer(nullptr);
108 dict_writer.OpenDictEntry(&dict_entry_writer);
109 dict_entry_writer.AppendString(pair.first);
110 dbus::AppendBasicTypeValueDataAsVariant(&dict_entry_writer,
111 *pair.second.get());
112 dict_writer.CloseContainer(&dict_entry_writer);
113 }
114 writer.CloseContainer(&dict_writer);
115
116 static const int timeout_ms = 3000;
117 scoped_ptr<dbus::Response> response(
118 proxy->CallMethodAndBlock(&method_call, timeout_ms));
119 if (!response) {
120 std::cout << "Failed to receive a response." << std::endl;
121 return false;
122 }
123
124 dbus::MessageReader reader(response.get());
125 std::string info;
126 if (!reader.PopString(&info)) {
127 std::cout << "No valid response." << std::endl;
128 return false;
129 }
130
131 std::cout << "Registration started: " << info << std::endl;
132 return true;
133}
134
135bool CallManagerFinishRegisterDevice(dbus::ObjectProxy* proxy,
136 const std::string& user_auth_code) {
137 dbus::MethodCall method_call(
138 buffet::dbus_constants::kManagerInterface,
139 buffet::dbus_constants::kManagerFinishRegisterDevice);
140 dbus::MessageWriter writer(&method_call);
141 writer.AppendString(user_auth_code);
142 static const int timeout_ms = 10000;
143 scoped_ptr<dbus::Response> response(
144 proxy->CallMethodAndBlock(&method_call, timeout_ms));
145 if (!response) {
146 std::cout << "Failed to receive a response." << std::endl;
147 return false;
148 }
149
150 dbus::MessageReader reader(response.get());
151 std::string device_id;
152 if (!reader.PopString(&device_id)) {
153 std::cout << "No device ID in response." << std::endl;
154 return false;
155 }
156
157 std::cout << "Device ID is "
158 << (device_id.empty() ? std::string("<unregistered>") : device_id)
159 << std::endl;
Christopher Wileya4915c42014-03-27 14:45:37 -0700160 return true;
161}
162
163bool CallManagerUpdateState(dbus::ObjectProxy* proxy,
164 const std::string& json_blob) {
165 dbus::MethodCall method_call(
166 buffet::dbus_constants::kManagerInterface,
167 buffet::dbus_constants::kManagerUpdateStateMethod);
168 dbus::MessageWriter writer(&method_call);
169 writer.AppendString(json_blob);
Christopher Wileya4915c42014-03-27 14:45:37 -0700170 scoped_ptr<dbus::Response> response(
Alex Vakulenko3cb466c2014-04-15 11:36:32 -0700171 proxy->CallMethodAndBlock(&method_call, default_timeout_ms));
Christopher Wileya4915c42014-03-27 14:45:37 -0700172 if (!response) {
173 std::cout << "Failed to receive a response." << std::endl;
174 return false;
175 }
176 return true;
177}
178
179void usage() {
180 std::cerr << "Possible commands:" << std::endl;
181 std::cerr << " " << kRootTestMethod << std::endl;
Alex Vakulenko3cb466c2014-04-15 11:36:32 -0700182 std::cerr << " " << kManagerCheckDeviceRegistered << std::endl;
183 std::cerr << " " << kManagerGetDeviceInfo << std::endl;
184 std::cerr << " " << kManagerStartRegisterDevice
185 << " param1 = val1&param2 = val2..." << std::endl;
186 std::cerr << " " << kManagerFinishRegisterDevice
187 << " device_id" << std::endl;
Christopher Wileya4915c42014-03-27 14:45:37 -0700188 std::cerr << " " << kManagerUpdateStateMethod << std::endl;
189}
190
191} // namespace
192
193int main(int argc, char** argv) {
Chris Sosaababc5c2014-04-09 15:42:01 -0700194 CommandLine::Init(argc, argv);
195 CommandLine* cl = CommandLine::ForCurrentProcess();
196
Christopher Wileya4915c42014-03-27 14:45:37 -0700197 dbus::Bus::Options options;
198 options.bus_type = dbus::Bus::SYSTEM;
199 scoped_refptr<dbus::Bus> bus(new dbus::Bus(options));
200
Chris Sosaababc5c2014-04-09 15:42:01 -0700201 CommandLine::StringVector args = cl->GetArgs();
Alex Vakulenko3cb466c2014-04-15 11:36:32 -0700202 if (args.empty()) {
Christopher Wileya4915c42014-03-27 14:45:37 -0700203 usage();
Chris Sosaababc5c2014-04-09 15:42:01 -0700204 return EX_USAGE;
Christopher Wileya4915c42014-03-27 14:45:37 -0700205 }
206
Chris Sosaababc5c2014-04-09 15:42:01 -0700207 // Pop the command off of the args list.
Alex Vakulenko3cb466c2014-04-15 11:36:32 -0700208 std::string command = args.front();
Chris Sosaababc5c2014-04-09 15:42:01 -0700209 args.erase(args.begin());
Christopher Wileya4915c42014-03-27 14:45:37 -0700210 bool success = false;
Chris Sosaababc5c2014-04-09 15:42:01 -0700211 if (command.compare(kRootTestMethod) == 0) {
Christopher Wileya4915c42014-03-27 14:45:37 -0700212 auto proxy = GetBuffetDBusProxy(
213 bus, buffet::dbus_constants::kRootServicePath);
214 success = CallTestMethod(proxy);
Alex Vakulenko3cb466c2014-04-15 11:36:32 -0700215 } else if (command.compare(kManagerCheckDeviceRegistered) == 0 ||
216 command.compare("cr") == 0) {
217 if (!args.empty()) {
Christopher Wileya4915c42014-03-27 14:45:37 -0700218 std::cerr << "Invalid number of arguments for "
Alex Vakulenko3cb466c2014-04-15 11:36:32 -0700219 << "Manager." << kManagerCheckDeviceRegistered << std::endl;
220 usage();
221 return EX_USAGE;
222 }
223 auto proxy = GetBuffetDBusProxy(
224 bus, buffet::dbus_constants::kManagerServicePath);
225 success = CallManagerCheckDeviceRegistered(proxy);
226 } else if (command.compare(kManagerGetDeviceInfo) == 0 ||
227 command.compare("di") == 0) {
228 if (!args.empty()) {
229 std::cerr << "Invalid number of arguments for "
230 << "Manager." << kManagerGetDeviceInfo << std::endl;
231 usage();
232 return EX_USAGE;
233 }
234 auto proxy = GetBuffetDBusProxy(
235 bus, buffet::dbus_constants::kManagerServicePath);
236 success = CallManagerGetDeviceInfo(proxy);
237 } else if (command.compare(kManagerStartRegisterDevice) == 0 ||
238 command.compare("sr") == 0) {
239 if (args.size() > 1) {
240 std::cerr << "Invalid number of arguments for "
241 << "Manager." << kManagerStartRegisterDevice << std::endl;
Christopher Wileya4915c42014-03-27 14:45:37 -0700242 usage();
Chris Sosaababc5c2014-04-09 15:42:01 -0700243 return EX_USAGE;
Christopher Wileya4915c42014-03-27 14:45:37 -0700244 }
245 auto proxy = GetBuffetDBusProxy(
246 bus, buffet::dbus_constants::kManagerServicePath);
Alex Vakulenko3cb466c2014-04-15 11:36:32 -0700247 std::map<std::string, std::shared_ptr<base::Value>> params;
248
249 if (!args.empty()) {
250 auto key_values = chromeos::data_encoding::WebParamsDecode(args.front());
251 for (auto&& pair : key_values) {
252 params.insert(std::make_pair(
253 pair.first, std::shared_ptr<base::Value>(
254 base::Value::CreateStringValue(pair.second))));
255 }
256 }
257
258 success = CallManagerStartRegisterDevice(proxy, params);
259 } else if (command.compare(kManagerFinishRegisterDevice) == 0 ||
260 command.compare("fr") == 0) {
261 if (args.size() > 1) {
262 std::cerr << "Invalid number of arguments for "
263 << "Manager." << kManagerFinishRegisterDevice << std::endl;
264 usage();
265 return EX_USAGE;
266 }
267 auto proxy = GetBuffetDBusProxy(
268 bus, buffet::dbus_constants::kManagerServicePath);
269 success = CallManagerFinishRegisterDevice(
270 proxy, !args.empty() ? args.front() : std::string());
271 } else if (command.compare(kManagerUpdateStateMethod) == 0 ||
272 command.compare("us") == 0) {
Chris Sosaababc5c2014-04-09 15:42:01 -0700273 if (args.size() != 1) {
Christopher Wileya4915c42014-03-27 14:45:37 -0700274 std::cerr << "Invalid number of arguments for "
Alex Vakulenko3cb466c2014-04-15 11:36:32 -0700275 << "Manager." << kManagerUpdateStateMethod << std::endl;
Christopher Wileya4915c42014-03-27 14:45:37 -0700276 usage();
Chris Sosaababc5c2014-04-09 15:42:01 -0700277 return EX_USAGE;
Christopher Wileya4915c42014-03-27 14:45:37 -0700278 }
279 auto proxy = GetBuffetDBusProxy(
280 bus, buffet::dbus_constants::kManagerServicePath);
Alex Vakulenko3cb466c2014-04-15 11:36:32 -0700281 success = CallManagerUpdateState(proxy, args.front());
Christopher Wileya4915c42014-03-27 14:45:37 -0700282 } else {
Chris Sosaababc5c2014-04-09 15:42:01 -0700283 std::cerr << "Unknown command: " << command << std::endl;
Christopher Wileya4915c42014-03-27 14:45:37 -0700284 usage();
Chris Sosaababc5c2014-04-09 15:42:01 -0700285 return EX_USAGE;
Christopher Wileya4915c42014-03-27 14:45:37 -0700286 }
287
288 if (success) {
289 std::cout << "Done." << std::endl;
Chris Sosaababc5c2014-04-09 15:42:01 -0700290 return EX_OK;
Christopher Wileya4915c42014-03-27 14:45:37 -0700291 }
292
Chris Sosaababc5c2014-04-09 15:42:01 -0700293 std::cerr << "Done, with errors." << std::endl;
294 return 1;
Chris Sosa45d9f102014-03-24 11:18:54 -0700295}
296