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 static void updateBatteryStatus(bool charging);
82 static void updateSystemPowerState(PowerStateType systemPowerState);
83 static uint32_t setConstrainedTunc (bool enable, float tuncConstraint,
84                                     uint32_t energyBudget);
85 static uint32_t setPositionAssistedClockEstimator(bool enable);
86 static uint32_t gnssUpdateSvConfig(const GnssSvTypeConfig& svTypeConfig,
87                                    const GnssSvIdConfig& svIdConfig);
88 static uint32_t gnssResetSvConfig();
89 static uint32_t configLeverArm(const LeverArmConfigInfo& configInfo);
90 
91 static const GnssInterface gGnssInterface = {
92     sizeof(GnssInterface),
93     initialize,
94     deinitialize,
95     addClient,
96     removeClient,
97     requestCapabilities,
98     startTracking,
99     updateTrackingOptions,
100     stopTracking,
101     gnssNiResponse,
102     setControlCallbacks,
103     enable,
104     disable,
105     gnssUpdateConfig,
106     gnssGetConfig,
107     gnssUpdateSvTypeConfig,
108     gnssGetSvTypeConfig,
109     gnssResetSvTypeConfig,
110     gnssDeleteAidingData,
111     gnssUpdateXtraThrottle,
112     injectLocation,
113     injectTime,
114     agpsInit,
115     agpsDataConnOpen,
116     agpsDataConnClosed,
117     agpsDataConnFailed,
118     getDebugReport,
119     updateConnectionStatus,
120     odcpiInit,
121     odcpiInject,
122     blockCPI,
123     getGnssEnergyConsumed,
124     enableNfwLocationAccess,
125     nfwInit,
126     getPowerStateChanges,
127     injectLocationExt,
128     updateBatteryStatus,
129     updateSystemPowerState,
130     setConstrainedTunc,
131     setPositionAssistedClockEstimator,
132     gnssUpdateSvConfig,
133     gnssResetSvConfig,
134     configLeverArm,
135 };
136 
137 #ifndef DEBUG_X86
getGnssInterface()138 extern "C" const GnssInterface* getGnssInterface()
139 #else
140 const GnssInterface* getGnssInterface()
141 #endif // DEBUG_X86
142 {
143    gGnssInterface.initialize();
144    return &gGnssInterface;
145 }
146 
initialize()147 static void initialize()
148 {
149     if (NULL == gGnssAdapter) {
150         gGnssAdapter = new GnssAdapter();
151     }
152 }
153 
deinitialize()154 static void deinitialize()
155 {
156     if (NULL != gGnssAdapter) {
157         delete gGnssAdapter;
158         gGnssAdapter = NULL;
159     }
160 }
161 
addClient(LocationAPI * client,const LocationCallbacks & callbacks)162 static void addClient(LocationAPI* client, const LocationCallbacks& callbacks)
163 {
164     if (NULL != gGnssAdapter) {
165         gGnssAdapter->addClientCommand(client, callbacks);
166     }
167 }
168 
removeClient(LocationAPI * client,removeClientCompleteCallback rmClientCb)169 static void removeClient(LocationAPI* client, removeClientCompleteCallback rmClientCb)
170 {
171     if (NULL != gGnssAdapter) {
172         gGnssAdapter->removeClientCommand(client, rmClientCb);
173     }
174 }
175 
requestCapabilities(LocationAPI * client)176 static void requestCapabilities(LocationAPI* client)
177 {
178     if (NULL != gGnssAdapter) {
179         gGnssAdapter->requestCapabilitiesCommand(client);
180     }
181 }
182 
startTracking(LocationAPI * client,TrackingOptions & trackingOptions)183 static uint32_t startTracking(
184         LocationAPI* client, TrackingOptions& trackingOptions)
185 {
186     if (NULL != gGnssAdapter) {
187         return gGnssAdapter->startTrackingCommand(client, trackingOptions);
188     } else {
189         return 0;
190     }
191 }
192 
updateTrackingOptions(LocationAPI * client,uint32_t id,TrackingOptions & trackingOptions)193 static void updateTrackingOptions(
194         LocationAPI* client, uint32_t id, TrackingOptions& trackingOptions)
195 {
196     if (NULL != gGnssAdapter) {
197         gGnssAdapter->updateTrackingOptionsCommand(
198                 client, id, trackingOptions);
199     }
200 }
201 
stopTracking(LocationAPI * client,uint32_t id)202 static void stopTracking(LocationAPI* client, uint32_t id)
203 {
204     if (NULL != gGnssAdapter) {
205         gGnssAdapter->stopTrackingCommand(client, id);
206     }
207 }
208 
gnssNiResponse(LocationAPI * client,uint32_t id,GnssNiResponse response)209 static void gnssNiResponse(LocationAPI* client, uint32_t id, GnssNiResponse response)
210 {
211     if (NULL != gGnssAdapter) {
212         gGnssAdapter->gnssNiResponseCommand(client, id, response);
213     }
214 }
215 
setControlCallbacks(LocationControlCallbacks & controlCallbacks)216 static void setControlCallbacks(LocationControlCallbacks& controlCallbacks)
217 {
218     if (NULL != gGnssAdapter) {
219         gGnssAdapter->setControlCallbacksCommand(controlCallbacks);
220     }
221 }
222 
enable(LocationTechnologyType techType)223 static uint32_t enable(LocationTechnologyType techType)
224 {
225     if (NULL != gGnssAdapter) {
226         return gGnssAdapter->enableCommand(techType);
227     } else {
228         return 0;
229     }
230 }
231 
disable(uint32_t id)232 static void disable(uint32_t id)
233 {
234     if (NULL != gGnssAdapter) {
235         gGnssAdapter->disableCommand(id);
236     }
237 }
238 
gnssUpdateConfig(GnssConfig config)239 static uint32_t* gnssUpdateConfig(GnssConfig config)
240 {
241     if (NULL != gGnssAdapter) {
242         return gGnssAdapter->gnssUpdateConfigCommand(config);
243     } else {
244         return NULL;
245     }
246 }
247 
gnssGetConfig(GnssConfigFlagsMask mask)248 static uint32_t* gnssGetConfig(GnssConfigFlagsMask mask)
249 {
250     if (NULL != gGnssAdapter) {
251         return gGnssAdapter->gnssGetConfigCommand(mask);
252     } else {
253         return NULL;
254     }
255 }
256 
gnssUpdateSvTypeConfig(GnssSvTypeConfig & config)257 static void gnssUpdateSvTypeConfig(GnssSvTypeConfig& config)
258 {
259     if (NULL != gGnssAdapter) {
260         gGnssAdapter->gnssUpdateSvTypeConfigCommand(config);
261     }
262 }
263 
gnssGetSvTypeConfig(GnssSvTypeConfigCallback & callback)264 static void gnssGetSvTypeConfig(GnssSvTypeConfigCallback& callback)
265 {
266     if (NULL != gGnssAdapter) {
267         gGnssAdapter->gnssGetSvTypeConfigCommand(callback);
268     }
269 }
270 
gnssResetSvTypeConfig()271 static void gnssResetSvTypeConfig()
272 {
273     if (NULL != gGnssAdapter) {
274         gGnssAdapter->gnssResetSvTypeConfigCommand();
275     }
276 }
277 
gnssDeleteAidingData(GnssAidingData & data)278 static uint32_t gnssDeleteAidingData(GnssAidingData& data)
279 {
280     if (NULL != gGnssAdapter) {
281         return gGnssAdapter->gnssDeleteAidingDataCommand(data);
282     } else {
283         return 0;
284     }
285 }
286 
gnssUpdateXtraThrottle(const bool enabled)287 static void gnssUpdateXtraThrottle(const bool enabled)
288 {
289     if (NULL != gGnssAdapter) {
290         gGnssAdapter->gnssUpdateXtraThrottleCommand(enabled);
291     }
292 }
293 
injectLocation(double latitude,double longitude,float accuracy)294 static void injectLocation(double latitude, double longitude, float accuracy)
295 {
296    if (NULL != gGnssAdapter) {
297        gGnssAdapter->injectLocationCommand(latitude, longitude, accuracy);
298    }
299 }
300 
injectTime(int64_t time,int64_t timeReference,int32_t uncertainty)301 static void injectTime(int64_t time, int64_t timeReference, int32_t uncertainty)
302 {
303    if (NULL != gGnssAdapter) {
304        gGnssAdapter->injectTimeCommand(time, timeReference, uncertainty);
305    }
306 }
307 
agpsInit(const AgpsCbInfo & cbInfo)308 static void agpsInit(const AgpsCbInfo& cbInfo) {
309 
310     if (NULL != gGnssAdapter) {
311         gGnssAdapter->initAgpsCommand(cbInfo);
312     }
313 }
agpsDataConnOpen(AGpsExtType agpsType,const char * apnName,int apnLen,int ipType)314 static void agpsDataConnOpen(
315         AGpsExtType agpsType, const char* apnName, int apnLen, int ipType) {
316 
317     if (NULL != gGnssAdapter) {
318         gGnssAdapter->dataConnOpenCommand(
319                 agpsType, apnName, apnLen, (AGpsBearerType)ipType);
320     }
321 }
agpsDataConnClosed(AGpsExtType agpsType)322 static void agpsDataConnClosed(AGpsExtType agpsType) {
323 
324     if (NULL != gGnssAdapter) {
325         gGnssAdapter->dataConnClosedCommand(agpsType);
326     }
327 }
agpsDataConnFailed(AGpsExtType agpsType)328 static void agpsDataConnFailed(AGpsExtType agpsType) {
329 
330     if (NULL != gGnssAdapter) {
331         gGnssAdapter->dataConnFailedCommand(agpsType);
332     }
333 }
334 
getDebugReport(GnssDebugReport & report)335 static void getDebugReport(GnssDebugReport& report) {
336 
337     if (NULL != gGnssAdapter) {
338         gGnssAdapter->getDebugReport(report);
339     }
340 }
341 
updateConnectionStatus(bool connected,int8_t type,bool roaming,NetworkHandle networkHandle)342 static void updateConnectionStatus(bool connected, int8_t type,
343                                    bool roaming, NetworkHandle networkHandle) {
344     if (NULL != gGnssAdapter) {
345         gGnssAdapter->getSystemStatus()->eventConnectionStatus(
346                 connected, type, roaming, networkHandle);
347     }
348 }
349 
odcpiInit(const OdcpiRequestCallback & callback)350 static void odcpiInit(const OdcpiRequestCallback& callback)
351 {
352     if (NULL != gGnssAdapter) {
353         gGnssAdapter->initOdcpiCommand(callback);
354     }
355 }
356 
odcpiInject(const Location & location)357 static void odcpiInject(const Location& location)
358 {
359     if (NULL != gGnssAdapter) {
360         gGnssAdapter->injectOdcpiCommand(location);
361     }
362 }
363 
blockCPI(double latitude,double longitude,float accuracy,int blockDurationMsec,double latLonDiffThreshold)364 static void blockCPI(double latitude, double longitude, float accuracy,
365                      int blockDurationMsec, double latLonDiffThreshold) {
366     if (NULL != gGnssAdapter) {
367         gGnssAdapter->blockCPICommand(latitude, longitude, accuracy,
368                                       blockDurationMsec, latLonDiffThreshold);
369     }
370 }
371 
getGnssEnergyConsumed(GnssEnergyConsumedCallback energyConsumedCb)372 static void getGnssEnergyConsumed(GnssEnergyConsumedCallback energyConsumedCb) {
373     if (NULL != gGnssAdapter) {
374         gGnssAdapter->getGnssEnergyConsumedCommand(energyConsumedCb);
375     }
376 }
377 
enableNfwLocationAccess(bool enable)378 static void enableNfwLocationAccess(bool enable) {
379     if (NULL != gGnssAdapter) {
380         gGnssAdapter->nfwControlCommand(enable);
381     }
382 }
383 
nfwInit(const NfwCbInfo & cbInfo)384 static void nfwInit(const NfwCbInfo& cbInfo) {
385     if (NULL != gGnssAdapter) {
386         gGnssAdapter->initNfwCommand(cbInfo);
387     }
388 }
getPowerStateChanges(void * powerStateCb)389 static void getPowerStateChanges(void* powerStateCb)
390 {
391     if (NULL != gGnssAdapter) {
392         gGnssAdapter->getPowerStateChangesCommand(powerStateCb);
393     }
394 }
395 
injectLocationExt(const GnssLocationInfoNotification & locationInfo)396 static void injectLocationExt(const GnssLocationInfoNotification &locationInfo)
397 {
398    if (NULL != gGnssAdapter) {
399        gGnssAdapter->injectLocationExtCommand(locationInfo);
400    }
401 }
402 
updateBatteryStatus(bool charging)403 static void updateBatteryStatus(bool charging) {
404     if (NULL != gGnssAdapter) {
405         gGnssAdapter->getSystemStatus()->updatePowerConnectState(charging);
406     }
407 }
408 
updateSystemPowerState(PowerStateType systemPowerState)409 static void updateSystemPowerState(PowerStateType systemPowerState) {
410    if (NULL != gGnssAdapter) {
411        gGnssAdapter->updateSystemPowerStateCommand(systemPowerState);
412    }
413 }
414 
setConstrainedTunc(bool enable,float tuncConstraint,uint32_t energyBudget)415 static uint32_t setConstrainedTunc (bool enable, float tuncConstraint, uint32_t energyBudget) {
416     if (NULL != gGnssAdapter) {
417         return gGnssAdapter->setConstrainedTuncCommand(enable, tuncConstraint, energyBudget);
418     } else {
419         return 0;
420     }
421 }
422 
setPositionAssistedClockEstimator(bool enable)423 static uint32_t setPositionAssistedClockEstimator(bool enable) {
424     if (NULL != gGnssAdapter) {
425         return gGnssAdapter->setPositionAssistedClockEstimatorCommand(enable);
426     } else {
427         return 0;
428     }
429 }
430 
gnssUpdateSvConfig(const GnssSvTypeConfig & svTypeConfig,const GnssSvIdConfig & svIdConfig)431 static uint32_t gnssUpdateSvConfig(
432         const GnssSvTypeConfig& svTypeConfig,
433         const GnssSvIdConfig& svIdConfig) {
434     if (NULL != gGnssAdapter) {
435         return gGnssAdapter->gnssUpdateSvConfigCommand(
436                 svTypeConfig, svIdConfig);
437     } else {
438         return 0;
439     }
440 }
441 
gnssResetSvConfig()442 static uint32_t gnssResetSvConfig() {
443     if (NULL != gGnssAdapter) {
444         return gGnssAdapter->gnssResetSvConfigCommand();
445     } else {
446         return 0;
447     }
448 }
449 
configLeverArm(const LeverArmConfigInfo & configInfo)450 static uint32_t configLeverArm(const LeverArmConfigInfo& configInfo){
451     if (NULL != gGnssAdapter) {
452         return gGnssAdapter->configLeverArmCommand(configInfo);
453     } else {
454         return 0;
455     }
456 }
457