之前發(fā)了一個(gè)圖描述了 Android GPS 架構

GPS芯片大多采用串口通信,所以只要CPU能夠提供一個(gè)UART 驅動(dòng), 那么跟GPS芯片通信是沒(méi)有問(wèn)題的,
但是現在很多GPS功能不是一個(gè)單獨的芯片,大部分都是一個(gè)芯片上有好幾種無(wú)線(xiàn)模塊 比如 Blletooth WiFi FM等
有時(shí)候 有些功能就會(huì )去復用一個(gè)UART,這樣在管理上就相對麻煩, 要將一個(gè)UART驅動(dòng), 模擬成一個(gè)HCI的接口而后才能使GPS功能(TI1281)
比較好的是我們的項目是單一的GPS chip 聽(tīng)說(shuō)apple也是用的這一款,不過(guò)人家拿的是全部的source code,而我們只有部分的code而沒(méi)有核心算法的code 很郁悶, 但是即便是只有部分code從里邊還是可以學(xué)到很多的機制
對于Android GPS驅動(dòng)來(lái)說(shuō)主要的工作是實(shí)現HAL層 計編譯生成一個(gè)叫做gps.***.so的動(dòng)態(tài)庫的 代碼.
由app發(fā)出定位申請,此時(shí)會(huì )去啟動(dòng)一個(gè)定位服務(wù)線(xiàn)程,此線(xiàn)程會(huì )調用到Location provider 服務(wù)在A(yíng)pp Framwork中,在次會(huì )通過(guò)被native方法調用到JNI中,JNI獲得GpsInterface并傳給接口一個(gè)回調函數的實(shí)現。
GpsInterface就是由HAL層最主要實(shí)現
介紹下gps.h下得重要相關(guān)結構題定義
在Android2.3中g(shù)ps.h定義在/hardware/inclue/hardware下
主要的結構
1、相關(guān)宏定義:
- 定位模式定義/** Requested operational mode for GPS operation. */
typedef uint32_t GpsPositionMode;
// IMPORTANT: Note that the following values must match
// constants in GpsLocationProvider.java.
/** Mode for running GPS standalone (no assistance). */
#define GPS_POSITION_MODE_STANDALONE 0 //純GPS 使用衛星定位
/** AGPS MS-Based mode. */
#define GPS_POSITION_MODE_MS_BASED 1 //基于用戶(hù)終端的A-GPS
/** AGPS MS-Assisted mode. */
#define GPS_POSITION_MODE_MS_ASSISTED 2 //基于用戶(hù)終端輔助A-GPS - 工作模式/** Requested recurrence mode for GPS operation. */
typedef uint32_t GpsPositionRecurrence;
// IMPORTANT: Note that the following values must match
// constants in GpsLocationProvider.java.
/** Receive GPS fixes on a recurring basis at a specified period. */
#define GPS_POSITION_RECURRENCE_PERIODIC 0//周期性的
/** Request a single shot GPS fix. */
#define GPS_POSITION_RECURRENCE_SINGLE 1//單次工作 - 輔助數據(在測試GPS性能時(shí)候又是需要刪除全部或部分的輔助數據)/** Flags used to specify which aiding data to delete
when calling delete_aiding_data(). */
typedef uint16_t GpsAidingData;
// IMPORTANT: Note that the following values must match
// constants in GpsLocationProvider.java.
#define GPS_DELETE_EPHEMERIS 0x0001
#define GPS_DELETE_ALMANAC 0x0002
#define GPS_DELETE_POSITION 0x0004
#define GPS_DELETE_TIME 0x0008
#define GPS_DELETE_IONO 0x0010
#define GPS_DELETE_UTC 0x0020
#define GPS_DELETE_HEALTH 0x0040
#define GPS_DELETE_SVDIR 0x0080
#define GPS_DELETE_SVSTEER 0x0100
#define GPS_DELETE_SADATA 0x0200
#define GPS_DELETE_RTI 0x0400
#define GPS_DELETE_CELLDB_INFO 0x8000
#define GPS_DELETE_ALL 0xFFFF - gps芯片可以處理的“能力”/** Flags for the gps_set_capabilities callback. */
/** GPS HAL schedules fixes for GPS_POSITION_RECURRENCE_PERIODIC mode.
If this is not set, then the framework will use 1000ms for min_interval
and will start and call start() and stop() to schedule the GPS.
*/
#define GPS_CAPABILITY_SCHEDULING 0x0000001/** GPS supports MS-Based AGPS mode */
#define GPS_CAPABILITY_MSB 0x0000002/** GPS supports MS-Assisted AGPS mode */
#define GPS_CAPABILITY_MSA 0x0000004/** GPS supports single-shot fixes */
#define GPS_CAPABILITY_SINGLE_SHOT 0x0000008 - 還有很多的定義此處不一一列舉 在gps.h中都有很好的解釋。
2、數據結構體
- GpsLocation
定義了一個(gè)表示方位的結構體,成員有經(jīng)緯度,高度,速度,方位角等。 - GpsStatus
表示GPS的當前狀態(tài),只有兩個(gè)成員一個(gè)是表示結構大小的成員,與一個(gè)表示Gps狀態(tài)的類(lèi)型GpsStatusValue/** GPS status event values. */
typedef uint16_t GpsStatusValue;
// IMPORTANT: Note that the following values must match
// constants in GpsLocationProvider.java.
/** GPS status unknown. */
#define GPS_STATUS_NONE 0/** GPS has begun navigating. */
#define GPS_STATUS_SESSION_BEGIN 1/** GPS has stopped navigating. */
#define GPS_STATUS_SESSION_END 2/** GPS has powered on but is not navigating. */
#define GPS_STATUS_ENGINE_ON 3/** GPS is powered off. */
#define GPS_STATUS_ENGINE_OFF 4 - GpsSvInfo
表示當前的衛星信息,有衛星編號,信號強度,衛星仰望角方位角等 - GpsSvStatus
表示衛星狀態(tài),包含了GpsSvInfo結構,可見(jiàn)衛星數,星歷時(shí)間,年歷時(shí)間,與用來(lái)定位的衛星的衛星構成的一個(gè)掩碼 - AGpsRefLocation/* 2G and 3G */
/* In 3G lac is discarded */
typedef struct {
uint16_t type;
uint16_t mcc;
uint16_t mnc;
uint16_t lac;
uint32_t cid;
} AGpsRefLocationCellID;
typedef struct {
uint8_t mac[6];
} AGpsRefLocationMac;
/** Represents ref locations */
typedef struct {
uint16_t type;
union {
AGpsRefLocationCellID cellID;
AGpsRefLocationMac mac;
} u;
} AGpsRefLocation;
/** Represents the status of AGPS. */
typedef struct {
/** set to sizeof(AGpsStatus) */
size_t size;
AGpsType type;
AGpsStatusValue status;
} AGpsStatus
3、回調函數指針定義
/* Callback with location information. 告知上層位置信息*/
typedef void (* gps_location_callback)(GpsLocation* location);
/** Callback with status information.GPS狀態(tài)信息回調*/
typedef void (* gps_status_callback)(GpsStatus* status);
/** Callback with SV status information.衛星狀態(tài)信息回調 */
typedef void (* gps_sv_status_callback)(GpsSvStatus* sv_info);
/** Callback for reporting NMEA sentences. 向上層傳遞NEMA數據*/
typedef void (* gps_nmea_callback)(GpsUtcTime timestamp, const char* nmea, int length);
/** Callback to inform framework of the GPS engine's capabilities.告知上層次GPS能實(shí)現那些功能 */
typedef void (* gps_set_capabilities)(uint32_t capabilities);
/** Callback utility for acquiring the GPS wakelock.gps上鎖 讓gps功能不會(huì )被suspend*/
typedef void (* gps_acquire_wakelock)();
/** Callback utility for releasing the GPS wakelock. 釋放鎖*/
typedef void (* gps_release_wakelock)();
/** Callback for creating a thread that can call into the Java framework code.thread create
在次線(xiàn)程處理函數中通常會(huì )由一個(gè)無(wú)限的循環(huán),去等待上層的請求,并應答*/
typedef pthread_t (* gps_create_thread)(const char* name, void (*start)(void *), void* arg);
4、接口結構體
- GpsCallbackstypedef struct {
/** set to sizeof(GpsCallbacks) */
size_t size;
gps_location_callback location_cb;
gps_status_callback status_cb;
gps_sv_status_callback sv_status_cb;
gps_nmea_callback nmea_cb;
gps_set_capabilities set_capabilities_cb;
gps_acquire_wakelock acquire_wakelock_cb;
gps_release_wakelock release_wakelock_cb;
gps_create_thread create_thread_cb;
} GpsCallbacks;
就是以上函數指針組成的結構體,是一組回調函數,函數的實(shí)現在JNI層是Android實(shí)現好了得,無(wú)需做(太大的)修改 - GpsInterface
Android GPS 最主要的數據結構,我們最主要的移植工作就是實(shí)現其中的函數/** Represents the standard GPS interface. */
typedef struct {
/** set to sizeof(GpsInterface) */
size_t size;
/*Opens the interface and provides the callback routines 初始化callbacks 為JNI傳下來(lái)的
在此會(huì )使用callbacks中的create_thread_cb 來(lái)創(chuàng )建一個(gè)線(xiàn)程處理函數一直循環(huán),等待任務(wù)處理*/
int (*init)( GpsCallbacks* callbacks );
/** Starts navigating. */
int (*start)( void );
/** Stops navigating. */
int (*stop)( void );
/** Closes the interface. */
void (*cleanup)( void );
/** Injects the current time. 填入時(shí)間*/
int (*inject_time)(GpsUtcTime time, int64_t timeReference,
int uncertainty);
/** Injects current location from another location provider 填入位置*/
int (*inject_location)(double latitude, double longitude, float accuracy);
/*刪除全部or部分輔助數據 在性能測試時(shí)候使用*/
void (*delete_aiding_data)(GpsAidingData flags);
/*設置定位模式 與GPS工作模式等等*/
int (*set_position_mode)(GpsPositionMode mode, GpsPositionRecurrence recurrence,
uint32_t min_interval, uint32_t preferred_accuracy, uint32_t preferred_time);
/*自行添加的 接口 AGpsInterface接口通過(guò)次函數獲得*/
const void* (*get_extension)(const char* name);
} GpsInterface; - AGpsCallbacks/** Callback with AGPS status information.
* Can only be called from a thread created by create_thread_cb.
*/
typedef void (* agps_status_callback)(AGpsStatus* status);
/** Callback structure for the AGPS interface. */
typedef struct {
agps_status_callback status_cb;
gps_create_thread create_thread_cb;
} AGpsCallbacks; - AGpsInterface/** Extended interface for AGPS support. */
typedef struct {
/** set to sizeof(AGpsInterface) */
size_t size;
/**
* Opens the AGPS interface and provides the callback routines
* to the implemenation of this interface.
*/
void (*init)( AGpsCallbacks* callbacks );
/**
* Notifies that a data connection is available and sets
* the name of the APN to be used for SUPL.
*/
int (*data_conn_open)( const char* apn );
/**
* Notifies that the AGPS data connection has been closed.
*/
int (*data_conn_closed)();
/**
* Notifies that a data connection is not available for AGPS.
*/
int (*data_conn_failed)();
/**
* Sets the hostname and port for the AGPS server.
*/
int (*set_server)( AGpsType type, const char* hostname, int port );
} AGpsInterface - struct gps_device_t
定義描述gps設備類(lèi)型struct gps_device_t {上層會(huì )通過(guò)動(dòng)態(tài)庫,獲得此結構,等到GpsInterface從而打通APP到GPS device的全部通信。
struct hw_device_t common;
/**
* Set the provided lights to the provided values.
*
* Returns: 0 on succes, error code on failure.
*/
const GpsInterface* (*get_gps_interface)(struct gps_device_t* dev);
}; - 還有 GpsNiInterface AGpsRilInterface等接口 在此不贅述


