Android模擬器學framework和driver之battery & backlight—–2. battery in jni

上篇/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) 
                gPaths.batteryTechnologyPath = strdup(path); 
        } 
    } 

closedir(dir); 
 
if (!gPaths.acOnlinePath) 
    LOGE("acOnlinePath not found"); 
if (!gPaths.usbOnlinePath) 
    LOGE("usbOnlinePath not found"); 
if (!gPaths.batteryStatusPath) 
    LOGE("batteryStatusPath not found"); 
if (!gPaths.batteryHealthPath) 
    LOGE("batteryHealthPath not found"); 
if (!gPaths.batteryPresentPath) 
    LOGE("batteryPresentPath not found"); 
if (!gPaths.batteryCapacityPath) 
    LOGE("batteryCapacityPath not found"); 
if (!gPaths.batteryVoltagePath) 
    LOGE("batteryVoltagePath not found"); 
if (!gPaths.batteryTemperaturePath) 
    LOGE("batteryTemperaturePath not found"); 
if (!gPaths.batteryTechnologyPath) 
    LOGE("batteryTechnologyPath not found"); 
    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)
                    gPaths.batteryTechnologyPath = strdup(path);
            }
        }
    }
    closedir(dir);

    if (!gPaths.acOnlinePath)
        LOGE("acOnlinePath not found");
    if (!gPaths.usbOnlinePath)
        LOGE("usbOnlinePath not found");
    if (!gPaths.batteryStatusPath)
        LOGE("batteryStatusPath not found");
    if (!gPaths.batteryHealthPath)
        LOGE("batteryHealthPath not found");
    if (!gPaths.batteryPresentPath)
        LOGE("batteryPresentPath not found");
    if (!gPaths.batteryCapacityPath)
        LOGE("batteryCapacityPath not found");
    if (!gPaths.batteryVoltagePath)
        LOGE("batteryVoltagePath not found");
    if (!gPaths.batteryTemperaturePath)
        LOGE("batteryTemperaturePath not found");
    if (!gPaths.batteryTechnologyPath)
        LOGE("batteryTechnologyPath not found");

2. 找到我們java文件中用到中battery信息相關的類

[cpp] jclass clazz = env->FindClass("com/android/server/BatteryService"); 
 
if (clazz == NULL) { 
    LOGE("Can't find com/android/server/BatteryService"); 
    return -1; 

    jclass clazz = env->FindClass("com/android/server/BatteryService");

    if (clazz == NULL) {
        LOGE("Can't find com/android/server/BatteryService");
        return -1;
    }

3. 得到我們java文件中定義的battery信息的變量的fieldID

[cpp] 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"); 
    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");

 

4.  讀取文件系統中相應的battery 信息的文件,通過fieldID給java中的變量賦值,進行battery狀態的更新。
[cpp] static void android_server_BatteryService_update(JNIEnv* env, jobject obj) 

    setBooleanField(env, obj, gPaths.acOnlinePath, gFieldIds.mAcOnline); 
    setBooleanField(env, obj, gPaths.usbOnlinePath, gFieldIds.mUsbOnline); 
    setBooleanField(env, obj, gPaths.batteryPresentPath, gFieldIds.mBatteryPresent); 
     
    setIntField(env, obj, gPaths.batteryCapacityPath, gFieldIds.mBatteryLevel); 
    setVoltageField(env, obj, gPaths.batteryVoltagePath, gFieldIds.mBatteryVoltage); 
    setIntField(env, obj, gPaths.batteryTemperaturePath, gFieldIds.mBatteryTemperature); 
     
    const int SIZE = 128; 
    char buf[SIZE]; 
     
    if (readFromFile(gPaths.batteryStatusPath, buf, SIZE) > 0) 
        env->SetIntField(obj, gFieldIds.mBatteryStatus, getBatteryStatus(buf)); 
    else 
        env->SetIntField(obj, gFieldIds.mBatteryStatus, 
                         gConstants.statusUnknown); 
     
    if (readFromFile(gPaths.batteryHealthPath, buf, SIZE) > 0) 
        env->SetIntField(obj, gFieldIds.mBatteryHealth, getBatteryHealth(buf)); 
 
    if (readFromFile(gPaths.batteryTechnologyPath, buf, SIZE) > 0) 
        env->SetObjectField(obj, gFieldIds.mBatteryTechnology, env->NewStringUTF(buf)); 

static void android_server_BatteryService_update(JNIEnv* env, jobject obj)
{
    setBooleanField(env, obj, gPaths.acOnlinePath, gFieldIds.mAcOnline);
    setBooleanField(env, obj, gPaths.usbOnlinePath, gFieldIds.mUsbOnline);
    setBooleanField(env, obj, gPaths.batteryPresentPath, gFieldIds.mBatteryPresent);
   
    setIntField(env, obj, gPaths.batteryCapacityPath, gFieldIds.mBatteryLevel);
    setVoltageField(env, obj, gPaths.batteryVoltagePath, gFieldIds.mBatteryVoltage);
    setIntField(env, obj, gPaths.batteryTemperaturePath, gFieldIds.mBatteryTemperature);
   
    const int SIZE = 128;
    char buf[SIZE];
   
    if (readFromFile(gPaths.batteryStatusPath, buf, SIZE) > 0)
        env->SetIntField(obj, gFieldIds.mBatteryStatus, getBatteryStatus(buf));
    else
        env->SetIntField(obj, gFieldIds.mBatteryStatus,
                         gConstants.statusUnknown);
   
    if (readFromFile(gPaths.batteryHealthPath, buf, SIZE) > 0)
        env->SetIntField(obj, gFieldIds.mBatteryHealth, getBatteryHealth(buf));

    if (readFromFile(gPaths.batteryTechnologyPath, buf, SIZE) > 0)
        env->SetObjectField(obj, gFieldIds.mBatteryTechnology, env->NewStringUTF(buf));
}

具體代碼自行分析,這邊我推薦我的一個關於JNI的專欄,那裡我寫的比較詳細,在jni是如何調用java中的成員變量和成員函數的。

http://blog.csdn.net/column/details/jnijni.html

jni層介紹到這邊,比較簡單。

 摘自 zhangjie201412的專欄
 

發佈留言