1 /* Copyright (c) 2011-2014, 2016-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 #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 bool LocAdapterBase::
189     requestOdcpiEvent(OdcpiRequestInfo& /*request*/)
190 DEFAULT_IMPL(false)
191 
192 bool LocAdapterBase::
193     reportGnssEngEnergyConsumedEvent(uint64_t /*energyConsumedSinceFirstBoot*/)
194 DEFAULT_IMPL(false)
195 
196 bool LocAdapterBase::
197     reportDeleteAidingDataEvent(GnssAidingData & /*aidingData*/)
198 DEFAULT_IMPL(false)
199 
200 bool LocAdapterBase::
201     reportKlobucharIonoModelEvent(GnssKlobucharIonoModel& /*ionoModel*/)
202 DEFAULT_IMPL(false)
203 
204 bool LocAdapterBase::
205     reportGnssAdditionalSystemInfoEvent(GnssAdditionalSystemInfo& /*additionalSystemInfo*/)
206 DEFAULT_IMPL(false)
207 
208 void LocAdapterBase::
209     reportNfwNotificationEvent(GnssNfwNotification& /*notification*/)
210 DEFAULT_IMPL()
211 
212 void
213 LocAdapterBase::geofenceBreachEvent(size_t /*count*/, uint32_t* /*hwIds*/, Location& /*location*/,
214                                     GeofenceBreachType /*breachType*/, uint64_t /*timestamp*/)
215 DEFAULT_IMPL()
216 
217 void
218 LocAdapterBase::geofenceStatusEvent(GeofenceStatusAvailable /*available*/)
219 DEFAULT_IMPL()
220 
221 void
222 LocAdapterBase::reportLocationsEvent(const Location* /*locations*/, size_t /*count*/,
223                                      BatchingMode /*batchingMode*/)
224 DEFAULT_IMPL()
225 
226 void
227 LocAdapterBase::reportCompletedTripsEvent(uint32_t /*accumulated_distance*/)
228 DEFAULT_IMPL()
229 
230 void
231 LocAdapterBase::reportBatchStatusChangeEvent(BatchingStatus /*batchStatus*/)
232 DEFAULT_IMPL()
233 
234 void
235 LocAdapterBase::reportPositionEvent(UlpLocation& /*location*/,
236                                     GpsLocationExtended& /*locationExtended*/,
237                                     enum loc_sess_status /*status*/,
238                                     LocPosTechMask /*loc_technology_mask*/)
239 DEFAULT_IMPL()
240 
241 void
242 LocAdapterBase::saveClient(LocationAPI* client, const LocationCallbacks& callbacks)
243 {
244     mClientData[client] = callbacks;
245     updateClientsEventMask();
246 }
247 
248 void
eraseClient(LocationAPI * client)249 LocAdapterBase::eraseClient(LocationAPI* client)
250 {
251     auto it = mClientData.find(client);
252     if (it != mClientData.end()) {
253         mClientData.erase(it);
254     }
255     updateClientsEventMask();
256 }
257 
258 LocationCallbacks
getClientCallbacks(LocationAPI * client)259 LocAdapterBase::getClientCallbacks(LocationAPI* client)
260 {
261     LocationCallbacks callbacks = {};
262     auto it = mClientData.find(client);
263     if (it != mClientData.end()) {
264         callbacks = it->second;
265     }
266     return callbacks;
267 }
268 
269 LocationCapabilitiesMask
getCapabilities()270 LocAdapterBase::getCapabilities()
271 {
272     LocationCapabilitiesMask mask = 0;
273 
274     if (isEngineCapabilitiesKnown()) {
275         // time based tracking always supported
276         mask |= LOCATION_CAPABILITIES_TIME_BASED_TRACKING_BIT;
277         if (ContextBase::isMessageSupported(
278                 LOC_API_ADAPTER_MESSAGE_DISTANCE_BASE_LOCATION_BATCHING)){
279             mask |= LOCATION_CAPABILITIES_TIME_BASED_BATCHING_BIT |
280                     LOCATION_CAPABILITIES_DISTANCE_BASED_BATCHING_BIT;
281         }
282         if (ContextBase::isMessageSupported(LOC_API_ADAPTER_MESSAGE_DISTANCE_BASE_TRACKING)) {
283             mask |= LOCATION_CAPABILITIES_DISTANCE_BASED_TRACKING_BIT;
284         }
285         if (ContextBase::isMessageSupported(LOC_API_ADAPTER_MESSAGE_OUTDOOR_TRIP_BATCHING)) {
286             mask |= LOCATION_CAPABILITIES_OUTDOOR_TRIP_BATCHING_BIT;
287         }
288         // geofence always supported
289         mask |= LOCATION_CAPABILITIES_GEOFENCE_BIT;
290         if (ContextBase::gnssConstellationConfig()) {
291             mask |= LOCATION_CAPABILITIES_GNSS_MEASUREMENTS_BIT;
292         }
293         uint32_t carrierCapabilities = ContextBase::getCarrierCapabilities();
294         if (carrierCapabilities & LOC_GPS_CAPABILITY_MSB) {
295             mask |= LOCATION_CAPABILITIES_GNSS_MSB_BIT;
296         }
297         if (LOC_GPS_CAPABILITY_MSA & carrierCapabilities) {
298             mask |= LOCATION_CAPABILITIES_GNSS_MSA_BIT;
299         }
300         if (ContextBase::isFeatureSupported(LOC_SUPPORTED_FEATURE_DEBUG_NMEA_V02)) {
301             mask |= LOCATION_CAPABILITIES_DEBUG_NMEA_BIT;
302         }
303         if (ContextBase::isFeatureSupported(LOC_SUPPORTED_FEATURE_CONSTELLATION_ENABLEMENT_V02)) {
304             mask |= LOCATION_CAPABILITIES_CONSTELLATION_ENABLEMENT_BIT;
305         }
306         if (ContextBase::isFeatureSupported(LOC_SUPPORTED_FEATURE_AGPM_V02)) {
307             mask |= LOCATION_CAPABILITIES_AGPM_BIT;
308         }
309         if (ContextBase::isFeatureSupported(LOC_SUPPORTED_FEATURE_LOCATION_PRIVACY)) {
310             mask |= LOCATION_CAPABILITIES_PRIVACY_BIT;
311         }
312     } else {
313         LOC_LOGE("%s]: attempt to get capabilities before they are known.", __func__);
314     }
315 
316     return mask;
317 }
318 
319 void
broadcastCapabilities(LocationCapabilitiesMask mask)320 LocAdapterBase::broadcastCapabilities(LocationCapabilitiesMask mask)
321 {
322     for (auto clientData : mClientData) {
323         if (nullptr != clientData.second.capabilitiesCb) {
324             clientData.second.capabilitiesCb(mask);
325         }
326     }
327 }
328 
329 void
updateClientsEventMask()330 LocAdapterBase::updateClientsEventMask()
331 DEFAULT_IMPL()
332 
333 void
334 LocAdapterBase::stopClientSessions(LocationAPI* client)
335 DEFAULT_IMPL()
336 
337 void
338 LocAdapterBase::addClientCommand(LocationAPI* client, const LocationCallbacks& callbacks)
339 {
340     LOC_LOGD("%s]: client %p", __func__, client);
341 
342     struct MsgAddClient : public LocMsg {
343         LocAdapterBase& mAdapter;
344         LocationAPI* mClient;
345         const LocationCallbacks mCallbacks;
346         inline MsgAddClient(LocAdapterBase& adapter,
347                             LocationAPI* client,
348                             const LocationCallbacks& callbacks) :
349             LocMsg(),
350             mAdapter(adapter),
351             mClient(client),
352             mCallbacks(callbacks) {}
353         inline virtual void proc() const {
354             mAdapter.saveClient(mClient, mCallbacks);
355         }
356     };
357 
358     sendMsg(new MsgAddClient(*this, client, callbacks));
359 }
360 
361 void
removeClientCommand(LocationAPI * client,removeClientCompleteCallback rmClientCb)362 LocAdapterBase::removeClientCommand(LocationAPI* client,
363                                 removeClientCompleteCallback rmClientCb)
364 {
365     LOC_LOGD("%s]: client %p", __func__, client);
366 
367     struct MsgRemoveClient : public LocMsg {
368         LocAdapterBase& mAdapter;
369         LocationAPI* mClient;
370         removeClientCompleteCallback mRmClientCb;
371         inline MsgRemoveClient(LocAdapterBase& adapter,
372                                LocationAPI* client,
373                                removeClientCompleteCallback rmCb) :
374             LocMsg(),
375             mAdapter(adapter),
376             mClient(client),
377             mRmClientCb(rmCb){}
378         inline virtual void proc() const {
379             mAdapter.stopClientSessions(mClient);
380             mAdapter.eraseClient(mClient);
381             if (nullptr != mRmClientCb) {
382                 (mRmClientCb)(mClient);
383             }
384         }
385     };
386 
387     sendMsg(new MsgRemoveClient(*this, client, rmClientCb));
388 }
389 
390 void
requestCapabilitiesCommand(LocationAPI * client)391 LocAdapterBase::requestCapabilitiesCommand(LocationAPI* client)
392 {
393     LOC_LOGD("%s]: ", __func__);
394 
395     struct MsgRequestCapabilities : public LocMsg {
396         LocAdapterBase& mAdapter;
397         LocationAPI* mClient;
398         inline MsgRequestCapabilities(LocAdapterBase& adapter,
399                                       LocationAPI* client) :
400             LocMsg(),
401             mAdapter(adapter),
402             mClient(client) {}
403         inline virtual void proc() const {
404             if (!mAdapter.isEngineCapabilitiesKnown()) {
405                 mAdapter.mPendingMsgs.push_back(new MsgRequestCapabilities(*this));
406                 return;
407             }
408             LocationCallbacks callbacks = mAdapter.getClientCallbacks(mClient);
409             if (callbacks.capabilitiesCb != nullptr) {
410                 callbacks.capabilitiesCb(mAdapter.getCapabilities());
411             }
412         }
413     };
414 
415     sendMsg(new MsgRequestCapabilities(*this, client));
416 }
417 
418 } // namespace loc_core
419