Remove executable flag from non-executable files
[platform/core/api/device.git] / src / led.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17
18 #include <stdio.h>
19 #include <stdbool.h>
20 #include <errno.h>
21 #include <system_info.h>
22
23 #include "led.h"
24 #include "led-internal.h"
25 #include "common.h"
26 #include "dbus.h"
27
28 #define METHOD_GET_MAX_BRIGHTNESS       "GetMaxBrightness"
29 #define METHOD_GET_BRIGHTNESS           "GetBrightness"
30 #define METHOD_SET_BRIGHTNESS           "SetBrightness"
31 #define METHOD_PLAY_CUSTOM              "playcustom"
32 #define METHOD_GET_LED_NUMBER           "GetNumOfLeds"
33 #define METHOD_MULTI_LED_CONTROL        "multi_led_control"
34 #define METHOD_STOP_CUSTOM              "stopcustom"
35
36 #define FRONT_LED_FEATURE               "tizen.org/feature/led"
37 #define CAMERA_LED_FEATURE              "tizen.org/feature/camera.back.flash"
38
39 static bool support_front_led;
40 static bool support_camera_led;
41 static int number_of_devices = -1;
42
43 static void __CONSTRUCTOR__ init(void)
44 {
45         int ret;
46         bool val;
47
48         ret = system_info_get_platform_bool(FRONT_LED_FEATURE, &val);
49         if (ret == SYSTEM_INFO_ERROR_NONE && val)
50                 support_front_led = true;
51
52         ret = system_info_get_platform_bool(CAMERA_LED_FEATURE, &val);
53         if (ret == SYSTEM_INFO_ERROR_NONE && val)
54                 support_camera_led = true;
55 }
56
57 int device_flash_get_max_brightness(int *max_brightness)
58 {
59         int ret;
60
61         if (!support_camera_led)
62                 return DEVICE_ERROR_NOT_SUPPORTED;
63
64         if (!max_brightness)
65                 return DEVICE_ERROR_INVALID_PARAMETER;
66
67         ret = dbus_method_sync(DEVICED_BUS_NAME,
68                         DEVICED_PATH_LED, DEVICED_INTERFACE_LED,
69                         METHOD_GET_MAX_BRIGHTNESS, NULL, NULL);
70         if (ret < 0)
71                 return errno_to_device_error(ret); //LCOV_EXCL_LINE System Error
72
73         *max_brightness = ret;
74         return DEVICE_ERROR_NONE;
75 }
76
77 int device_flash_get_brightness(int *brightness)
78 {
79         int ret;
80
81         if (!support_camera_led)
82                 return DEVICE_ERROR_NOT_SUPPORTED;
83
84         if (!brightness)
85                 return DEVICE_ERROR_INVALID_PARAMETER;
86
87         ret = dbus_method_sync(DEVICED_BUS_NAME,
88                         DEVICED_PATH_LED, DEVICED_INTERFACE_LED,
89                         METHOD_GET_BRIGHTNESS, NULL, NULL);
90         if (ret < 0)
91                 return errno_to_device_error(ret); //LCOV_EXCL_LINE System Error
92
93         *brightness = ret;
94         return DEVICE_ERROR_NONE;
95 }
96
97 int device_flash_set_brightness(int brightness)
98 {
99         char *arr[2];
100         char buf_val[32];
101         char buf_noti[32];
102         int max, ret;
103
104         if (!support_camera_led)
105                 return DEVICE_ERROR_NOT_SUPPORTED;
106
107         ret = device_flash_get_max_brightness(&max);
108         if (ret < 0)
109                 return ret;
110
111         if (brightness < 0 || brightness > max)
112                 return DEVICE_ERROR_INVALID_PARAMETER;
113
114         snprintf(buf_val, sizeof(buf_val), "%d", brightness);
115         arr[0] = buf_val;
116         snprintf(buf_noti, sizeof(buf_noti), "%d", 0);
117         arr[1] = buf_noti;
118
119         /* if camera API preempt a flash device, it will return -EBUSY error. */
120         ret = dbus_method_sync(DEVICED_BUS_NAME,
121                         DEVICED_PATH_LED, DEVICED_INTERFACE_LED,
122                         METHOD_SET_BRIGHTNESS, "ii", arr);
123         if (ret < 0)
124                 return errno_to_device_error(ret); //LCOV_EXCL_LINE System Error
125
126         return DEVICE_ERROR_NONE;
127 }
128
129 int device_led_play_custom(int on, int off, unsigned int color, unsigned int flags)
130 {
131         char *arr[4];
132         char str_on[32], str_off[32];
133         char str_color[32], str_flags[32];
134         int ret;
135
136         if (!support_front_led)
137                 return DEVICE_ERROR_NOT_SUPPORTED;
138
139         if (on < 0 || off < 0)
140                 return DEVICE_ERROR_INVALID_PARAMETER;
141
142         snprintf(str_on, sizeof(str_on), "%d", on);
143         arr[0] = str_on;
144         snprintf(str_off, sizeof(str_off), "%d", off);
145         arr[1] = str_off;
146         snprintf(str_color, sizeof(str_color), "%lu", (long unsigned int)color);
147         arr[2] = str_color;
148         snprintf(str_flags, sizeof(str_flags), "%lu", (long unsigned int)flags);
149         arr[3] = str_flags;
150
151         ret = dbus_method_sync(DEVICED_BUS_NAME,
152                         DEVICED_PATH_LED, DEVICED_INTERFACE_LED,
153                         METHOD_PLAY_CUSTOM, "iiuu", arr);
154 //LCOV_EXCL_START System Error
155         if (ret < 0)
156                 return errno_to_device_error(ret);
157 //LCOV_EXCL_STOP
158         return DEVICE_ERROR_NONE;
159 }
160
161 int device_led_stop_custom(void)
162 {
163         int ret;
164
165         if (!support_front_led)
166                 return DEVICE_ERROR_NOT_SUPPORTED;
167
168         ret = dbus_method_sync(DEVICED_BUS_NAME,
169                         DEVICED_PATH_LED, DEVICED_INTERFACE_LED,
170                         METHOD_STOP_CUSTOM, NULL, NULL);
171 //LCOV_EXCL_START System Error
172         if (ret < 0)
173                 return errno_to_device_error(ret);
174 //LCOV_EXCL_STOP
175
176         return DEVICE_ERROR_NONE;
177 }
178
179 //LCOV_EXCL_START Not used function
180 int device_multi_led_get_number(int *num_of_leds)
181 {
182         int ret;
183
184         if (!support_front_led)
185                 return DEVICE_ERROR_NOT_SUPPORTED;
186
187         if (!num_of_leds)
188                 return DEVICE_ERROR_INVALID_PARAMETER;
189
190         if (number_of_devices < 0) {
191                 ret = dbus_method_sync(DEVICED_BUS_NAME,
192                                 DEVICED_PATH_LED, DEVICED_INTERFACE_LED,
193                                 METHOD_GET_LED_NUMBER, NULL, NULL);
194                 if (ret < 0)
195                         return errno_to_device_error(ret); //LCOV_EXCL_LINE System Error
196                 number_of_devices = ret;
197         }
198
199         *num_of_leds = number_of_devices;
200
201         return DEVICE_ERROR_NONE;
202 }
203
204 int device_multi_led_control(unsigned int color[])
205 {
206         GVariantBuilder *builder = NULL;
207         GVariant *var = NULL;
208         int i, ret;
209
210         if (!support_front_led)
211                 return DEVICE_ERROR_NOT_SUPPORTED;
212
213         if (color == NULL)
214                 return DEVICE_ERROR_INVALID_PARAMETER;
215
216         if (number_of_devices < 0) {
217                 ret = device_multi_led_get_number(&number_of_devices);
218                 if (ret != DEVICE_ERROR_NONE) //LCOV_EXCL_LINE System Error
219                         return ret;
220         }
221
222         builder = g_variant_builder_new(G_VARIANT_TYPE("au"));
223         for (i = 0; i < number_of_devices; i++)
224                 g_variant_builder_add(builder, "u", color[i]);
225
226         var = g_variant_new("(au)", builder);
227         g_variant_builder_unref(builder);
228
229         return dbus_method_sync_var(DEVICED_BUS_NAME, DEVICED_PATH_LED,
230                                 DEVICED_INTERFACE_LED, METHOD_MULTI_LED_CONTROL, var);
231 }
232 //LCOV_EXCL_STOP