1. 灯的定义:LightsManager.java
public abstract class LightsManager {
public static final int LIGHT_ID_BACKLIGHT = Type.BACKLIGHT; //背景灯,即屏幕灯光
public static final int LIGHT_ID_KEYBOARD = Type.KEYBOARD; //键盘灯
public static final int LIGHT_ID_BUTTONS = Type.BUTTONS; //按键灯,BACK,HOME,MENU实体按键的背景灯
public static final int LIGHT_ID_BATTERY = Type.BATTERY; //电池灯,当电量改变时(如低电量),闪烁的灯
public static final int LIGHT_ID_NOTIFICATIONS = Type.NOTIFICATIONS; //通知灯,应用发送通知时,闪烁的灯,和电池灯共用
public static final int LIGHT_ID_ATTENTION = Type.ATTENTION; //警告灯,和电池灯共用
public static final int LIGHT_ID_BLUETOOTH = Type.BLUETOOTH;
public static final int LIGHT_ID_WIFI = Type.WIFI;
public static final int LIGHT_ID_COUNT = Type.COUNT;
public abstract Light getLight(int id); //其他模块通过此方法获取灯的ID
}
2. 设置灯的属性:Light.java (接口类,实现在LightsService.java)
public abstract void setBrightness(int brightness);
public abstract void setBrightness(int brightness, int brightnessMode);
public abstract void setColor(int color);
public abstract void setFlashing(int color, int mode, int onMS, int offMS);
public abstract void pulse();
public abstract void pulse(int color, int onMS);
public abstract void turnOff();
public abstract void setVrMode(boolean enabled);
3. 接收上层的亮灯命令,筛选判断后向HAL层发送命令:LightsService.java
public class LightsService extends SystemService {
static final String TAG = "LightsService";
static final boolean DEBUG = false;
final LightImpl mLights[] = new LightImpl[LightsManager.LIGHT_ID_COUNT]; //定义LightsManager中所有的灯
private final class LightImpl extends Light {
private LightImpl(int id) { 以灯的id来区分当前是哪个灯
mId = id;
}
@Override
public void setBrightness(int brightness) { //设置亮度
setBrightness(brightness, BRIGHTNESS_MODE_USER);
}
@Override
public void setBrightness(int brightness, int brightnessMode) { //设置亮度
synchronized (this) {
// LOW_PERSISTENCE cannot be manually set
if (brightnessMode == BRIGHTNESS_MODE_LOW_PERSISTENCE) {
Slog.w(TAG, "setBrightness with LOW_PERSISTENCE unexpected #" + mId +
": brightness=0x" + Integer.toHexString(brightness));
return;
}
int color = brightness & 0x000000ff;
color = 0xff000000 | (color << 16) | (color << 8) | color;
setLightLocked(color, LIGHT_FLASH_NONE, 0, 0, brightnessMode);
}
}
@Override
public void setColor(int color) { //设置颜色
synchronized (this) {
setLightLocked(color, LIGHT_FLASH_NONE, 0, 0, 0);
}
}
@Override
public void setFlashing(int color, int mode, int onMS, int offMS) { //设置亮灯属性
synchronized (this) {
setLightLocked(color, mode, onMS, offMS, BRIGHTNESS_MODE_USER);
}
}
@Override
public void pulse() { //闪烁亮灯
pulse(0x00ffffff, 7);
}
@Override
public void pulse(int color, int onMS) { //闪烁亮灯,可设置颜色和亮起的时间
synchronized (this) {
if (mColor == 0 && !mFlashing) {
setLightLocked(color, LIGHT_FLASH_HARDWARE, onMS, 1000,
BRIGHTNESS_MODE_USER);
mColor = 0;
mH.sendMessageDelayed(Message.obtain(mH, 1, this), onMS);
}
}
}
@Override
public void turnOff() { //关灯
synchronized (this) {
setLightLocked(0, LIGHT_FLASH_NONE, 0, 0, 0);
}
}
@Override
public void setVrMode(boolean enabled) {
synchronized (this) {
if (mVrModeEnabled != enabled) {
mVrModeEnabled = enabled;
mUseLowPersistenceForVR =
(getVrDisplayMode() == Settings.Secure.VR_DISPLAY_MODE_LOW_PERSISTENCE);
if (shouldBeInLowPersistenceMode()) {
mLastBrightnessMode = mBrightnessMode;
}
// NOTE: We do not trigger a call to setLightLocked here. We do not know the
// current brightness or other values when leaving VR so we avoid any incorrect
// jumps. The code that calls this method will immediately issue a brightness
// update which is when the change will occur.
}
}
}
private void stopFlashing() {
synchronized (this) {
setLightLocked(mColor, LIGHT_FLASH_NONE, 0, 0, BRIGHTNESS_MODE_USER);
}
}
private void setLightLocked(int color, int mode, int onMS, int offMS, int brightnessMode) { //以上所有方法都是调用的此方法
if (shouldBeInLowPersistenceMode()) {
brightnessMode = BRIGHTNESS_MODE_LOW_PERSISTENCE;
} else if (brightnessMode == BRIGHTNESS_MODE_LOW_PERSISTENCE) {
brightnessMode = mLastBrightnessMode;
}
//如果传来的参数和上次的参数一样,则不进行任何动作
if (!mInitialized || color != mColor || mode != mMode || onMS != mOnMS ||
offMS != mOffMS || mBrightnessMode != brightnessMode) {
if (DEBUG) Slog.v(TAG, "setLight #" + mId + ": color=#"
+ Integer.toHexString(color) + ": brightnessMode=" + brightnessMode);
//保留这次的参数,用作下次做对比
mInitialized = true;
mLastColor = mColor;
mColor = color;
mMode = mode;
mOnMS = onMS;
mOffMS = offMS;
mBrightnessMode = brightnessMode;
Trace.traceBegin(Trace.TRACE_TAG_POWER, "setLight(" + mId + ", 0x"
+ Integer.toHexString(color) + ")");
try {
//调用HAL层方法(com_android_server_lights_LightsService.cpp),将参数传到底层
setLight_native(mId, color, mode, onMS, offMS, brightnessMode);
} finally {
Trace.traceEnd(Trace.TRACE_TAG_POWER);
}
}
}
private boolean shouldBeInLowPersistenceMode() {
return mVrModeEnabled && mUseLowPersistenceForVR;
}
private int mId;
private int mColor;
private int mMode;
private int mOnMS;
private int mOffMS;
private boolean mFlashing;
private int mBrightnessMode;
private int mLastBrightnessMode;
private int mLastColor;
private boolean mVrModeEnabled;
private boolean mUseLowPersistenceForVR;
private boolean mInitialized;
}
public LightsService(Context context) {
super(context);
for (int i = 0; i < LightsManager.LIGHT_ID_COUNT; i++) {
mLights[i] = new LightImpl(i);
}
}
@Override
public void onStart() {
publishLocalService(LightsManager.class, mService);
}
@Override
public void onBootPhase(int phase) {
}
private int getVrDisplayMode() {
int currentUser = ActivityManager.getCurrentUser();
return Settings.Secure.getIntForUser(getContext().getContentResolver(),
Settings.Secure.VR_DISPLAY_MODE,
/*default*/Settings.Secure.VR_DISPLAY_MODE_LOW_PERSISTENCE,
currentUser);
}
private final LightsManager mService = new LightsManager() {
@Override
public Light getLight(int id) {
if (0 <= id && id < LIGHT_ID_COUNT) {
return mLights[id];
} else {
return null;
}
}
};
private Handler mH = new Handler() {
@Override
public void handleMessage(Message msg) {
LightImpl light = (LightImpl)msg.obj;
light.stopFlashing();
}
};
static native void setLight_native(int light, int color, int mode,
int onMS, int offMS, int brightnessMode);
}
4. HAL层中转信息:com_android_server_lights_LightsService.cpp
static void setLight_native(
JNIEnv* /* env */,
jobject /* clazz */,
jint light, //上层的id
jint colorARGB,
jint flashMode,
jint onMS,
jint offMS,
jint brightnessMode) {
if (!validate(light, flashMode, brightnessMode)) {
return;
}
sp hal = LightHal::associate();
if (hal == nullptr) {
return;
}
Type type = static_cast(light);
LightState state = constructState(
colorARGB, flashMode, onMS, offMS, brightnessMode);
{
android::base::Timer t;
Return ret = hal->setLight(type, state);
processReturn(ret, type, state);
if (t.duration() > 50ms) ALOGD("Excessive delay setting light");
}
}
5. 调用驱动层:lights.c
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);
pthread_mutex_lock(&g_lock);
g_backlight = brightness;
err = write_int(LCD_FILE, brightness);
if (g_haveTrackballLight) {
handle_trackball_light_locked(dev);
}
pthread_mutex_unlock(&g_lock);
return err;
}
static int
set_light_keyboard(__attribute__((__unused__)) struct light_device_t* dev,
struct light_state_t const* state) //键盘灯
{
int err = 0;
int on = is_lit(state);
pthread_mutex_lock(&g_lock);
err = write_int(KEYBOARD_FILE, on?255:0);
pthread_mutex_unlock(&g_lock);
return err;
}
static int
set_light_buttons(__attribute__((__unused__)) struct light_device_t* dev,
struct light_state_t const* state) //按键灯
{
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_speaker_light_locked(__attribute__((__unused__)) struct light_device_t* dev,
struct light_state_t const* state) //battery 和 notification 使用的方法
{
int len;
int alpha, red, green, blue;
int onMS, offMS;
unsigned int colorRGB;
switch (state->flashMode) {
case LIGHT_FLASH_TIMED:
onMS = state->flashOnMS;
offMS = state->flashOffMS;
break;
case LIGHT_FLASH_NONE:
default:
onMS = 0;
offMS = 0;
break;
}
colorRGB = state->color;
#ifdef LIGHTS_DBG_ON
ALOGD("set_led_state colorRGB=%08X, onMS=%d, offMS=%d\n",
colorRGB, onMS, offMS);
#endif
alpha = (colorRGB >> 24) & 0xFF;
if (alpha) {
red = (colorRGB >> 16) & 0xFF;
green = (colorRGB >> 8) & 0xFF;
blue = colorRGB & 0xFF;
} else { // alpha = 0 means turn the LED off
red = green = blue = 0;
}
if (red) {
ALOGD("wxmled if (red) ");
//Redmine112320 wuxiaoming modify for vdf led 2017-12-06 begin //如果是单色灯,则要去掉其他颜色,不然会报错导致不亮灯
//blink_green(0, 0, 0);
//blink_blue(0, 0, 0);
blink_red(red, onMS, offMS);
} else {
ALOGD("wxmled if (red) else turnOff");
blink_red(0, 0, 0);
//blink_blue(0, 0, 0);
//blink_green(green, onMS, offMS);
}
/*
else if (blue) {
blink_red(0, 0, 0);
blink_green(0, 0, 0);
blink_blue(blue, onMS, offMS);
}
else {
blink_red(0, 0, 0);
blink_green(0, 0, 0);
blink_blue(0, 0, 0);
}*/
//Redmine112320 wuxiaoming modify for vdf led 2017-12-06 end
return 0;
}
static void
handle_speaker_battery_locked(struct light_device_t* dev) //设置电池灯和通知灯的优先级
{
//Redmine112320 wuxiaoming modify for vdf led 2017-12-06 begin
ALOGD("wxmled handle_speaker_battery_locked");
/*if (is_lit(&g_battery)) {
set_speaker_light_locked(dev, &g_battery);
} else {
set_speaker_light_locked(dev, &g_battery); Turkey workaround: notification and Low battery case, IPO bootup, NLED cannot blink*/
/* set_speaker_light_locked(dev, &g_notification);
}*/
if (is_lit(&g_notification)) {
ALOGD("wxmled handle_speaker_battery_locked g_notification");
set_speaker_light_locked(dev, &g_notification);
} else {
ALOGD("wxmled handle_speaker_battery_locked g_notification then g_battery");
set_speaker_light_locked(dev, &g_notification);
set_speaker_light_locked(dev, &g_battery);
}
//Redmine112320 wuxiaoming modify for vdf led 2017-12-06 end
}
static int
set_light_battery(struct light_device_t* dev,
struct light_state_t const* state) //电池灯
{
pthread_mutex_lock(&g_lock);
g_battery = *state;
if (g_haveTrackballLight) {
set_speaker_light_locked(dev, 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) //通知灯
{
pthread_mutex_lock(&g_lock);
g_notification = *state;
ALOGV("set_light_notifications g_trackball=%d color=0x%08x",
g_trackball, state->color);
//Redmine112320 wuxiaoming modify for vdf led 2017-12-06 begin
ALOGD("wxmled set_light_notifications: g_battery.flashOnMS = %d; flashOffMS = %d", g_battery.flashOnMS, g_battery.flashOffMS);
if (g_battery.flashOnMS > 0 && g_battery.flashOffMS > 0) {
ALOGD("wxmled set_light_notifications: low power, so notification return");
pthread_mutex_unlock(&g_lock);
return 0;
}
//Redmine112320 wuxiaoming modify for vdf led 2017-12-06 end
if (g_haveTrackballLight) {
handle_trackball_light_locked(dev);
}
handle_speaker_battery_locked(dev);
pthread_mutex_unlock(&g_lock);
return 0;
}
static int
set_light_attention(struct light_device_t* dev,
struct light_state_t const* state) //警告灯
{
pthread_mutex_lock(&g_lock);
ALOGV("set_light_attention g_trackball=%d color=0x%08x",
g_trackball, state->color);
if (state->flashMode == LIGHT_FLASH_HARDWARE) {
g_attention = state->flashOnMS;
} else if (state->flashMode == LIGHT_FLASH_NONE) {
g_attention = 0;
}
if (g_haveTrackballLight) {
handle_trackball_light_locked(dev);
}
pthread_mutex_unlock(&g_lock);
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) //初始化动作
{
ALOGD("wxmled open_lights");//Redmine112320 wuxiaoming modify for vdf led 2017-12-06
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;
if (access(LCD_FILE, F_OK) < 0)
return -errno;
}
else if (0 == strcmp(LIGHT_ID_KEYBOARD, name)) {
set_light = set_light_keyboard;
if (access(KEYBOARD_FILE, F_OK) < 0)
return -errno;
}
else if (0 == strcmp(LIGHT_ID_BUTTONS, name)) {
set_light = set_light_buttons;
if (access(BUTTON_FILE, F_OK) < 0)
return -errno;
}
else if (0 == strcmp(LIGHT_ID_BATTERY, name)) {
set_light = set_light_battery;
if (access(RED_LED_FILE, F_OK) < 0)
return -errno;
//Redmine112320 wuxiaoming modify for vdf led 2017-12-06 begin //如果为单色灯,不要检查其他颜色的文件,不然报错无法亮灯
/*
if (access(GREEN_LED_FILE, F_OK) < 0)
return -errno;
if (access(BLUE_LED_FILE, F_OK) < 0)
return -errno;
*/
//Redmine112320 wuxiaoming modify for vdf led 2017-12-06 end
}
else if (0 == strcmp(LIGHT_ID_NOTIFICATIONS, name)) {
set_light = set_light_notifications;
if (access(RED_LED_FILE, F_OK) < 0)
return -errno;
//Redmine112320 wuxiaoming modify for vdf led 2017-12-06 begin
/*
if (access(GREEN_LED_FILE, F_OK) < 0)
return -errno;
if (access(BLUE_LED_FILE, F_OK) < 0)
return -errno;
*/
//Redmine112320 wuxiaoming modify for vdf led 2017-12-06 end
}
else if (0 == strcmp(LIGHT_ID_ATTENTION, name)) {
set_light = set_light_attention;
if (access(RED_LED_FILE, F_OK) < 0)
return -errno;
//Redmine112320 wuxiaoming modify for vdf led 2017-12-06 begin
/*
if (access(GREEN_LED_FILE, F_OK) < 0)
return -errno;
if (access(BLUE_LED_FILE, F_OK) < 0)
return -errno;
*/
//Redmine112320 wuxiaoming modify for vdf led 2017-12-06 end
}
else {
return -EINVAL;
}
pthread_once(&g_init, init_globals);
struct light_device_t *dev = malloc(sizeof(struct light_device_t));
if (!dev)
return -ENOMEM;
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;
}