lights.c revision d57c7d55c9aef7fdae6c47bf03d84a3122b80847
1/*
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
19#define LOG_TAG "lights"
20
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
55char const*const RED_FREQ_FILE
56        = "/sys/class/leds/red/device/grpfreq";
57
58char const*const RED_PWM_FILE
59        = "/sys/class/leds/red/device/grppwm";
60
61char const*const RED_BLINK_FILE
62        = "/sys/class/leds/red/device/blink";
63
64char const*const LED_LOCK_UPDATE_FILE
65        = "/sys/class/leds/red/device/lock";
66
67/**
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];
86        int bytes = sprintf(buffer, "%d\n", value);
87        int amt = write(fd, buffer, bytes);
88        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);
119    pthread_mutex_lock(&g_lock);
120    err = write_int(LCD_FILE, brightness);
121    pthread_mutex_unlock(&g_lock);
122    return err;
123}
124
125static int
126set_speaker_light_locked(struct light_device_t* dev,
127        struct light_state_t const* state)
128{
129    int len;
130    int alpha, red, green, blue;
131    int blink, freq, pwm;
132    int onMS, offMS;
133    unsigned int colorRGB;
134
135    switch (state->flashMode) {
136        case LIGHT_FLASH_TIMED:
137            onMS = state->flashOnMS;
138            offMS = state->flashOffMS;
139            break;
140        case LIGHT_FLASH_NONE:
141        default:
142            onMS = 0;
143            offMS = 0;
144            break;
145    }
146
147    colorRGB = state->color;
148
149#if 0
150    ALOGD("set_speaker_light_locked mode %d, colorRGB=%08X, onMS=%d, offMS=%d\n",
151            state->flashMode, colorRGB, onMS, offMS);
152#endif
153
154    red = (colorRGB >> 16) & 0xFF;
155    green = (colorRGB >> 8) & 0xFF;
156    blue = colorRGB & 0xFF;
157
158    if (onMS > 0 && offMS > 0) {
159        int totalMS = onMS + offMS;
160
161        // the LED appears to blink about once per second if freq is 20
162        // 1000ms / 20 = 50
163        freq = totalMS / 50;
164        // pwm specifies the ratio of ON versus OFF
165        // pwm = 0 -> always off
166        // pwm = 255 => always on
167        pwm = (onMS * 255) / totalMS;
168
169        // the low 4 bits are ignored, so round up if necessary
170        if (pwm > 0 && pwm < 16)
171            pwm = 16;
172
173        blink = 1;
174    } else {
175        blink = 0;
176        freq = 0;
177        pwm = 0;
178    }
179
180    write_int(LED_LOCK_UPDATE_FILE, 1); // for LED On/Off synchronization
181
182    write_int(RED_LED_FILE, red);
183    write_int(GREEN_LED_FILE, green);
184    write_int(BLUE_LED_FILE, blue);
185
186    if (blink) {
187        write_int(RED_FREQ_FILE, freq);
188        write_int(RED_PWM_FILE, pwm);
189    }
190    write_int(RED_BLINK_FILE, blink);
191
192    write_int(LED_LOCK_UPDATE_FILE, 0);
193
194    return 0;
195}
196
197static void
198handle_speaker_battery_locked(struct light_device_t* dev)
199{
200    if (is_lit(&g_battery)) {
201        set_speaker_light_locked(dev, &g_battery);
202    } else {
203        set_speaker_light_locked(dev, &g_notification);
204    }
205}
206
207static int
208set_light_battery(struct light_device_t* dev,
209        struct light_state_t const* state)
210{
211    pthread_mutex_lock(&g_lock);
212    g_battery = *state;
213    handle_speaker_battery_locked(dev);
214    pthread_mutex_unlock(&g_lock);
215    return 0;
216}
217
218static int
219set_light_notifications(struct light_device_t* dev,
220        struct light_state_t const* state)
221{
222    pthread_mutex_lock(&g_lock);
223    g_notification = *state;
224    handle_speaker_battery_locked(dev);
225    pthread_mutex_unlock(&g_lock);
226    return 0;
227}
228
229static int
230set_light_attention(struct light_device_t* dev,
231        struct light_state_t const* state)
232{
233    pthread_mutex_lock(&g_lock);
234    if (state->flashMode == LIGHT_FLASH_HARDWARE) {
235        g_attention = state->flashOnMS;
236    } else if (state->flashMode == LIGHT_FLASH_NONE) {
237        g_attention = 0;
238    }
239    handle_speaker_battery_locked(dev);
240    pthread_mutex_unlock(&g_lock);
241    return 0;
242}
243
244
245/** Close the lights device */
246static int
247close_lights(struct light_device_t *dev)
248{
249    if (dev) {
250        free(dev);
251    }
252    return 0;
253}
254
255
256/******************************************************************************/
257
258/**
259 * module methods
260 */
261
262/** Open a new instance of a lights device using name */
263static int open_lights(const struct hw_module_t* module, char const* name,
264        struct hw_device_t** device)
265{
266    int (*set_light)(struct light_device_t* dev,
267            struct light_state_t const* state);
268
269    if (0 == strcmp(LIGHT_ID_BACKLIGHT, name))
270        set_light = set_light_backlight;
271    else if (0 == strcmp(LIGHT_ID_BATTERY, name))
272        set_light = set_light_battery;
273    else if (0 == strcmp(LIGHT_ID_NOTIFICATIONS, name))
274        set_light = set_light_notifications;
275    else if (0 == strcmp(LIGHT_ID_ATTENTION, name))
276        set_light = set_light_attention;
277    else
278        return -EINVAL;
279
280    pthread_once(&g_init, init_globals);
281
282    struct light_device_t *dev = malloc(sizeof(struct light_device_t));
283    memset(dev, 0, sizeof(*dev));
284
285    dev->common.tag = HARDWARE_DEVICE_TAG;
286    dev->common.version = 0;
287    dev->common.module = (struct hw_module_t*)module;
288    dev->common.close = (int (*)(struct hw_device_t*))close_lights;
289    dev->set_light = set_light;
290
291    *device = (struct hw_device_t*)dev;
292    return 0;
293}
294
295static struct hw_module_methods_t lights_module_methods = {
296    .open =  open_lights,
297};
298
299/*
300 * The lights Module
301 */
302struct hw_module_t HAL_MODULE_INFO_SYM = {
303    .tag = HARDWARE_MODULE_TAG,
304    .version_major = 1,
305    .version_minor = 0,
306    .id = LIGHTS_HARDWARE_MODULE_ID,
307    .name = "lights Module",
308    .author = "Google, Inc.",
309    .methods = &lights_module_methods,
310};
311