1 /* Copyright (c) 2011-2014, 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 LOC_API_ENG_ADAPTER_H
30 #define LOC_API_ENG_ADAPTER_H
31 
32 #include <ctype.h>
33 #include <hardware/gps.h>
34 #include <loc.h>
35 #include <loc_eng_log.h>
36 #include <log_util.h>
37 #include <LocAdapterBase.h>
38 #include <LocDualContext.h>
39 #include <UlpProxyBase.h>
40 #include <platform_lib_includes.h>
41 
42 #define MAX_URL_LEN 256
43 
44 using namespace loc_core;
45 
46 class LocEngAdapter;
47 
48 class LocInternalAdapter : public LocAdapterBase {
49     LocEngAdapter* mLocEngAdapter;
50 public:
51     LocInternalAdapter(LocEngAdapter* adapter);
52 
53     virtual void reportPosition(UlpLocation &location,
54                                 GpsLocationExtended &locationExtended,
55                                 void* locationExt,
56                                 enum loc_sess_status status,
57                                 LocPosTechMask loc_technology_mask);
58     virtual void reportSv(GpsSvStatus &svStatus,
59                           GpsLocationExtended &locationExtended,
60                           void* svExt);
61     virtual void reportStatus(GpsStatusValue status);
62     virtual void setPositionModeInt(LocPosMode& posMode);
63     virtual void startFixInt();
64     virtual void stopFixInt();
65     virtual void getZppInt();
66     virtual void setUlpProxy(UlpProxyBase* ulp);
67 };
68 
69 typedef void (*loc_msg_sender)(void* loc_eng_data_p, void* msgp);
70 
71 class LocEngAdapter : public LocAdapterBase {
72     void* mOwner;
73     LocInternalAdapter* mInternalAdapter;
74     UlpProxyBase* mUlp;
75     LocPosMode mFixCriteria;
76     bool mNavigating;
77     // mPowerVote is encoded as
78     // mPowerVote & 0x20 -- powerVoteRight
79     // mPowerVote & 0x10 -- power On / Off
80     unsigned int mPowerVote;
81     static const unsigned int POWER_VOTE_RIGHT = 0x20;
82     static const unsigned int POWER_VOTE_VALUE = 0x10;
83 
84 public:
85     bool mSupportsAgpsRequests;
86     bool mSupportsPositionInjection;
87     bool mSupportsTimeInjection;
88 
89     LocEngAdapter(LOC_API_ADAPTER_EVENT_MASK_T mask,
90                   void* owner, ContextBase* context,
91                   MsgTask::tCreate tCreator);
92     virtual ~LocEngAdapter();
93 
94     virtual void setUlpProxy(UlpProxyBase* ulp);
requestUlp(unsigned long capabilities)95     inline void requestUlp(unsigned long capabilities) {
96         mContext->requestUlp(mInternalAdapter, capabilities);
97     }
getInternalAdapter()98     inline LocInternalAdapter* getInternalAdapter() { return mInternalAdapter; }
getUlpProxy()99     inline UlpProxyBase* getUlpProxy() { return mUlp; }
getOwner()100     inline void* getOwner() { return mOwner; }
hasAgpsExtendedCapabilities()101     inline bool hasAgpsExtendedCapabilities() {
102         return mContext->hasAgpsExtendedCapabilities();
103     }
hasCPIExtendedCapabilities()104     inline bool hasCPIExtendedCapabilities() {
105         return mContext->hasCPIExtendedCapabilities();
106     }
getMsgTask()107     inline const MsgTask* getMsgTask() { return mMsgTask; }
108 
109     inline enum loc_api_adapter_err
startFix()110         startFix()
111     {
112         return mLocApi->startFix(mFixCriteria);
113     }
114     inline enum loc_api_adapter_err
stopFix()115         stopFix()
116     {
117         return mLocApi->stopFix();
118     }
119     inline enum loc_api_adapter_err
deleteAidingData(GpsAidingData f)120         deleteAidingData(GpsAidingData f)
121     {
122         return mLocApi->deleteAidingData(f);
123     }
124     inline enum loc_api_adapter_err
enableData(int enable)125         enableData(int enable)
126     {
127         return mLocApi->enableData(enable);
128     }
129     inline enum loc_api_adapter_err
setAPN(char * apn,int len)130         setAPN(char* apn, int len)
131     {
132         return mLocApi->setAPN(apn, len);
133     }
134     inline enum loc_api_adapter_err
injectPosition(double latitude,double longitude,float accuracy)135         injectPosition(double latitude, double longitude, float accuracy)
136     {
137         return mLocApi->injectPosition(latitude, longitude, accuracy);
138     }
139     inline enum loc_api_adapter_err
setXtraData(char * data,int length)140         setXtraData(char* data, int length)
141     {
142         return mLocApi->setXtraData(data, length);
143     }
144     inline enum loc_api_adapter_err
requestXtraServer()145         requestXtraServer()
146     {
147         return mLocApi->requestXtraServer();
148     }
149     inline enum loc_api_adapter_err
atlOpenStatus(int handle,int is_succ,char * apn,AGpsBearerType bearer,AGpsType agpsType)150         atlOpenStatus(int handle, int is_succ, char* apn, AGpsBearerType bearer, AGpsType agpsType)
151     {
152         return mLocApi->atlOpenStatus(handle, is_succ, apn, bearer, agpsType);
153     }
154     inline enum loc_api_adapter_err
atlCloseStatus(int handle,int is_succ)155         atlCloseStatus(int handle, int is_succ)
156     {
157         return mLocApi->atlCloseStatus(handle, is_succ);
158     }
159     inline enum loc_api_adapter_err
setPositionMode(const LocPosMode * posMode)160         setPositionMode(const LocPosMode *posMode)
161     {
162         if (NULL != posMode) {
163             mFixCriteria = *posMode;
164         }
165         return mLocApi->setPositionMode(mFixCriteria);
166     }
167     inline enum loc_api_adapter_err
setServer(const char * url,int len)168         setServer(const char* url, int len)
169     {
170         return mLocApi->setServer(url, len);
171     }
172     inline enum loc_api_adapter_err
setServer(unsigned int ip,int port,LocServerType type)173         setServer(unsigned int ip, int port,
174                   LocServerType type)
175     {
176         return mLocApi->setServer(ip, port, type);
177     }
178     inline enum loc_api_adapter_err
informNiResponse(GpsUserResponseType userResponse,const void * passThroughData)179         informNiResponse(GpsUserResponseType userResponse, const void* passThroughData)
180     {
181         return mLocApi->informNiResponse(userResponse, passThroughData);
182     }
183     inline enum loc_api_adapter_err
setSUPLVersion(uint32_t version)184         setSUPLVersion(uint32_t version)
185     {
186         return mLocApi->setSUPLVersion(version);
187     }
188     inline enum loc_api_adapter_err
setLPPConfig(uint32_t profile)189         setLPPConfig(uint32_t profile)
190     {
191         return mLocApi->setLPPConfig(profile);
192     }
193     inline enum loc_api_adapter_err
setSensorControlConfig(int sensorUsage,int sensorProvider)194         setSensorControlConfig(int sensorUsage, int sensorProvider)
195     {
196         return mLocApi->setSensorControlConfig(sensorUsage, sensorProvider);
197     }
198     inline enum loc_api_adapter_err
setSensorProperties(bool gyroBiasVarianceRandomWalk_valid,float gyroBiasVarianceRandomWalk,bool accelBiasVarianceRandomWalk_valid,float accelBiasVarianceRandomWalk,bool angleBiasVarianceRandomWalk_valid,float angleBiasVarianceRandomWalk,bool rateBiasVarianceRandomWalk_valid,float rateBiasVarianceRandomWalk,bool velocityBiasVarianceRandomWalk_valid,float velocityBiasVarianceRandomWalk)199         setSensorProperties(bool gyroBiasVarianceRandomWalk_valid, float gyroBiasVarianceRandomWalk,
200                             bool accelBiasVarianceRandomWalk_valid, float accelBiasVarianceRandomWalk,
201                             bool angleBiasVarianceRandomWalk_valid, float angleBiasVarianceRandomWalk,
202                             bool rateBiasVarianceRandomWalk_valid, float rateBiasVarianceRandomWalk,
203                             bool velocityBiasVarianceRandomWalk_valid, float velocityBiasVarianceRandomWalk)
204     {
205         return mLocApi->setSensorProperties(gyroBiasVarianceRandomWalk_valid, gyroBiasVarianceRandomWalk,
206                                            accelBiasVarianceRandomWalk_valid, accelBiasVarianceRandomWalk,
207                                            angleBiasVarianceRandomWalk_valid, angleBiasVarianceRandomWalk,
208                                            rateBiasVarianceRandomWalk_valid, rateBiasVarianceRandomWalk,
209                                            velocityBiasVarianceRandomWalk_valid, velocityBiasVarianceRandomWalk);
210     }
211     inline virtual enum loc_api_adapter_err
setSensorPerfControlConfig(int controlMode,int accelSamplesPerBatch,int accelBatchesPerSec,int gyroSamplesPerBatch,int gyroBatchesPerSec,int accelSamplesPerBatchHigh,int accelBatchesPerSecHigh,int gyroSamplesPerBatchHigh,int gyroBatchesPerSecHigh,int algorithmConfig)212         setSensorPerfControlConfig(int controlMode, int accelSamplesPerBatch, int accelBatchesPerSec,
213                             int gyroSamplesPerBatch, int gyroBatchesPerSec,
214                             int accelSamplesPerBatchHigh, int accelBatchesPerSecHigh,
215                             int gyroSamplesPerBatchHigh, int gyroBatchesPerSecHigh, int algorithmConfig)
216     {
217         return mLocApi->setSensorPerfControlConfig(controlMode, accelSamplesPerBatch, accelBatchesPerSec,
218                                                   gyroSamplesPerBatch, gyroBatchesPerSec,
219                                                   accelSamplesPerBatchHigh, accelBatchesPerSecHigh,
220                                                   gyroSamplesPerBatchHigh, gyroBatchesPerSecHigh,
221                                                   algorithmConfig);
222     }
223     inline virtual enum loc_api_adapter_err
setExtPowerConfig(int isBatteryCharging)224         setExtPowerConfig(int isBatteryCharging)
225     {
226         return mLocApi->setExtPowerConfig(isBatteryCharging);
227     }
228     inline virtual enum loc_api_adapter_err
setAGLONASSProtocol(unsigned long aGlonassProtocol)229         setAGLONASSProtocol(unsigned long aGlonassProtocol)
230     {
231         return mLocApi->setAGLONASSProtocol(aGlonassProtocol);
232     }
initDataServiceClient()233     inline virtual int initDataServiceClient()
234     {
235         return mLocApi->initDataServiceClient();
236     }
openAndStartDataCall()237     inline virtual int openAndStartDataCall()
238     {
239         return mLocApi->openAndStartDataCall();
240     }
stopDataCall()241     inline virtual void stopDataCall()
242     {
243         mLocApi->stopDataCall();
244     }
closeDataCall()245     inline virtual void closeDataCall()
246     {
247         mLocApi->closeDataCall();
248     }
249     inline enum loc_api_adapter_err
getZpp(GpsLocation & zppLoc,LocPosTechMask & tech_mask)250         getZpp(GpsLocation &zppLoc, LocPosTechMask &tech_mask)
251     {
252         return mLocApi->getBestAvailableZppFix(zppLoc, tech_mask);
253     }
254     enum loc_api_adapter_err setTime(GpsUtcTime time,
255                                      int64_t timeReference,
256                                      int uncertainty);
257     enum loc_api_adapter_err setXtraVersionCheck(int check);
installAGpsCert(const DerEncodedCertificate * pData,size_t length,uint32_t slotBitMask)258     inline virtual void installAGpsCert(const DerEncodedCertificate* pData,
259                                         size_t length,
260                                         uint32_t slotBitMask)
261     {
262         mLocApi->installAGpsCert(pData, length, slotBitMask);
263     }
264     virtual void handleEngineDownEvent();
265     virtual void handleEngineUpEvent();
266     virtual void reportPosition(UlpLocation &location,
267                                 GpsLocationExtended &locationExtended,
268                                 void* locationExt,
269                                 enum loc_sess_status status,
270                                 LocPosTechMask loc_technology_mask);
271     virtual void reportSv(GpsSvStatus &svStatus,
272                           GpsLocationExtended &locationExtended,
273                           void* svExt);
274     virtual void reportStatus(GpsStatusValue status);
275     virtual void reportNmea(const char* nmea, int length);
276     virtual bool reportXtraServer(const char* url1, const char* url2,
277                                   const char* url3, const int maxlength);
278     virtual bool requestXtraData();
279     virtual bool requestTime();
280     virtual bool requestATL(int connHandle, AGpsType agps_type);
281     virtual bool releaseATL(int connHandle);
282     virtual bool requestNiNotify(GpsNiNotification &notify, const void* data);
283     virtual bool requestSuplES(int connHandle);
284     virtual bool reportDataCallOpened();
285     virtual bool reportDataCallClosed();
286     virtual void reportGpsMeasurementData(GpsData &gpsMeasurementData);
287 
getPositionMode()288     inline const LocPosMode& getPositionMode() const
289     {return mFixCriteria;}
isInSession()290     inline virtual bool isInSession()
291     { return mNavigating; }
292     void setInSession(bool inSession);
293 
294     // Permit/prohibit power voting
setPowerVoteRight(bool powerVoteRight)295     inline void setPowerVoteRight(bool powerVoteRight) {
296         mPowerVote = powerVoteRight ? (mPowerVote | POWER_VOTE_RIGHT) :
297                                       (mPowerVote & ~POWER_VOTE_RIGHT);
298     }
getPowerVoteRight()299     inline bool getPowerVoteRight() const {
300         return (mPowerVote & POWER_VOTE_RIGHT) != 0 ;
301     }
302     // Set the power voting up/down and do actual operation if permitted
setPowerVote(bool powerOn)303     inline void setPowerVote(bool powerOn) {
304         mPowerVote = powerOn ? (mPowerVote | POWER_VOTE_VALUE) :
305                                (mPowerVote & ~POWER_VOTE_VALUE);
306         requestPowerVote();
307         mContext->modemPowerVote(powerOn);
308     }
getPowerVote()309     inline bool getPowerVote() const {
310         return (mPowerVote & POWER_VOTE_VALUE) != 0 ;
311     }
312     // Do power voting according to last settings if permitted
313     void requestPowerVote();
314 
315     /*Values for lock
316       1 = Do not lock any position sessions
317       2 = Lock MI position sessions
318       3 = Lock MT position sessions
319       4 = Lock all position sessions
320     */
setGpsLock(LOC_GPS_LOCK_MASK lock)321     inline int setGpsLock(LOC_GPS_LOCK_MASK lock)
322     {
323         return mLocApi->setGpsLock(lock);
324     }
325 
326     int setGpsLockMsg(LOC_GPS_LOCK_MASK lock);
327 
328     /*
329       Returns
330       Current value of GPS lock on success
331       -1 on failure
332      */
getGpsLock()333     inline int getGpsLock()
334     {
335         return mLocApi->getGpsLock();
336     }
337 
338     /*
339       Update Registration Mask
340      */
341     void updateRegistrationMask(LOC_API_ADAPTER_EVENT_MASK_T event,
342                                 loc_registration_mask_status isEnabled);
343 
344     /*
345       Set Gnss Constellation Config
346      */
347     bool gnssConstellationConfig();
348 };
349 
350 #endif //LOC_API_ENG_ADAPTER_H
351