1 /* Copyright (c) 2012-2018, 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 
30 #ifndef AGPS_H
31 #define AGPS_H
32 
33 #include <functional>
34 #include <list>
35 #include <MsgTask.h>
36 #include <gps_extended_c.h>
37 #include <loc_pla.h>
38 #include <log_util.h>
39 
40 /* ATL callback function pointers
41  * Passed in by Adapter to AgpsManager */
42 typedef std::function<void(
43         int handle, int isSuccess, char* apn, uint32_t apnLen,
44         AGpsBearerType bearerType, AGpsExtType agpsType,
45         LocApnTypeMask mask)> AgpsAtlOpenStatusCb;
46 
47 typedef std::function<void(int handle, int isSuccess)> AgpsAtlCloseStatusCb;
48 
49 /* Post message to adapter's message queue */
50 typedef std::function<void(LocMsg* msg)>     SendMsgToAdapterMsgQueueFn;
51 
52 /* AGPS States */
53 typedef enum {
54     AGPS_STATE_INVALID = 0,
55     AGPS_STATE_RELEASED,
56     AGPS_STATE_PENDING,
57     AGPS_STATE_ACQUIRED,
58     AGPS_STATE_RELEASING
59 } AgpsState;
60 
61 typedef enum {
62     AGPS_EVENT_INVALID = 0,
63     AGPS_EVENT_SUBSCRIBE,
64     AGPS_EVENT_UNSUBSCRIBE,
65     AGPS_EVENT_GRANTED,
66     AGPS_EVENT_RELEASED,
67     AGPS_EVENT_DENIED
68 } AgpsEvent;
69 
70 /* Notification Types sent to subscribers */
71 typedef enum {
72     AGPS_NOTIFICATION_TYPE_INVALID = 0,
73 
74     /* Meant for all subscribers, either active or inactive */
75     AGPS_NOTIFICATION_TYPE_FOR_ALL_SUBSCRIBERS,
76 
77     /* Meant for only inactive subscribers */
78     AGPS_NOTIFICATION_TYPE_FOR_INACTIVE_SUBSCRIBERS,
79 
80     /* Meant for only active subscribers */
81     AGPS_NOTIFICATION_TYPE_FOR_ACTIVE_SUBSCRIBERS
82 } AgpsNotificationType;
83 
84 /* Classes in this header */
85 class AgpsSubscriber;
86 class AgpsManager;
87 class AgpsStateMachine;
88 
89 /* SUBSCRIBER
90  * Each Subscriber instance corresponds to one AGPS request,
91  * received by the AGPS state machine */
92 class AgpsSubscriber {
93 
94 public:
95     int mConnHandle;
96 
97     /* Does this subscriber wait for data call close complete,
98      * before being notified ATL close ?
99      * While waiting for data call close, subscriber will be in
100      * inactive state. */
101     bool mWaitForCloseComplete;
102     bool mIsInactive;
103     LocApnTypeMask mApnTypeMask;
104 
AgpsSubscriber(int connHandle,bool waitForCloseComplete,bool isInactive,LocApnTypeMask apnTypeMask)105     inline AgpsSubscriber(
106             int connHandle, bool waitForCloseComplete, bool isInactive,
107             LocApnTypeMask apnTypeMask) :
108             mConnHandle(connHandle),
109             mWaitForCloseComplete(waitForCloseComplete),
110             mIsInactive(isInactive),
111             mApnTypeMask(apnTypeMask) {}
~AgpsSubscriber()112     inline virtual ~AgpsSubscriber() {}
113 
equals(const AgpsSubscriber * s)114     inline virtual bool equals(const AgpsSubscriber *s) const
115     { return (mConnHandle == s->mConnHandle); }
116 
clone()117     inline virtual AgpsSubscriber* clone()
118     { return new AgpsSubscriber(
119             mConnHandle, mWaitForCloseComplete, mIsInactive, mApnTypeMask); }
120 };
121 
122 /* AGPS STATE MACHINE */
123 class AgpsStateMachine {
124 protected:
125     /* AGPS Manager instance, from where this state machine is created */
126     AgpsManager* mAgpsManager;
127 
128     /* List of all subscribers for this State Machine.
129      * Once a subscriber is notified for ATL open/close status,
130      * it is deleted */
131     std::list<AgpsSubscriber*> mSubscriberList;
132 
133     /* Current subscriber, whose request this State Machine is
134      * currently processing */
135     AgpsSubscriber* mCurrentSubscriber;
136 
137     /* Current state for this state machine */
138     AgpsState mState;
139 
140 private:
141     /* AGPS Type for this state machine
142        LOC_AGPS_TYPE_ANY           0
143        LOC_AGPS_TYPE_SUPL          1
144        LOC_AGPS_TYPE_WWAN_ANY      3
145        LOC_AGPS_TYPE_SUPL_ES       5 */
146     AGpsExtType mAgpsType;
147     LocApnTypeMask mApnTypeMask;
148 
149     /* APN and IP Type info for AGPS Call */
150     char* mAPN;
151     unsigned int mAPNLen;
152     AGpsBearerType mBearer;
153 
154 public:
155     /* CONSTRUCTOR */
AgpsStateMachine(AgpsManager * agpsManager,AGpsExtType agpsType)156     AgpsStateMachine(AgpsManager* agpsManager, AGpsExtType agpsType):
157         mAgpsManager(agpsManager), mSubscriberList(),
158         mCurrentSubscriber(NULL), mState(AGPS_STATE_RELEASED),
159         mAgpsType(agpsType), mAPN(NULL), mAPNLen(0),
160         mBearer(AGPS_APN_BEARER_INVALID) {};
161 
~AgpsStateMachine()162     virtual ~AgpsStateMachine() { if(NULL != mAPN) delete[] mAPN; };
163 
164     /* Getter/Setter methods */
165     void setAPN(char* apn, unsigned int len);
getAPN()166     inline char* getAPN() const { return (char*)mAPN; }
getAPNLen()167     inline uint32_t getAPNLen() const { return mAPNLen; }
setBearer(AGpsBearerType bearer)168     inline void setBearer(AGpsBearerType bearer) { mBearer = bearer; }
getApnTypeMask()169     inline LocApnTypeMask getApnTypeMask() const { return mApnTypeMask; }
setApnTypeMask(LocApnTypeMask apnTypeMask)170     inline void setApnTypeMask(LocApnTypeMask apnTypeMask)
171     { mApnTypeMask = apnTypeMask; }
getBearer()172     inline AGpsBearerType getBearer() const { return mBearer; }
setType(AGpsExtType type)173     inline void setType(AGpsExtType type) { mAgpsType = type; }
getType()174     inline AGpsExtType getType() const { return mAgpsType; }
setCurrentSubscriber(AgpsSubscriber * subscriber)175     inline void setCurrentSubscriber(AgpsSubscriber* subscriber)
176     { mCurrentSubscriber = subscriber; }
177 
178     /* Fetch subscriber with specified handle */
179     AgpsSubscriber* getSubscriber(int connHandle);
180 
181     /* Fetch first active or inactive subscriber in list
182      * isInactive = true : fetch first inactive subscriber
183      * isInactive = false : fetch first active subscriber */
184     AgpsSubscriber* getFirstSubscriber(bool isInactive);
185 
186     /* Process LOC AGPS Event being passed in
187      * onRsrcEvent */
188     virtual void processAgpsEvent(AgpsEvent event);
189 
190     /* Drop all subscribers, in case of Modem SSR */
191     void dropAllSubscribers();
192 
193 protected:
194     /* Remove the specified subscriber from list if present.
195      * Also delete the subscriber instance. */
196     void deleteSubscriber(AgpsSubscriber* subscriber);
197 
198 private:
199     /* Send call setup request to framework
200      * sendRsrcRequest(LOC_GPS_REQUEST_AGPS_DATA_CONN)
201      * sendRsrcRequest(LOC_GPS_RELEASE_AGPS_DATA_CONN) */
202     void requestOrReleaseDataConn(bool request);
203 
204     /* Individual event processing methods */
205     void processAgpsEventSubscribe();
206     void processAgpsEventUnsubscribe();
207     void processAgpsEventGranted();
208     void processAgpsEventReleased();
209     void processAgpsEventDenied();
210 
211     /* Clone the passed in subscriber and add to the subscriber list
212      * if not already present */
213     void addSubscriber(AgpsSubscriber* subscriber);
214 
215     /* Notify subscribers about AGPS events */
216     void notifyAllSubscribers(
217             AgpsEvent event, bool deleteSubscriberPostNotify,
218             AgpsNotificationType notificationType);
219     virtual void notifyEventToSubscriber(
220             AgpsEvent event, AgpsSubscriber* subscriber,
221             bool deleteSubscriberPostNotify);
222 
223     /* Do we have any subscribers in active state */
224     bool anyActiveSubscribers();
225 
226     /* Transition state */
227     void transitionState(AgpsState newState);
228 };
229 
230 /* LOC AGPS MANAGER */
231 class AgpsManager {
232 
233     friend class AgpsStateMachine;
234 public:
235     /* CONSTRUCTOR */
AgpsManager()236     AgpsManager():
237         mFrameworkStatusV4Cb(NULL),
238         mAtlOpenStatusCb(), mAtlCloseStatusCb(),
239         mAgnssNif(NULL), mInternetNif(NULL)/*, mDsNif(NULL)*/ {}
240 
241     /* Register callbacks */
registerATLCallbacks(AgpsAtlOpenStatusCb atlOpenStatusCb,AgpsAtlCloseStatusCb atlCloseStatusCb)242     inline void registerATLCallbacks(AgpsAtlOpenStatusCb  atlOpenStatusCb,
243             AgpsAtlCloseStatusCb                atlCloseStatusCb) {
244 
245         mAtlOpenStatusCb = atlOpenStatusCb;
246         mAtlCloseStatusCb = atlCloseStatusCb;
247     }
248 
registerFrameworkStatusCallback(AgnssStatusIpV4Cb frameworkStatusV4Cb)249     inline void registerFrameworkStatusCallback(AgnssStatusIpV4Cb frameworkStatusV4Cb) {
250         mFrameworkStatusV4Cb = frameworkStatusV4Cb;
251     }
252 
253     /* Create all AGPS state machines */
254     void createAgpsStateMachines();
255 
256     /* Process incoming ATL requests */
257     void requestATL(int connHandle, AGpsExtType agpsType, LocApnTypeMask apnTypeMask);
258     void releaseATL(int connHandle);
259     /* Process incoming framework data call events */
260     void reportAtlOpenSuccess(AGpsExtType agpsType, char* apnName, int apnLen,
261             AGpsBearerType bearerType);
262     void reportAtlOpenFailed(AGpsExtType agpsType);
263     void reportAtlClosed(AGpsExtType agpsType);
264 
265     /* Handle Modem SSR */
266     void handleModemSSR();
267 
268 protected:
269     AgnssStatusIpV4Cb     mFrameworkStatusV4Cb;
270 
271     AgpsAtlOpenStatusCb   mAtlOpenStatusCb;
272     AgpsAtlCloseStatusCb  mAtlCloseStatusCb;
273     AgpsStateMachine*   mAgnssNif;
274     AgpsStateMachine*   mInternetNif;
275 private:
276     /* Fetch state machine for handling request ATL call */
277     AgpsStateMachine* getAgpsStateMachine(AGpsExtType agpsType);
278 };
279 
280 /* Request SUPL/INTERNET/SUPL_ES ATL
281  * This LocMsg is defined in this header since it has to be used from more
282  * than one place, other Agps LocMsg are restricted to GnssAdapter and
283  * declared inline */
284 struct AgpsMsgRequestATL: public LocMsg {
285 
286     AgpsManager* mAgpsManager;
287     int mConnHandle;
288     AGpsExtType mAgpsType;
289     LocApnTypeMask mApnTypeMask;
290 
AgpsMsgRequestATLAgpsMsgRequestATL291     inline AgpsMsgRequestATL(AgpsManager* agpsManager, int connHandle,
292             AGpsExtType agpsType, LocApnTypeMask apnTypeMask) :
293             LocMsg(), mAgpsManager(agpsManager), mConnHandle(connHandle),
294             mAgpsType(agpsType), mApnTypeMask(apnTypeMask){
295 
296         LOC_LOGV("AgpsMsgRequestATL");
297     }
298 
procAgpsMsgRequestATL299     inline virtual void proc() const {
300 
301         LOC_LOGV("AgpsMsgRequestATL::proc()");
302         mAgpsManager->requestATL(mConnHandle, mAgpsType, mApnTypeMask);
303     }
304 };
305 
306 namespace AgpsUtils {
307 
308 AGpsBearerType ipTypeToBearerType(LocApnIpType ipType);
309 LocApnIpType bearerTypeToIpType(AGpsBearerType bearerType);
310 
311 }
312 
313 #endif /* AGPS_H */
314