summaryrefslogtreecommitdiffstats
path: root/gps/libloc_api_50001/loc_eng_agps.h
blob: a0873d04d1d8798d5850cdd779865f58d1b7ae84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/* Copyright (c) 2011,2012, Code Aurora Forum. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 *       copyright notice, this list of conditions and the following
 *       disclaimer in the documentation and/or other materials provided
 *       with the distribution.
 *     * Neither the name of Code Aurora Forum, Inc. nor the names of its
 *       contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

#ifndef __LOC_ENG_AGPS_H__
#define __LOC_ENG_AGPS_H__

#include <stdbool.h>
#include <ctype.h>
#include <string.h>
#include <arpa/inet.h>
#include <hardware/gps.h>
#include <linked_list.h>
#include <LocApiAdapter.h>
#include "loc_eng_msg.h"

// forward declaration
class AgpsStateMachine;
class Subscriber;

// NIF resource events
typedef enum {
    RSRC_SUBSCRIBE,
    RSRC_UNSUBSCRIBE,
    RSRC_GRANTED,
    RSRC_RELEASED,
    RSRC_DENIED,
    RSRC_STATUS_MAX
} AgpsRsrcStatus;

// information bundle for subscribers
struct Notification {
    // goes to every subscriber
    static const int BROADCAST_ALL;
    // goes to every ACTIVE subscriber
    static const int BROADCAST_ACTIVE;
    // goes to every INACTIVE subscriber
    static const int BROADCAST_INACTIVE;

    // go to a specific subscriber
    const Subscriber* rcver;
    // broadcast
    const int groupID;
    // the new resource status event
    const AgpsRsrcStatus rsrcStatus;
    // should the subscriber be deleted after the notification
    const bool postNotifyDelete;

    // convenient constructor
    inline Notification(const int broadcast,
                        const AgpsRsrcStatus status,
                        const bool deleteAfterwards) :
        rcver(NULL), groupID(broadcast), rsrcStatus(status),
        postNotifyDelete(deleteAfterwards) {}

    // convenient constructor
    inline Notification(const Subscriber* subscriber,
                        const AgpsRsrcStatus status,
                        const bool deleteAfterwards) :
        rcver(subscriber), groupID(-1), rsrcStatus(status),
        postNotifyDelete(deleteAfterwards) {}

    // convenient constructor
    inline Notification(const int broadcast) :
        rcver(NULL), groupID(broadcast), rsrcStatus(RSRC_STATUS_MAX),
        postNotifyDelete(false) {}

    // convenient constructor
    inline Notification(const Subscriber* subscriber) :
        rcver(subscriber), groupID(-1), rsrcStatus(RSRC_STATUS_MAX),
        postNotifyDelete(false) {}
};

class AgpsState {
    // allows AgpsStateMachine to access private data
    // no class members are public.  We don't want
    // anyone but state machine to use state.
    friend class AgpsStateMachine;

    // state transitions are done here.
    // Each state implements its own transitions (of course).
    inline virtual AgpsState* onRsrcEvent(AgpsRsrcStatus event, void* data) = 0;

protected:
    // handle back to state machine
    const AgpsStateMachine* mStateMachine;
    // each state has pointers to all 3 states
    // one of which is to itself.
    AgpsState* mReleasedState;
    AgpsState* mAcquiredState;
    AgpsState* mPendingState;
    AgpsState* mReleasingState;

    inline AgpsState(const AgpsStateMachine *stateMachine) :
        mStateMachine(stateMachine),
        mReleasedState(NULL),
        mAcquiredState(NULL),
        mPendingState(NULL),
        mReleasingState(NULL) {}
    virtual ~AgpsState() {}

public:
    // for logging purpose
    inline virtual char* whoami() = 0;
};

class AgpsStateMachine {
    // allows AgpsState to access private data
    // each state is really internal data to the
    // state machine, so it should be able to
    // access anything within the state machine.
    friend class AgpsState;

    // handle to whoever provides the service
    void (* const mServicer)(AGpsStatus* status);
    // NIF type: AGNSS or INTERNET.
    const AGpsType mType;
    // pointer to the current state.
    AgpsState* mStatePtr;
    // a linked list of subscribers.
    void* mSubscribers;
    // apn to the NIF.  Each state machine tracks
    // resource state of a particular NIF.  For each
    // NIF, there is also an active APN.
    char* mAPN;
    // for convenience, we don't do strlen each time.
    unsigned int mAPNLen;
    // bear
    AGpsBearerType mBearer;
    // ipv4 address for routing
    bool mEnforceSingleSubscriber;

public:
    AgpsStateMachine(void (*servicer)(AGpsStatus* status), AGpsType type, bool enforceSingleSubscriber);
    virtual ~AgpsStateMachine();

    // self explanatory methods below
    void setAPN(const char* apn, unsigned int len);
    inline const char* getAPN() const { return (const char*)mAPN; }
    inline void setBearer(AGpsBearerType bearer) { mBearer = bearer; }
    inline AGpsBearerType getBearer() const { return mBearer; }
    inline AGpsType getType() const { return (AGpsType)mType; }

    // someone, a ATL client or BIT, is asking for NIF
    void subscribeRsrc(Subscriber *subscriber);

    // someone, a ATL client or BIT, is done with NIF
    bool unsubscribeRsrc(Subscriber *subscriber);

    // add a subscriber in the linked list, if not already there.
    void addSubscriber(Subscriber* subscriber) const;

    void onRsrcEvent(AgpsRsrcStatus event);

    // put the data together and send the FW
    void sendRsrcRequest(AGpsStatusValue action) const;

    inline bool hasSubscribers() const
    { return !linked_list_empty(mSubscribers); }

    bool hasActiveSubscribers() const;

    inline void dropAllSubscribers() const
    { linked_list_flush(mSubscribers); }

    // private. Only a state gets to call this.
    void notifySubscribers(Notification& notification) const;
};

// each subscriber is a AGPS client.  In the case of ATL, there could be
// multiple clients from modem.  In the case of BIT, there is only one
// cilent from BIT daemon.
struct Subscriber {
    const int ID;
    const AgpsStateMachine* mStateMachine;
    inline Subscriber(const int id,
                      const AgpsStateMachine* stateMachine) :
        ID(id), mStateMachine(stateMachine) {}
    inline virtual ~Subscriber() {}

    virtual void setIPAddresses(int &v4, char* v6) = 0;
    inline virtual void setWifiInfo(char* ssid, char* password) {}

    inline virtual bool equals(const Subscriber *s) const
    { return ID == s->ID; }

    // notifies a subscriber a new NIF resource status, usually
    // either GRANTE, DENIED, or RELEASED
    virtual bool notifyRsrcStatus(Notification &notification) = 0;

    virtual bool waitForCloseComplete() { return false; }
    virtual void setInactive() {}
    virtual bool isInactive() { return false; }

    virtual Subscriber* clone() = 0;
    // checks if this notification is for me, i.e.
    // either has my id, or has a broadcast id.
    bool forMe(Notification &notification);
};

// BITSubscriber, created with requests from BIT daemon
struct BITSubscriber : public Subscriber {
    inline BITSubscriber(const AgpsStateMachine* stateMachine,
                         unsigned int ipv4, char* ipv6) :
        Subscriber(ipv4, stateMachine)
    {
        if (NULL == ipv6) {
            ipv6Addr[0] = NULL;
        } else {
            memcpy(ipv6Addr, ipv6, sizeof(ipv6Addr));
        }
    }

    virtual bool notifyRsrcStatus(Notification &notification);

    inline virtual void setIPAddresses(int &v4, char* v6)
    { v4 = ID; memcpy(v6, ipv6Addr, sizeof(ipv6Addr)); }

    virtual Subscriber* clone()
    {
        return new BITSubscriber(mStateMachine, ID, ipv6Addr);
    }

    virtual bool equals(const Subscriber *s) const;

private:
    char ipv6Addr[16];
};

// ATLSubscriber, created with requests from ATL
struct ATLSubscriber : public Subscriber {
    const LocApiAdapter* mLocAdapter;
    const bool mBackwardCompatibleMode;
    inline ATLSubscriber(const int id,
                         const AgpsStateMachine* stateMachine,
                         const LocApiAdapter* adapter,
                         const bool compatibleMode) :
        Subscriber(id, stateMachine), mLocAdapter(adapter),
        mBackwardCompatibleMode(compatibleMode){}
    virtual bool notifyRsrcStatus(Notification &notification);

    inline virtual void setIPAddresses(int &v4, char* v6)
    { v4 = INADDR_NONE; v6[0] = 0; }

    inline virtual Subscriber* clone()
    {
        return new ATLSubscriber(ID, mStateMachine, mLocAdapter,
                                 mBackwardCompatibleMode);
    }
};

// WIFISubscriber, created with requests from MSAPM or QuIPC
struct WIFISubscriber : public Subscriber {
    char * mSSID;
    char * mPassword;
    loc_if_req_sender_id_e_type senderId;
    bool mIsInactive;
    inline WIFISubscriber(const AgpsStateMachine* stateMachine,
                         char * ssid, char * password, loc_if_req_sender_id_e_type sender_id) :
        Subscriber(sender_id, stateMachine),
        mSSID(NULL == ssid ? NULL : new char[SSID_BUF_SIZE]),
        mPassword(NULL == password ? NULL : new char[SSID_BUF_SIZE]),
        senderId(sender_id)
    {
      if (NULL != mSSID)
          strlcpy(mSSID, ssid, SSID_BUF_SIZE);
      if (NULL != mPassword)
          strlcpy(mPassword, password, SSID_BUF_SIZE);
      mIsInactive = false;
    }

    virtual bool notifyRsrcStatus(Notification &notification);

    inline virtual void setIPAddresses(int &v4, char* v6) {}

    inline virtual void setWifiInfo(char* ssid, char* password)
    {
      if (NULL != mSSID)
          strlcpy(ssid, mSSID, SSID_BUF_SIZE);
      else
          ssid[0] = '\0';
      if (NULL != mPassword)
          strlcpy(password, mPassword, SSID_BUF_SIZE);
      else
          password[0] = '\0';
    }

    inline virtual bool waitForCloseComplete() { return true; }

    inline virtual void setInactive() { mIsInactive = true; }
    inline virtual bool isInactive() { return mIsInactive; }

    virtual Subscriber* clone()
    {
        return new WIFISubscriber(mStateMachine, mSSID, mPassword, senderId);
    }
};

#endif //__LOC_ENG_AGPS_H__