1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "lights"
18 #define DEBUG 1
19 
20 #include <log/log.h>
21 
22 #include <malloc.h>
23 #include <stdint.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <pthread.h>
29 
30 #include <sys/ioctl.h>
31 #include <sys/types.h>
32 
33 #include <hardware/lights.h>
34 
35 /******************************************************************************/
36 
37 static pthread_once_t g_init = PTHREAD_ONCE_INIT;
38 static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER;
39 
40 char const *const LCD_FILE           = "/sys/class/leds/lcd-backlight/brightness";
41 char const *const RED_LED_FILE       = "/sys/class/leds/red/brightness";
42 char const *const GREEN_LED_FILE     = "/sys/class/leds/green/brightness";
43 char const *const BLUE_LED_FILE      = "/sys/class/leds/blue/brightness";
44 char const *const RED_TIMEOUT_FILE   = "/sys/class/leds/red/on_off_ms";
45 char const *const GREEN_TIMEOUT_FILE = "/sys/class/leds/green/on_off_ms";
46 char const *const BLUE_TIMEOUT_FILE  = "/sys/class/leds/blue/on_off_ms";
47 char const *const RGB_LOCKED_FILE    = "/sys/class/leds/red/rgb_start";
48 
49 /**
50  * device methods
51  */
52 
init_globals(void)53 void init_globals(void)
54 {
55     // init the mutex
56     pthread_mutex_init(&g_lock, NULL);
57 }
58 
write_int(char const * path,int value)59 static int write_int(char const* path, int value)
60 {
61     int fd;
62     static int already_warned = 0;
63 
64     fd = open(path, O_RDWR);
65     if (fd >= 0) {
66         char buffer[32] = {0,};
67         int bytes = snprintf(buffer, sizeof(buffer), "%d\n", value);
68         int amt = write(fd, buffer, bytes);
69         close(fd);
70         return amt == -1 ? -errno : 0;
71     } else {
72         if (already_warned == 0) {
73             ALOGE("write_int failed to open %s\n", path);
74             already_warned = 1;
75         }
76         return -errno;
77     }
78 }
79 
write_on_off(char const * path,int on,int off)80 static int write_on_off(char const* path, int on, int off)
81 {
82     int fd;
83     static int already_warned = 0;
84 
85     fd = open(path, O_RDWR);
86     if (fd >= 0) {
87         char buffer[32] = {0,};
88         int bytes = snprintf(buffer, sizeof(buffer), "%d %d\n", on, off);
89         int amt = write(fd, buffer, 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\n", path);
95             already_warned = 1;
96         }
97         return -errno;
98     }
99 }
100 
rgb_to_brightness(struct light_state_t const * state)101 static int rgb_to_brightness(struct light_state_t const* state)
102 {
103     int color = state->color & 0x00ffffff;
104 
105     return ((77 * ((color >> 16) & 0x00ff))
106             + (150 * ((color >> 8) & 0x00ff)) + (29 * (color & 0x00ff))) >> 8;
107 }
108 
109 #if TEST_COMPLEX_CFG
simple_recursion(int recursion)110 void simple_recursion(int recursion) {
111   if (recursion == 0) return;
112   ALOGE("simple_recursion %d", recursion);
113   simple_recursion(recursion - 1);
114 }
115 #endif
116 
set_light_backlight(struct light_device_t * dev __unused,struct light_state_t const * state)117 static int set_light_backlight(struct light_device_t* dev __unused,
118         struct light_state_t const* state)
119 {
120     int err = 0;
121     int brightness = rgb_to_brightness(state);
122 
123     pthread_mutex_lock(&g_lock);
124     err = write_int(LCD_FILE, brightness);
125     pthread_mutex_unlock(&g_lock);
126 #if TEST_COMPLEX_CFG
127     simple_recursion(10);
128 #endif
129 
130     return err;
131 }
132 
set_light_locked(struct light_state_t const * state,int type __unused)133 static int set_light_locked(struct light_state_t const* state, int type __unused)
134 {
135     int red, green, blue;
136     int onMS, offMS;
137     unsigned int colorRGB;
138 
139     switch (state->flashMode) {
140     case LIGHT_FLASH_TIMED:
141     case LIGHT_FLASH_HARDWARE:
142         onMS = state->flashOnMS;
143         offMS = state->flashOffMS;
144         break;
145     case LIGHT_FLASH_NONE:
146     default:
147         onMS = 0;
148         offMS = 0;
149         break;
150     }
151 
152     colorRGB = state->color;
153 
154 #if DEBUG
155     ALOGD("set_light_locked mode %d, colorRGB=%08X, onMS=%d, offMS=%d\n",
156             state->flashMode, colorRGB, onMS, offMS);
157 #endif
158 
159     red = (colorRGB >> 16) & 0xFF;
160     green = (colorRGB >> 8) & 0xFF;
161     blue = colorRGB & 0xFF;
162 
163     // due to limitation of driver
164     if (onMS == 0) {
165         red = 0;
166         green = 0;
167         blue = 0;
168     }
169 
170     write_int(RGB_LOCKED_FILE, 0);
171 
172     write_int(RED_LED_FILE, red);
173     write_int(GREEN_LED_FILE, green);
174     write_int(BLUE_LED_FILE, blue);
175 
176     write_on_off(RED_TIMEOUT_FILE, onMS, offMS);
177     write_on_off(GREEN_TIMEOUT_FILE, onMS, offMS);
178     write_on_off(BLUE_TIMEOUT_FILE, onMS, offMS);
179 
180     write_int(RGB_LOCKED_FILE, 1);
181 
182     return 0;
183 }
184 
set_light_notifications(struct light_device_t * dev __unused,struct light_state_t const * state)185 static int set_light_notifications(struct light_device_t* dev __unused,
186         struct light_state_t const* state)
187 {
188     pthread_mutex_lock(&g_lock);
189     set_light_locked(state, 0);
190     pthread_mutex_unlock(&g_lock);
191 
192     return 0;
193 }
194 
set_light_attention(struct light_device_t * dev __unused,struct light_state_t const * state)195 static int set_light_attention(struct light_device_t* dev __unused,
196         struct light_state_t const* state)
197 {
198     pthread_mutex_lock(&g_lock);
199     set_light_locked(state, 1);
200     pthread_mutex_unlock(&g_lock);
201 
202     return 0;
203 }
204 
205 
206 /** Close the lights device */
close_lights(struct light_device_t * dev)207 static int close_lights(struct light_device_t *dev)
208 {
209     if (dev)
210         free(dev);
211 
212     return 0;
213 }
214 
215 /******************************************************************************/
216 
217 /**
218  * module methods
219  */
220 
221 /** 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)222 static int open_lights(const struct hw_module_t* module, char const* name,
223         struct hw_device_t** device)
224 {
225     int (*set_light)(struct light_device_t* dev,
226             struct light_state_t const* state);
227 
228     if (!strcmp(LIGHT_ID_BACKLIGHT, name))
229         set_light = set_light_backlight;
230     else if (!strcmp(LIGHT_ID_NOTIFICATIONS, name))
231         set_light = set_light_notifications;
232     else if (!strcmp(LIGHT_ID_ATTENTION, name))
233         set_light = set_light_attention;
234     else
235         return -EINVAL;
236 
237     pthread_once(&g_init, init_globals);
238 
239     struct light_device_t *dev = malloc(sizeof(struct light_device_t));
240 
241     if(!dev)
242         return -ENOMEM;
243 
244     memset(dev, 0, sizeof(*dev));
245 
246     dev->common.tag = HARDWARE_DEVICE_TAG;
247     dev->common.version = 0;
248     dev->common.module = (struct hw_module_t*)module;
249     dev->common.close = (int (*)(struct hw_device_t*))close_lights;
250     dev->set_light = set_light;
251 
252     *device = (struct hw_device_t*)dev;
253 
254     return 0;
255 }
256 
257 static struct hw_module_methods_t lights_module_methods = {
258     .open =  open_lights,
259 };
260 
261 /*
262  * The lights Module
263  */
264 struct hw_module_t HAL_MODULE_INFO_SYM = {
265     .tag = HARDWARE_MODULE_TAG,
266     .version_major = 1,
267     .version_minor = 0,
268     .id = LIGHTS_HARDWARE_MODULE_ID,
269     .name = "lights Module (VTS instrumented)",
270     .author = "Google, Inc.",
271     .methods = &lights_module_methods,
272 };
273