`
lovecontry
  • 浏览: 1035698 次
文章分类
社区版块
存档分类
最新评论

基于android 的GPS 移植——主要结构体及接口介绍

 
阅读更多

最近负责 Android 驱动的GPS部分,所谓Android驱动 我觉得可以说成是linux应用

之前发了一个图描述了 Android GPS 架构

GPS芯片大多采用串口通信,所以只要CPU能够提供一个UART 驱动, 那么跟GPS芯片通信是没有问题的,

但是现在很多GPS功能不是一个单独的芯片,大部分都是一个芯片上有好几种无线模块 比如 Blletooth WiFi FM等

有时候 有些功能就会去复用一个UART,这样在管理上就相对麻烦, 要将一个UART驱动, 模拟成一个HCI的接口而后才能使GPS功能(TI1281)


比较好的是我们的项目是单一的GPS chip 听说apple也是用的这一款,不过人家拿的是全部的source code,而我们只有部分的code而没有核心算法的code 很郁闷, 但是即便是只有部分code从里边还是可以学到很多的机制


对于android GPS驱动来说主要的工作是实现HAL层 计编译生成一个叫做gps.***.so的动态库的 代码.
由app发出定位申请,此时会去启动一个定位服务线程,此线程会调用到Location provider 服务在App Framwork中,在次会通过被native方法调用到JNI中,JNI获得GpsInterface并传给接口一个回调函数的实现。
GpsInterface就是由HAL层最主要实现

介绍下gps.h下得重要相关结构题定义
在android2.3中gps.h定义在/hardware/inclue/hardware下
主要的结构
1、相关宏定义:

  • 定位模式定义
    /**RequestedoperationalmodeforGPSoperation.*/
    typedefuint32_tGpsPositionMode;
    //IMPORTANT:Notethatthefollowingvaluesmustmatch
    //constantsinGpsLocationProvider.java.
    /**ModeforrunningGPSstandalone(noassistance).*/
    #defineGPS_POSITION_MODE_STANDALONE0 //纯GPS使用卫星定位
    /**AGPSMS-Basedmode.*/
    #defineGPS_POSITION_MODE_MS_BASED1 //基于用户终端的A-GPS
    /**AGPSMS-Assistedmode.*/
    #defineGPS_POSITION_MODE_MS_ASSISTED2 //基于用户终端辅助A-GPS



  • 工作模式
    /**RequestedrecurrencemodeforGPSoperation.*/
    typedefuint32_tGpsPositionRecurrence;
    //IMPORTANT:Notethatthefollowingvaluesmustmatch
    //constantsinGpsLocationProvider.java.
    /**ReceiveGPSfixesonarecurringbasisataspecifiedperiod.*/
    #defineGPS_POSITION_RECURRENCE_PERIODIC0//周期性的
    /**RequestasingleshotGPSfix.*/
    #defineGPS_POSITION_RECURRENCE_SINGLE1//单次工作


  • 辅助数据(在测试GPS性能时候又是需要删除全部或部分的辅助数据)
    /**Flagsusedtospecifywhichaidingdatatodelete
    whencallingdelete_aiding_data().*/

    typedefuint16_tGpsAidingData;
    //IMPORTANT:Notethatthefollowingvaluesmustmatch
    //constantsinGpsLocationProvider.java.
    #defineGPS_DELETE_EPHEMERIS0x0001
    #defineGPS_DELETE_ALMANAC0x0002
    #defineGPS_DELETE_POSITION0x0004
    #defineGPS_DELETE_TIME0x0008
    #defineGPS_DELETE_IONO0x0010
    #defineGPS_DELETE_UTC0x0020
    #defineGPS_DELETE_HEALTH0x0040
    #defineGPS_DELETE_SVDIR0x0080
    #defineGPS_DELETE_SVSTEER0x0100
    #defineGPS_DELETE_SADATA0x0200
    #defineGPS_DELETE_RTI0x0400
    #defineGPS_DELETE_CELLDB_INFO0x8000
    #defineGPS_DELETE_ALL0xFFFF

  • gps芯片可以处理的“能力”
    /**Flagsforthegps_set_capabilitiescallback.*/

    /**GPSHALschedulesfixesforGPS_POSITION_RECURRENCE_PERIODICmode.
    Ifthisisnotset,thentheframeworkwilluse1000msformin_interval
    andwillstartandcallstart()andstop()toscheduletheGPS.
    */

    #defineGPS_CAPABILITY_SCHEDULING0x0000001/**GPSsupportsMS-BasedAGPSmode*/
    #defineGPS_CAPABILITY_MSB0x0000002/**GPSsupportsMS-AssistedAGPSmode*/
    #defineGPS_CAPABILITY_MSA0x0000004/**GPSsupportssingle-shotfixes*/
    #defineGPS_CAPABILITY_SINGLE_SHOT0x0000008

  • 还有很多的定义此处不一一列举 在gps.h中都有很好的解释。

2、数据结构体

  • GpsLocation
    定义了一个表示方位的结构体,成员有经纬度,高度,速度,方位角等。
  • GpsStatus
    表示GPS的当前状态,只有两个成员一个是表示结构大小的成员,与一个表示Gps状态的类型GpsStatusValue
    /**GPSstatuseventvalues.*/
    typedefuint16_tGpsStatusValue;
    //IMPORTANT:Notethatthefollowingvaluesmustmatch
    //constantsinGpsLocationProvider.java.
    /**GPSstatusunknown.*/
    #defineGPS_STATUS_NONE0/**GPShasbegunnavigating.*/
    #defineGPS_STATUS_SESSION_BEGIN1/**GPShasstoppednavigating.*/
    #defineGPS_STATUS_SESSION_END2/**GPShaspoweredonbutisnotnavigating.*/
    #defineGPS_STATUS_ENGINE_ON3/**GPSispoweredoff.*/
    #defineGPS_STATUS_ENGINE_OFF4

  • GpsSvInfo
    表示当前的卫星信息,有卫星编号,信号强度,卫星仰望角方位角等
  • GpsSvStatus
    表示卫星状态,包含了GpsSvInfo结构,可见卫星数,星历时间,年历时间,与用来定位的卫星的卫星构成的一个掩码
  • AGpsRefLocation
    /*2Gand3G*/
    /*In3Glacisdiscarded*/
    typedefstruct{
    uint16_ttype;
    uint16_tmcc;
    uint16_tmnc;
    uint16_tlac;
    uint32_tcid;
    }AGpsRefLocationCellID;

    typedefstruct{
    uint8_tmac[6];
    }AGpsRefLocationMac;

    /**Representsreflocations*/
    typedefstruct{
    uint16_ttype;
    union{
    AGpsRefLocationCellIDcellID;
    AGpsRefLocationMacmac;
    }u;
    }AGpsRefLocation;
    /**RepresentsthestatusofAGPS.*/
    typedefstruct{
    /**settosizeof(AGpsStatus)*/
    size_tsize;


    AGpsTypetype;
    AGpsStatusValuestatus;
    }AGpsStatus

3、回调函数指针定义

/*Callbackwithlocationinformation.告知上层位置信息*/
typedefvoid(*gps_location_callback)(GpsLocation*location);

/**Callbackwithstatusinformation.GPS状态信息回调*/
typedefvoid(*gps_status_callback)(GpsStatus*status);

/**CallbackwithSVstatusinformation.卫星状态信息回调*/
typedefvoid(*gps_sv_status_callback)(GpsSvStatus*sv_info);

/**CallbackforreportingNMEAsentences.向上层传递NEMA数据*/
typedefvoid(*gps_nmea_callback)(GpsUtcTimetimestamp,constchar*nmea,intlength);

/**CallbacktoinformframeworkoftheGPSengine'scapabilities.告知上层次GPS能实现那些功能*/
typedefvoid(*gps_set_capabilities)(uint32_tcapabilities);

/**CallbackutilityforacquiringtheGPSwakelock.gps上锁让gps功能不会被suspend*/
typedefvoid(*gps_acquire_wakelock)();

/**CallbackutilityforreleasingtheGPSwakelock.释放锁*/
typedefvoid(*gps_release_wakelock)();

/**CallbackforcreatingathreadthatcancallintotheJavaframeworkcode.threadcreate
在次线程处理函数中通常会由一个无限的循环,去等待上层的请求,并应答*/

typedefpthread_t(*gps_create_thread)(constchar*name,void(*start)(void*),void*arg);

4、接口结构体

  • GpsCallbacks
    typedefstruct{
    /**settosizeof(GpsCallbacks)*/
    size_tsize;
    gps_location_callbacklocation_cb;
    gps_status_callbackstatus_cb;
    gps_sv_status_callbacksv_status_cb;
    gps_nmea_callbacknmea_cb;
    gps_set_capabilitiesset_capabilities_cb;
    gps_acquire_wakelockacquire_wakelock_cb;
    gps_release_wakelockrelease_wakelock_cb;
    gps_create_threadcreate_thread_cb;
    }GpsCallbacks;

    就是以上函数指针组成的结构体,是一组回调函数,函数的实现在JNI层是Android实现好了得,无需做(太大的)修改

  • GpsInterface
    Android GPS 最主要的数据结构,我们最主要的移植工作就是实现其中的函数
    /**RepresentsthestandardGPSinterface.*/
    typedefstruct{
    /**settosizeof(GpsInterface)*/
    size_tsize;
    /*Openstheinterfaceandprovidesthecallbackroutines初始化callbacks为JNI传下来的
    在此会使用callbacks中的create_thread_cb来创建一个线程处理函数一直循环,等待任务处理*/

    int(*init)(GpsCallbacks*callbacks);

    /**Startsnavigating.*/
    int(*start)(void);

    /**Stopsnavigating.*/
    int(*stop)(void);

    /**Closestheinterface.*/
    void(*cleanup)(void);

    /**Injectsthecurrenttime.填入时间*/
    int(*inject_time)(GpsUtcTimetime,int64_ttimeReference,
    intuncertainty);

    /**Injectscurrentlocationfromanotherlocationprovider填入位置*/
    int(*inject_location)(doublelatitude,doublelongitude,floataccuracy);

    /*删除全部or部分辅助数据在性能测试时候使用*/
    void(*delete_aiding_data)(GpsAidingDataflags);

    /*设置定位模式与GPS工作模式等等*/
    int(*set_position_mode)(GpsPositionModemode,GpsPositionRecurrencerecurrence,
    uint32_tmin_interval,uint32_tpreferred_accuracy,uint32_tpreferred_time);

    /*自行添加的接口AGpsInterface接口通过次函数获得*/
    constvoid*(*get_extension)(constchar*name);
    }GpsInterface;
  • AGpsCallbacks
    /**CallbackwithAGPSstatusinformation.
    *Canonlybecalledfromathreadcreatedbycreate_thread_cb.
    */

    typedefvoid(*agps_status_callback)(AGpsStatus*status);

    /**CallbackstructurefortheAGPSinterface.*/
    typedefstruct{
    agps_status_callbackstatus_cb;
    gps_create_threadcreate_thread_cb;
    }AGpsCallbacks;

  • AGpsInterface
    /**ExtendedinterfaceforAGPSsupport.*/
    typedefstruct{
    /**settosizeof(AGpsInterface)*/
    size_tsize;

    /**
    *OpenstheAGPSinterfaceandprovidesthecallbackroutines
    *totheimplemenationofthisinterface.
    */

    void(*init)(AGpsCallbacks*callbacks);
    /**
    *Notifiesthatadataconnectionisavailableandsets
    *thenameoftheAPNtobeusedforSUPL.
    */

    int(*data_conn_open)(constchar*apn);
    /**
    *NotifiesthattheAGPSdataconnectionhasbeenclosed.
    */

    int(*data_conn_closed)();
    /**
    *NotifiesthatadataconnectionisnotavailableforAGPS.
    */

    int(*data_conn_failed)();
    /**
    *SetsthehostnameandportfortheAGPSserver.
    */

    int(*set_server)(AGpsTypetype,constchar*hostname,intport);
    }AGpsInterface

  • struct gps_device_t
    定义描述gps设备类型
    structgps_device_t{
    structhw_device_tcommon;

    /**
    *Settheprovidedlightstotheprovidedvalues.
    *
    *Returns:0onsucces,errorcodeonfailure.
    */

    constGpsInterface*(*get_gps_interface)(structgps_device_t*dev);
    };
    上层会通过动态库,获得此结构,等到GpsInterface从而打通APP到GPS device的全部通信。

  • 还有 GpsNiInterfaceAGpsRilInterface等接口 在此不赘述

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics