Add comments for line/function coverage analysis
[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 "common.h"
25 #include "dbus.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_STOP_CUSTOM      "stopcustom"
32
33 #define FRONT_LED_FEATURE               "tizen.org/feature/led"
34 #define CAMERA_LED_FEATURE              "tizen.org/feature/camera.back.flash"
35
36 static bool support_front_led;
37 static bool support_camera_led;
38
39 static void __CONSTRUCTOR__ init(void)
40 {
41         int ret;
42         bool val;
43
44         ret = system_info_get_platform_bool(FRONT_LED_FEATURE, &val);
45         if (ret == SYSTEM_INFO_ERROR_NONE && val)
46                 support_front_led = true;
47
48         ret = system_info_get_platform_bool(CAMERA_LED_FEATURE, &val);
49         if (ret == SYSTEM_INFO_ERROR_NONE && val)
50                 support_camera_led = true;
51 }
52
53 int device_flash_get_max_brightness(int *max_brightness)
54 {
55         int ret;
56
57         if (!support_camera_led)
58                 return DEVICE_ERROR_NOT_SUPPORTED;
59
60         if (!max_brightness)
61                 return DEVICE_ERROR_INVALID_PARAMETER;
62
63         ret = dbus_method_sync(DEVICED_BUS_NAME,
64                         DEVICED_PATH_LED, DEVICED_INTERFACE_LED,
65                         METHOD_GET_MAX_BRIGHTNESS, NULL, NULL);
66         if (ret < 0)
67                 return errno_to_device_error(ret); //LCOV_EXCL_LINE System Error
68
69         *max_brightness = ret;
70         return DEVICE_ERROR_NONE;
71 }
72
73 int device_flash_get_brightness(int *brightness)
74 {
75         int ret;
76
77         if (!support_camera_led)
78                 return DEVICE_ERROR_NOT_SUPPORTED;
79
80         if (!brightness)
81                 return DEVICE_ERROR_INVALID_PARAMETER;
82
83         ret = dbus_method_sync(DEVICED_BUS_NAME,
84                         DEVICED_PATH_LED, DEVICED_INTERFACE_LED,
85                         METHOD_GET_BRIGHTNESS, NULL, NULL);
86         if (ret < 0)
87                 return errno_to_device_error(ret); //LCOV_EXCL_LINE System Error
88
89         *brightness = ret;
90         return DEVICE_ERROR_NONE;
91 }
92
93 int device_flash_set_brightness(int brightness)
94 {
95         char *arr[2];
96         char buf_val[32];
97         char buf_noti[32];
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         snprintf(buf_val, sizeof(buf_val), "%d", brightness);
111         arr[0] = buf_val;
112         snprintf(buf_noti, sizeof(buf_noti), "%d", 0);
113         arr[1] = buf_noti;
114
115         /* if camera API preempt a flash device, it will return -EBUSY error. */
116         ret = dbus_method_sync(DEVICED_BUS_NAME,
117                         DEVICED_PATH_LED, DEVICED_INTERFACE_LED,
118                         METHOD_SET_BRIGHTNESS, "ii", arr);
119         if (ret < 0)
120                 return errno_to_device_error(ret); //LCOV_EXCL_LINE System Error
121
122         return DEVICE_ERROR_NONE;
123 }
124
125 int device_led_play_custom(int on, int off, unsigned int color, unsigned int flags)
126 {
127         char *arr[4];
128         char str_on[32], str_off[32];
129         char str_color[32], str_flags[32];
130         int ret;
131
132         if (!support_front_led)
133                 return DEVICE_ERROR_NOT_SUPPORTED;
134
135         if (on < 0 || off < 0)
136                 return DEVICE_ERROR_INVALID_PARAMETER;
137
138         snprintf(str_on, sizeof(str_on), "%d", on);
139         arr[0] = str_on;
140         snprintf(str_off, sizeof(str_off), "%d", off);
141         arr[1] = str_off;
142         snprintf(str_color, sizeof(str_color), "%lu", (long unsigned int)color);
143         arr[2] = str_color;
144         snprintf(str_flags, sizeof(str_flags), "%lu", (long unsigned int)flags);
145         arr[3] = str_flags;
146
147         ret = dbus_method_sync(DEVICED_BUS_NAME,
148                         DEVICED_PATH_LED, DEVICED_INTERFACE_LED,
149                         METHOD_PLAY_CUSTOM, "iiuu", arr);
150         if (ret < 0)
151                 return errno_to_device_error(ret);
152
153         return DEVICE_ERROR_NONE;
154 }
155
156 int device_led_stop_custom(void)
157 {
158         int ret;
159
160         if (!support_front_led)
161                 return DEVICE_ERROR_NOT_SUPPORTED;
162
163         ret = dbus_method_sync(DEVICED_BUS_NAME,
164                         DEVICED_PATH_LED, DEVICED_INTERFACE_LED,
165                         METHOD_STOP_CUSTOM, NULL, NULL);
166 //LCOV_EXCL_START System Error
167         if (ret < 0)
168                 return errno_to_device_error(ret);
169 //LCOV_EXCL_STOP
170
171         return DEVICE_ERROR_NONE;
172 }