1、GPS架構
2、GPS代碼簡介
2.1 頭檔案
我們先從頭檔案(hardware/libhardware/include/hardware/gps.h)入手,看一下android原生态代碼中提供了什麼結構體等。
結構體:GpsLocation -- 提供了經緯度、速度、方位角、高度、精度、UTC時間戳資訊;
GpsStatus -- 提供GPS定位狀态,一共有五種:
#define GPS_STATUS_NONE 0//未知狀态
#define GPS_STATUS_SESSION_BEGIN 1 //已經開始導航
#define GPS_STATUS_SESSION_END 2//停止導航
#define GPS_STATUS_ENGINE_ON 3//已經通電但沒有導航
#define GPS_STATUS_ENGINE_OFF 4//沒有通電狀态
GpsSvInfo -- 可見衛星資訊,衛星僞随機數、信号強度、海拔、方位角(但是有人說是衛星編号,信号強度,衛星仰望角,方位角,待求證!)
GpsSvStatus -- 可見衛星狀态。可見衛星數、一組可見衛星的衛星資訊(GpsSvInfo)、星曆(ephemeris)、曆書(almanac)、參與定位定位的衛星。
GpsCallbacks -- 回調函數。其中包括8個回調函數:location_callback、status_callback、sv_status_callback、nmea_callback、set_capabilities、acquire_wakelock、release_wakelock、create_thread。
GpsInterface -- 接口定義,包括8個接口:init、start、stop、cleanup、inject_time、inject_location、delete_aiding_data、set_position_mode。
(callback與interface可以這麼了解:callback在别的地方實作的,我來調用(call),以傳回我的資訊(back);initerface是我實作的,提供了一個接口(face),供别人調用)
gps_device_t --繼承自hw_device_tcommon,硬體适配接口,向上層提供了重要的get_gps_interface接口。
2.2 子產品和回調函數的注冊
GPS采用的是HAL Stub架構,這種架構被概括為:三個結構體、兩個常量、一個函數,具體參考http://blog.csdn.net/mr_raptor/article/details/8074549,(感謝老師講解)這裡就不做介紹了。
注冊過程中,調用open_gps();得到device,而dev->get_gps_interface = gps__get_gps_interface,進而得到GpsInterface,上層即可使用init等8個接口了;上層在使用Init接口時傳入了gpsCallbacks。這樣上下就建立了聯系,接下來gps_state_init初始化GPS序列槽,建立gps_state_thread,然後啟動for(;;)循環主要着NMEA資料的讀出、解析、上報等。
3、JNI中間層
GPSJNI适配層的源碼位于:frameworks/base/services/jni/com_android_server_location_GpsLocationProvider.cpp
如想深入了解JNI的工作機理和中間件的作用,請參考http://blog.csdn.net/mr_raptor/article/details/8082360。
首先看注冊JNI方法的函數定義:
[plain] view plaincopy
- int register_android_server_location_GpsLocationProvider(JNIEnv* env)
- {
- return jniRegisterNativeMethods(env, "com/android/server/location/GpsLocationProvider", sMethods, NELEM(sMethods));
- }
此函數被同目錄下onload.cpp檔案調用,調用地方在:
[cpp] view plaincopy
- 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) {
- LOGE("GetEnv failed!");
- return result;
- }
- LOG_ASSERT(env, "Could not retrieve the env!");
- //...省略其他注冊代碼
- register_android_server_location_GpsLocationProvider(env);
- return JNI_VERSION_1_4;
- }
從這裡可以看到,JNI初始化的時候,即會進行JNI方法的注冊,進而使上層應用能通過JNI調用c/c++本地方法。
回到register_android_server_location_GpsLocationProvider函數,變量sMethods定義如下:
[cpp] view plaincopy
- static JNINativeMethod sMethods[] = {
- {"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;)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", "(ZIZLjava/lang/String;)V", (void*)android_location_GpsLocationProvider_update_network_state },
- };
JNI是framework與HAL的中間信使,這些函數值得我們認真研究。JNI中,函數的簽名的得到,可以參考http://hi.baidu.com/scuyangl/item/414a039590ef82b5cd80e5dd。在HAL與JNI間調用依靠callback和interface,在framework和JNI間依靠函數Regist(regist是JNI與framework的函數映射,framework可以通過對應的函數調用到JNI的對應函數)和GetMethodID(getMethodID得到的函數在framework實作,JNI可以使用)。
上面函數都是在該.cpp檔案下實作的,比如:
- static jboolean android_location_GpsLocationProvider_is_supported(JNIEnv* env, jclass clazz) {
- return (sGpsInterface != NULL || get_gps_interface() != NULL);
- }
4、Java Framework
framework層代碼比較複雜,可參考:http://blog.csdn.net/lbmygf/article/details/6525195、http://blog.csdn.net/lbmygf/article/details/652519)
本文參考:
http://blog.csdn.net/xnwyd/article/details/7198728
http://blog.csdn.net/liukun321/article/details/8871963
http://blog.csdn.net/mr_raptor/article/details/8082360
http://blog.csdn.net/lbmygf/article/details/6525195