Remove internal header files
[platform/hal/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 #include <libsyscommon/dbus-system.h>
23
24 #include "hal-led.h"
25 #include "common.h"
26
27 #define METHOD_GET_MAX_BRIGHTNESS       "GetMaxBrightness"
28 #define METHOD_GET_BRIGHTNESS           "GetBrightness"
29 #define METHOD_SET_BRIGHTNESS           "SetBrightness"
30 #define METHOD_PLAY_CUSTOM              "playcustom"
31 #define METHOD_GET_LED_NUMBER           "GetNumOfLeds"
32 #define METHOD_MULTI_LED_CONTROL        "multi_led_control"
33 #define METHOD_STOP_CUSTOM              "stopcustom"
34
35 #define FRONT_LED_FEATURE               "tizen.org/feature/led"
36 #define CAMERA_LED_FEATURE              "tizen.org/feature/camera.back.flash"
37
38 static bool support_front_led;
39 static bool support_camera_led;
40 static int number_of_devices = -1;
41
42 static void __CONSTRUCTOR__ init(void)
43 {
44         int ret;
45         bool val;
46
47         ret = system_info_get_platform_bool(FRONT_LED_FEATURE, &val);
48         if (ret == SYSTEM_INFO_ERROR_NONE && val)
49                 support_front_led = true;
50
51         ret = system_info_get_platform_bool(CAMERA_LED_FEATURE, &val);
52         if (ret == SYSTEM_INFO_ERROR_NONE && val)
53                 support_camera_led = true;
54 }
55
56 int device_flash_get_max_brightness(int *max_brightness)
57 {
58         int ret;
59
60         if (!support_camera_led)
61                 return DEVICE_ERROR_NOT_SUPPORTED;
62
63         if (!max_brightness)
64                 return DEVICE_ERROR_INVALID_PARAMETER;
65
66         ret = dbus_handle_method_sync_var(DEVICED_BUS_NAME,
67                         DEVICED_PATH_LED, DEVICED_INTERFACE_LED,
68                         METHOD_GET_MAX_BRIGHTNESS, NULL);
69         if (ret < 0)
70                 return errno_to_device_error(ret); //LCOV_EXCL_LINE System Error
71
72         *max_brightness = ret;
73         return DEVICE_ERROR_NONE;
74 }
75
76 int device_flash_get_brightness(int *brightness)
77 {
78         int ret;
79
80         if (!support_camera_led)
81                 return DEVICE_ERROR_NOT_SUPPORTED;
82
83         if (!brightness)
84                 return DEVICE_ERROR_INVALID_PARAMETER;
85
86         ret = dbus_handle_method_sync_var(DEVICED_BUS_NAME,
87                         DEVICED_PATH_LED, DEVICED_INTERFACE_LED,
88                         METHOD_GET_BRIGHTNESS, NULL);
89         if (ret < 0)
90                 return errno_to_device_error(ret); //LCOV_EXCL_LINE System Error
91
92         *brightness = ret;
93         return DEVICE_ERROR_NONE;
94 }
95
96 int device_flash_set_brightness(int brightness)
97 {
98         int max, ret;
99
100         if (!support_camera_led)
101                 return DEVICE_ERROR_NOT_SUPPORTED;
102
103         ret = device_flash_get_max_brightness(&max);
104         if (ret < 0)
105                 return ret;
106
107         if (brightness < 0 || brightness > max)
108                 return DEVICE_ERROR_INVALID_PARAMETER;
109
110         /* if camera API preempt a flash device, it will return -EBUSY error. */
111         ret = dbus_handle_method_sync_var(DEVICED_BUS_NAME,
112                         DEVICED_PATH_LED, DEVICED_INTERFACE_LED,
113                         METHOD_SET_BRIGHTNESS, g_variant_new("(ii)", brightness, 0));
114         if (ret < 0)
115                 return errno_to_device_error(ret); //LCOV_EXCL_LINE System Error
116
117         return DEVICE_ERROR_NONE;
118 }
119
120 int device_led_play_custom(int on, int off, unsigned int color, unsigned int flags)
121 {
122         int ret;
123
124         if (!support_front_led)
125                 return DEVICE_ERROR_NOT_SUPPORTED;
126
127         if (on < 0 || off < 0)
128                 return DEVICE_ERROR_INVALID_PARAMETER;
129
130         ret = dbus_handle_method_sync_var(DEVICED_BUS_NAME,
131                         DEVICED_PATH_LED, DEVICED_INTERFACE_LED,
132                         METHOD_PLAY_CUSTOM, g_variant_new("(iiuu)", on, off, (unsigned int)color, (unsigned int)flags));
133 //LCOV_EXCL_START System Error
134         if (ret < 0)
135                 return errno_to_device_error(ret);
136 //LCOV_EXCL_STOP
137
138         return DEVICE_ERROR_NONE;
139 }
140
141 int device_led_stop_custom(void)
142 {
143         int ret;
144
145         if (!support_front_led)
146                 return DEVICE_ERROR_NOT_SUPPORTED;
147
148         ret = dbus_handle_method_sync_var(DEVICED_BUS_NAME,
149                         DEVICED_PATH_LED, DEVICED_INTERFACE_LED,
150                         METHOD_STOP_CUSTOM, NULL);
151 //LCOV_EXCL_START System Error
152         if (ret < 0)
153                 return errno_to_device_error(ret);
154 //LCOV_EXCL_STOP
155
156         return DEVICE_ERROR_NONE;
157 }
158
159 int device_multi_led_get_number(int *num_of_leds)
160 {
161         int ret;
162
163         if (!support_front_led)
164                 return DEVICE_ERROR_NOT_SUPPORTED;
165
166         if (!num_of_leds)
167                 return DEVICE_ERROR_INVALID_PARAMETER;
168
169         if (number_of_devices < 0) {
170                 ret = dbus_handle_method_sync_var(DEVICED_BUS_NAME,
171                                 DEVICED_PATH_LED, DEVICED_INTERFACE_LED,
172                                 METHOD_GET_LED_NUMBER, NULL);
173                 if (ret < 0)
174                         return errno_to_device_error(ret); //LCOV_EXCL_LINE System Error
175                 number_of_devices = ret;
176         }
177
178         *num_of_leds = number_of_devices;
179
180         return DEVICE_ERROR_NONE;
181 }
182
183 int device_multi_led_control(unsigned int color[])
184 {
185         GVariantBuilder *builder = NULL;
186         GVariant *var = NULL;
187         int i, ret;
188
189         if (!support_front_led)
190                 return DEVICE_ERROR_NOT_SUPPORTED;
191
192         if (color == NULL)
193                 return DEVICE_ERROR_INVALID_PARAMETER;
194
195         if (number_of_devices < 0) {
196                 ret = device_multi_led_get_number(&number_of_devices);
197                 if (ret != DEVICE_ERROR_NONE) //LCOV_EXCL_LINE System Error
198                         return ret;
199         }
200
201         builder = g_variant_builder_new(G_VARIANT_TYPE("au"));
202         for (i = 0; i < number_of_devices; i++)
203                 g_variant_builder_add(builder, "u", color[i]);
204
205         var = g_variant_new("(au)", builder);
206         g_variant_builder_unref(builder);
207
208         ret = dbus_handle_method_sync_var(DEVICED_BUS_NAME, DEVICED_PATH_LED,
209                                 DEVICED_INTERFACE_LED, METHOD_MULTI_LED_CONTROL, var);
210         if (ret < 0)
211                 return errno_to_device_error(ret); //LCOV_EXCL_LINE System Error
212
213         return DEVICE_ERROR_NONE;
214 }