Sungmin Choi | b514889 | 2012-07-02 17:00:07 -0700 | [diff] [blame] | 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 |
Sungmin Choi | b514889 | 2012-07-02 17:00:07 -0700 | [diff] [blame] | 19 | |
| 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 | |
| 36 | static pthread_once_t g_init = PTHREAD_ONCE_INIT; |
| 37 | static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER; |
| 38 | static struct light_state_t g_notification; |
| 39 | static struct light_state_t g_battery; |
| 40 | static int g_attention = 0; |
| 41 | |
| 42 | char const*const RED_LED_FILE |
| 43 | = "/sys/class/leds/red/brightness"; |
| 44 | |
| 45 | char const*const GREEN_LED_FILE |
| 46 | = "/sys/class/leds/green/brightness"; |
| 47 | |
| 48 | char const*const BLUE_LED_FILE |
| 49 | = "/sys/class/leds/blue/brightness"; |
| 50 | |
| 51 | char const*const LCD_FILE |
| 52 | = "/sys/class/leds/lcd-backlight/brightness"; |
| 53 | |
HuiWang | f73bc57 | 2013-07-31 10:48:36 +0800 | [diff] [blame] | 54 | char const*const BUTTON_FILE |
| 55 | = "/sys/class/leds/button-backlight/brightness"; |
| 56 | |
samin.ryu | d57c7d5 | 2012-08-03 23:59:41 +0900 | [diff] [blame] | 57 | char const*const RED_BLINK_FILE |
Yulian Shandorov | 784d739 | 2013-06-20 04:28:59 -0700 | [diff] [blame] | 58 | = "/sys/class/leds/red/blink"; |
samin.ryu | d57c7d5 | 2012-08-03 23:59:41 +0900 | [diff] [blame] | 59 | |
Ameya Thakur | 192c8ac | 2013-08-12 16:50:01 -0700 | [diff] [blame] | 60 | char const*const GREEN_BLINK_FILE |
| 61 | = "/sys/class/leds/green/blink"; |
| 62 | |
| 63 | char const*const BLUE_BLINK_FILE |
| 64 | = "/sys/class/leds/blue/blink"; |
| 65 | |
Sungmin Choi | b514889 | 2012-07-02 17:00:07 -0700 | [diff] [blame] | 66 | /** |
| 67 | * device methods |
| 68 | */ |
| 69 | |
| 70 | void init_globals(void) |
| 71 | { |
| 72 | // init the mutex |
| 73 | pthread_mutex_init(&g_lock, NULL); |
| 74 | } |
| 75 | |
| 76 | static int |
| 77 | write_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 | |
| 98 | static int |
| 99 | is_lit(struct light_state_t const* state) |
| 100 | { |
| 101 | return state->color & 0x00ffffff; |
| 102 | } |
| 103 | |
| 104 | static int |
| 105 | rgb_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 | |
| 112 | static int |
| 113 | set_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 | |
| 124 | static int |
| 125 | set_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 Shandorov | 784d739 | 2013-06-20 04:28:59 -0700 | [diff] [blame] | 130 | int blink; |
Sungmin Choi | b514889 | 2012-07-02 17:00:07 -0700 | [diff] [blame] | 131 | 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.ryu | d57c7d5 | 2012-08-03 23:59:41 +0900 | [diff] [blame] | 149 | ALOGD("set_speaker_light_locked mode %d, colorRGB=%08X, onMS=%d, offMS=%d\n", |
| 150 | state->flashMode, colorRGB, onMS, offMS); |
Sungmin Choi | b514889 | 2012-07-02 17:00:07 -0700 | [diff] [blame] | 151 | #endif |
| 152 | |
| 153 | red = (colorRGB >> 16) & 0xFF; |
| 154 | green = (colorRGB >> 8) & 0xFF; |
| 155 | blue = colorRGB & 0xFF; |
| 156 | |
Sungmin Choi | b514889 | 2012-07-02 17:00:07 -0700 | [diff] [blame] | 157 | if (onMS > 0 && offMS > 0) { |
Sungmin Choi | b514889 | 2012-07-02 17:00:07 -0700 | [diff] [blame] | 158 | blink = 1; |
| 159 | } else { |
| 160 | blink = 0; |
Sungmin Choi | b514889 | 2012-07-02 17:00:07 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | if (blink) { |
Ameya Thakur | 192c8ac | 2013-08-12 16:50:01 -0700 | [diff] [blame] | 164 | 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 Shandorov | 784d739 | 2013-06-20 04:28:59 -0700 | [diff] [blame] | 170 | } else { |
| 171 | write_int(RED_LED_FILE, red); |
| 172 | write_int(GREEN_LED_FILE, green); |
| 173 | write_int(BLUE_LED_FILE, blue); |
Sungmin Choi | b514889 | 2012-07-02 17:00:07 -0700 | [diff] [blame] | 174 | } |
Sungmin Choi | b514889 | 2012-07-02 17:00:07 -0700 | [diff] [blame] | 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | static void |
| 180 | handle_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 | |
| 189 | static int |
Sungmin Choi | b514889 | 2012-07-02 17:00:07 -0700 | [diff] [blame] | 190 | set_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 | |
| 200 | static int |
| 201 | set_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 | |
HuiWang | f73bc57 | 2013-07-31 10:48:36 +0800 | [diff] [blame] | 215 | static int |
| 216 | set_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 Choi | b514889 | 2012-07-02 17:00:07 -0700 | [diff] [blame] | 225 | |
| 226 | /** Close the lights device */ |
| 227 | static int |
| 228 | close_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 */ |
| 244 | static 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 Choi | b514889 | 2012-07-02 17:00:07 -0700 | [diff] [blame] | 252 | else if (0 == strcmp(LIGHT_ID_NOTIFICATIONS, name)) |
| 253 | set_light = set_light_notifications; |
HuiWang | f73bc57 | 2013-07-31 10:48:36 +0800 | [diff] [blame] | 254 | else if (0 == strcmp(LIGHT_ID_BUTTONS, name)) |
| 255 | set_light = set_light_buttons; |
Sungmin Choi | b514889 | 2012-07-02 17:00:07 -0700 | [diff] [blame] | 256 | 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 | |
| 276 | static struct hw_module_methods_t lights_module_methods = { |
| 277 | .open = open_lights, |
| 278 | }; |
| 279 | |
| 280 | /* |
| 281 | * The lights Module |
| 282 | */ |
| 283 | struct 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 | }; |