http://blog.youkuaiyun.com/zhangjie201412/article/details/7332606
这里我们来介绍下backlight hal层,和之前一样,跟之前我们介绍的temperature的hal差不多,比sensor的hal还简单,这里我就不多说hal的介绍了,之前都有介绍的比较详细了,不清楚的可以参考前面的文章:
http://blog.youkuaiyun.com/zhangjie201412/article/details/7225617
首先要明确我们的目标,这里主要是封装一些方法,然后给android framework层使用(jni layer),打开一个sysfs中的节点,然后read/write,
明白这点就够了,直接贴代码:
/hardware/libhardware/module/lights/android_light.c
- #define LOG_TAG "light"
- #include <hardware/lights.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <stdlib.h>
- #include <cutils/log.h>
- #include <cutils/atomic.h>
- #include <cutils/properties.h>
- #define MAX_BRIGHTNESS 255
- #define DEF_BACKLIGHT_DEV "android-backlight"
- #define DEF_BACKLIGHT_PATH "/sys/class/backlight/"
- /***************************************************************/
- struct lights_module_t{
- struct hw_module_t common;
- };
- static int lights_device_open(const struct hw_module_t* module,
- const char* name,struct hw_device_t** device);
- static struct hw_module_methods_t lights_module_methods = {
- .open=lights_device_open
- };
- const struct lights_module_t HAL_MODULE_INFO_SYM = {
- .common= {
- .tag= HARDWARE_MODULE_TAG,
- .version_major= 1,
- .version_minor= 0,
- .id= LIGHTS_HARDWARE_MODULE_ID,
- .name= "Lights module",
- .author= "Freescale Semiconductor",
- .methods= &lights_module_methods,
- }
- };
- static char max_path[256], path[256];
- // ****************************************************************************
- // module
- // ****************************************************************************
- static int set_light_backlight(struct light_device_t* dev,
- struct light_state_t const* state)
- {
- int result = -1;
- unsigned int color = state->color;
- unsigned int brightness = 0, max_brightness = 0;
- FILE *file;
- brightness = ((77*((color>>16)&0x00ff)) + (150*((color>>8)&0x00ff)) +
- (29*(color&0x00ff))) >> 8;
- LOGV("set_light, get brightness=%d", brightness);
- file = fopen(max_path, "r");
- if (!file) {
- LOGE("can not open file %s\n", max_path);
- return result;
- }
- fread(&max_brightness, 1, 3, file);
- fclose(file);
- max_brightness = atoi((char *) &max_brightness);
- brightness = brightness * max_brightness / MAX_BRIGHTNESS;
- LOGV("set_light, max_brightness=%d, target brightness=%d",
- max_brightness, brightness);
- file = fopen(path, "w");
- if (!file) {
- LOGE("can not open file %s\n", path);
- return result;
- }
- fprintf(file, "%d", brightness);
- fclose(file);
- result = 0;
- return result;
- }
- static int light_close_backlight(struct hw_device_t *dev)
- {
- struct light_device_t *device = (struct light_device_t*)dev;
- if (device)
- free(device);
- return 0;
- }
- /*****************************************************************************/
- static int lights_device_open(const struct hw_module_t* module,
- const char* name, struct hw_device_t** device)
- {
- int status = -EINVAL;
- LOGV("lights_device_open\n");
- if (!strcmp(name, LIGHT_ID_BACKLIGHT)) {
- struct light_device_t *dev;
- char value[PROPERTY_VALUE_MAX];
- dev = malloc(sizeof(*dev));
- /* initialize our state here */
- memset(dev, 0, sizeof(*dev));
- /* initialize the procs */
- dev->common.tag = HARDWARE_DEVICE_TAG;
- dev->common.version = 0;
- dev->common.module = (struct hw_module_t*) module;
- dev->common.close = light_close_backlight;
- dev->set_light = set_light_backlight;
- *device = &dev->common;
- // property_get("hw.backlight.dev", value, DEF_BACKLIGHT_DEV);
- // strcpy(path, DEF_BACKLIGHT_PATH);
- // strcat(path, value);
- // strcpy(max_path, path);
- // strcat(max_path, "/max_brightness");
- // strcat(path, "/brightness");
- strcpy(path, "/sys/devices/platform/android-backlight.0/backlight/android-backlight.0/brightness");
- strcpy(max_path, "/sys/devices/platform/android-backlight.0/backlight/android-backlight.0/max_brightness");
- LOGI("max backlight file is %s\n", max_path);
- LOGI("backlight brightness file is %s\n", path);
- status = 0;
- }
- /* todo other lights device init */
- return status;
- }
#define LOG_TAG "light"
#include <hardware/lights.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <cutils/log.h>
#include <cutils/atomic.h>
#include <cutils/properties.h>
#define MAX_BRIGHTNESS 255
#define DEF_BACKLIGHT_DEV "android-backlight"
#define DEF_BACKLIGHT_PATH "/sys/class/backlight/"
/***************************************************************/
struct lights_module_t{
struct hw_module_t common;
};
static int lights_device_open(const struct hw_module_t* module,
const char* name,struct hw_device_t** device);
static struct hw_module_methods_t lights_module_methods = {
.open=lights_device_open
};
const struct lights_module_t HAL_MODULE_INFO_SYM = {
.common= {
.tag= HARDWARE_MODULE_TAG,
.version_major= 1,
.version_minor= 0,
.id= LIGHTS_HARDWARE_MODULE_ID,
.name= "Lights module",
.author= "Freescale Semiconductor",
.methods= &lights_module_methods,
}
};
static char max_path[256], path[256];
// ****************************************************************************
// module
// ****************************************************************************
static int set_light_backlight(struct light_device_t* dev,
struct light_state_t const* state)
{
int result = -1;
unsigned int color = state->color;
unsigned int brightness = 0, max_brightness = 0;
FILE *file;
brightness = ((77*((color>>16)&0x00ff)) + (150*((color>>8)&0x00ff)) +
(29*(color&0x00ff))) >> 8;
LOGV("set_light, get brightness=%d", brightness);
file = fopen(max_path, "r");
if (!file) {
LOGE("can not open file %s\n", max_path);
return result;
}
fread(&max_brightness, 1, 3, file);
fclose(file);
max_brightness = atoi((char *) &max_brightness);
brightness = brightness * max_brightness / MAX_BRIGHTNESS;
LOGV("set_light, max_brightness=%d, target brightness=%d",
max_brightness, brightness);
file = fopen(path, "w");
if (!file) {
LOGE("can not open file %s\n", path);
return result;
}
fprintf(file, "%d", brightness);
fclose(file);
result = 0;
return result;
}
static int light_close_backlight(struct hw_device_t *dev)
{
struct light_device_t *device = (struct light_device_t*)dev;
if (device)
free(device);
return 0;
}
/*****************************************************************************/
static int lights_device_open(const struct hw_module_t* module,
const char* name, struct hw_device_t** device)
{
int status = -EINVAL;
LOGV("lights_device_open\n");
if (!strcmp(name, LIGHT_ID_BACKLIGHT)) {
struct light_device_t *dev;
char value[PROPERTY_VALUE_MAX];
dev = malloc(sizeof(*dev));
/* initialize our state here */
memset(dev, 0, sizeof(*dev));
/* initialize the procs */
dev->common.tag = HARDWARE_DEVICE_TAG;
dev->common.version = 0;
dev->common.module = (struct hw_module_t*) module;
dev->common.close = light_close_backlight;
dev->set_light = set_light_backlight;
*device = &dev->common;
// property_get("hw.backlight.dev", value, DEF_BACKLIGHT_DEV);
// strcpy(path, DEF_BACKLIGHT_PATH);
// strcat(path, value);
// strcpy(max_path, path);
// strcat(max_path, "/max_brightness");
// strcat(path, "/brightness");
strcpy(path, "/sys/devices/platform/android-backlight.0/backlight/android-backlight.0/brightness");
strcpy(max_path, "/sys/devices/platform/android-backlight.0/backlight/android-backlight.0/max_brightness");
LOGI("max backlight file is %s\n", max_path);
LOGI("backlight brightness file is %s\n", path);
status = 0;
}
/* todo other lights device init */
return status;
}
接下来是android.mk
- LOCAL_PATH := $(call my-dir)
- # HAL module implemenation, not prelinked and stored in hw/
- include $(CLEAR_VARS)
- LOCAL_MODULE_TAGS := optional
- LOCAL_PRELINK_MODULE := false
- LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/hw
- LOCAL_SHARED_LIBRARIES := liblog libcutils
- LOCAL_SRC_FILES := android_light.c
- LOCAL_MODULE := lights.default
- #LOCAL_MODULE_TAGS := eng
- include $(BUILD_SHARED_LIBRARY)
LOCAL_PATH := $(call my-dir)
# HAL module implemenation, not prelinked and stored in hw/
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_PRELINK_MODULE := false
LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/hw
LOCAL_SHARED_LIBRARIES := liblog libcutils
LOCAL_SRC_FILES := android_light.c
LOCAL_MODULE := lights.default
#LOCAL_MODULE_TAGS := eng
include $(BUILD_SHARED_LIBRARY)
这里我要声明的是,在读写之前先把文件的权限打开,在init.rc中去做
- #add by Jay
- chmod 777 /sys/bus/iio/devices/device0/lux
- chmod 777 /sys/bus/iio/devices/device1/value
- chmod 777 /sys/bus/iio/devices/device1/active
- chmod 777 /sys/class/backlight/android-backlight.0/brightness
- chmod 777 /sys/class/backlight/android-backlight.0/max_brightness
#add by Jay
chmod 777 /sys/bus/iio/devices/device0/lux
chmod 777 /sys/bus/iio/devices/device1/value
chmod 777 /sys/bus/iio/devices/device1/active
chmod 777 /sys/class/backlight/android-backlight.0/brightness
chmod 777 /sys/class/backlight/android-backlight.0/max_brightness
好了,hal层我们就添加到这边,我们来重新编译,然后打开我们的模拟器,
打开setting中的display,然后调节屏幕亮度,然后再cat brightness会发现节点中的值改变了

把亮度调到最高,值变成了255.
结束!!!!下面会分析android framework中jni去实现lightService。
本文介绍 Android 系统中 HAL 层背光控制模块的实现细节,包括如何通过 sysfs 接口读写背光亮度,并展示了关键代码片段。
992

被折叠的 条评论
为什么被折叠?



