Amit Pundir | 4c517ea | 2021-05-23 02:24:19 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014, 2017-2018 The Linux Foundation. All rights reserved. |
| 3 | * Not a contribution |
| 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 |
| 21 | |
| 22 | #include <log/log.h> |
| 23 | #include <cutils/properties.h> |
| 24 | |
Caleb Connolly | b0addf4 | 2021-08-13 16:51:20 +0100 | [diff] [blame] | 25 | #include <dirent.h> |
| 26 | #include <fcntl.h> |
Amit Pundir | 4c517ea | 2021-05-23 02:24:19 +0530 | [diff] [blame] | 27 | #include <stdint.h> |
| 28 | #include <stdlib.h> |
Caleb Connolly | b0addf4 | 2021-08-13 16:51:20 +0100 | [diff] [blame] | 29 | #include <stdio.h> |
Amit Pundir | 4c517ea | 2021-05-23 02:24:19 +0530 | [diff] [blame] | 30 | #include <string.h> |
| 31 | #include <unistd.h> |
| 32 | #include <errno.h> |
| 33 | #include <fcntl.h> |
| 34 | #include <pthread.h> |
| 35 | |
| 36 | #include <sys/ioctl.h> |
| 37 | #include <sys/types.h> |
| 38 | |
| 39 | #include <hardware/lights.h> |
| 40 | |
| 41 | #define CG_COLOR_ID_PROPERTY "ro.boot.hardware.color" |
| 42 | |
| 43 | /******************************************************************************/ |
| 44 | static pthread_once_t g_init = PTHREAD_ONCE_INIT; |
| 45 | static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER; |
| 46 | static int rgb_brightness_ratio = 255; |
| 47 | |
Caleb Connolly | b0addf4 | 2021-08-13 16:51:20 +0100 | [diff] [blame] | 48 | char const*const BACKLIGHT_CLASS |
| 49 | = "/sys/class/backlight"; |
| 50 | char const*const BACKLIGHT_CLASS_FSTRING |
| 51 | = "/sys/class/backlight/%s/brightness"; |
Amit Pundir | 4c517ea | 2021-05-23 02:24:19 +0530 | [diff] [blame] | 52 | |
| 53 | /** |
| 54 | * device methods |
| 55 | */ |
| 56 | |
| 57 | void init_globals(void) |
| 58 | { |
| 59 | char color_id_prop[PROPERTY_VALUE_MAX] = {""}; |
| 60 | |
| 61 | // init the mutex |
| 62 | pthread_mutex_init(&g_lock, NULL); |
| 63 | |
| 64 | // check CG color |
| 65 | property_get(CG_COLOR_ID_PROPERTY, color_id_prop, "DEF00"); |
| 66 | if (strcmp(color_id_prop, "GRA00") == 0) { |
| 67 | rgb_brightness_ratio = 25; |
| 68 | } else if (strcmp(color_id_prop, "SLV00") == 0) { |
| 69 | rgb_brightness_ratio = 15; |
| 70 | } else if (strcmp(color_id_prop, "BLU00") == 0) { |
| 71 | rgb_brightness_ratio = 15; |
| 72 | } else { |
| 73 | rgb_brightness_ratio = 20; |
| 74 | } |
| 75 | } |
| 76 | |
Caleb Connolly | b0addf4 | 2021-08-13 16:51:20 +0100 | [diff] [blame] | 77 | static bool findFirstBacklightDevice(char *filename) { |
| 78 | struct dirent *de; |
| 79 | DIR *dir = opendir("/sys/class/backlight"); |
| 80 | if (dir == NULL) |
| 81 | return NULL; |
| 82 | while((de = readdir(dir))) { |
| 83 | if(de->d_name[0] == '.' && |
| 84 | (de->d_name[1] == '\0' || |
| 85 | (de->d_name[1] == '.' && de->d_name[2] == '\0'))) |
| 86 | continue; |
| 87 | strcpy(filename, de->d_name); |
| 88 | closedir(dir); |
| 89 | return true; |
| 90 | } |
| 91 | closedir(dir); |
| 92 | return false; |
| 93 | } |
| 94 | |
Amit Pundir | 4c517ea | 2021-05-23 02:24:19 +0530 | [diff] [blame] | 95 | static int |
Caleb Connolly | b0addf4 | 2021-08-13 16:51:20 +0100 | [diff] [blame] | 96 | write_brightness(int value) |
Amit Pundir | 4c517ea | 2021-05-23 02:24:19 +0530 | [diff] [blame] | 97 | { |
| 98 | int fd; |
Caleb Connolly | b0addf4 | 2021-08-13 16:51:20 +0100 | [diff] [blame] | 99 | char brightness_path[4096]; |
| 100 | char filename[256]; |
| 101 | if (!findFirstBacklightDevice(filename)) { |
| 102 | ALOGE("CA:: Couldn't find backlight brightness path"); |
| 103 | return -EINVAL; |
| 104 | } |
Amit Pundir | 4c517ea | 2021-05-23 02:24:19 +0530 | [diff] [blame] | 105 | |
Caleb Connolly | b0addf4 | 2021-08-13 16:51:20 +0100 | [diff] [blame] | 106 | snprintf(brightness_path, 4096, BACKLIGHT_CLASS_FSTRING, filename); |
| 107 | fd = open(brightness_path, O_WRONLY); |
Amit Pundir | 4c517ea | 2021-05-23 02:24:19 +0530 | [diff] [blame] | 108 | if (fd >= 0) { |
| 109 | char buffer[20]; |
| 110 | ssize_t amt; |
| 111 | size_t bytes = snprintf(buffer, sizeof(buffer), "%d\n", value); |
| 112 | if(bytes < sizeof(buffer)) { |
| 113 | amt = write(fd, buffer, bytes); |
| 114 | } else { |
| 115 | amt = -1; |
| 116 | errno = -EINVAL; |
| 117 | } |
| 118 | close(fd); |
| 119 | return amt == -1 ? -errno : 0; |
| 120 | } else { |
Caleb Connolly | b0addf4 | 2021-08-13 16:51:20 +0100 | [diff] [blame] | 121 | ALOGE("write_brightness failed to open %s, fd = %d, errno = %d\n", brightness_path, fd, errno); |
Amit Pundir | 4c517ea | 2021-05-23 02:24:19 +0530 | [diff] [blame] | 122 | return -errno; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | static int |
| 127 | rgb_to_brightness(struct light_state_t const* state) |
| 128 | { |
| 129 | int color = state->color & 0x00ffffff; |
| 130 | return ((77*((color>>16)&0x00ff)) |
| 131 | + (150*((color>>8)&0x00ff)) + (29*(color&0x00ff))) >> 8; |
| 132 | } |
| 133 | |
| 134 | static int |
| 135 | set_light_backlight(struct light_device_t* dev, |
| 136 | struct light_state_t const* state) |
| 137 | { |
| 138 | if(!dev) { |
| 139 | return -1; |
| 140 | } |
| 141 | |
| 142 | int brightness = rgb_to_brightness(state) << 4; // Scale up to 4095 |
Caleb Connolly | b0addf4 | 2021-08-13 16:51:20 +0100 | [diff] [blame] | 143 | write_brightness(brightness); |
Amit Pundir | 4c517ea | 2021-05-23 02:24:19 +0530 | [diff] [blame] | 144 | |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | /** Close the lights device */ |
| 149 | static int |
| 150 | close_lights(struct light_device_t *dev) |
| 151 | { |
| 152 | if (dev) { |
| 153 | free(dev); |
| 154 | } |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | |
| 159 | /******************************************************************************/ |
| 160 | |
| 161 | /** |
| 162 | * module methods |
| 163 | */ |
| 164 | |
| 165 | /** Open a new instance of a lights device using name */ |
| 166 | static int open_lights(const struct hw_module_t* module, char const* name, |
| 167 | struct hw_device_t** device) |
| 168 | { |
Caleb Connolly | b0addf4 | 2021-08-13 16:51:20 +0100 | [diff] [blame] | 169 | (void)name; |
Amit Pundir | 4c517ea | 2021-05-23 02:24:19 +0530 | [diff] [blame] | 170 | pthread_once(&g_init, init_globals); |
| 171 | |
| 172 | struct light_device_t *dev = malloc(sizeof(struct light_device_t)); |
| 173 | |
| 174 | if(!dev) |
| 175 | return -ENOMEM; |
| 176 | |
| 177 | memset(dev, 0, sizeof(*dev)); |
| 178 | |
| 179 | dev->common.tag = HARDWARE_DEVICE_TAG; |
| 180 | dev->common.version = LIGHTS_DEVICE_API_VERSION_2_0; |
| 181 | dev->common.module = (struct hw_module_t*)module; |
| 182 | dev->common.close = (int (*)(struct hw_device_t*))close_lights; |
| 183 | dev->set_light = set_light_backlight; |
| 184 | |
| 185 | *device = (struct hw_device_t*)dev; |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | static struct hw_module_methods_t lights_module_methods = { |
| 190 | .open = open_lights, |
| 191 | }; |
| 192 | |
| 193 | /* |
| 194 | * The lights Module |
| 195 | */ |
| 196 | struct hw_module_t HAL_MODULE_INFO_SYM = { |
| 197 | .tag = HARDWARE_MODULE_TAG, |
| 198 | .version_major = 1, |
| 199 | .version_minor = 0, |
| 200 | .id = LIGHTS_HARDWARE_MODULE_ID, |
| 201 | .name = "lights Module", |
| 202 | .author = "Google, Inc.", |
| 203 | .methods = &lights_module_methods, |
| 204 | }; |