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                                      bool fromEngineHub = false,
155                                      GnssDataNotification* pDataNotify = nullptr,
156                                      int msInWeek = -1);
157     virtual void reportSvEvent(const GnssSvNotification& svNotify,
158                                bool fromEngineHub=false);
159     virtual void reportDataEvent(const GnssDataNotification& dataNotify, int msInWeek);
160     virtual void reportNmeaEvent(const char* nmea, size_t length);
161     virtual void reportSvPolynomialEvent(GnssSvPolynomial &svPolynomial);
162     virtual void reportSvEphemerisEvent(GnssSvEphemerisReport &svEphemeris);
163     virtual void reportStatus(LocGpsStatusValue status);
164     virtual bool reportXtraServer(const char* url1, const char* url2,
165                                   const char* url3, const int maxlength);
166     virtual void reportLocationSystemInfoEvent(const LocationSystemInfo& locationSystemInfo);
167 
168     virtual bool requestXtraData();
169     virtual bool requestTime();
170     virtual bool requestLocation();
171     virtual bool requestATL(int connHandle, LocAGpsType agps_type,
172                             LocApnTypeMask apn_type_mask);
173     virtual bool releaseATL(int connHandle);
174     virtual bool requestNiNotifyEvent(const GnssNiNotification &notify, const void* data);
isInSession()175     inline virtual bool isInSession() { return false; }
getContext()176     ContextBase* getContext() const { return mContext; }
177     virtual void reportGnssMeasurementsEvent(const GnssMeasurements& gnssMeasurements,
178                                                 int msInWeek);
179     virtual bool reportWwanZppFix(LocGpsLocation &zppLoc);
180     virtual bool reportZppBestAvailableFix(LocGpsLocation &zppLoc,
181             GpsLocationExtended &location_extended, LocPosTechMask tech_mask);
182     virtual void reportGnssSvIdConfigEvent(const GnssSvIdConfig& config);
183     virtual void reportGnssSvTypeConfigEvent(const GnssSvTypeConfig& config);
184     virtual bool requestOdcpiEvent(OdcpiRequestInfo& request);
185     virtual bool reportGnssEngEnergyConsumedEvent(uint64_t energyConsumedSinceFirstBoot);
186     virtual bool reportDeleteAidingDataEvent(GnssAidingData &aidingData);
187     virtual bool reportKlobucharIonoModelEvent(GnssKlobucharIonoModel& ionoModel);
188     virtual bool reportGnssAdditionalSystemInfoEvent(
189             GnssAdditionalSystemInfo& additionalSystemInfo);
190     virtual void reportNfwNotificationEvent(GnssNfwNotification& notification);
191 
192     virtual void geofenceBreachEvent(size_t count, uint32_t* hwIds, Location& location,
193                                      GeofenceBreachType breachType, uint64_t timestamp);
194     virtual void geofenceStatusEvent(GeofenceStatusAvailable available);
195 
196     virtual void reportPositionEvent(UlpLocation &location,
197                                      GpsLocationExtended &locationExtended,
198                                      enum loc_sess_status status,
199                                      LocPosTechMask loc_technology_mask);
200 
201     virtual void reportLocationsEvent(const Location* locations, size_t count,
202             BatchingMode batchingMode);
203     virtual void reportCompletedTripsEvent(uint32_t accumulated_distance);
204     virtual void reportBatchStatusChangeEvent(BatchingStatus batchStatus);
205 
206     /* ==== CLIENT ========================================================================= */
207     /* ======== COMMANDS ====(Called from Client Thread)==================================== */
208     void addClientCommand(LocationAPI* client, const LocationCallbacks& callbacks);
209     void removeClientCommand(LocationAPI* client,
210                              removeClientCompleteCallback rmClientCb);
211     void requestCapabilitiesCommand(LocationAPI* client);
212 
213 };
214 
215 } // namespace loc_core
216 
217 #endif //LOC_API_ADAPTER_BASE_H
218