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