Christopher N. Hesse | 79a9b15 | 2016-01-26 12:17:55 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
Christopher N. Hesse | ae27148 | 2016-12-01 15:51:12 +0100 | [diff] [blame^] | 3 | * Copyright (C) 2015-2016 The CyanogenMod Project |
Christopher N. Hesse | 79a9b15 | 2016-01-26 12:17:55 +0100 | [diff] [blame] | 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 | #define LOG_TAG "SamsungLightsHAL" |
| 19 | /* #define LOG_NDEBUG 0 */ |
| 20 | |
| 21 | #include <cutils/log.h> |
| 22 | #include <stdlib.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 | |
Christopher N. Hesse | ae27148 | 2016-12-01 15:51:12 +0100 | [diff] [blame^] | 34 | #include "samsung_lights.h" |
Christopher N. Hesse | 79a9b15 | 2016-01-26 12:17:55 +0100 | [diff] [blame] | 35 | |
| 36 | #define COLOR_MASK 0x00ffffff |
| 37 | |
| 38 | static pthread_once_t g_init = PTHREAD_ONCE_INIT; |
| 39 | static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER; |
| 40 | |
| 41 | struct led_config { |
| 42 | unsigned int color; |
| 43 | int delay_on, delay_off; |
| 44 | }; |
| 45 | |
| 46 | static struct led_config g_leds[3]; // For battery, notifications, and attention. |
| 47 | static int g_cur_led = -1; // Presently showing LED of the above. |
| 48 | |
| 49 | void init_g_lock(void) |
| 50 | { |
| 51 | pthread_mutex_init(&g_lock, NULL); |
| 52 | } |
| 53 | |
| 54 | static int write_int(char const *path, int value) |
| 55 | { |
| 56 | int fd; |
| 57 | static int already_warned; |
| 58 | |
| 59 | already_warned = 0; |
| 60 | |
| 61 | ALOGV("write_int: path %s, value %d", path, value); |
| 62 | fd = open(path, O_RDWR); |
| 63 | |
| 64 | if (fd >= 0) { |
| 65 | char buffer[20]; |
| 66 | int bytes = sprintf(buffer, "%d\n", value); |
| 67 | int amt = write(fd, buffer, bytes); |
| 68 | close(fd); |
| 69 | return amt == -1 ? -errno : 0; |
| 70 | } else { |
| 71 | if (already_warned == 0) { |
| 72 | ALOGE("write_int failed to open %s\n", path); |
| 73 | already_warned = 1; |
| 74 | } |
| 75 | return -errno; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | static int write_str(char const *path, const char* value) |
| 80 | { |
| 81 | int fd; |
| 82 | static int already_warned; |
| 83 | |
| 84 | already_warned = 0; |
| 85 | |
| 86 | ALOGV("write_str: path %s, value %s", path, value); |
| 87 | fd = open(path, O_RDWR); |
| 88 | |
| 89 | if (fd >= 0) { |
| 90 | int amt = write(fd, value, strlen(value)); |
| 91 | close(fd); |
| 92 | return amt == -1 ? -errno : 0; |
| 93 | } else { |
| 94 | if (already_warned == 0) { |
| 95 | ALOGE("write_str failed to open %s\n", path); |
| 96 | already_warned = 1; |
| 97 | } |
| 98 | return -errno; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | static int rgb_to_brightness(struct light_state_t const *state) |
| 103 | { |
| 104 | int color = state->color & COLOR_MASK; |
| 105 | |
| 106 | return ((77*((color>>16) & 0x00ff)) |
| 107 | + (150*((color>>8) & 0x00ff)) + (29*(color & 0x00ff))) >> 8; |
| 108 | } |
| 109 | |
| 110 | static int set_light_backlight(struct light_device_t *dev __unused, |
| 111 | struct light_state_t const *state) |
| 112 | { |
| 113 | int err = 0; |
| 114 | int brightness = rgb_to_brightness(state); |
| 115 | |
| 116 | pthread_mutex_lock(&g_lock); |
Christopher N. Hesse | ae27148 | 2016-12-01 15:51:12 +0100 | [diff] [blame^] | 117 | err = write_int(PANEL_BRIGHTNESS_NODE, brightness); |
Christopher N. Hesse | 79a9b15 | 2016-01-26 12:17:55 +0100 | [diff] [blame] | 118 | |
| 119 | pthread_mutex_unlock(&g_lock); |
| 120 | return err; |
| 121 | } |
| 122 | |
| 123 | static int set_light_buttons(struct light_device_t* dev __unused, |
| 124 | struct light_state_t const* state) |
| 125 | { |
| 126 | int err = 0; |
| 127 | int on = (state->color & COLOR_MASK); |
| 128 | |
| 129 | pthread_mutex_lock(&g_lock); |
| 130 | |
Christopher N. Hesse | ae27148 | 2016-12-01 15:51:12 +0100 | [diff] [blame^] | 131 | err = write_int(BUTTON_BRIGHTNESS_NODE, on ? 1 : 0); |
Christopher N. Hesse | 79a9b15 | 2016-01-26 12:17:55 +0100 | [diff] [blame] | 132 | |
| 133 | pthread_mutex_unlock(&g_lock); |
| 134 | |
| 135 | return err; |
| 136 | } |
| 137 | |
| 138 | static int close_lights(struct light_device_t *dev) |
| 139 | { |
| 140 | ALOGV("close_light is called"); |
| 141 | if (dev) |
| 142 | free(dev); |
| 143 | |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | /* LEDs */ |
| 148 | static int write_leds(const struct led_config *led) |
| 149 | { |
| 150 | static const struct led_config led_off = {0, 0, 0}; |
| 151 | |
| 152 | char blink[32]; |
| 153 | int count, err; |
| 154 | int color; |
| 155 | |
| 156 | if (led == NULL) |
| 157 | led = &led_off; |
| 158 | |
| 159 | count = snprintf(blink, |
| 160 | sizeof(blink) - 1, |
| 161 | "0x%08x %d %d", |
| 162 | led->color, |
| 163 | led->delay_on, |
| 164 | led->delay_off); |
| 165 | if (count < 0) { |
| 166 | return -errno; |
| 167 | } else if ((unsigned int)count >= sizeof(blink)-1) { |
| 168 | ALOGE("%s: Truncated string: blink=\"%s\".", __func__, blink); |
| 169 | return -EINVAL; |
| 170 | } |
| 171 | |
| 172 | ALOGV("%s: color=0x%08x, delay_on=%d, delay_off=%d, blink=\"%s\".", |
| 173 | __func__, led->color, led->delay_on, led->delay_off, blink); |
| 174 | |
| 175 | /* Add '\n' here to make the above log message clean. */ |
| 176 | blink[count] = '\n'; |
| 177 | blink[count+1] = '\0'; |
| 178 | |
| 179 | pthread_mutex_lock(&g_lock); |
Christopher N. Hesse | ae27148 | 2016-12-01 15:51:12 +0100 | [diff] [blame^] | 180 | err = write_str(LED_BLINK_NODE, blink); |
Christopher N. Hesse | 79a9b15 | 2016-01-26 12:17:55 +0100 | [diff] [blame] | 181 | pthread_mutex_unlock(&g_lock); |
| 182 | |
| 183 | return err; |
| 184 | } |
| 185 | |
| 186 | static int set_light_leds(struct light_state_t const *state, int type) |
| 187 | { |
| 188 | struct led_config *led; |
| 189 | int err = 0; |
| 190 | |
| 191 | ALOGV("%s: type=%d, color=0x%010x, fM=%d, fOnMS=%d, fOffMs=%d.", __func__, |
| 192 | type, state->color,state->flashMode, state->flashOnMS, state->flashOffMS); |
| 193 | |
| 194 | if (type < 0 || (size_t)type >= sizeof(g_leds)/sizeof(g_leds[0])) { |
| 195 | return -EINVAL; |
| 196 | } |
| 197 | |
| 198 | /* type is one of: |
| 199 | * 0. battery |
| 200 | * 1. notifications |
| 201 | * 2. attention |
| 202 | * which are multiplexed onto the same physical LED in the above order. */ |
| 203 | led = &g_leds[type]; |
| 204 | |
| 205 | switch (state->flashMode) { |
| 206 | case LIGHT_FLASH_NONE: |
| 207 | /* Set LED to a solid color, spec is unclear on the exact behavior here. */ |
| 208 | led->delay_on = led->delay_off = 0; |
| 209 | break; |
| 210 | case LIGHT_FLASH_TIMED: |
| 211 | case LIGHT_FLASH_HARDWARE: |
| 212 | led->delay_on = state->flashOnMS; |
| 213 | led->delay_off = state->flashOffMS; |
| 214 | break; |
| 215 | default: |
| 216 | return -EINVAL; |
| 217 | } |
| 218 | |
| 219 | led->color = state->color & COLOR_MASK; |
| 220 | |
| 221 | if (led->color > 0) { |
| 222 | /* This LED is lit. */ |
| 223 | if (type >= g_cur_led) { |
| 224 | /* And it has the highest priority, so show it. */ |
| 225 | err = write_leds(led); |
| 226 | g_cur_led = type; |
| 227 | } |
| 228 | } else { |
| 229 | /* This LED is not (any longer) lit. */ |
| 230 | if (type == g_cur_led) { |
| 231 | /* But it is currently showing, switch to a lower-priority LED. */ |
| 232 | int i; |
| 233 | |
| 234 | for (i = type-1; i >= 0; i--) { |
| 235 | if (g_leds[i].color > 0) { |
| 236 | /* Found a lower-priority LED to switch to. */ |
| 237 | err = write_leds(&g_leds[i]); |
| 238 | goto switched; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | /* No LEDs are lit, turn off. */ |
| 243 | err = write_leds(NULL); |
| 244 | switched: |
| 245 | g_cur_led = i; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | return err; |
| 250 | } |
| 251 | |
| 252 | static int set_light_leds_battery(struct light_device_t *dev __unused, |
| 253 | struct light_state_t const *state) |
| 254 | { |
| 255 | return set_light_leds(state, 0); |
| 256 | } |
| 257 | |
| 258 | static int set_light_leds_notifications(struct light_device_t *dev __unused, |
| 259 | struct light_state_t const *state) |
| 260 | { |
| 261 | return set_light_leds(state, 1); |
| 262 | } |
| 263 | |
| 264 | static int set_light_leds_attention(struct light_device_t *dev __unused, |
| 265 | struct light_state_t const *state) |
| 266 | { |
| 267 | struct light_state_t fixed; |
| 268 | |
| 269 | memcpy(&fixed, state, sizeof(fixed)); |
| 270 | |
| 271 | /* The framework does odd things with the attention lights, fix them up to |
| 272 | * do something sensible here. */ |
| 273 | switch (fixed.flashMode) { |
| 274 | case LIGHT_FLASH_NONE: |
| 275 | /* LightsService.Light::stopFlashing calls with non-zero color. */ |
| 276 | fixed.color = 0; |
| 277 | break; |
| 278 | case LIGHT_FLASH_HARDWARE: |
| 279 | /* PowerManagerService::setAttentionLight calls with onMS=3, offMS=0, which |
| 280 | * just makes for a slightly-dimmer LED. */ |
| 281 | if (fixed.flashOnMS > 0 && fixed.flashOffMS == 0) |
| 282 | fixed.flashMode = LIGHT_FLASH_NONE; |
| 283 | fixed.color = 0x000000ff; |
| 284 | break; |
| 285 | } |
| 286 | |
| 287 | return set_light_leds(&fixed, 2); |
| 288 | } |
| 289 | |
| 290 | static int open_lights(const struct hw_module_t *module, char const *name, |
| 291 | struct hw_device_t **device) |
| 292 | { |
| 293 | int (*set_light)(struct light_device_t *dev, |
| 294 | struct light_state_t const *state); |
| 295 | |
| 296 | if (0 == strcmp(LIGHT_ID_BACKLIGHT, name)) |
| 297 | set_light = set_light_backlight; |
| 298 | else if (0 == strcmp(LIGHT_ID_BUTTONS, name)) |
| 299 | set_light = set_light_buttons; |
| 300 | else if (0 == strcmp(LIGHT_ID_BATTERY, name)) |
| 301 | set_light = set_light_leds_battery; |
| 302 | else if (0 == strcmp(LIGHT_ID_NOTIFICATIONS, name)) |
| 303 | set_light = set_light_leds_notifications; |
| 304 | else if (0 == strcmp(LIGHT_ID_ATTENTION, name)) |
| 305 | set_light = set_light_leds_attention; |
| 306 | else |
| 307 | return -EINVAL; |
| 308 | |
| 309 | pthread_once(&g_init, init_g_lock); |
| 310 | |
| 311 | struct light_device_t *dev = malloc(sizeof(struct light_device_t)); |
| 312 | memset(dev, 0, sizeof(*dev)); |
| 313 | |
| 314 | dev->common.tag = HARDWARE_DEVICE_TAG; |
| 315 | dev->common.version = 0; |
| 316 | dev->common.module = (struct hw_module_t *)module; |
| 317 | dev->common.close = (int (*)(struct hw_device_t *))close_lights; |
| 318 | dev->set_light = set_light; |
| 319 | |
| 320 | *device = (struct hw_device_t *)dev; |
| 321 | |
| 322 | return 0; |
| 323 | } |
| 324 | |
| 325 | static struct hw_module_methods_t lights_module_methods = { |
| 326 | .open = open_lights, |
| 327 | }; |
| 328 | |
| 329 | struct hw_module_t HAL_MODULE_INFO_SYM = { |
| 330 | .tag = HARDWARE_MODULE_TAG, |
| 331 | .version_major = 1, |
| 332 | .version_minor = 0, |
| 333 | .id = LIGHTS_HARDWARE_MODULE_ID, |
| 334 | .name = "Samsung Lights Module", |
| 335 | .author = "The CyanogenMod Project", |
| 336 | .methods = &lights_module_methods, |
| 337 | }; |