1 /* Copyright (c) 2017-2019, 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 
30 #include "GnssAdapter.h"
31 #include "location_interface.h"
32 
33 static GnssAdapter* gGnssAdapter = NULL;
34 
35 static void initialize();
36 static void deinitialize();
37 
38 static void addClient(LocationAPI* client, const LocationCallbacks& callbacks);
39 static void removeClient(LocationAPI* client, removeClientCompleteCallback rmClientCb);
40 static void requestCapabilities(LocationAPI* client);
41 
42 static uint32_t startTracking(LocationAPI* client, TrackingOptions&);
43 static void updateTrackingOptions(LocationAPI* client, uint32_t id, TrackingOptions&);
44 static void stopTracking(LocationAPI* client, uint32_t id);
45 
46 static void gnssNiResponse(LocationAPI* client, uint32_t id, GnssNiResponse response);
47 static uint32_t gnssDeleteAidingData(GnssAidingData& data);
48 static void gnssUpdateXtraThrottle(const bool enabled);
49 
50 static void setControlCallbacks(LocationControlCallbacks& controlCallbacks);
51 static uint32_t enable(LocationTechnologyType techType);
52 static void disable(uint32_t id);
53 static uint32_t* gnssUpdateConfig(GnssConfig config);
54 static uint32_t* gnssGetConfig(GnssConfigFlagsMask mask);
55 
56 static void gnssUpdateSvTypeConfig(GnssSvTypeConfig& config);
57 static void gnssGetSvTypeConfig(GnssSvTypeConfigCallback& callback);
58 static void gnssResetSvTypeConfig();
59 
60 static void injectLocation(double latitude, double longitude, float accuracy);
61 static void injectLocationExt(const GnssLocationInfoNotification &locationInfo);
62 static void injectTime(int64_t time, int64_t timeReference, int32_t uncertainty);
63 
64 static void agpsInit(const AgpsCbInfo& cbInfo);
65 static void agpsDataConnOpen(AGpsExtType agpsType, const char* apnName, int apnLen, int ipType);
66 static void agpsDataConnClosed(AGpsExtType agpsType);
67 static void agpsDataConnFailed(AGpsExtType agpsType);
68 static void getDebugReport(GnssDebugReport& report);
69 static void updateConnectionStatus(bool connected, int8_t type, bool roaming = false,
70                                    NetworkHandle networkHandle = NETWORK_HANDLE_UNKNOWN);
71 static void getGnssEnergyConsumed(GnssEnergyConsumedCallback energyConsumedCb);
72 static void enableNfwLocationAccess(bool enable);
73 static void nfwInit(const NfwCbInfo& cbInfo);
74 static void getPowerStateChanges(void* powerStateCb);
75 
76 static void odcpiInit(const OdcpiRequestCallback& callback);
77 static void odcpiInject(const Location& location);
78 
79 static void blockCPI(double latitude, double longitude, float accuracy,
80                      int blockDurationMsec, double latLonDiffThreshold);
81 
82 static const GnssInterface gGnssInterface = {
83     sizeof(GnssInterface),
84     initialize,
85     deinitialize,
86     addClient,
87     removeClient,
88     requestCapabilities,
89     startTracking,
90     updateTrackingOptions,
91     stopTracking,
92     gnssNiResponse,
93     setControlCallbacks,
94     enable,
95     disable,
96     gnssUpdateConfig,
97     gnssGetConfig,
98     gnssUpdateSvTypeConfig,
99     gnssGetSvTypeConfig,
100     gnssResetSvTypeConfig,
101     gnssDeleteAidingData,
102     gnssUpdateXtraThrottle,
103     injectLocation,
104     injectTime,
105     agpsInit,
106     agpsDataConnOpen,
107     agpsDataConnClosed,
108     agpsDataConnFailed,
109     getDebugReport,
110     updateConnectionStatus,
111     odcpiInit,
112     odcpiInject,
113     blockCPI,
114     getGnssEnergyConsumed,
115     enableNfwLocationAccess,
116     nfwInit,
117     getPowerStateChanges,
118     injectLocationExt
119 };
120 
121 #ifndef DEBUG_X86
getGnssInterface()122 extern "C" const GnssInterface* getGnssInterface()
123 #else
124 const GnssInterface* getGnssInterface()
125 #endif // DEBUG_X86
126 {
127    gGnssInterface.initialize();
128    return &gGnssInterface;
129 }
130 
initialize()131 static void initialize()
132 {
133     if (NULL == gGnssAdapter) {
134         gGnssAdapter = new GnssAdapter();
135     }
136 }
137 
deinitialize()138 static void deinitialize()
139 {
140     if (NULL != gGnssAdapter) {
141         delete gGnssAdapter;
142         gGnssAdapter = NULL;
143     }
144 }
145 
addClient(LocationAPI * client,const LocationCallbacks & callbacks)146 static void addClient(LocationAPI* client, const LocationCallbacks& callbacks)
147 {
148     if (NULL != gGnssAdapter) {
149         gGnssAdapter->addClientCommand(client, callbacks);
150     }
151 }
152 
removeClient(LocationAPI * client,removeClientCompleteCallback rmClientCb)153 static void removeClient(LocationAPI* client, removeClientCompleteCallback rmClientCb)
154 {
155     if (NULL != gGnssAdapter) {
156         gGnssAdapter->removeClientCommand(client, rmClientCb);
157     }
158 }
159 
requestCapabilities(LocationAPI * client)160 static void requestCapabilities(LocationAPI* client)
161 {
162     if (NULL != gGnssAdapter) {
163         gGnssAdapter->requestCapabilitiesCommand(client);
164     }
165 }
166 
startTracking(LocationAPI * client,TrackingOptions & trackingOptions)167 static uint32_t startTracking(
168         LocationAPI* client, TrackingOptions& trackingOptions)
169 {
170     if (NULL != gGnssAdapter) {
171         return gGnssAdapter->startTrackingCommand(client, trackingOptions);
172     } else {
173         return 0;
174     }
175 }
176 
updateTrackingOptions(LocationAPI * client,uint32_t id,TrackingOptions & trackingOptions)177 static void updateTrackingOptions(
178         LocationAPI* client, uint32_t id, TrackingOptions& trackingOptions)
179 {
180     if (NULL != gGnssAdapter) {
181         gGnssAdapter->updateTrackingOptionsCommand(
182                 client, id, trackingOptions);
183     }
184 }
185 
stopTracking(LocationAPI * client,uint32_t id)186 static void stopTracking(LocationAPI* client, uint32_t id)
187 {
188     if (NULL != gGnssAdapter) {
189         gGnssAdapter->stopTrackingCommand(client, id);
190     }
191 }
192 
gnssNiResponse(LocationAPI * client,uint32_t id,GnssNiResponse response)193 static void gnssNiResponse(LocationAPI* client, uint32_t id, GnssNiResponse response)
194 {
195     if (NULL != gGnssAdapter) {
196         gGnssAdapter->gnssNiResponseCommand(client, id, response);
197     }
198 }
199 
setControlCallbacks(LocationControlCallbacks & controlCallbacks)200 static void setControlCallbacks(LocationControlCallbacks& controlCallbacks)
201 {
202     if (NULL != gGnssAdapter) {
203         gGnssAdapter->setControlCallbacksCommand(controlCallbacks);
204     }
205 }
206 
enable(LocationTechnologyType techType)207 static uint32_t enable(LocationTechnologyType techType)
208 {
209     if (NULL != gGnssAdapter) {
210         return gGnssAdapter->enableCommand(techType);
211     } else {
212         return 0;
213     }
214 }
215 
disable(uint32_t id)216 static void disable(uint32_t id)
217 {
218     if (NULL != gGnssAdapter) {
219         gGnssAdapter->disableCommand(id);
220     }
221 }
222 
gnssUpdateConfig(GnssConfig config)223 static uint32_t* gnssUpdateConfig(GnssConfig config)
224 {
225     if (NULL != gGnssAdapter) {
226         return gGnssAdapter->gnssUpdateConfigCommand(config);
227     } else {
228         return NULL;
229     }
230 }
231 
gnssGetConfig(GnssConfigFlagsMask mask)232 static uint32_t* gnssGetConfig(GnssConfigFlagsMask mask)
233 {
234     if (NULL != gGnssAdapter) {
235         return gGnssAdapter->gnssGetConfigCommand(mask);
236     } else {
237         return NULL;
238     }
239 }
240 
gnssUpdateSvTypeConfig(GnssSvTypeConfig & config)241 static void gnssUpdateSvTypeConfig(GnssSvTypeConfig& config)
242 {
243     if (NULL != gGnssAdapter) {
244         gGnssAdapter->gnssUpdateSvTypeConfigCommand(config);
245     }
246 }
247 
gnssGetSvTypeConfig(GnssSvTypeConfigCallback & callback)248 static void gnssGetSvTypeConfig(GnssSvTypeConfigCallback& callback)
249 {
250     if (NULL != gGnssAdapter) {
251         gGnssAdapter->gnssGetSvTypeConfigCommand(callback);
252     }
253 }
254 
gnssResetSvTypeConfig()255 static void gnssResetSvTypeConfig()
256 {
257     if (NULL != gGnssAdapter) {
258         gGnssAdapter->gnssResetSvTypeConfigCommand();
259     }
260 }
261 
gnssDeleteAidingData(GnssAidingData & data)262 static uint32_t gnssDeleteAidingData(GnssAidingData& data)
263 {
264     if (NULL != gGnssAdapter) {
265         return gGnssAdapter->gnssDeleteAidingDataCommand(data);
266     } else {
267         return 0;
268     }
269 }
270 
gnssUpdateXtraThrottle(const bool enabled)271 static void gnssUpdateXtraThrottle(const bool enabled)
272 {
273     if (NULL != gGnssAdapter) {
274         gGnssAdapter->gnssUpdateXtraThrottleCommand(enabled);
275     }
276 }
277 
injectLocation(double latitude,double longitude,float accuracy)278 static void injectLocation(double latitude, double longitude, float accuracy)
279 {
280    if (NULL != gGnssAdapter) {
281        gGnssAdapter->injectLocationCommand(latitude, longitude, accuracy);
282    }
283 }
284 
injectTime(int64_t time,int64_t timeReference,int32_t uncertainty)285 static void injectTime(int64_t time, int64_t timeReference, int32_t uncertainty)
286 {
287    if (NULL != gGnssAdapter) {
288        gGnssAdapter->injectTimeCommand(time, timeReference, uncertainty);
289    }
290 }
291 
agpsInit(const AgpsCbInfo & cbInfo)292 static void agpsInit(const AgpsCbInfo& cbInfo) {
293 
294     if (NULL != gGnssAdapter) {
295         gGnssAdapter->initAgpsCommand(cbInfo);
296     }
297 }
agpsDataConnOpen(AGpsExtType agpsType,const char * apnName,int apnLen,int ipType)298 static void agpsDataConnOpen(
299         AGpsExtType agpsType, const char* apnName, int apnLen, int ipType) {
300 
301     if (NULL != gGnssAdapter) {
302         gGnssAdapter->dataConnOpenCommand(
303                 agpsType, apnName, apnLen, (AGpsBearerType)ipType);
304     }
305 }
agpsDataConnClosed(AGpsExtType agpsType)306 static void agpsDataConnClosed(AGpsExtType agpsType) {
307 
308     if (NULL != gGnssAdapter) {
309         gGnssAdapter->dataConnClosedCommand(agpsType);
310     }
311 }
agpsDataConnFailed(AGpsExtType agpsType)312 static void agpsDataConnFailed(AGpsExtType agpsType) {
313 
314     if (NULL != gGnssAdapter) {
315         gGnssAdapter->dataConnFailedCommand(agpsType);
316     }
317 }
318 
getDebugReport(GnssDebugReport & report)319 static void getDebugReport(GnssDebugReport& report) {
320 
321     if (NULL != gGnssAdapter) {
322         gGnssAdapter->getDebugReport(report);
323     }
324 }
325 
updateConnectionStatus(bool connected,int8_t type,bool roaming,NetworkHandle networkHandle)326 static void updateConnectionStatus(bool connected, int8_t type,
327                                    bool roaming, NetworkHandle networkHandle) {
328     if (NULL != gGnssAdapter) {
329         gGnssAdapter->getSystemStatus()->eventConnectionStatus(
330                 connected, type, roaming, networkHandle);
331     }
332 }
333 
odcpiInit(const OdcpiRequestCallback & callback)334 static void odcpiInit(const OdcpiRequestCallback& callback)
335 {
336     if (NULL != gGnssAdapter) {
337         gGnssAdapter->initOdcpiCommand(callback);
338     }
339 }
340 
odcpiInject(const Location & location)341 static void odcpiInject(const Location& location)
342 {
343     if (NULL != gGnssAdapter) {
344         gGnssAdapter->injectOdcpiCommand(location);
345     }
346 }
347 
blockCPI(double latitude,double longitude,float accuracy,int blockDurationMsec,double latLonDiffThreshold)348 static void blockCPI(double latitude, double longitude, float accuracy,
349                      int blockDurationMsec, double latLonDiffThreshold) {
350     if (NULL != gGnssAdapter) {
351         gGnssAdapter->blockCPICommand(latitude, longitude, accuracy,
352                                       blockDurationMsec, latLonDiffThreshold);
353     }
354 }
355 
getGnssEnergyConsumed(GnssEnergyConsumedCallback energyConsumedCb)356 static void getGnssEnergyConsumed(GnssEnergyConsumedCallback energyConsumedCb) {
357     if (NULL != gGnssAdapter) {
358         gGnssAdapter->getGnssEnergyConsumedCommand(energyConsumedCb);
359     }
360 }
361 
enableNfwLocationAccess(bool enable)362 static void enableNfwLocationAccess(bool enable) {
363     if (NULL != gGnssAdapter) {
364         gGnssAdapter->nfwControlCommand(enable);
365     }
366 }
367 
nfwInit(const NfwCbInfo & cbInfo)368 static void nfwInit(const NfwCbInfo& cbInfo) {
369     if (NULL != gGnssAdapter) {
370         gGnssAdapter->initNfwCommand(cbInfo);
371     }
372 }
getPowerStateChanges(void * powerStateCb)373 static void getPowerStateChanges(void* powerStateCb)
374 {
375     if (NULL != gGnssAdapter) {
376         gGnssAdapter->getPowerStateChangesCommand(powerStateCb);
377     }
378 }
379 
injectLocationExt(const GnssLocationInfoNotification & locationInfo)380 static void injectLocationExt(const GnssLocationInfoNotification &locationInfo)
381 {
382    if (NULL != gGnssAdapter) {
383        gGnssAdapter->injectLocationExtCommand(locationInfo);
384    }
385 }
386