当前位置:操作系统 > 安卓/Android >>

Android模拟器学framework和driver之battery & backlight-----2. battery in jni

上篇http://www.zzzyk.com/kf/201203/121808.html介绍了模拟器的内核goldfish中的battery模块,也介绍了power_supply是如何工作的,一般我自己写驱动也差不多是这个流程,驱动抄着抄着自己就熟悉了,呵呵,记得之前我问过一个前辈,驱动要怎么学习,她说:抄代码先!!!

废话不多扯,这里介绍android jni层是如何处理battery的信息的。

上一篇中讲到在driver中做的事情对我们user有用的就只有2件事情,一是当battery信息发生变化的时候会给用户层发送一个uevent,二是在在文件系统中生成了battery信息的文件,包括电池电量,电池状态灯信息。

这里介绍中间层jni,主要封装读取battery信息的函数,代码位置:android2.3.3/frameworks/base/services/jni/com_android_server_BatteryService.cpp

这里做的事情比较简单:

1. 找到文件系统中我们生成的battery信息的文件路径

2. 找到我们java文件中用到中battery信息相关的类

3. 得到我们java文件中定义的battery信息的变量的fieldID

4.  读取文件系统中相应的battery 信息的文件,通过fieldID给java中的变量赋值,进行battery状态的更新。

 


1. 找到文件系统中我们生成的battery信息的文件路径


[cpp] DIR* dir = opendir(POWER_SUPPLY_PATH); 
if (dir == NULL) { 
    LOGE("Could not open %s\n", POWER_SUPPLY_PATH); 
    return -1; 

while ((entry = readdir(dir))) { 
    const char* name = entry->d_name; 
 
    // ignore "." and ".."  
    if (name[0] == '.' && (name[1] == 0 || (name[1] == '.' && name[2] == 0))) { 
        continue; 
    } 
 
    char buf[20]; 
    // Look for "type" file in each subdirectory  
    snprintf(path, sizeof(path), "%s/%s/type", POWER_SUPPLY_PATH, name); 
    int length = readFromFile(path, buf, sizeof(buf)); 
    if (length > 0) { 
        if (buf[length - 1] == '\n') 
            buf[length - 1] = 0; 
 
        if (strcmp(buf, "Mains") == 0) { 
            snprintf(path, sizeof(path), "%s/%s/online", POWER_SUPPLY_PATH, name); 
            if (access(path, R_OK) == 0) 
                gPaths.acOnlinePath = strdup(path); 
        } 
        else if (strcmp(buf, "USB") == 0) { 
            snprintf(path, sizeof(path), "%s/%s/online", POWER_SUPPLY_PATH, name); 
            if (access(path, R_OK) == 0) 
                gPaths.usbOnlinePath = strdup(path); 
        } 
        else if (strcmp(buf, "Battery") == 0) { 
            snprintf(path, sizeof(path), "%s/%s/status", POWER_SUPPLY_PATH, name); 
            if (access(path, R_OK) == 0) 
                gPaths.batteryStatusPath = strdup(path); 
            snprintf(path, sizeof(path), "%s/%s/health", POWER_SUPPLY_PATH, name); 
            if (access(path, R_OK) == 0) 
                gPaths.batteryHealthPath = strdup(path); 
            snprintf(path, sizeof(path), "%s/%s/present", POWER_SUPPLY_PATH, name); 
            if (access(path, R_OK) == 0) 
                gPaths.batteryPresentPath = strdup(path); 
            snprintf(path, sizeof(path), "%s/%s/capacity", POWER_SUPPLY_PATH, name); 
            if (access(path, R_OK) == 0) 
                gPaths.batteryCapacityPath = strdup(path); 
 
            snprintf(path, sizeof(path), "%s/%s/voltage_now", POWER_SUPPLY_PATH, name); 
            if (access(path, R_OK) == 0) { 
                gPaths.batteryVoltagePath = strdup(path); 
                // voltage_now is in microvolts, not millivolts  
                gVoltageDivisor = 1000; 
            } else { 
                snprintf(path, sizeof(path), "%s/%s/batt_vol", POWER_SUPPLY_PATH, name); 
                if (access(path, R_OK) == 0) 
                    gPaths.batteryVoltagePath = strdup(path); 
            } 
 
            snprintf(path, sizeof(path), "%s/%s/temp", POWER_SUPPLY_PATH, name); 
            if (access(path, R_OK) == 0) { 
                gPaths.batteryTemperaturePath = strdup(path); 
            } else { 
                snprintf(path, sizeof(path), "%s/%s/batt_temp", POWER_SUPPLY_PATH, name); 
                if (access(path, R_OK) == 0) 
                    gPaths.batteryTemperaturePath = strdup(path); 
            } 
 
            snprintf(path, sizeof(path), "%s/%s/technology", POWER_SUPPLY_PATH, name); 
            if (access(path, R_OK) == 0) 
   &nb

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,