贾玲更年期遇上青春期:android GPS application
来源:百度文库 编辑:九乡新闻网 时间:2024/04/29 00:04:47
HAL introdution:
HAL(hardware abstraction layer)存在的目的在与让android framework与linux device driver分离开来.上层应用通过Dalvik VM与core services【runtime】(如sensor service,camera service等)来加载动态库文件(*.so),这里的so文件指的是HAL的实现,core service通过JNI接口调用HAL层提供的接口,这样就实现了android 应用程序对硬件的操作。 在Android源码中,HAL的主要实现位于hardware/目录下,老的实现版本和新的实现版本并存,老的HAL实现是通过JNI层直接加载动态链接库的方式,如下图所示: 图引用自: http://www.jollen.org/blog/新的HAL实现仍然是以加载动态链接库的方式来与linux device driver通信,不过libhardware屏蔽了具体的加载细节,每种应用以HAL stub的概念呈现给JNI层,一个HAL stub编译成一个动态链接库文件,结构如下图所示: 图引用自: http://www.jollen.org/blog/ 移植一个新的HAL实现需要开发人员编写HAL module(stub),上层应用通过libhardware获取HAL module的一系列回调函数(callback ops),这一系列回调函数直接与底层的linux device driver通信(一般是通过读写设备文件来实现的)。 目前,HAL对上层还不能完全做到与硬件无关,往往厂家添加自己的硬件设备需要改动相应的runtime(service)实现,android手机一般情况下需要的实现的HAL功能有:- Camera
- GPS
- RIL
- WLAN
- BlueTooth
- Sensor
- vibrator等
GPS的HAL 实现:
在这里探讨的GPS HAL实例采用的是老的HAL实现方式,主要的实现代码位置:
- frameworks/base/location/* (client)
- frameworks/base/core/jni/android_location_GpsLocationProvider.cpp (JNI )
- frameworks/base/services/java/com/android/serverLocationManagerService.java(service)
- hardware/libhardware_legacy/gps/* (HAL)
首先介绍几个重要的数据结构:
/** 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);
/** GPS callback structure. */
typedef struct {
gps_location_callback location_cb;
gps_status_callback status_cb;
gps_sv_status_callback sv_status_cb;
} GpsCallbacks;
/*GPS 接口*/
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,
int uncertainty);
/** 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;
GPS 的HAL 实现主要工作就是填充一个GpsInterface结构,android应用启动LOCATION_SERVICE时,将检测系统是否支持GPS应用,若支持GPS,则在JNI层初始化GPS设备时将返回一个GpsInterface结构,然后通过JNI层的回调函数将GPS信息发送给framework层,调用过程如下所示:
在GpsInterface->Start后,启用读线程通过epoll检测GPS设备文件的状态,若受到数据则读取NEMA数据,将数据解析后,根据解析后的数据填充GpsStatus,GpsLocation和GpsSvInfo结构,然后调用相应的JNI层在Init时传进来的回调函数,service部分的代码将自动更新GPS信息。