Android GPS学习笔记(3)—JNI层实现

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

目录:

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


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

[cpp]  view plain copy
  1. int register_android_server_location_GpsLocationProvider(JNIEnv* env)  
  2. {  
  3.     return jniRegisterNativeMethods(  
  4.             env,  
  5.             "com/android/server/location/GpsLocationProvider",  
  6.             sMethods,  
  7.             NELEM(sMethods));  
  8. }  

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

[cpp]  view plain copy
  1. extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved)  
  2. {  
  3.     JNIEnv* env = NULL;  
  4.     jint result = -1;  
  5.   
  6.     if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {  
  7.         ALOGE("GetEnv failed!");  
  8.         return result;  
  9.     }  
  10.     ALOG_ASSERT(env, "Could not retrieve the env!");  
  11.   
  12.     //... ...省略其他注册代码  
  13.   
  14.     register_android_server_location_GpsLocationProvider(env);  
  15.   
  16.     return JNI_VERSION_1_4;  
  17. }  

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


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

[cpp]  view plain copy
  1. static JNINativeMethod sMethods[] = {  
  2.      /* name, signature, funcPtr */  
  3.     {"class_init_native""()V", (void *)android_location_GpsLocationProvider_class_init_native},  
  4.     {"native_is_supported""()Z", (void*)android_location_GpsLocationProvider_is_supported},  
  5.     {"native_init""()Z", (void*)android_location_GpsLocationProvider_init},  
  6.     {"native_cleanup""()V", (void*)android_location_GpsLocationProvider_cleanup},  
  7.     {"native_set_position_mode",  
  8.             "(IIIII)Z",  
  9.             (void*)android_location_GpsLocationProvider_set_position_mode},  
  10.     {"native_start""()Z", (void*)android_location_GpsLocationProvider_start},  
  11.     {"native_stop""()Z", (void*)android_location_GpsLocationProvider_stop},  
  12.     {"native_delete_aiding_data",  
  13.             "(I)V",  
  14.             (void*)android_location_GpsLocationProvider_delete_aiding_data},  
  15.     {"native_read_sv_status",  
  16.             "([I[F[F[F[I)I",  
  17.             (void*)android_location_GpsLocationProvider_read_sv_status},  
  18.     {"native_read_nmea""([BI)I", (void*)android_location_GpsLocationProvider_read_nmea},  
  19.     {"native_inject_time""(JJI)V", (void*)android_location_GpsLocationProvider_inject_time},  
  20.     {"native_inject_location",  
  21.             "(DDF)V",  
  22.             (void*)android_location_GpsLocationProvider_inject_location},  
  23.     {"native_supports_xtra""()Z", (void*)android_location_GpsLocationProvider_supports_xtra},  
  24.     {"native_inject_xtra_data",  
  25.             "([BI)V",  
  26.             (void*)android_location_GpsLocationProvider_inject_xtra_data},  
  27.     {"native_agps_data_conn_open",  
  28.             "(Ljava/lang/String;I)V",  
  29.             (void*)android_location_GpsLocationProvider_agps_data_conn_open},  
  30.     {"native_agps_data_conn_closed",  
  31.             "()V",  
  32.             (void*)android_location_GpsLocationProvider_agps_data_conn_closed},  
  33.     {"native_agps_data_conn_failed",  
  34.             "()V",  
  35.             (void*)android_location_GpsLocationProvider_agps_data_conn_failed},  
  36.     {"native_agps_set_id",  
  37.             "(ILjava/lang/String;)V",  
  38.             (void*)android_location_GpsLocationProvider_agps_set_id},  
  39.     {"native_agps_set_ref_location_cellid",  
  40.             "(IIIII)V",  
  41.             (void*)android_location_GpsLocationProvider_agps_set_reference_location_cellid},  
  42.     {"native_set_agps_server",  
  43.             "(ILjava/lang/String;I)V",  
  44.             (void*)android_location_GpsLocationProvider_set_agps_server},  
  45.     {"native_send_ni_response",  
  46.             "(II)V",  
  47.             (void*)android_location_GpsLocationProvider_send_ni_response},  
  48.     {"native_agps_ni_message",  
  49.             "([BI)V",  
  50.             (void *)android_location_GpsLocationProvider_agps_send_ni_message},  
  51.     {"native_get_internal_state",  
  52.             "()Ljava/lang/String;",  
  53.             (void*)android_location_GpsLocationProvider_get_internal_state},  
  54.     {"native_update_network_state",  
  55.             "(ZIZZLjava/lang/String;Ljava/lang/String;)V",  
  56.             (void*)android_location_GpsLocationProvider_update_network_state },  
  57.     {"native_is_geofence_supported",  
  58.             "()Z",  
  59.             (void*) android_location_GpsLocationProvider_is_geofence_supported},  
  60.     {"native_add_geofence",  
  61.             "(IDDDIIII)Z",  
  62.             (void *)android_location_GpsLocationProvider_add_geofence},  
  63.     {"native_remove_geofence",  
  64.             "(I)Z",  
  65.             (void *)android_location_GpsLocationProvider_remove_geofence},  
  66.     {"native_pause_geofence""(I)Z", (void *)android_location_GpsLocationProvider_pause_geofence},  
  67.     {"native_resume_geofence",  
  68.             "(II)Z",  
  69.             (void *)android_location_GpsLocationProvider_resume_geofence},  
  70.     {"native_is_measurement_supported",  
  71.             "()Z",  
  72.             (void*) android_location_GpsLocationProvider_is_measurement_supported},  
  73.     {"native_start_measurement_collection",  
  74.             "()Z",  
  75.             (void*) android_location_GpsLocationProvider_start_measurement_collection},  
  76.     {"native_stop_measurement_collection",  
  77.             "()Z",  
  78.             (void*) android_location_GpsLocationProvider_stop_measurement_collection},  
  79.     {"native_is_navigation_message_supported",  
  80.             "()Z",  
  81.             (void*) android_location_GpsLocationProvider_is_navigation_message_supported},  
  82.     {"native_start_navigation_message_collection",  
  83.             "()Z",  
  84.             (void*) android_location_GpsLocationProvider_start_navigation_message_collection},  
  85.     {"native_stop_navigation_message_collection",  
  86.             "()Z",  
  87.             (void*) android_location_GpsLocationProvider_stop_navigation_message_collection},  
  88.     {"native_configuration_update",  
  89.             "(Ljava/lang/String;)V",  
  90.             (void*)android_location_GpsLocationProvider_configuration_update},  
  91. };  
这里定义了 GPS 所有向上层提供的 JNI 本地方法,这些JNI方法是如何与HAL层交互的呢?

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

[cpp]  view plain copy
  1. static jboolean android_location_GpsLocationProvider_start(JNIEnv* env, jobject obj)  
  2. {  
  3.     if (sGpsInterface) {  
  4.         if (sGpsInterface->start() == 0) {  
  5.             return JNI_TRUE;  
  6.         } else {  
  7.             return JNI_FALSE;  
  8.         }  
  9.     }  
  10.     else  
  11.         return JNI_FALSE;  
  12. }  
其中,sGpsInterface是在函数android_location_GpsLocationProvider_class_init_native()中通过 调用get_gps_interface()获得的GpsInterface接口 ,获取代码片段如下,函数android_location_GpsLocationProvider_start()直接调用 了GpsInterface 接口的start回调函数

[cpp]  view plain copy
  1. err = hw_get_module(GPS_HARDWARE_MODULE_ID, (hw_module_t const**)&module);  
  2. if (err == 0) {  
  3.     hw_device_t* device;  
  4.     err = module->methods->open(module, GPS_HARDWARE_MODULE_ID, &device);  
  5.     if (err == 0) {  
  6.         gps_device_t* gps_device = (gps_device_t *)device;  
  7.         sGpsInterface = gps_device->get_gps_interface(gps_device);  
  8.     }  
  9. }  

以上代码可以看到,函数调用了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,从而打通了上层到底层的通道。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值