以下摘自 Daniel Wood 的博客:
http://blogold.chinaunix.net/u4/123238/article_150426.html
Android GPS 架构分析(gps 启动过程图)
Gps 启动过程图(基于 Google Android 2.2 代码)
下面再贴一张从 GoogleI/O 大会文档里面截来的图
Android GPS 架构分析-preview
Android GPS 架构分析
Daniel Wood 20101222
转载时请注明出处和作者
文章出处:http://danielwood.cublog.cn
作者:Daniel Wood
----------------------------------------------------------
看 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 中。
/** Represents the standard GPS interface. */
typedef struct {
/**
* Opens the interface and provides the callback routines
* to the implemenation of this interface.
*/
int (*init)( GpsCallbacks* callbacks );
/** Starts navigating. */
int (*start)( void );
/** Stops navigating. */
int (*stop)( void );
/** Closes the interface. */
void (*cleanup)( void );
/** Injects the current time. */
int (*inject_time)(GpsUtcTime time, int64_t timeReference,
uncertainty);
int
/** Injects current location from another location provider
* (typically cell ID).
* latitude and longitude are measured in degrees
* expected accuracy is measured in meters
*/
int (*inject_location)(double latitude, double longitude,
float accuracy);
/**
* Specifies that the next call to start will not use the
* information defined in the flags. GPS_DELETE_ALL is
passed for
* a cold start.
*/
void (*delete_aiding_data)(GpsAidingData flags);
/**
* fix_frequency represents the time between fixes in
seconds.
* Set fix_frequency to zero for a single-shot fix.
*/
int (*set_position_mode)(GpsPositionMode mode, int
fix_frequency);
/** Get a pointer to extension information. */
const void* (*get_extension)(const char* name);
} GpsInterface;
2. GpsCallbacks 回调函数
这个是回调函数结构体,定义也在 gps.h 中。它们的实现是在
android_location_GpsLocationProvider.cpp 中,google 已经实现了,我们
不需要做任何动作。
/** GPS callback structure. */
typedef struct {
gps_location_callback location_cb;
gps_status_callback status_cb;
gps_sv_status_callback sv_status_cb;
gps_nmea_callback nmea_cb;
} GpsCallbacks;
/** Callback with location information. */
typedef void (* gps_location_callback)(GpsLocation* location);
/** Callback with status information. */
typedef void (* gps_status_callback)(GpsStatus* status);
/** Callback with SV status information. */
typedef void (* gps_sv_status_callback)(GpsSvStatus* sv_info);
/** Callback for reporting NMEA sentences. */
typedef void (* gps_nmea_callback)(GpsUtcTime timestamp, const
char* nmea, int length);
3. GpsLocation
表示 Locatin 数据信息,底层驱动获得 Location 的 raw 信息,通常是 nmea
码,然后通过解析就得到了 location 信息。
/** Represents a location. */
typedef struct {
/** Contains GpsLocationFlags bits. */
uint16_t flags;
/** Represents latitude in degrees. */
double latitude;
/** Represents longitude in degrees. */
double longitude;
/** Represents altitude in meters above the WGS 84 reference
* ellipsoid. */
double altitude;
/** Represents speed in meters per second. */
float speed;
/** Represents heading in degrees. */
float bearing;
/** Represents expected accuracy in meters. */
float accuracy;
/** Timestamp for the location fix. */
GpsUtcTime timestamp;
} GpsLocation;
Android GPS 架构分析(一)
Android GPS 架构分析
Daniel Wood 20101222
转载时请注明出处和作者
文章出处:http://danielwood.cublog.cn
作者:Daniel Wood
--------------------------------------------------------------------
介绍完了主体代码结构以及重要的数据结构后,下面来看看 gps 的定位服
务(LocationManager)的启动过程。我总是喜欢追本溯源地从源头去认识事物。
因为“人之初,性本善”,从事物的本性去认识事物。
LocationManager 这项服务是在 SystemServer.java 中启动的,也就是系统启
动之后,这个服务就已经启动了:
systemServer.java [framework\base\services\java\com\android\server]
在 SystemServer.java 的 init2 函数中启动了一个线程来注册 Android 的诸
多服务,如:Bluetooth Service,NetworkManagement Service,Notification
Manager 等,当然也包括 Location Service。
SystemServer.java [frameworks\base\services\java\com\android\server]
public static final void init2() {
Slog.i(TAG, "Entered the Android system server!");
Thread thr = new ServerThread();
thr.setName("android.server.ServerThread");
thr.start();
}
在 ServerThread 线程的 run 函数中 LocationManager 服务的代码段如下:
2.1 版本
try {
Log.i(TAG, "Location Manager");
ServiceManager.addService(Context.L
OCATION_SERVICE, new LocationManagerService(context));
} catch (Throwable e) {
Log.e(TAG, "Failure starting Location
Manager", e);
}
2.2 的代码中代码段如下形式:
try {
LocationManagerService(context);
OCATION_SERVICE, location);
Slog.i(TAG, "Location Manager");
location = new
ServiceManager.addService(Context.L
} catch (Throwable e) {
Slog.e(TAG, "Failure starting
Location Manager
在 run 函数的后半部分,是服务对系统的反馈,就是 systemReady()函数。
LocationManager 服务的反馈函数如下: