Sungmin Choi | b514889 | 2012-07-02 17:00:07 -0700 | [diff] [blame] | 1 | /* |
Ch Ganesh Kumar | 71f50f3 | 2018-03-14 16:51:43 +0530 | [diff] [blame] | 2 | * Copyright (C) 2014, 2017-2018 The Linux Foundation. All rights reserved. |
Sathish Ambley | 4342f27 | 2017-01-04 10:57:05 -0800 | [diff] [blame] | 3 | * Not a contribution |
Sungmin Choi | b514889 | 2012-07-02 17:00:07 -0700 | [diff] [blame] | 4 | * Copyright (C) 2008 The Android Open Source Project |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | |
| 20 | // #define LOG_NDEBUG 0 |
Sungmin Choi | b514889 | 2012-07-02 17:00:07 -0700 | [diff] [blame] | 21 | |
Naseer Ahmed | 79dc3b0 | 2018-03-06 20:41:14 -0500 | [diff] [blame] | 22 | #include <log/log.h> |
Xu Yang | 586c6d5 | 2016-09-19 17:54:16 +0800 | [diff] [blame] | 23 | #include <cutils/properties.h> |
Sungmin Choi | b514889 | 2012-07-02 17:00:07 -0700 | [diff] [blame] | 24 | #include <stdint.h> |
Arun Kumar K.R | 6203161 | 2015-07-30 11:38:19 -0700 | [diff] [blame] | 25 | #include <stdlib.h> |
Sungmin Choi | b514889 | 2012-07-02 17:00:07 -0700 | [diff] [blame] | 26 | #include <string.h> |
| 27 | #include <unistd.h> |
| 28 | #include <errno.h> |
| 29 | #include <fcntl.h> |
| 30 | #include <pthread.h> |
| 31 | |
| 32 | #include <sys/ioctl.h> |
| 33 | #include <sys/types.h> |
| 34 | |
| 35 | #include <hardware/lights.h> |
| 36 | |
Sathish Ambley | 4342f27 | 2017-01-04 10:57:05 -0800 | [diff] [blame] | 37 | #ifndef DEFAULT_LOW_PERSISTENCE_MODE_BRIGHTNESS |
| 38 | #define DEFAULT_LOW_PERSISTENCE_MODE_BRIGHTNESS 0x80 |
| 39 | #endif |
| 40 | |
yutingshih | 0e7c4bb | 2020-04-21 15:56:11 +0800 | [diff] [blame] | 41 | #if defined( ARIMA_DEBUG ) |
| 42 | #undef ARIMA_DEBUG |
| 43 | #endif |
| 44 | #define ARIMA_DEBUG 0 |
| 45 | |
| 46 | #if defined( PNX ) |
| 47 | #undef PNX |
| 48 | #endif |
| 49 | #define PNX "[ARIMA]" |
| 50 | |
| 51 | #if defined( LIGHT_DBG ) |
| 52 | #undef LIGHT_DBG |
| 53 | #endif |
| 54 | |
| 55 | #if( ARIMA_DEBUG ) |
| 56 | #define LIGHT_DBG(fmt,args...) ALOGE(PNX "[%s] " fmt,__FUNCTION__,##args) |
| 57 | #else |
| 58 | #define LIGHT_DBG(fmt,args...) do{}while(0) |
| 59 | #endif |
| 60 | |
Sungmin Choi | b514889 | 2012-07-02 17:00:07 -0700 | [diff] [blame] | 61 | /******************************************************************************/ |
| 62 | |
| 63 | static pthread_once_t g_init = PTHREAD_ONCE_INIT; |
| 64 | static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER; |
| 65 | static struct light_state_t g_notification; |
| 66 | static struct light_state_t g_battery; |
Sathish Ambley | 4342f27 | 2017-01-04 10:57:05 -0800 | [diff] [blame] | 67 | static int g_last_backlight_mode = BRIGHTNESS_MODE_USER; |
Sungmin Choi | b514889 | 2012-07-02 17:00:07 -0700 | [diff] [blame] | 68 | static int g_attention = 0; |
| 69 | |
| 70 | char const*const RED_LED_FILE |
| 71 | = "/sys/class/leds/red/brightness"; |
| 72 | |
| 73 | char const*const GREEN_LED_FILE |
| 74 | = "/sys/class/leds/green/brightness"; |
| 75 | |
| 76 | char const*const BLUE_LED_FILE |
| 77 | = "/sys/class/leds/blue/brightness"; |
| 78 | |
| 79 | char const*const LCD_FILE |
| 80 | = "/sys/class/leds/lcd-backlight/brightness"; |
| 81 | |
Saurabh Shah | 8b021cf | 2017-03-14 12:16:43 -0700 | [diff] [blame] | 82 | char const*const LCD_FILE2 |
| 83 | = "/sys/class/backlight/panel0-backlight/brightness"; |
| 84 | |
HuiWang | f73bc57 | 2013-07-31 10:48:36 +0800 | [diff] [blame] | 85 | char const*const BUTTON_FILE |
| 86 | = "/sys/class/leds/button-backlight/brightness"; |
| 87 | |
yutingshih | 0e7c4bb | 2020-04-21 15:56:11 +0800 | [diff] [blame] | 88 | #if( 1 ) |
| 89 | |
| 90 | char const*const RED_BLINK_FILE |
| 91 | = "/sys/class/leds/red/breath"; |
| 92 | |
| 93 | char const*const GREEN_BLINK_FILE |
| 94 | = "/sys/class/leds/green/breath"; |
| 95 | |
| 96 | char const*const BLUE_BLINK_FILE |
| 97 | = "/sys/class/leds/blue/breath"; |
| 98 | |
| 99 | #else /* Default */ |
samin.ryu | d57c7d5 | 2012-08-03 23:59:41 +0900 | [diff] [blame] | 100 | char const*const RED_BLINK_FILE |
Yulian Shandorov | 784d739 | 2013-06-20 04:28:59 -0700 | [diff] [blame] | 101 | = "/sys/class/leds/red/blink"; |
samin.ryu | d57c7d5 | 2012-08-03 23:59:41 +0900 | [diff] [blame] | 102 | |
Ameya Thakur | 192c8ac | 2013-08-12 16:50:01 -0700 | [diff] [blame] | 103 | char const*const GREEN_BLINK_FILE |
| 104 | = "/sys/class/leds/green/blink"; |
| 105 | |
| 106 | char const*const BLUE_BLINK_FILE |
| 107 | = "/sys/class/leds/blue/blink"; |
| 108 | |
yutingshih | 0e7c4bb | 2020-04-21 15:56:11 +0800 | [diff] [blame] | 109 | #endif /* End.. */ |
| 110 | |
| 111 | #if defined( LED_DELAY_FUNC_USAGE ) |
| 112 | #undef LED_DELAY_FUNC_USAGE |
| 113 | #endif |
| 114 | #define LED_DELAY_FUNC_USAGE 0 |
| 115 | |
| 116 | #if( LED_DELAY_FUNC_USAGE ) |
| 117 | char const*const RED_DELAY_ON_FILE |
| 118 | = "/sys/class/leds/red/delay_on"; |
| 119 | |
| 120 | char const*const RED_DELAY_OFF_FILE |
| 121 | = "/sys/class/leds/red/delay_off"; |
| 122 | |
| 123 | char const*const GREEN_DELAY_ON_FILE |
| 124 | = "/sys/class/leds/green/delay_on"; |
| 125 | |
| 126 | char const*const GREEN_DELAY_OFF_FILE |
| 127 | = "/sys/class/leds/green/delay_off"; |
| 128 | |
| 129 | char const*const BLUE_DELAY_ON_FILE |
| 130 | = "/sys/class/leds/blue/delay_on"; |
| 131 | |
| 132 | char const*const BLUE_DELAY_OFF_FILE |
| 133 | = "/sys/class/leds/blue/delay_off"; |
| 134 | |
| 135 | #define LED_DELAY_MAX_MS 1500 |
| 136 | |
| 137 | #endif /* End..(LED_DELAY_FUNC_USAGE) */ |
| 138 | |
| 139 | |
Sathish Ambley | 4342f27 | 2017-01-04 10:57:05 -0800 | [diff] [blame] | 140 | char const*const PERSISTENCE_FILE |
| 141 | = "/sys/class/graphics/fb0/msm_fb_persist_mode"; |
| 142 | |
Sungmin Choi | b514889 | 2012-07-02 17:00:07 -0700 | [diff] [blame] | 143 | /** |
| 144 | * device methods |
| 145 | */ |
| 146 | |
| 147 | void init_globals(void) |
| 148 | { |
| 149 | // init the mutex |
| 150 | pthread_mutex_init(&g_lock, NULL); |
| 151 | } |
| 152 | |
| 153 | static int |
| 154 | write_int(char const* path, int value) |
| 155 | { |
| 156 | int fd; |
| 157 | static int already_warned = 0; |
| 158 | |
| 159 | fd = open(path, O_RDWR); |
| 160 | if (fd >= 0) { |
| 161 | char buffer[20]; |
Manoj Kumar AVM | 001b309 | 2014-04-29 22:08:51 -0700 | [diff] [blame] | 162 | int bytes = snprintf(buffer, sizeof(buffer), "%d\n", value); |
Dileep Kumar Reddi | bf333c7 | 2014-02-25 14:32:51 +0530 | [diff] [blame] | 163 | ssize_t amt = write(fd, buffer, (size_t)bytes); |
Sungmin Choi | b514889 | 2012-07-02 17:00:07 -0700 | [diff] [blame] | 164 | close(fd); |
| 165 | return amt == -1 ? -errno : 0; |
| 166 | } else { |
| 167 | if (already_warned == 0) { |
| 168 | ALOGE("write_int failed to open %s\n", path); |
| 169 | already_warned = 1; |
| 170 | } |
| 171 | return -errno; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | static int |
| 176 | is_lit(struct light_state_t const* state) |
| 177 | { |
| 178 | return state->color & 0x00ffffff; |
| 179 | } |
| 180 | |
| 181 | static int |
| 182 | rgb_to_brightness(struct light_state_t const* state) |
| 183 | { |
| 184 | int color = state->color & 0x00ffffff; |
| 185 | return ((77*((color>>16)&0x00ff)) |
| 186 | + (150*((color>>8)&0x00ff)) + (29*(color&0x00ff))) >> 8; |
| 187 | } |
| 188 | |
| 189 | static int |
| 190 | set_light_backlight(struct light_device_t* dev, |
| 191 | struct light_state_t const* state) |
| 192 | { |
| 193 | int err = 0; |
| 194 | int brightness = rgb_to_brightness(state); |
Sathish Ambley | 4342f27 | 2017-01-04 10:57:05 -0800 | [diff] [blame] | 195 | unsigned int lpEnabled = |
| 196 | state->brightnessMode == BRIGHTNESS_MODE_LOW_PERSISTENCE; |
Arun Kumar K.R | 0efad60 | 2014-01-21 21:32:36 -0800 | [diff] [blame] | 197 | if(!dev) { |
| 198 | return -1; |
| 199 | } |
Sathish Ambley | 4342f27 | 2017-01-04 10:57:05 -0800 | [diff] [blame] | 200 | |
Sungmin Choi | b514889 | 2012-07-02 17:00:07 -0700 | [diff] [blame] | 201 | pthread_mutex_lock(&g_lock); |
Sathish Ambley | 4342f27 | 2017-01-04 10:57:05 -0800 | [diff] [blame] | 202 | // Toggle low persistence mode state |
| 203 | if ((g_last_backlight_mode != state->brightnessMode && lpEnabled) || |
| 204 | (!lpEnabled && |
| 205 | g_last_backlight_mode == BRIGHTNESS_MODE_LOW_PERSISTENCE)) { |
| 206 | if ((err = write_int(PERSISTENCE_FILE, lpEnabled)) != 0) { |
| 207 | ALOGE("%s: Failed to write to %s: %s\n", __FUNCTION__, |
| 208 | PERSISTENCE_FILE, strerror(errno)); |
| 209 | } |
| 210 | if (lpEnabled != 0) { |
| 211 | brightness = DEFAULT_LOW_PERSISTENCE_MODE_BRIGHTNESS; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | g_last_backlight_mode = state->brightnessMode; |
| 216 | |
| 217 | if (!err) { |
Saurabh Shah | 8b021cf | 2017-03-14 12:16:43 -0700 | [diff] [blame] | 218 | if (!access(LCD_FILE, F_OK)) { |
| 219 | err = write_int(LCD_FILE, brightness); |
| 220 | } else { |
| 221 | err = write_int(LCD_FILE2, brightness); |
| 222 | } |
Sathish Ambley | 4342f27 | 2017-01-04 10:57:05 -0800 | [diff] [blame] | 223 | } |
| 224 | |
Sungmin Choi | b514889 | 2012-07-02 17:00:07 -0700 | [diff] [blame] | 225 | pthread_mutex_unlock(&g_lock); |
| 226 | return err; |
| 227 | } |
| 228 | |
| 229 | static int |
| 230 | set_speaker_light_locked(struct light_device_t* dev, |
| 231 | struct light_state_t const* state) |
| 232 | { |
Arun Kumar K.R | 0efad60 | 2014-01-21 21:32:36 -0800 | [diff] [blame] | 233 | int red, green, blue; |
Yulian Shandorov | 784d739 | 2013-06-20 04:28:59 -0700 | [diff] [blame] | 234 | int blink; |
Sungmin Choi | b514889 | 2012-07-02 17:00:07 -0700 | [diff] [blame] | 235 | int onMS, offMS; |
| 236 | unsigned int colorRGB; |
| 237 | |
Arun Kumar K.R | 0efad60 | 2014-01-21 21:32:36 -0800 | [diff] [blame] | 238 | if(!dev) { |
| 239 | return -1; |
| 240 | } |
| 241 | |
Sungmin Choi | b514889 | 2012-07-02 17:00:07 -0700 | [diff] [blame] | 242 | switch (state->flashMode) { |
| 243 | case LIGHT_FLASH_TIMED: |
| 244 | onMS = state->flashOnMS; |
| 245 | offMS = state->flashOffMS; |
| 246 | break; |
| 247 | case LIGHT_FLASH_NONE: |
| 248 | default: |
| 249 | onMS = 0; |
| 250 | offMS = 0; |
| 251 | break; |
| 252 | } |
| 253 | |
| 254 | colorRGB = state->color; |
| 255 | |
| 256 | #if 0 |
samin.ryu | d57c7d5 | 2012-08-03 23:59:41 +0900 | [diff] [blame] | 257 | ALOGD("set_speaker_light_locked mode %d, colorRGB=%08X, onMS=%d, offMS=%d\n", |
| 258 | state->flashMode, colorRGB, onMS, offMS); |
Sungmin Choi | b514889 | 2012-07-02 17:00:07 -0700 | [diff] [blame] | 259 | #endif |
| 260 | |
| 261 | red = (colorRGB >> 16) & 0xFF; |
| 262 | green = (colorRGB >> 8) & 0xFF; |
| 263 | blue = colorRGB & 0xFF; |
| 264 | |
Sungmin Choi | b514889 | 2012-07-02 17:00:07 -0700 | [diff] [blame] | 265 | if (onMS > 0 && offMS > 0) { |
Ashay Jaiswal | 26f0b53 | 2015-05-03 17:58:58 +0530 | [diff] [blame] | 266 | /* |
| 267 | * if ON time == OFF time |
| 268 | * use blink mode 2 |
| 269 | * else |
| 270 | * use blink mode 1 |
| 271 | */ |
| 272 | if (onMS == offMS) |
| 273 | blink = 2; |
| 274 | else |
| 275 | blink = 1; |
Sungmin Choi | b514889 | 2012-07-02 17:00:07 -0700 | [diff] [blame] | 276 | } else { |
| 277 | blink = 0; |
Sungmin Choi | b514889 | 2012-07-02 17:00:07 -0700 | [diff] [blame] | 278 | } |
| 279 | |
yutingshih | 0e7c4bb | 2020-04-21 15:56:11 +0800 | [diff] [blame] | 280 | LIGHT_DBG("red=%02X, green=%02X, blue=%02X, blink=%d\n", red, green, blue, blink ); |
| 281 | |
Sungmin Choi | b514889 | 2012-07-02 17:00:07 -0700 | [diff] [blame] | 282 | if (blink) { |
yutingshih | 0e7c4bb | 2020-04-21 15:56:11 +0800 | [diff] [blame] | 283 | write_int( RED_BLINK_FILE, 0 ); |
| 284 | write_int( GREEN_BLINK_FILE, 0 ); |
| 285 | write_int( BLUE_BLINK_FILE, 0 ); |
| 286 | usleep( 6000 ); /* Delay 6ms for off effective and complete */ |
| 287 | |
Mao Li | 728ee0b | 2014-07-01 23:06:16 +0800 | [diff] [blame] | 288 | if (red) { |
| 289 | if (write_int(RED_BLINK_FILE, blink)) |
| 290 | write_int(RED_LED_FILE, 0); |
Ch Ganesh Kumar | 71f50f3 | 2018-03-14 16:51:43 +0530 | [diff] [blame] | 291 | } |
yutingshih | 0e7c4bb | 2020-04-21 15:56:11 +0800 | [diff] [blame] | 292 | else |
| 293 | { |
| 294 | write_int( RED_LED_FILE, 0 ); |
| 295 | } |
| 296 | |
Mao Li | 728ee0b | 2014-07-01 23:06:16 +0800 | [diff] [blame] | 297 | if (green) { |
| 298 | if (write_int(GREEN_BLINK_FILE, blink)) |
| 299 | write_int(GREEN_LED_FILE, 0); |
Ch Ganesh Kumar | 71f50f3 | 2018-03-14 16:51:43 +0530 | [diff] [blame] | 300 | } |
yutingshih | 0e7c4bb | 2020-04-21 15:56:11 +0800 | [diff] [blame] | 301 | else |
| 302 | { |
| 303 | write_int( GREEN_LED_FILE, 0 ); |
| 304 | } |
| 305 | |
Mao Li | 728ee0b | 2014-07-01 23:06:16 +0800 | [diff] [blame] | 306 | if (blue) { |
| 307 | if (write_int(BLUE_BLINK_FILE, blink)) |
| 308 | write_int(BLUE_LED_FILE, 0); |
Ch Ganesh Kumar | 71f50f3 | 2018-03-14 16:51:43 +0530 | [diff] [blame] | 309 | } |
yutingshih | 0e7c4bb | 2020-04-21 15:56:11 +0800 | [diff] [blame] | 310 | else |
| 311 | { |
| 312 | write_int( BLUE_LED_FILE, 0 ); |
| 313 | } |
Yulian Shandorov | 784d739 | 2013-06-20 04:28:59 -0700 | [diff] [blame] | 314 | } else { |
yutingshih | 0e7c4bb | 2020-04-21 15:56:11 +0800 | [diff] [blame] | 315 | write_int( RED_LED_FILE, 0 ); |
| 316 | write_int( GREEN_LED_FILE, 0 ); |
| 317 | write_int( BLUE_LED_FILE, 0 ); |
| 318 | usleep( 6000 ); /* Delay 6ms for off effective and complete */ |
| 319 | |
Yulian Shandorov | 784d739 | 2013-06-20 04:28:59 -0700 | [diff] [blame] | 320 | write_int(RED_LED_FILE, red); |
| 321 | write_int(GREEN_LED_FILE, green); |
| 322 | write_int(BLUE_LED_FILE, blue); |
Sungmin Choi | b514889 | 2012-07-02 17:00:07 -0700 | [diff] [blame] | 323 | } |
yutingshih | 0e7c4bb | 2020-04-21 15:56:11 +0800 | [diff] [blame] | 324 | usleep( 5000 ); /* 5ms */ |
Sungmin Choi | b514889 | 2012-07-02 17:00:07 -0700 | [diff] [blame] | 325 | |
| 326 | return 0; |
| 327 | } |
| 328 | |
yutingshih | 0e7c4bb | 2020-04-21 15:56:11 +0800 | [diff] [blame] | 329 | #if defined( LED_FIRST_NOTIFICATION ) |
| 330 | #undef LED_FIRST_NOTIFICATION |
| 331 | #endif |
| 332 | #define LED_FIRST_NOTIFICATION 1 |
| 333 | |
Sungmin Choi | b514889 | 2012-07-02 17:00:07 -0700 | [diff] [blame] | 334 | static void |
| 335 | handle_speaker_battery_locked(struct light_device_t* dev) |
| 336 | { |
yutingshih | 0e7c4bb | 2020-04-21 15:56:11 +0800 | [diff] [blame] | 337 | #if( LED_FIRST_NOTIFICATION ) |
| 338 | if( is_lit( &g_notification )) |
| 339 | { |
| 340 | LIGHT_DBG("Notification\n"); |
| 341 | set_speaker_light_locked( dev, &g_notification ); |
| 342 | } |
| 343 | else |
| 344 | { |
| 345 | LIGHT_DBG("Battery\n"); |
| 346 | set_speaker_light_locked( dev, &g_battery ); |
| 347 | } |
| 348 | #else /* Default */ |
Sungmin Choi | b514889 | 2012-07-02 17:00:07 -0700 | [diff] [blame] | 349 | if (is_lit(&g_battery)) { |
| 350 | set_speaker_light_locked(dev, &g_battery); |
| 351 | } else { |
| 352 | set_speaker_light_locked(dev, &g_notification); |
| 353 | } |
yutingshih | 0e7c4bb | 2020-04-21 15:56:11 +0800 | [diff] [blame] | 354 | #endif |
Sungmin Choi | b514889 | 2012-07-02 17:00:07 -0700 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | static int |
Mao Li | a4e053a | 2014-06-16 23:05:17 +0530 | [diff] [blame] | 358 | set_light_battery(struct light_device_t* dev, |
| 359 | struct light_state_t const* state) |
| 360 | { |
yutingshih | 0e7c4bb | 2020-04-21 15:56:11 +0800 | [diff] [blame] | 361 | LIGHT_DBG("Enter...\n"); |
Mao Li | a4e053a | 2014-06-16 23:05:17 +0530 | [diff] [blame] | 362 | pthread_mutex_lock(&g_lock); |
| 363 | g_battery = *state; |
| 364 | handle_speaker_battery_locked(dev); |
| 365 | pthread_mutex_unlock(&g_lock); |
| 366 | return 0; |
| 367 | } |
| 368 | |
| 369 | static int |
Sungmin Choi | b514889 | 2012-07-02 17:00:07 -0700 | [diff] [blame] | 370 | set_light_notifications(struct light_device_t* dev, |
| 371 | struct light_state_t const* state) |
| 372 | { |
yutingshih | 0e7c4bb | 2020-04-21 15:56:11 +0800 | [diff] [blame] | 373 | LIGHT_DBG("Enter...\n"); |
Sungmin Choi | b514889 | 2012-07-02 17:00:07 -0700 | [diff] [blame] | 374 | pthread_mutex_lock(&g_lock); |
| 375 | g_notification = *state; |
| 376 | handle_speaker_battery_locked(dev); |
| 377 | pthread_mutex_unlock(&g_lock); |
| 378 | return 0; |
| 379 | } |
| 380 | |
| 381 | static int |
| 382 | set_light_attention(struct light_device_t* dev, |
| 383 | struct light_state_t const* state) |
| 384 | { |
| 385 | pthread_mutex_lock(&g_lock); |
| 386 | if (state->flashMode == LIGHT_FLASH_HARDWARE) { |
| 387 | g_attention = state->flashOnMS; |
| 388 | } else if (state->flashMode == LIGHT_FLASH_NONE) { |
| 389 | g_attention = 0; |
| 390 | } |
| 391 | handle_speaker_battery_locked(dev); |
| 392 | pthread_mutex_unlock(&g_lock); |
| 393 | return 0; |
| 394 | } |
| 395 | |
HuiWang | f73bc57 | 2013-07-31 10:48:36 +0800 | [diff] [blame] | 396 | static int |
| 397 | set_light_buttons(struct light_device_t* dev, |
| 398 | struct light_state_t const* state) |
| 399 | { |
| 400 | int err = 0; |
Arun Kumar K.R | 0efad60 | 2014-01-21 21:32:36 -0800 | [diff] [blame] | 401 | if(!dev) { |
| 402 | return -1; |
| 403 | } |
HuiWang | f73bc57 | 2013-07-31 10:48:36 +0800 | [diff] [blame] | 404 | pthread_mutex_lock(&g_lock); |
| 405 | err = write_int(BUTTON_FILE, state->color & 0xFF); |
| 406 | pthread_mutex_unlock(&g_lock); |
| 407 | return err; |
| 408 | } |
Sungmin Choi | b514889 | 2012-07-02 17:00:07 -0700 | [diff] [blame] | 409 | |
| 410 | /** Close the lights device */ |
| 411 | static int |
| 412 | close_lights(struct light_device_t *dev) |
| 413 | { |
| 414 | if (dev) { |
| 415 | free(dev); |
| 416 | } |
| 417 | return 0; |
| 418 | } |
| 419 | |
| 420 | |
| 421 | /******************************************************************************/ |
| 422 | |
| 423 | /** |
| 424 | * module methods |
| 425 | */ |
| 426 | |
| 427 | /** Open a new instance of a lights device using name */ |
| 428 | static int open_lights(const struct hw_module_t* module, char const* name, |
| 429 | struct hw_device_t** device) |
| 430 | { |
| 431 | int (*set_light)(struct light_device_t* dev, |
| 432 | struct light_state_t const* state); |
| 433 | |
yutingshih | 0e7c4bb | 2020-04-21 15:56:11 +0800 | [diff] [blame] | 434 | LIGHT_DBG("Enter...\n"); |
Xu Yang | 586c6d5 | 2016-09-19 17:54:16 +0800 | [diff] [blame] | 435 | if (0 == strcmp(LIGHT_ID_BACKLIGHT, name)) { |
Ch Ganesh Kumar | 71f50f3 | 2018-03-14 16:51:43 +0530 | [diff] [blame] | 436 | set_light = set_light_backlight; |
Xu Yang | 586c6d5 | 2016-09-19 17:54:16 +0800 | [diff] [blame] | 437 | } else if (0 == strcmp(LIGHT_ID_BATTERY, name)) |
Mao Li | a4e053a | 2014-06-16 23:05:17 +0530 | [diff] [blame] | 438 | set_light = set_light_battery; |
Sungmin Choi | b514889 | 2012-07-02 17:00:07 -0700 | [diff] [blame] | 439 | else if (0 == strcmp(LIGHT_ID_NOTIFICATIONS, name)) |
| 440 | set_light = set_light_notifications; |
Arun Kumar K.R | 9e35f2b | 2017-08-23 13:46:03 +0530 | [diff] [blame] | 441 | else if (0 == strcmp(LIGHT_ID_BUTTONS, name)) { |
| 442 | if (!access(BUTTON_FILE, F_OK)) { |
| 443 | // enable light button when the file is present |
| 444 | set_light = set_light_buttons; |
| 445 | } else { |
| 446 | return -EINVAL; |
| 447 | } |
| 448 | } |
Sungmin Choi | b514889 | 2012-07-02 17:00:07 -0700 | [diff] [blame] | 449 | else if (0 == strcmp(LIGHT_ID_ATTENTION, name)) |
| 450 | set_light = set_light_attention; |
| 451 | else |
| 452 | return -EINVAL; |
| 453 | |
| 454 | pthread_once(&g_init, init_globals); |
| 455 | |
| 456 | struct light_device_t *dev = malloc(sizeof(struct light_device_t)); |
Manoj Kumar AVM | 001b309 | 2014-04-29 22:08:51 -0700 | [diff] [blame] | 457 | |
| 458 | if(!dev) |
| 459 | return -ENOMEM; |
| 460 | |
Sungmin Choi | b514889 | 2012-07-02 17:00:07 -0700 | [diff] [blame] | 461 | memset(dev, 0, sizeof(*dev)); |
| 462 | |
| 463 | dev->common.tag = HARDWARE_DEVICE_TAG; |
Sathish Ambley | 4342f27 | 2017-01-04 10:57:05 -0800 | [diff] [blame] | 464 | dev->common.version = LIGHTS_DEVICE_API_VERSION_2_0; |
Sungmin Choi | b514889 | 2012-07-02 17:00:07 -0700 | [diff] [blame] | 465 | dev->common.module = (struct hw_module_t*)module; |
| 466 | dev->common.close = (int (*)(struct hw_device_t*))close_lights; |
| 467 | dev->set_light = set_light; |
| 468 | |
| 469 | *device = (struct hw_device_t*)dev; |
| 470 | return 0; |
| 471 | } |
| 472 | |
| 473 | static struct hw_module_methods_t lights_module_methods = { |
| 474 | .open = open_lights, |
| 475 | }; |
| 476 | |
| 477 | /* |
| 478 | * The lights Module |
| 479 | */ |
| 480 | struct hw_module_t HAL_MODULE_INFO_SYM = { |
| 481 | .tag = HARDWARE_MODULE_TAG, |
| 482 | .version_major = 1, |
| 483 | .version_minor = 0, |
| 484 | .id = LIGHTS_HARDWARE_MODULE_ID, |
| 485 | .name = "lights Module", |
| 486 | .author = "Google, Inc.", |
| 487 | .methods = &lights_module_methods, |
| 488 | }; |