1 /* Copyright (c) 2011-2014, 2016-2017 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 <UlpProxyBase.h>
34 #include <ContextBase.h>
35 #include <LocationAPI.h>
36 #include <map>
37 
38 typedef struct LocationSessionKey {
39     LocationAPI* client;
40     uint32_t id;
LocationSessionKeyLocationSessionKey41     inline LocationSessionKey(LocationAPI* _client, uint32_t _id) :
42         client(_client), id(_id) {}
43 } LocationSessionKey;
44 inline bool operator <(LocationSessionKey const& left, LocationSessionKey const& right) {
45     return left.id < right.id || (left.id == right.id && left.client < right.client);
46 }
47 inline bool operator ==(LocationSessionKey const& left, LocationSessionKey const& right) {
48     return left.id == right.id && left.client == right.client;
49 }
50 inline bool operator !=(LocationSessionKey const& left, LocationSessionKey const& right) {
51     return left.id != right.id || left.client != right.client;
52 }
53 typedef std::map<LocationSessionKey, LocationOptions> LocationSessionMap;
54 
55 namespace loc_core {
56 
57 class LocAdapterProxyBase;
58 
59 class LocAdapterBase {
60 private:
61     static uint32_t mSessionIdCounter;
62 protected:
63     LOC_API_ADAPTER_EVENT_MASK_T mEvtMask;
64     ContextBase* mContext;
65     LocApiBase* mLocApi;
66     LocAdapterProxyBase* mLocAdapterProxyBase;
67     const MsgTask* mMsgTask;
LocAdapterBase(const MsgTask * msgTask)68     inline LocAdapterBase(const MsgTask* msgTask) :
69         mEvtMask(0), mContext(NULL), mLocApi(NULL),
70         mLocAdapterProxyBase(NULL), mMsgTask(msgTask) {}
71 public:
~LocAdapterBase()72     inline virtual ~LocAdapterBase() { mLocApi->removeAdapter(this); }
73     LocAdapterBase(const LOC_API_ADAPTER_EVENT_MASK_T mask,
74                    ContextBase* context, LocAdapterProxyBase *adapterProxyBase = NULL);
75     inline LOC_API_ADAPTER_EVENT_MASK_T
checkMask(LOC_API_ADAPTER_EVENT_MASK_T mask)76         checkMask(LOC_API_ADAPTER_EVENT_MASK_T mask) const {
77         return mEvtMask & mask;
78     }
79 
getEvtMask()80     inline LOC_API_ADAPTER_EVENT_MASK_T getEvtMask() const {
81         return mEvtMask;
82     }
83 
sendMsg(const LocMsg * msg)84     inline void sendMsg(const LocMsg* msg) const {
85         mMsgTask->sendMsg(msg);
86     }
87 
sendMsg(const LocMsg * msg)88     inline void sendMsg(const LocMsg* msg) {
89         mMsgTask->sendMsg(msg);
90     }
91 
updateEvtMask(LOC_API_ADAPTER_EVENT_MASK_T event,loc_registration_mask_status status)92     inline void updateEvtMask(LOC_API_ADAPTER_EVENT_MASK_T event,
93                               loc_registration_mask_status status)
94     {
95         switch(status) {
96             case (LOC_REGISTRATION_MASK_ENABLED):
97                 mEvtMask = mEvtMask | event;
98                 break;
99             case (LOC_REGISTRATION_MASK_DISABLED):
100                 mEvtMask = mEvtMask &~ event;
101                 break;
102             case (LOC_REGISTRATION_MASK_SET):
103                 mEvtMask = event;
104                 break;
105         }
106         mLocApi->updateEvtMask();
107     }
108 
isFeatureSupported(uint8_t featureVal)109     inline bool isFeatureSupported(uint8_t featureVal) {
110         return mLocApi->isFeatureSupported(featureVal);
111     }
112 
113     uint32_t generateSessionId();
114 
115     // This will be overridden by the individual adapters
116     // if necessary.
setUlpProxyCommand(UlpProxyBase * ulp)117     inline virtual void setUlpProxyCommand(UlpProxyBase* ulp) {
118 
119         (void)ulp;
120     }
121     virtual void handleEngineUpEvent();
122     virtual void handleEngineDownEvent();
setPositionModeCommand(LocPosMode & posMode)123     inline virtual void setPositionModeCommand(LocPosMode& posMode) {
124 
125         (void)posMode;
126     }
startTrackingCommand()127     virtual void startTrackingCommand() {}
stopTrackingCommand()128     virtual void stopTrackingCommand() {}
getZppCommand()129     virtual void getZppCommand() {}
130     virtual void reportPositionEvent(const UlpLocation& location,
131                                      const GpsLocationExtended& locationExtended,
132                                      enum loc_sess_status status,
133                                      LocPosTechMask loc_technology_mask,
134                                      bool fromUlp=false);
135     virtual void reportSvEvent(const GnssSvNotification& svNotify, bool fromUlp=false);
136     virtual void reportNmeaEvent(const char* nmea, size_t length, bool fromUlp=false);
137     virtual void reportSvMeasurementEvent(GnssSvMeasurementSet &svMeasurementSet);
138     virtual void reportSvPolynomialEvent(GnssSvPolynomial &svPolynomial);
139     virtual void reportStatus(LocGpsStatusValue status);
140     virtual bool reportXtraServer(const char* url1, const char* url2,
141                                   const char* url3, const int maxlength);
142     virtual bool requestXtraData();
143     virtual bool requestTime();
144     virtual bool requestLocation();
145     virtual bool requestATL(int connHandle, LocAGpsType agps_type);
146     virtual bool releaseATL(int connHandle);
147     virtual bool requestSuplES(int connHandle);
148     virtual bool reportDataCallOpened();
149     virtual bool reportDataCallClosed();
150     virtual bool requestNiNotifyEvent(const GnssNiNotification &notify, const void* data);
isInSession()151     inline virtual bool isInSession() { return false; }
getContext()152     ContextBase* getContext() const { return mContext; }
153     virtual void reportGnssMeasurementDataEvent(const GnssMeasurementsNotification& measurements,
154                                                 int msInWeek);
155     virtual bool reportWwanZppFix(LocGpsLocation &zppLoc);
156 };
157 
158 } // namespace loc_core
159 
160 #endif //LOC_API_ADAPTER_BASE_H
161