From 0344dd1ee4db13f8b144e3c79892f79115cbc8ca Mon Sep 17 00:00:00 2001 From: charansingh Date: Mon, 24 Jan 2011 18:10:37 +0530 Subject: [PATCH] fix sensors code and broken build --- liblights/Android.mk | 26 +- liblights/MODULE_LICENSE_APACHE2 | 0 liblights/NOTICE | 2 +- liblights/events.c | 82 +++ liblights/events.h | 29 + liblights/lights.c | 702 ------------------------- liblights/lights.c-mahimahi | 635 ---------------------- liblights/lights_leo.c | 656 +++++++++++++++++++++++ libsensors/Android.mk | 2 +- libsensors/MODULE_LICENSE_APACHE2 | 0 libsensors/NOTICE | 0 libsensors/{sensors.c => leosensors.c} | 94 ++-- 12 files changed, 843 insertions(+), 1385 deletions(-) mode change 100755 => 100644 liblights/Android.mk mode change 100755 => 100644 liblights/MODULE_LICENSE_APACHE2 mode change 100755 => 100644 liblights/NOTICE create mode 100644 liblights/events.c create mode 100644 liblights/events.h delete mode 100755 liblights/lights.c delete mode 100755 liblights/lights.c-mahimahi create mode 100644 liblights/lights_leo.c mode change 100755 => 100644 libsensors/Android.mk mode change 100755 => 100644 libsensors/MODULE_LICENSE_APACHE2 mode change 100755 => 100644 libsensors/NOTICE rename libsensors/{sensors.c => leosensors.c} (97%) mode change 100755 => 100644 diff --git a/liblights/Android.mk b/liblights/Android.mk old mode 100755 new mode 100644 index c3fd9b1..2e9b2ca --- a/liblights/Android.mk +++ b/liblights/Android.mk @@ -12,20 +12,24 @@ # See the License for the specific language governing permissions and # limitations under the License. - LOCAL_PATH:= $(call my-dir) -# HAL module implemenation, not prelinked and stored in -# hw/..so + +ifneq ($(TARGET_SIMULATOR),true) + include $(CLEAR_VARS) -LOCAL_SRC_FILES := lights.c - -LOCAL_PRELINK_MODULE := false -LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/hw - -LOCAL_SHARED_LIBRARIES := liblog - -##LOCAL_MODULE := lights.$(TARGET_BOARD_PLATFORM) LOCAL_MODULE := lights.leo +LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/hw + +LOCAL_MODULE_TAGS := optional + +LOCAL_SRC_FILES := lights_leo.c \ + events.c + +LOCAL_SHARED_LIBRARIES := liblog libcutils +LOCAL_PRELINK_MODULE := false + include $(BUILD_SHARED_LIBRARY) + +endif # !TARGET_SIMULATOR diff --git a/liblights/MODULE_LICENSE_APACHE2 b/liblights/MODULE_LICENSE_APACHE2 old mode 100755 new mode 100644 diff --git a/liblights/NOTICE b/liblights/NOTICE old mode 100755 new mode 100644 index 7340b9e..c5b1efa --- a/liblights/NOTICE +++ b/liblights/NOTICE @@ -1,5 +1,5 @@ - Copyright (c) 2008, The Android Open Source Project + Copyright (c) 2005-2008, The Android Open Source Project Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/liblights/events.c b/liblights/events.c new file mode 100644 index 0000000..28e027b --- /dev/null +++ b/liblights/events.c @@ -0,0 +1,82 @@ +/* + * Copyright (C) 2007 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include + +#include + +#include "events.h" + +#define MAX_DEVICES 16 + +static struct pollfd ev_fds[MAX_DEVICES]; +static unsigned ev_count = 0; + +int ev_init(void) +{ + DIR *dir; + struct dirent *de; + int fd; + + dir = opendir("/dev/input"); + if(dir != 0) { + while((de = readdir(dir))) { +// fprintf(stderr,"/dev/input/%s\n", de->d_name); + if(strncmp(de->d_name,"event",5)) continue; + fd = openat(dirfd(dir), de->d_name, O_RDONLY); + if(fd < 0) continue; + + ev_fds[ev_count].fd = fd; + ev_fds[ev_count].events = POLLIN; + ev_count++; + if(ev_count == MAX_DEVICES) break; + } + } + + return 0; +} + +void ev_exit(void) +{ + while (ev_count > 0) { + close(ev_fds[--ev_count].fd); + } +} + +int ev_get(struct input_event *ev, unsigned dont_wait) +{ + int r; + unsigned n; + + do { + r = poll(ev_fds, ev_count, dont_wait ? 0 : -1); + + if(r > 0) { + for(n = 0; n < ev_count; n++) { + if(ev_fds[n].revents & POLLIN) { + r = read(ev_fds[n].fd, ev, sizeof(*ev)); + if(r == sizeof(*ev)) return 0; + } + } + } + } while(dont_wait == 0); + + return -1; +} diff --git a/liblights/events.h b/liblights/events.h new file mode 100644 index 0000000..4bcba3a --- /dev/null +++ b/liblights/events.h @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2007 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _EVENTS_H_ +#define _EVENTS_H_ + + +// input event structure, include for the definition. +// see http://www.mjmwired.net/kernel/Documentation/input/ for info. +struct input_event; + +int ev_init(void); +int ev_get(struct input_event *ev, unsigned dont_wait); +void ev_exit(void); + +#endif diff --git a/liblights/lights.c b/liblights/lights.c deleted file mode 100755 index 436402c..0000000 --- a/liblights/lights.c +++ /dev/null @@ -1,702 +0,0 @@ -/* - * Copyright (C) 2010 Danijel Posilovic aka dan1j3l - * Copyright (C) 2008 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - - -// #define LOG_NDEBUG 0 -#define LOG_TAG "lights" - -#include - -#include -#include -#include -#include -#include -#include - -#include -#include -#include - -#include -#include -#include - -/******************************************************************************/ - - -static pthread_once_t g_init = PTHREAD_ONCE_INIT; -static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER; - -static struct light_state_t g_notification; -static struct light_state_t g_battery; - -static int g_backlight = 255; -static int g_buttons = 0; -static int g_attention = 0; -static int button_state = 0; -static int screen_suspended = 1; -static int last_battery_state = 0; - -static time_t time_to_off; - -// working threads -static pthread_t t_timed_off = 0; -static pthread_t t_button_checker = 0; -static pthread_t t_battery_checker = 0; - -static pthread_t t_blink_button_backlight = 0; - -static int blink_button = 1; -static int battery_thread_led = 1; -static int was_blinking_notification = 0; -static int g_brightnessMode = 0; - - - -// HTC LEO LEDS -char const*const GREEN_LED_FILE - = "/sys/class/leds/green/brightness"; -char const*const GREEN_BLINK_FILE - = "/sys/class/leds/green/blink"; - -char const*const AMBER_LED_FILE - = "/sys/class/leds/amber/brightness"; -char const*const AMBER_BLINK_FILE - = "/sys/class/leds/amber/blink"; - -char const*const LCD_FILE - = "/sys/class/leds/lcd-backlight/brightness"; - -char const*const BUTTON_FILE - = "/sys/class/leds/button-backlight/brightness"; - -char const*const BUTTON_STATE_DEV - = "/dev/input/event3"; - -char const*const BATTERY_STATUS_FILE - = "/sys/class/power_supply/battery/status"; - -char const*const LS_FILE - = "/sys/devices/platform/htcleo-backlight/auto_bl"; - -/** -device methods -*/ - -void init_globals(void){ - // init the mutex - pthread_mutex_init(&g_lock, NULL); -} - -static int write_int(char const* path, int value){ - int fd; - static int already_warned = 0; - - fd = open(path, O_RDWR); - if (fd >= 0) { - char buffer[20]; - int bytes = sprintf(buffer, "%d\n", value); - int amt = write(fd, buffer, bytes); - close(fd); - return amt == -1 ? -errno : 0; - } else { - if (already_warned == 0) { - LOGE("write_int failed to open %s\n", path); - already_warned = 1; - } - return -errno; - } -} - -static int is_lit(struct light_state_t const* state){ - return state->color & 0x00ffffff; -} - -static int rgb_to_brightness(struct light_state_t const* state){ - int color = state->color & 0x00ffffff; - return ((77*((color>>16)&0x00ff))+ (150*((color>>8)&0x00ff)) + (29*(color&0x00ff))) >> 8; -} - - - -// dan1j3l EXPERIMENTAL - ONLY FOR SENSE BUILDS !!! -// TODO: Add sense detection and start this thread only in sense builds !!! -// Battery checking service ! -void *battery_state_checker(void *arg){ - int fd,size, rs; - char state[20]; - struct timespec t; - t.tv_nsec = 0; - t.tv_sec = 5; - - struct timespec t2; - t.tv_nsec = 0; - t.tv_sec = 1; - - - while (battery_thread_led){ - - memset(&state[0], 0, sizeof(state)); - - fd = open(BATTERY_STATUS_FILE,O_RDONLY); - read(fd, state,20); - close(fd); - - rs=0; - rs = sprintf(state,"%s",state); - - if ( !was_blinking_notification && last_battery_state != rs){ - last_battery_state=rs; - - if ( rs == 9){ // Charging - write_int(GREEN_LED_FILE, 0); - write_int(AMBER_BLINK_FILE, 0); - nanosleep(&t2,NULL); - write_int(AMBER_LED_FILE, 1); - }else if(rs == 5){ // FULL - write_int(AMBER_LED_FILE, 0); - write_int(GREEN_BLINK_FILE, 0); - nanosleep(&t2,NULL); - write_int(GREEN_LED_FILE, 1); - }else{ - write_int(AMBER_LED_FILE, 0); - write_int(GREEN_LED_FILE, 0); - } - - } - - nanosleep(&t,NULL); - } - - t_battery_checker = 0; - - return 0; -} -void start_battery_checker(){ - if (t_button_checker == 0) - pthread_create(&t_battery_checker, NULL, battery_state_checker, NULL); -} - - - -// Functions for timed powering on buttons -void *button_state_checker(void *arg) { - struct input_event ev[64]; - struct timespec t; - int fd, size = sizeof (struct input_event); - t.tv_nsec= 0; - t.tv_sec = 1; - - fd = open(BUTTON_STATE_DEV,O_RDONLY); - - while (1){ - - if (screen_suspended == 1 ){ - write_int(BUTTON_FILE,0); - button_state = 0; - break; - } - - read (fd, ev, size * 64); - - if (ev[0].value == 1){ - write_int(BUTTON_FILE,1); - button_state = 1; - button_timed_off(); - nanosleep(&t,NULL); - } - } - - close(fd); - t_button_checker = 0; // reset thread so we can recreate it later - - return 0; -} -int do_check_button_state() { - - if (t_button_checker == 0) // create thread only if does not exist - pthread_create(&t_button_checker, NULL, button_state_checker, NULL); - - return 0; -} - -void *blink_button_backlight(void *arg) { - struct timespec t; - t.tv_nsec= 0; - t.tv_sec = 1; - - blink_button = 1; - - while (blink_button){ - write_int(BUTTON_FILE,1); - nanosleep(&t,NULL); - write_int(BUTTON_FILE,0); - nanosleep(&t,NULL); - } - - t_blink_button_backlight = 0; // reset thread so we can recreate it later - - return 0; -} -int do_blink_button_backlight() { - - if (t_blink_button_backlight == 0) // create thread only if does not exist - pthread_create(&t_blink_button_backlight, NULL, blink_button_backlight, NULL); - - return 0; -} - - -// Functions for timed powering off buttons -void *timer_button_off(void *arg) { - struct timespec t; - time_t secs; - - t.tv_nsec = 0; - t.tv_sec = 1; - secs = time(NULL); - - // wait untill is time for power off buttons - while (secs < time_to_off){ - secs = time(NULL); - nanosleep(&t, NULL); - }; - - // if screen is suspended and buttons already turned off we exit thread - if (screen_suspended == 1 && button_state == 0) { - t_timed_off = 0; // reset thread - return 0; - } - - // turn off button lights - write_int(BUTTON_FILE,0); - button_state=0; - - // we check button states only if screen is active, else we close both threads - if (screen_suspended == 0) do_check_button_state(); - - t_timed_off = 0; // reset thread - return 0; -} -int button_timed_off() { - // we set time for power off (time now + 10 secs) - time_to_off = time(NULL) + 10; - - if (t_timed_off == 0) // if thread does not exist, we crate it - pthread_create(&t_timed_off, NULL, timer_button_off, NULL); - - return 0; -} - - -static int set_light_buttons(struct light_device_t* dev,struct light_state_t const* state){ - LOGD("set_light_buttons"); - int err = 0; - /*int on = is_lit(state); - pthread_mutex_lock(&g_lock); - g_buttons = on; - err = write_int(BUTTON_FILE, on?255:0); - pthread_mutex_unlock(&g_lock); -*/ - return err; -} - - -static int set_light_backlight(struct light_device_t* dev,struct light_state_t const* state){ - LOGD("set_light_backlight"); - int err = 0; - int brightness = rgb_to_brightness(state); - pthread_mutex_lock(&g_lock); - - // On/Off Buttons - - if (g_brightnessMode != state->brightnessMode) { - g_brightnessMode = state->brightnessMode; - LOGD("Switched brightnessMode=%d brightness=%d\n",g_brightnessMode, - brightness); - write_int(LS_FILE, state->brightnessMode); - } - // if we switched to user mode, allow for setting the backlight immedeately - if (g_brightnessMode == BRIGHTNESS_MODE_USER || brightness==0 || g_backlight==0){ - LOGD("Setting brightnessMode=%d brightness=%d\n", g_brightnessMode, - brightness); - err = write_int(LCD_FILE, brightness); - } - - - - if (brightness == 0 && button_state == 1) { -// LOGD("button off"); - if(g_backlight) err = write_int(BUTTON_FILE,0); - button_state=0; - screen_suspended = 1; - }else if (brightness > 0 && button_state == 0){ -// LOGD("button on"); - if(!g_backlight) err = write_int(BUTTON_FILE,1); - button_state=1; - screen_suspended = 0; - // after powering buttons off, it's time to shut them down after declared amount of time (for now 10 sec) - button_timed_off(); - } - g_backlight = brightness; - pthread_mutex_unlock(&g_lock); - return err; -} - -// dan1j3l: maybe remove this funct -static int set_light_keyboard(struct light_device_t* dev,struct light_state_t const* state){ - LOGD("set_light_keyboard"); - // Nothing to do in leo - return 0; -} - -static void blinkLed(unsigned int color, int blink){ - struct timespec t; - int red, green, blue; - - t.tv_nsec = 0; - t.tv_sec = 1; - - - red = (color >> 16) & 0xFF; - green = (color >> 8) & 0xFF; - blue = color & 0xFF; - - - if (red) { // Amber on / blink - //if(blink){ - // battery_thread_led = 0; // turn off battery thread for a while - //} - write_int(GREEN_LED_FILE, 0); - write_int(AMBER_LED_FILE, 1); - nanosleep(&t, NULL); - if(blink) write_int(AMBER_BLINK_FILE, blink); - LOGD("amber on, blink:%d",blink); - } else if (green || blue || color == 0x1) { // Green on / blink - write_int(AMBER_LED_FILE, 0); - write_int(GREEN_LED_FILE, 1); - nanosleep(&t, NULL); - if(blink) write_int(GREEN_BLINK_FILE, blink); - LOGD("green on, blink:%d",blink); - } else { // Leds off - write_int(GREEN_LED_FILE, 0); - write_int(AMBER_LED_FILE, 0); - write_int(AMBER_BLINK_FILE, 0); - write_int(GREEN_BLINK_FILE, 0); - //start_battery_checker(); - LOGD("leds off"); - } - - LOGD("light-colors red:%d green:%d blue:%d",red,green,blue); -} - -// dan1j3l TODO: Refactor, and simplify this funct -static int set_speaker_light_locked(struct light_device_t* dev,struct light_state_t const* state){ - int blink; - - unsigned int colorRGB; - - LOGD("set_speaker_light_locked"); - - // Blink or solid - switch (state->flashMode) { - case LIGHT_FLASH_TIMED: - blink = 1; - break; - case LIGHT_FLASH_NONE: - blink = 0; - break; - default: - blink = 0; - break; - } - - // dan1j3l: TODO: simplify color detection - colorRGB = state->color; - - // dan1j3l BUG: on some android releases leds needs to be reseted by putting blink & brightness to 0 before enabling them - - blinkLed(colorRGB, blink); - - return 0; -} - -// dan1j3l TODO: Refactor this funct -static void handle_speaker_battery_locked(struct light_device_t* dev){ - - LOGD("handle_speaker_battery_locked"); - - if (is_lit(&g_battery)) { - set_speaker_light_locked(dev, &g_battery); - } else { - set_speaker_light_locked(dev, &g_notification); - } -} - -static int set_light_battery(struct light_device_t* dev,struct light_state_t const* state){ - - LOGD("set_light_battery"); - - pthread_mutex_lock(&g_lock); - g_battery = *state; - - handle_speaker_battery_locked(dev); - - pthread_mutex_unlock(&g_lock); - return 0; -} - -static int set_light_notifications(struct light_device_t* dev,struct light_state_t const* state){ - - LOGD("set_light_notifications"); - pthread_mutex_lock(&g_lock); - g_notification = *state; - - handle_speaker_battery_locked(dev); - - pthread_mutex_unlock(&g_lock); - return 0; -} - - -// dan1j3l BUG: Button lights ? -static int set_light_attention(struct light_device_t* dev,struct light_state_t const* state){ - pthread_mutex_lock(&g_lock); - -/* if (state->flashMode == LIGHT_FLASH_HARDWARE) { - g_attention = state->flashOnMS; - } else if (state->flashMode == LIGHT_FLASH_NONE) { - g_attention = 0; - } - - int mode = g_attention;*/ - - if(is_lit(state) && state->flashOnMS) { - do_blink_button_backlight(); - } else { - blink_button = 0; - } - LOGD("set_light_attention: %p(%d,%d,%d)\n", state->color, state->flashMode, state->flashOnMS, state->flashOffMS); - -/* if (mode == 7 && g_backlight) { - mode = 0; - }*/ - - pthread_mutex_unlock(&g_lock); - return 0; -} - -/** New stuff */ -// dan1j3l TODO: Verify each device and create led detection - -static int set_light_bluetooth(struct light_device_t* dev,struct light_state_t const* state){ - LOGD("set_light_bluetooth"); - return 0; -} - -static int set_light_wifi(struct light_device_t* dev,struct light_state_t const* state){ - LOGD("set_light_wifi"); - return 0; -} - -static int set_light_dualled(struct light_device_t* dev,struct light_state_t const* state){ - - pthread_mutex_lock(&g_lock); - - // we only care for blinking states atm - if(is_lit(state) && state->flashOnMS) { - blinkLed(state->color,1); - was_blinking_notification = 1; - } else if(was_blinking_notification && !is_lit(state)) { - blinkLed(0,0); - was_blinking_notification = 0; - } - LOGD("set_light_dualled: %p(%d,%d,%d)\n", state->color, state->flashMode, state->flashOnMS, state->flashOffMS); - - pthread_mutex_unlock(&g_lock); - return 0; - - - /* - // Blink or solid - switch (state->flashMode) { - case LIGHT_FLASH_TIMED: - blink = 1; - break; - case LIGHT_FLASH_HARDWARE: - blink = 1; - break; - case LIGHT_FLASH_NONE: - blink = 0; - break; - default: - blink = 0; - break; - } - - - LOGD("dl mode:%d, offMS:%d, OnMS:%d, brightness:%d",state->flashMode,state->flashOffMS,state->flashOnMS,state->brightnessMode); - - - // dan1j3l: TODO: simplify color detection - colorRGB = state->color; - red = (colorRGB >> 16) & 0xFF; - green = (colorRGB >> 8) & 0xFF; - blue = colorRGB & 0xFF; - - // dan1j3l TEST ON DIFFERENT BUILDS !!! - if (state->flashMode == LIGHT_FLASH_NONE && red == 0 && green == 0 && blue == 0){ - // turn amber on - LOGD("amber on"); - write_int(AMBER_BLINK_FILE,0); - write_int(AMBER_LED_FILE,1); - }else{ - //turn amber off - LOGD("amber off"); - write_int(AMBER_LED_FILE,0); - } - - - - LOGD("dl fullcolor:%d",state->color); - LOGD("dl-color red:%d green:%d blue:%d",red,green,blue); - - */ - - pthread_mutex_unlock(&g_lock); - return 0; -} - -// dan1j3l ??? -static int set_light_capsfunc(struct light_device_t* dev,struct light_state_t const* state){ - LOGD("set_light_capsfunc"); - return 0; -} - -// dan1j3l - Nothing interesting for leo -static int set_light_jogball(struct light_device_t* dev,struct light_state_t const* state){ - LOGD("set_light_jogball"); - return 0; -} - -/** ****************************************************/ - - -/** Close the lights device */ -static int close_lights(struct light_device_t *dev){ - if (dev) { - free(dev); - } - return 0; -} - - -/******************************************************************************/ - -/** - * module methods - */ - -/** Open a new instance of a lights device using name */ -static int open_lights(const struct hw_module_t* module, char const* name,struct hw_device_t** device){ - - int (*set_light)(struct light_device_t* dev,struct light_state_t const* state); - - if (0 == strcmp(LIGHT_ID_BACKLIGHT, name)) { - set_light = set_light_backlight; - LOGD("init backlight, id:%s",name); - } - else if (0 == strcmp(LIGHT_ID_KEYBOARD, name)) { //dan1j3l: Ignored on leo - set_light = set_light_keyboard; - LOGD("init keyboard, id:%s",name); - } - else if (0 == strcmp(LIGHT_ID_BUTTONS, name)) { - set_light = set_light_buttons; - LOGD("init buttons, id:%s",name); - } - else if (0 == strcmp(LIGHT_ID_BATTERY, name)) { - set_light = set_light_battery; - start_battery_checker(); // needed for sense builds - LOGD("init battery, id:%s",name); - } - else if (0 == strcmp(LIGHT_ID_NOTIFICATIONS, name)) { - set_light = set_light_notifications; - LOGD("init notifications, id:%s",name); - } - else if (0 == strcmp(LIGHT_ID_ATTENTION, name)) { - set_light = set_light_attention; - LOGD("init attention, id:%s",name); - } - else if (0 == strcmp(LIGHT_ID_BLUETOOTH, name)) { - set_light = set_light_bluetooth; - LOGD("init bluetooth, id:%s",name); - } - else if (0 == strcmp(LIGHT_ID_WIFI, name)) { - set_light = set_light_wifi; - LOGD("init wifi, id:%s",name); - } - else if (0 == strcmp(LIGHT_ID_DUALLED, name)) { - set_light = set_light_dualled; - LOGD("init dualled, id:%s",name); - } - else if (0 == strcmp(LIGHT_ID_CAPSFUNC, name)) { // dan1j3l: what is capsfunc ? - set_light = set_light_capsfunc; - LOGD("init capsfunc, id:%s",name); - } - else if (0 == strcmp(LIGHT_ID_JOGBALL, name)) { // dan1j3l: is needed in leo ? - set_light = set_light_jogball; - LOGD("init jogball, id:%s",name); - } - else { - LOGD("Undefined device, id:%s",name); - return -EINVAL; - } - - - pthread_once(&g_init, init_globals); - - struct light_device_t *dev = malloc(sizeof(struct light_device_t)); - memset(dev, 0, sizeof(*dev)); - - dev->common.tag = HARDWARE_DEVICE_TAG; - dev->common.version = 0; - dev->common.module = (struct hw_module_t*)module; - dev->common.close = (int (*)(struct hw_device_t*))close_lights; - dev->set_light = set_light; - - *device = (struct hw_device_t*)dev; - return 0; -} - - -static struct hw_module_methods_t lights_module_methods = { - .open = open_lights, -}; - -/* - * The lights Module - */ -const struct hw_module_t HAL_MODULE_INFO_SYM = { - .tag = HARDWARE_MODULE_TAG, - .version_major = 1, - .version_minor = 0, - .id = LIGHTS_HARDWARE_MODULE_ID, - .name = "HTC Leo Lights module", - .author = "dan1j3l", - .methods = &lights_module_methods, -}; diff --git a/liblights/lights.c-mahimahi b/liblights/lights.c-mahimahi deleted file mode 100755 index bec21e0..0000000 --- a/liblights/lights.c-mahimahi +++ /dev/null @@ -1,635 +0,0 @@ -/* - * Copyright (C) 2009 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#define LOG_TAG "lights" - -#include - -#include -#include -#include -#include -#include -#include - -#include -#include - -#include - -#define LIGHT_ATTENTION 1 -#define LIGHT_NOTIFY 2 - -/******************************************************************************/ -static struct light_state_t *g_notify; -static struct light_state_t *g_attention; - -static pthread_once_t g_init = PTHREAD_ONCE_INIT; -static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER; - -static int g_backlight = 255; -static int g_buttons = 0; - -struct led_prop { - const char *filename; - int fd; -}; - -struct led { - struct led_prop mode; - struct led_prop brightness; - struct led_prop blink; - struct led_prop color; - struct led_prop period; -}; - -enum { - JOGBALL_LED, - BUTTONS_LED, - AMBER_LED, - GREEN_LED, - BLUE_LED, - RED_LED, - LCD_BACKLIGHT, - NUM_LEDS, -}; - -struct led leds[NUM_LEDS] = { -// [JOGBALL_LED] = { -// .brightness = { "/sys/class/leds/jogball-backlight/brightness", 0}, -// .color = { "/sys/class/leds/jogball-backlight/color", 0}, -// .period = { "/sys/class/leds/jogball-backlight/period", 0}, -// }, - [BUTTONS_LED] = { - .brightness = { "/sys/class/leds/button-backlight/brightness", 0}, - }, -// [RED_LED] = { -// .brightness = { "/sys/class/leds/red/brightness", 0}, -// .blink = { "/sys/class/leds/red/blink", 0}, -// }, - [GREEN_LED] = { - .brightness = { "/sys/class/leds/green/brightness", 0}, - .blink = { "/sys/class/leds/green/blink", 0}, - }, -// [BLUE_LED] = { -// .brightness = { "/sys/class/leds/blue/brightness", 0}, -// .blink = { "/sys/class/leds/blue/blink", 0}, -// }, - [AMBER_LED] = { - .brightness = { "/sys/class/leds/amber/brightness", 0}, - .blink = { "/sys/class/leds/amber/blink", 0}, - }, - [LCD_BACKLIGHT] = { - .brightness = { "/sys/class/leds/lcd-backlight/brightness", 0}, - }, -}; - -enum { - RGB_BLACK = 0x000000, - RGB_RED = 0xFF0000, - RGB_AMBER = 0xFFFF00, /* note this is actually RGB yellow */ - RGB_GREEN = 0x00FF00, - RGB_BLUE = 0x0000FF, - RGB_WHITE = 0xFFFFFF, - RGB_PINK = 0xFFC0CB, - RGB_ORANGE = 0xFFA500, - RGB_YELLOW = 0xFFFF00, - RGB_PURPLE = 0x800080, - RGB_LT_BLUE = 0xADD8E6, -}; - -/** - * device methods - */ - -static int init_prop(struct led_prop *prop) -{ - int fd; - - prop->fd = -1; - if (!prop->filename) - return 0; - fd = open(prop->filename, O_RDWR); - if (fd < 0) { - LOGE("init_prop: %s cannot be opened (%s)\n", prop->filename, - strerror(errno)); - return -errno; - } - - prop->fd = fd; - return 0; -} - -static void close_prop(struct led_prop *prop) -{ - int fd; - - if (prop->fd > 0) - close(prop->fd); - return; -} - -void init_globals(void) -{ - int i; - pthread_mutex_init(&g_lock, NULL); - - for (i = 0; i < NUM_LEDS; ++i) { - init_prop(&leds[i].brightness); - init_prop(&leds[i].blink); -// init_prop(&leds[i].mode); -// init_prop(&leds[i].color); -// init_prop(&leds[i].period); - } - g_attention = malloc(sizeof(struct light_state_t)); - memset(g_attention, 0, sizeof(*g_attention)); - g_notify = malloc(sizeof(struct light_state_t)); - memset(g_notify, 0, sizeof(*g_notify)); -} - -static int -write_int(struct led_prop *prop, int value) -{ - char buffer[20]; - int bytes; - int amt; - - if (prop->fd < 0) - return 0; - - LOGV("%s %s: 0x%x\n", __func__, prop->filename, value); - - bytes = snprintf(buffer, sizeof(buffer), "%d\n", value); - while (bytes > 0) { - amt = write(prop->fd, buffer, bytes); - if (amt < 0) { - if (errno == EINTR) - continue; - return -errno; - } - bytes -= amt; - } - - return 0; -} - -static int -write_rgb(struct led_prop *prop, int red, int green, int blue) -{ - char buffer[20]; - int bytes; - int amt; - - if (prop->fd < 0) - return 0; - - LOGV("%s %s: red:%d green:%d blue:%d\n", - __func__, prop->filename, red, green, blue); - - bytes = snprintf(buffer, sizeof(buffer), "%d %d %d\n", red, green, blue); - while (bytes > 0) { - amt = write(prop->fd, buffer, bytes); - if (amt < 0) { - if (errno == EINTR) - continue; - return -errno; - } - bytes -= amt; - } - - return 0; -} - -static unsigned int -set_rgb(int red, int green, int blue) -{ - return(((red << 16) & 0x00ff0000) | - ((green << 8) & 0x0000ff00) | - (blue & 0x000000ff)); -} - -static int -is_lit(struct light_state_t const* state) -{ - return state->color & 0x00ffffff; -} - -static int -set_button_light(struct light_state_t const* state) -{ - static int button_mode = 0; - int rc = 0; - int mode = state->flashMode; - int red, blue, green; - int period = 0; - - if (state->flashMode == LIGHT_FLASH_HARDWARE) { - mode = state->flashOnMS; - period = state->flashOffMS; - } - -// LOGD("%s color=%08x mode=%d period %d\n", __func__,state->color, mode, period); - - if (mode != 0) { - write_int(&leds[BUTTONS_LED].brightness,1); - } - // If the value isn't changing, don't set it, because this - // can reset the timer on the breathing mode, which looks bad. - if (button_mode == mode) { - return 0; - } - button_mode = mode; - - LOGD("set_button_light mode:%d",mode); - - - return write_int(&leds[BUTTONS_LED].brightness, mode); -} - -static void -handle_button_light_locked(int type) -{ - LOGD("handle_button_light_locked"); - - struct light_state_t *new_state = 0; - int attn_mode = 0; - - if (g_attention->flashMode == LIGHT_FLASH_HARDWARE) - attn_mode = g_attention->flashOnMS; - - //LOGD("%s type %d attention %p notify %p\n",__func__, type, g_attention, g_notify); - - switch (type) { - case LIGHT_ATTENTION: { - if (attn_mode == 0) { - /* go back to notify state */ - new_state = g_notify; - } else { - new_state = g_attention; - } - break; - } - case LIGHT_NOTIFY: { - if (attn_mode != 0) { - /* attention takes priority over notify state */ - new_state = g_attention; - } else { - new_state = g_notify; - } - break; - } - } - if (new_state == 0) { - LOGD("%s: unknown type (%d)\n", __func__, type); - return; - } - LOGD("%s new state %p\n", __func__, new_state); - - set_button_light(new_state); - return; -} - -static int -rgb_to_brightness(struct light_state_t const* state) -{ - int color = state->color & 0x00ffffff; - return ((77*((color>>16)&0x00ff)) - + (150*((color>>8)&0x00ff)) + (29*(color&0x00ff))) >> 8; -} - -static int -set_light_backlight(struct light_device_t* dev, - struct light_state_t const* state) -{ - LOGD("set_light_backlight"); - int err = 0; - int brightness = rgb_to_brightness(state); - //LOGD("%s brightness=%d color=0x%08x",__func__,brightness, state->color); - pthread_mutex_lock(&g_lock); - g_backlight = brightness; - err = write_int(&leds[LCD_BACKLIGHT].brightness, brightness); - pthread_mutex_unlock(&g_lock); - return err; -} - -static int -set_light_keyboard(struct light_device_t* dev, - struct light_state_t const* state) -{ - /* nothing to do on mahimahi*/ - return 0; -} - -static int -set_light_buttons(struct light_device_t* dev, - struct light_state_t const* state) -{ - LOGD("set_light_buttons"); - int err = 0; - int on = is_lit(state); - pthread_mutex_lock(&g_lock); - g_buttons = on; - err = write_int(&leds[BUTTONS_LED].brightness, on?255:0); - pthread_mutex_unlock(&g_lock); - return err; -} - -static int -set_speaker_light_locked(struct light_device_t* dev, - struct light_state_t const* state) -{ - LOGD("set_speaker_light_locked"); - int len; - unsigned int colorRGB; - - /* Red = amber_led, blue or green = green_led */ - colorRGB = state->color & 0xFFFFFF; - - - switch (state->flashMode) { - case LIGHT_FLASH_TIMED: - switch (colorRGB) { - case RGB_RED: - LOGD("flash red"); - write_int(&leds[RED_LED].blink, 1); - break; - case RGB_AMBER: - LOGD("flash amber"); - write_int(&leds[AMBER_LED].blink, 2); - break; - case RGB_GREEN: - LOGD("flash green"); - write_int(&leds[GREEN_LED].blink, 1); - break; - case RGB_BLUE: - LOGD("flash blue"); - write_int(&leds[BLUE_LED].blink, 1); - break; - case RGB_BLACK: /*off*/ - LOGD("flash off"); - write_int(&leds[GREEN_LED].blink, 0); - write_int(&leds[AMBER_LED].blink, 0); - break; - default: - break; - } - break; - - case LIGHT_FLASH_NONE: - switch (colorRGB) { - case RGB_AMBER: - LOGD("solid amber"); - write_int(&leds[GREEN_LED].brightness, 0); - write_int(&leds[AMBER_LED].blink, 0); - write_int(&leds[AMBER_LED].brightness, 1); - break; - case RGB_GREEN: - LOGD("solid green"); - write_int(&leds[AMBER_LED].brightness, 0); - write_int(&leds[GREEN_LED].blink, 0); - write_int(&leds[GREEN_LED].brightness, 1); - break; - case RGB_BLACK: /*off*/ - LOGD("all off"); - write_int(&leds[GREEN_LED].brightness, 0); - write_int(&leds[AMBER_LED].brightness, 0); - break; - default: - break; - } - break; - default: - break; - } - return 0; -} - -static int -set_light_battery(struct light_device_t* dev, - struct light_state_t const* state) -{ - pthread_mutex_lock(&g_lock); - LOGD("set_light_battery"); - LOGD("%s mode=%d color=0x%08x", - __func__,state->flashMode, state->color); - - set_speaker_light_locked(dev, state); - - pthread_mutex_unlock(&g_lock); - return 0; -} - -static int -set_light_notifications(struct light_device_t* dev, - struct light_state_t const* state) -{ - pthread_mutex_lock(&g_lock); - - LOGD("set_light_notification"); - - LOGD("%s mode=%d color=0x%08x On=%d Off=%d\n", - __func__,state->flashMode, state->color, - state->flashOnMS, state->flashOffMS); - /* - ** TODO Allow for user settings of color and interval - ** Setting 60% brightness - */ - switch (state->color & 0x00FFFFFF) { - case RGB_BLACK: - g_notify->color = set_rgb(0, 0, 0); - break; - case RGB_WHITE: - g_notify->color = set_rgb(50, 127, 48); - break; - case RGB_RED: - g_notify->color = set_rgb(141, 0, 0); - break; - case RGB_GREEN: - g_notify->color = set_rgb(0, 141, 0); - break; - case RGB_BLUE: - g_notify->color = set_rgb(0, 0, 141); - break; - case RGB_PINK: - g_notify->color = set_rgb(141, 52, 58); - break; - case RGB_PURPLE: - g_notify->color = set_rgb(70, 0, 70); - break; - case RGB_ORANGE: - g_notify->color = set_rgb(141, 99, 0); - break; - case RGB_YELLOW: - g_notify->color = set_rgb(100, 141, 0); - break; - case RGB_LT_BLUE: - g_notify->color = set_rgb(35, 55, 98); - break; - default: - g_notify->color = state->color; - break; - } - - if (state->flashMode != LIGHT_FLASH_NONE) { - g_notify->flashMode = LIGHT_FLASH_HARDWARE; - g_notify->flashOnMS = 7; - g_notify->flashOffMS = (state->flashOnMS + state->flashOffMS)/1000; - } else { - g_notify->flashOnMS = 0; - g_notify->flashOffMS = 0; - } - handle_button_light_locked(LIGHT_NOTIFY); - - pthread_mutex_unlock(&g_lock); - return 0; -} - -static int -set_light_attention(struct light_device_t* dev, - struct light_state_t const* state) -{ - unsigned int colorRGB; - - - LOGD("set_light_attention"); - LOGD("%s color=0x%08x mode=0x%08x submode=0x%08x", - __func__, state->color, state->flashMode, state->flashOnMS); - - pthread_mutex_lock(&g_lock); - /* tune color for hardware*/ - switch (state->color & 0x00FFFFFF) { - case RGB_WHITE: - colorRGB = set_rgb(101, 255, 96); - break; - case RGB_BLUE: - colorRGB = set_rgb(0, 0, 235); - break; - case RGB_BLACK: - colorRGB = set_rgb(0, 0, 0); - break; - default: - LOGD("%s colorRGB=%08X, unknown color\n", - __func__, state->color); - colorRGB = set_rgb(101, 255, 96); - break; - } - - g_attention->flashMode = state->flashMode; - g_attention->flashOnMS = state->flashOnMS; - g_attention->color = colorRGB; - g_attention->flashOffMS = 0; - handle_button_light_locked(LIGHT_ATTENTION); - - pthread_mutex_unlock(&g_lock); - return 0; -} - - -/** Close the lights device */ -static int -close_lights(struct light_device_t *dev) -{ - int i; - - LOGD("close_lights"); - for (i = 0; i < NUM_LEDS; ++i) { - close_prop(&leds[i].brightness); - close_prop(&leds[i].blink); - close_prop(&leds[i].mode); - } - - if (dev) { - free(dev); - } - return 0; -} - - -/******************************************************************************/ - -/** - * module methods - */ - -/** Open a new instance of a lights device using name */ -static int open_lights(const struct hw_module_t* module, char const* name, - struct hw_device_t** device) -{ - int (*set_light)(struct light_device_t* dev, - struct light_state_t const* state); - - if (0 == strcmp(LIGHT_ID_BACKLIGHT, name)) { - set_light = set_light_backlight; - LOGD("init backlight"); - } - else if (0 == strcmp(LIGHT_ID_KEYBOARD, name)) { - set_light = set_light_keyboard; - LOGD("init keyboard"); - } - else if (0 == strcmp(LIGHT_ID_BUTTONS, name)) { - set_light = set_light_buttons; - LOGD("init buttons"); - } - else if (0 == strcmp(LIGHT_ID_BATTERY, name)) { - set_light = set_light_battery; - LOGD("init battery"); - } - else if (0 == strcmp(LIGHT_ID_NOTIFICATIONS, name)) { - set_light = set_light_notifications; - LOGD("init notifications"); - } - else if (0 == strcmp(LIGHT_ID_ATTENTION, name)) { - set_light = set_light_attention; - LOGD("init attention"); - } - else { - return -EINVAL; - } - - pthread_once(&g_init, init_globals); - - struct light_device_t *dev = malloc(sizeof(struct light_device_t)); - memset(dev, 0, sizeof(*dev)); - - LOGD("hw device tag:%d",HARDWARE_DEVICE_TAG); - - dev->common.tag = HARDWARE_DEVICE_TAG; - dev->common.version = 0; - dev->common.module = (struct hw_module_t*)module; - dev->common.close = (int (*)(struct hw_device_t*))close_lights; - dev->set_light = set_light; - - *device = (struct hw_device_t*)dev; - return 0; -} - - -static struct hw_module_methods_t lights_module_methods = { - .open = open_lights, -}; - -/* - * The lights Module - */ -const struct hw_module_t HAL_MODULE_INFO_SYM = { - .tag = HARDWARE_MODULE_TAG, - .version_major = 1, - .version_minor = 0, - .id = LIGHTS_HARDWARE_MODULE_ID, - .name = "mahimahi lights Module", - .author = "Google, Inc.", - .methods = &lights_module_methods, -}; diff --git a/liblights/lights_leo.c b/liblights/lights_leo.c new file mode 100644 index 0000000..373f443 --- /dev/null +++ b/liblights/lights_leo.c @@ -0,0 +1,656 @@ +/* + * Copyright (C) 2009 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define LOG_TAG "lights_leo" + +#include +#include + +#include +#include +#include +#include +#include +#include + +#include +#include + +#include + +#include +#include +#include + +#include "events.h" + +#define LIGHT_ATTENTION 1 +#define LIGHT_NOTIFY 2 + +//#define ENABLE_LCDSAVE +//#define ENABLE_BATTERY_POOL + +#define ENABLE_RADIO_POOL + +#define LED_DEBUG 1 + +#if LED_DEBUG +# define D(...) LOGD(__VA_ARGS__) +#else +# define D(...) ((void)0) +#endif + +/******************************************************************************/ +static struct light_state_t *g_notify; +static struct light_state_t *g_attention; +static pthread_once_t g_init = PTHREAD_ONCE_INIT; +static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER; + +#ifdef ENABLE_LCDSAVE +static int g_current_backlight = 0; +#endif + +static int g_ts = 0; +static int g_backlight = 255; +static int g_buttons = 0; + +struct led_prop { + const char *filename; + int fd; + int value; +}; + +struct led { + struct led_prop brightness; + struct led_prop blink; +}; + +enum { + BUTTONS_LED, + GREEN_LED, + AMBER_LED, + LCD_BACKLIGHT, + NUM_LEDS, +}; + +struct led leds[NUM_LEDS] = { + [BUTTONS_LED] = { + .brightness = { "/sys/class/leds/button-backlight/brightness", 0, 0}, + .blink = {NULL, 0, 0}, + }, + [GREEN_LED] = { + .brightness = { "/sys/class/leds/green/brightness", 0, 0}, + .blink = { "/sys/class/leds/green/blink", 0, 0}, + }, + [AMBER_LED] = { + .brightness = { "/sys/class/leds/amber/brightness", 0, 0}, + .blink = { "/sys/class/leds/amber/blink", 0, 0}, + }, + [LCD_BACKLIGHT] = { + .brightness = { "/sys/class/leds/lcd-backlight/brightness", 0, 0}, + .blink = {NULL, 0, 0}, + }, +}; + +/** + * device methods + */ + +static int init_prop(struct led_prop *prop) +{ + int fd; + + prop->fd = -1; + if (!prop->filename) + return 0; + fd = open(prop->filename, O_RDWR); + if (fd < 0) { + LOGE("init_prop: %s cannot be opened (%s)\n", prop->filename, + strerror(errno)); + return -errno; + } + + prop->fd = fd; + return 0; +} + +static void close_prop(struct led_prop *prop) +{ + int fd; + + if (prop->fd > 0) + close(prop->fd); + return; +} + +void init_globals(void) +{ + int i; + pthread_mutex_init(&g_lock, NULL); + + for (i = 0; i < NUM_LEDS; ++i) { + init_prop(&leds[i].brightness); + if (leds[i].blink.filename) { + init_prop(&leds[i].blink); + } + } + g_attention = malloc(sizeof(struct light_state_t)); + memset(g_attention, 0, sizeof(*g_attention)); + g_notify = malloc(sizeof(struct light_state_t)); + memset(g_notify, 0, sizeof(*g_notify)); +} + +static int +write_int(struct led_prop *prop, int value) +{ + char buffer[20]; + int bytes; + int amt; + + if (prop->fd < 0) + return 0; + if (prop->value != value) { + //LOGV("%s %s: 0x%x\n", __func__, prop->filename, value); + bytes = snprintf(buffer, sizeof(buffer), "%d\n", value); + while (bytes > 0) { + amt = write(prop->fd, buffer, bytes); + if (amt < 0) { + if (errno == EINTR) + continue; + return -errno; + } + bytes -= amt; + } + prop->value = value; + } + return 0; +} + +static int +is_lit(struct light_state_t const* state) +{ + return state->color & 0x00ffffff; +} + +static int +rgb_to_brightness(struct light_state_t const* state) +{ + int color = state->color & 0x00ffffff; + return ((77*((color>>16)&0x00ff)) + + (150*((color>>8)&0x00ff)) + (29*(color&0x00ff))) >> 8; +} + +//===================================================================================== +#ifdef ENABLE_BATTERY_POLL +static pthread_t battery_check_t = 0; +static int last_battery_state = 0; + +void *battery_state_thread(void *arg){ + int fd,size, rs; + char state[20]; + struct timespec t; + t.tv_nsec = 0; + t.tv_sec = 5; + + fd = open("/sys/class/power_supply/battery/status",O_RDONLY | O_NDELAY); + if(fd < 0) { + LOGE("Couldn't open /sys/class/power_supply/battery/status\n"); + return 0; + } + + for (;;) { + memset(&state[0], 0, sizeof(state)); + + read(fd, state,20); + close(fd); + + rs=0; + rs = sprintf(state,"%s",state); + + if ( last_battery_state != rs){ + last_battery_state=rs; + + if ( rs == 9){ // Charging + write_int(&leds[GREEN_LED].brightness, 0); + write_int(&leds[AMBER_LED].brightness, 1); + write_int(&leds[AMBER_LED].blink, 0); + }else if(rs == 5){ // FULL + write_int(&leds[GREEN_LED].brightness, 1); + write_int(&leds[AMBER_LED].brightness, 0); + write_int(&leds[GREEN_LED].blink, 0); + } + + } + + nanosleep(&t,NULL); + } + + close(fd); + return 0; +} + +void start_battery_thread(){ + if (battery_check_t == 0) //ensure only 1 thread + pthread_create(&battery_check_t, NULL, battery_state_thread, NULL); + return; +} +#endif +//===================================================================================== +#ifdef ENABLE_RADIO_POOL +static int last_radio_state = 0; +#endif + +static pthread_t events_ct = 0; +static time_t keys_tm; +#ifdef ENABLE_LCDSAVE +static time_t abs_tm; +static time_t last_activity_tm; + +static int user_activity_idle() { + int fd; + int ret =0; + char tmp[20]; + + fd= open("/sys/android_power/auto_off_timeout",O_RDONLY); + if(fd < 0) { + LOGE("Couldn't open /sys/android_power/auto_off_timeout\n"); + return 0; + } + + memset(&tmp[0], 0, sizeof(tmp)); + read(fd, tmp,20); + + D("@@ %s->%s\n", __func__, tmp); + + close(fd); + return (ret); +} +#endif + +static int +switch_led_button(int on) { + int err = 0; + if (g_buttons!=on) { + //D("@@ %s->%s\n", __func__, g_buttons?"ON":"OFF"); + err = write_int(&leds[BUTTONS_LED].brightness, on); + keys_tm = time(NULL) + (8*on); // switch off button keypad after 8 seconds + g_buttons = on; + } + return err; +} + +static int +set_led_backlight(int level) { + int err = 0; + //D("%s: [%d %d %d]\n", __func__, level, g_backlight, g_current_backlight); + if (g_backlight != level ){ + err = write_int(&leds[LCD_BACKLIGHT].brightness, level); + g_backlight = level; + } +#ifdef ENABLE_LCDSAVE + if (level>=g_current_backlight){ + g_current_backlight=level; + abs_tm = time(NULL) + (10); + } +#endif + return err; +} + +void *events_cthread(void *arg) { +#ifdef ENABLE_RADIO_POOL + int radio_state = 0; + char sim_state[PROPERTY_VALUE_MAX]; +#endif + struct input_event ev; + struct timespec t; + time_t a_tm, k_tm; + //int fd; + t.tv_nsec= 10000*1000; + t.tv_sec = 0; + + ev_init(); + + for (;;) { + /* radio events tracking */ +#ifdef ENABLE_RADIO_POOL + radio_state = 0; + if (property_get("gsm.sim.state", sim_state, NULL) && (strcmp(sim_state, "READY")==0)) { + radio_state = 1; + } + //radio state changed + if ( last_radio_state != radio_state){ + D("@@ %s: |%s| %d->%d\n", __func__, sim_state, last_radio_state, radio_state ); + //green blink if radio is on + write_int(&leds[AMBER_LED].brightness, radio_state?0:1); + write_int(&leds[GREEN_LED].brightness, radio_state?1:0); + write_int(&leds[GREEN_LED].blink, radio_state?1:0); + last_radio_state=radio_state; + } +#endif + /* battery events tracking */ + + /* button events tracking */ + ev_get(&ev, 1); + if (ev.type==EV_KEY) { + if (ev.value == 1){ + switch (ev.code) { + case BTN_TOUCH: +#ifdef ENABLE_LCDSAVE + if (g_backlight > 0) && (abs_tm==0) + set_led_backlight(g_current_backlight); + } +#endif + break; + case KEY_SEND: + case KEY_MENU: + case KEY_HOME: + case KEY_BACK: + case KEY_END: + case KEY_POWER: +#ifdef ENABLE_LCDSAVE + if (abs_tm==0) {set_led_backlight(g_current_backlight);} +#endif + switch_led_button(1); + sleep(1); + break; +#ifdef ENABLE_LCDSAVE + case KEY_VOLUMEUP: + case KEY_VOLUMEDOWN: + if (abs_tm==0) { set_led_backlight(g_current_backlight);} + break; +#endif + default: + /*LOGD("keys: code %d, value %d\n", ev.code, ev.value);*/ + sleep(1); + break; + } + } + } +//========================= +#ifdef ENABLE_LCDSAVE + if (g_backlight > 0) { + a_tm = time(NULL); + //D("%ld %ld\n", a_tm, abs_tm); + if ((abs_tm>0) && (a_tm >= abs_tm) && (user_activity_idle())) { + //D("LCD_BACKLIGHT->down brightness\n"); + set_led_backlight(50); + //write_int(&leds[LCD_BACKLIGHT].brightness, 50); + abs_tm = 0; + } + } +#endif + k_tm = time(NULL); + //D("[%ld][%ld][%d][%d %d %d][%d]\n", tm, keys_tm, ev.type, ev.code, ev.value, g_buttons, g_backlight); + if (g_buttons==1) { + if (k_tm == keys_tm || g_backlight == 0){ + switch_led_button(0); + sleep(1); + } + } +//========================= + nanosleep(&t,NULL); //avoid 100% CPU usage by system_server + } + + ev_exit(); + events_ct = 0; + return 0; +} + +void start_events_thread() { + if (events_ct == 0) //ensure only 1 thread + pthread_create(&events_ct, NULL, events_cthread, NULL); + return; +} +//================================================================================================= +static int +set_light_backlight(struct light_device_t* dev, + struct light_state_t const* state) { + int err = 0; + int brightness = rgb_to_brightness(state); + LOGV("%s brightness=%d color=0x%08x", + __func__,brightness, state->color); + pthread_mutex_lock(&g_lock); + set_led_backlight(brightness); + pthread_mutex_unlock(&g_lock); + return err; +} + +static int +set_light_buttons(struct light_device_t* dev, + struct light_state_t const* state) { + int err = 0; + int on = is_lit(state); + pthread_mutex_lock(&g_lock); + LOGV("%s mode=%d color=0x%08x", + __func__,state->flashMode, state->color); + switch_led_button(on); + pthread_mutex_unlock(&g_lock); + return err; +} + +static int +set_speaker_light_locked(struct light_device_t* dev, + struct light_state_t const* state) { + int len; + unsigned int colorRGB; + + colorRGB = state->color & 0xFFFFFF; + + D("@@ %s colorRGB=%08X, state->flashMode:%d\n", __func__, colorRGB, state->flashMode); + + /*if (colorRGB ==0) return 0;*/ + + int red = (colorRGB >> 16)&0xFF; + int green = (colorRGB >> 8)&0xFF; + int blue = (colorRGB) & 0xFF; + int g_blink = 0; + + switch (state->flashMode) { + case LIGHT_FLASH_HARDWARE: + g_blink = 3; + break; + case LIGHT_FLASH_TIMED: + g_blink = 1; + break; + case LIGHT_FLASH_NONE: + g_blink = 0; + break; + default: + LOGE("set_led_state colorRGB=%08X, unknown mode %d\n", + colorRGB, state->flashMode); + break; + } + + if (red) { + D("@@ %s AMBER, blink: %d\n", __func__, g_blink); + write_int(&leds[GREEN_LED].brightness, 0); + write_int(&leds[AMBER_LED].brightness, 1); + write_int(&leds[AMBER_LED].blink, g_blink); + } else if (green) { + D("@@ %s GREEN, blink: %d\n", __func__, g_blink); + write_int(&leds[AMBER_LED].brightness, 0); + write_int(&leds[GREEN_LED].brightness, 1); + write_int(&leds[GREEN_LED].blink, g_blink); + } else { + write_int(&leds[GREEN_LED].brightness, 0); + write_int(&leds[GREEN_LED].blink, 0); + write_int(&leds[AMBER_LED].brightness, 0); + write_int(&leds[AMBER_LED].blink, 0); + } + + return 0; +} + +static int +set_light_battery(struct light_device_t* dev, + struct light_state_t const* state) { + pthread_mutex_lock(&g_lock); + LOGV("%s mode=%d color=0x%08x", + __func__,state->flashMode, state->color); + set_speaker_light_locked(dev, state); + pthread_mutex_unlock(&g_lock); + return 0; +} + +static int +set_light_notifications(struct light_device_t* dev, + struct light_state_t const* state) { + pthread_mutex_lock(&g_lock); + LOGV("%s mode=%d color=0x%08x", + __func__,state->flashMode, state->color); + pthread_mutex_unlock(&g_lock); + return 0; +} + +static int +set_light_attention(struct light_device_t* dev, + struct light_state_t const* state) { + LOGV("%s color=0x%08x mode=0x%08x submode=0x%08x", + __func__, state->color, state->flashMode, state->flashOnMS); + pthread_mutex_lock(&g_lock); +/* +/lights_leo( 252): set_light_attention color=0x00ffffff mode=0x00000002 submode=0x00000003 +/lights_leo( 252): set_light_attention color=0x00000000 mode=0x00000002 submode=0x00000000 +/lights_leo( 252): set_light_attention color=0x00ffffff mode=0x00000002 submode=0x00000007 +/lights_leo( 252): set_light_attention color=0x00ffffff mode=0x00000000 submode=0x00000000 +*/ +#if 0 + if (state->flashMode==2 && state->flashOnMS==7){ + write_int(&leds[GREEN_LED].brightness, 0); + write_int(&leds[AMBER_LED].brightness, 1); + write_int(&leds[AMBER_LED].blink, 2); + } else { + write_int(&leds[AMBER_LED].brightness, 0); + write_int(&leds[GREEN_LED].brightness, 1); + write_int(&leds[GREEN_LED].blink, 4); + } +#endif + pthread_mutex_unlock(&g_lock); + return 0; +} + +static int +set_light_flashlight(struct light_device_t* dev, + struct light_state_t const* state) { + LOGV("%s color=0x%08x mode=0x%08x submode=0x%08x", + __func__, state->color, state->flashMode, state->flashOnMS); + + return 0; +} + +static int +set_light_function(struct light_device_t* dev, + struct light_state_t const* state) { + LOGV("%s color=0x%08x mode=0x%08x submode=0x%08x", + __func__, state->color, state->flashMode, state->flashOnMS); + + return 0; +} + +/** Close the lights device */ +static int +close_lights(struct light_device_t *dev) +{ + int i; + + for (i = 0; i < NUM_LEDS; ++i) { + close_prop(&leds[i].brightness); + close_prop(&leds[i].blink); + } + + if (dev) { + free(dev); + } + return 0; +} + + +/******************************************************************************/ + +/** + * module methods + */ + +/** Open a new instance of a lights device using name */ +static int open_lights(const struct hw_module_t* module, char const* name, + struct hw_device_t** device) +{ + int (*set_light)(struct light_device_t* dev, + struct light_state_t const* state); + + LOGV("%s name=%s", __func__, name); + + if (0 == strcmp(LIGHT_ID_BACKLIGHT, name)) { + set_light = set_light_backlight; + } + else if (0 == strcmp(LIGHT_ID_BUTTONS, name)) { + set_light = set_light_buttons; + start_events_thread(); + } + else if (0 == strcmp(LIGHT_ID_BATTERY, name)) { + set_light = set_light_battery; +#ifdef ENABLE_BATTERY_POLL + start_battery_thread(); +#endif + } + else if (0 == strcmp(LIGHT_ID_NOTIFICATIONS, name)) { + set_light = set_light_notifications; + } + else if (0 == strcmp(LIGHT_ID_ATTENTION, name)) { + set_light = set_light_attention; + } + else if (0 == strcmp(LIGHT_ID_FLASHLIGHT, name)) { + set_light = set_light_flashlight; + } + else if (0 == strcmp(LIGHT_ID_FUNC, name)) { + set_light = set_light_function; + } + else { + return -EINVAL; + } + + pthread_once(&g_init, init_globals); + + struct light_device_t *dev = malloc(sizeof(struct light_device_t)); + memset(dev, 0, sizeof(*dev)); + + dev->common.tag = HARDWARE_DEVICE_TAG; + dev->common.version = 0; + dev->common.module = (struct hw_module_t*)module; + dev->common.close = (int (*)(struct hw_device_t*))close_lights; + dev->set_light = set_light; + + *device = (struct hw_device_t*)dev; + return 0; +} + + +static struct hw_module_methods_t lights_module_methods = { + .open = open_lights, +}; + +/* + * The lights Module + */ +const struct hw_module_t HAL_MODULE_INFO_SYM = { + .tag = HARDWARE_MODULE_TAG, + .version_major = 1, + .version_minor = 0, + .id = LIGHTS_HARDWARE_MODULE_ID, + .name = "htcleo lights Module", + .author = "Google, Inc.", + .methods = &lights_module_methods, +}; diff --git a/libsensors/Android.mk b/libsensors/Android.mk old mode 100755 new mode 100644 index da67174..cc62133 --- a/libsensors/Android.mk +++ b/libsensors/Android.mk @@ -27,7 +27,7 @@ LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/hw LOCAL_MODULE_TAGS := optional -LOCAL_SRC_FILES := sensors.c +LOCAL_SRC_FILES := leosensors.c LOCAL_SHARED_LIBRARIES := liblog libcutils LOCAL_PRELINK_MODULE := false diff --git a/libsensors/MODULE_LICENSE_APACHE2 b/libsensors/MODULE_LICENSE_APACHE2 old mode 100755 new mode 100644 diff --git a/libsensors/NOTICE b/libsensors/NOTICE old mode 100755 new mode 100644 diff --git a/libsensors/sensors.c b/libsensors/leosensors.c old mode 100755 new mode 100644 similarity index 97% rename from libsensors/sensors.c rename to libsensors/leosensors.c index a39b457..ce8062d --- a/libsensors/sensors.c +++ b/libsensors/leosensors.c @@ -1,5 +1,4 @@ /* - * Copyright (C) 2010 Danijel Posilovic - dan1j3l * Copyright (C) 2008 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -15,7 +14,7 @@ * limitations under the License. */ -#define LOG_TAG "Sensors" +#define LOG_TAG "sensors_leo" #define LOG_NDEBUG 1 @@ -40,7 +39,7 @@ #define __MAX(a,b) ((a)>=(b)?(a):(b)) /*****************************************************************************/ - +#define TEMP_AZIMUTH_HACK 1 #define MAX_NUM_SENSORS 6 #define SUPPORTED_SENSORS ((1<akmd_fd <= 0) { return -1; } - short delay = ms; - if (!ioctl(dev->akmd_fd, ECS_IOCTL_APP_SET_DELAY, &delay)) { + g_delay = ms; + if (!ioctl(dev->akmd_fd, ECS_IOCTL_APP_SET_DELAY, &g_delay)) { return -errno; } return 0; @@ -703,6 +679,19 @@ static int pick_sensor(struct sensors_data_context_t *dev, return -1; } +static uint32_t fix_azimuth(uint32_t value) { + uint32_t ret=value; +#ifdef TEMP_AZIMUTH_HACK + if (ret>=271 && ret<=360) { + ret=ret-270; + } else if (ret>=0 && ret<=270) { + ret=ret+90; + } + //LOGD("%s: %-5d->%-5d ", __func__, value, ret); +#endif + return (ret); +} + static uint32_t data__poll_process_akm_abs(struct sensors_data_context_t *dev, int fd __attribute__((unused)), struct input_event *event) @@ -739,7 +728,7 @@ static uint32_t data__poll_process_akm_abs(struct sensors_data_context_t *dev, break; case EVENT_TYPE_YAW: new_sensors |= SENSORS_AKM_ORIENTATION; - dev->sensors[ID_O].orientation.azimuth = event->value; + dev->sensors[ID_O].orientation.azimuth = fix_azimuth(event->value); break; case EVENT_TYPE_PITCH: new_sensors |= SENSORS_AKM_ORIENTATION; @@ -948,6 +937,12 @@ static int data__poll(struct sensors_data_context_t *dev, sensors_data_t* values return 0x7FFFFFFF; } + /* + if (g_delay > 0) { + usleep(g_delay * 1000); + } + */ + if (got_syn && dev->pendingSensors) { LOGV("got syn, picking sensor"); return pick_sensor(dev, values); @@ -986,6 +981,7 @@ static int open_sensors(const struct hw_module_t* module, const char* name, struct hw_device_t** device) { int status = -EINVAL; + if (!strcmp(name, SENSORS_HARDWARE_CONTROL)) { struct sensors_control_context_t *dev; dev = malloc(sizeof(*dev)); @@ -1018,5 +1014,33 @@ static int open_sensors(const struct hw_module_t* module, const char* name, dev->device.poll = data__poll; *device = &dev->device.common; } + + /*LOGD("%s: %s \n",__func__,name);*/ return status; } + + +static int sensors__get_sensors_list(struct sensors_module_t* module, + struct sensor_t const** list) +{ + *list = sSensorList; + return ARRAY_SIZE(sSensorList); +} + +static struct hw_module_methods_t sensors_module_methods = { + .open = open_sensors +}; + +const struct sensors_module_t HAL_MODULE_INFO_SYM = { + .common = { + .tag = HARDWARE_MODULE_TAG, + .version_major = 1, + .version_minor = 0, + .id = SENSORS_HARDWARE_MODULE_ID, + .name = "AK8973A & CM3602 Sensors Module", + .author = "The Android Open Source Project", + .methods = &sensors_module_methods, + }, + .get_sensors_list = sensors__get_sensors_list +}; +