tizen 2.3 release
[framework/system/deviced.git] / src / libdeviced / led.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 <stdbool.h>
22 #include <errno.h>
23
24 #include "log.h"
25 #include "dbus.h"
26 #include "common.h"
27
28 #define METHOD_GET_BRIGHTNESS           "GetBrightness"
29 #define METHOD_GET_MAX_BRIGHTNESS       "GetMaxBrightness"
30 #define METHOD_SET_BRIGHTNESS           "SetBrightness"
31 #define METHOD_SET_IR_COMMAND           "SetIrCommand"
32 #define METHOD_SET_MODE                         "SetMode"
33
34 API int led_get_brightness(void)
35 {
36         DBusError err;
37         DBusMessage *msg;
38         int ret, ret_val;
39
40         msg = dbus_method_sync_with_reply(DEVICED_BUS_NAME,
41                         DEVICED_PATH_LED, DEVICED_INTERFACE_LED,
42                         METHOD_GET_BRIGHTNESS, NULL, NULL);
43         if (!msg)
44                 return -EBADMSG;
45
46         dbus_error_init(&err);
47
48         ret = dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &ret_val, DBUS_TYPE_INVALID);
49         if (!ret) {
50                 _E("no message : [%s:%s]", err.name, err.message);
51                 dbus_error_free(&err);
52                 ret_val = -EBADMSG;
53         }
54
55         dbus_message_unref(msg);
56         return ret_val;
57 }
58
59 API int led_get_max_brightness(void)
60 {
61         DBusError err;
62         DBusMessage *msg;
63         int ret, ret_val;
64
65         msg = dbus_method_sync_with_reply(DEVICED_BUS_NAME,
66                         DEVICED_PATH_LED, DEVICED_INTERFACE_LED,
67                         METHOD_GET_MAX_BRIGHTNESS, NULL, NULL);
68         if (!msg)
69                 return -EBADMSG;
70
71         dbus_error_init(&err);
72
73         ret = dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &ret_val, DBUS_TYPE_INVALID);
74         if (!ret) {
75                 _E("no message : [%s:%s]", err.name, err.message);
76                 dbus_error_free(&err);
77                 ret_val = -EBADMSG;
78         }
79
80         dbus_message_unref(msg);
81         return ret_val;
82 }
83
84 API int led_set_brightness_with_noti(int val, bool enable)
85 {
86         DBusError err;
87         DBusMessage *msg;
88         char *arr[2];
89         char buf_val[32];
90         char buf_noti[32];
91         int ret, ret_val;
92
93         snprintf(buf_val, sizeof(buf_val), "%d", val);
94         arr[0] = buf_val;
95         snprintf(buf_noti, sizeof(buf_noti), "%d", enable);
96         arr[1] = buf_noti;
97
98         msg = dbus_method_sync_with_reply(DEVICED_BUS_NAME,
99                         DEVICED_PATH_LED, DEVICED_INTERFACE_LED,
100                         METHOD_SET_BRIGHTNESS, "ii", arr);
101         if (!msg)
102                 return -EBADMSG;
103
104         dbus_error_init(&err);
105
106         ret = dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &ret_val, DBUS_TYPE_INVALID);
107         if (!ret) {
108                 _E("no message : [%s:%s]", err.name, err.message);
109                 dbus_error_free(&err);
110                 ret_val = -EBADMSG;
111         }
112
113         dbus_message_unref(msg);
114         return ret_val;
115 }
116
117 API int led_set_ir_command(char *value)
118 {
119         if (value == NULL) {
120                 return -EINVAL;
121         }
122
123         DBusError err;
124         DBusMessage *msg;
125         char *arr[1];
126         int ret, ret_val;
127
128         arr[0] = value;
129
130         msg = dbus_method_sync_with_reply(DEVICED_BUS_NAME,
131                         DEVICED_PATH_LED, DEVICED_INTERFACE_LED,
132                         METHOD_SET_IR_COMMAND, "s", arr);
133         if (!msg)
134                 return -EBADMSG;
135
136         dbus_error_init(&err);
137
138         ret = dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &ret_val, DBUS_TYPE_INVALID);
139         if (!ret) {
140                 _E("no message : [%s:%s]", err.name, err.message);
141                 dbus_error_free(&err);
142                 ret_val = -EBADMSG;
143         }
144
145         dbus_message_unref(msg);
146         return ret_val;
147 }
148
149 API int led_set_mode_with_property(int mode, bool val, int on, int off, unsigned int color)
150 {
151         DBusError err;
152         DBusMessage *msg;
153         char *arr[5];
154         char buf_mode[32], buf_val[32];
155         char buf_on[32], buf_off[32];
156         char buf_color[32];
157         int ret, ret_val;
158
159         snprintf(buf_mode, sizeof(buf_mode), "%d", mode);
160         arr[0] = buf_mode;
161         snprintf(buf_val, sizeof(buf_val), "%d", val);
162         arr[1] = buf_val;
163         snprintf(buf_on, sizeof(buf_on), "%d", on);
164         arr[2] = buf_on;
165         snprintf(buf_off, sizeof(buf_off), "%d", off);
166         arr[3] = buf_off;
167         snprintf(buf_color, sizeof(buf_color), "%lu", color);
168         arr[4] = buf_color;
169
170         return dbus_method_sync(DEVICED_BUS_NAME,
171                         DEVICED_PATH_LED, DEVICED_INTERFACE_LED,
172                         METHOD_SET_MODE, "iiiiu", arr);
173 }
174
175 API int led_set_mode_with_color(int mode, bool on, unsigned int color)
176 {
177         return led_set_mode_with_property(mode, on, -1, -1, color);
178 }