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