Android模拟器学framework和driver之battery & backlight-----3. battery in framework
上一篇http://www.zzzyk.com/kf/201203/121809.html中我们提到了jni,若大家对android中jni是怎么运作的,可以先参考我之前关于android jni的一个专栏:
http://blog.csdn.net/column/details/jnijni.html
这里有讲到jni和java是怎么互调的,好,这里我们来介绍下battery service在android中是如何工作的,我们先理清楚android framework层总的来说是做什么呢?
其实就是对底层进行封装成API,然后使用SDK开发的人员可以根据这些API做应用层的开发,所以说,我们这边的目的就是,让用户知道什么时候电池信息发生了变化,什么信息发生了变化,还有就是实时可以让用户查询到电池的信息。
1. 用户知道什么时候电池信息发生了变化
2. 什么信息发生了变化
3. 实时可以让用户查询到电池的信息
下面就先针对这3个问题分析下代码
这部分之前有提到,我们driver层有注册uevent,当电池信息发生变化的时候会向用户层发送一个uevent,OK,我们来找用户在哪边去抓这个uevent的,找啊找原来在hal层:
hardware/libhardware_legacy/uevent/uevent.c
[cpp] view plaincopyprint?int uevent_init()
{
struct sockaddr_nl addr;
int sz = 64*1024;
int s;
memset(&addr, 0, sizeof(addr));
addr.nl_family = AF_NETLINK;
addr.nl_pid = getpid();
addr.nl_groups = 0xffffffff;
<span style="color:#ff0000;"> s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);</span>
if(s < 0)
return 0;
setsockopt(s, SOL_SOCKET, SO_RCVBUFFORCE, &sz, sizeof(sz));
if(bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
close(s);
return 0;
}
fd = s;
return (fd > 0);
}
int uevent_init()
{
struct sockaddr_nl addr;
int sz = 64*1024;
int s;
memset(&addr, 0, sizeof(addr));
addr.nl_family = AF_NETLINK;
addr.nl_pid = getpid();
addr.nl_groups = 0xffffffff;
<span style="color:#ff0000;"> s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);</span>
if(s < 0)
return 0;
setsockopt(s, SOL_SOCKET, SO_RCVBUFFORCE, &sz, sizeof(sz));
if(bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
close(s);
return 0;
}
fd = s;
return (fd > 0);
}
也就是说在android启动的时候这里的uevent_init也会被调用到,在这里最关心的就是上面红色部分的代码,这里注册了一个socket来抓取kobject_uevent,判断环境变量是否发生变化,这里uevent被frameworks的封装在jni中:
frameworks/base/core/jni/android_os_UEventObserver.cpp
[cpp] view plaincopyprint?static void
android_os_UEventObserver_native_setup(JNIEnv *env, jclass clazz)
{
if (!uevent_init()) {
jniThrowException(env, "java/lang/RuntimeException",
"Unable to open socket for UEventObserver");
}
}
static int
android_os_UEventObserver_next_event(JNIEnv *env, jclass clazz, jbyteArray jbuffer)
{
int buf_sz = env->GetArrayLength(jbuffer);
char *buffer = (char*)env->GetByteArrayElements(jbuffer, NULL);
int length = uevent_next_event(buffer, buf_sz - 1);
env->ReleaseByteArrayElements(jbuffer, (jbyte*)buffer, 0);
return length;
}
static JNINativeMethod gMethods[] = {
{"native_setup", "()V", (void *)android_os_UEventObserver_native_setup},
{"next_event", "([B)I", (void *)android_os_UEventObserver_next_event},
};
static void
android_os_UEventObserver_native_setup(JNIEnv *env, jclass clazz)
{
if (!uevent_init()) {
jniThrowException(env, "java/lang/RuntimeException",
"Unable to open socket for UEventObserver");
}
}
static int
android_os_UEventObserver_next_event(JNIEnv *env, jclass clazz, jbyteArray jbuffer)
{
int buf_sz = env->GetArrayLength(jbuffer);
char *buffer = (char*)env->GetByteArrayElements(jbuffer, NULL);
int length = uevent_next_event(buffer, buf_sz - 1);
env->ReleaseByteArrayElements(jbuffer, (jbyte*)buffer, 0);
return length;
}
static JNINativeMethod gMethods[] = {
{"native_setup", "()V", (void *)android_os_UEventObserver_native_setup},
{"next_event", "([B)I", (void *)android_os_UEventObserver_next_event},
};
这里提供了2个方法,一个是setup,在setup中调用了刚才hal中的init函数,还有就是一个next_event函数,这个函数会去遍历所有的uevent事件是否发生了变化。
这边顺带讲一下android framework中对uevent的java代码的封装,因为在我们的battery service中会用到。
frameworks/base/core/java/android/os/UEventObserver.java
[cpp] view plaincopyprint?private static native void native_setup();
private static native int next_event(byte[] buffer);
private static native void native_setup();
private static native int next_event(byte[] buffer);
在这里先是注册了刚才jni中的2个方法,这里首先开启了一个线程:run方法如下:
[cpp] view plaincopyprint?public void run() {
native_setup();
byte[] buffer = new byte[1024];
int len;
while (true) {
len = next_event(buffer);
if (len > 0) {
String bufferStr = new String(buffer, 0, len); // easier to search a String
synchronized (mObservers) {
for (int i = 0; i < mObservers.size(); i += 2) {
&nb
补充:移动开发 , Android ,