blob: f351040660adde2252ddd96383c0261c7e74689f [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"
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070019#include "buffet/data_encoding.h"
Chris Sosa45d9f102014-03-24 11:18:54 -070020
Christopher Wileya4915c42014-03-27 14:45:37 -070021using namespace buffet::dbus_constants;
Chris Sosa45d9f102014-03-24 11:18:54 -070022
23namespace {
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070024static const int default_timeout_ms = 1000;
Chris Sosa45d9f102014-03-24 11:18:54 -070025
Christopher Wileya4915c42014-03-27 14:45:37 -070026dbus::ObjectProxy* GetBuffetDBusProxy(dbus::Bus *bus,
27 const std::string& object_path) {
Chris Sosa45d9f102014-03-24 11:18:54 -070028 return bus->GetObjectProxy(
Christopher Wileyeb19fa02014-03-27 13:27:30 -070029 buffet::dbus_constants::kServiceName,
Christopher Wileya4915c42014-03-27 14:45:37 -070030 dbus::ObjectPath(object_path));
Chris Sosa45d9f102014-03-24 11:18:54 -070031}
32
Christopher Wileya4915c42014-03-27 14:45:37 -070033bool CallTestMethod(dbus::ObjectProxy* proxy) {
Christopher Wileyb76eb292014-05-05 16:09:16 -070034 dbus::MethodCall method_call(buffet::dbus_constants::kManagerInterface,
35 buffet::dbus_constants::kManagerTestMethod);
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070036 scoped_ptr<dbus::Response> response(
37 proxy->CallMethodAndBlock(&method_call, default_timeout_ms));
Chris Sosa45d9f102014-03-24 11:18:54 -070038 if (!response) {
Christopher Wileya4915c42014-03-27 14:45:37 -070039 std::cout << "Failed to receive a response." << std::endl;
40 return false;
Chris Sosa45d9f102014-03-24 11:18:54 -070041 }
Christopher Wileya4915c42014-03-27 14:45:37 -070042 std::cout << "Received a response." << std::endl;
43 return true;
Chris Sosa45d9f102014-03-24 11:18:54 -070044}
45
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070046bool CallManagerCheckDeviceRegistered(dbus::ObjectProxy* proxy) {
Christopher Wileya4915c42014-03-27 14:45:37 -070047 dbus::MethodCall method_call(
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070048 buffet::dbus_constants::kManagerInterface,
49 buffet::dbus_constants::kManagerCheckDeviceRegistered);
50
Christopher Wileya4915c42014-03-27 14:45:37 -070051 scoped_ptr<dbus::Response> response(
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070052 proxy->CallMethodAndBlock(&method_call, default_timeout_ms));
Christopher Wileya4915c42014-03-27 14:45:37 -070053 if (!response) {
54 std::cout << "Failed to receive a response." << std::endl;
55 return false;
Chris Sosa45d9f102014-03-24 11:18:54 -070056 }
57
Christopher Wileya4915c42014-03-27 14:45:37 -070058 dbus::MessageReader reader(response.get());
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070059 std::string device_id;
60 if (!reader.PopString(&device_id)) {
61 std::cout << "No device ID in response." << std::endl;
Christopher Wileya4915c42014-03-27 14:45:37 -070062 return false;
63 }
64
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070065 std::cout << "Device ID: "
66 << (device_id.empty() ? std::string("<unregistered>") : device_id)
67 << std::endl;
68 return true;
69}
70
71bool CallManagerGetDeviceInfo(dbus::ObjectProxy* proxy) {
72 dbus::MethodCall method_call(
73 buffet::dbus_constants::kManagerInterface,
74 buffet::dbus_constants::kManagerGetDeviceInfo);
75
76 scoped_ptr<dbus::Response> response(
77 proxy->CallMethodAndBlock(&method_call, default_timeout_ms));
78 if (!response) {
79 std::cout << "Failed to receive a response." << std::endl;
80 return false;
81 }
82
83 dbus::MessageReader reader(response.get());
84 std::string device_info;
85 if (!reader.PopString(&device_info)) {
86 std::cout << "No device info in response." << std::endl;
87 return false;
88 }
89
90 std::cout << "Device Info: "
91 << (device_info.empty() ? std::string("<unregistered>") : device_info)
92 << std::endl;
93 return true;
94}
95
96bool CallManagerStartRegisterDevice(
97 dbus::ObjectProxy* proxy,
98 const std::map<std::string, std::shared_ptr<base::Value>>& params) {
99 dbus::MethodCall method_call(
100 buffet::dbus_constants::kManagerInterface,
101 buffet::dbus_constants::kManagerStartRegisterDevice);
102 dbus::MessageWriter writer(&method_call);
103 dbus::MessageWriter dict_writer(nullptr);
104 writer.OpenArray("{sv}", &dict_writer);
105 for (auto&& pair : params) {
106 dbus::MessageWriter dict_entry_writer(nullptr);
107 dict_writer.OpenDictEntry(&dict_entry_writer);
108 dict_entry_writer.AppendString(pair.first);
109 dbus::AppendBasicTypeValueDataAsVariant(&dict_entry_writer,
110 *pair.second.get());
111 dict_writer.CloseContainer(&dict_entry_writer);
112 }
113 writer.CloseContainer(&dict_writer);
114
115 static const int timeout_ms = 3000;
116 scoped_ptr<dbus::Response> response(
117 proxy->CallMethodAndBlock(&method_call, timeout_ms));
118 if (!response) {
119 std::cout << "Failed to receive a response." << std::endl;
120 return false;
121 }
122
123 dbus::MessageReader reader(response.get());
124 std::string info;
125 if (!reader.PopString(&info)) {
126 std::cout << "No valid response." << std::endl;
127 return false;
128 }
129
130 std::cout << "Registration started: " << info << std::endl;
131 return true;
132}
133
134bool CallManagerFinishRegisterDevice(dbus::ObjectProxy* proxy,
135 const std::string& user_auth_code) {
136 dbus::MethodCall method_call(
137 buffet::dbus_constants::kManagerInterface,
138 buffet::dbus_constants::kManagerFinishRegisterDevice);
139 dbus::MessageWriter writer(&method_call);
140 writer.AppendString(user_auth_code);
141 static const int timeout_ms = 10000;
142 scoped_ptr<dbus::Response> response(
143 proxy->CallMethodAndBlock(&method_call, timeout_ms));
144 if (!response) {
145 std::cout << "Failed to receive a response." << std::endl;
146 return false;
147 }
148
149 dbus::MessageReader reader(response.get());
150 std::string device_id;
151 if (!reader.PopString(&device_id)) {
152 std::cout << "No device ID in response." << std::endl;
153 return false;
154 }
155
156 std::cout << "Device ID is "
157 << (device_id.empty() ? std::string("<unregistered>") : device_id)
158 << std::endl;
Christopher Wileya4915c42014-03-27 14:45:37 -0700159 return true;
160}
161
162bool CallManagerUpdateState(dbus::ObjectProxy* proxy,
163 const std::string& json_blob) {
164 dbus::MethodCall method_call(
165 buffet::dbus_constants::kManagerInterface,
166 buffet::dbus_constants::kManagerUpdateStateMethod);
167 dbus::MessageWriter writer(&method_call);
168 writer.AppendString(json_blob);
Christopher Wileya4915c42014-03-27 14:45:37 -0700169 scoped_ptr<dbus::Response> response(
Alex Vakulenko3cb466c2014-04-15 11:36:32 -0700170 proxy->CallMethodAndBlock(&method_call, default_timeout_ms));
Christopher Wileya4915c42014-03-27 14:45:37 -0700171 if (!response) {
172 std::cout << "Failed to receive a response." << std::endl;
173 return false;
174 }
175 return true;
176}
177
178void usage() {
179 std::cerr << "Possible commands:" << std::endl;
Christopher Wileyb76eb292014-05-05 16:09:16 -0700180 std::cerr << " " << kManagerTestMethod << std::endl;
Alex Vakulenko3cb466c2014-04-15 11:36:32 -0700181 std::cerr << " " << kManagerCheckDeviceRegistered << std::endl;
182 std::cerr << " " << kManagerGetDeviceInfo << std::endl;
183 std::cerr << " " << kManagerStartRegisterDevice
184 << " param1 = val1&param2 = val2..." << std::endl;
185 std::cerr << " " << kManagerFinishRegisterDevice
186 << " device_id" << std::endl;
Christopher Wileya4915c42014-03-27 14:45:37 -0700187 std::cerr << " " << kManagerUpdateStateMethod << std::endl;
188}
189
190} // namespace
191
192int main(int argc, char** argv) {
Chris Sosaababc5c2014-04-09 15:42:01 -0700193 CommandLine::Init(argc, argv);
194 CommandLine* cl = CommandLine::ForCurrentProcess();
195
Christopher Wileya4915c42014-03-27 14:45:37 -0700196 dbus::Bus::Options options;
197 options.bus_type = dbus::Bus::SYSTEM;
198 scoped_refptr<dbus::Bus> bus(new dbus::Bus(options));
199
Chris Sosaababc5c2014-04-09 15:42:01 -0700200 CommandLine::StringVector args = cl->GetArgs();
Alex Vakulenko3cb466c2014-04-15 11:36:32 -0700201 if (args.empty()) {
Christopher Wileya4915c42014-03-27 14:45:37 -0700202 usage();
Chris Sosaababc5c2014-04-09 15:42:01 -0700203 return EX_USAGE;
Christopher Wileya4915c42014-03-27 14:45:37 -0700204 }
205
Chris Sosaababc5c2014-04-09 15:42:01 -0700206 // Pop the command off of the args list.
Alex Vakulenko3cb466c2014-04-15 11:36:32 -0700207 std::string command = args.front();
Chris Sosaababc5c2014-04-09 15:42:01 -0700208 args.erase(args.begin());
Christopher Wileya4915c42014-03-27 14:45:37 -0700209 bool success = false;
Christopher Wileyb76eb292014-05-05 16:09:16 -0700210 if (command.compare(kManagerTestMethod) == 0) {
Christopher Wileya4915c42014-03-27 14:45:37 -0700211 auto proxy = GetBuffetDBusProxy(
Christopher Wileyb76eb292014-05-05 16:09:16 -0700212 bus, buffet::dbus_constants::kManagerServicePath);
Christopher Wileya4915c42014-03-27 14:45:37 -0700213 success = CallTestMethod(proxy);
Alex Vakulenko3cb466c2014-04-15 11:36:32 -0700214 } else if (command.compare(kManagerCheckDeviceRegistered) == 0 ||
215 command.compare("cr") == 0) {
216 if (!args.empty()) {
Christopher Wileya4915c42014-03-27 14:45:37 -0700217 std::cerr << "Invalid number of arguments for "
Alex Vakulenko3cb466c2014-04-15 11:36:32 -0700218 << "Manager." << kManagerCheckDeviceRegistered << std::endl;
219 usage();
220 return EX_USAGE;
221 }
222 auto proxy = GetBuffetDBusProxy(
223 bus, buffet::dbus_constants::kManagerServicePath);
224 success = CallManagerCheckDeviceRegistered(proxy);
225 } else if (command.compare(kManagerGetDeviceInfo) == 0 ||
226 command.compare("di") == 0) {
227 if (!args.empty()) {
228 std::cerr << "Invalid number of arguments for "
229 << "Manager." << kManagerGetDeviceInfo << std::endl;
230 usage();
231 return EX_USAGE;
232 }
233 auto proxy = GetBuffetDBusProxy(
234 bus, buffet::dbus_constants::kManagerServicePath);
235 success = CallManagerGetDeviceInfo(proxy);
236 } else if (command.compare(kManagerStartRegisterDevice) == 0 ||
237 command.compare("sr") == 0) {
238 if (args.size() > 1) {
239 std::cerr << "Invalid number of arguments for "
240 << "Manager." << kManagerStartRegisterDevice << std::endl;
Christopher Wileya4915c42014-03-27 14:45:37 -0700241 usage();
Chris Sosaababc5c2014-04-09 15:42:01 -0700242 return EX_USAGE;
Christopher Wileya4915c42014-03-27 14:45:37 -0700243 }
244 auto proxy = GetBuffetDBusProxy(
245 bus, buffet::dbus_constants::kManagerServicePath);
Alex Vakulenko3cb466c2014-04-15 11:36:32 -0700246 std::map<std::string, std::shared_ptr<base::Value>> params;
247
248 if (!args.empty()) {
249 auto key_values = chromeos::data_encoding::WebParamsDecode(args.front());
250 for (auto&& pair : key_values) {
251 params.insert(std::make_pair(
252 pair.first, std::shared_ptr<base::Value>(
253 base::Value::CreateStringValue(pair.second))));
254 }
255 }
256
257 success = CallManagerStartRegisterDevice(proxy, params);
258 } else if (command.compare(kManagerFinishRegisterDevice) == 0 ||
259 command.compare("fr") == 0) {
260 if (args.size() > 1) {
261 std::cerr << "Invalid number of arguments for "
262 << "Manager." << kManagerFinishRegisterDevice << std::endl;
263 usage();
264 return EX_USAGE;
265 }
266 auto proxy = GetBuffetDBusProxy(
267 bus, buffet::dbus_constants::kManagerServicePath);
268 success = CallManagerFinishRegisterDevice(
269 proxy, !args.empty() ? args.front() : std::string());
270 } else if (command.compare(kManagerUpdateStateMethod) == 0 ||
271 command.compare("us") == 0) {
Chris Sosaababc5c2014-04-09 15:42:01 -0700272 if (args.size() != 1) {
Christopher Wileya4915c42014-03-27 14:45:37 -0700273 std::cerr << "Invalid number of arguments for "
Alex Vakulenko3cb466c2014-04-15 11:36:32 -0700274 << "Manager." << kManagerUpdateStateMethod << std::endl;
Christopher Wileya4915c42014-03-27 14:45:37 -0700275 usage();
Chris Sosaababc5c2014-04-09 15:42:01 -0700276 return EX_USAGE;
Christopher Wileya4915c42014-03-27 14:45:37 -0700277 }
278 auto proxy = GetBuffetDBusProxy(
279 bus, buffet::dbus_constants::kManagerServicePath);
Alex Vakulenko3cb466c2014-04-15 11:36:32 -0700280 success = CallManagerUpdateState(proxy, args.front());
Christopher Wileya4915c42014-03-27 14:45:37 -0700281 } else {
Chris Sosaababc5c2014-04-09 15:42:01 -0700282 std::cerr << "Unknown command: " << command << std::endl;
Christopher Wileya4915c42014-03-27 14:45:37 -0700283 usage();
Chris Sosaababc5c2014-04-09 15:42:01 -0700284 return EX_USAGE;
Christopher Wileya4915c42014-03-27 14:45:37 -0700285 }
286
287 if (success) {
288 std::cout << "Done." << std::endl;
Chris Sosaababc5c2014-04-09 15:42:01 -0700289 return EX_OK;
Christopher Wileya4915c42014-03-27 14:45:37 -0700290 }
291
Chris Sosaababc5c2014-04-09 15:42:01 -0700292 std::cerr << "Done, with errors." << std::endl;
293 return 1;
Chris Sosa45d9f102014-03-24 11:18:54 -0700294}
295