blob: 8f67369ee4717da8f046ba2731d7283b581995d1 [file] [log] [blame]
Michael Bestas3a0209e2023-05-04 01:15:47 +03001/* Copyright (c) 2017-2020, 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
33static GnssAdapter* gGnssAdapter = NULL;
34
35static void initialize();
36static void deinitialize();
37
38static void addClient(LocationAPI* client, const LocationCallbacks& callbacks);
39static void removeClient(LocationAPI* client, removeClientCompleteCallback rmClientCb);
40static void requestCapabilities(LocationAPI* client);
41
42static uint32_t startTracking(LocationAPI* client, TrackingOptions&);
43static void updateTrackingOptions(LocationAPI* client, uint32_t id, TrackingOptions&);
44static void stopTracking(LocationAPI* client, uint32_t id);
45
46static void gnssNiResponse(LocationAPI* client, uint32_t id, GnssNiResponse response);
47static uint32_t gnssDeleteAidingData(GnssAidingData& data);
48static void gnssUpdateXtraThrottle(const bool enabled);
49
50static void setControlCallbacks(LocationControlCallbacks& controlCallbacks);
51static uint32_t enable(LocationTechnologyType techType);
52static void disable(uint32_t id);
53static uint32_t* gnssUpdateConfig(const GnssConfig& config);
54static uint32_t* gnssGetConfig(GnssConfigFlagsMask mask);
55
56static void gnssUpdateSvTypeConfig(GnssSvTypeConfig& config);
57static void gnssGetSvTypeConfig(GnssSvTypeConfigCallback& callback);
58static void gnssResetSvTypeConfig();
59
60static void injectLocation(double latitude, double longitude, float accuracy);
61static void injectLocationExt(const GnssLocationInfoNotification &locationInfo);
62static void injectTime(int64_t time, int64_t timeReference, int32_t uncertainty);
63
64static void agpsInit(const AgpsCbInfo& cbInfo);
65static void agpsDataConnOpen(AGpsExtType agpsType, const char* apnName, int apnLen, int ipType);
66static void agpsDataConnClosed(AGpsExtType agpsType);
67static void agpsDataConnFailed(AGpsExtType agpsType);
68static void getDebugReport(GnssDebugReport& report);
69static void updateConnectionStatus(bool connected, int8_t type, bool roaming,
70 NetworkHandle networkHandle, string& apn);
71static void getGnssEnergyConsumed(GnssEnergyConsumedCallback energyConsumedCb);
72static void enableNfwLocationAccess(bool enable);
73static void nfwInit(const NfwCbInfo& cbInfo);
74static void getPowerStateChanges(std::function<void(bool)> powerStateCb);
75
76static void odcpiInit(const OdcpiRequestCallback& callback, OdcpiPrioritytype priority);
77static void odcpiInject(const Location& location);
78
79static void blockCPI(double latitude, double longitude, float accuracy,
80 int blockDurationMsec, double latLonDiffThreshold);
81static void updateBatteryStatus(bool charging);
82static void updateSystemPowerState(PowerStateType systemPowerState);
83static uint32_t setConstrainedTunc (bool enable, float tuncConstraint,
84 uint32_t energyBudget);
85static uint32_t setPositionAssistedClockEstimator(bool enable);
86static uint32_t gnssUpdateSvConfig(const GnssSvTypeConfig& constellationEnablementConfig,
87 const GnssSvIdConfig& blacklistSvConfig);
88static uint32_t gnssResetSvConfig();
89static uint32_t configLeverArm(const LeverArmConfigInfo& configInfo);
90static uint32_t configRobustLocation(bool enable, bool enableForE911);
91static uint32_t configMinGpsWeek(uint16_t minGpsWeek);
92static uint32_t configDeadReckoningEngineParams(const DeadReckoningEngineConfig& dreConfig);
93static uint32_t gnssUpdateSecondaryBandConfig(const GnssSvTypeConfig& secondaryBandConfig);
94static uint32_t gnssGetSecondaryBandConfig();
95static void resetNetworkInfo();
96
97static void updateNTRIPGGAConsent(bool consentAccepted);
98static void enablePPENtripStream(const GnssNtripConnectionParams& params, bool enableRTKEngine);
99static void disablePPENtripStream();
100
101static bool measCorrInit(const measCorrSetCapabilitiesCb setCapabilitiesCb);
102static bool measCorrSetCorrections(const GnssMeasurementCorrections gnssMeasCorr);
103static void measCorrClose();
104static uint32_t antennaInfoInit(const antennaInfoCb antennaInfoCallback);
105static void antennaInfoClose();
106static uint32_t configEngineRunState(PositioningEngineMask engType, LocEngineRunState engState);
107
108static const GnssInterface gGnssInterface = {
109 sizeof(GnssInterface),
110 initialize,
111 deinitialize,
112 addClient,
113 removeClient,
114 requestCapabilities,
115 startTracking,
116 updateTrackingOptions,
117 stopTracking,
118 gnssNiResponse,
119 setControlCallbacks,
120 enable,
121 disable,
122 gnssUpdateConfig,
123 gnssGetConfig,
124 gnssUpdateSvTypeConfig,
125 gnssGetSvTypeConfig,
126 gnssResetSvTypeConfig,
127 gnssDeleteAidingData,
128 gnssUpdateXtraThrottle,
129 injectLocation,
130 injectTime,
131 agpsInit,
132 agpsDataConnOpen,
133 agpsDataConnClosed,
134 agpsDataConnFailed,
135 getDebugReport,
136 updateConnectionStatus,
137 odcpiInit,
138 odcpiInject,
139 blockCPI,
140 getGnssEnergyConsumed,
141 enableNfwLocationAccess,
142 nfwInit,
143 getPowerStateChanges,
144 injectLocationExt,
145 updateBatteryStatus,
146 updateSystemPowerState,
147 setConstrainedTunc,
148 setPositionAssistedClockEstimator,
149 gnssUpdateSvConfig,
150 configLeverArm,
151 measCorrInit,
152 measCorrSetCorrections,
153 measCorrClose,
154 antennaInfoInit,
155 antennaInfoClose,
156 configRobustLocation,
157 configMinGpsWeek,
158 configDeadReckoningEngineParams,
159 updateNTRIPGGAConsent,
160 enablePPENtripStream,
161 disablePPENtripStream,
162 gnssUpdateSecondaryBandConfig,
163 gnssGetSecondaryBandConfig,
164 resetNetworkInfo,
165 configEngineRunState
166};
167
168#ifndef DEBUG_X86
169extern "C" const GnssInterface* getGnssInterface()
170#else
171const GnssInterface* getGnssInterface()
172#endif // DEBUG_X86
173{
174 gGnssInterface.initialize();
175 return &gGnssInterface;
176}
177
178static void initialize()
179{
180 if (NULL == gGnssAdapter) {
181 gGnssAdapter = new GnssAdapter();
182 }
183}
184
185static void deinitialize()
186{
187 if (NULL != gGnssAdapter) {
188 delete gGnssAdapter;
189 gGnssAdapter = NULL;
190 }
191}
192
193static void addClient(LocationAPI* client, const LocationCallbacks& callbacks)
194{
195 if (NULL != gGnssAdapter) {
196 gGnssAdapter->addClientCommand(client, callbacks);
197 }
198}
199
200static void removeClient(LocationAPI* client, removeClientCompleteCallback rmClientCb)
201{
202 if (NULL != gGnssAdapter) {
203 gGnssAdapter->removeClientCommand(client, rmClientCb);
204 }
205}
206
207static void requestCapabilities(LocationAPI* client)
208{
209 if (NULL != gGnssAdapter) {
210 gGnssAdapter->requestCapabilitiesCommand(client);
211 }
212}
213
214static uint32_t startTracking(
215 LocationAPI* client, TrackingOptions& trackingOptions)
216{
217 if (NULL != gGnssAdapter) {
218 return gGnssAdapter->startTrackingCommand(client, trackingOptions);
219 } else {
220 return 0;
221 }
222}
223
224static void updateTrackingOptions(
225 LocationAPI* client, uint32_t id, TrackingOptions& trackingOptions)
226{
227 if (NULL != gGnssAdapter) {
228 gGnssAdapter->updateTrackingOptionsCommand(
229 client, id, trackingOptions);
230 }
231}
232
233static void stopTracking(LocationAPI* client, uint32_t id)
234{
235 if (NULL != gGnssAdapter) {
236 gGnssAdapter->stopTrackingCommand(client, id);
237 }
238}
239
240static void gnssNiResponse(LocationAPI* client, uint32_t id, GnssNiResponse response)
241{
242 if (NULL != gGnssAdapter) {
243 gGnssAdapter->gnssNiResponseCommand(client, id, response);
244 }
245}
246
247static void setControlCallbacks(LocationControlCallbacks& controlCallbacks)
248{
249 if (NULL != gGnssAdapter) {
250 gGnssAdapter->setControlCallbacksCommand(controlCallbacks);
251 }
252}
253
254static uint32_t enable(LocationTechnologyType techType)
255{
256 if (NULL != gGnssAdapter) {
257 return gGnssAdapter->enableCommand(techType);
258 } else {
259 return 0;
260 }
261}
262
263static void disable(uint32_t id)
264{
265 if (NULL != gGnssAdapter) {
266 gGnssAdapter->disableCommand(id);
267 }
268}
269
270static uint32_t* gnssUpdateConfig(const GnssConfig& config)
271{
272 if (NULL != gGnssAdapter) {
273 return gGnssAdapter->gnssUpdateConfigCommand(config);
274 } else {
275 return NULL;
276 }
277}
278
279static uint32_t* gnssGetConfig(GnssConfigFlagsMask mask)
280{
281 if (NULL != gGnssAdapter) {
282 return gGnssAdapter->gnssGetConfigCommand(mask);
283 } else {
284 return NULL;
285 }
286}
287
288static void gnssUpdateSvTypeConfig(GnssSvTypeConfig& config)
289{
290 if (NULL != gGnssAdapter) {
291 gGnssAdapter->gnssUpdateSvTypeConfigCommand(config);
292 }
293}
294
295static void gnssGetSvTypeConfig(GnssSvTypeConfigCallback& callback)
296{
297 if (NULL != gGnssAdapter) {
298 gGnssAdapter->gnssGetSvTypeConfigCommand(callback);
299 }
300}
301
302static void gnssResetSvTypeConfig()
303{
304 if (NULL != gGnssAdapter) {
305 gGnssAdapter->gnssResetSvTypeConfigCommand();
306 }
307}
308
309static uint32_t gnssDeleteAidingData(GnssAidingData& data)
310{
311 if (NULL != gGnssAdapter) {
312 return gGnssAdapter->gnssDeleteAidingDataCommand(data);
313 } else {
314 return 0;
315 }
316}
317
318static void gnssUpdateXtraThrottle(const bool enabled)
319{
320 if (NULL != gGnssAdapter) {
321 gGnssAdapter->gnssUpdateXtraThrottleCommand(enabled);
322 }
323}
324
325static void injectLocation(double latitude, double longitude, float accuracy)
326{
327 if (NULL != gGnssAdapter) {
328 gGnssAdapter->injectLocationCommand(latitude, longitude, accuracy);
329 }
330}
331
332static void injectTime(int64_t time, int64_t timeReference, int32_t uncertainty)
333{
334 if (NULL != gGnssAdapter) {
335 gGnssAdapter->injectTimeCommand(time, timeReference, uncertainty);
336 }
337}
338
339static void agpsInit(const AgpsCbInfo& cbInfo) {
340
341 if (NULL != gGnssAdapter) {
342 gGnssAdapter->initAgpsCommand(cbInfo);
343 }
344}
345static void agpsDataConnOpen(
346 AGpsExtType agpsType, const char* apnName, int apnLen, int ipType) {
347
348 if (NULL != gGnssAdapter) {
349 gGnssAdapter->dataConnOpenCommand(
350 agpsType, apnName, apnLen, (AGpsBearerType)ipType);
351 }
352}
353static void agpsDataConnClosed(AGpsExtType agpsType) {
354
355 if (NULL != gGnssAdapter) {
356 gGnssAdapter->dataConnClosedCommand(agpsType);
357 }
358}
359static void agpsDataConnFailed(AGpsExtType agpsType) {
360
361 if (NULL != gGnssAdapter) {
362 gGnssAdapter->dataConnFailedCommand(agpsType);
363 }
364}
365
366static void getDebugReport(GnssDebugReport& report) {
367
368 if (NULL != gGnssAdapter) {
369 gGnssAdapter->getDebugReport(report);
370 }
371}
372
373static void updateConnectionStatus(bool connected, int8_t type,
374 bool roaming, NetworkHandle networkHandle,
375 string& apn) {
376 if (NULL != gGnssAdapter) {
377 gGnssAdapter->getSystemStatus()->eventConnectionStatus(
378 connected, type, roaming, networkHandle, apn);
379 }
380}
381
382static void odcpiInit(const OdcpiRequestCallback& callback, OdcpiPrioritytype priority)
383{
384 if (NULL != gGnssAdapter) {
385 gGnssAdapter->initOdcpiCommand(callback, priority);
386 }
387}
388
389static void odcpiInject(const Location& location)
390{
391 if (NULL != gGnssAdapter) {
392 gGnssAdapter->injectOdcpiCommand(location);
393 }
394}
395
396static void blockCPI(double latitude, double longitude, float accuracy,
397 int blockDurationMsec, double latLonDiffThreshold) {
398 if (NULL != gGnssAdapter) {
399 gGnssAdapter->blockCPICommand(latitude, longitude, accuracy,
400 blockDurationMsec, latLonDiffThreshold);
401 }
402}
403
404static void getGnssEnergyConsumed(GnssEnergyConsumedCallback energyConsumedCb) {
405 if (NULL != gGnssAdapter) {
406 gGnssAdapter->getGnssEnergyConsumedCommand(energyConsumedCb);
407 }
408}
409
410static void enableNfwLocationAccess(bool enable) {
411 if (NULL != gGnssAdapter) {
412 gGnssAdapter->nfwControlCommand(enable);
413 }
414}
415
416static void nfwInit(const NfwCbInfo& cbInfo) {
417 if (NULL != gGnssAdapter) {
418 gGnssAdapter->initNfwCommand(cbInfo);
419 }
420}
421
422static void getPowerStateChanges(std::function<void(bool)> powerStateCb)
423{
424 if (NULL != gGnssAdapter) {
425 gGnssAdapter->getPowerStateChangesCommand(powerStateCb);
426 }
427}
428
429static void injectLocationExt(const GnssLocationInfoNotification &locationInfo)
430{
431 if (NULL != gGnssAdapter) {
432 gGnssAdapter->injectLocationExtCommand(locationInfo);
433 }
434}
435
436static void updateBatteryStatus(bool charging) {
437 if (NULL != gGnssAdapter) {
438 gGnssAdapter->getSystemStatus()->updatePowerConnectState(charging);
439 }
440}
441
442static void resetNetworkInfo() {
443 if (NULL != gGnssAdapter) {
444 gGnssAdapter->getSystemStatus()->resetNetworkInfo();
445 }
446}
447
448static void updateSystemPowerState(PowerStateType systemPowerState) {
449 if (NULL != gGnssAdapter) {
450 gGnssAdapter->updateSystemPowerStateCommand(systemPowerState);
451 }
452}
453
454static uint32_t setConstrainedTunc (bool enable, float tuncConstraint, uint32_t energyBudget) {
455 if (NULL != gGnssAdapter) {
456 return gGnssAdapter->setConstrainedTuncCommand(enable, tuncConstraint, energyBudget);
457 } else {
458 return 0;
459 }
460}
461
462static uint32_t setPositionAssistedClockEstimator(bool enable) {
463 if (NULL != gGnssAdapter) {
464 return gGnssAdapter->setPositionAssistedClockEstimatorCommand(enable);
465 } else {
466 return 0;
467 }
468}
469
470static uint32_t gnssUpdateSvConfig(
471 const GnssSvTypeConfig& constellationEnablementConfig,
472 const GnssSvIdConfig& blacklistSvConfig) {
473 if (NULL != gGnssAdapter) {
474 return gGnssAdapter->gnssUpdateSvConfigCommand(
475 constellationEnablementConfig, blacklistSvConfig);
476 } else {
477 return 0;
478 }
479}
480
481static uint32_t configLeverArm(const LeverArmConfigInfo& configInfo){
482 if (NULL != gGnssAdapter) {
483 return gGnssAdapter->configLeverArmCommand(configInfo);
484 } else {
485 return 0;
486 }
487}
488
489static bool measCorrInit(const measCorrSetCapabilitiesCb setCapabilitiesCb) {
490 if (NULL != gGnssAdapter) {
491 return gGnssAdapter->openMeasCorrCommand(setCapabilitiesCb);
492 } else {
493 return false;
494 }
495}
496
497static bool measCorrSetCorrections(const GnssMeasurementCorrections gnssMeasCorr) {
498 if (NULL != gGnssAdapter) {
499 return gGnssAdapter->measCorrSetCorrectionsCommand(gnssMeasCorr);
500 } else {
501 return false;
502 }
503}
504
505static void measCorrClose() {
506 if (NULL != gGnssAdapter) {
507 gGnssAdapter->closeMeasCorrCommand();
508 }
509}
510
511static uint32_t antennaInfoInit(const antennaInfoCb antennaInfoCallback) {
512 if (NULL != gGnssAdapter) {
513 return gGnssAdapter->antennaInfoInitCommand(antennaInfoCallback);
514 } else {
515 return ANTENNA_INFO_ERROR_GENERIC;
516 }
517}
518
519static void antennaInfoClose() {
520 if (NULL != gGnssAdapter) {
521 return gGnssAdapter->antennaInfoCloseCommand();
522 }
523}
524
525static uint32_t configRobustLocation(bool enable, bool enableForE911){
526 if (NULL != gGnssAdapter) {
527 return gGnssAdapter->configRobustLocationCommand(enable, enableForE911);
528 } else {
529 return 0;
530 }
531}
532
533static uint32_t configMinGpsWeek(uint16_t minGpsWeek){
534 if (NULL != gGnssAdapter) {
535 return gGnssAdapter->configMinGpsWeekCommand(minGpsWeek);
536 } else {
537 return 0;
538 }
539}
540
541static uint32_t configDeadReckoningEngineParams(const DeadReckoningEngineConfig& dreConfig){
542 if (NULL != gGnssAdapter) {
543 return gGnssAdapter->configDeadReckoningEngineParamsCommand(dreConfig);
544 } else {
545 return 0;
546 }
547}
548
549static uint32_t gnssUpdateSecondaryBandConfig(
550 const GnssSvTypeConfig& secondaryBandConfig) {
551 if (NULL != gGnssAdapter) {
552 return gGnssAdapter->gnssUpdateSecondaryBandConfigCommand(secondaryBandConfig);
553 } else {
554 return 0;
555 }
556}
557
558static uint32_t gnssGetSecondaryBandConfig(){
559 if (NULL != gGnssAdapter) {
560 return gGnssAdapter->gnssGetSecondaryBandConfigCommand();
561 } else {
562 return 0;
563 }
564}
565
566static void updateNTRIPGGAConsent(bool consentAccepted){
567 if (NULL != gGnssAdapter) {
568 // Call will be enabled once GnssAdapter impl. is ready.
569 gGnssAdapter->updateNTRIPGGAConsentCommand(consentAccepted);
570 }
571}
572
573static void enablePPENtripStream(const GnssNtripConnectionParams& params, bool enableRTKEngine){
574 if (NULL != gGnssAdapter) {
575 // Call will be enabled once GnssAdapter impl. is ready.
576 gGnssAdapter->enablePPENtripStreamCommand(params, enableRTKEngine);
577 }
578}
579
580static void disablePPENtripStream(){
581 if (NULL != gGnssAdapter) {
582 // Call will be enabled once GnssAdapter impl. is ready.
583 gGnssAdapter->disablePPENtripStreamCommand();
584 }
585}
586
587static uint32_t configEngineRunState(PositioningEngineMask engType, LocEngineRunState engState) {
588 if (NULL != gGnssAdapter) {
589 return gGnssAdapter->configEngineRunStateCommand(engType, engState);
590 } else {
591 return 0;
592 }
593}