1 /* Copyright (c) 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 __SYSTEM_STATUS__
30 #define __SYSTEM_STATUS__
31 
32 #include <stdint.h>
33 #include <vector>
34 #include <gps_extended_c.h>
35 
36 #define GPS_MIN  (1)   //1-32
37 #define SBAS_MIN (33)
38 #define GLO_MIN  (65)  //65-88
39 #define QZSS_MIN (193) //193-197
40 #define BDS_MIN  (201) //201-237
41 #define GAL_MIN  (301) //301-336
42 
43 #define GPS_NUM  (32)
44 #define SBAS_NUM (32)
45 #define GLO_NUM  (24)
46 #define QZSS_NUM (5)
47 #define BDS_NUM  (37)
48 #define GAL_NUM  (36)
49 #define SV_ALL_NUM  (GPS_NUM+GLO_NUM+QZSS_NUM+BDS_NUM+GAL_NUM) //=134
50 
51 namespace loc_core
52 {
53 
54 /******************************************************************************
55  SystemStatus report data structure
56 ******************************************************************************/
57 class SystemStatusItemBase
58 {
59 public:
60     timespec mUtcTime;     // UTC timestamp when this info was last updated
61     timespec mUtcReported; // UTC timestamp when this info was reported
62 
SystemStatusItemBase()63     SystemStatusItemBase() {
64         timeval tv;
65         gettimeofday(&tv, NULL);
66         mUtcTime.tv_sec  = tv.tv_sec;
67         mUtcTime.tv_nsec = tv.tv_usec *1000ULL;
68         mUtcReported = mUtcTime;
69     };
~SystemStatusItemBase()70     virtual ~SystemStatusItemBase() { };
dump(void)71     virtual void dump(void) { };
72 };
73 
74 class SystemStatusLocation : public SystemStatusItemBase
75 {
76 public:
77     bool mValid;
78     UlpLocation mLocation;
79     GpsLocationExtended mLocationEx;
SystemStatusLocation()80     inline SystemStatusLocation() :
81         mValid(false) {}
SystemStatusLocation(const UlpLocation & location,const GpsLocationExtended & locationEx)82     inline SystemStatusLocation(const UlpLocation& location,
83                          const GpsLocationExtended& locationEx) :
84         mValid(true),
85         mLocation(location),
86         mLocationEx(locationEx) { }
87     bool equals(SystemStatusLocation& peer);
88     void dump(void);
89 };
90 
91 class SystemStatusPQWM1;
92 class SystemStatusTimeAndClock : public SystemStatusItemBase
93 {
94 public:
95     uint16_t mGpsWeek;
96     uint32_t mGpsTowMs;
97     uint8_t  mTimeValid;
98     uint8_t  mTimeSource;
99     int32_t  mTimeUnc;
100     int32_t  mClockFreqBias;
101     int32_t  mClockFreqBiasUnc;
102     int32_t  mLeapSeconds;
103     int32_t  mLeapSecUnc;
SystemStatusTimeAndClock()104     inline SystemStatusTimeAndClock() :
105         mGpsWeek(0),
106         mGpsTowMs(0),
107         mTimeValid(0),
108         mTimeSource(0),
109         mTimeUnc(0),
110         mClockFreqBias(0),
111         mClockFreqBiasUnc(0),
112         mLeapSeconds(0),
113         mLeapSecUnc(0) {}
114     inline SystemStatusTimeAndClock(const SystemStatusPQWM1& nmea);
115     bool equals(SystemStatusTimeAndClock& peer);
116     void dump(void);
117 };
118 
119 class SystemStatusXoState : public SystemStatusItemBase
120 {
121 public:
122     uint8_t  mXoState;
SystemStatusXoState()123     inline SystemStatusXoState() :
124         mXoState(0) {}
125     inline SystemStatusXoState(const SystemStatusPQWM1& nmea);
126     bool equals(SystemStatusXoState& peer);
127     void dump(void);
128 };
129 
130 class SystemStatusRfAndParams : public SystemStatusItemBase
131 {
132 public:
133     int32_t  mPgaGain;
134     uint32_t mGpsBpAmpI;
135     uint32_t mGpsBpAmpQ;
136     uint32_t mAdcI;
137     uint32_t mAdcQ;
138     uint32_t mJammerGps;
139     uint32_t mJammerGlo;
140     uint32_t mJammerBds;
141     uint32_t mJammerGal;
142     double   mAgcGps;
143     double   mAgcGlo;
144     double   mAgcBds;
145     double   mAgcGal;
SystemStatusRfAndParams()146     inline SystemStatusRfAndParams() :
147         mPgaGain(0),
148         mGpsBpAmpI(0),
149         mGpsBpAmpQ(0),
150         mAdcI(0),
151         mAdcQ(0),
152         mJammerGps(0),
153         mJammerGlo(0),
154         mJammerBds(0),
155         mJammerGal(0),
156         mAgcGps(0),
157         mAgcGlo(0),
158         mAgcBds(0),
159         mAgcGal(0) {}
160     inline SystemStatusRfAndParams(const SystemStatusPQWM1& nmea);
161     bool equals(SystemStatusRfAndParams& peer);
162     void dump(void);
163 };
164 
165 class SystemStatusErrRecovery : public SystemStatusItemBase
166 {
167 public:
168     uint32_t mRecErrorRecovery;
SystemStatusErrRecovery()169     inline SystemStatusErrRecovery() :
170         mRecErrorRecovery(0) {};
171     inline SystemStatusErrRecovery(const SystemStatusPQWM1& nmea);
172     bool equals(SystemStatusErrRecovery& peer);
173     void dump(void);
174 };
175 
176 class SystemStatusPQWP1;
177 class SystemStatusInjectedPosition : public SystemStatusItemBase
178 {
179 public:
180     uint8_t  mEpiValidity;
181     float    mEpiLat;
182     float    mEpiLon;
183     float    mEpiAlt;
184     float    mEpiHepe;
185     float    mEpiAltUnc;
186     uint8_t  mEpiSrc;
SystemStatusInjectedPosition()187     inline SystemStatusInjectedPosition() :
188         mEpiValidity(0),
189         mEpiLat(0),
190         mEpiLon(0),
191         mEpiAlt(0),
192         mEpiHepe(0),
193         mEpiAltUnc(0),
194         mEpiSrc(0) {}
195     inline SystemStatusInjectedPosition(const SystemStatusPQWP1& nmea);
196     bool equals(SystemStatusInjectedPosition& peer);
197     void dump(void);
198 };
199 
200 class SystemStatusPQWP2;
201 class SystemStatusBestPosition : public SystemStatusItemBase
202 {
203 public:
204     bool     mValid;
205     float    mBestLat;
206     float    mBestLon;
207     float    mBestAlt;
208     float    mBestHepe;
209     float    mBestAltUnc;
SystemStatusBestPosition()210     inline SystemStatusBestPosition() :
211         mValid(false),
212         mBestLat(0),
213         mBestLon(0),
214         mBestAlt(0),
215         mBestHepe(0),
216         mBestAltUnc(0) {}
217     inline SystemStatusBestPosition(const SystemStatusPQWP2& nmea);
218     bool equals(SystemStatusBestPosition& peer);
219     void dump(void);
220 };
221 
222 class SystemStatusPQWP3;
223 class SystemStatusXtra : public SystemStatusItemBase
224 {
225 public:
226     uint8_t   mXtraValidMask;
227     uint32_t  mGpsXtraAge;
228     uint32_t  mGloXtraAge;
229     uint32_t  mBdsXtraAge;
230     uint32_t  mGalXtraAge;
231     uint32_t  mQzssXtraAge;
232     uint32_t  mGpsXtraValid;
233     uint32_t  mGloXtraValid;
234     uint64_t  mBdsXtraValid;
235     uint64_t  mGalXtraValid;
236     uint8_t   mQzssXtraValid;
SystemStatusXtra()237     inline SystemStatusXtra() :
238         mXtraValidMask(0),
239         mGpsXtraAge(0),
240         mGloXtraAge(0),
241         mBdsXtraAge(0),
242         mGalXtraAge(0),
243         mQzssXtraAge(0),
244         mGpsXtraValid(0),
245         mGloXtraValid(0),
246         mBdsXtraValid(0ULL),
247         mGalXtraValid(0ULL),
248         mQzssXtraValid(0) {}
249     inline SystemStatusXtra(const SystemStatusPQWP3& nmea);
250     bool equals(SystemStatusXtra& peer);
251     void dump(void);
252 };
253 
254 class SystemStatusPQWP4;
255 class SystemStatusEphemeris : public SystemStatusItemBase
256 {
257 public:
258     uint32_t  mGpsEpheValid;
259     uint32_t  mGloEpheValid;
260     uint64_t  mBdsEpheValid;
261     uint64_t  mGalEpheValid;
262     uint8_t   mQzssEpheValid;
SystemStatusEphemeris()263     inline SystemStatusEphemeris() :
264         mGpsEpheValid(0),
265         mGloEpheValid(0),
266         mBdsEpheValid(0ULL),
267         mGalEpheValid(0ULL),
268         mQzssEpheValid(0) {}
269     inline SystemStatusEphemeris(const SystemStatusPQWP4& nmea);
270     bool equals(SystemStatusEphemeris& peer);
271     void dump(void);
272 };
273 
274 class SystemStatusPQWP5;
275 class SystemStatusSvHealth : public SystemStatusItemBase
276 {
277 public:
278     uint32_t  mGpsUnknownMask;
279     uint32_t  mGloUnknownMask;
280     uint64_t  mBdsUnknownMask;
281     uint64_t  mGalUnknownMask;
282     uint8_t   mQzssUnknownMask;
283     uint32_t  mGpsGoodMask;
284     uint32_t  mGloGoodMask;
285     uint64_t  mBdsGoodMask;
286     uint64_t  mGalGoodMask;
287     uint8_t   mQzssGoodMask;
288     uint32_t  mGpsBadMask;
289     uint32_t  mGloBadMask;
290     uint64_t  mBdsBadMask;
291     uint64_t  mGalBadMask;
292     uint8_t   mQzssBadMask;
SystemStatusSvHealth()293     inline SystemStatusSvHealth() :
294         mGpsUnknownMask(0),
295         mGloUnknownMask(0),
296         mBdsUnknownMask(0ULL),
297         mGalUnknownMask(0ULL),
298         mQzssUnknownMask(0),
299         mGpsGoodMask(0),
300         mGloGoodMask(0),
301         mBdsGoodMask(0ULL),
302         mGalGoodMask(0ULL),
303         mQzssGoodMask(0),
304         mGpsBadMask(0),
305         mGloBadMask(0),
306         mBdsBadMask(0ULL),
307         mGalBadMask(0ULL),
308         mQzssBadMask(0) {}
309     inline SystemStatusSvHealth(const SystemStatusPQWP5& nmea);
310     bool equals(SystemStatusSvHealth& peer);
311     void dump(void);
312 };
313 
314 class SystemStatusPQWP6;
315 class SystemStatusPdr : public SystemStatusItemBase
316 {
317 public:
318     uint32_t  mFixInfoMask;
SystemStatusPdr()319     inline SystemStatusPdr() :
320         mFixInfoMask(0) {}
321     inline SystemStatusPdr(const SystemStatusPQWP6& nmea);
322     bool equals(SystemStatusPdr& peer);
323     void dump(void);
324 };
325 
326 class SystemStatusPQWP7;
327 struct SystemStatusNav
328 {
329     GnssEphemerisType   mType;
330     GnssEphemerisSource mSource;
331     int32_t             mAgeSec;
332 };
333 
334 class SystemStatusNavData : public SystemStatusItemBase
335 {
336 public:
337     SystemStatusNav mNav[SV_ALL_NUM];
SystemStatusNavData()338     inline SystemStatusNavData() {
339         for (uint32_t i=0; i<SV_ALL_NUM; i++) {
340             mNav[i].mType = GNSS_EPH_TYPE_UNKNOWN;
341             mNav[i].mSource = GNSS_EPH_SOURCE_UNKNOWN;
342             mNav[i].mAgeSec = 0;
343         }
344     }
345     inline SystemStatusNavData(const SystemStatusPQWP7& nmea);
346     bool equals(SystemStatusNavData& peer);
347     void dump(void);
348 };
349 
350 class SystemStatusPQWS1;
351 class SystemStatusPositionFailure : public SystemStatusItemBase
352 {
353 public:
354     uint32_t  mFixInfoMask;
355     uint32_t  mHepeLimit;
SystemStatusPositionFailure()356     inline SystemStatusPositionFailure() :
357         mFixInfoMask(0),
358         mHepeLimit(0) {}
359     inline SystemStatusPositionFailure(const SystemStatusPQWS1& nmea);
360     bool equals(SystemStatusPositionFailure& peer);
361     void dump(void);
362 };
363 
364 /******************************************************************************
365  SystemStatusReports
366 ******************************************************************************/
367 class SystemStatusReports
368 {
369 public:
370     std::vector<SystemStatusLocation>         mLocation;
371 
372     std::vector<SystemStatusTimeAndClock>     mTimeAndClock;
373     std::vector<SystemStatusXoState>          mXoState;
374     std::vector<SystemStatusRfAndParams>      mRfAndParams;
375     std::vector<SystemStatusErrRecovery>      mErrRecovery;
376 
377     std::vector<SystemStatusInjectedPosition> mInjectedPosition;
378     std::vector<SystemStatusBestPosition>     mBestPosition;
379     std::vector<SystemStatusXtra>             mXtra;
380     std::vector<SystemStatusEphemeris>        mEphemeris;
381     std::vector<SystemStatusSvHealth>         mSvHealth;
382     std::vector<SystemStatusPdr>              mPdr;
383     std::vector<SystemStatusNavData>          mNavData;
384 
385     std::vector<SystemStatusPositionFailure>  mPositionFailure;
386 };
387 
388 /******************************************************************************
389  SystemStatus
390 ******************************************************************************/
391 class SystemStatus
392 {
393     static pthread_mutex_t mMutexSystemStatus;
394 
395     static const uint32_t                     maxLocation = 5;
396 
397     static const uint32_t                     maxTimeAndClock = 5;
398     static const uint32_t                     maxXoState = 5;
399     static const uint32_t                     maxRfAndParams = 5;
400     static const uint32_t                     maxErrRecovery = 5;
401 
402     static const uint32_t                     maxInjectedPosition = 5;
403     static const uint32_t                     maxBestPosition = 5;
404     static const uint32_t                     maxXtra = 5;
405     static const uint32_t                     maxEphemeris = 5;
406     static const uint32_t                     maxSvHealth = 5;
407     static const uint32_t                     maxPdr = 5;
408     static const uint32_t                     maxNavData = 5;
409 
410     static const uint32_t                     maxPositionFailure = 5;
411 
412     SystemStatusReports mCache;
413 
414     bool setLocation(const UlpLocation& location);
415 
416     bool setTimeAndCLock(const SystemStatusPQWM1& nmea);
417     bool setXoState(const SystemStatusPQWM1& nmea);
418     bool setRfAndParams(const SystemStatusPQWM1& nmea);
419     bool setErrRecovery(const SystemStatusPQWM1& nmea);
420 
421     bool setInjectedPosition(const SystemStatusPQWP1& nmea);
422     bool setBestPosition(const SystemStatusPQWP2& nmea);
423     bool setXtra(const SystemStatusPQWP3& nmea);
424     bool setEphemeris(const SystemStatusPQWP4& nmea);
425     bool setSvHealth(const SystemStatusPQWP5& nmea);
426     bool setPdr(const SystemStatusPQWP6& nmea);
427     bool setNavData(const SystemStatusPQWP7& nmea);
428 
429     bool setPositionFailure(const SystemStatusPQWS1& nmea);
430 
431 public:
432     SystemStatus();
~SystemStatus()433     ~SystemStatus() { }
434 
435     bool eventPosition(const UlpLocation& location,const GpsLocationExtended& locationEx);
436     bool setNmeaString(const char *data, uint32_t len);
437     bool getReport(SystemStatusReports& reports, bool isLatestonly = false) const;
438     bool setDefaultReport(void);
439 };
440 
441 } // namespace loc_core
442 
443 #endif //__SYSTEM_STATUS__
444 
445