Modify multi-LED APIs
[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_PLAY_CUSTOM        "multi_playcustom"
34 #define METHOD_MULTI_STOP_CUSTOM        "multi_stopcustom"
35 #define METHOD_STOP_CUSTOM              "stopcustom"
36
37 #define FRONT_LED_FEATURE               "tizen.org/feature/led"
38 #define CAMERA_LED_FEATURE              "tizen.org/feature/camera.back.flash"
39
40 static bool support_front_led;
41 static bool support_camera_led;
42 static int number_of_devices;
43
44 static void __CONSTRUCTOR__ init(void)
45 {
46         int ret;
47         bool val;
48
49         ret = system_info_get_platform_bool(FRONT_LED_FEATURE, &val);
50         if (ret == SYSTEM_INFO_ERROR_NONE && val)
51                 support_front_led = true;
52
53         ret = system_info_get_platform_bool(CAMERA_LED_FEATURE, &val);
54         if (ret == SYSTEM_INFO_ERROR_NONE && val)
55                 support_camera_led = true;
56 }
57
58 int device_flash_get_max_brightness(int *max_brightness)
59 {
60         int ret;
61
62         if (!support_camera_led)
63                 return DEVICE_ERROR_NOT_SUPPORTED;
64
65         if (!max_brightness)
66                 return DEVICE_ERROR_INVALID_PARAMETER;
67
68         ret = dbus_method_sync(DEVICED_BUS_NAME,
69                         DEVICED_PATH_LED, DEVICED_INTERFACE_LED,
70                         METHOD_GET_MAX_BRIGHTNESS, NULL, NULL);
71         if (ret < 0)
72                 return errno_to_device_error(ret); //LCOV_EXCL_LINE System Error
73
74         *max_brightness = ret;
75         return DEVICE_ERROR_NONE;
76 }
77
78 int device_flash_get_brightness(int *brightness)
79 {
80         int ret;
81
82         if (!support_camera_led)
83                 return DEVICE_ERROR_NOT_SUPPORTED;
84
85         if (!brightness)
86                 return DEVICE_ERROR_INVALID_PARAMETER;
87
88         ret = dbus_method_sync(DEVICED_BUS_NAME,
89                         DEVICED_PATH_LED, DEVICED_INTERFACE_LED,
90                         METHOD_GET_BRIGHTNESS, NULL, NULL);
91         if (ret < 0)
92                 return errno_to_device_error(ret); //LCOV_EXCL_LINE System Error
93
94         *brightness = ret;
95         return DEVICE_ERROR_NONE;
96 }
97
98 int device_flash_set_brightness(int brightness)
99 {
100         char *arr[2];
101         char buf_val[32];
102         char buf_noti[32];
103         int max, ret;
104
105         if (!support_camera_led)
106                 return DEVICE_ERROR_NOT_SUPPORTED;
107
108         ret = device_flash_get_max_brightness(&max);
109         if (ret < 0)
110                 return ret;
111
112         if (brightness < 0 || brightness > max)
113                 return DEVICE_ERROR_INVALID_PARAMETER;
114
115         snprintf(buf_val, sizeof(buf_val), "%d", brightness);
116         arr[0] = buf_val;
117         snprintf(buf_noti, sizeof(buf_noti), "%d", 0);
118         arr[1] = buf_noti;
119
120         /* if camera API preempt a flash device, it will return -EBUSY error. */
121         ret = dbus_method_sync(DEVICED_BUS_NAME,
122                         DEVICED_PATH_LED, DEVICED_INTERFACE_LED,
123                         METHOD_SET_BRIGHTNESS, "ii", arr);
124         if (ret < 0)
125                 return errno_to_device_error(ret); //LCOV_EXCL_LINE System Error
126
127         return DEVICE_ERROR_NONE;
128 }
129
130 int device_led_play_custom(int on, int off, unsigned int color, unsigned int flags)
131 {
132         char *arr[4];
133         char str_on[32], str_off[32];
134         char str_color[32], str_flags[32];
135         int ret;
136
137         if (!support_front_led)
138                 return DEVICE_ERROR_NOT_SUPPORTED;
139
140         if (on < 0 || off < 0)
141                 return DEVICE_ERROR_INVALID_PARAMETER;
142
143         snprintf(str_on, sizeof(str_on), "%d", on);
144         arr[0] = str_on;
145         snprintf(str_off, sizeof(str_off), "%d", off);
146         arr[1] = str_off;
147         snprintf(str_color, sizeof(str_color), "%lu", (long unsigned int)color);
148         arr[2] = str_color;
149         snprintf(str_flags, sizeof(str_flags), "%lu", (long unsigned int)flags);
150         arr[3] = str_flags;
151
152         ret = dbus_method_sync(DEVICED_BUS_NAME,
153                         DEVICED_PATH_LED, DEVICED_INTERFACE_LED,
154                         METHOD_PLAY_CUSTOM, "iiuu", arr);
155         if (ret < 0)
156                 return errno_to_device_error(ret);
157
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 int device_multi_led_get_number(int *num_of_leds)
180 {
181         int ret;
182
183         if (!support_front_led)
184                 return DEVICE_ERROR_NOT_SUPPORTED;
185
186         if (!num_of_leds)
187                 return DEVICE_ERROR_INVALID_PARAMETER;
188
189         ret = dbus_method_sync(DEVICED_BUS_NAME,
190                         DEVICED_PATH_LED, DEVICED_INTERFACE_LED,
191                         METHOD_GET_LED_NUMBER, NULL, NULL);
192         if (ret < 0)
193                 return errno_to_device_error(ret); //LCOV_EXCL_LINE System Error
194
195         *num_of_leds = ret;
196         number_of_devices = ret;
197         return DEVICE_ERROR_NONE;
198 }
199
200 int device_multi_led_play_custom(unsigned int color[])
201 {
202         GVariantBuilder *builder = NULL;
203         GVariant *var = NULL;
204         int i;
205
206         if (!support_front_led)
207                 return DEVICE_ERROR_NOT_SUPPORTED;
208
209         if (color == NULL)
210                 return DEVICE_ERROR_INVALID_PARAMETER;
211
212         builder = g_variant_builder_new(G_VARIANT_TYPE("au"));
213         for (i = 0; i < number_of_devices; i++)
214                 g_variant_builder_add(builder, "u", color[i]);
215
216         var = g_variant_new("(au)", builder);
217         g_variant_builder_unref(builder);
218
219         return dbus_method_sync_var(DEVICED_BUS_NAME, DEVICED_PATH_LED,
220                                 DEVICED_INTERFACE_LED, METHOD_MULTI_PLAY_CUSTOM, var);
221 }