blob: eb71b7423333c0a06c139e94edca7df84b3b6836 [file] [log] [blame]
Sungmin Choib5148892012-07-02 17:00:07 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
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// #define LOG_NDEBUG 0
Sungmin Choib5148892012-07-02 17:00:07 -070019
20#include <cutils/log.h>
21
22#include <stdint.h>
23#include <string.h>
24#include <unistd.h>
25#include <errno.h>
26#include <fcntl.h>
27#include <pthread.h>
28
29#include <sys/ioctl.h>
30#include <sys/types.h>
31
32#include <hardware/lights.h>
33
34/******************************************************************************/
35
36static pthread_once_t g_init = PTHREAD_ONCE_INIT;
37static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER;
38static struct light_state_t g_notification;
39static struct light_state_t g_battery;
40static int g_attention = 0;
41
42char const*const RED_LED_FILE
43 = "/sys/class/leds/red/brightness";
44
45char const*const GREEN_LED_FILE
46 = "/sys/class/leds/green/brightness";
47
48char const*const BLUE_LED_FILE
49 = "/sys/class/leds/blue/brightness";
50
51char const*const LCD_FILE
52 = "/sys/class/leds/lcd-backlight/brightness";
53
samin.ryud57c7d52012-08-03 23:59:41 +090054char const*const RED_BLINK_FILE
Yulian Shandorov784d7392013-06-20 04:28:59 -070055 = "/sys/class/leds/red/blink";
samin.ryud57c7d52012-08-03 23:59:41 +090056
Ameya Thakur192c8ac2013-08-12 16:50:01 -070057char const*const GREEN_BLINK_FILE
58 = "/sys/class/leds/green/blink";
59
60char const*const BLUE_BLINK_FILE
61 = "/sys/class/leds/blue/blink";
62
Sungmin Choib5148892012-07-02 17:00:07 -070063/**
64 * device methods
65 */
66
67void init_globals(void)
68{
69 // init the mutex
70 pthread_mutex_init(&g_lock, NULL);
71}
72
73static int
74write_int(char const* path, int value)
75{
76 int fd;
77 static int already_warned = 0;
78
79 fd = open(path, O_RDWR);
80 if (fd >= 0) {
81 char buffer[20];
82 int bytes = sprintf(buffer, "%d\n", value);
83 int amt = write(fd, buffer, bytes);
84 close(fd);
85 return amt == -1 ? -errno : 0;
86 } else {
87 if (already_warned == 0) {
88 ALOGE("write_int failed to open %s\n", path);
89 already_warned = 1;
90 }
91 return -errno;
92 }
93}
94
95static int
96is_lit(struct light_state_t const* state)
97{
98 return state->color & 0x00ffffff;
99}
100
101static int
102rgb_to_brightness(struct light_state_t const* state)
103{
104 int color = state->color & 0x00ffffff;
105 return ((77*((color>>16)&0x00ff))
106 + (150*((color>>8)&0x00ff)) + (29*(color&0x00ff))) >> 8;
107}
108
109static int
110set_light_backlight(struct light_device_t* dev,
111 struct light_state_t const* state)
112{
113 int err = 0;
114 int brightness = rgb_to_brightness(state);
115 pthread_mutex_lock(&g_lock);
116 err = write_int(LCD_FILE, brightness);
117 pthread_mutex_unlock(&g_lock);
118 return err;
119}
120
121static int
122set_speaker_light_locked(struct light_device_t* dev,
123 struct light_state_t const* state)
124{
125 int len;
126 int alpha, red, green, blue;
Yulian Shandorov784d7392013-06-20 04:28:59 -0700127 int blink;
Sungmin Choib5148892012-07-02 17:00:07 -0700128 int onMS, offMS;
129 unsigned int colorRGB;
130
131 switch (state->flashMode) {
132 case LIGHT_FLASH_TIMED:
133 onMS = state->flashOnMS;
134 offMS = state->flashOffMS;
135 break;
136 case LIGHT_FLASH_NONE:
137 default:
138 onMS = 0;
139 offMS = 0;
140 break;
141 }
142
143 colorRGB = state->color;
144
145#if 0
samin.ryud57c7d52012-08-03 23:59:41 +0900146 ALOGD("set_speaker_light_locked mode %d, colorRGB=%08X, onMS=%d, offMS=%d\n",
147 state->flashMode, colorRGB, onMS, offMS);
Sungmin Choib5148892012-07-02 17:00:07 -0700148#endif
149
150 red = (colorRGB >> 16) & 0xFF;
151 green = (colorRGB >> 8) & 0xFF;
152 blue = colorRGB & 0xFF;
153
Sungmin Choib5148892012-07-02 17:00:07 -0700154 if (onMS > 0 && offMS > 0) {
Sungmin Choib5148892012-07-02 17:00:07 -0700155 blink = 1;
156 } else {
157 blink = 0;
Sungmin Choib5148892012-07-02 17:00:07 -0700158 }
159
160 if (blink) {
Ameya Thakur192c8ac2013-08-12 16:50:01 -0700161 if (red)
162 write_int(RED_BLINK_FILE, blink);
163 if (green)
164 write_int(GREEN_BLINK_FILE, blink);
165 if (blue)
166 write_int(BLUE_BLINK_FILE, blink);
Yulian Shandorov784d7392013-06-20 04:28:59 -0700167 } else {
168 write_int(RED_LED_FILE, red);
169 write_int(GREEN_LED_FILE, green);
170 write_int(BLUE_LED_FILE, blue);
Sungmin Choib5148892012-07-02 17:00:07 -0700171 }
Sungmin Choib5148892012-07-02 17:00:07 -0700172
173 return 0;
174}
175
176static void
177handle_speaker_battery_locked(struct light_device_t* dev)
178{
179 if (is_lit(&g_battery)) {
180 set_speaker_light_locked(dev, &g_battery);
181 } else {
182 set_speaker_light_locked(dev, &g_notification);
183 }
184}
185
186static int
Sungmin Choib5148892012-07-02 17:00:07 -0700187set_light_notifications(struct light_device_t* dev,
188 struct light_state_t const* state)
189{
190 pthread_mutex_lock(&g_lock);
191 g_notification = *state;
192 handle_speaker_battery_locked(dev);
193 pthread_mutex_unlock(&g_lock);
194 return 0;
195}
196
197static int
198set_light_attention(struct light_device_t* dev,
199 struct light_state_t const* state)
200{
201 pthread_mutex_lock(&g_lock);
202 if (state->flashMode == LIGHT_FLASH_HARDWARE) {
203 g_attention = state->flashOnMS;
204 } else if (state->flashMode == LIGHT_FLASH_NONE) {
205 g_attention = 0;
206 }
207 handle_speaker_battery_locked(dev);
208 pthread_mutex_unlock(&g_lock);
209 return 0;
210}
211
212
213/** Close the lights device */
214static int
215close_lights(struct light_device_t *dev)
216{
217 if (dev) {
218 free(dev);
219 }
220 return 0;
221}
222
223
224/******************************************************************************/
225
226/**
227 * module methods
228 */
229
230/** Open a new instance of a lights device using name */
231static int open_lights(const struct hw_module_t* module, char const* name,
232 struct hw_device_t** device)
233{
234 int (*set_light)(struct light_device_t* dev,
235 struct light_state_t const* state);
236
237 if (0 == strcmp(LIGHT_ID_BACKLIGHT, name))
238 set_light = set_light_backlight;
Sungmin Choib5148892012-07-02 17:00:07 -0700239 else if (0 == strcmp(LIGHT_ID_NOTIFICATIONS, name))
240 set_light = set_light_notifications;
241 else if (0 == strcmp(LIGHT_ID_ATTENTION, name))
242 set_light = set_light_attention;
243 else
244 return -EINVAL;
245
246 pthread_once(&g_init, init_globals);
247
248 struct light_device_t *dev = malloc(sizeof(struct light_device_t));
249 memset(dev, 0, sizeof(*dev));
250
251 dev->common.tag = HARDWARE_DEVICE_TAG;
252 dev->common.version = 0;
253 dev->common.module = (struct hw_module_t*)module;
254 dev->common.close = (int (*)(struct hw_device_t*))close_lights;
255 dev->set_light = set_light;
256
257 *device = (struct hw_device_t*)dev;
258 return 0;
259}
260
261static struct hw_module_methods_t lights_module_methods = {
262 .open = open_lights,
263};
264
265/*
266 * The lights Module
267 */
268struct hw_module_t HAL_MODULE_INFO_SYM = {
269 .tag = HARDWARE_MODULE_TAG,
270 .version_major = 1,
271 .version_minor = 0,
272 .id = LIGHTS_HARDWARE_MODULE_ID,
273 .name = "lights Module",
274 .author = "Google, Inc.",
275 .methods = &lights_module_methods,
276};