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
44 static enum device_type arg_id;
46 static const struct device {
47 const enum device_type id;
52 { DEVICE_CORE, "core", DEVICED_PATH_CORE, DEVICED_INTERFACE_CORE },
53 { DEVICE_DISPLAY, "display", DEVICED_PATH_DISPLAY, DEVICED_INTERFACE_DISPLAY },
54 { DEVICE_LED, "led", DEVICED_PATH_LED, DEVICED_INTERFACE_LED },
55 { DEVICE_PASS, "pass", DEVICED_PATH_PASS, DEVICED_INTERFACE_PASS },
56 { DEVICE_USB, "usb", DEVICED_PATH_USB, DEVICED_INTERFACE_USB },
57 { DEVICE_EXTCON, "extcon", DEVICED_PATH_EXTCON, DEVICED_INTERFACE_EXTCON },
60 static int start_device(char **args)
67 printf("start %s device!\n", args[1]);
69 msg = dbus_method_sync_with_reply(DEVICED_BUS_NAME,
70 devices[arg_id].path, devices[arg_id].iface,
75 dbus_message_unref(msg);
80 static int stop_device(char **args)
87 printf("stop %s device!\n", args[1]);
89 msg = dbus_method_sync_with_reply(DEVICED_BUS_NAME,
90 devices[arg_id].path, devices[arg_id].iface,
95 dbus_message_unref(msg);
100 static int dump_mode(char **args)
107 if (!args[1] || !args[2] || !args[3])
110 printf("%s (%s %s)!\n", args[1], args[2], args[3]);
113 msg = dbus_method_sync_with_reply(DEVICED_BUS_NAME,
114 devices[arg_id].path, devices[arg_id].iface,
115 "Dumpmode", "s", arr);
119 dbus_error_init(&err);
121 ret = dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &val, DBUS_TYPE_INVALID);
123 printf("no message : [%s:%s]", err.name, err.message);
124 dbus_error_free(&err);
128 dbus_message_unref(msg);
132 static int save_log(char **args)
139 printf("save log %s device!\n", args[1]);
141 msg = dbus_method_sync_with_reply(DEVICED_BUS_NAME,
142 devices[arg_id].path, devices[arg_id].iface,
143 "SaveLog", NULL, NULL);
147 dbus_message_unref(msg);
152 static int set_usb_mode(char **args)
154 return load_usb_mode(args[3]);
157 static int unset_usb_mode(char **args)
159 return unload_usb_mode(args[3]);
162 static int enable_device(char **args)
170 printf("enable %s device!\n", args[3]);
174 msg = dbus_method_sync_with_reply(DEVICED_BUS_NAME,
175 devices[arg_id].path, devices[arg_id].iface,
180 dbus_message_unref(msg);
185 static int disable_device(char **args)
193 printf("disable %s device!\n", args[3]);
197 msg = dbus_method_sync_with_reply(DEVICED_BUS_NAME,
198 devices[arg_id].path, devices[arg_id].iface,
199 "disable", "s", arr);
203 dbus_message_unref(msg);
208 static const struct action {
209 const enum device_type id;
212 int (* const func)(char **args);
215 { DEVICE_ALL, "start", 3, start_device, "" },
216 { DEVICE_ALL, "stop", 3, stop_device, "" },
217 { DEVICE_DISPLAY, "dumpmode", 4, dump_mode, "[on|off]" },
218 { DEVICE_LED, "dumpmode", 4, dump_mode, "[on|off]" },
219 { DEVICE_DISPLAY, "savelog", 3, save_log, "" },
220 { DEVICE_USB, "set", 4, set_usb_mode, "[sdb|ssh]" },
221 { DEVICE_USB, "unset", 4, unset_usb_mode, "[sdb|ssh]" },
222 { DEVICE_EXTCON, "enable", 4, enable_device, "[USB|HEADPHONE|HDMI|DOCK]" },
223 { DEVICE_EXTCON, "disable", 4, disable_device, "[USB|HEADPHONE|HDMI|DOCK]" },
226 static inline void usage()
228 printf("[usage] devicectl <device_name> <action>\n");
229 printf("Please use option --help to check options\n");
236 printf("[usage] devicectl <device_name> <action> <option>\n");
237 printf("device name & action & option\n");
238 for (i = 0; i < ARRAY_SIZE(actions); i++) {
239 if (actions[i].id == DEVICE_ALL) {
240 printf(" [all-device] %s %s\n", actions[i].action,
243 printf(" %s %s %s\n", devices[actions[i].id].name,
244 actions[i].action, actions[i].option);
249 int main(int argc, char *argv[])
253 if (argc == 2 && !strcmp(argv[1], "--help")) {
263 for (i = 0; i < argc; i++)
264 if (argv[i] == NULL) {
269 for (i = 0; i < ARRAY_SIZE(devices); i++)
270 if (!strcmp(argv[1], devices[i].name))
273 if (i >= ARRAY_SIZE(devices)) {
274 printf("invalid device name! %s\n", argv[1]);
279 arg_id = devices[i].id;
281 for (i = 0; i < ARRAY_SIZE(actions); i++)
282 if (actions[i].id == arg_id || actions[i].id == DEVICE_ALL)
283 if (!strcmp(argv[2], actions[i].action))
286 if (i >= ARRAY_SIZE(actions)) {
287 printf("invalid action name! %s\n", argv[2]);
292 if (actions[i].argc != argc) {
293 printf("invalid arg count!\n");
298 return actions[i].func(argv);