Initialize Tizen 2.3
[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         DBusError err;
120         DBusMessage *msg;
121         char *arr[1];
122         int ret, ret_val;
123
124         arr[0] = value;
125
126         msg = dbus_method_sync_with_reply(DEVICED_BUS_NAME,
127                         DEVICED_PATH_LED, DEVICED_INTERFACE_LED,
128                         METHOD_SET_IR_COMMAND, "s", arr);
129         if (!msg)
130                 return -EBADMSG;
131
132         dbus_error_init(&err);
133
134         ret = dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &ret_val, DBUS_TYPE_INVALID);
135         if (!ret) {
136                 _E("no message : [%s:%s]", err.name, err.message);
137                 dbus_error_free(&err);
138                 ret_val = -EBADMSG;
139         }
140
141         dbus_message_unref(msg);
142         return ret_val;
143 }
144
145 API int led_set_mode_with_property(int mode, bool val, int on, int off, unsigned int color)
146 {
147         DBusError err;
148         DBusMessage *msg;
149         char *arr[5];
150         char buf_mode[32], buf_val[32];
151         char buf_on[32], buf_off[32];
152         char buf_color[32];
153         int ret, ret_val;
154
155         snprintf(buf_mode, sizeof(buf_mode), "%d", mode);
156         arr[0] = buf_mode;
157         snprintf(buf_val, sizeof(buf_val), "%d", val);
158         arr[1] = buf_val;
159         snprintf(buf_on, sizeof(buf_on), "%d", on);
160         arr[2] = buf_on;
161         snprintf(buf_off, sizeof(buf_off), "%d", off);
162         arr[3] = buf_off;
163         snprintf(buf_color, sizeof(buf_color), "%lu", color);
164         arr[4] = buf_color;
165
166         return dbus_method_sync(DEVICED_BUS_NAME,
167                         DEVICED_PATH_LED, DEVICED_INTERFACE_LED,
168                         METHOD_SET_MODE, "iiiiu", arr);
169 }
170
171 API int led_set_mode_with_color(int mode, bool on, unsigned int color)
172 {
173         return led_set_mode_with_property(mode, on, -1, -1, color);
174 }