blob: ccfbe1025abd799997c296803cd0887c5cf8b3a7 [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
HuiWangf73bc572013-07-31 10:48:36 +080054char const*const BUTTON_FILE
55 = "/sys/class/leds/button-backlight/brightness";
56
samin.ryud57c7d52012-08-03 23:59:41 +090057char const*const RED_BLINK_FILE
Yulian Shandorov784d7392013-06-20 04:28:59 -070058 = "/sys/class/leds/red/blink";
samin.ryud57c7d52012-08-03 23:59:41 +090059
Ameya Thakur192c8ac2013-08-12 16:50:01 -070060char const*const GREEN_BLINK_FILE
61 = "/sys/class/leds/green/blink";
62
63char const*const BLUE_BLINK_FILE
64 = "/sys/class/leds/blue/blink";
65
Sungmin Choib5148892012-07-02 17:00:07 -070066/**
67 * device methods
68 */
69
70void init_globals(void)
71{
72 // init the mutex
73 pthread_mutex_init(&g_lock, NULL);
74}
75
76static int
77write_int(char const* path, int value)
78{
79 int fd;
80 static int already_warned = 0;
81
82 fd = open(path, O_RDWR);
83 if (fd >= 0) {
84 char buffer[20];
85 int bytes = sprintf(buffer, "%d\n", value);
86 int amt = write(fd, buffer, bytes);
87 close(fd);
88 return amt == -1 ? -errno : 0;
89 } else {
90 if (already_warned == 0) {
91 ALOGE("write_int failed to open %s\n", path);
92 already_warned = 1;
93 }
94 return -errno;
95 }
96}
97
98static int
99is_lit(struct light_state_t const* state)
100{
101 return state->color & 0x00ffffff;
102}
103
104static int
105rgb_to_brightness(struct light_state_t const* state)
106{
107 int color = state->color & 0x00ffffff;
108 return ((77*((color>>16)&0x00ff))
109 + (150*((color>>8)&0x00ff)) + (29*(color&0x00ff))) >> 8;
110}
111
112static int
113set_light_backlight(struct light_device_t* dev,
114 struct light_state_t const* state)
115{
116 int err = 0;
117 int brightness = rgb_to_brightness(state);
118 pthread_mutex_lock(&g_lock);
119 err = write_int(LCD_FILE, brightness);
120 pthread_mutex_unlock(&g_lock);
121 return err;
122}
123
124static int
125set_speaker_light_locked(struct light_device_t* dev,
126 struct light_state_t const* state)
127{
128 int len;
129 int alpha, red, green, blue;
Yulian Shandorov784d7392013-06-20 04:28:59 -0700130 int blink;
Sungmin Choib5148892012-07-02 17:00:07 -0700131 int onMS, offMS;
132 unsigned int colorRGB;
133
134 switch (state->flashMode) {
135 case LIGHT_FLASH_TIMED:
136 onMS = state->flashOnMS;
137 offMS = state->flashOffMS;
138 break;
139 case LIGHT_FLASH_NONE:
140 default:
141 onMS = 0;
142 offMS = 0;
143 break;
144 }
145
146 colorRGB = state->color;
147
148#if 0
samin.ryud57c7d52012-08-03 23:59:41 +0900149 ALOGD("set_speaker_light_locked mode %d, colorRGB=%08X, onMS=%d, offMS=%d\n",
150 state->flashMode, colorRGB, onMS, offMS);
Sungmin Choib5148892012-07-02 17:00:07 -0700151#endif
152
153 red = (colorRGB >> 16) & 0xFF;
154 green = (colorRGB >> 8) & 0xFF;
155 blue = colorRGB & 0xFF;
156
Sungmin Choib5148892012-07-02 17:00:07 -0700157 if (onMS > 0 && offMS > 0) {
Sungmin Choib5148892012-07-02 17:00:07 -0700158 blink = 1;
159 } else {
160 blink = 0;
Sungmin Choib5148892012-07-02 17:00:07 -0700161 }
162
163 if (blink) {
Ameya Thakur192c8ac2013-08-12 16:50:01 -0700164 if (red)
165 write_int(RED_BLINK_FILE, blink);
166 if (green)
167 write_int(GREEN_BLINK_FILE, blink);
168 if (blue)
169 write_int(BLUE_BLINK_FILE, blink);
Yulian Shandorov784d7392013-06-20 04:28:59 -0700170 } else {
171 write_int(RED_LED_FILE, red);
172 write_int(GREEN_LED_FILE, green);
173 write_int(BLUE_LED_FILE, blue);
Sungmin Choib5148892012-07-02 17:00:07 -0700174 }
Sungmin Choib5148892012-07-02 17:00:07 -0700175
176 return 0;
177}
178
179static void
180handle_speaker_battery_locked(struct light_device_t* dev)
181{
182 if (is_lit(&g_battery)) {
183 set_speaker_light_locked(dev, &g_battery);
184 } else {
185 set_speaker_light_locked(dev, &g_notification);
186 }
187}
188
189static int
Sungmin Choib5148892012-07-02 17:00:07 -0700190set_light_notifications(struct light_device_t* dev,
191 struct light_state_t const* state)
192{
193 pthread_mutex_lock(&g_lock);
194 g_notification = *state;
195 handle_speaker_battery_locked(dev);
196 pthread_mutex_unlock(&g_lock);
197 return 0;
198}
199
200static int
201set_light_attention(struct light_device_t* dev,
202 struct light_state_t const* state)
203{
204 pthread_mutex_lock(&g_lock);
205 if (state->flashMode == LIGHT_FLASH_HARDWARE) {
206 g_attention = state->flashOnMS;
207 } else if (state->flashMode == LIGHT_FLASH_NONE) {
208 g_attention = 0;
209 }
210 handle_speaker_battery_locked(dev);
211 pthread_mutex_unlock(&g_lock);
212 return 0;
213}
214
HuiWangf73bc572013-07-31 10:48:36 +0800215static int
216set_light_buttons(struct light_device_t* dev,
217 struct light_state_t const* state)
218{
219 int err = 0;
220 pthread_mutex_lock(&g_lock);
221 err = write_int(BUTTON_FILE, state->color & 0xFF);
222 pthread_mutex_unlock(&g_lock);
223 return err;
224}
Sungmin Choib5148892012-07-02 17:00:07 -0700225
226/** Close the lights device */
227static int
228close_lights(struct light_device_t *dev)
229{
230 if (dev) {
231 free(dev);
232 }
233 return 0;
234}
235
236
237/******************************************************************************/
238
239/**
240 * module methods
241 */
242
243/** Open a new instance of a lights device using name */
244static int open_lights(const struct hw_module_t* module, char const* name,
245 struct hw_device_t** device)
246{
247 int (*set_light)(struct light_device_t* dev,
248 struct light_state_t const* state);
249
250 if (0 == strcmp(LIGHT_ID_BACKLIGHT, name))
251 set_light = set_light_backlight;
Sungmin Choib5148892012-07-02 17:00:07 -0700252 else if (0 == strcmp(LIGHT_ID_NOTIFICATIONS, name))
253 set_light = set_light_notifications;
HuiWangf73bc572013-07-31 10:48:36 +0800254 else if (0 == strcmp(LIGHT_ID_BUTTONS, name))
255 set_light = set_light_buttons;
Sungmin Choib5148892012-07-02 17:00:07 -0700256 else if (0 == strcmp(LIGHT_ID_ATTENTION, name))
257 set_light = set_light_attention;
258 else
259 return -EINVAL;
260
261 pthread_once(&g_init, init_globals);
262
263 struct light_device_t *dev = malloc(sizeof(struct light_device_t));
264 memset(dev, 0, sizeof(*dev));
265
266 dev->common.tag = HARDWARE_DEVICE_TAG;
267 dev->common.version = 0;
268 dev->common.module = (struct hw_module_t*)module;
269 dev->common.close = (int (*)(struct hw_device_t*))close_lights;
270 dev->set_light = set_light;
271
272 *device = (struct hw_device_t*)dev;
273 return 0;
274}
275
276static struct hw_module_methods_t lights_module_methods = {
277 .open = open_lights,
278};
279
280/*
281 * The lights Module
282 */
283struct hw_module_t HAL_MODULE_INFO_SYM = {
284 .tag = HARDWARE_MODULE_TAG,
285 .version_major = 1,
286 .version_minor = 0,
287 .id = LIGHTS_HARDWARE_MODULE_ID,
288 .name = "lights Module",
289 .author = "Google, Inc.",
290 .methods = &lights_module_methods,
291};