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 = NULL;
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))
74 FIND_DEVICE_INT(dev_ops, devices[i].name);
76 ret = device_start(dev_ops);
78 ret = device_stop(dev_ops);
83 static int get_control_handler(int argc, char **argv)
90 const struct device_ops *dev_ops = NULL;
92 _I("argc : %d", argc);
93 for (i = 0; i < argc; ++i)
94 _I("[%2d] %s", i, argv[i]);
97 _E("Invalid argument");
103 device = atoi(argv[1]);
104 _I("pid : %d, device : %d", pid, device);
106 for (i = 0; i < ARRAY_SIZE(devices); i++)
107 if (devices[i].id == device)
110 if (i >= ARRAY_SIZE(devices))
113 FIND_DEVICE_INT(dev_ops, devices[i].name);
115 return device_get_status(dev_ops);
119 static DBusMessage *dbus_control_handler(E_DBus_Object *obj, DBusMessage *msg)
122 DBusMessageIter iter;
130 dbus_error_init(&err);
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");
144 _E("message is invalid!");
149 pid = get_edbus_sender_pid(msg);
150 if (kill(pid, 0) == -1) {
151 _E("%d process does not exist, dbus ignored!", pid);
156 ret = control_handler(argc, (char **)&argv);
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);
165 static DBusMessage *dbus_get_control_handler(E_DBus_Object *obj, DBusMessage *msg)
168 DBusMessageIter iter;
176 dbus_error_init(&err);
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");
189 _E("message is invalid!");
194 pid = get_edbus_sender_pid(msg);
195 if (kill(pid, 0) == -1) {
196 _E("%d process does not exist, dbus ignored!", pid);
201 ret = get_control_handler(argc, (char **)&argv);
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);
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 },
215 static void control_init(void *data)
219 ret = register_edbus_method(DEVICED_PATH_SYSNOTI, edbus_methods, ARRAY_SIZE(edbus_methods));
221 _E("fail to init edbus method(%d)", ret);
224 static const struct device_ops control_device_ops = {
225 .priority = DEVICE_PRIORITY_NORMAL,
227 .init = control_init,
230 DEVICE_OPS_REGISTER(&control_device_ops)