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;
LocAdapterBase(const MsgTask * msgTask)73     inline LocAdapterBase(const MsgTask* msgTask) :
74         mIsMaster(false), mEvtMask(0), mContext(NULL), mLocApi(NULL),
75         mLocAdapterProxyBase(NULL), mMsgTask(msgTask) {}
76 
77     /* ==== CLIENT ========================================================================= */
78     typedef std::map<LocationAPI*, LocationCallbacks> ClientDataMap;
79     ClientDataMap mClientData;
80     std::vector<LocMsg*> mPendingMsgs; // For temporal storage of msgs before Open is completed
81     /* ======== UTILITIES ================================================================== */
82     void saveClient(LocationAPI* client, const LocationCallbacks& callbacks);
83     void eraseClient(LocationAPI* client);
84     LocationCallbacks getClientCallbacks(LocationAPI* client);
85     LocationCapabilitiesMask getCapabilities();
86     void broadcastCapabilities(LocationCapabilitiesMask mask);
87     virtual void updateClientsEventMask();
88     virtual void stopClientSessions(LocationAPI* client);
89 
90 public:
~LocAdapterBase()91     inline virtual ~LocAdapterBase() { mLocApi->removeAdapter(this); }
92     LocAdapterBase(const LOC_API_ADAPTER_EVENT_MASK_T mask,
93                    ContextBase* context, bool isMaster = false,
94                    LocAdapterProxyBase *adapterProxyBase = NULL);
95 
96     inline LOC_API_ADAPTER_EVENT_MASK_T
checkMask(LOC_API_ADAPTER_EVENT_MASK_T mask)97         checkMask(LOC_API_ADAPTER_EVENT_MASK_T mask) const {
98         return mEvtMask & mask;
99     }
100 
getEvtMask()101     inline LOC_API_ADAPTER_EVENT_MASK_T getEvtMask() const {
102         return mEvtMask;
103     }
104 
sendMsg(const LocMsg * msg)105     inline void sendMsg(const LocMsg* msg) const {
106         mMsgTask->sendMsg(msg);
107     }
108 
sendMsg(const LocMsg * msg)109     inline void sendMsg(const LocMsg* msg) {
110         mMsgTask->sendMsg(msg);
111     }
112 
updateEvtMask(LOC_API_ADAPTER_EVENT_MASK_T event,loc_registration_mask_status status)113     inline void updateEvtMask(LOC_API_ADAPTER_EVENT_MASK_T event,
114                               loc_registration_mask_status status)
115     {
116         switch(status) {
117             case (LOC_REGISTRATION_MASK_ENABLED):
118                 mEvtMask = mEvtMask | event;
119                 break;
120             case (LOC_REGISTRATION_MASK_DISABLED):
121                 mEvtMask = mEvtMask &~ event;
122                 break;
123             case (LOC_REGISTRATION_MASK_SET):
124                 mEvtMask = event;
125                 break;
126         }
127         mLocApi->updateEvtMask();
128     }
129 
updateNmeaMask(uint32_t mask)130     inline void updateNmeaMask(uint32_t mask)
131     {
132         mLocApi->updateNmeaMask(mask);
133     }
134 
isFeatureSupported(uint8_t featureVal)135     inline bool isFeatureSupported(uint8_t featureVal) {
136         return ContextBase::isFeatureSupported(featureVal);
137     }
138 
139     uint32_t generateSessionId();
140 
isAdapterMaster()141     inline bool isAdapterMaster() {
142         return mIsMaster;
143     }
144 
isEngineCapabilitiesKnown()145     inline bool isEngineCapabilitiesKnown() { return mIsEngineCapabilitiesKnown;}
setEngineCapabilitiesKnown(bool value)146     inline void setEngineCapabilitiesKnown(bool value) { mIsEngineCapabilitiesKnown = value;}
147 
148     virtual void handleEngineUpEvent();
149     virtual void handleEngineDownEvent();
150     virtual void reportPositionEvent(const UlpLocation& location,
151                                      const GpsLocationExtended& locationExtended,
152                                      enum loc_sess_status status,
153                                      LocPosTechMask loc_technology_mask,
154                                      GnssDataNotification* pDataNotify = nullptr,
155                                      int msInWeek = -1);
reportEnginePositionsEvent(unsigned int count,EngineLocationInfo * locationArr)156     virtual void reportEnginePositionsEvent(unsigned int count,
157                                             EngineLocationInfo* locationArr) {
158         (void)count;
159         (void)locationArr;
160     }
161     virtual void reportSvEvent(const GnssSvNotification& svNotify,
162                                bool fromEngineHub=false);
163     virtual void reportDataEvent(const GnssDataNotification& dataNotify, int msInWeek);
164     virtual void reportNmeaEvent(const char* nmea, size_t length);
165     virtual void reportSvPolynomialEvent(GnssSvPolynomial &svPolynomial);
166     virtual void reportSvEphemerisEvent(GnssSvEphemerisReport &svEphemeris);
167     virtual void reportStatus(LocGpsStatusValue status);
168     virtual bool reportXtraServer(const char* url1, const char* url2,
169                                   const char* url3, const int maxlength);
170     virtual void reportLocationSystemInfoEvent(const LocationSystemInfo& locationSystemInfo);
171 
172     virtual bool requestXtraData();
173     virtual bool requestTime();
174     virtual bool requestLocation();
175     virtual bool requestATL(int connHandle, LocAGpsType agps_type,
176                             LocApnTypeMask apn_type_mask);
177     virtual bool releaseATL(int connHandle);
178     virtual bool requestNiNotifyEvent(const GnssNiNotification &notify, const void* data,
179                                       const LocInEmergency emergencyState);
isInSession()180     inline virtual bool isInSession() { return false; }
getContext()181     ContextBase* getContext() const { return mContext; }
182     virtual void reportGnssMeasurementsEvent(const GnssMeasurements& gnssMeasurements,
183                                                 int msInWeek);
184     virtual bool reportWwanZppFix(LocGpsLocation &zppLoc);
185     virtual bool reportZppBestAvailableFix(LocGpsLocation &zppLoc,
186             GpsLocationExtended &location_extended, LocPosTechMask tech_mask);
187     virtual void reportGnssSvIdConfigEvent(const GnssSvIdConfig& config);
188     virtual void reportGnssSvTypeConfigEvent(const GnssSvTypeConfig& config);
189     virtual bool requestOdcpiEvent(OdcpiRequestInfo& request);
190     virtual bool reportGnssEngEnergyConsumedEvent(uint64_t energyConsumedSinceFirstBoot);
191     virtual bool reportDeleteAidingDataEvent(GnssAidingData &aidingData);
192     virtual bool reportKlobucharIonoModelEvent(GnssKlobucharIonoModel& ionoModel);
193     virtual bool reportGnssAdditionalSystemInfoEvent(
194             GnssAdditionalSystemInfo& additionalSystemInfo);
195     virtual void reportNfwNotificationEvent(GnssNfwNotification& notification);
196 
197     virtual void geofenceBreachEvent(size_t count, uint32_t* hwIds, Location& location,
198                                      GeofenceBreachType breachType, uint64_t timestamp);
199     virtual void geofenceStatusEvent(GeofenceStatusAvailable available);
200 
201     virtual void reportPositionEvent(UlpLocation &location,
202                                      GpsLocationExtended &locationExtended,
203                                      enum loc_sess_status status,
204                                      LocPosTechMask loc_technology_mask);
205 
206     virtual void reportLocationsEvent(const Location* locations, size_t count,
207             BatchingMode batchingMode);
208     virtual void reportCompletedTripsEvent(uint32_t accumulated_distance);
209     virtual void reportBatchStatusChangeEvent(BatchingStatus batchStatus);
210 
211     /* ==== CLIENT ========================================================================= */
212     /* ======== COMMANDS ====(Called from Client Thread)==================================== */
213     void addClientCommand(LocationAPI* client, const LocationCallbacks& callbacks);
214     void removeClientCommand(LocationAPI* client,
215                              removeClientCompleteCallback rmClientCb);
216     void requestCapabilitiesCommand(LocationAPI* client);
217 
218 };
219 
220 } // namespace loc_core
221 
222 #endif //LOC_API_ADAPTER_BASE_H
223