Android GPS学习笔记—JNI层实现

来源:互联网 发布:r软件使用 编辑:程序博客网 时间:2024/04/29 20:16

Android GPS JNI层只有一个文件,起到承上启下的作用。上层承接Framework,下层调用HAL层具体硬件抽象实现。

目录:

frameworks/base/services/core/jni/com_android_server_location_GpsLocationProvider.cpp


首先来看注册JNI方法的函数定义:

int register_android_server_location_GpsLocationProvider(JNIEnv* env){    return jniRegisterNativeMethods(            env,            "com/android/server/location/GpsLocationProvider",            sMethods,            NELEM(sMethods));}

此函数被同目录下onload.cpp文件调用,调用地方在:

extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved){    JNIEnv* env = NULL;    jint result = -1;    if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {        ALOGE("GetEnv failed!");        return result;    }    ALOG_ASSERT(env, "Could not retrieve the env!");    //... ...省略其他注册代码    register_android_server_location_GpsLocationProvider(env);    return JNI_VERSION_1_4;}

从这里可以看到,JNI初始化的时候,即会进行JNI方法的注册,从而使上层应用通过JNI调用C/C++本地的方法


com_android_server_location_GpsLocationProvider.cpp中,数组sMethods定义如下:

static JNINativeMethod sMethods[] = {     /* name, signature, funcPtr */    {"class_init_native", "()V", (void *)android_location_GpsLocationProvider_class_init_native},    {"native_is_supported", "()Z", (void*)android_location_GpsLocationProvider_is_supported},    {"native_init", "()Z", (void*)android_location_GpsLocationProvider_init},    {"native_cleanup", "()V", (void*)android_location_GpsLocationProvider_cleanup},    {"native_set_position_mode",            "(IIIII)Z",            (void*)android_location_GpsLocationProvider_set_position_mode},    {"native_start", "()Z", (void*)android_location_GpsLocationProvider_start},    {"native_stop", "()Z", (void*)android_location_GpsLocationProvider_stop},    {"native_delete_aiding_data",            "(I)V",            (void*)android_location_GpsLocationProvider_delete_aiding_data},    {"native_read_sv_status",            "([I[F[F[F[I)I",            (void*)android_location_GpsLocationProvider_read_sv_status},    {"native_read_nmea", "([BI)I", (void*)android_location_GpsLocationProvider_read_nmea},    {"native_inject_time", "(JJI)V", (void*)android_location_GpsLocationProvider_inject_time},    {"native_inject_location",            "(DDF)V",            (void*)android_location_GpsLocationProvider_inject_location},    {"native_supports_xtra", "()Z", (void*)android_location_GpsLocationProvider_supports_xtra},    {"native_inject_xtra_data",            "([BI)V",            (void*)android_location_GpsLocationProvider_inject_xtra_data},    {"native_agps_data_conn_open",            "(Ljava/lang/String;I)V",            (void*)android_location_GpsLocationProvider_agps_data_conn_open},    {"native_agps_data_conn_closed",            "()V",            (void*)android_location_GpsLocationProvider_agps_data_conn_closed},    {"native_agps_data_conn_failed",            "()V",            (void*)android_location_GpsLocationProvider_agps_data_conn_failed},    {"native_agps_set_id",            "(ILjava/lang/String;)V",            (void*)android_location_GpsLocationProvider_agps_set_id},    {"native_agps_set_ref_location_cellid",            "(IIIII)V",            (void*)android_location_GpsLocationProvider_agps_set_reference_location_cellid},    {"native_set_agps_server",            "(ILjava/lang/String;I)V",            (void*)android_location_GpsLocationProvider_set_agps_server},    {"native_send_ni_response",            "(II)V",            (void*)android_location_GpsLocationProvider_send_ni_response},    {"native_agps_ni_message",            "([BI)V",            (void *)android_location_GpsLocationProvider_agps_send_ni_message},    {"native_get_internal_state",            "()Ljava/lang/String;",            (void*)android_location_GpsLocationProvider_get_internal_state},    {"native_update_network_state",            "(ZIZZLjava/lang/String;Ljava/lang/String;)V",            (void*)android_location_GpsLocationProvider_update_network_state },    {"native_is_geofence_supported",            "()Z",            (void*) android_location_GpsLocationProvider_is_geofence_supported},    {"native_add_geofence",            "(IDDDIIII)Z",            (void *)android_location_GpsLocationProvider_add_geofence},    {"native_remove_geofence",            "(I)Z",            (void *)android_location_GpsLocationProvider_remove_geofence},    {"native_pause_geofence", "(I)Z", (void *)android_location_GpsLocationProvider_pause_geofence},    {"native_resume_geofence",            "(II)Z",            (void *)android_location_GpsLocationProvider_resume_geofence},    {"native_is_measurement_supported",            "()Z",            (void*) android_location_GpsLocationProvider_is_measurement_supported},    {"native_start_measurement_collection",            "()Z",            (void*) android_location_GpsLocationProvider_start_measurement_collection},    {"native_stop_measurement_collection",            "()Z",            (void*) android_location_GpsLocationProvider_stop_measurement_collection},    {"native_is_navigation_message_supported",            "()Z",            (void*) android_location_GpsLocationProvider_is_navigation_message_supported},    {"native_start_navigation_message_collection",            "()Z",            (void*) android_location_GpsLocationProvider_start_navigation_message_collection},    {"native_stop_navigation_message_collection",            "()Z",            (void*) android_location_GpsLocationProvider_stop_navigation_message_collection},    {"native_configuration_update",            "(Ljava/lang/String;)V",            (void*)android_location_GpsLocationProvider_configuration_update},};
这里定义了GPS所有向上层提供的JNI本地方法,这些JNI方法是如何与HAL层交互的呢?

我们来看其中一个本地方法android_location_GpsLocationProvider_start的实现:

static jboolean android_location_GpsLocationProvider_start(JNIEnv* env, jobject obj){    if (sGpsInterface) {        if (sGpsInterface->start() == 0) {            return JNI_TRUE;        } else {            return JNI_FALSE;        }    }    else        return JNI_FALSE;}
其中,sGpsInterface是在函数android_location_GpsLocationProvider_class_init_native()中通过调用get_gps_interface()获得的GpsInterface接口,获取代码片段如下,函数android_location_GpsLocationProvider_start()直接调用了GpsInterface接口的start回调函数

    err = hw_get_module(GPS_HARDWARE_MODULE_ID, (hw_module_t const**)&module);    if (err == 0) {        hw_device_t* device;        err = module->methods->open(module, GPS_HARDWARE_MODULE_ID, &device);        if (err == 0) {            gps_device_t* gps_device = (gps_device_t *)device;            sGpsInterface = gps_device->get_gps_interface(gps_device);        }    }

以上代码可以看到,函数调用了hw_get_module加载硬件适配模块.so文件,接着通过hw_device_t接口调用open()函数,实际执行了gps/loc_api/libloc_api/gps.c定义的open_gps函数,然后调用gps_device_t接口的get_gps_interface函数,此函数也是在gps.c中定义的,最后返回HAL层中loc_eng.cpp文件的sLocEngInterface,从而打通了上层到底层的通道。







1 0
原创粉丝点击