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 #include <stdint.h>
25 #include <stdlib.h>
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 
37 #ifndef DEFAULT_LOW_PERSISTENCE_MODE_BRIGHTNESS
38 #define DEFAULT_LOW_PERSISTENCE_MODE_BRIGHTNESS 0x80
39 #endif
40 
41 /******************************************************************************/
42 
43 static pthread_once_t g_init = PTHREAD_ONCE_INIT;
44 static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER;
45 static struct light_state_t g_notification;
46 static struct light_state_t g_battery;
47 static int g_last_backlight_mode = BRIGHTNESS_MODE_USER;
48 static int g_attention = 0;
49 static bool g_has_persistence_node = false;
50 
51 char const*const LCD_FILE
52         = "/sys/class/leds/lcd-backlight/brightness";
53 
54 char const*const LCD_FILE2
55         = "/sys/class/backlight/panel0-backlight/brightness";
56 
57 char const*const BUTTON_FILE
58         = "/sys/class/leds/button-backlight/brightness";
59 
60 char const*const PERSISTENCE_FILE
61         = "/sys/class/graphics/fb0/msm_fb_persist_mode";
62 
63 enum rgb_led {
64     LED_RED = 0,
65     LED_GREEN,
66     LED_BLUE,
67 };
68 
69 char *led_names[] = {
70     "red",
71     "green",
72     "blue",
73 };
74 /**
75  * device methods
76  */
77 
init_globals(void)78 void init_globals(void)
79 {
80     // init the mutex
81     pthread_mutex_init(&g_lock, NULL);
82 }
83 
write_int(char const * path,int value)84 static int write_int(char const* path, int value)
85 {
86     int fd;
87     static int already_warned = 0;
88 
89     fd = open(path, O_RDWR);
90     if (fd >= 0) {
91         char buffer[20];
92         int bytes = snprintf(buffer, sizeof(buffer), "%d\n", value);
93         ssize_t amt = write(fd, buffer, (size_t)bytes);
94         close(fd);
95         return amt == -1 ? -errno : 0;
96     } else {
97         if (already_warned == 0) {
98             ALOGE("write_int failed to open %s, errno = %d\n", path, errno);
99             already_warned = 1;
100         }
101         return -errno;
102     }
103 }
104 
file_exists(const char * file)105 static bool file_exists(const char *file)
106 {
107     int fd;
108 
109     fd = open(file, O_RDWR);
110     if (fd < 0) {
111         ALOGE("failed to open %s, errno=%d\n", file, errno);
112         return false;
113     }
114 
115     close(fd);
116     return true;
117 }
118 
119 static int
is_lit(struct light_state_t const * state)120 is_lit(struct light_state_t const* state)
121 {
122     return state->color & 0x00ffffff;
123 }
124 
125 static int
rgb_to_brightness(struct light_state_t const * state)126 rgb_to_brightness(struct light_state_t const* state)
127 {
128     int color = state->color & 0x00ffffff;
129     return ((77*((color>>16)&0x00ff))
130             + (150*((color>>8)&0x00ff)) + (29*(color&0x00ff))) >> 8;
131 }
132 
133 static int
set_light_backlight(struct light_device_t * dev,struct light_state_t const * state)134 set_light_backlight(struct light_device_t* dev,
135         struct light_state_t const* state)
136 {
137     int err = 0;
138     int brightness = rgb_to_brightness(state);
139     unsigned int lpEnabled =
140         state->brightnessMode == BRIGHTNESS_MODE_LOW_PERSISTENCE;
141     if(!dev) {
142         return -1;
143     }
144 
145     pthread_mutex_lock(&g_lock);
146     // Toggle low persistence mode state
147     bool persistence_mode = ((g_last_backlight_mode != state->brightnessMode && lpEnabled) ||
148                             (!lpEnabled &&
149                             g_last_backlight_mode == BRIGHTNESS_MODE_LOW_PERSISTENCE));
150     bool cannot_handle_persistence = !g_has_persistence_node && persistence_mode;
151     if (g_has_persistence_node) {
152         if (persistence_mode) {
153             if ((err = write_int(PERSISTENCE_FILE, lpEnabled)) != 0) {
154                 ALOGE("%s: Failed to write to %s: %s\n", __FUNCTION__,
155                        PERSISTENCE_FILE, strerror(errno));
156             }
157             if (lpEnabled != 0) {
158                 brightness = DEFAULT_LOW_PERSISTENCE_MODE_BRIGHTNESS;
159             }
160         }
161         g_last_backlight_mode = state->brightnessMode;
162     }
163 
164     if (!err) {
165         if (!access(LCD_FILE, F_OK)) {
166             err = write_int(LCD_FILE, brightness);
167         } else {
168             err = write_int(LCD_FILE2, brightness);
169         }
170     }
171 
172     pthread_mutex_unlock(&g_lock);
173     return cannot_handle_persistence ? -ENOSYS : err;
174 }
175 
set_rgb_led_brightness(enum rgb_led led,int brightness)176 static int set_rgb_led_brightness(enum rgb_led led, int brightness)
177 {
178     char file[48];
179 
180     snprintf(file, sizeof(file), "/sys/class/leds/%s/brightness", led_names[led]);
181     return write_int(file, brightness);
182 }
183 
set_rgb_led_timer_trigger(enum rgb_led led,int onMS,int offMS)184 static int set_rgb_led_timer_trigger(enum rgb_led led, int onMS, int offMS)
185 {
186     char file[48];
187     int rc;
188 
189     snprintf(file, sizeof(file), "/sys/class/leds/%s/delay_off", led_names[led]);
190     rc = write_int(file, offMS);
191     if (rc < 0)
192         goto out;
193 
194     snprintf(file, sizeof(file), "/sys/class/leds/%s/delay_on", led_names[led]);
195     rc = write_int(file, onMS);
196     if (rc < 0)
197         goto out;
198 
199     return 0;
200 out:
201     ALOGD("%s doesn't support timer trigger\n", led_names[led]);
202     return rc;
203 }
204 
set_rgb_led_hw_blink(enum rgb_led led,int blink)205 static int set_rgb_led_hw_blink(enum rgb_led led, int blink)
206 {
207     char file[48];
208 
209     snprintf(file, sizeof(file), "/sys/class/leds/%s/breath", led_names[led]);
210     if (!file_exists(file))
211         snprintf(file, sizeof(file), "/sys/class/leds/%s/blink", led_names[led]);
212 
213     return write_int(file, blink);
214 }
215 
216 static int
set_speaker_light_locked(struct light_device_t * dev,struct light_state_t const * state)217 set_speaker_light_locked(struct light_device_t* dev,
218         struct light_state_t const* state)
219 {
220     int red, green, blue;
221     int onMS, offMS;
222     unsigned int colorRGB;
223     int blink = 0;
224     int rc = 0;
225 
226     if(!dev) {
227         return -1;
228     }
229 
230     colorRGB = state->color;
231     red = (colorRGB >> 16) & 0xFF;
232     green = (colorRGB >> 8) & 0xFF;
233     blue = colorRGB & 0xFF;
234 
235     onMS = state->flashOnMS;
236     offMS = state->flashOffMS;
237 
238     if (onMS != 0 && offMS != 0)
239         blink = 1;
240 
241     switch (state->flashMode) {
242         case LIGHT_FLASH_HARDWARE:
243             if (!!red)
244                 rc = set_rgb_led_hw_blink(LED_RED, blink);
245             if (!!green)
246                 rc |= set_rgb_led_hw_blink(LED_GREEN, blink);
247             if (!!blue)
248                 rc |= set_rgb_led_hw_blink(LED_BLUE, blink);
249             /* fallback to timed blinking if breath is not supported */
250             if (rc == 0)
251                 break;
252         case LIGHT_FLASH_TIMED:
253             if (!!red)
254                 rc = set_rgb_led_timer_trigger(LED_RED, onMS, offMS);
255             if (!!green)
256                 rc |= set_rgb_led_timer_trigger(LED_GREEN, onMS, offMS);
257             if (!!blue)
258                 rc |= set_rgb_led_timer_trigger(LED_BLUE, onMS, offMS);
259             /* fallback to constant on if timed blinking is not supported */
260             if (rc == 0)
261                 break;
262         case LIGHT_FLASH_NONE:
263         default:
264             rc = set_rgb_led_brightness(LED_RED, red);
265             rc |= set_rgb_led_brightness(LED_GREEN, green);
266             rc |= set_rgb_led_brightness(LED_BLUE, blue);
267             break;
268     }
269 
270     ALOGD("set_speaker_light_locked mode=%d, colorRGB=%08X, onMS=%d, offMS=%d, rc=%d\n",
271             state->flashMode, colorRGB, onMS, offMS, rc);
272 
273     return rc;
274 }
275 
276 static void
handle_speaker_battery_locked(struct light_device_t * dev)277 handle_speaker_battery_locked(struct light_device_t* dev)
278 {
279     if (is_lit(&g_battery)) {
280         set_speaker_light_locked(dev, &g_battery);
281     } else {
282         set_speaker_light_locked(dev, &g_notification);
283     }
284 }
285 
286 static int
set_light_battery(struct light_device_t * dev,struct light_state_t const * state)287 set_light_battery(struct light_device_t* dev,
288         struct light_state_t const* state)
289 {
290     pthread_mutex_lock(&g_lock);
291     g_battery = *state;
292     handle_speaker_battery_locked(dev);
293     pthread_mutex_unlock(&g_lock);
294     return 0;
295 }
296 
297 static int
set_light_notifications(struct light_device_t * dev,struct light_state_t const * state)298 set_light_notifications(struct light_device_t* dev,
299         struct light_state_t const* state)
300 {
301     pthread_mutex_lock(&g_lock);
302     g_notification = *state;
303     handle_speaker_battery_locked(dev);
304     pthread_mutex_unlock(&g_lock);
305     return 0;
306 }
307 
308 static int
set_light_attention(struct light_device_t * dev,struct light_state_t const * state)309 set_light_attention(struct light_device_t* dev,
310         struct light_state_t const* state)
311 {
312     pthread_mutex_lock(&g_lock);
313     if (state->flashMode == LIGHT_FLASH_HARDWARE) {
314         g_attention = state->flashOnMS;
315     } else if (state->flashMode == LIGHT_FLASH_NONE) {
316         g_attention = 0;
317     }
318     handle_speaker_battery_locked(dev);
319     pthread_mutex_unlock(&g_lock);
320     return 0;
321 }
322 
323 static int
set_light_buttons(struct light_device_t * dev,struct light_state_t const * state)324 set_light_buttons(struct light_device_t* dev,
325         struct light_state_t const* state)
326 {
327     int err = 0;
328     if(!dev) {
329         return -1;
330     }
331     pthread_mutex_lock(&g_lock);
332     err = write_int(BUTTON_FILE, state->color & 0xFF);
333     pthread_mutex_unlock(&g_lock);
334     return err;
335 }
336 
337 /** Close the lights device */
338 static int
close_lights(struct light_device_t * dev)339 close_lights(struct light_device_t *dev)
340 {
341     if (dev) {
342         free(dev);
343     }
344     return 0;
345 }
346 
347 
348 /******************************************************************************/
349 
350 /**
351  * module methods
352  */
353 
354 /** Open a new instance of a lights device using name */
open_lights(const struct hw_module_t * module,char const * name,struct hw_device_t ** device)355 static int open_lights(const struct hw_module_t* module, char const* name,
356         struct hw_device_t** device)
357 {
358     int (*set_light)(struct light_device_t* dev,
359             struct light_state_t const* state);
360 
361     if (0 == strcmp(LIGHT_ID_BACKLIGHT, name)) {
362         g_has_persistence_node = !access(PERSISTENCE_FILE, F_OK);
363         set_light = set_light_backlight;
364     } else if (0 == strcmp(LIGHT_ID_BATTERY, name))
365         set_light = set_light_battery;
366     else if (0 == strcmp(LIGHT_ID_NOTIFICATIONS, name))
367         set_light = set_light_notifications;
368     else if (0 == strcmp(LIGHT_ID_BUTTONS, name)) {
369         if (!access(BUTTON_FILE, F_OK)) {
370           // enable light button when the file is present
371           set_light = set_light_buttons;
372         } else {
373           return -EINVAL;
374         }
375     }
376     else if (0 == strcmp(LIGHT_ID_ATTENTION, name))
377         set_light = set_light_attention;
378     else
379         return -EINVAL;
380 
381     pthread_once(&g_init, init_globals);
382 
383     struct light_device_t *dev = malloc(sizeof(struct light_device_t));
384 
385     if(!dev)
386         return -ENOMEM;
387 
388     memset(dev, 0, sizeof(*dev));
389 
390     dev->common.tag = HARDWARE_DEVICE_TAG;
391     dev->common.version = LIGHTS_DEVICE_API_VERSION_2_0;
392     dev->common.module = (struct hw_module_t*)module;
393     dev->common.close = (int (*)(struct hw_device_t*))close_lights;
394     dev->set_light = set_light;
395 
396     *device = (struct hw_device_t*)dev;
397     return 0;
398 }
399 
400 static struct hw_module_methods_t lights_module_methods = {
401     .open =  open_lights,
402 };
403 
404 /*
405  * The lights Module
406  */
407 struct hw_module_t HAL_MODULE_INFO_SYM = {
408     .tag = HARDWARE_MODULE_TAG,
409     .version_major = 1,
410     .version_minor = 0,
411     .id = LIGHTS_HARDWARE_MODULE_ID,
412     .name = "lights Module",
413     .author = "Google, Inc.",
414     .methods = &lights_module_methods,
415 };
416