1/* Copyright (c) 2011-2014, 2016-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 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 <LocationAPI.h>
36#include <MsgTask.h>
37#include <platform_lib_log_util.h>
38
39namespace loc_core {
40class ContextBase;
41
42int hexcode(char *hexstring, int string_size,
43            const char *data, int data_size);
44int decodeAddress(char *addr_string, int string_size,
45                  const char *data, int data_size);
46
47#define MAX_ADAPTERS          10
48#define MAX_FEATURE_LENGTH    100
49
50#define TO_ALL_ADAPTERS(adapters, call)                                \
51    for (int i = 0; i < MAX_ADAPTERS && NULL != (adapters)[i]; i++) {  \
52        call;                                                          \
53    }
54
55#define TO_1ST_HANDLING_ADAPTER(adapters, call)                              \
56    for (int i = 0; i <MAX_ADAPTERS && NULL != (adapters)[i] && !(call); i++);
57
58enum xtra_version_check {
59    DISABLED,
60    AUTO,
61    XTRA2,
62    XTRA3
63};
64
65class LocAdapterBase;
66struct LocSsrMsg;
67struct LocOpenMsg;
68
69class LocApiProxyBase {
70public:
71    inline LocApiProxyBase() {}
72    inline virtual ~LocApiProxyBase() {}
73    inline virtual void* getSibling2() { return NULL; }
74};
75
76class LocApiBase {
77    friend struct LocSsrMsg;
78    //LocOpenMsg calls open() which makes it necessary to declare
79    //it as a friend
80    friend struct LocOpenMsg;
81    friend class ContextBase;
82    const MsgTask* mMsgTask;
83    ContextBase *mContext;
84    LocAdapterBase* mLocAdapters[MAX_ADAPTERS];
85    uint64_t mSupportedMsg;
86    uint8_t mFeaturesSupported[MAX_FEATURE_LENGTH];
87
88protected:
89    virtual enum loc_api_adapter_err
90        open(LOC_API_ADAPTER_EVENT_MASK_T mask);
91    virtual enum loc_api_adapter_err
92        close();
93    LOC_API_ADAPTER_EVENT_MASK_T getEvtMask();
94    LOC_API_ADAPTER_EVENT_MASK_T mMask;
95    LocApiBase(const MsgTask* msgTask,
96               LOC_API_ADAPTER_EVENT_MASK_T excludedMask,
97               ContextBase* context = NULL);
98    inline virtual ~LocApiBase() { close(); }
99    bool isInSession();
100    const LOC_API_ADAPTER_EVENT_MASK_T mExcludedMask;
101
102public:
103    inline void sendMsg(const LocMsg* msg) const {
104        mMsgTask->sendMsg(msg);
105    }
106    void addAdapter(LocAdapterBase* adapter);
107    void removeAdapter(LocAdapterBase* adapter);
108
109    // upward calls
110    void handleEngineUpEvent();
111    void handleEngineDownEvent();
112    void reportPosition(UlpLocation& location,
113                        GpsLocationExtended& locationExtended,
114                        enum loc_sess_status status,
115                        LocPosTechMask loc_technology_mask =
116                                  LOC_POS_TECH_MASK_DEFAULT);
117    void reportSv(GnssSvNotification& svNotify);
118    void reportSvMeasurement(GnssSvMeasurementSet &svMeasurementSet);
119    void reportSvPolynomial(GnssSvPolynomial &svPolynomial);
120    void reportStatus(LocGpsStatusValue status);
121    void reportNmea(const char* nmea, int length);
122    void reportXtraServer(const char* url1, const char* url2,
123                          const char* url3, const int maxlength);
124    void requestXtraData();
125    void requestTime();
126    void requestLocation();
127    void requestATL(int connHandle, LocAGpsType agps_type);
128    void releaseATL(int connHandle);
129    void requestSuplES(int connHandle);
130    void reportDataCallOpened();
131    void reportDataCallClosed();
132    void requestNiNotify(GnssNiNotification &notify, const void* data);
133    void saveSupportedMsgList(uint64_t supportedMsgList);
134    void reportGnssMeasurementData(GnssMeasurementsNotification& measurementsNotify);
135    void saveSupportedFeatureList(uint8_t *featureList);
136    void reportWwanZppFix(LocGpsLocation &zppLoc);
137
138    // downward calls
139    // All below functions are to be defined by adapter specific modules:
140    // RPC, QMI, etc.  The default implementation is empty.
141
142    virtual void* getSibling();
143    virtual LocApiProxyBase* getLocApiProxy();
144    virtual enum loc_api_adapter_err
145        startFix(const LocPosMode& posMode);
146    virtual enum loc_api_adapter_err
147        stopFix();
148    virtual LocationError
149        deleteAidingData(const GnssAidingData& data);
150    virtual enum loc_api_adapter_err
151        enableData(int enable);
152    virtual enum loc_api_adapter_err
153        setAPN(char* apn, int len);
154    virtual enum loc_api_adapter_err
155        injectPosition(double latitude, double longitude, float accuracy);
156    virtual enum loc_api_adapter_err
157        setTime(LocGpsUtcTime time, int64_t timeReference, int uncertainty);
158    virtual enum loc_api_adapter_err
159        setXtraData(char* data, int length);
160    virtual enum loc_api_adapter_err
161        requestXtraServer();
162    virtual enum loc_api_adapter_err
163        atlOpenStatus(int handle, int is_succ, char* apn, AGpsBearerType bear, LocAGpsType agpsType);
164    virtual enum loc_api_adapter_err
165        atlCloseStatus(int handle, int is_succ);
166    virtual enum loc_api_adapter_err
167        setPositionMode(const LocPosMode& posMode);
168    virtual LocationError
169        setServer(const char* url, int len);
170    virtual LocationError
171        setServer(unsigned int ip, int port,
172                  LocServerType type);
173    virtual LocationError
174        informNiResponse(GnssNiResponse userResponse, const void* passThroughData);
175    virtual LocationError setSUPLVersion(GnssConfigSuplVersion version);
176    virtual enum loc_api_adapter_err
177        setNMEATypes (uint32_t typesMask);
178    virtual LocationError setLPPConfig(GnssConfigLppProfile profile);
179    virtual enum loc_api_adapter_err
180        setSensorControlConfig(int sensorUsage, int sensorProvider);
181    virtual enum loc_api_adapter_err
182        setSensorProperties(bool gyroBiasVarianceRandomWalk_valid,
183                            float gyroBiasVarianceRandomWalk,
184                            bool accelBiasVarianceRandomWalk_valid,
185                            float accelBiasVarianceRandomWalk,
186                            bool angleBiasVarianceRandomWalk_valid,
187                            float angleBiasVarianceRandomWalk,
188                            bool rateBiasVarianceRandomWalk_valid,
189                            float rateBiasVarianceRandomWalk,
190                            bool velocityBiasVarianceRandomWalk_valid,
191                            float velocityBiasVarianceRandomWalk);
192    virtual enum loc_api_adapter_err
193        setSensorPerfControlConfig(int controlMode,
194                               int accelSamplesPerBatch,
195                               int accelBatchesPerSec,
196                               int gyroSamplesPerBatch,
197                               int gyroBatchesPerSec,
198                               int accelSamplesPerBatchHigh,
199                               int accelBatchesPerSecHigh,
200                               int gyroSamplesPerBatchHigh,
201                               int gyroBatchesPerSecHigh,
202                               int algorithmConfig);
203    virtual LocationError
204        setAGLONASSProtocol(GnssConfigAGlonassPositionProtocolMask aGlonassProtocol);
205    virtual LocationError setLPPeProtocolCp(GnssConfigLppeControlPlaneMask lppeCP);
206    virtual LocationError setLPPeProtocolUp(GnssConfigLppeUserPlaneMask lppeUP);
207    virtual enum loc_api_adapter_err
208        getWwanZppFix();
209    virtual enum loc_api_adapter_err
210        getBestAvailableZppFix(LocGpsLocation & zppLoc);
211    virtual enum loc_api_adapter_err
212        getBestAvailableZppFix(LocGpsLocation & zppLoc, LocPosTechMask & tech_mask);
213    virtual int initDataServiceClient(bool isDueToSsr);
214    virtual int openAndStartDataCall();
215    virtual void stopDataCall();
216    virtual void closeDataCall();
217    virtual void releaseDataServiceClient();
218    virtual void installAGpsCert(const LocDerEncodedCertificate* pData,
219                                 size_t length,
220                                 uint32_t slotBitMask);
221    inline virtual void setInSession(bool inSession) {
222
223        (void)inSession;
224    }
225    inline bool isMessageSupported (LocCheckingMessagesID msgID) const {
226
227        // confirm if msgID is not larger than the number of bits in
228        // mSupportedMsg
229        if ((uint64_t)msgID > (sizeof(mSupportedMsg) << 3)) {
230            return false;
231        } else {
232            uint32_t messageChecker = 1 << msgID;
233            return (messageChecker & mSupportedMsg) == messageChecker;
234        }
235    }
236
237    void updateEvtMask();
238
239    virtual LocationError setGpsLock(GnssConfigGpsLock lock);
240    /*
241      Returns
242      Current value of GPS Lock on success
243      -1 on failure
244     */
245    virtual int getGpsLock(void);
246
247    virtual LocationError setXtraVersionCheck(uint32_t check);
248    /*
249      Check if the modem support the service
250     */
251    virtual bool gnssConstellationConfig();
252
253    /*
254       Check if a feature is supported
255      */
256    bool isFeatureSupported(uint8_t featureVal);
257};
258
259typedef LocApiBase* (getLocApi_t)(const MsgTask* msgTask,
260                                  LOC_API_ADAPTER_EVENT_MASK_T exMask,
261                                  ContextBase *context);
262
263} // namespace loc_core
264
265#endif //LOC_API_BASE_H
266