4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 #include <sys/types.h>
24 #include <dd-control.h>
27 #include "core/common.h"
28 #include "core/devices.h"
29 #include "core/edbus-handler.h"
31 #define CONTROL_HANDLER_NAME "control"
32 #define CONTROL_GETTER_NAME "getcontrol"
34 static const struct control_device {
38 /* Add id & ops to provide start/stop control */
39 { DEVICE_CONTROL_MMC, "mmc" },
40 { DEVICE_CONTROL_USBCLIENT, "usbclient" },
41 { DEVICE_CONTROL_RGBLED, "rgbled" },
44 static int control_handler(int argc, char **argv)
51 const struct device_ops *dev_ops;
53 _I("argc : %d", argc);
54 for (i = 0; i < argc; ++i)
55 _I("[%2d] %s", i, argv[i]);
58 _E("Invalid argument");
64 device = atoi(argv[1]);
65 enable = atoi(argv[2]);
66 _I("pid : %d, device : %d, enable :%d", pid, device, enable);
68 for (i = 0; i < ARRAY_SIZE(devices); i++)
69 if (devices[i].id == device)
72 if (i >= ARRAY_SIZE(devices))
75 dev_ops = find_device(devices[i].name);
80 ret = device_start(dev_ops);
82 ret = device_stop(dev_ops);
87 static int get_control_handler(int argc, char **argv)
94 const struct device_ops *dev_ops;
96 _I("argc : %d", argc);
97 for (i = 0; i < argc; ++i)
98 _I("[%2d] %s", i, argv[i]);
101 _E("Invalid argument");
107 device = atoi(argv[1]);
108 _I("pid : %d, device : %d", pid, device);
110 for (i = 0; i < ARRAY_SIZE(devices); i++)
111 if (devices[i].id == device)
114 if (i >= ARRAY_SIZE(devices))
117 dev_ops = find_device(devices[i].name);
121 return device_get_status(dev_ops);
125 static DBusMessage *dbus_control_handler(E_DBus_Object *obj, DBusMessage *msg)
128 DBusMessageIter iter;
136 dbus_error_init(&err);
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");
150 _E("message is invalid!");
155 pid = get_edbus_sender_pid(msg);
156 if (kill(pid, 0) == -1) {
157 _E("%d process does not exist, dbus ignored!", pid);
162 ret = control_handler(argc, (char **)&argv);
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);
171 static DBusMessage *dbus_get_control_handler(E_DBus_Object *obj, DBusMessage *msg)
174 DBusMessageIter iter;
182 dbus_error_init(&err);
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");
195 _E("message is invalid!");
200 pid = get_edbus_sender_pid(msg);
201 if (kill(pid, 0) == -1) {
202 _E("%d process does not exist, dbus ignored!", pid);
207 ret = get_control_handler(argc, (char **)&argv);
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);
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 },
221 static void control_init(void *data)
225 ret = register_edbus_method(DEVICED_PATH_SYSNOTI, edbus_methods, ARRAY_SIZE(edbus_methods));
227 _E("fail to init edbus method(%d)", ret);
229 register_action(CONTROL_HANDLER_NAME, control_handler, NULL, NULL);
232 static const struct device_ops control_device_ops = {
233 .priority = DEVICE_PRIORITY_NORMAL,
235 .init = control_init,
238 DEVICE_OPS_REGISTER(&control_device_ops)