android Sensors学习笔记

SensorManager的构造函数调用sensors_module_init();,以及间接调用get_sensors_list

 

              // initialize the sensor list
                sensors_module_init();
                final ArrayList<Sensor> fullList = sFullSensorsList;
                int i = 0;
                do {
                    Sensor sensor = new Sensor();
                    i = sensors_module_get_next_sensor(sensor, i);

                    if (i>=0) {
                        //Log.d(TAG, "found sensor: " + sensor.getName() +
                        //        ", handle=" + sensor.getHandle());
                        sensor.setLegacyType(getLegacySensorType(sensor.getType()));
                        fullList.add(sensor);
                        sHandleToSensor.append(sensor.getHandle(), sensor);
                    }
                } while (i>0);

                sSensorThread = new SensorThread();

 

sensors_module_init是一个JNI函数

static jint
sensors_module_init(JNIEnv *env, jclass clazz)
{
    int err = 0;
    sensors_module_t const* module;
    err = hw_get_module(SENSORS_HARDWARE_MODULE_ID, (const hw_module_t **)&module);
    if (err == 0)
        sSensorModule = (sensors_module_t*)module;
    return err;
}

hw_get_module函数是一个hal层的标准API函数调用,它通过SENSORS_HARDWARE_MODULE_ID名字得到一个hw_module_t对象。

这样就得到了sensors_module_t对象,sensors_module_t对象是在sensor.c中定义的

const struct sensors_module_t HAL_MODULE_INFO_SYM = {
    .common = {
        .tag            = HARDWARE_MODULE_TAG,
        .version_major  = SENSOR_HAL_VERSION_MAJOR,
        .version_minor  = SENSOR_HAL_VERSION_MINOR,
        .id             = SENSORS_HARDWARE_MODULE_ID,
        .name           = "Yamaha Sensors module",
        .author         = "Yamaha Corporation",
        .methods        = &sensors_module_methods,
    },
    .get_sensors_list   = hal_get_sensors_list,
};
HAL_MODULE_INFO_SYM这个名字是固定的。

通过sensors_module_init在JNI层就给初始化了sSensorModule 对象,以后的函数调用就会使用sSensorModule

比如通过get_sensors_list方法得到sensor_list

static jint
sensors_module_get_next_sensor(JNIEnv *env, jobject clazz, jobject sensor, jint next)
{
    if (sSensorModule == NULL)
        return 0;

    SensorOffsets& sensorOffsets = gSensorOffsets;
    const struct sensor_t* list;
    int count = sSensorModule->get_sensors_list(sSensorModule, &list);
    if (next >= count)
        return -1;
   
    list += next;

    jstring name = env->NewStringUTF(list->name);
    jstring vendor = env->NewStringUTF(list->vendor);
    env->SetObjectField(sensor, sensorOffsets.name,      name);
    env->SetObjectField(sensor, sensorOffsets.vendor,    vendor);
    env->SetIntField(sensor, sensorOffsets.version,      list->version);
    env->SetIntField(sensor, sensorOffsets.handle,       list->handle);
    env->SetIntField(sensor, sensorOffsets.type,         list->type);
    env->SetFloatField(sensor, sensorOffsets.range,      list->maxRange);
    env->SetFloatField(sensor, sensorOffsets.resolution, list->resolution);
    env->SetFloatField(sensor, sensorOffsets.power,      list->power);
   
    next++;
    return next<count ? next : 0;
}

在 SensorThread对象的构造函数里面调用 sensors_data_init

       SensorThread() {
            // this gets to the sensor module. We can have only one per process.
            sensors_data_init();
        }

static int
haldevice_open(const struct hw_module_t* module, const char* name,
                    struct hw_device_t** device)
是static struct hw_module_methods_t sensors_module_methods = {
    .open = haldevice_open,
};的一个方法,会根据name创建dev = malloc(sizeof(struct sensors_control_device_t))或者

(dev = malloc(sizeof(struct sensors_data_device_t))) == 0)

联系起来的方法是

static jint
sensors_data_init(JNIEnv *env, jclass clazz)
{
    if (sSensorModule == NULL)
        return -1;
    int err = sensors_data_open(&sSensorModule->common, &sSensorDevice);
    return err;
}

static inline int sensors_data_open(const struct hw_module_t* module,
        struct sensors_data_device_t** device) {
    return module->methods->open(module,
            SENSORS_HARDWARE_DATA, (struct hw_device_t**)device);
}

所以在线程构造的时候,将获得sensors_data_device_t对象。

在  线程类的run函数中              while (true) {
                    // wait for an event
                    final int sensor = sensors_data_poll(values, status, timestamp);

不停的查询数据。

在线程类的open函数中调用 sensors_data_open(fds, ints);

获得所有的监控设备文件的文件描述符fd。

static native_handle_t*
halcontrol_open_data_source(struct sensors_control_device_t *dev)

负责打开所有的输入文件。

static int
haldata_data_open(struct sensors_data_device_t *dev, native_handle_t*handle)

只是复制handle里面已经被halcontrol_open_data_source已经打开了的文件句柄。

在SensorService对象中

    public SensorService(Context context) {
        if (localLOGV) Slog.d(TAG, "SensorService startup");
        _sensors_control_init();
    }
   
    public Bundle getDataChannel() throws RemoteException {
        // synchronize so we do not require sensor HAL to be thread-safe.
        synchronized(mListeners) {
            return _sensors_control_open();
        }
    }

static jint
android_init(JNIEnv *env, jclass clazz)
{
    sensors_module_t* module;
    if (hw_get_module(SENSORS_HARDWARE_MODULE_ID, (const hw_module_t**)&module) == 0) {
        if (sensors_control_open(&module->common, &sSensorDevice) == 0) {
            const struct sensor_t* list;
            int count = module->get_sensors_list(module, &list);
            return count;
        }
    }
    return 0;
}

static jobject
android_open(JNIEnv *env, jclass clazz)
{
    native_handle_t* handle = sSensorDevice->open_data_source(sSensorDevice);
    if (!handle) {
        return NULL;
    }

和sensor_data_device打开的设备是不同的。

static inline int sensors_control_open(const struct hw_module_t* module,
        struct sensors_control_device_t** device) {
    return module->methods->open(module,
            SENSORS_HARDWARE_CONTROL, (struct hw_device_t**)device);
}

你可能感兴趣的:(android Sensors学习笔记)