1 /* Copyright (c) 2011-2014, 2016-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 #define LOG_NDEBUG 0
30 #define LOG_TAG "LocSvc_LocAdapterBase"
31 
32 #include <dlfcn.h>
33 #include <LocAdapterBase.h>
34 #include <loc_target.h>
35 #include <log_util.h>
36 #include <LocAdapterProxyBase.h>
37 
38 namespace loc_core {
39 
40 // This is the top level class, so the constructor will
41 // always gets called. Here we prepare for the default.
42 // But if getLocApi(targetEnumType target) is overriden,
43 // the right locApi should get created.
LocAdapterBase(const LOC_API_ADAPTER_EVENT_MASK_T mask,ContextBase * context,bool isMaster,LocAdapterProxyBase * adapterProxyBase,bool waitForDoneInit)44 LocAdapterBase::LocAdapterBase(const LOC_API_ADAPTER_EVENT_MASK_T mask,
45                                ContextBase* context, bool isMaster,
46                                LocAdapterProxyBase *adapterProxyBase,
47                                bool waitForDoneInit) :
48     mIsMaster(isMaster), mEvtMask(mask), mContext(context),
49     mLocApi(context->getLocApi()), mLocAdapterProxyBase(adapterProxyBase),
50     mMsgTask(context->getMsgTask()),
51     mIsEngineCapabilitiesKnown(ContextBase::sIsEngineCapabilitiesKnown)
52 {
53     LOC_LOGd("waitForDoneInit: %d", waitForDoneInit);
54     if (!waitForDoneInit) {
55         mLocApi->addAdapter(this);
56         mAdapterAdded = true;
57     } else {
58         mAdapterAdded = false;
59     }
60 }
61 
62 uint32_t LocAdapterBase::mSessionIdCounter(1);
63 
generateSessionId()64 uint32_t LocAdapterBase::generateSessionId()
65 {
66     if (++mSessionIdCounter == 0xFFFFFFFF)
67         mSessionIdCounter = 1;
68 
69      return mSessionIdCounter;
70 }
71 
handleEngineUpEvent()72 void LocAdapterBase::handleEngineUpEvent()
73 {
74     if (mLocAdapterProxyBase) {
75         mLocAdapterProxyBase->handleEngineUpEvent();
76     }
77 }
78 
handleEngineDownEvent()79 void LocAdapterBase::handleEngineDownEvent()
80 {
81     if (mLocAdapterProxyBase) {
82         mLocAdapterProxyBase->handleEngineDownEvent();
83     }
84 }
85 
86 void LocAdapterBase::
reportPositionEvent(const UlpLocation & location,const GpsLocationExtended & locationExtended,enum loc_sess_status status,LocPosTechMask loc_technology_mask,GnssDataNotification * pDataNotify,int msInWeek)87     reportPositionEvent(const UlpLocation& location,
88                         const GpsLocationExtended& locationExtended,
89                         enum loc_sess_status status,
90                         LocPosTechMask loc_technology_mask,
91                         GnssDataNotification* pDataNotify,
92                         int msInWeek)
93 {
94     if (mLocAdapterProxyBase != NULL) {
95         mLocAdapterProxyBase->reportPositionEvent((UlpLocation&)location,
96                                                    (GpsLocationExtended&)locationExtended,
97                                                    status,
98                                                    loc_technology_mask);
99     } else {
100         DEFAULT_IMPL()
101     }
102 }
103 
104 void LocAdapterBase::
reportSvEvent(const GnssSvNotification &,bool)105     reportSvEvent(const GnssSvNotification& /*svNotify*/,
106                   bool /*fromEngineHub*/)
107 DEFAULT_IMPL()
108 
109 void LocAdapterBase::
110     reportSvPolynomialEvent(GnssSvPolynomial &/*svPolynomial*/)
111 DEFAULT_IMPL()
112 
113 void LocAdapterBase::
114     reportSvEphemerisEvent(GnssSvEphemerisReport &/*svEphemeris*/)
115 DEFAULT_IMPL()
116 
117 
118 void LocAdapterBase::
119     reportStatus(LocGpsStatusValue /*status*/)
120 DEFAULT_IMPL()
121 
122 
123 void LocAdapterBase::
124     reportNmeaEvent(const char* /*nmea*/, size_t /*length*/)
125 DEFAULT_IMPL()
126 
127 void LocAdapterBase::
128     reportDataEvent(const GnssDataNotification& /*dataNotify*/,
129                     int /*msInWeek*/)
130 DEFAULT_IMPL()
131 
132 bool LocAdapterBase::
133     reportXtraServer(const char* /*url1*/, const char* /*url2*/,
134                      const char* /*url3*/, const int /*maxlength*/)
135 DEFAULT_IMPL(false)
136 
137 void LocAdapterBase::
138     reportLocationSystemInfoEvent(const LocationSystemInfo& /*locationSystemInfo*/)
139 DEFAULT_IMPL()
140 
141 bool LocAdapterBase::
142     requestXtraData()
143 DEFAULT_IMPL(false)
144 
145 bool LocAdapterBase::
146     requestTime()
147 DEFAULT_IMPL(false)
148 
149 bool LocAdapterBase::
150     requestLocation()
151 DEFAULT_IMPL(false)
152 
153 bool LocAdapterBase::
154     requestATL(int /*connHandle*/, LocAGpsType /*agps_type*/,
155                LocApnTypeMask /*apn_type_mask*/)
156 DEFAULT_IMPL(false)
157 
158 bool LocAdapterBase::
159     releaseATL(int /*connHandle*/)
160 DEFAULT_IMPL(false)
161 
162 bool LocAdapterBase::
163     requestNiNotifyEvent(const GnssNiNotification &/*notify*/,
164                          const void* /*data*/,
165                          const LocInEmergency emergencyState)
166 DEFAULT_IMPL(false)
167 
168 void LocAdapterBase::
169 reportGnssMeasurementsEvent(const GnssMeasurements& /*gnssMeasurements*/,
170                                    int /*msInWeek*/)
171 DEFAULT_IMPL()
172 
173 bool LocAdapterBase::
174     reportWwanZppFix(LocGpsLocation &/*zppLoc*/)
175 DEFAULT_IMPL(false)
176 
177 bool LocAdapterBase::
178     reportZppBestAvailableFix(LocGpsLocation& /*zppLoc*/,
179             GpsLocationExtended& /*location_extended*/, LocPosTechMask /*tech_mask*/)
180 DEFAULT_IMPL(false)
181 
182 void LocAdapterBase::reportGnssSvIdConfigEvent(const GnssSvIdConfig& /*config*/)
183 DEFAULT_IMPL()
184 
185 void LocAdapterBase::reportGnssSvTypeConfigEvent(const GnssSvTypeConfig& /*config*/)
186 DEFAULT_IMPL()
187 
188 void LocAdapterBase::reportGnssConfigEvent(uint32_t,  /* session id*/
189             const GnssConfig& /*gnssConfig*/)
190 DEFAULT_IMPL()
191 
192 bool LocAdapterBase::
193     requestOdcpiEvent(OdcpiRequestInfo& /*request*/)
194 DEFAULT_IMPL(false)
195 
196 bool LocAdapterBase::
197     reportGnssEngEnergyConsumedEvent(uint64_t /*energyConsumedSinceFirstBoot*/)
198 DEFAULT_IMPL(false)
199 
200 bool LocAdapterBase::
201     reportDeleteAidingDataEvent(GnssAidingData & /*aidingData*/)
202 DEFAULT_IMPL(false)
203 
204 bool LocAdapterBase::
205     reportKlobucharIonoModelEvent(GnssKlobucharIonoModel& /*ionoModel*/)
206 DEFAULT_IMPL(false)
207 
208 bool LocAdapterBase::
209     reportGnssAdditionalSystemInfoEvent(GnssAdditionalSystemInfo& /*additionalSystemInfo*/)
210 DEFAULT_IMPL(false)
211 
212 void LocAdapterBase::
213     reportNfwNotificationEvent(GnssNfwNotification& /*notification*/)
214 DEFAULT_IMPL()
215 
216 void
217 LocAdapterBase::geofenceBreachEvent(size_t /*count*/, uint32_t* /*hwIds*/, Location& /*location*/,
218                                     GeofenceBreachType /*breachType*/, uint64_t /*timestamp*/)
219 DEFAULT_IMPL()
220 
221 void
222 LocAdapterBase::geofenceStatusEvent(GeofenceStatusAvailable /*available*/)
223 DEFAULT_IMPL()
224 
225 void
226 LocAdapterBase::reportLocationsEvent(const Location* /*locations*/, size_t /*count*/,
227                                      BatchingMode /*batchingMode*/)
228 DEFAULT_IMPL()
229 
230 void
231 LocAdapterBase::reportCompletedTripsEvent(uint32_t /*accumulated_distance*/)
232 DEFAULT_IMPL()
233 
234 void
235 LocAdapterBase::reportBatchStatusChangeEvent(BatchingStatus /*batchStatus*/)
236 DEFAULT_IMPL()
237 
238 void
239 LocAdapterBase::reportPositionEvent(UlpLocation& /*location*/,
240                                     GpsLocationExtended& /*locationExtended*/,
241                                     enum loc_sess_status /*status*/,
242                                     LocPosTechMask /*loc_technology_mask*/)
243 DEFAULT_IMPL()
244 
245 void
246 LocAdapterBase::saveClient(LocationAPI* client, const LocationCallbacks& callbacks)
247 {
248     mClientData[client] = callbacks;
249     updateClientsEventMask();
250 }
251 
252 void
eraseClient(LocationAPI * client)253 LocAdapterBase::eraseClient(LocationAPI* client)
254 {
255     auto it = mClientData.find(client);
256     if (it != mClientData.end()) {
257         mClientData.erase(it);
258     }
259     updateClientsEventMask();
260 }
261 
262 LocationCallbacks
getClientCallbacks(LocationAPI * client)263 LocAdapterBase::getClientCallbacks(LocationAPI* client)
264 {
265     LocationCallbacks callbacks = {};
266     auto it = mClientData.find(client);
267     if (it != mClientData.end()) {
268         callbacks = it->second;
269     }
270     return callbacks;
271 }
272 
273 LocationCapabilitiesMask
getCapabilities()274 LocAdapterBase::getCapabilities()
275 {
276     LocationCapabilitiesMask mask = 0;
277 
278     if (isEngineCapabilitiesKnown()) {
279         // time based tracking always supported
280         mask |= LOCATION_CAPABILITIES_TIME_BASED_TRACKING_BIT;
281         if (ContextBase::isMessageSupported(
282                 LOC_API_ADAPTER_MESSAGE_DISTANCE_BASE_LOCATION_BATCHING)){
283             mask |= LOCATION_CAPABILITIES_TIME_BASED_BATCHING_BIT |
284                     LOCATION_CAPABILITIES_DISTANCE_BASED_BATCHING_BIT;
285         }
286         if (ContextBase::isMessageSupported(LOC_API_ADAPTER_MESSAGE_DISTANCE_BASE_TRACKING)) {
287             mask |= LOCATION_CAPABILITIES_DISTANCE_BASED_TRACKING_BIT;
288         }
289         if (ContextBase::isMessageSupported(LOC_API_ADAPTER_MESSAGE_OUTDOOR_TRIP_BATCHING)) {
290             mask |= LOCATION_CAPABILITIES_OUTDOOR_TRIP_BATCHING_BIT;
291         }
292         // geofence always supported
293         mask |= LOCATION_CAPABILITIES_GEOFENCE_BIT;
294         if (ContextBase::gnssConstellationConfig()) {
295             mask |= LOCATION_CAPABILITIES_GNSS_MEASUREMENTS_BIT;
296         }
297         uint32_t carrierCapabilities = ContextBase::getCarrierCapabilities();
298         if (carrierCapabilities & LOC_GPS_CAPABILITY_MSB) {
299             mask |= LOCATION_CAPABILITIES_GNSS_MSB_BIT;
300         }
301         if (LOC_GPS_CAPABILITY_MSA & carrierCapabilities) {
302             mask |= LOCATION_CAPABILITIES_GNSS_MSA_BIT;
303         }
304         if (ContextBase::isFeatureSupported(LOC_SUPPORTED_FEATURE_DEBUG_NMEA_V02)) {
305             mask |= LOCATION_CAPABILITIES_DEBUG_NMEA_BIT;
306         }
307         if (ContextBase::isFeatureSupported(LOC_SUPPORTED_FEATURE_CONSTELLATION_ENABLEMENT_V02)) {
308             mask |= LOCATION_CAPABILITIES_CONSTELLATION_ENABLEMENT_BIT;
309         }
310         if (ContextBase::isFeatureSupported(LOC_SUPPORTED_FEATURE_AGPM_V02)) {
311             mask |= LOCATION_CAPABILITIES_AGPM_BIT;
312         }
313         if (ContextBase::isFeatureSupported(LOC_SUPPORTED_FEATURE_LOCATION_PRIVACY)) {
314             mask |= LOCATION_CAPABILITIES_PRIVACY_BIT;
315         }
316         if (ContextBase::isFeatureSupported(LOC_SUPPORTED_FEATURE_MEASUREMENTS_CORRECTION)) {
317             mask |= LOCATION_CAPABILITIES_MEASUREMENTS_CORRECTION_BIT;
318         }
319         if (ContextBase::isFeatureSupported(LOC_SUPPORTED_FEATURE_ROBUST_LOCATION)) {
320             mask |= LOCATION_CAPABILITIES_CONFORMITY_INDEX_BIT;
321         }
322         if (ContextBase::isFeatureSupported(LOC_SUPPORTED_FEATURE_EDGNSS)) {
323             mask |= LOCATION_CAPABILITIES_EDGNSS_BIT;
324         }
325     } else {
326         LOC_LOGE("%s]: attempt to get capabilities before they are known.", __func__);
327     }
328 
329     return mask;
330 }
331 
332 void
broadcastCapabilities(LocationCapabilitiesMask mask)333 LocAdapterBase::broadcastCapabilities(LocationCapabilitiesMask mask)
334 {
335     for (auto clientData : mClientData) {
336         if (nullptr != clientData.second.capabilitiesCb) {
337             clientData.second.capabilitiesCb(mask);
338         }
339     }
340 }
341 
342 void
updateClientsEventMask()343 LocAdapterBase::updateClientsEventMask()
344 DEFAULT_IMPL()
345 
346 void
347 LocAdapterBase::stopClientSessions(LocationAPI* client)
348 DEFAULT_IMPL()
349 
350 void
351 LocAdapterBase::addClientCommand(LocationAPI* client, const LocationCallbacks& callbacks)
352 {
353     LOC_LOGD("%s]: client %p", __func__, client);
354 
355     struct MsgAddClient : public LocMsg {
356         LocAdapterBase& mAdapter;
357         LocationAPI* mClient;
358         const LocationCallbacks mCallbacks;
359         inline MsgAddClient(LocAdapterBase& adapter,
360                             LocationAPI* client,
361                             const LocationCallbacks& callbacks) :
362             LocMsg(),
363             mAdapter(adapter),
364             mClient(client),
365             mCallbacks(callbacks) {}
366         inline virtual void proc() const {
367             mAdapter.saveClient(mClient, mCallbacks);
368         }
369     };
370 
371     sendMsg(new MsgAddClient(*this, client, callbacks));
372 }
373 
374 void
removeClientCommand(LocationAPI * client,removeClientCompleteCallback rmClientCb)375 LocAdapterBase::removeClientCommand(LocationAPI* client,
376                                 removeClientCompleteCallback rmClientCb)
377 {
378     LOC_LOGD("%s]: client %p", __func__, client);
379 
380     struct MsgRemoveClient : public LocMsg {
381         LocAdapterBase& mAdapter;
382         LocationAPI* mClient;
383         removeClientCompleteCallback mRmClientCb;
384         inline MsgRemoveClient(LocAdapterBase& adapter,
385                                LocationAPI* client,
386                                removeClientCompleteCallback rmCb) :
387             LocMsg(),
388             mAdapter(adapter),
389             mClient(client),
390             mRmClientCb(rmCb){}
391         inline virtual void proc() const {
392             mAdapter.stopClientSessions(mClient);
393             mAdapter.eraseClient(mClient);
394             if (nullptr != mRmClientCb) {
395                 (mRmClientCb)(mClient);
396             }
397         }
398     };
399 
400     sendMsg(new MsgRemoveClient(*this, client, rmClientCb));
401 }
402 
403 void
requestCapabilitiesCommand(LocationAPI * client)404 LocAdapterBase::requestCapabilitiesCommand(LocationAPI* client)
405 {
406     LOC_LOGD("%s]: ", __func__);
407 
408     struct MsgRequestCapabilities : public LocMsg {
409         LocAdapterBase& mAdapter;
410         LocationAPI* mClient;
411         inline MsgRequestCapabilities(LocAdapterBase& adapter,
412                                       LocationAPI* client) :
413             LocMsg(),
414             mAdapter(adapter),
415             mClient(client) {}
416         inline virtual void proc() const {
417             if (!mAdapter.isEngineCapabilitiesKnown()) {
418                 mAdapter.mPendingMsgs.push_back(new MsgRequestCapabilities(*this));
419                 return;
420             }
421             LocationCallbacks callbacks = mAdapter.getClientCallbacks(mClient);
422             if (callbacks.capabilitiesCb != nullptr) {
423                 callbacks.capabilitiesCb(mAdapter.getCapabilities());
424             }
425         }
426     };
427 
428     sendMsg(new MsgRequestCapabilities(*this, client));
429 }
430 
431 void
432 LocAdapterBase::reportLatencyInfoEvent(const GnssLatencyInfo& /*gnssLatencyInfo*/)
433 DEFAULT_IMPL()
434 
435 bool LocAdapterBase::
436     reportQwesCapabilities(const std::unordered_map<LocationQwesFeatureType, bool> &featureMap)
437 DEFAULT_IMPL(false)
438 
439 } // namespace loc_core
440