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