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