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     AgnssStatusIpV4Cb     mFrameworkStatusV4Cb;
141 private:
142     /* AGPS Type for this state machine
143        LOC_AGPS_TYPE_ANY           0
144        LOC_AGPS_TYPE_SUPL          1
145        LOC_AGPS_TYPE_WWAN_ANY      3
146        LOC_AGPS_TYPE_SUPL_ES       5 */
147     AGpsExtType mAgpsType;
148     LocApnTypeMask mApnTypeMask;
149 
150     /* APN and IP Type info for AGPS Call */
151     char* mAPN;
152     unsigned int mAPNLen;
153     AGpsBearerType mBearer;
154 
155 public:
156     /* CONSTRUCTOR */
AgpsStateMachine(AgpsManager * agpsManager,AGpsExtType agpsType)157     AgpsStateMachine(AgpsManager* agpsManager, AGpsExtType agpsType):
158         mFrameworkStatusV4Cb(NULL),
159         mAgpsManager(agpsManager), mSubscriberList(),
160         mCurrentSubscriber(NULL), mState(AGPS_STATE_RELEASED),
161         mAgpsType(agpsType), mAPN(NULL), mAPNLen(0),
162         mBearer(AGPS_APN_BEARER_INVALID) {};
163 
~AgpsStateMachine()164     virtual ~AgpsStateMachine() { if(NULL != mAPN) delete[] mAPN; };
165 
166     /* Getter/Setter methods */
167     void setAPN(char* apn, unsigned int len);
getAPN()168     inline char* getAPN() const { return (char*)mAPN; }
getAPNLen()169     inline uint32_t getAPNLen() const { return mAPNLen; }
setBearer(AGpsBearerType bearer)170     inline void setBearer(AGpsBearerType bearer) { mBearer = bearer; }
getApnTypeMask()171     inline LocApnTypeMask getApnTypeMask() const { return mApnTypeMask; }
setApnTypeMask(LocApnTypeMask apnTypeMask)172     inline void setApnTypeMask(LocApnTypeMask apnTypeMask)
173     { mApnTypeMask = apnTypeMask; }
getBearer()174     inline AGpsBearerType getBearer() const { return mBearer; }
setType(AGpsExtType type)175     inline void setType(AGpsExtType type) { mAgpsType = type; }
getType()176     inline AGpsExtType getType() const { return mAgpsType; }
setCurrentSubscriber(AgpsSubscriber * subscriber)177     inline void setCurrentSubscriber(AgpsSubscriber* subscriber)
178     { mCurrentSubscriber = subscriber; }
179 
registerFrameworkStatusCallback(AgnssStatusIpV4Cb frameworkStatusV4Cb)180     inline void registerFrameworkStatusCallback(AgnssStatusIpV4Cb frameworkStatusV4Cb) {
181         mFrameworkStatusV4Cb = frameworkStatusV4Cb;
182     }
183 
184     /* Fetch subscriber with specified handle */
185     AgpsSubscriber* getSubscriber(int connHandle);
186 
187     /* Fetch first active or inactive subscriber in list
188      * isInactive = true : fetch first inactive subscriber
189      * isInactive = false : fetch first active subscriber */
190     AgpsSubscriber* getFirstSubscriber(bool isInactive);
191 
192     /* Process LOC AGPS Event being passed in
193      * onRsrcEvent */
194     virtual void processAgpsEvent(AgpsEvent event);
195 
196     /* Drop all subscribers, in case of Modem SSR */
197     void dropAllSubscribers();
198 
199 protected:
200     /* Remove the specified subscriber from list if present.
201      * Also delete the subscriber instance. */
202     void deleteSubscriber(AgpsSubscriber* subscriber);
203 
204 private:
205     /* Send call setup request to framework
206      * sendRsrcRequest(LOC_GPS_REQUEST_AGPS_DATA_CONN)
207      * sendRsrcRequest(LOC_GPS_RELEASE_AGPS_DATA_CONN) */
208     void requestOrReleaseDataConn(bool request);
209 
210     /* Individual event processing methods */
211     void processAgpsEventSubscribe();
212     void processAgpsEventUnsubscribe();
213     void processAgpsEventGranted();
214     void processAgpsEventReleased();
215     void processAgpsEventDenied();
216 
217     /* Clone the passed in subscriber and add to the subscriber list
218      * if not already present */
219     void addSubscriber(AgpsSubscriber* subscriber);
220 
221     /* Notify subscribers about AGPS events */
222     void notifyAllSubscribers(
223             AgpsEvent event, bool deleteSubscriberPostNotify,
224             AgpsNotificationType notificationType);
225     virtual void notifyEventToSubscriber(
226             AgpsEvent event, AgpsSubscriber* subscriber,
227             bool deleteSubscriberPostNotify);
228 
229     /* Do we have any subscribers in active state */
230     bool anyActiveSubscribers();
231 
232     /* Transition state */
233     void transitionState(AgpsState newState);
234 };
235 
236 /* LOC AGPS MANAGER */
237 class AgpsManager {
238 
239     friend class AgpsStateMachine;
240 public:
241     /* CONSTRUCTOR */
AgpsManager()242     AgpsManager():
243         mAtlOpenStatusCb(), mAtlCloseStatusCb(),
244         mAgnssNif(NULL), mInternetNif(NULL)/*, mDsNif(NULL)*/ {}
245 
246     /* Register callbacks */
registerATLCallbacks(AgpsAtlOpenStatusCb atlOpenStatusCb,AgpsAtlCloseStatusCb atlCloseStatusCb)247     inline void registerATLCallbacks(AgpsAtlOpenStatusCb  atlOpenStatusCb,
248             AgpsAtlCloseStatusCb                atlCloseStatusCb) {
249 
250         mAtlOpenStatusCb = atlOpenStatusCb;
251         mAtlCloseStatusCb = atlCloseStatusCb;
252     }
253 
254     /* Check if AGPS client is registered */
isRegistered()255     inline bool isRegistered() { return nullptr != mAgnssNif || nullptr != mInternetNif; }
256 
257     /* Create all AGPS state machines */
258     void createAgpsStateMachines(const AgpsCbInfo& cbInfo);
259 
260     /* Process incoming ATL requests */
261     void requestATL(int connHandle, AGpsExtType agpsType, LocApnTypeMask apnTypeMask);
262     void releaseATL(int connHandle);
263     /* Process incoming framework data call events */
264     void reportAtlOpenSuccess(AGpsExtType agpsType, char* apnName, int apnLen,
265             AGpsBearerType bearerType);
266     void reportAtlOpenFailed(AGpsExtType agpsType);
267     void reportAtlClosed(AGpsExtType agpsType);
268 
269     /* Handle Modem SSR */
270     void handleModemSSR();
271 
272 protected:
273 
274     AgpsAtlOpenStatusCb   mAtlOpenStatusCb;
275     AgpsAtlCloseStatusCb  mAtlCloseStatusCb;
276     AgpsStateMachine*   mAgnssNif;
277     AgpsStateMachine*   mInternetNif;
278 private:
279     /* Fetch state machine for handling request ATL call */
280     AgpsStateMachine* getAgpsStateMachine(AGpsExtType agpsType);
281 };
282 
283 /* Request SUPL/INTERNET/SUPL_ES ATL
284  * This LocMsg is defined in this header since it has to be used from more
285  * than one place, other Agps LocMsg are restricted to GnssAdapter and
286  * declared inline */
287 struct AgpsMsgRequestATL: public LocMsg {
288 
289     AgpsManager* mAgpsManager;
290     int mConnHandle;
291     AGpsExtType mAgpsType;
292     LocApnTypeMask mApnTypeMask;
293 
AgpsMsgRequestATLAgpsMsgRequestATL294     inline AgpsMsgRequestATL(AgpsManager* agpsManager, int connHandle,
295             AGpsExtType agpsType, LocApnTypeMask apnTypeMask) :
296             LocMsg(), mAgpsManager(agpsManager), mConnHandle(connHandle),
297             mAgpsType(agpsType), mApnTypeMask(apnTypeMask){
298 
299         LOC_LOGV("AgpsMsgRequestATL");
300     }
301 
procAgpsMsgRequestATL302     inline virtual void proc() const {
303 
304         LOC_LOGV("AgpsMsgRequestATL::proc()");
305         mAgpsManager->requestATL(mConnHandle, mAgpsType, mApnTypeMask);
306     }
307 };
308 
309 namespace AgpsUtils {
310 
311 AGpsBearerType ipTypeToBearerType(LocApnIpType ipType);
312 LocApnIpType bearerTypeToIpType(AGpsBearerType bearerType);
313 
314 }
315 
316 #endif /* AGPS_H */
317