device: Merge public api on Tizen 2.3 into tizen branch
[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);
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);
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         ret = dbus_method_sync(DEVICED_BUS_NAME,
116                         DEVICED_PATH_LED, DEVICED_INTERFACE_LED,
117                         METHOD_SET_BRIGHTNESS, "ii", arr);
118         if (ret < 0)
119                 return errno_to_device_error(ret);
120
121         return DEVICE_ERROR_NONE;
122 }
123
124 int device_led_play_custom(int on, int off, unsigned int color, unsigned int flags)
125 {
126         char *arr[4];
127         char str_on[32], str_off[32];
128         char str_color[32], str_flags[32];
129         int ret;
130
131         if (!support_front_led)
132                 return DEVICE_ERROR_NOT_SUPPORTED;
133
134         if (on < 0 || off < 0)
135                 return DEVICE_ERROR_INVALID_PARAMETER;
136
137         snprintf(str_on, sizeof(str_on), "%d", on);
138         arr[0] = str_on;
139         snprintf(str_off, sizeof(str_off), "%d", off);
140         arr[1] = str_off;
141         snprintf(str_color, sizeof(str_color), "%lu", (long unsigned int)color);
142         arr[2] = str_color;
143         snprintf(str_flags, sizeof(str_flags), "%lu", (long unsigned int)flags);
144         arr[3] = str_flags;
145
146         ret = dbus_method_sync(DEVICED_BUS_NAME,
147                         DEVICED_PATH_LED, DEVICED_INTERFACE_LED,
148                         METHOD_PLAY_CUSTOM, "iiuu", arr);
149         if (ret < 0)
150                 return errno_to_device_error(ret);
151
152         return DEVICE_ERROR_NONE;
153 }
154
155 int device_led_stop_custom(void)
156 {
157         int ret;
158
159         if (!support_front_led)
160                 return DEVICE_ERROR_NOT_SUPPORTED;
161
162         ret = dbus_method_sync(DEVICED_BUS_NAME,
163                         DEVICED_PATH_LED, DEVICED_INTERFACE_LED,
164                         METHOD_STOP_CUSTOM, NULL, NULL);
165         if (ret < 0)
166                 return errno_to_device_error(ret);
167
168         return DEVICE_ERROR_NONE;
169 }