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

上篇介绍了模拟器的内核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信息的文件路径

    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信息相关的类

    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

    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状态的更新。

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层介绍到这边,比较简单。

你可能感兴趣的:(java,android,server,jni,Access,Path)