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.
24 #include <dbus/dbus.h>
25 #include <shared/dbus.h>
26 #include <core/common.h>
30 * devicectl [device] [action]
31 * ex> devicectl display stop
32 * devicectl pass start
45 static enum device_type arg_id;
47 static const struct device {
48 const enum device_type id;
53 { DEVICE_CORE, "core", DEVICED_PATH_CORE, DEVICED_INTERFACE_CORE },
54 { DEVICE_DISPLAY, "display", DEVICED_PATH_DISPLAY, DEVICED_INTERFACE_DISPLAY },
55 { DEVICE_LED, "led", DEVICED_PATH_LED, DEVICED_INTERFACE_LED },
56 { DEVICE_PASS, "pass", DEVICED_PATH_PASS, DEVICED_INTERFACE_PASS },
57 { DEVICE_USB, "usb", DEVICED_PATH_USB, DEVICED_INTERFACE_USB },
58 { DEVICE_EXTCON, "extcon", DEVICED_PATH_EXTCON, DEVICED_INTERFACE_EXTCON },
61 static int start_device(char **args)
68 printf("start %s device!\n", args[1]);
70 msg = dbus_method_sync_with_reply(DEVICED_BUS_NAME,
71 devices[arg_id].path, devices[arg_id].iface,
76 dbus_message_unref(msg);
81 static int stop_device(char **args)
88 printf("stop %s device!\n", args[1]);
90 msg = dbus_method_sync_with_reply(DEVICED_BUS_NAME,
91 devices[arg_id].path, devices[arg_id].iface,
96 dbus_message_unref(msg);
101 static int dump_mode(char **args)
106 if (!args[1] || !args[2] || !args[3])
109 printf("%s (%s %s)!\n", args[1], args[2], args[3]);
112 ret = dbus_method_async(DEVICED_BUS_NAME,
113 devices[arg_id].path, devices[arg_id].iface,
114 "Dumpmode", "s", arr);
116 printf("failed to set dump mode (%d)", ret);
121 static int save_log(char **args)
128 printf("save log %s device!\n", args[1]);
130 ret = dbus_method_async(DEVICED_BUS_NAME,
131 devices[arg_id].path, devices[arg_id].iface,
132 "SaveLog", NULL, NULL);
134 printf("failed to save log (%d)", ret);
139 static void get_pname(pid_t pid, char *pname)
144 snprintf(buf, PATH_MAX, "/proc/%d/cmdline", pid);
145 cmdline = open(buf, O_RDONLY);
151 r = read(cmdline, pname, PATH_MAX);
152 if ((r >= 0) && (r < PATH_MAX))
160 static int save_dbus_name(char **args)
167 char pname[PATH_MAX];
172 printf("save dbus name!\n");
174 msg = dbus_method_sync_with_reply(DBUS_BUS_NAME,
175 DBUS_OBJECT_PATH, DBUS_INTERFACE_NAME,
176 "ListNames", NULL, NULL);
178 printf("failed to get list names");
182 ret = dbus_message_get_args(msg, NULL, DBUS_TYPE_ARRAY,
183 DBUS_TYPE_STRING, &list, &size, DBUS_TYPE_INVALID);
184 dbus_message_unref(msg);
186 printf("invalid list name arguments!");
190 printf("%d connections\n", size);
192 for (i = 0; i < size; i++) {
194 msg = dbus_method_sync_with_reply(DBUS_BUS_NAME,
195 DBUS_OBJECT_PATH, DBUS_INTERFACE_NAME,
196 "GetConnectionUnixProcessID", "s", arr);
200 ret = dbus_message_get_args(msg, NULL, DBUS_TYPE_UINT32,
201 &pid, DBUS_TYPE_INVALID);
202 dbus_message_unref(msg);
206 get_pname(pid, pname);
207 printf("%6d %6s %s\n", pid, list[i], pname);
214 static int device_list(char **args)
221 printf("print %s to dlog!\n", args[1]);
223 msg = dbus_method_sync_with_reply(DEVICED_BUS_NAME,
224 devices[arg_id].path, devices[arg_id].iface,
225 "DeviceList", NULL, NULL);
229 dbus_message_unref(msg);
234 static int set_usb_mode(char **args)
236 return load_usb_mode(args[3]);
239 static int unset_usb_mode(char **args)
241 return unload_usb_mode(args[3]);
244 static int enable_device(char **args)
252 printf("enable %s device!\n", args[3]);
256 msg = dbus_method_sync_with_reply(DEVICED_BUS_NAME,
257 devices[arg_id].path, devices[arg_id].iface,
262 dbus_message_unref(msg);
267 static int disable_device(char **args)
275 printf("disable %s device!\n", args[3]);
279 msg = dbus_method_sync_with_reply(DEVICED_BUS_NAME,
280 devices[arg_id].path, devices[arg_id].iface,
281 "disable", "s", arr);
285 dbus_message_unref(msg);
290 static const struct action {
291 const enum device_type id;
294 int (* const func)(char **args);
297 { DEVICE_ALL, "start", 3, start_device, "" },
298 { DEVICE_ALL, "stop", 3, stop_device, "" },
299 { DEVICE_DISPLAY, "dumpmode", 4, dump_mode, "[on|off]" },
300 { DEVICE_LED, "dumpmode", 4, dump_mode, "[on|off]" },
301 { DEVICE_DISPLAY, "savelog", 3, save_log, "" },
302 { DEVICE_USB, "set", 4, set_usb_mode, "[sdb|ssh]" },
303 { DEVICE_USB, "unset", 4, unset_usb_mode, "[sdb|ssh]" },
304 { DEVICE_CORE, "dbusname", 3, save_dbus_name, "" },
305 { DEVICE_CORE, "devicelist", 3, device_list, "" },
306 { DEVICE_EXTCON, "enable", 4, enable_device, "[USB|HEADPHONE|HDMI|DOCK]" },
307 { DEVICE_EXTCON, "disable", 4, disable_device, "[USB|HEADPHONE|HDMI|DOCK]" },
310 static inline void usage()
312 printf("[usage] devicectl <device_name> <action>\n");
313 printf("Please use option --help to check options\n");
320 printf("[usage] devicectl <device_name> <action> <option>\n");
321 printf("device name & action & option\n");
322 for (i = 0; i < ARRAY_SIZE(actions); i++) {
323 if (actions[i].id == DEVICE_ALL) {
324 printf(" [all-device] %s %s\n", actions[i].action,
327 printf(" %s %s %s\n", devices[actions[i].id].name,
328 actions[i].action, actions[i].option);
333 int main(int argc, char *argv[])
337 if (argc == 2 && !strcmp(argv[1], "--help")) {
347 for (i = 0; i < argc; i++)
348 if (argv[i] == NULL) {
353 for (i = 0; i < ARRAY_SIZE(devices); i++)
354 if (!strcmp(argv[1], devices[i].name))
357 if (i >= ARRAY_SIZE(devices)) {
358 printf("invalid device name! %s\n", argv[1]);
363 arg_id = devices[i].id;
365 for (i = 0; i < ARRAY_SIZE(actions); i++)
366 if (actions[i].id == arg_id || actions[i].id == DEVICE_ALL)
367 if (!strcmp(argv[2], actions[i].action))
370 if (i >= ARRAY_SIZE(actions)) {
371 printf("invalid action name! %s\n", argv[2]);
376 if (actions[i].argc != argc) {
377 printf("invalid arg count!\n");
382 return actions[i].func(argv);