• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <cutils/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 len;
136     int red, green, blue;
137     int onMS, offMS;
138     unsigned int colorRGB;
139 
140     switch (state->flashMode) {
141     case LIGHT_FLASH_TIMED:
142     case LIGHT_FLASH_HARDWARE:
143         onMS = state->flashOnMS;
144         offMS = state->flashOffMS;
145         break;
146     case LIGHT_FLASH_NONE:
147     default:
148         onMS = 0;
149         offMS = 0;
150         break;
151     }
152 
153     colorRGB = state->color;
154 
155 #if DEBUG
156     ALOGD("set_light_locked mode %d, colorRGB=%08X, onMS=%d, offMS=%d\n",
157             state->flashMode, colorRGB, onMS, offMS);
158 #endif
159 
160     red = (colorRGB >> 16) & 0xFF;
161     green = (colorRGB >> 8) & 0xFF;
162     blue = colorRGB & 0xFF;
163 
164     // due to limitation of driver
165     if (onMS == 0) {
166         red = 0;
167         green = 0;
168         blue = 0;
169     }
170 
171     write_int(RGB_LOCKED_FILE, 0);
172 
173     write_int(RED_LED_FILE, red);
174     write_int(GREEN_LED_FILE, green);
175     write_int(BLUE_LED_FILE, blue);
176 
177     write_on_off(RED_TIMEOUT_FILE, onMS, offMS);
178     write_on_off(GREEN_TIMEOUT_FILE, onMS, offMS);
179     write_on_off(BLUE_TIMEOUT_FILE, onMS, offMS);
180 
181     write_int(RGB_LOCKED_FILE, 1);
182 
183     return 0;
184 }
185 
set_light_notifications(struct light_device_t * dev __unused,struct light_state_t const * state)186 static int set_light_notifications(struct light_device_t* dev __unused,
187         struct light_state_t const* state)
188 {
189     pthread_mutex_lock(&g_lock);
190     set_light_locked(state, 0);
191     pthread_mutex_unlock(&g_lock);
192 
193     return 0;
194 }
195 
set_light_attention(struct light_device_t * dev __unused,struct light_state_t const * state)196 static int set_light_attention(struct light_device_t* dev __unused,
197         struct light_state_t const* state)
198 {
199     pthread_mutex_lock(&g_lock);
200     set_light_locked(state, 1);
201     pthread_mutex_unlock(&g_lock);
202 
203     return 0;
204 }
205 
206 
207 /** Close the lights device */
close_lights(struct light_device_t * dev)208 static int close_lights(struct light_device_t *dev)
209 {
210     if (dev)
211         free(dev);
212 
213     return 0;
214 }
215 
216 /******************************************************************************/
217 
218 /**
219  * module methods
220  */
221 
222 /** 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)223 static int open_lights(const struct hw_module_t* module, char const* name,
224         struct hw_device_t** device)
225 {
226     int (*set_light)(struct light_device_t* dev,
227             struct light_state_t const* state);
228 
229     if (!strcmp(LIGHT_ID_BACKLIGHT, name))
230         set_light = set_light_backlight;
231     else if (!strcmp(LIGHT_ID_NOTIFICATIONS, name))
232         set_light = set_light_notifications;
233     else if (!strcmp(LIGHT_ID_ATTENTION, name))
234         set_light = set_light_attention;
235     else
236         return -EINVAL;
237 
238     pthread_once(&g_init, init_globals);
239 
240     struct light_device_t *dev = malloc(sizeof(struct light_device_t));
241 
242     if(!dev)
243         return -ENOMEM;
244 
245     memset(dev, 0, sizeof(*dev));
246 
247     dev->common.tag = HARDWARE_DEVICE_TAG;
248     dev->common.version = 0;
249     dev->common.module = (struct hw_module_t*)module;
250     dev->common.close = (int (*)(struct hw_device_t*))close_lights;
251     dev->set_light = set_light;
252 
253     *device = (struct hw_device_t*)dev;
254 
255     return 0;
256 }
257 
258 static struct hw_module_methods_t lights_module_methods = {
259     .open =  open_lights,
260 };
261 
262 /*
263  * The lights Module
264  */
265 struct hw_module_t HAL_MODULE_INFO_SYM = {
266     .tag = HARDWARE_MODULE_TAG,
267     .version_major = 1,
268     .version_minor = 0,
269     .id = LIGHTS_HARDWARE_MODULE_ID,
270     .name = "lights Module (VTS instrumented)",
271     .author = "Google, Inc.",
272     .methods = &lights_module_methods,
273 };
274