Lights System
一、Overall Architeture
二、Linux Driver Layer
三、Android HAL Layer
四、Hardware Interface
五、JNI Layer
六、Java Service
七、Java App
一、OverallArchiteture
Android lightssystem
二、LinuxDriver Layer
Linux DriverLayer 采用标准linux的LED框架,代码位于drivers/leds/,接口采用sys文件系统提供,分别为/sys/class/leds/[device]/brightness,/sys/class/leds/[device]/ max_brightness, /sys/class/leds/[device]/trigger.
三、AndroidHAL Layer
首先创建hw_module_t对象,对象名称唯一“HAL_MODULE_INFO_SYM”,
#define HAL_MODULE_INFO_SYMHMI
/**
* Name of the hal_module_info as a string
*/
#defineHAL_MODULE_INFO_SYM_AS_STR"HMI"
conststruct hw_module_t HAL_MODULE_INFO_SYM = {
.tag = HARDWARE_MODULE_TAG,
.version_major = 1,
.version_minor = 0,
.id = LIGHTS_HARDWARE_MODULE_ID,
.name = "QCT MSM7K lightsModule",
.author = "Google, Inc.",
.methods = &lights_module_methods,
};
然后实现hw_module_methods_t方法,返回hw_device_t对象指针:
staticstruct hw_module_methods_t lights_module_methods = {
.open =open_lights,
};
static intopen_lights(const struct hw_module_t* module, char const* name,
struct hw_device_t** device)
{
&&&&&&&&&&&&&&&&
*device =(struct hw_device_t*)dev;
&&&&&&&&&&&&&&&&
}
四、HardwareInterface
JNI call hw_get_module fuction,returnhw_device_t object.
五、JNILayer
JNI code 在frameworks/base/services/jni/com_android_server_LightsService.cpp
首先实现标准接口函数int register_android_server_LightsService(JNIEnv*env);这个函数调用JNI 函数jniRegisterNativeMethods(在onload.cpp)实现C/C++函数和JAVA对应关系.最后由JAVA程序调用JNI_OnLoad。见下图:
六、JavaService
Service layer 其实就是一个JAVA类,其中声明为本地的函数,这些函数通过JNI最后调用底层设备。
public class LightsService {
private staticnative int init_native();
private static native void finalize_native(int ptr);
private static native void setLight_native(int ptr, int light, intcolor, int mode,
int onMS, int offMS, int brightnessMode);
}
构造函数:
LightsService(Context context) {
mNativePointer = init_native();
mContext = context;
ServiceManager.addService("hardware", mLegacyFlashlightHack);
for (int i = 0; i < LIGHT_ID_COUNT; i++) {
mLights[i] = new Light(i);
}
}
析构函数:
protected void finalize() throws Throwable {
finalize_native(mNativePointer);
super.finalize();
}
private void setLightLocked(int color, intmode, int onMS, int offMS, int brightnessMode) {
if (color != mColor || mode != mMode || onMS != mOnMS || offMS !=mOffMS) {
mColor = color;
mMode = mode;
mOnMS = onMS;
mOffMS = offMS;
setLight_native(mNativePointer,mId, color, mode, onMS, offMS, brightnessMode);
}
}
七、JavaApp
class PowerManagerService{
private LightsService.Light mLcdLight;
private LightsService.Light mButtonLight;
private LightsService.Light mKeyboardLight;
private LightsService.Light mAttentionLight;
}
private void setLightBrightness(int mask,int value) {
int brightnessMode = (mAutoBrightessEnabled
?LightsService.BRIGHTNESS_MODE_SENSOR
:LightsService.BRIGHTNESS_MODE_USER);
if ((mask & SCREEN_BRIGHT_BIT) != 0) {
mLcdLight.setBrightness(value, brightnessMode);
}
if ((mask & BUTTON_BRIGHT_BIT) != 0) {
mButtonLight.setBrightness(value);
}
if ((mask & KEYBOARD_BRIGHT_BIT) != 0) {
mKeyboardLight.setBrightness(value);
}
}
转载于:https://blog.51cto.com/linuxkernel/1255621