deviced: Remove build warnings and do not use thread unsafey functions
[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 <dbus/dbus.h>
24 #include <shared/dbus.h>
25 #include <core/common.h>
26 #include "usb.h"
27
28 /*
29  * devicectl [device] [action]
30  * ex> devicectl display stop
31  *     devicectl pass start
32  */
33
34 enum device_type {
35         DEVICE_CORE,
36         DEVICE_DISPLAY,
37         DEVICE_LED,
38         DEVICE_PASS,
39         DEVICE_USB,
40         DEVICE_EXTCON,
41         DEVICE_MAX,
42         DEVICE_ALL,
43 };
44 static enum device_type arg_id;
45
46 static const struct device {
47         const enum device_type id;
48         const char *name;
49         const char *path;
50         const char *iface;
51 } devices[] = {
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  },
58 };
59
60 static int start_device(char **args)
61 {
62         DBusMessage *msg;
63
64         if (!args[1])
65                 return -EINVAL;
66
67         printf("start %s device!\n", args[1]);
68
69         msg = dbus_method_sync_with_reply(DEVICED_BUS_NAME,
70                     devices[arg_id].path, devices[arg_id].iface,
71                     "start", NULL, NULL);
72         if (!msg)
73                 return -EBADMSG;
74
75         dbus_message_unref(msg);
76
77         return 0;
78 }
79
80 static int stop_device(char **args)
81 {
82         DBusMessage *msg;
83
84         if (!args[1])
85                 return -EINVAL;
86
87         printf("stop %s device!\n", args[1]);
88
89         msg = dbus_method_sync_with_reply(DEVICED_BUS_NAME,
90                     devices[arg_id].path, devices[arg_id].iface,
91                     "stop", NULL, NULL);
92         if (!msg)
93                 return -EBADMSG;
94
95         dbus_message_unref(msg);
96
97         return 0;
98 }
99
100 static int dump_mode(char **args)
101 {
102         DBusError err;
103         DBusMessage *msg;
104         int ret, val;
105         char *arr[1];
106
107         if (!args[1] || !args[2] || !args[3])
108                 return -EINVAL;
109
110         printf("%s (%s %s)!\n", args[1], args[2], args[3]);
111
112         arr[0] = 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);
116         if (!msg)
117                 return -EBADMSG;
118
119         dbus_error_init(&err);
120
121         ret = dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &val, DBUS_TYPE_INVALID);
122         if (!ret) {
123                 printf("no message : [%s:%s]", err.name, err.message);
124                 dbus_error_free(&err);
125                 val = -ENOMSG;
126         }
127
128         dbus_message_unref(msg);
129         return val;
130 }
131
132 static int save_log(char **args)
133 {
134         DBusMessage *msg;
135
136         if (!args[1])
137                 return -EINVAL;
138
139         printf("save log %s device!\n", args[1]);
140
141         msg = dbus_method_sync_with_reply(DEVICED_BUS_NAME,
142                     devices[arg_id].path, devices[arg_id].iface,
143                     "SaveLog", NULL, NULL);
144         if (!msg)
145                 return -EBADMSG;
146
147         dbus_message_unref(msg);
148
149         return 0;
150 }
151
152 static int set_usb_mode(char **args)
153 {
154         return load_usb_mode(args[3]);
155 }
156
157 static int unset_usb_mode(char **args)
158 {
159         return unload_usb_mode(args[3]);
160 }
161
162 static int enable_device(char **args)
163 {
164         DBusMessage *msg;
165         char *arr[1];
166
167         if (!args[3])
168                 return -EINVAL;
169
170         printf("enable %s device!\n", args[3]);
171
172         arr[0] = args[3];
173
174         msg = dbus_method_sync_with_reply(DEVICED_BUS_NAME,
175                     devices[arg_id].path, devices[arg_id].iface,
176                     "enable", "s", arr);
177         if (!msg)
178                 return -EBADMSG;
179
180         dbus_message_unref(msg);
181
182         return 0;
183 }
184
185 static int disable_device(char **args)
186 {
187         DBusMessage *msg;
188         char *arr[1];
189
190         if (!args[3])
191                 return -EINVAL;
192
193         printf("disable %s device!\n", args[3]);
194
195         arr[0] = args[3];
196
197         msg = dbus_method_sync_with_reply(DEVICED_BUS_NAME,
198                     devices[arg_id].path, devices[arg_id].iface,
199                     "disable", "s", arr);
200         if (!msg)
201                 return -EBADMSG;
202
203         dbus_message_unref(msg);
204
205         return 0;
206 }
207
208 static const struct action {
209         const enum device_type id;
210         const char *action;
211         const int argc;
212         int (* const func)(char **args);
213         const char *option;
214 } actions[] = {
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]" },
224 };
225
226 static inline void usage()
227 {
228         printf("[usage] devicectl <device_name> <action>\n");
229         printf("Please use option --help to check options\n");
230 }
231
232 static void help()
233 {
234         int i;
235
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,
241                             actions[i].option);
242                 } else {
243                         printf("    %s %s %s\n", devices[actions[i].id].name,
244                             actions[i].action, actions[i].option);
245                 }
246         }
247 }
248
249 int main(int argc, char *argv[])
250 {
251         int i;
252
253         if (argc == 2 && !strcmp(argv[1], "--help")) {
254                 help();
255                 return 0;
256         }
257
258         if (argc < 3) {
259                 usage();
260                 return -EINVAL;
261         }
262
263         for (i = 0; i < argc; i++)
264                 if (argv[i] == NULL) {
265                         usage();
266                         return -EINVAL;
267                 }
268
269         for (i = 0; i < ARRAY_SIZE(devices); i++)
270                 if (!strcmp(argv[1], devices[i].name))
271                         break;
272
273         if (i >= ARRAY_SIZE(devices)) {
274                 printf("invalid device name! %s\n", argv[1]);
275                 usage();
276                 return -EINVAL;
277         }
278
279         arg_id = devices[i].id;
280
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))
284                                 break;
285
286         if (i >= ARRAY_SIZE(actions)) {
287                 printf("invalid action name! %s\n", argv[2]);
288                 usage();
289                 return -EINVAL;
290         }
291
292         if (actions[i].argc != argc) {
293                 printf("invalid arg count!\n");
294                 usage();
295                 return -EINVAL;
296         }
297
298         return actions[i].func(argv);
299 }
300