Android GPS架构分析

看Android的GPS模块有两个月了吧,终于可以写点东西出来。首先来看看GPS模块的代码结构:
Framework:
1.frameworks/base/location/java/android/location
这里主要是用来被App调用的,API包是android.location。
2.frameworks/base/location/java/com/android/internal/location
这个目录是Framework对Location服务的内部实现。
3.framework\services\java\com\android\server
这个目录只有一个文件
|-- LocationManagerService.java
是Location服务对内部实现的一种封装。

JNI:
frameworks/base/core/jni/android_location_GpsLocationProvider.cpp
JNI层只有一个文件,起到承上启下的作用。上层承接Framework,下层调用HAL层具体硬件抽象实现。

HAL:Hardware Abstract Layer 硬件抽象层
hardware\libhardware_legacy\gps
hardware\libhardware_legacy\include\hardware_legacy\gps.h
HAL层相当于一个linux应用程序接口,通过open,close等操作,操作硬件设备。Android的源代码只实现了模拟器的gps接口,具体在文件gps_qemu.c中。在2.2版本中提供了对QCOM公司的gps的实现,在以下目录:
\hardware\qcom

 
下面介绍几个重要的数据结构:

1. GpsInterface接口是gps模块中最重要的数据结构,它是底层驱动实现的接口,如果要porting到自己的板子上,就需要实现这些接口。该接口的定义在gps.h中,模拟器实现在gps_qemu.c中。

<span style="color:#000000;"><font face="Courier New"><span style="color:#ff9900;">/** Represents the standard GPS interface. */</span><br /><span style="color:#0000ff;">typedef</span> <span style="color:#0000ff;">struct</span> <span style="color:#0000cc;">{</span><br />    </font><font face="Courier New"><span style="color:#ff9900;">/**<br />     * Opens the interface and provides the callback routines<br />     * to the implemenation of this interface.<br />     */</span><br />    <span style="color:#0000ff;">int</span> <span style="color:#0000cc;">(</span><span style="color:#0000cc;">*</span>init<span style="color:#0000cc;">)</span><span style="color:#0000cc;">(</span> GpsCallbacks<span style="color:#0000cc;">*</span> callbacks <span style="color:#0000cc;">)</span><span style="color:#0000cc;">;</span><br />    <span style="color:#ff9900;">/** Starts navigating. */</span><br />    <span style="color:#0000ff;">int</span> <span style="color:#0000cc;">(</span><span style="color:#0000cc;">*</span>start<span style="color:#0000cc;">)</span><span style="color:#0000cc;">(</span> <span style="color:#0000ff;">void</span> <span style="color:#0000cc;">)</span><span style="color:#0000cc;">;</span><br />    <span style="color:#ff9900;">/** Stops navigating. */</span><br />    <span style="color:#0000ff;">int</span> <span style="color:#0000cc;">(</span><span style="color:#0000cc;">*</span>stop<span style="color:#0000cc;">)</span><span style="color:#0000cc;">(</span> <span style="color:#0000ff;">void</span> <span style="color:#0000cc;">)</span><span style="color:#0000cc;">;</span><br />    <span style="color:#ff9900;">/** Closes the interface. */</span><br />    <span style="color:#0000ff;">void</span> <span style="color:#0000cc;">(</span><span style="color:#0000cc;">*</span>cleanup<span style="color:#0000cc;">)</span><span style="color:#0000cc;">(</span> <span style="color:#0000ff;">void</span> <span style="color:#0000cc;">)</span><span style="color:#0000cc;">;</span><br />    <span style="color:#ff9900;">/** Injects the current time. */</span><br />    <span style="color:#0000ff;">int</span> <span style="color:#0000cc;">(</span><span style="color:#0000cc;">*</span>inject_time<span style="color:#0000cc;">)</span><span style="color:#0000cc;">(</span>GpsUtcTime <span style="color:#ff0000;">time</span><span style="color:#0000cc;">,</span> <span style="color:#ff0000;">int64_t</span> timeReference<span style="color:#0000cc;">,</span><br />                         <span style="color:#0000ff;">int</span> uncertainty<span style="color:#0000cc;">)</span><span style="color:#0000cc;">;</span><br />    </font><font face="Courier New"><span style="color:#ff9900;">/** Injects current location from another location provider<br />     * (typically cell ID).<br />     * latitude and longitude are measured in degrees<br />     * expected accuracy is measured in meters<br />     */</span><br />    <span style="color:#0000ff;">int</span> <span style="color:#0000cc;">(</span><span style="color:#0000cc;">*</span>inject_location<span style="color:#0000cc;">)</span><span style="color:#0000cc;">(</span><span style="color:#0000ff;">double</span> latitude<span style="color:#0000cc;">,</span> <span style="color:#0000ff;">double</span> longitude<span style="color:#0000cc;">,</span> <span style="color:#0000ff;">float</span> accuracy<span style="color:#0000cc;">)</span><span style="color:#0000cc;">;</span><br />    </font><font face="Courier New"><span style="color:#ff9900;">/**<br />     * Specifies that the next call to start will not use the<br />     * information defined in the flags. GPS_DELETE_ALL is passed for<br />     * a cold start.<br />     */</span><br />    <span style="color:#0000ff;">void</span> <span style="color:#0000cc;">(</span><span style="color:#0000cc;">*</span>delete_aiding_data<span style="color:#0000cc;">)</span><span style="color:#0000cc;">(</span>GpsAidingData flags<span style="color:#0000cc;">)</span><span style="color:#0000cc;">;</span><br />    </font><font face="Courier New"><span style="color:#ff9900;">/**<br />     * fix_frequency represents the time between fixes in seconds.<br />     * Set fix_frequency to zero for a single-shot fix.<br />     */</span><br />    <span style="color:#0000ff;">int</span> <span style="color:#0000cc;">(</span><span style="color:#0000cc;">*</span>set_position_mode<span style="color:#0000cc;">)</span><span style="color:#0000cc;">(</span>GpsPositionMode mode<span style="color:#0000cc;">,</span> <span style="color:#0000ff;">int</span> fix_frequency<span style="color:#0000cc;">)</span><span style="color:#0000cc;">;</span><br />    <span style="color:#ff9900;">/** Get a pointer to extension information. */</span><br />    <span style="color:#0000ff;">const</span> <span style="color:#0000ff;">void</span><span style="color:#0000cc;">*</span> <span style="color:#0000cc;">(</span><span style="color:#0000cc;">*</span>get_extension<span style="color:#0000cc;">)</span><span style="color:#0000cc;">(</span><span style="color:#0000ff;">const</span> <span style="color:#0000ff;">char</span><span style="color:#0000cc;">*</span> name<span style="color:#0000cc;">)</span><span style="color:#0000cc;">;</span><br /><span style="color:#0000cc;">}</span> GpsInterface<span style="color:#0000cc;">;</span><br /></font></span>


2. GpsCallbacks回调函数

这个是回调函数结构体,定义也在gps.h中。<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

它们的实现是在android_location_GpsLocationProvider.cpp中,google已经实现了,我们不需要做任何动作。

<span style="color:#000000;"><font face="Courier New"><span style="color:#ff9900;">/** GPS callback structure. */</span><br /><span style="color:#0000ff;">typedef</span> <span style="color:#0000ff;">struct</span> <span style="color:#0000cc;">{</span><br />        gps_location_callback location_cb<span style="color:#0000cc;">;</span><br />        gps_status_callback status_cb<span style="color:#0000cc;">;</span><br />        gps_sv_status_callback sv_status_cb<span style="color:#0000cc;">;</span><br />        gps_nmea_callback nmea_cb<span style="color:#0000cc;">;</span><br /><span style="color:#0000cc;">}</span> GpsCallbacks<span style="color:#0000cc;">;</span><br /><span style="color:#ff9900;">/** Callback with location information. */</span><br /><span style="color:#0000ff;">typedef</span> <span style="color:#0000ff;">void</span> <span style="color:#0000cc;">(</span><span style="color:#0000cc;">*</span> gps_location_callback<span style="color:#0000cc;">)</span><span style="color:#0000cc;">(</span>GpsLocation<span style="color:#0000cc;">*</span> location<span style="color:#0000cc;">)</span><span style="color:#0000cc;">;</span><br /><span style="color:#ff9900;">/** Callback with status information. */</span><br /><span style="color:#0000ff;">typedef</span> <span style="color:#0000ff;">void</span> <span style="color:#0000cc;">(</span><span style="color:#0000cc;">*</span> gps_status_callback<span style="color:#0000cc;">)</span><span style="color:#0000cc;">(</span>GpsStatus<span style="color:#0000cc;">*</span> status<span style="color:#0000cc;">)</span><span style="color:#0000cc;">;</span><br /><span style="color:#ff9900;">/** Callback with SV status information. */</span><br /><span style="color:#0000ff;">typedef</span> <span style="color:#0000ff;">void</span> <span style="color:#0000cc;">(</span><span style="color:#0000cc;">*</span> gps_sv_status_callback<span style="color:#0000cc;">)</span><span style="color:#0000cc;">(</span>GpsSvStatus<span style="color:#0000cc;">*</span> sv_info<span style="color:#0000cc;">)</span><span style="color:#0000cc;">;</span><br /><span style="color:#ff9900;">/** Callback for reporting NMEA sentences. */</span><br /><span style="color:#0000ff;">typedef</span> <span style="color:#0000ff;">void</span> <span style="color:#0000cc;">(</span><span style="color:#0000cc;">*</span> gps_nmea_callback<span style="color:#0000cc;">)</span><span style="color:#0000cc;">(</span>GpsUtcTime timestamp<span style="color:#0000cc;">,</span> <span style="color:#0000ff;">const</span> <span style="color:#0000ff;">char</span><span style="color:#0000cc;">*</span> nmea<span style="color:#0000cc;">,</span> <span style="color:#0000ff;">int</span> length<span style="color:#0000cc;">)</span><span style="color:#0000cc;">;</span><br /></font></span>


3. GpsLocation

表示Locatin数据信息,底层驱动获得Location的raw信息,通常是nmea码,然后通过解析就得到了location信息。

<span style="color:#000000;"><font face="Courier New"><span style="color:#ff9900;">/** Represents a location. */</span><br /><span style="color:#0000ff;">typedef</span> <span style="color:#0000ff;">struct</span> <span style="color:#0000cc;">{</span><br />    <span style="color:#ff9900;">/** Contains GpsLocationFlags bits. */</span><br />    <span style="color:#ff0000;">uint16_t</span> flags<span style="color:#0000cc;">;</span><br />    <span style="color:#ff9900;">/** Represents latitude in degrees. */</span><br />    <span style="color:#0000ff;">double</span> latitude<span style="color:#0000cc;">;</span><br />    <span style="color:#ff9900;">/** Represents longitude in degrees. */</span><br />    <span style="color:#0000ff;">double</span> longitude<span style="color:#0000cc;">;</span><br />    </font><font face="Courier New"><span style="color:#ff9900;">/** Represents altitude in meters above the WGS 84 reference<br />     * ellipsoid. */</span><br />    <span style="color:#0000ff;">double</span> altitude<span style="color:#0000cc;">;</span><br />    <span style="color:#ff9900;">/** Represents speed in meters per second. */</span><br />    <span style="color:#0000ff;">float</span> speed<span style="color:#0000cc;">;</span><br />    <span style="color:#ff9900;">/** Represents heading in degrees. */</span><br />    <span style="color:#0000ff;">float</span> bearing<span style="color:#0000cc;">;</span><br />    <span style="color:#ff9900;">/** Represents expected accuracy in meters. */</span><br />    <span style="color:#0000ff;">float</span> accuracy<span style="color:#0000cc;">;</span><br />    <span style="color:#ff9900;">/** Timestamp for the location fix. */</span><br />    GpsUtcTime timestamp<span style="color:#0000cc;">;</span><br /><span style="color:#0000cc;">}</span> GpsLocation<span style="color:#0000cc;">;</span></font><br /></span>

相关推荐