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_BASE_H
30#define LOC_API_BASE_H
31
32#include <stddef.h>
33#include <ctype.h>
34#include <gps_extended.h>
35#include <MsgTask.h>
36#include <log_util.h>
37
38namespace loc_core {
39class ContextBase;
40
41int hexcode(char *hexstring, int string_size,
42            const char *data, int data_size);
43int decodeAddress(char *addr_string, int string_size,
44                  const char *data, int data_size);
45
46#define MAX_ADAPTERS          10
47
48#define TO_ALL_ADAPTERS(adapters, call)                                \
49    for (int i = 0; i < MAX_ADAPTERS && NULL != (adapters)[i]; i++) {  \
50        call;                                                          \
51    }
52
53#define TO_1ST_HANDLING_ADAPTER(adapters, call)                              \
54    for (int i = 0; i <MAX_ADAPTERS && NULL != (adapters)[i] && !(call); i++);
55
56class LocAdapterBase;
57struct LocSsrMsg;
58struct LocOpenMsg;
59
60class LocApiProxyBase {
61public:
62    inline LocApiProxyBase() {}
63    inline virtual ~LocApiProxyBase() {}
64    inline virtual void* getSibling2() { return NULL; }
65};
66
67class LocApiBase {
68    friend struct LocSsrMsg;
69    //LocOpenMsg calls open() which makes it necessary to declare
70    //it as a friend
71    friend struct LocOpenMsg;
72    friend class ContextBase;
73    const MsgTask* mMsgTask;
74    ContextBase *mContext;
75    LocAdapterBase* mLocAdapters[MAX_ADAPTERS];
76
77protected:
78    virtual enum loc_api_adapter_err
79        open(LOC_API_ADAPTER_EVENT_MASK_T mask);
80    virtual enum loc_api_adapter_err
81        close();
82    LOC_API_ADAPTER_EVENT_MASK_T getEvtMask();
83    LOC_API_ADAPTER_EVENT_MASK_T mMask;
84    LocApiBase(const MsgTask* msgTask,
85               LOC_API_ADAPTER_EVENT_MASK_T excludedMask,
86               ContextBase* context = NULL);
87    inline virtual ~LocApiBase() { close(); }
88    bool isInSession();
89    const LOC_API_ADAPTER_EVENT_MASK_T mExcludedMask;
90
91public:
92    inline void sendMsg(const LocMsg* msg) const {
93        mMsgTask->sendMsg(msg);
94    }
95
96    void addAdapter(LocAdapterBase* adapter);
97    void removeAdapter(LocAdapterBase* adapter);
98
99    // upward calls
100    void handleEngineUpEvent();
101    void handleEngineDownEvent();
102    void reportPosition(UlpLocation &location,
103                        GpsLocationExtended &locationExtended,
104                        void* locationExt,
105                        enum loc_sess_status status,
106                        LocPosTechMask loc_technology_mask =
107                                  LOC_POS_TECH_MASK_DEFAULT);
108    void reportSv(GpsSvStatus &svStatus,
109                  GpsLocationExtended &locationExtended,
110                  void* svExt);
111    void reportStatus(GpsStatusValue status);
112    void reportNmea(const char* nmea, int length);
113    void reportXtraServer(const char* url1, const char* url2,
114                          const char* url3, const int maxlength);
115    void requestXtraData();
116    void requestTime();
117    void requestLocation();
118    void requestATL(int connHandle, AGpsType agps_type);
119    void releaseATL(int connHandle);
120    void requestSuplES(int connHandle);
121    void reportDataCallOpened();
122    void reportDataCallClosed();
123    void requestNiNotify(GpsNiNotification &notify, const void* data);
124    void reportGpsMeasurementData(GpsData &gpsMeasurementData);
125
126    // downward calls
127    // All below functions are to be defined by adapter specific modules:
128    // RPC, QMI, etc.  The default implementation is empty.
129
130    virtual void* getSibling();
131    virtual LocApiProxyBase* getLocApiProxy();
132    virtual enum loc_api_adapter_err
133        startFix(const LocPosMode& posMode);
134    virtual enum loc_api_adapter_err
135        stopFix();
136    virtual enum loc_api_adapter_err
137        deleteAidingData(GpsAidingData f);
138    virtual enum loc_api_adapter_err
139        enableData(int enable);
140    virtual enum loc_api_adapter_err
141        setAPN(char* apn, int len);
142    virtual enum loc_api_adapter_err
143        injectPosition(double latitude, double longitude, float accuracy);
144    virtual enum loc_api_adapter_err
145        setTime(GpsUtcTime time, int64_t timeReference, int uncertainty);
146    virtual enum loc_api_adapter_err
147        setXtraData(char* data, int length);
148    virtual enum loc_api_adapter_err
149        requestXtraServer();
150    virtual enum loc_api_adapter_err
151        atlOpenStatus(int handle, int is_succ, char* apn, AGpsBearerType bear, AGpsType agpsType);
152    virtual enum loc_api_adapter_err
153        atlCloseStatus(int handle, int is_succ);
154    virtual enum loc_api_adapter_err
155        setPositionMode(const LocPosMode& posMode);
156    virtual enum loc_api_adapter_err
157        setServer(const char* url, int len);
158    virtual enum loc_api_adapter_err
159        setServer(unsigned int ip, int port,
160                  LocServerType type);
161    virtual enum loc_api_adapter_err
162        informNiResponse(GpsUserResponseType userResponse, const void* passThroughData);
163    virtual enum loc_api_adapter_err
164        setSUPLVersion(uint32_t version);
165    virtual enum loc_api_adapter_err
166        setLPPConfig(uint32_t profile);
167    virtual enum loc_api_adapter_err
168        setSensorControlConfig(int sensorUsage, int sensorProvider);
169    virtual enum loc_api_adapter_err
170        setSensorProperties(bool gyroBiasVarianceRandomWalk_valid,
171                            float gyroBiasVarianceRandomWalk,
172                            bool accelBiasVarianceRandomWalk_valid,
173                            float accelBiasVarianceRandomWalk,
174                            bool angleBiasVarianceRandomWalk_valid,
175                            float angleBiasVarianceRandomWalk,
176                            bool rateBiasVarianceRandomWalk_valid,
177                            float rateBiasVarianceRandomWalk,
178                            bool velocityBiasVarianceRandomWalk_valid,
179                            float velocityBiasVarianceRandomWalk);
180    virtual enum loc_api_adapter_err
181        setSensorPerfControlConfig(int controlMode,
182                               int accelSamplesPerBatch,
183                               int accelBatchesPerSec,
184                               int gyroSamplesPerBatch,
185                               int gyroBatchesPerSec,
186                               int accelSamplesPerBatchHigh,
187                               int accelBatchesPerSecHigh,
188                               int gyroSamplesPerBatchHigh,
189                               int gyroBatchesPerSecHigh,
190                               int algorithmConfig);
191    virtual enum loc_api_adapter_err
192        setExtPowerConfig(int isBatteryCharging);
193    virtual enum loc_api_adapter_err
194        setAGLONASSProtocol(unsigned long aGlonassProtocol);
195    virtual enum loc_api_adapter_err
196        getWwanZppFix(GpsLocation & zppLoc);
197    virtual enum loc_api_adapter_err
198        getBestAvailableZppFix(GpsLocation & zppLoc);
199    virtual enum loc_api_adapter_err
200        getBestAvailableZppFix(GpsLocation & zppLoc, LocPosTechMask & tech_mask);
201    virtual int initDataServiceClient();
202    virtual int openAndStartDataCall();
203    virtual void stopDataCall();
204    virtual void closeDataCall();
205    virtual void installAGpsCert(const DerEncodedCertificate* pData,
206                                 size_t length,
207                                 uint32_t slotBitMask);
208
209    inline virtual void setInSession(bool inSession) {}
210
211    /*Values for lock
212      1 = Do not lock any position sessions
213      2 = Lock MI position sessions
214      3 = Lock MT position sessions
215      4 = Lock all position sessions
216     */
217    virtual int setGpsLock(LOC_GPS_LOCK_MASK lock);
218    /*
219      Returns
220      Current value of GPS Lock on success
221      -1 on failure
222     */
223    virtual int getGpsLock(void);
224
225    /*
226      Update gps reporting events
227     */
228    virtual int updateRegistrationMask(LOC_API_ADAPTER_EVENT_MASK_T event,
229                                       loc_registration_mask_status isEnabled);
230    /*
231      Check if the modem support the service
232     */
233    virtual bool gnssConstellationConfig();
234};
235
236typedef LocApiBase* (getLocApi_t)(const MsgTask* msgTask,
237                                  LOC_API_ADAPTER_EVENT_MASK_T exMask,
238                                  ContextBase *context);
239
240} // namespace loc_core
241
242#endif //LOC_API_BASE_H
243