blob: 63dbc5c770ec46053f5b96f3003ebb809abe1ed0
1 | /* Copyright (C) 2011 The Android Open Source Project |
2 | * |
3 | * Original code licensed under the Apache License, Version 2.0 (the "License"); |
4 | * you may not use this software except in compliance with the License. |
5 | * You may obtain a copy of the License at |
6 | * |
7 | * http://www.apache.org/licenses/LICENSE-2.0 |
8 | * |
9 | * Unless required by applicable law or agreed to in writing, software |
10 | * distributed under the License is distributed on an "AS IS" BASIS, |
11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | * See the License for the specific language governing permissions and |
13 | * limitations under the License. |
14 | * |
15 | * This implements a lights hardware library for the Android emulator. |
16 | * the following code should be built as a shared library that will be |
17 | * placed into /system/lib/hw/lights.goldfish.so |
18 | * |
19 | * It will be loaded by the code in hardware/libhardware/hardware.c |
20 | * which is itself called from |
21 | * ./frameworks/base/services/jni/com_android_server_HardwareService.cpp |
22 | */ |
23 | |
24 | #ifdef LOG_TAG |
25 | #undef LOG_TAG |
26 | #define LOG_TAG "Lights" |
27 | #endif |
28 | #include <cutils/log.h> |
29 | #include <stdint.h> |
30 | #include <string.h> |
31 | #include <unistd.h> |
32 | #include <errno.h> |
33 | #include <fcntl.h> |
34 | #include <pthread.h> |
35 | #include <sys/ioctl.h> |
36 | #include <sys/types.h> |
37 | #include <hardware/lights.h> |
38 | #include <hardware/hardware.h> |
39 | |
40 | /* Set to 1 to enable debug messages to the log */ |
41 | #define DEBUG 0 |
42 | #if DEBUG |
43 | # define D(...) ALOGD(__VA_ARGS__) |
44 | #else |
45 | # define D(...) do{}while(0) |
46 | #endif |
47 | |
48 | #define E(...) ALOGE(__VA_ARGS__) |
49 | #define BACKLIGHT "/sys/class/backlight/aml-bl/brightness" |
50 | |
51 | /* set backlight brightness by LIGHTS_SERVICE_NAME service. */ |
52 | static int |
53 | set_light_backlight( struct light_device_t* dev, struct light_state_t const* state ) |
54 | |
55 | { |
56 | int nwr, ret = -1, fd; |
57 | char value[20]; |
58 | int light_level; |
59 | light_level =state->color&0xff; |
60 | |
61 | fd = open(BACKLIGHT, O_RDWR); |
62 | if (fd > 0) { |
63 | nwr = sprintf(value, "%d\n", light_level); |
64 | ret = write(fd, value, nwr); |
65 | close(fd); |
66 | } |
67 | |
68 | return ret; |
69 | } |
70 | |
71 | static int |
72 | set_light_buttons( struct light_device_t* dev, struct light_state_t const* state ) |
73 | { |
74 | /* @Waiting for later implementation. */ |
75 | D( "%s: Not implemented.", __FUNCTION__ ); |
76 | |
77 | return 0; |
78 | } |
79 | static int |
80 | set_light_battery( struct light_device_t* dev, struct light_state_t const* state ) |
81 | { |
82 | /* @Waiting for later implementation. */ |
83 | D( "%s: Not implemented.", __FUNCTION__ ); |
84 | |
85 | return 0; |
86 | } |
87 | static int |
88 | set_light_keyboard( struct light_device_t* dev, struct light_state_t const* state ) |
89 | { |
90 | /* @Waiting for later implementation. */ |
91 | D( "%s: Not implemented.", __FUNCTION__ ); |
92 | |
93 | return 0; |
94 | } |
95 | static int |
96 | set_light_notifications( struct light_device_t* dev, struct light_state_t const* state ) |
97 | { |
98 | /* @Waiting for later implementation. */ |
99 | D( "%s: Not implemented.", __FUNCTION__ ); |
100 | |
101 | return 0; |
102 | } |
103 | static int |
104 | set_light_attention( struct light_device_t* dev, struct light_state_t const* state ) |
105 | { |
106 | /* @Waiting for later implementation. */ |
107 | D( "%s: Not implemented.", __FUNCTION__ ); |
108 | |
109 | return 0; |
110 | } |
111 | /** Close the lights device */ |
112 | static int |
113 | close_lights( struct light_device_t *dev ) |
114 | { |
115 | free( dev ); |
116 | |
117 | return 0; |
118 | } |
119 | /** |
120 | * module methods |
121 | */ |
122 | /** Open a new instance of a lights device using name */ |
123 | static int |
124 | open_lights( const struct hw_module_t* module, char const *name, |
125 | struct hw_device_t **device ) |
126 | { |
127 | void* set_light; |
128 | |
129 | if (0 == strcmp( LIGHT_ID_BACKLIGHT, name )) { |
130 | set_light = set_light_backlight; |
131 | } else if (0 == strcmp( LIGHT_ID_KEYBOARD, name )) { |
132 | set_light = set_light_keyboard; |
133 | } else if (0 == strcmp( LIGHT_ID_BUTTONS, name )) { |
134 | set_light = set_light_buttons; |
135 | } else if (0 == strcmp( LIGHT_ID_BATTERY, name )) { |
136 | set_light = set_light_battery; |
137 | } else if (0 == strcmp( LIGHT_ID_NOTIFICATIONS, name )) { |
138 | set_light = set_light_notifications; |
139 | } else if (0 == strcmp( LIGHT_ID_ATTENTION, name )) { |
140 | set_light = set_light_attention; |
141 | } else { |
142 | D( "%s: %s light isn't supported yet.", __FUNCTION__, name ); |
143 | return -EINVAL; |
144 | } |
145 | |
146 | struct light_device_t *dev = malloc( sizeof(struct light_device_t) ); |
147 | if (dev == NULL) { |
148 | return -EINVAL; |
149 | } |
150 | memset( dev, 0, sizeof(*dev) ); |
151 | |
152 | dev->common.tag = HARDWARE_DEVICE_TAG; |
153 | dev->common.version = 0; |
154 | dev->common.module = (struct hw_module_t*)module; |
155 | dev->common.close = (int (*)(struct hw_device_t*))close_lights; |
156 | dev->set_light = set_light; |
157 | |
158 | *device = (struct hw_device_t*)dev; |
159 | return 0; |
160 | } |
161 | |
162 | static struct hw_module_methods_t lights_module_methods = { |
163 | .open = open_lights, |
164 | }; |
165 | |
166 | /* |
167 | * The emulator lights Module |
168 | */ |
169 | struct hw_module_t HAL_MODULE_INFO_SYM = { |
170 | .tag = HARDWARE_MODULE_TAG, |
171 | .version_major = 1, |
172 | .version_minor = 0, |
173 | .id = LIGHTS_HARDWARE_MODULE_ID, |
174 | .name = "Amlogic lights Module", |
175 | .author = "Amlogic", |
176 | .methods = &lights_module_methods, |
177 | }; |
178 |