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 #ifndef LOC_API_ADAPTER_BASE_H
30 #define LOC_API_ADAPTER_BASE_H
31 
32 #include <gps_extended.h>
33 #include <ContextBase.h>
34 #include <LocationAPI.h>
35 #include <map>
36 
37 #define MIN_TRACKING_INTERVAL (100) // 100 msec
38 
39 typedef struct LocationSessionKey {
40     LocationAPI* client;
41     uint32_t id;
LocationSessionKeyLocationSessionKey42     inline LocationSessionKey(LocationAPI* _client, uint32_t _id) :
43         client(_client), id(_id) {}
44 } LocationSessionKey;
45 inline bool operator <(LocationSessionKey const& left, LocationSessionKey const& right) {
46     return left.id < right.id || (left.id == right.id && left.client < right.client);
47 }
48 inline bool operator ==(LocationSessionKey const& left, LocationSessionKey const& right) {
49     return left.id == right.id && left.client == right.client;
50 }
51 inline bool operator !=(LocationSessionKey const& left, LocationSessionKey const& right) {
52     return left.id != right.id || left.client != right.client;
53 }
54 
55 typedef void (*removeClientCompleteCallback)(LocationAPI* client);
56 
57 namespace loc_core {
58 
59 class LocAdapterProxyBase;
60 
61 class LocAdapterBase {
62 private:
63     static uint32_t mSessionIdCounter;
64     const bool mIsMaster;
65     bool mIsEngineCapabilitiesKnown = false;
66 
67 protected:
68     LOC_API_ADAPTER_EVENT_MASK_T mEvtMask;
69     ContextBase* mContext;
70     LocApiBase* mLocApi;
71     LocAdapterProxyBase* mLocAdapterProxyBase;
72     const MsgTask* mMsgTask;
73     bool mAdapterAdded;
74 
LocAdapterBase(const MsgTask * msgTask)75     inline LocAdapterBase(const MsgTask* msgTask) :
76         mIsMaster(false), mEvtMask(0), mContext(NULL), mLocApi(NULL),
77         mLocAdapterProxyBase(NULL), mMsgTask(msgTask), mAdapterAdded(false) {}
78 
79     /* ==== CLIENT ========================================================================= */
80     typedef std::map<LocationAPI*, LocationCallbacks> ClientDataMap;
81     ClientDataMap mClientData;
82     std::vector<LocMsg*> mPendingMsgs; // For temporal storage of msgs before Open is completed
83     /* ======== UTILITIES ================================================================== */
84     void saveClient(LocationAPI* client, const LocationCallbacks& callbacks);
85     void eraseClient(LocationAPI* client);
86     LocationCallbacks getClientCallbacks(LocationAPI* client);
87     LocationCapabilitiesMask getCapabilities();
88     void broadcastCapabilities(LocationCapabilitiesMask mask);
89     virtual void updateClientsEventMask();
90     virtual void stopClientSessions(LocationAPI* client);
91 
92 public:
~LocAdapterBase()93     inline virtual ~LocAdapterBase() { mLocApi->removeAdapter(this); }
94     // When waitForDoneInit is not specified or specified as false,
95     // handleEngineUpEvent may be called on the child adapter object from
96     // a different thread before the constructor of the child
97     // object finishes.
98     //
99     // If the handleEngineUpEvent relies on member variables of the constructor
100     // of the child adapter to be initialized first, we need to specify the
101     // waitForDoneInit to *TRUE* to delay handleEngineUpEvent to get called
102     // until when the child adapter finishes its initialization and notify
103     // LocAdapterBase via doneInit method.
104     LocAdapterBase(const LOC_API_ADAPTER_EVENT_MASK_T mask,
105                    ContextBase* context, bool isMaster = false,
106                    LocAdapterProxyBase *adapterProxyBase = NULL,
107                    bool waitForDoneInit = false);
108 
doneInit()109     inline void doneInit() {
110         if (!mAdapterAdded) {
111             mLocApi->addAdapter(this);
112             mAdapterAdded = true;
113         }
114     }
115 
116     inline LOC_API_ADAPTER_EVENT_MASK_T
checkMask(LOC_API_ADAPTER_EVENT_MASK_T mask)117         checkMask(LOC_API_ADAPTER_EVENT_MASK_T mask) const {
118         return mEvtMask & mask;
119     }
120 
getEvtMask()121     inline LOC_API_ADAPTER_EVENT_MASK_T getEvtMask() const {
122         return mEvtMask;
123     }
124 
sendMsg(const LocMsg * msg)125     inline void sendMsg(const LocMsg* msg) const {
126         mMsgTask->sendMsg(msg);
127     }
128 
sendMsg(const LocMsg * msg)129     inline void sendMsg(const LocMsg* msg) {
130         mMsgTask->sendMsg(msg);
131     }
132 
updateEvtMask(LOC_API_ADAPTER_EVENT_MASK_T event,loc_registration_mask_status status)133     inline void updateEvtMask(LOC_API_ADAPTER_EVENT_MASK_T event,
134                               loc_registration_mask_status status)
135     {
136         switch(status) {
137             case (LOC_REGISTRATION_MASK_ENABLED):
138                 mEvtMask = mEvtMask | event;
139                 break;
140             case (LOC_REGISTRATION_MASK_DISABLED):
141                 mEvtMask = mEvtMask &~ event;
142                 break;
143             case (LOC_REGISTRATION_MASK_SET):
144                 mEvtMask = event;
145                 break;
146         }
147         mLocApi->updateEvtMask();
148     }
149 
updateNmeaMask(uint32_t mask)150     inline void updateNmeaMask(uint32_t mask)
151     {
152         mLocApi->updateNmeaMask(mask);
153     }
154 
isFeatureSupported(uint8_t featureVal)155     inline bool isFeatureSupported(uint8_t featureVal) {
156         return ContextBase::isFeatureSupported(featureVal);
157     }
158 
159     uint32_t generateSessionId();
160 
isAdapterMaster()161     inline bool isAdapterMaster() {
162         return mIsMaster;
163     }
164 
isEngineCapabilitiesKnown()165     inline bool isEngineCapabilitiesKnown() { return mIsEngineCapabilitiesKnown;}
setEngineCapabilitiesKnown(bool value)166     inline void setEngineCapabilitiesKnown(bool value) { mIsEngineCapabilitiesKnown = value;}
167 
168     virtual void handleEngineUpEvent();
169     virtual void handleEngineDownEvent();
170     virtual void reportPositionEvent(const UlpLocation& location,
171                                      const GpsLocationExtended& locationExtended,
172                                      enum loc_sess_status status,
173                                      LocPosTechMask loc_technology_mask,
174                                      GnssDataNotification* pDataNotify = nullptr,
175                                      int msInWeek = -1);
reportEnginePositionsEvent(unsigned int count,EngineLocationInfo * locationArr)176     virtual void reportEnginePositionsEvent(unsigned int count,
177                                             EngineLocationInfo* locationArr) {
178         (void)count;
179         (void)locationArr;
180     }
181     virtual void reportSvEvent(const GnssSvNotification& svNotify,
182                                bool fromEngineHub=false);
183     virtual void reportDataEvent(const GnssDataNotification& dataNotify, int msInWeek);
184     virtual void reportNmeaEvent(const char* nmea, size_t length);
185     virtual void reportSvPolynomialEvent(GnssSvPolynomial &svPolynomial);
186     virtual void reportSvEphemerisEvent(GnssSvEphemerisReport &svEphemeris);
187     virtual void reportStatus(LocGpsStatusValue status);
188     virtual bool reportXtraServer(const char* url1, const char* url2,
189                                   const char* url3, const int maxlength);
190     virtual void reportLocationSystemInfoEvent(const LocationSystemInfo& locationSystemInfo);
191 
192     virtual bool requestXtraData();
193     virtual bool requestTime();
194     virtual bool requestLocation();
195     virtual bool requestATL(int connHandle, LocAGpsType agps_type,
196                             LocApnTypeMask apn_type_mask);
197     virtual bool releaseATL(int connHandle);
198     virtual bool requestNiNotifyEvent(const GnssNiNotification &notify, const void* data,
199                                       const LocInEmergency emergencyState);
isInSession()200     inline virtual bool isInSession() { return false; }
getContext()201     ContextBase* getContext() const { return mContext; }
202     virtual void reportGnssMeasurementsEvent(const GnssMeasurements& gnssMeasurements,
203                                                 int msInWeek);
204     virtual bool reportWwanZppFix(LocGpsLocation &zppLoc);
205     virtual bool reportZppBestAvailableFix(LocGpsLocation &zppLoc,
206             GpsLocationExtended &location_extended, LocPosTechMask tech_mask);
207     virtual void reportGnssSvIdConfigEvent(const GnssSvIdConfig& config);
208     virtual void reportGnssSvTypeConfigEvent(const GnssSvTypeConfig& config);
209     virtual bool requestOdcpiEvent(OdcpiRequestInfo& request);
210     virtual bool reportGnssEngEnergyConsumedEvent(uint64_t energyConsumedSinceFirstBoot);
211     virtual bool reportDeleteAidingDataEvent(GnssAidingData &aidingData);
212     virtual bool reportKlobucharIonoModelEvent(GnssKlobucharIonoModel& ionoModel);
213     virtual bool reportGnssAdditionalSystemInfoEvent(
214             GnssAdditionalSystemInfo& additionalSystemInfo);
215     virtual void reportNfwNotificationEvent(GnssNfwNotification& notification);
216 
217     virtual void geofenceBreachEvent(size_t count, uint32_t* hwIds, Location& location,
218                                      GeofenceBreachType breachType, uint64_t timestamp);
219     virtual void geofenceStatusEvent(GeofenceStatusAvailable available);
220 
221     virtual void reportPositionEvent(UlpLocation &location,
222                                      GpsLocationExtended &locationExtended,
223                                      enum loc_sess_status status,
224                                      LocPosTechMask loc_technology_mask);
225 
226     virtual void reportLocationsEvent(const Location* locations, size_t count,
227             BatchingMode batchingMode);
228     virtual void reportCompletedTripsEvent(uint32_t accumulated_distance);
229     virtual void reportBatchStatusChangeEvent(BatchingStatus batchStatus);
230 
231     /* ==== CLIENT ========================================================================= */
232     /* ======== COMMANDS ====(Called from Client Thread)==================================== */
233     void addClientCommand(LocationAPI* client, const LocationCallbacks& callbacks);
234     void removeClientCommand(LocationAPI* client,
235                              removeClientCompleteCallback rmClientCb);
236     void requestCapabilitiesCommand(LocationAPI* client);
237 
238 };
239 
240 } // namespace loc_core
241 
242 #endif //LOC_API_ADAPTER_BASE_H
243