blob: 12af3849b085aafa08030bd1c42b6ab317883d60 [file] [log] [blame]
Sungmin Choib5148892012-07-02 17:00:07 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
Arun Kumar K.R0efad602014-01-21 21:32:36 -08003 * Copyright (C) 2014 The Linux Foundation. All rights reserved.
Sungmin Choib5148892012-07-02 17:00:07 -07004 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
19// #define LOG_NDEBUG 0
Sungmin Choib5148892012-07-02 17:00:07 -070020
21#include <cutils/log.h>
22
23#include <stdint.h>
24#include <string.h>
25#include <unistd.h>
26#include <errno.h>
27#include <fcntl.h>
28#include <pthread.h>
29
30#include <sys/ioctl.h>
31#include <sys/types.h>
32
33#include <hardware/lights.h>
34
35/******************************************************************************/
36
37static pthread_once_t g_init = PTHREAD_ONCE_INIT;
38static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER;
39static struct light_state_t g_notification;
40static struct light_state_t g_battery;
41static int g_attention = 0;
42
43char const*const RED_LED_FILE
44 = "/sys/class/leds/red/brightness";
45
46char const*const GREEN_LED_FILE
47 = "/sys/class/leds/green/brightness";
48
49char const*const BLUE_LED_FILE
50 = "/sys/class/leds/blue/brightness";
51
52char const*const LCD_FILE
53 = "/sys/class/leds/lcd-backlight/brightness";
54
HuiWangf73bc572013-07-31 10:48:36 +080055char const*const BUTTON_FILE
56 = "/sys/class/leds/button-backlight/brightness";
57
samin.ryud57c7d52012-08-03 23:59:41 +090058char const*const RED_BLINK_FILE
Yulian Shandorov784d7392013-06-20 04:28:59 -070059 = "/sys/class/leds/red/blink";
samin.ryud57c7d52012-08-03 23:59:41 +090060
Ameya Thakur192c8ac2013-08-12 16:50:01 -070061char const*const GREEN_BLINK_FILE
62 = "/sys/class/leds/green/blink";
63
64char const*const BLUE_BLINK_FILE
65 = "/sys/class/leds/blue/blink";
66
Sungmin Choib5148892012-07-02 17:00:07 -070067/**
68 * device methods
69 */
70
71void init_globals(void)
72{
73 // init the mutex
74 pthread_mutex_init(&g_lock, NULL);
75}
76
77static int
78write_int(char const* path, int value)
79{
80 int fd;
81 static int already_warned = 0;
82
83 fd = open(path, O_RDWR);
84 if (fd >= 0) {
85 char buffer[20];
Manoj Kumar AVM001b3092014-04-29 22:08:51 -070086 int bytes = snprintf(buffer, sizeof(buffer), "%d\n", value);
Dileep Kumar Reddibf333c72014-02-25 14:32:51 +053087 ssize_t amt = write(fd, buffer, (size_t)bytes);
Sungmin Choib5148892012-07-02 17:00:07 -070088 close(fd);
89 return amt == -1 ? -errno : 0;
90 } else {
91 if (already_warned == 0) {
92 ALOGE("write_int failed to open %s\n", path);
93 already_warned = 1;
94 }
95 return -errno;
96 }
97}
98
99static int
100is_lit(struct light_state_t const* state)
101{
102 return state->color & 0x00ffffff;
103}
104
105static int
106rgb_to_brightness(struct light_state_t const* state)
107{
108 int color = state->color & 0x00ffffff;
109 return ((77*((color>>16)&0x00ff))
110 + (150*((color>>8)&0x00ff)) + (29*(color&0x00ff))) >> 8;
111}
112
113static int
114set_light_backlight(struct light_device_t* dev,
115 struct light_state_t const* state)
116{
117 int err = 0;
118 int brightness = rgb_to_brightness(state);
Arun Kumar K.R0efad602014-01-21 21:32:36 -0800119 if(!dev) {
120 return -1;
121 }
Sungmin Choib5148892012-07-02 17:00:07 -0700122 pthread_mutex_lock(&g_lock);
123 err = write_int(LCD_FILE, brightness);
124 pthread_mutex_unlock(&g_lock);
125 return err;
126}
127
128static int
129set_speaker_light_locked(struct light_device_t* dev,
130 struct light_state_t const* state)
131{
Arun Kumar K.R0efad602014-01-21 21:32:36 -0800132 int red, green, blue;
Yulian Shandorov784d7392013-06-20 04:28:59 -0700133 int blink;
Sungmin Choib5148892012-07-02 17:00:07 -0700134 int onMS, offMS;
135 unsigned int colorRGB;
136
Arun Kumar K.R0efad602014-01-21 21:32:36 -0800137 if(!dev) {
138 return -1;
139 }
140
Sungmin Choib5148892012-07-02 17:00:07 -0700141 switch (state->flashMode) {
142 case LIGHT_FLASH_TIMED:
143 onMS = state->flashOnMS;
144 offMS = state->flashOffMS;
145 break;
146 case LIGHT_FLASH_NONE:
147 default:
148 onMS = 0;
149 offMS = 0;
150 break;
151 }
152
153 colorRGB = state->color;
154
155#if 0
samin.ryud57c7d52012-08-03 23:59:41 +0900156 ALOGD("set_speaker_light_locked mode %d, colorRGB=%08X, onMS=%d, offMS=%d\n",
157 state->flashMode, colorRGB, onMS, offMS);
Sungmin Choib5148892012-07-02 17:00:07 -0700158#endif
159
160 red = (colorRGB >> 16) & 0xFF;
161 green = (colorRGB >> 8) & 0xFF;
162 blue = colorRGB & 0xFF;
163
Sungmin Choib5148892012-07-02 17:00:07 -0700164 if (onMS > 0 && offMS > 0) {
Ashay Jaiswal26f0b532015-05-03 17:58:58 +0530165 /*
166 * if ON time == OFF time
167 * use blink mode 2
168 * else
169 * use blink mode 1
170 */
171 if (onMS == offMS)
172 blink = 2;
173 else
174 blink = 1;
Sungmin Choib5148892012-07-02 17:00:07 -0700175 } else {
176 blink = 0;
Sungmin Choib5148892012-07-02 17:00:07 -0700177 }
178
179 if (blink) {
Mao Li728ee0b2014-07-01 23:06:16 +0800180 if (red) {
181 if (write_int(RED_BLINK_FILE, blink))
182 write_int(RED_LED_FILE, 0);
183 }
184 if (green) {
185 if (write_int(GREEN_BLINK_FILE, blink))
186 write_int(GREEN_LED_FILE, 0);
187 }
188 if (blue) {
189 if (write_int(BLUE_BLINK_FILE, blink))
190 write_int(BLUE_LED_FILE, 0);
191 }
Yulian Shandorov784d7392013-06-20 04:28:59 -0700192 } else {
193 write_int(RED_LED_FILE, red);
194 write_int(GREEN_LED_FILE, green);
195 write_int(BLUE_LED_FILE, blue);
Sungmin Choib5148892012-07-02 17:00:07 -0700196 }
Sungmin Choib5148892012-07-02 17:00:07 -0700197
198 return 0;
199}
200
201static void
202handle_speaker_battery_locked(struct light_device_t* dev)
203{
204 if (is_lit(&g_battery)) {
205 set_speaker_light_locked(dev, &g_battery);
206 } else {
207 set_speaker_light_locked(dev, &g_notification);
208 }
209}
210
211static int
Mao Lia4e053a2014-06-16 23:05:17 +0530212set_light_battery(struct light_device_t* dev,
213 struct light_state_t const* state)
214{
215 pthread_mutex_lock(&g_lock);
216 g_battery = *state;
217 handle_speaker_battery_locked(dev);
218 pthread_mutex_unlock(&g_lock);
219 return 0;
220}
221
222static int
Sungmin Choib5148892012-07-02 17:00:07 -0700223set_light_notifications(struct light_device_t* dev,
224 struct light_state_t const* state)
225{
226 pthread_mutex_lock(&g_lock);
227 g_notification = *state;
228 handle_speaker_battery_locked(dev);
229 pthread_mutex_unlock(&g_lock);
230 return 0;
231}
232
233static int
234set_light_attention(struct light_device_t* dev,
235 struct light_state_t const* state)
236{
237 pthread_mutex_lock(&g_lock);
238 if (state->flashMode == LIGHT_FLASH_HARDWARE) {
239 g_attention = state->flashOnMS;
240 } else if (state->flashMode == LIGHT_FLASH_NONE) {
241 g_attention = 0;
242 }
243 handle_speaker_battery_locked(dev);
244 pthread_mutex_unlock(&g_lock);
245 return 0;
246}
247
HuiWangf73bc572013-07-31 10:48:36 +0800248static int
249set_light_buttons(struct light_device_t* dev,
250 struct light_state_t const* state)
251{
252 int err = 0;
Arun Kumar K.R0efad602014-01-21 21:32:36 -0800253 if(!dev) {
254 return -1;
255 }
HuiWangf73bc572013-07-31 10:48:36 +0800256 pthread_mutex_lock(&g_lock);
257 err = write_int(BUTTON_FILE, state->color & 0xFF);
258 pthread_mutex_unlock(&g_lock);
259 return err;
260}
Sungmin Choib5148892012-07-02 17:00:07 -0700261
262/** Close the lights device */
263static int
264close_lights(struct light_device_t *dev)
265{
266 if (dev) {
267 free(dev);
268 }
269 return 0;
270}
271
272
273/******************************************************************************/
274
275/**
276 * module methods
277 */
278
279/** Open a new instance of a lights device using name */
280static int open_lights(const struct hw_module_t* module, char const* name,
281 struct hw_device_t** device)
282{
283 int (*set_light)(struct light_device_t* dev,
284 struct light_state_t const* state);
285
286 if (0 == strcmp(LIGHT_ID_BACKLIGHT, name))
287 set_light = set_light_backlight;
Mao Lia4e053a2014-06-16 23:05:17 +0530288 else if (0 == strcmp(LIGHT_ID_BATTERY, name))
289 set_light = set_light_battery;
Sungmin Choib5148892012-07-02 17:00:07 -0700290 else if (0 == strcmp(LIGHT_ID_NOTIFICATIONS, name))
291 set_light = set_light_notifications;
HuiWangf73bc572013-07-31 10:48:36 +0800292 else if (0 == strcmp(LIGHT_ID_BUTTONS, name))
293 set_light = set_light_buttons;
Sungmin Choib5148892012-07-02 17:00:07 -0700294 else if (0 == strcmp(LIGHT_ID_ATTENTION, name))
295 set_light = set_light_attention;
296 else
297 return -EINVAL;
298
299 pthread_once(&g_init, init_globals);
300
301 struct light_device_t *dev = malloc(sizeof(struct light_device_t));
Manoj Kumar AVM001b3092014-04-29 22:08:51 -0700302
303 if(!dev)
304 return -ENOMEM;
305
Sungmin Choib5148892012-07-02 17:00:07 -0700306 memset(dev, 0, sizeof(*dev));
307
308 dev->common.tag = HARDWARE_DEVICE_TAG;
309 dev->common.version = 0;
310 dev->common.module = (struct hw_module_t*)module;
311 dev->common.close = (int (*)(struct hw_device_t*))close_lights;
312 dev->set_light = set_light;
313
314 *device = (struct hw_device_t*)dev;
315 return 0;
316}
317
318static struct hw_module_methods_t lights_module_methods = {
319 .open = open_lights,
320};
321
322/*
323 * The lights Module
324 */
325struct hw_module_t HAL_MODULE_INFO_SYM = {
326 .tag = HARDWARE_MODULE_TAG,
327 .version_major = 1,
328 .version_minor = 0,
329 .id = LIGHTS_HARDWARE_MODULE_ID,
330 .name = "lights Module",
331 .author = "Google, Inc.",
332 .methods = &lights_module_methods,
333};