一、 Android uevent架构
Android很多事件都是通过 uevent跟 kernel来异步通信的。其中类 UEventObserver是核心。
UEventObserver接收 kernel的 uevent信息的抽象类。
1、 server层代码
1)battery server:
frameworks/frameworks/base/services/java/com/android/server/SystemServer.java
frameworks/frameworks/base/services/java/com/android/server/BatteryService.java
2、 java层代码
frameworks/base/core/java/android/os/UEventObserver.java
3、 JNI层代码
frameworks/base/core/jni/android_os_UEventObserver.cpp
4、底层代码
hardware/libhardware_legacy/uevent/uevent.c
读写 kernel的接口 socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
二、 UEventObserver的使用
类 UEventObserver提供了三个接口给子类来调用:
1、 onUEvent(UEvent event)
子类必须重写这个 onUEvent来处理 uevent。
2、 startObserving(String match)
启动进程,要提供一个字符串参数。
3、 stopObserving()
停止进程。
例子:
//在 BatteryService.java中
- mUEventObserver.startObserving("SUBSYSTEM=power_supply");
- private UEventObserver mUEventObserver = new UEventObserver() {
- @Override
- public void onUEvent(UEventObserver.UEvent event) {
- update();
- }
- };
在 UEvent thread
中会不停调用 update()
方法,来更新电池的信息数据。
三、 vold server分析
1、在 system/vold/NetlinkManager.cpp中:
- if ((mSock = socket(PF_NETLINK,SOCK_DGRAM,NETLINK_KOBJECT_UEVENT)) < 0) {
- SLOGE("Unable to create uevent socket: %s", strerror(errno));
- return -1;
- }
- if (setsockopt(mSock, SOL_SOCKET, SO_RCVBUFFORCE, &sz, sizeof(sz)) < 0) {
- SLOGE("Unable to set uevent socket options: %s", strerror(errno));
- return -1;
- }
- if (bind(mSock, (struct sockaddr *) &nladdr, sizeof(nladdr)) < 0) {
- SLOGE("Unable to bind uevent socket: %s", strerror(errno));
- return -1;
- }
2
、然后在 system/vold/NetlinkHandler.cpp
的 NetlinkHandler::onEvent
中处理- void NetlinkHandler::onEvent(NetlinkEvent *evt) {
- VolumeManager *vm = VolumeManager::Instance();
- const char *subsys = evt->getSubsystem();
- if (!subsys) {
- SLOGW("No subsystem found in netlink event");
- return;
- }
- if (!strcmp(subsys, "block")) {
- vm->handleBlockEvent(evt);
- } else if (!strcmp(subsys, "switch")) {
- vm->handleSwitchEvent(evt);
- } else if (!strcmp(subsys, "battery")) {
- } else if (!strcmp(subsys, "power_supply")) {
- }
- }
3
、在 system/core/libsysutils/src/NetlinkListener.cpp
中监听。 四、 battery server
分析
java代码:
frameworks/frameworks/base/services/java/com/android/server/BatteryService.java
JNI代码:
frameworks/base/services/jni/com_android_server_BatteryService.cpp
1、 BatteryService是跑在 system_process当中,在系统初始化的时候启动,如下
在 BatteryService.java中:
Log.i(TAG, “Starting Battery Service.”);
BatteryService battery = new BatteryService(context);
ServiceManager.addService(“battery”, battery);
2、数据来源
BatteryService通过 JNI( com_android_server_BatteryService.cpp)读取数据。
BatteryService通过 JNI注册的不仅有函数,还有变量。 如下:
-
- private boolean mAcOnline;
- private boolean mUsbOnline;
- private int mBatteryStatus;
- private int mBatteryHealth;
- private boolean mBatteryPresent;
- private int mBatteryLevel;
- private int mBatteryVoltage;
- private int mBatteryTemperature;
- private String mBatteryTechnology;
-
- 用,即在com_android_server_BatteryService.cpp中其实操作的也是BatteryService.java中声
- 明的变量。
- gFieldIds.mAcOnline = env->GetFieldID(clazz, “mAcOnline”, “Z”);
- gFieldIds.mUsbOnline = env->GetFieldID(clazz, “mUsbOnline”, “Z”);
- gFieldIds.mBatteryStatus = env->GetFieldID(clazz, “mBatteryStatus”, “I”);
- gFieldIds.mBatteryHealth = env->GetFieldID(clazz, “mBatteryHealth”, “I”);
- gFieldIds.mBatteryPresent = env->GetFieldID(clazz, “mBatteryPresent”, “Z”);
- gFieldIds.mBatteryLevel = env->GetFieldID(clazz, “mBatteryLevel”, “I”);
- gFieldIds.mBatteryTechnology = env->GetFieldID(clazz, “mBatteryTechnology”,
- “Ljava/lang/String;”);
- gFieldIds.mBatteryVoltage = env->GetFieldID(clazz, “mBatteryVoltage”, “I”);
- gFieldIds.mBatteryTemperature = env->GetFieldID(clazz, “mBatteryTemperature”,
- “I”);
-
- #define AC_ONLINE_PATH “/sys/class/power_supply/ac/online”
- #define USB_ONLINE_PATH “/sys/class/power_supply/usb/online”
- #define BATTERY_STATUS_PATH “/sys/class/power_supply/battery/status”
- #define BATTERY_HEALTH_PATH “/sys/class/power_supply/battery/health”
- #define BATTERY_PRESENT_PATH “/sys/class/power_supply/battery/present”
- #define BATTERY_CAPACITY_PATH “/sys/class/power_supply/battery/capacity”
- #define BATTERY_VOLTAGE_PATH “/sys/class/power_supply/battery/batt_vol”
- #define BATTERY_TEMPERATURE_PATH “/sys/class/power_supply/battery/batt_temp”
- #define BATTERY_TECHNOLOGY_PATH “/sys/class/power_supply/battery/technology”
3、数据传送
BatteryService主动把数据传送给所关心的应用程序,所有的电池的信息数据是通过 Intent传送出去
的。
在 BatteryService.java中, Code如下:
- Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED);
- intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
- intent.putExtra(“status”, mBatteryStatus);
- intent.putExtra(“health”, mBatteryHealth);
- intent.putExtra(“present”, mBatteryPresent);
- intent.putExtra(“level”, mBatteryLevel);
- intent.putExtra(“scale”, BATTERY_SCALE);
- intent.putExtra(“icon-small”, icon);
- intent.putExtra(“plugged”, mPlugType);
- intent.putExtra(“voltage”, mBatteryVoltage);
- intent.putExtra(“temperature”, mBatteryTemperature);
- intent.putExtra(“technology”, mBatteryTechnology);
- ActivityManagerNative.broadcastStickyIntent(intent, null);
4、数据接收
应用如果想要接收到 BatteryService发送出来的电池信息,
则需要注册一个 Intent为 Intent.ACTION_BATTERY_CHANGED的 BroadcastReceiver。
注册方法如下:
- IntentFilter mIntentFilter = new IntentFilter();
- mIntentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
- registerReceiver(mIntentReceiver, mIntentFilter);
- private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent) {
-
- String action = intent.getAction();
- if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
- int nVoltage = intent.getIntExtra(“voltage”, 0);
- if(nVoltage!=0){
- mVoltage.setText(“V: ” + nVoltage + “mV – Success…”);
- }
- else{
- mVoltage.setText(“V: ” + nVoltage + “mV – fail…”);
- }
- }
- }
- };
5、数据更新
电池的信息会随着时间不停变化,自然地,就需要考虑如何实时的更新电池的数据信息。在
BatteryService启动的时候,会同时通过 UEventObserver启动一个 onUEvent Thread。每一个
Process最多只能有一个 onUEvent Thread,即使这个 Process中有多个 UEventObserver的实例。
当在一个 Process中,第一次 Call startObserving()方法后,这个 UEvent thread就启动了。
而一旦这个 UEvent thread启动之后,就不会停止。
//在 BatteryService.java中
- mUEventObserver.startObserving(“SUBSYSTEM=power_supply”);
- private UEventObserver mUEventObserver = new UEventObserver() {
- @Override
- public void onUEvent(UEventObserver.UEvent event) {
- update();
- }
- };
在 UEvent thread
中会不停调用 update()
方法,来更新电池的信息数据。
http://blog.csdn.net/ericyelai/article/details/6221308
阅读(868) | 评论(0) | 转发(0) |