Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2015
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.21 KB | None | 0 0
  1. /*
  2. * Copyright (C) 2008 The Android Open Source Project
  3. * Copyright (C) 2011 Diogo Ferreira <defer@cyanogenmod.com>
  4. * Copyright (C) 2012 Andreas Makris <andreas.makris@gmail.com>
  5. * Copyright (C) 2012 The CyanogenMod Project <http://www.cyanogenmod.com>
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19.  
  20. #define LOG_NDEBUG 0
  21.  
  22. #include <cutils/log.h>
  23. #include <stdint.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <unistd.h>
  27. #include <errno.h>
  28. #include <fcntl.h>
  29. #include <pthread.h>
  30. #include <sys/ioctl.h>
  31. #include <sys/types.h>
  32. #include <hardware/lights.h>
  33. #include "lights.h"
  34.  
  35. /* Synchronization primities */
  36. static pthread_once_t g_init = PTHREAD_ONCE_INIT;
  37. static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER;
  38.  
  39. /* Mini-led state machine */
  40. static struct light_state_t g_notification;
  41. static struct light_state_t g_battery;
  42. static int g_backlight = 255;
  43.  
  44. /* The leds we have */
  45. enum {
  46. LED_RED,
  47. LED_GREEN,
  48. LED_BLUE,
  49. LED_BLANK
  50. };
  51. enum {
  52. MANUAL = 0,
  53. AUTOMATIC,
  54. MANUAL_SENSOR
  55. };
  56.  
  57. static int write_int (const char *path, int value) {
  58. int fd;
  59. static int already_warned = 0;
  60. fd = open(path, O_RDWR);
  61. if (fd < 0) {
  62. if (already_warned == 0) {
  63. ALOGE("write_int failed to open %s\n", path);
  64. already_warned = 1;
  65. }
  66. return -errno;
  67. }
  68. char buffer[20];
  69. int bytes = snprintf(buffer, sizeof(buffer), "%d\n", value);
  70. int written = write (fd, buffer, bytes);
  71. close(fd);
  72. return written == -1 ? -errno : 0;
  73. }
  74.  
  75. static int write_string (const char *path, const char *value) {
  76. int fd;
  77. static int already_warned = 0;
  78. fd = open(path, O_RDWR);
  79. if (fd < 0) {
  80. if (already_warned == 0) {
  81. ALOGE("write_string failed to open %s\n", path);
  82. already_warned = 1;
  83. }
  84. return -errno;
  85. }
  86. char buffer[20];
  87. int bytes = snprintf(buffer, sizeof(buffer), "%s\n", value);
  88. int written = write (fd, buffer, bytes);
  89. close(fd);
  90. return written == -1 ? -errno : 0;
  91. }
  92.  
  93. /* Color tools */
  94. static int is_lit (struct light_state_t const* state) {
  95. return state->color & 0x00ffffff;
  96. }
  97.  
  98. static int rgb_to_brightness (struct light_state_t const* state) {
  99. int color = state->color & 0x00ffffff;
  100. return ((77*((color>>16)&0x00ff))
  101. + (150*((color>>8)&0x00ff)) + (29*(color&0x00ff))) >> 8;
  102. }
  103.  
  104. /* The actual lights controlling section */
  105. static int set_light_backlight (struct light_device_t *dev, struct light_state_t const *state) {
  106. int brightness = rgb_to_brightness(state);
  107. int als_mode;
  108. switch (state->brightnessMode) {
  109. case BRIGHTNESS_MODE_SENSOR:
  110. als_mode = AUTOMATIC;
  111. break;
  112. case BRIGHTNESS_MODE_USER:
  113. als_mode = BRIGHTNESS_MODE_USER;
  114. break;
  115. default:
  116. als_mode = MANUAL_SENSOR;
  117. break;
  118. }
  119. ALOGV("%s brightness=%d color=0x%08x", __func__,brightness,state->color);
  120. pthread_mutex_lock(&g_lock);
  121. g_backlight = brightness;
  122. write_int (ALS_FILE, als_mode);
  123. write_int (LCD_BACKLIGHT_FILE, brightness);
  124. pthread_mutex_unlock(&g_lock);
  125. return 0;
  126. }
  127.  
  128. static int set_light_buttons (struct light_device_t *dev, struct light_state_t const* state) {
  129. size_t i;
  130. int on = is_lit(state);
  131. pthread_mutex_lock(&g_lock);
  132. for (i = 0; i < sizeof(BUTTON_BACKLIGHT_FILE)/sizeof(BUTTON_BACKLIGHT_FILE[0]); i++) {
  133. write_int (BUTTON_BACKLIGHT_FILE[i],on?255:0);
  134. }
  135. pthread_mutex_unlock(&g_lock);
  136. return 0;
  137. }
  138.  
  139. static void set_shared_light_locked (struct light_device_t *dev, struct light_state_t *state) {
  140. int r, g, b;
  141. int delayOn,delayOff;
  142. r = (state->color >> 16) & 0xFF;
  143. g = (state->color >> 8) & 0xFF;
  144. b = (state->color) & 0xFF;
  145. delayOn = state->flashOnMS;
  146. delayOff = state->flashOffMS;
  147.  
  148. ALOGV("set_shared_light_lock, R: %d, G: %d, B: %d, color=0x%08x", r, g, b, state->color);
  149.  
  150. if (state->flashMode != LIGHT_FLASH_NONE) {
  151. write_string (RED_LED_FILE_TRIGGER, "timer");
  152. write_string (GREEN_LED_FILE_TRIGGER, "timer");
  153. write_string (BLUE_LED_FILE_TRIGGER, "timer");
  154. write_int (RED_LED_FILE_DELAYON, delayOn);
  155. write_int (GREEN_LED_FILE_DELAYON, delayOn);
  156. write_int (BLUE_LED_FILE_DELAYON, delayOn);
  157. write_int (RED_LED_FILE_DELAYOFF, delayOff);
  158. write_int (GREEN_LED_FILE_DELAYOFF, delayOff);
  159. write_int (BLUE_LED_FILE_DELAYOFF, delayOff);
  160. } else {
  161. write_string (RED_LED_FILE_TRIGGER, "none");
  162. write_string (GREEN_LED_FILE_TRIGGER, "none");
  163. write_string (BLUE_LED_FILE_TRIGGER, "none");
  164. }
  165. write_int (RED_LED_FILE, r);
  166. write_int (GREEN_LED_FILE, g);
  167. write_int (BLUE_LED_FILE, b);
  168. }
  169.  
  170. static void handle_shared_battery_locked (struct light_device_t *dev) {
  171. if (is_lit (&g_notification)) {
  172. set_shared_light_locked (dev, &g_notification);
  173. } else {
  174. set_shared_light_locked (dev, &g_battery);
  175. }
  176. }
  177.  
  178. static int set_light_battery (struct light_device_t *dev, struct light_state_t const* state) {
  179. pthread_mutex_lock (&g_lock);
  180. g_battery = *state;
  181. handle_shared_battery_locked(dev);
  182. pthread_mutex_unlock (&g_lock);
  183. return 0;
  184. }
  185.  
  186. static int set_light_notifications (struct light_device_t *dev, struct light_state_t const* state) {
  187. pthread_mutex_lock (&g_lock);
  188. g_notification = *state;
  189. handle_shared_battery_locked(dev);
  190. pthread_mutex_unlock (&g_lock);
  191. return 0;
  192. }
  193.  
  194. /* Initializations */
  195. void init_globals () {
  196. pthread_mutex_init (&g_lock, NULL);
  197. }
  198.  
  199. /* Glueing boilerplate */
  200. static int close_lights (struct light_device_t *dev) {
  201. if (dev)
  202. free(dev);
  203. return 0;
  204. }
  205.  
  206. static int open_lights (const struct hw_module_t* module, char const* name,
  207. struct hw_device_t** device) {
  208. int (*set_light)(struct light_device_t* dev,
  209. struct light_state_t const *state);
  210. if (0 == strcmp(LIGHT_ID_BACKLIGHT, name)) {
  211. set_light = set_light_backlight;
  212. }
  213. else if (0 == strcmp(LIGHT_ID_BUTTONS, name)) {
  214. set_light = set_light_buttons;
  215. }
  216. else if (0 == strcmp(LIGHT_ID_BATTERY, name)) {
  217. set_light = set_light_battery;
  218. }
  219. else if (0 == strcmp(LIGHT_ID_NOTIFICATIONS, name)) {
  220. set_light = set_light_notifications;
  221. }
  222. else {
  223. return -EINVAL;
  224. }
  225. pthread_once (&g_init, init_globals);
  226. struct light_device_t *dev = malloc(sizeof (struct light_device_t));
  227. memset(dev, 0, sizeof(*dev));
  228. dev->common.tag = HARDWARE_DEVICE_TAG;
  229. dev->common.version = 0;
  230. dev->common.module = (struct hw_module_t*)module;
  231. dev->common.close = (int (*)(struct hw_device_t*))close_lights;
  232. dev->set_light = set_light;
  233. *device = (struct hw_device_t*)dev;
  234. return 0;
  235. }
  236.  
  237. static struct hw_module_methods_t lights_module_methods = {
  238. .open = open_lights,
  239. };
  240.  
  241. struct hw_module_t HAL_MODULE_INFO_SYM = {
  242. .tag = HARDWARE_MODULE_TAG,
  243. .version_major = 1,
  244. .version_minor = 0,
  245. .id = LIGHTS_HARDWARE_MODULE_ID,
  246. .name = "Sony lights module",
  247. .author = "Diogo Ferreira <defer@cyanogenmod.com>, Andreas Makris <Andreas.Makris@gmail.com>",
  248. .methods = &lights_module_methods,
  249. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement