Home | History | Annotate | Download | only in gnss
      1 /* Copyright (c) 2017, The Linux Foundation. All rights reserved.
      2  *
      3  * Redistribution and use in source and binary forms, with or without
      4  * modification, are permitted provided that the following conditions are
      5  * met:
      6  *     * Redistributions of source code must retain the above copyright
      7  *       notice, this list of conditions and the following disclaimer.
      8  *     * Redistributions in binary form must reproduce the above
      9  *       copyright notice, this list of conditions and the following
     10  *       disclaimer in the documentation and/or other materials provided
     11  *       with the distribution.
     12  *     * Neither the name of The Linux Foundation, nor the names of its
     13  *       contributors may be used to endorse or promote products derived
     14  *       from this software without specific prior written permission.
     15  *
     16  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
     17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
     23  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     24  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
     25  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
     26  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  *
     28  */
     29 #ifndef GNSS_ADAPTER_H
     30 #define GNSS_ADAPTER_H
     31 
     32 #include <LocAdapterBase.h>
     33 #include <LocDualContext.h>
     34 #include <UlpProxyBase.h>
     35 #include <LocationAPI.h>
     36 #include <Agps.h>
     37 #include <SystemStatus.h>
     38 
     39 #define MAX_URL_LEN 256
     40 #define NMEA_SENTENCE_MAX_LENGTH 200
     41 #define GLONASS_SV_ID_OFFSET 64
     42 #define MAX_SATELLITES_IN_USE 12
     43 #define LOC_NI_NO_RESPONSE_TIME 20
     44 #define LOC_GPS_NI_RESPONSE_IGNORE 4
     45 
     46 class GnssAdapter;
     47 
     48 typedef struct {
     49     pthread_t               thread;        /* NI thread */
     50     uint32_t                respTimeLeft;  /* examine time for NI response */
     51     bool                    respRecvd;     /* NI User reponse received or not from Java layer*/
     52     void*                   rawRequest;
     53     uint32_t                reqID;         /* ID to check against response */
     54     GnssNiResponse          resp;
     55     pthread_cond_t          tCond;
     56     pthread_mutex_t         tLock;
     57     GnssAdapter*            adapter;
     58 } NiSession;
     59 typedef struct {
     60     NiSession session;    /* SUPL NI Session */
     61     NiSession sessionEs;  /* Emergency SUPL NI Session */
     62     uint32_t reqIDCounter;
     63 } NiData;
     64 
     65 typedef enum {
     66     NMEA_PROVIDER_AP = 0, // Application Processor Provider of NMEA
     67     NMEA_PROVIDER_MP      // Modem Processor Provider of NMEA
     68 } NmeaProviderType;
     69 typedef struct {
     70     GnssSvType svType;
     71     const char* talker;
     72     uint64_t mask;
     73     uint32_t svIdOffset;
     74 } NmeaSvMeta;
     75 
     76 using namespace loc_core;
     77 
     78 class GnssAdapter : public LocAdapterBase {
     79     /* ==== ULP ============================================================================ */
     80     UlpProxyBase* mUlpProxy;
     81 
     82     /* ==== CLIENT ========================================================================= */
     83     typedef std::map<LocationAPI*, LocationCallbacks> ClientDataMap;
     84     ClientDataMap mClientData;
     85 
     86     /* ==== TRACKING ======================================================================= */
     87     LocationSessionMap mTrackingSessions;
     88     LocPosMode mUlpPositionMode;
     89     GnssSvUsedInPosition mGnssSvIdUsedInPosition;
     90     bool mGnssSvIdUsedInPosAvail;
     91 
     92     /* ==== CONTROL ======================================================================== */
     93     LocationControlCallbacks mControlCallbacks;
     94     uint32_t mPowerVoteId;
     95 
     96     /* ==== NI ============================================================================= */
     97     NiData mNiData;
     98 
     99     /* ==== AGPS ========================================================*/
    100     // This must be initialized via initAgps()
    101     AgpsManager mAgpsManager;
    102 
    103     /*==== CONVERSION ===================================================================*/
    104     static void convertOptions(LocPosMode& out, const LocationOptions& options);
    105     static void convertLocation(Location& out, const LocGpsLocation& locGpsLocation,
    106                                 const GpsLocationExtended& locationExtended,
    107                                 const LocPosTechMask techMask);
    108     static void convertLocationInfo(GnssLocationInfoNotification& out,
    109                                     const GpsLocationExtended& locationExtended);
    110 
    111 public:
    112 
    113     GnssAdapter();
    114     virtual ~GnssAdapter();
    115 
    116     /* ==== SSR ============================================================================ */
    117     /* ======== EVENTS ====(Called from QMI Thread)========================================= */
    118     virtual void handleEngineUpEvent();
    119     /* ======== UTILITIES ================================================================== */
    120     void restartSessions();
    121 
    122     /* ==== ULP ============================================================================ */
    123     /* ======== COMMANDS ====(Called from ULP Thread)==================================== */
    124     virtual void setUlpProxyCommand(UlpProxyBase* ulp);
    125     /* ======== UTILITIES ================================================================== */
    126     void setUlpProxy(UlpProxyBase* ulp);
    127     inline UlpProxyBase* getUlpProxy() { return mUlpProxy; }
    128 
    129     /* ==== CLIENT ========================================================================= */
    130     /* ======== COMMANDS ====(Called from Client Thread)==================================== */
    131     void addClientCommand(LocationAPI* client, const LocationCallbacks& callbacks);
    132     void removeClientCommand(LocationAPI* client);
    133     void requestCapabilitiesCommand(LocationAPI* client);
    134     /* ======== UTILITIES ================================================================== */
    135     void saveClient(LocationAPI* client, const LocationCallbacks& callbacks);
    136     void eraseClient(LocationAPI* client);
    137     void updateClientsEventMask();
    138     void stopClientSessions(LocationAPI* client);
    139     LocationCallbacks getClientCallbacks(LocationAPI* client);
    140 
    141     /* ==== TRACKING ======================================================================= */
    142     /* ======== COMMANDS ====(Called from Client Thread)==================================== */
    143     uint32_t startTrackingCommand(LocationAPI* client, LocationOptions& options);
    144     void updateTrackingOptionsCommand(LocationAPI* client, uint32_t id, LocationOptions& options);
    145     void stopTrackingCommand(LocationAPI* client, uint32_t id);
    146     /* ======================(Called from ULP Thread)======================================= */
    147     virtual void setPositionModeCommand(LocPosMode& locPosMode);
    148     virtual void startTrackingCommand();
    149     virtual void stopTrackingCommand();
    150     virtual void getZppCommand();
    151     /* ======== RESPONSES ================================================================== */
    152     void reportResponse(LocationAPI* client, LocationError err, uint32_t sessionId);
    153     /* ======== UTILITIES ================================================================== */
    154     bool hasTrackingCallback(LocationAPI* client);
    155     bool hasMeasurementsCallback(LocationAPI* client);
    156     bool isTrackingSession(LocationAPI* client, uint32_t sessionId);
    157     void saveTrackingSession(LocationAPI* client, uint32_t sessionId,
    158                              const LocationOptions& options);
    159     void eraseTrackingSession(LocationAPI* client, uint32_t sessionId);
    160     void setUlpPositionMode(const LocPosMode& mode) { mUlpPositionMode = mode; }
    161     LocPosMode& getUlpPositionMode() { return mUlpPositionMode; }
    162     LocationError startTrackingMultiplex(const LocationOptions& options);
    163     LocationError startTracking(const LocationOptions& options);
    164     LocationError stopTrackingMultiplex(LocationAPI* client, uint32_t id);
    165     LocationError stopTracking();
    166     LocationError updateTrackingMultiplex(LocationAPI* client, uint32_t id,
    167                                           const LocationOptions& options);
    168 
    169     /* ==== NI ============================================================================= */
    170     /* ======== COMMANDS ====(Called from Client Thread)==================================== */
    171     void gnssNiResponseCommand(LocationAPI* client, uint32_t id, GnssNiResponse response);
    172     /* ======================(Called from NI Thread)======================================== */
    173     void gnssNiResponseCommand(GnssNiResponse response, void* rawRequest);
    174     /* ======== UTILITIES ================================================================== */
    175     bool hasNiNotifyCallback(LocationAPI* client);
    176     NiData& getNiData() { return mNiData; }
    177 
    178     /* ==== CONTROL ======================================================================== */
    179     /* ======== COMMANDS ====(Called from Client Thread)==================================== */
    180     uint32_t enableCommand(LocationTechnologyType techType);
    181     void disableCommand(uint32_t id);
    182     void setControlCallbacksCommand(LocationControlCallbacks& controlCallbacks);
    183     void readConfigCommand();
    184     void setConfigCommand();
    185     uint32_t* gnssUpdateConfigCommand(GnssConfig config);
    186     uint32_t gnssDeleteAidingDataCommand(GnssAidingData& data);
    187 
    188     void initAgpsCommand(void* statusV4Cb);
    189     void dataConnOpenCommand(
    190             AGpsExtType agpsType,
    191             const char* apnName, int apnLen, LocApnIpType ipType);
    192     void dataConnClosedCommand(AGpsExtType agpsType);
    193     void dataConnFailedCommand(AGpsExtType agpsType);
    194 
    195     /* ======== RESPONSES ================================================================== */
    196     void reportResponse(LocationError err, uint32_t sessionId);
    197     void reportResponse(size_t count, LocationError* errs, uint32_t* ids);
    198     /* ======== UTILITIES ================================================================== */
    199     LocationControlCallbacks& getControlCallbacks() { return mControlCallbacks; }
    200     void setControlCallbacks(const LocationControlCallbacks& controlCallbacks)
    201     { mControlCallbacks = controlCallbacks; }
    202     void setPowerVoteId(uint32_t id) { mPowerVoteId = id; }
    203     uint32_t getPowerVoteId() { return mPowerVoteId; }
    204     bool resolveInAddress(const char* hostAddress, struct in_addr* inAddress);
    205 
    206     /* ==== REPORTS ======================================================================== */
    207     /* ======== EVENTS ====(Called from QMI/ULP Thread)===================================== */
    208     virtual void reportPositionEvent(const UlpLocation& ulpLocation,
    209                                      const GpsLocationExtended& locationExtended,
    210                                      enum loc_sess_status status,
    211                                      LocPosTechMask techMask,
    212                                      bool fromUlp=false);
    213     virtual void reportSvEvent(const GnssSvNotification& svNotify, bool fromUlp=false);
    214     virtual void reportNmeaEvent(const char* nmea, size_t length, bool fromUlp=false);
    215     virtual bool requestNiNotifyEvent(const GnssNiNotification& notify, const void* data);
    216     virtual void reportGnssMeasurementDataEvent(const GnssMeasurementsNotification& measurementsNotify);
    217     virtual void reportSvMeasurementEvent(GnssSvMeasurementSet &svMeasurementSet);
    218     virtual void reportSvPolynomialEvent(GnssSvPolynomial &svPolynomial);
    219 
    220     virtual bool requestATL(int connHandle, LocAGpsType agps_type);
    221     virtual bool releaseATL(int connHandle);
    222     virtual bool requestSuplES(int connHandle);
    223     virtual bool reportDataCallOpened();
    224     virtual bool reportDataCallClosed();
    225 
    226     /* ======== UTILITIES ================================================================= */
    227     void reportPosition(const UlpLocation &ulpLocation,
    228                         const GpsLocationExtended &locationExtended,
    229                         enum loc_sess_status status,
    230                         LocPosTechMask techMask);
    231     void reportSv(GnssSvNotification& svNotify);
    232     void reportNmea(const char* nmea, size_t length);
    233     bool requestNiNotify(const GnssNiNotification& notify, const void* data);
    234     void reportGnssMeasurementData(const GnssMeasurementsNotification& measurementsNotify);
    235 
    236     /*======== GNSSDEBUG ================================================================*/
    237     bool getDebugReport(GnssDebugReport& report);
    238 
    239     /*==== CONVERSION ===================================================================*/
    240     static uint32_t convertGpsLock(const GnssConfigGpsLock gpsLock);
    241     static GnssConfigGpsLock convertGpsLock(const uint32_t gpsLock);
    242     static uint32_t convertSuplVersion(const GnssConfigSuplVersion suplVersion);
    243     static GnssConfigSuplVersion convertSuplVersion(const uint32_t suplVersion);
    244     static uint32_t convertLppProfile(const GnssConfigLppProfile lppProfile);
    245     static GnssConfigLppProfile convertLppProfile(const uint32_t lppProfile);
    246     static uint32_t convertEP4ES(const GnssConfigEmergencyPdnForEmergencySupl);
    247     static uint32_t convertSuplEs(const GnssConfigSuplEmergencyServices suplEmergencyServices);
    248     static uint32_t convertLppeCp(const GnssConfigLppeControlPlaneMask lppeControlPlaneMask);
    249     static GnssConfigLppeControlPlaneMask convertLppeCp(const uint32_t lppeControlPlaneMask);
    250     static uint32_t convertLppeUp(const GnssConfigLppeUserPlaneMask lppeUserPlaneMask);
    251     static GnssConfigLppeUserPlaneMask convertLppeUp(const uint32_t lppeUserPlaneMask);
    252     static uint32_t convertAGloProt(const GnssConfigAGlonassPositionProtocolMask);
    253     static uint32_t convertSuplMode(const GnssConfigSuplModeMask suplModeMask);
    254     static void convertSatelliteInfo(std::vector<GnssDebugSatelliteInfo>& out,
    255                                      const GnssSvType& in_constellation,
    256                                      const SystemStatusReports& in);
    257 
    258     void injectLocationCommand(double latitude, double longitude, float accuracy);
    259     void injectTimeCommand(int64_t time, int64_t timeReference, int32_t uncertainty);
    260 
    261 };
    262 
    263 #endif //GNSS_ADAPTER_H
    264