blob: 14a664614736b014c39e857e88864cf648a2adeb [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
Sungmin Choib5148892012-07-02 17:00:07 -070057/**
58 * device methods
59 */
60
61void init_globals(void)
62{
63 // init the mutex
64 pthread_mutex_init(&g_lock, NULL);
65}
66
67static int
68write_int(char const* path, int value)
69{
70 int fd;
71 static int already_warned = 0;
72
73 fd = open(path, O_RDWR);
74 if (fd >= 0) {
75 char buffer[20];
76 int bytes = sprintf(buffer, "%d\n", value);
77 int amt = write(fd, buffer, bytes);
78 close(fd);
79 return amt == -1 ? -errno : 0;
80 } else {
81 if (already_warned == 0) {
82 ALOGE("write_int failed to open %s\n", path);
83 already_warned = 1;
84 }
85 return -errno;
86 }
87}
88
89static int
90is_lit(struct light_state_t const* state)
91{
92 return state->color & 0x00ffffff;
93}
94
95static int
96rgb_to_brightness(struct light_state_t const* state)
97{
98 int color = state->color & 0x00ffffff;
99 return ((77*((color>>16)&0x00ff))
100 + (150*((color>>8)&0x00ff)) + (29*(color&0x00ff))) >> 8;
101}
102
103static int
104set_light_backlight(struct light_device_t* dev,
105 struct light_state_t const* state)
106{
107 int err = 0;
108 int brightness = rgb_to_brightness(state);
109 pthread_mutex_lock(&g_lock);
110 err = write_int(LCD_FILE, brightness);
111 pthread_mutex_unlock(&g_lock);
112 return err;
113}
114
115static int
116set_speaker_light_locked(struct light_device_t* dev,
117 struct light_state_t const* state)
118{
119 int len;
120 int alpha, red, green, blue;
Yulian Shandorov784d7392013-06-20 04:28:59 -0700121 int blink;
Sungmin Choib5148892012-07-02 17:00:07 -0700122 int onMS, offMS;
123 unsigned int colorRGB;
124
125 switch (state->flashMode) {
126 case LIGHT_FLASH_TIMED:
127 onMS = state->flashOnMS;
128 offMS = state->flashOffMS;
129 break;
130 case LIGHT_FLASH_NONE:
131 default:
132 onMS = 0;
133 offMS = 0;
134 break;
135 }
136
137 colorRGB = state->color;
138
139#if 0
samin.ryud57c7d52012-08-03 23:59:41 +0900140 ALOGD("set_speaker_light_locked mode %d, colorRGB=%08X, onMS=%d, offMS=%d\n",
141 state->flashMode, colorRGB, onMS, offMS);
Sungmin Choib5148892012-07-02 17:00:07 -0700142#endif
143
144 red = (colorRGB >> 16) & 0xFF;
145 green = (colorRGB >> 8) & 0xFF;
146 blue = colorRGB & 0xFF;
147
Sungmin Choib5148892012-07-02 17:00:07 -0700148 if (onMS > 0 && offMS > 0) {
Sungmin Choib5148892012-07-02 17:00:07 -0700149 blink = 1;
150 } else {
151 blink = 0;
Sungmin Choib5148892012-07-02 17:00:07 -0700152 }
153
154 if (blink) {
Yulian Shandorov784d7392013-06-20 04:28:59 -0700155 write_int(RED_BLINK_FILE, blink);
156 } else {
157 write_int(RED_LED_FILE, red);
158 write_int(GREEN_LED_FILE, green);
159 write_int(BLUE_LED_FILE, blue);
Sungmin Choib5148892012-07-02 17:00:07 -0700160 }
Sungmin Choib5148892012-07-02 17:00:07 -0700161
162 return 0;
163}
164
165static void
166handle_speaker_battery_locked(struct light_device_t* dev)
167{
168 if (is_lit(&g_battery)) {
169 set_speaker_light_locked(dev, &g_battery);
170 } else {
171 set_speaker_light_locked(dev, &g_notification);
172 }
173}
174
175static int
Sungmin Choib5148892012-07-02 17:00:07 -0700176set_light_notifications(struct light_device_t* dev,
177 struct light_state_t const* state)
178{
179 pthread_mutex_lock(&g_lock);
180 g_notification = *state;
181 handle_speaker_battery_locked(dev);
182 pthread_mutex_unlock(&g_lock);
183 return 0;
184}
185
186static int
187set_light_attention(struct light_device_t* dev,
188 struct light_state_t const* state)
189{
190 pthread_mutex_lock(&g_lock);
191 if (state->flashMode == LIGHT_FLASH_HARDWARE) {
192 g_attention = state->flashOnMS;
193 } else if (state->flashMode == LIGHT_FLASH_NONE) {
194 g_attention = 0;
195 }
196 handle_speaker_battery_locked(dev);
197 pthread_mutex_unlock(&g_lock);
198 return 0;
199}
200
201
202/** Close the lights device */
203static int
204close_lights(struct light_device_t *dev)
205{
206 if (dev) {
207 free(dev);
208 }
209 return 0;
210}
211
212
213/******************************************************************************/
214
215/**
216 * module methods
217 */
218
219/** Open a new instance of a lights device using name */
220static int open_lights(const struct hw_module_t* module, char const* name,
221 struct hw_device_t** device)
222{
223 int (*set_light)(struct light_device_t* dev,
224 struct light_state_t const* state);
225
226 if (0 == strcmp(LIGHT_ID_BACKLIGHT, name))
227 set_light = set_light_backlight;
Sungmin Choib5148892012-07-02 17:00:07 -0700228 else if (0 == strcmp(LIGHT_ID_NOTIFICATIONS, name))
229 set_light = set_light_notifications;
230 else if (0 == strcmp(LIGHT_ID_ATTENTION, name))
231 set_light = set_light_attention;
232 else
233 return -EINVAL;
234
235 pthread_once(&g_init, init_globals);
236
237 struct light_device_t *dev = malloc(sizeof(struct light_device_t));
238 memset(dev, 0, sizeof(*dev));
239
240 dev->common.tag = HARDWARE_DEVICE_TAG;
241 dev->common.version = 0;
242 dev->common.module = (struct hw_module_t*)module;
243 dev->common.close = (int (*)(struct hw_device_t*))close_lights;
244 dev->set_light = set_light;
245
246 *device = (struct hw_device_t*)dev;
247 return 0;
248}
249
250static struct hw_module_methods_t lights_module_methods = {
251 .open = open_lights,
252};
253
254/*
255 * The lights Module
256 */
257struct hw_module_t HAL_MODULE_INFO_SYM = {
258 .tag = HARDWARE_MODULE_TAG,
259 .version_major = 1,
260 .version_minor = 0,
261 .id = LIGHTS_HARDWARE_MODULE_ID,
262 .name = "lights Module",
263 .author = "Google, Inc.",
264 .methods = &lights_module_methods,
265};