4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
23 #include <dbus/dbus.h>
24 #include <shared/dbus.h>
25 #include <core/common.h>
29 * devicectl [device] [action]
30 * ex> devicectl display stop
31 * devicectl pass start
43 static enum device_type arg_id;
45 static const struct device {
46 const enum device_type id;
51 { DEVICE_CORE, "core", DEVICED_PATH_CORE, DEVICED_INTERFACE_CORE },
52 { DEVICE_DISPLAY, "display", DEVICED_PATH_DISPLAY, DEVICED_INTERFACE_DISPLAY },
53 { DEVICE_LED, "led", DEVICED_PATH_LED, DEVICED_INTERFACE_LED },
54 { DEVICE_PASS, "pass", DEVICED_PATH_PASS, DEVICED_INTERFACE_PASS },
55 { DEVICE_USB, "usb", DEVICED_PATH_USB, DEVICED_INTERFACE_USB },
58 static int start_device(char **args)
65 printf("start %s device!\n", args[1]);
67 msg = dbus_method_sync_with_reply(DEVICED_BUS_NAME,
68 devices[arg_id].path, devices[arg_id].iface,
73 dbus_message_unref(msg);
78 static int stop_device(char **args)
85 printf("start %s device!\n", args[1]);
87 msg = dbus_method_sync_with_reply(DEVICED_BUS_NAME,
88 devices[arg_id].path, devices[arg_id].iface,
93 dbus_message_unref(msg);
98 static int dump_mode(char **args)
105 if (!args[1] || !args[2] || !args[3])
108 printf("%s (%s %s)!\n", args[1], args[2], args[3]);
111 msg = dbus_method_sync_with_reply(DEVICED_BUS_NAME,
112 devices[arg_id].path, devices[arg_id].iface,
113 "Dumpmode", "s", arr);
117 dbus_error_init(&err);
119 ret = dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &val, DBUS_TYPE_INVALID);
121 printf("no message : [%s:%s]", err.name, err.message);
122 dbus_error_free(&err);
126 dbus_message_unref(msg);
130 static int save_log(char **args)
137 printf("save log %s device!\n", args[1]);
139 msg = dbus_method_sync_with_reply(DEVICED_BUS_NAME,
140 devices[arg_id].path, devices[arg_id].iface,
141 "SaveLog", NULL, NULL);
145 dbus_message_unref(msg);
150 static int set_usb_mode(char **args)
152 return load_usb_mode(args[3]);
155 static int unset_usb_mode(char **args)
157 return unload_usb_mode(args[3]);
160 static const struct action {
161 const enum device_type id;
164 int (* const func)(char **args);
167 { DEVICE_ALL, "start", 3, start_device, "" },
168 { DEVICE_ALL, "stop", 3, stop_device, "" },
169 { DEVICE_DISPLAY, "dumpmode", 4, dump_mode, "[on|off]" },
170 { DEVICE_LED, "dumpmode", 4, dump_mode, "[on|off]" },
171 { DEVICE_DISPLAY, "savelog", 3, save_log, "" },
172 { DEVICE_USB, "set", 4, set_usb_mode, "[sdb|ssh]" },
173 { DEVICE_USB, "unset", 4, unset_usb_mode, "[sdb|ssh]" },
176 static inline void usage()
178 printf("[usage] devicectl <device_name> <action>\n");
179 printf("Please use option --help to check options\n");
186 printf("[usage] devicectl <device_name> <action> <option>\n");
187 printf("device name & action & option\n");
188 for (i = 0; i < ARRAY_SIZE(actions); i++) {
189 if (actions[i].id == DEVICE_ALL) {
190 printf(" [all-device] %s %s\n", actions[i].action,
193 printf(" %s %s %s\n", devices[actions[i].id].name,
194 actions[i].action, actions[i].option);
199 int main(int argc, char *argv[])
203 if (argc == 2 && !strcmp(argv[1], "--help")) {
213 for (i = 0; i < argc; i++)
214 if (argv[i] == NULL) {
219 for (i = 0; i < ARRAY_SIZE(devices); i++)
220 if (!strcmp(argv[1], devices[i].name))
223 if (i >= ARRAY_SIZE(devices)) {
224 printf("invalid device name! %s\n", argv[1]);
229 arg_id = devices[i].id;
231 for (i = 0; i < ARRAY_SIZE(actions); i++)
232 if (actions[i].id == arg_id || actions[i].id == DEVICE_ALL)
233 if (!strcmp(argv[2], actions[i].action))
236 if (i >= ARRAY_SIZE(actions)) {
237 printf("invalid action name! %s\n", argv[2]);
242 if (actions[i].argc != argc) {
243 printf("invalid arg count!\n");
248 return actions[i].func(argv);