tizen 2.3 release
[framework/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_MAX,
41         DEVICE_ALL,
42 };
43 static enum device_type arg_id;
44
45 static const struct device {
46         const enum device_type id;
47         const char *name;
48         const char *path;
49         const char *iface;
50 } devices[] = {
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     },
56 };
57
58 static int start_device(char **args)
59 {
60         DBusMessage *msg;
61
62         if (!args[1])
63                 return -EINVAL;
64
65         printf("start %s device!\n", args[1]);
66
67         msg = dbus_method_sync_with_reply(DEVICED_BUS_NAME,
68                     devices[arg_id].path, devices[arg_id].iface,
69                     "start", NULL, NULL);
70         if (!msg)
71                 return -EBADMSG;
72
73         dbus_message_unref(msg);
74
75         return 0;
76 }
77
78 static int stop_device(char **args)
79 {
80         DBusMessage *msg;
81
82         if (!args[1])
83                 return -EINVAL;
84
85         printf("start %s device!\n", args[1]);
86
87         msg = dbus_method_sync_with_reply(DEVICED_BUS_NAME,
88                     devices[arg_id].path, devices[arg_id].iface,
89                     "stop", NULL, NULL);
90         if (!msg)
91                 return -EBADMSG;
92
93         dbus_message_unref(msg);
94
95         return 0;
96 }
97
98 static int dump_mode(char **args)
99 {
100         DBusError err;
101         DBusMessage *msg;
102         int ret, val;
103         char *arr[1];
104
105         if (!args[1] || !args[2] || !args[3])
106                 return -EINVAL;
107
108         printf("%s (%s %s)!\n", args[1], args[2], args[3]);
109
110         arr[0] = 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);
114         if (!msg)
115                 return -EBADMSG;
116
117         dbus_error_init(&err);
118
119         ret = dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &val, DBUS_TYPE_INVALID);
120         if (!ret) {
121                 printf("no message : [%s:%s]", err.name, err.message);
122                 dbus_error_free(&err);
123                 val = -ENOMSG;
124         }
125
126         dbus_message_unref(msg);
127         return val;
128 }
129
130 static int save_log(char **args)
131 {
132         DBusMessage *msg;
133
134         if (!args[1])
135                 return -EINVAL;
136
137         printf("save log %s device!\n", args[1]);
138
139         msg = dbus_method_sync_with_reply(DEVICED_BUS_NAME,
140                     devices[arg_id].path, devices[arg_id].iface,
141                     "SaveLog", NULL, NULL);
142         if (!msg)
143                 return -EBADMSG;
144
145         dbus_message_unref(msg);
146
147         return 0;
148 }
149
150 static int set_usb_mode(char **args)
151 {
152         return load_usb_mode(args[3]);
153 }
154
155 static int unset_usb_mode(char **args)
156 {
157         return unload_usb_mode(args[3]);
158 }
159
160 static const struct action {
161         const enum device_type id;
162         const char *action;
163         const int argc;
164         int (* const func)(char **args);
165         const char *option;
166 } actions[] = {
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]"   },
174 };
175
176 static inline void usage()
177 {
178         printf("[usage] devicectl <device_name> <action>\n");
179         printf("Please use option --help to check options\n");
180 }
181
182 static void help()
183 {
184         int i;
185
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,
191                             actions[i].option);
192                 } else {
193                         printf("    %s %s %s\n", devices[actions[i].id].name,
194                             actions[i].action, actions[i].option);
195                 }
196         }
197 }
198
199 int main(int argc, char *argv[])
200 {
201         int i;
202
203         if (argc == 2 && !strcmp(argv[1], "--help")) {
204                 help();
205                 return 0;
206         }
207
208         if (argc < 3) {
209                 usage();
210                 return -EINVAL;
211         }
212
213         for (i = 0; i < argc; i++)
214                 if (argv[i] == NULL) {
215                         usage();
216                         return -EINVAL;
217                 }
218
219         for (i = 0; i < ARRAY_SIZE(devices); i++)
220                 if (!strcmp(argv[1], devices[i].name))
221                         break;
222
223         if (i >= ARRAY_SIZE(devices)) {
224                 printf("invalid device name! %s\n", argv[1]);
225                 usage();
226                 return -EINVAL;
227         }
228
229         arg_id = devices[i].id;
230
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))
234                                 break;
235
236         if (i >= ARRAY_SIZE(actions)) {
237                 printf("invalid action name! %s\n", argv[2]);
238                 usage();
239                 return -EINVAL;
240         }
241
242         if (actions[i].argc != argc) {
243                 printf("invalid arg count!\n");
244                 usage();
245                 return -EINVAL;
246         }
247
248         return actions[i].func(argv);
249 }
250