1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * Copyright (C) 2014 The Linux Foundation. All rights reserved. 4 * 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 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 #include <malloc.h> 30 31 #include <sys/ioctl.h> 32 #include <sys/types.h> 33 34 #include <hardware/lights.h> 35 36 /******************************************************************************/ 37 38 static pthread_once_t g_init = PTHREAD_ONCE_INIT; 39 static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER; 40 static struct light_state_t g_notification; 41 static struct light_state_t g_battery; 42 static int g_attention = 0; 43 44 char const*const RED_LED_FILE 45 = "/sys/class/leds/red/brightness"; 46 47 char const*const GREEN_LED_FILE 48 = "/sys/class/leds/green/brightness"; 49 50 char const*const BLUE_LED_FILE 51 = "/sys/class/leds/blue/brightness"; 52 53 char const*const LCD_FILE 54 = "/sys/class/leds/lcd-backlight/brightness"; 55 56 char const*const BUTTON_FILE 57 = "/sys/class/leds/button-backlight/brightness"; 58 59 char const*const RED_BLINK_FILE 60 = "/sys/class/leds/red/blink"; 61 62 char const*const GREEN_BLINK_FILE 63 = "/sys/class/leds/green/blink"; 64 65 char const*const BLUE_BLINK_FILE 66 = "/sys/class/leds/blue/blink"; 67 68 /** 69 * device methods 70 */ 71 72 void init_globals(void) 73 { 74 // init the mutex 75 pthread_mutex_init(&g_lock, NULL); 76 } 77 78 static int 79 write_int(char const* path, int value) 80 { 81 int fd; 82 static int already_warned = 0; 83 84 fd = open(path, O_RDWR); 85 if (fd >= 0) { 86 char buffer[20]; 87 int bytes = snprintf(buffer, sizeof(buffer), "%d\n", value); 88 ssize_t amt = write(fd, buffer, (size_t)bytes); 89 close(fd); 90 return amt == -1 ? -errno : 0; 91 } else { 92 if (already_warned == 0) { 93 ALOGE("write_int failed to open %s\n", path); 94 already_warned = 1; 95 } 96 return -errno; 97 } 98 } 99 100 static int 101 is_lit(struct light_state_t const* state) 102 { 103 return state->color & 0x00ffffff; 104 } 105 106 static int 107 rgb_to_brightness(struct light_state_t const* state) 108 { 109 int color = state->color & 0x00ffffff; 110 return ((77*((color>>16)&0x00ff)) 111 + (150*((color>>8)&0x00ff)) + (29*(color&0x00ff))) >> 8; 112 } 113 114 static int 115 set_light_backlight(struct light_device_t* dev, 116 struct light_state_t const* state) 117 { 118 int err = 0; 119 int brightness = rgb_to_brightness(state); 120 if(!dev) { 121 return -1; 122 } 123 pthread_mutex_lock(&g_lock); 124 err = write_int(LCD_FILE, brightness); 125 pthread_mutex_unlock(&g_lock); 126 return err; 127 } 128 129 static int 130 set_speaker_light_locked(struct light_device_t* dev, 131 struct light_state_t const* state) 132 { 133 int red, green, blue; 134 int blink; 135 int onMS, offMS; 136 unsigned int colorRGB; 137 138 if(!dev) { 139 return -1; 140 } 141 142 switch (state->flashMode) { 143 case LIGHT_FLASH_TIMED: 144 onMS = state->flashOnMS; 145 offMS = state->flashOffMS; 146 break; 147 case LIGHT_FLASH_NONE: 148 default: 149 onMS = 0; 150 offMS = 0; 151 break; 152 } 153 154 colorRGB = state->color; 155 156 #if 0 157 ALOGD("set_speaker_light_locked mode %d, colorRGB=%08X, onMS=%d, offMS=%d\n", 158 state->flashMode, colorRGB, onMS, offMS); 159 #endif 160 161 red = (colorRGB >> 16) & 0xFF; 162 green = (colorRGB >> 8) & 0xFF; 163 blue = colorRGB & 0xFF; 164 165 if (onMS > 0 && offMS > 0) { 166 blink = 1; 167 } else { 168 blink = 0; 169 } 170 171 if (blink) { 172 if (red) { 173 if (write_int(RED_BLINK_FILE, blink)) 174 write_int(RED_LED_FILE, 0); 175 } 176 if (green) { 177 if (write_int(GREEN_BLINK_FILE, blink)) 178 write_int(GREEN_LED_FILE, 0); 179 } 180 if (blue) { 181 if (write_int(BLUE_BLINK_FILE, blink)) 182 write_int(BLUE_LED_FILE, 0); 183 } 184 } else { 185 write_int(RED_LED_FILE, red); 186 write_int(GREEN_LED_FILE, green); 187 write_int(BLUE_LED_FILE, blue); 188 } 189 190 return 0; 191 } 192 193 static void 194 handle_speaker_battery_locked(struct light_device_t* dev) 195 { 196 if (is_lit(&g_battery)) { 197 set_speaker_light_locked(dev, &g_battery); 198 } else { 199 set_speaker_light_locked(dev, &g_notification); 200 } 201 } 202 203 static int 204 set_light_battery(struct light_device_t* dev, 205 struct light_state_t const* state) 206 { 207 pthread_mutex_lock(&g_lock); 208 g_battery = *state; 209 handle_speaker_battery_locked(dev); 210 pthread_mutex_unlock(&g_lock); 211 return 0; 212 } 213 214 static int 215 set_light_notifications(struct light_device_t* dev, 216 struct light_state_t const* state) 217 { 218 pthread_mutex_lock(&g_lock); 219 g_notification = *state; 220 handle_speaker_battery_locked(dev); 221 pthread_mutex_unlock(&g_lock); 222 return 0; 223 } 224 225 static int 226 set_light_attention(struct light_device_t* dev, 227 struct light_state_t const* state) 228 { 229 pthread_mutex_lock(&g_lock); 230 if (state->flashMode == LIGHT_FLASH_HARDWARE) { 231 g_attention = state->flashOnMS; 232 } else if (state->flashMode == LIGHT_FLASH_NONE) { 233 g_attention = 0; 234 } 235 handle_speaker_battery_locked(dev); 236 pthread_mutex_unlock(&g_lock); 237 return 0; 238 } 239 240 static int 241 set_light_buttons(struct light_device_t* dev, 242 struct light_state_t const* state) 243 { 244 int err = 0; 245 if(!dev) { 246 return -1; 247 } 248 pthread_mutex_lock(&g_lock); 249 err = write_int(BUTTON_FILE, state->color & 0xFF); 250 pthread_mutex_unlock(&g_lock); 251 return err; 252 } 253 254 /** Close the lights device */ 255 static int 256 close_lights(struct light_device_t *dev) 257 { 258 if (dev) { 259 free(dev); 260 } 261 return 0; 262 } 263 264 265 /******************************************************************************/ 266 267 /** 268 * module methods 269 */ 270 271 /** Open a new instance of a lights device using name */ 272 static int open_lights(const struct hw_module_t* module, char const* name, 273 struct hw_device_t** device) 274 { 275 int (*set_light)(struct light_device_t* dev, 276 struct light_state_t const* state); 277 278 if (0 == strcmp(LIGHT_ID_BACKLIGHT, name)) 279 set_light = set_light_backlight; 280 else if (0 == strcmp(LIGHT_ID_BATTERY, name)) 281 set_light = set_light_battery; 282 else if (0 == strcmp(LIGHT_ID_NOTIFICATIONS, name)) 283 set_light = set_light_notifications; 284 else if (0 == strcmp(LIGHT_ID_BUTTONS, name)) 285 set_light = set_light_buttons; 286 else if (0 == strcmp(LIGHT_ID_ATTENTION, name)) 287 set_light = set_light_attention; 288 else 289 return -EINVAL; 290 291 pthread_once(&g_init, init_globals); 292 293 struct light_device_t *dev = malloc(sizeof(struct light_device_t)); 294 295 if(!dev) 296 return -ENOMEM; 297 298 memset(dev, 0, sizeof(*dev)); 299 300 dev->common.tag = HARDWARE_DEVICE_TAG; 301 dev->common.version = 0; 302 dev->common.module = (struct hw_module_t*)module; 303 dev->common.close = (int (*)(struct hw_device_t*))close_lights; 304 dev->set_light = set_light; 305 306 *device = (struct hw_device_t*)dev; 307 return 0; 308 } 309 310 static struct hw_module_methods_t lights_module_methods = { 311 .open = open_lights, 312 }; 313 314 /* 315 * The lights Module 316 */ 317 struct hw_module_t HAL_MODULE_INFO_SYM = { 318 .tag = HARDWARE_MODULE_TAG, 319 .version_major = 1, 320 .version_minor = 0, 321 .id = LIGHTS_HARDWARE_MODULE_ID, 322 .name = "lights Module", 323 .author = "Google, Inc.", 324 .methods = &lights_module_methods, 325 }; 326