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