tizen 2.3 release
[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 = NULL;
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         FIND_DEVICE_INT(dev_ops, devices[i].name);
75         if (enable)
76                 ret = device_start(dev_ops);
77         else
78                 ret = device_stop(dev_ops);
79
80         return ret;
81 }
82
83 static int get_control_handler(int argc, char **argv)
84 {
85         int i;
86         int pid;
87         int device;
88         bool enable;
89         int ret;
90         const struct device_ops *dev_ops = NULL;
91
92         _I("argc : %d", argc);
93         for (i = 0; i < argc; ++i)
94                 _I("[%2d] %s", i, argv[i]);
95
96         if (argc > 4) {
97                 _E("Invalid argument");
98                 errno = EINVAL;
99                 return -1;
100         }
101
102         pid = atoi(argv[0]);
103         device = atoi(argv[1]);
104         _I("pid : %d, device : %d", pid, device);
105
106         for (i = 0; i < ARRAY_SIZE(devices); i++)
107                 if (devices[i].id == device)
108                         break;
109
110         if (i >= ARRAY_SIZE(devices))
111                 return -EINVAL;
112
113         FIND_DEVICE_INT(dev_ops, devices[i].name);
114
115         return device_get_status(dev_ops);
116 }
117
118
119 static DBusMessage *dbus_control_handler(E_DBus_Object *obj, DBusMessage *msg)
120 {
121         DBusError err;
122         DBusMessageIter iter;
123         DBusMessage *reply;
124         pid_t pid;
125         int ret;
126         int argc;
127         char *type_str;
128         char *argv[3];
129
130         dbus_error_init(&err);
131
132         if (!dbus_message_get_args(msg, &err,
133                     DBUS_TYPE_STRING, &type_str,
134                     DBUS_TYPE_INT32, &argc,
135                     DBUS_TYPE_STRING, &argv[0],
136                     DBUS_TYPE_STRING, &argv[1],
137                     DBUS_TYPE_STRING, &argv[2], DBUS_TYPE_INVALID)) {
138                 _E("there is no message");
139                 ret = -EINVAL;
140                 goto out;
141         }
142
143         if (argc < 0) {
144                 _E("message is invalid!");
145                 ret = -EINVAL;
146                 goto out;
147         }
148
149         pid = get_edbus_sender_pid(msg);
150         if (kill(pid, 0) == -1) {
151                 _E("%d process does not exist, dbus ignored!", pid);
152                 ret = -ESRCH;
153                 goto out;
154         }
155
156         ret = control_handler(argc, (char **)&argv);
157 out:
158         reply = dbus_message_new_method_return(msg);
159         dbus_message_iter_init_append(reply, &iter);
160         dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &ret);
161
162         return reply;
163 }
164
165 static DBusMessage *dbus_get_control_handler(E_DBus_Object *obj, DBusMessage *msg)
166 {
167         DBusError err;
168         DBusMessageIter iter;
169         DBusMessage *reply;
170         pid_t pid;
171         int ret;
172         int argc;
173         char *type_str;
174         char *argv[2];
175
176         dbus_error_init(&err);
177
178         if (!dbus_message_get_args(msg, &err,
179                     DBUS_TYPE_STRING, &type_str,
180                     DBUS_TYPE_INT32, &argc,
181                     DBUS_TYPE_STRING, &argv[0],
182                     DBUS_TYPE_STRING, &argv[1], DBUS_TYPE_INVALID)) {
183                 _E("there is no message");
184                 ret = -EINVAL;
185                 goto out;
186         }
187
188         if (argc < 0) {
189                 _E("message is invalid!");
190                 ret = -EINVAL;
191                 goto out;
192         }
193
194         pid = get_edbus_sender_pid(msg);
195         if (kill(pid, 0) == -1) {
196                 _E("%d process does not exist, dbus ignored!", pid);
197                 ret = -ESRCH;
198                 goto out;
199         }
200
201         ret = get_control_handler(argc, (char **)&argv);
202 out:
203         reply = dbus_message_new_method_return(msg);
204         dbus_message_iter_init_append(reply, &iter);
205         dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &ret);
206
207         return reply;
208 }
209
210 static const struct edbus_method edbus_methods[] = {
211         { CONTROL_HANDLER_NAME, "sisss", "i", dbus_control_handler     },
212         { CONTROL_GETTER_NAME ,  "siss", "i", dbus_get_control_handler },
213 };
214
215 static void control_init(void *data)
216 {
217         int ret;
218
219         ret = register_edbus_method(DEVICED_PATH_SYSNOTI, edbus_methods, ARRAY_SIZE(edbus_methods));
220         if (ret < 0)
221                 _E("fail to init edbus method(%d)", ret);
222 }
223
224 static const struct device_ops control_device_ops = {
225         .priority = DEVICE_PRIORITY_NORMAL,
226         .name     = "control",
227         .init     = control_init,
228 };
229
230 DEVICE_OPS_REGISTER(&control_device_ops)