devicectl: fix misstype issue
[platform/core/system/deviced.git] / src / devicectl / devicectl.c
1 /*
2  * devicectl
3  *
4  * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  */
19
20 #include <stdio.h>
21 #include <string.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <dbus/dbus.h>
25 #include <shared/dbus.h>
26 #include <core/common.h>
27 #include "usb.h"
28
29 /*
30  * devicectl [device] [action]
31  * ex> devicectl display stop
32  *     devicectl pass start
33  */
34
35 enum device_type {
36         DEVICE_CORE,
37         DEVICE_DISPLAY,
38         DEVICE_LED,
39         DEVICE_PASS,
40         DEVICE_USB,
41         DEVICE_EXTCON,
42         DEVICE_MAX,
43         DEVICE_ALL,
44 };
45 static enum device_type arg_id;
46
47 static const struct device {
48         const enum device_type id;
49         const char *name;
50         const char *path;
51         const char *iface;
52 } devices[] = {
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  },
59 };
60
61 static int start_device(char **args)
62 {
63         DBusMessage *msg;
64
65         if (!args[1])
66                 return -EINVAL;
67
68         printf("start %s device!\n", args[1]);
69
70         msg = dbus_method_sync_with_reply(DEVICED_BUS_NAME,
71                     devices[arg_id].path, devices[arg_id].iface,
72                     "start", NULL, NULL);
73         if (!msg)
74                 return -EBADMSG;
75
76         dbus_message_unref(msg);
77
78         return 0;
79 }
80
81 static int stop_device(char **args)
82 {
83         DBusMessage *msg;
84
85         if (!args[1])
86                 return -EINVAL;
87
88         printf("stop %s device!\n", args[1]);
89
90         msg = dbus_method_sync_with_reply(DEVICED_BUS_NAME,
91                     devices[arg_id].path, devices[arg_id].iface,
92                     "stop", NULL, NULL);
93         if (!msg)
94                 return -EBADMSG;
95
96         dbus_message_unref(msg);
97
98         return 0;
99 }
100
101 static int dump_mode(char **args)
102 {
103         int ret;
104         char *arr[1];
105
106         if (!args[1] || !args[2] || !args[3])
107                 return -EINVAL;
108
109         printf("%s (%s %s)!\n", args[1], args[2], args[3]);
110
111         arr[0] = args[3];
112         ret = dbus_method_async(DEVICED_BUS_NAME,
113                     devices[arg_id].path, devices[arg_id].iface,
114                     "Dumpmode", "s", arr);
115         if (ret < 0)
116                 printf("failed to set dump mode (%d)", ret);
117
118         return ret;
119 }
120
121 static int save_log(char **args)
122 {
123         int ret;
124
125         if (!args[1])
126                 return -EINVAL;
127
128         printf("save log %s device!\n", args[1]);
129
130         ret = dbus_method_async(DEVICED_BUS_NAME,
131                     devices[arg_id].path, devices[arg_id].iface,
132                     "SaveLog", NULL, NULL);
133         if (ret < 0)
134                 printf("failed to save log (%d)", ret);
135
136         return ret;
137 }
138
139 static void get_pname(pid_t pid, char *pname)
140 {
141         char buf[PATH_MAX];
142         int cmdline, r;
143
144         snprintf(buf, PATH_MAX, "/proc/%d/cmdline", pid);
145         cmdline = open(buf, O_RDONLY);
146         if (cmdline < 0) {
147                 pname[0] = '\0';
148                 return;
149         }
150
151         r = read(cmdline, pname, PATH_MAX);
152         if ((r >= 0) && (r < PATH_MAX))
153                 pname[r] = '\0';
154         else
155                 pname[0] = '\0';
156
157         close(cmdline);
158 }
159
160 static int save_dbus_name(char **args)
161 {
162         DBusMessage *msg;
163         char **list;
164         int ret, size, i;
165         pid_t pid;
166         char *arr[1];
167         char pname[PATH_MAX];
168
169         if (!args[1])
170                 return -EINVAL;
171
172         printf("save dbus name!\n");
173
174         msg = dbus_method_sync_with_reply(DBUS_BUS_NAME,
175             DBUS_OBJECT_PATH, DBUS_INTERFACE_NAME,
176             "ListNames", NULL, NULL);
177         if (!msg) {
178                 printf("failed to get list names");
179                 return -EBADMSG;
180         }
181
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);
185         if (!ret) {
186                 printf("invalid list name arguments!");
187                 return -EINVAL;
188         }
189
190         printf("%d connections\n", size);
191
192         for (i = 0; i < size; i++) {
193                 arr[0] = list[i];
194                 msg = dbus_method_sync_with_reply(DBUS_BUS_NAME,
195                     DBUS_OBJECT_PATH, DBUS_INTERFACE_NAME,
196                     "GetConnectionUnixProcessID", "s", arr);
197                 if (!msg)
198                         continue;
199
200                 ret = dbus_message_get_args(msg, NULL, DBUS_TYPE_UINT32,
201                     &pid, DBUS_TYPE_INVALID);
202                 dbus_message_unref(msg);
203                 if (!ret)
204                         continue;
205
206                 get_pname(pid, pname);
207                 printf("%6d  %6s  %s\n", pid, list[i], pname);
208
209         }
210
211         return 0;
212 }
213
214 static int device_list(char **args)
215 {
216         DBusMessage *msg;
217
218         if (!args[1])
219                 return -EINVAL;
220
221         printf("print %s to dlog!\n", args[1]);
222
223         msg = dbus_method_sync_with_reply(DEVICED_BUS_NAME,
224                     devices[arg_id].path, devices[arg_id].iface,
225                     "DeviceList", NULL, NULL);
226         if (!msg)
227                 return -EBADMSG;
228
229         dbus_message_unref(msg);
230
231         return 0;
232 }
233
234 static int set_usb_mode(char **args)
235 {
236         return load_usb_mode(args[3]);
237 }
238
239 static int unset_usb_mode(char **args)
240 {
241         return unload_usb_mode(args[3]);
242 }
243
244 static int enable_device(char **args)
245 {
246         DBusMessage *msg;
247         char *arr[1];
248
249         if (!args[3])
250                 return -EINVAL;
251
252         printf("enable %s device!\n", args[3]);
253
254         arr[0] = args[3];
255
256         msg = dbus_method_sync_with_reply(DEVICED_BUS_NAME,
257                     devices[arg_id].path, devices[arg_id].iface,
258                     "enable", "s", arr);
259         if (!msg)
260                 return -EBADMSG;
261
262         dbus_message_unref(msg);
263
264         return 0;
265 }
266
267 static int disable_device(char **args)
268 {
269         DBusMessage *msg;
270         char *arr[1];
271
272         if (!args[3])
273                 return -EINVAL;
274
275         printf("disable %s device!\n", args[3]);
276
277         arr[0] = args[3];
278
279         msg = dbus_method_sync_with_reply(DEVICED_BUS_NAME,
280                     devices[arg_id].path, devices[arg_id].iface,
281                     "disable", "s", arr);
282         if (!msg)
283                 return -EBADMSG;
284
285         dbus_message_unref(msg);
286
287         return 0;
288 }
289
290 static const struct action {
291         const enum device_type id;
292         const char *action;
293         const int argc;
294         int (* const func)(char **args);
295         const char *option;
296 } actions[] = {
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]" },
308 };
309
310 static inline void usage()
311 {
312         printf("[usage] devicectl <device_name> <action>\n");
313         printf("Please use option --help to check options\n");
314 }
315
316 static void help()
317 {
318         int i;
319
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,
325                             actions[i].option);
326                 } else {
327                         printf("    %s %s %s\n", devices[actions[i].id].name,
328                             actions[i].action, actions[i].option);
329                 }
330         }
331 }
332
333 int main(int argc, char *argv[])
334 {
335         int i;
336
337         if (argc == 2 && !strcmp(argv[1], "--help")) {
338                 help();
339                 return 0;
340         }
341
342         if (argc < 3) {
343                 usage();
344                 return -EINVAL;
345         }
346
347         for (i = 0; i < argc; i++)
348                 if (argv[i] == NULL) {
349                         usage();
350                         return -EINVAL;
351                 }
352
353         for (i = 0; i < ARRAY_SIZE(devices); i++)
354                 if (!strcmp(argv[1], devices[i].name))
355                         break;
356
357         if (i >= ARRAY_SIZE(devices)) {
358                 printf("invalid device name! %s\n", argv[1]);
359                 usage();
360                 return -EINVAL;
361         }
362
363         arg_id = devices[i].id;
364
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))
368                                 break;
369
370         if (i >= ARRAY_SIZE(actions)) {
371                 printf("invalid action name! %s\n", argv[2]);
372                 usage();
373                 return -EINVAL;
374         }
375
376         if (actions[i].argc != argc) {
377                 printf("invalid arg count!\n");
378                 usage();
379                 return -EINVAL;
380         }
381
382         return actions[i].func(argv);
383 }
384