Initialize Tizen 2.3
[framework/system/deviced.git] / src / control / control.c
1 /*
2  * deviced
3  *
4  * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd.
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 <errno.h>
22 #include <assert.h>
23 #include <sys/types.h>
24 #include <dd-control.h>
25
26 #include "core/log.h"
27 #include "core/common.h"
28 #include "core/devices.h"
29 #include "core/edbus-handler.h"
30
31 #define CONTROL_HANDLER_NAME            "control"
32 #define CONTROL_GETTER_NAME                     "getcontrol"
33
34 static const struct control_device {
35         const int id;
36         const char *name;
37 } devices[] = {
38         /* Add id & ops to provide start/stop control */
39         { DEVICE_CONTROL_MMC,               "mmc" },
40         { DEVICE_CONTROL_USBCLIENT,   "usbclient" },
41         { DEVICE_CONTROL_RGBLED,         "rgbled" },
42 };
43
44 static int control_handler(int argc, char **argv)
45 {
46         int i;
47         int pid;
48         int device;
49         bool enable;
50         int ret;
51         const struct device_ops *dev_ops;
52
53         _I("argc : %d", argc);
54         for (i = 0; i < argc; ++i)
55                 _I("[%2d] %s", i, argv[i]);
56
57         if (argc > 5) {
58                 _E("Invalid argument");
59                 errno = EINVAL;
60                 return -1;
61         }
62
63         pid = atoi(argv[0]);
64         device = atoi(argv[1]);
65         enable = atoi(argv[2]);
66         _I("pid : %d, device : %d, enable :%d", pid, device, enable);
67
68         for (i = 0; i < ARRAY_SIZE(devices); i++)
69                 if (devices[i].id == device)
70                         break;
71
72         if (i >= ARRAY_SIZE(devices))
73                 return -EINVAL;
74
75         dev_ops = find_device(devices[i].name);
76         if (!dev_ops)
77                 return -ENODEV;
78
79         if (enable)
80                 ret = device_start(dev_ops);
81         else
82                 ret = device_stop(dev_ops);
83
84         return ret;
85 }
86
87 static int get_control_handler(int argc, char **argv)
88 {
89         int i;
90         int pid;
91         int device;
92         bool enable;
93         int ret;
94         const struct device_ops *dev_ops;
95
96         _I("argc : %d", argc);
97         for (i = 0; i < argc; ++i)
98                 _I("[%2d] %s", i, argv[i]);
99
100         if (argc > 4) {
101                 _E("Invalid argument");
102                 errno = EINVAL;
103                 return -1;
104         }
105
106         pid = atoi(argv[0]);
107         device = atoi(argv[1]);
108         _I("pid : %d, device : %d", pid, device);
109
110         for (i = 0; i < ARRAY_SIZE(devices); i++)
111                 if (devices[i].id == device)
112                         break;
113
114         if (i >= ARRAY_SIZE(devices))
115                 return -EINVAL;
116
117         dev_ops = find_device(devices[i].name);
118         if (!dev_ops)
119                 return -ENODEV;
120
121         return device_get_status(dev_ops);
122 }
123
124
125 static DBusMessage *dbus_control_handler(E_DBus_Object *obj, DBusMessage *msg)
126 {
127         DBusError err;
128         DBusMessageIter iter;
129         DBusMessage *reply;
130         pid_t pid;
131         int ret;
132         int argc;
133         char *type_str;
134         char *argv[3];
135
136         dbus_error_init(&err);
137
138         if (!dbus_message_get_args(msg, &err,
139                     DBUS_TYPE_STRING, &type_str,
140                     DBUS_TYPE_INT32, &argc,
141                     DBUS_TYPE_STRING, &argv[0],
142                     DBUS_TYPE_STRING, &argv[1],
143                     DBUS_TYPE_STRING, &argv[2], DBUS_TYPE_INVALID)) {
144                 _E("there is no message");
145                 ret = -EINVAL;
146                 goto out;
147         }
148
149         if (argc < 0) {
150                 _E("message is invalid!");
151                 ret = -EINVAL;
152                 goto out;
153         }
154
155         pid = get_edbus_sender_pid(msg);
156         if (kill(pid, 0) == -1) {
157                 _E("%d process does not exist, dbus ignored!", pid);
158                 ret = -ESRCH;
159                 goto out;
160         }
161
162         ret = control_handler(argc, (char **)&argv);
163 out:
164         reply = dbus_message_new_method_return(msg);
165         dbus_message_iter_init_append(reply, &iter);
166         dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &ret);
167
168         return reply;
169 }
170
171 static DBusMessage *dbus_get_control_handler(E_DBus_Object *obj, DBusMessage *msg)
172 {
173         DBusError err;
174         DBusMessageIter iter;
175         DBusMessage *reply;
176         pid_t pid;
177         int ret;
178         int argc;
179         char *type_str;
180         char *argv[2];
181
182         dbus_error_init(&err);
183
184         if (!dbus_message_get_args(msg, &err,
185                     DBUS_TYPE_STRING, &type_str,
186                     DBUS_TYPE_INT32, &argc,
187                     DBUS_TYPE_STRING, &argv[0],
188                     DBUS_TYPE_STRING, &argv[1], DBUS_TYPE_INVALID)) {
189                 _E("there is no message");
190                 ret = -EINVAL;
191                 goto out;
192         }
193
194         if (argc < 0) {
195                 _E("message is invalid!");
196                 ret = -EINVAL;
197                 goto out;
198         }
199
200         pid = get_edbus_sender_pid(msg);
201         if (kill(pid, 0) == -1) {
202                 _E("%d process does not exist, dbus ignored!", pid);
203                 ret = -ESRCH;
204                 goto out;
205         }
206
207         ret = get_control_handler(argc, (char **)&argv);
208 out:
209         reply = dbus_message_new_method_return(msg);
210         dbus_message_iter_init_append(reply, &iter);
211         dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &ret);
212
213         return reply;
214 }
215
216 static const struct edbus_method edbus_methods[] = {
217         { CONTROL_HANDLER_NAME, "sisss", "i", dbus_control_handler     },
218         { CONTROL_GETTER_NAME ,  "siss", "i", dbus_get_control_handler },
219 };
220
221 static void control_init(void *data)
222 {
223         int ret;
224
225         ret = register_edbus_method(DEVICED_PATH_SYSNOTI, edbus_methods, ARRAY_SIZE(edbus_methods));
226         if (ret < 0)
227                 _E("fail to init edbus method(%d)", ret);
228
229         register_action(CONTROL_HANDLER_NAME, control_handler, NULL, NULL);
230 }
231
232 static const struct device_ops control_device_ops = {
233         .priority = DEVICE_PRIORITY_NORMAL,
234         .name     = "control",
235         .init     = control_init,
236 };
237
238 DEVICE_OPS_REGISTER(&control_device_ops)