1 /* Copyright (c) 2017-2019, 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 <sys/time.h>
34 #include <vector>
35 #include <algorithm>
36 #include <iterator>
37 #include <loc_pla.h>
38 #include <log_util.h>
39 #include <MsgTask.h>
40 #include <IDataItemCore.h>
41 #include <IOsObserver.h>
42 #include <DataItemConcreteTypesBase.h>
43 #include <SystemStatusOsObserver.h>
44 
45 #include <gps_extended_c.h>
46 
47 #define GPS_MIN    (1)   //1-32
48 #define SBAS_MIN   (33)
49 #define GLO_MIN    (65)  //65-88
50 #define QZSS_MIN   (193) //193-197
51 #define BDS_MIN    (201) //201-237
52 #define GAL_MIN    (301) //301-336
53 #define NAVIC_MIN  (401) //401-414
54 
55 #define GPS_NUM     (32)
56 #define SBAS_NUM    (32)
57 #define GLO_NUM     (24)
58 #define QZSS_NUM    (5)
59 #define BDS_NUM     (37)
60 #define GAL_NUM     (36)
61 #define NAVIC_NUM   (14)
62 #define SV_ALL_NUM_MIN  (GPS_NUM + GLO_NUM + QZSS_NUM + BDS_NUM + GAL_NUM) //=134
63 #define SV_ALL_NUM      (SV_ALL_NUM_MIN + NAVIC_NUM) //=148
64 
65 namespace loc_core
66 {
67 
68 /******************************************************************************
69  SystemStatus report data structure
70 ******************************************************************************/
71 class SystemStatusItemBase
72 {
73 public:
74     timespec  mUtcTime;
75     timespec  mUtcReported;
76     static const uint32_t maxItem = 5;
77 
SystemStatusItemBase()78     SystemStatusItemBase() {
79         timeval tv;
80         gettimeofday(&tv, NULL);
81         mUtcTime.tv_sec  = tv.tv_sec;
82         mUtcTime.tv_nsec = tv.tv_usec*1000ULL;
83         mUtcReported = mUtcTime;
84     };
~SystemStatusItemBase()85     virtual ~SystemStatusItemBase() {};
collate(SystemStatusItemBase &)86     inline virtual SystemStatusItemBase& collate(SystemStatusItemBase&) {
87         return *this;
88     }
dump(void)89     virtual void dump(void) {};
ignore()90     inline virtual bool ignore() { return false; };
91 };
92 
93 class SystemStatusLocation : public SystemStatusItemBase
94 {
95 public:
96     bool mValid;
97     UlpLocation mLocation;
98     GpsLocationExtended mLocationEx;
SystemStatusLocation()99     inline SystemStatusLocation() :
100         mValid(false) {}
SystemStatusLocation(const UlpLocation & location,const GpsLocationExtended & locationEx)101     inline SystemStatusLocation(const UlpLocation& location,
102                          const GpsLocationExtended& locationEx) :
103         mValid(true),
104         mLocation(location),
105         mLocationEx(locationEx) {}
106     bool equals(const SystemStatusLocation& peer);
107     void dump(void) override;
108 };
109 
110 class SystemStatusPQWM1;
111 class SystemStatusTimeAndClock : public SystemStatusItemBase
112 {
113 public:
114     uint16_t mGpsWeek;
115     uint32_t mGpsTowMs;
116     uint8_t  mTimeValid;
117     uint8_t  mTimeSource;
118     int32_t  mTimeUnc;
119     int32_t  mClockFreqBias;
120     int32_t  mClockFreqBiasUnc;
121     int32_t  mLeapSeconds;
122     int32_t  mLeapSecUnc;
123     uint64_t mTimeUncNs;
SystemStatusTimeAndClock()124     inline SystemStatusTimeAndClock() :
125         mGpsWeek(0),
126         mGpsTowMs(0),
127         mTimeValid(0),
128         mTimeSource(0),
129         mTimeUnc(0),
130         mClockFreqBias(0),
131         mClockFreqBiasUnc(0),
132         mLeapSeconds(0),
133         mLeapSecUnc(0),
134         mTimeUncNs(0ULL) {}
135     inline SystemStatusTimeAndClock(const SystemStatusPQWM1& nmea);
136     bool equals(const SystemStatusTimeAndClock& peer);
137     void dump(void) override;
138 };
139 
140 class SystemStatusXoState : public SystemStatusItemBase
141 {
142 public:
143     uint8_t  mXoState;
SystemStatusXoState()144     inline SystemStatusXoState() :
145         mXoState(0) {}
146     inline SystemStatusXoState(const SystemStatusPQWM1& nmea);
147     bool equals(const SystemStatusXoState& peer);
148     void dump(void) override;
149 };
150 
151 class SystemStatusRfAndParams : public SystemStatusItemBase
152 {
153 public:
154     int32_t  mPgaGain;
155     uint32_t mGpsBpAmpI;
156     uint32_t mGpsBpAmpQ;
157     uint32_t mAdcI;
158     uint32_t mAdcQ;
159     uint32_t mJammerGps;
160     uint32_t mJammerGlo;
161     uint32_t mJammerBds;
162     uint32_t mJammerGal;
163     double   mAgcGps;
164     double   mAgcGlo;
165     double   mAgcBds;
166     double   mAgcGal;
167     uint32_t mGloBpAmpI;
168     uint32_t mGloBpAmpQ;
169     uint32_t mBdsBpAmpI;
170     uint32_t mBdsBpAmpQ;
171     uint32_t mGalBpAmpI;
172     uint32_t mGalBpAmpQ;
SystemStatusRfAndParams()173     inline SystemStatusRfAndParams() :
174         mPgaGain(0),
175         mGpsBpAmpI(0),
176         mGpsBpAmpQ(0),
177         mAdcI(0),
178         mAdcQ(0),
179         mJammerGps(0),
180         mJammerGlo(0),
181         mJammerBds(0),
182         mJammerGal(0),
183         mAgcGps(0),
184         mAgcGlo(0),
185         mAgcBds(0),
186         mAgcGal(0),
187         mGloBpAmpI(0),
188         mGloBpAmpQ(0),
189         mBdsBpAmpI(0),
190         mBdsBpAmpQ(0),
191         mGalBpAmpI(0),
192         mGalBpAmpQ(0) {}
193     inline SystemStatusRfAndParams(const SystemStatusPQWM1& nmea);
194     bool equals(const SystemStatusRfAndParams& peer);
195     void dump(void) override;
196 };
197 
198 class SystemStatusErrRecovery : public SystemStatusItemBase
199 {
200 public:
201     uint32_t mRecErrorRecovery;
SystemStatusErrRecovery()202     inline SystemStatusErrRecovery() :
203         mRecErrorRecovery(0) {};
204     inline SystemStatusErrRecovery(const SystemStatusPQWM1& nmea);
205     bool equals(const SystemStatusErrRecovery& peer);
ignore()206     inline bool ignore() override { return 0 == mRecErrorRecovery; };
207     void dump(void) override;
208 };
209 
210 class SystemStatusPQWP1;
211 class SystemStatusInjectedPosition : public SystemStatusItemBase
212 {
213 public:
214     uint8_t  mEpiValidity;
215     float    mEpiLat;
216     float    mEpiLon;
217     float    mEpiAlt;
218     float    mEpiHepe;
219     float    mEpiAltUnc;
220     uint8_t  mEpiSrc;
SystemStatusInjectedPosition()221     inline SystemStatusInjectedPosition() :
222         mEpiValidity(0),
223         mEpiLat(0),
224         mEpiLon(0),
225         mEpiAlt(0),
226         mEpiHepe(0),
227         mEpiAltUnc(0),
228         mEpiSrc(0) {}
229     inline SystemStatusInjectedPosition(const SystemStatusPQWP1& nmea);
230     bool equals(const SystemStatusInjectedPosition& peer);
231     void dump(void) override;
232 };
233 
234 class SystemStatusPQWP2;
235 class SystemStatusBestPosition : public SystemStatusItemBase
236 {
237 public:
238     bool     mValid;
239     float    mBestLat;
240     float    mBestLon;
241     float    mBestAlt;
242     float    mBestHepe;
243     float    mBestAltUnc;
SystemStatusBestPosition()244     inline SystemStatusBestPosition() :
245         mValid(false),
246         mBestLat(0),
247         mBestLon(0),
248         mBestAlt(0),
249         mBestHepe(0),
250         mBestAltUnc(0) {}
251     inline SystemStatusBestPosition(const SystemStatusPQWP2& nmea);
252     bool equals(const SystemStatusBestPosition& peer);
253     void dump(void) override;
254 };
255 
256 class SystemStatusPQWP3;
257 class SystemStatusXtra : public SystemStatusItemBase
258 {
259 public:
260     uint8_t   mXtraValidMask;
261     uint32_t  mGpsXtraAge;
262     uint32_t  mGloXtraAge;
263     uint32_t  mBdsXtraAge;
264     uint32_t  mGalXtraAge;
265     uint32_t  mQzssXtraAge;
266     uint32_t  mNavicXtraAge;
267     uint32_t  mGpsXtraValid;
268     uint32_t  mGloXtraValid;
269     uint64_t  mBdsXtraValid;
270     uint64_t  mGalXtraValid;
271     uint8_t   mQzssXtraValid;
272     uint32_t  mNavicXtraValid;
SystemStatusXtra()273     inline SystemStatusXtra() :
274         mXtraValidMask(0),
275         mGpsXtraAge(0),
276         mGloXtraAge(0),
277         mBdsXtraAge(0),
278         mGalXtraAge(0),
279         mQzssXtraAge(0),
280         mNavicXtraAge(0),
281         mGpsXtraValid(0),
282         mGloXtraValid(0),
283         mBdsXtraValid(0ULL),
284         mGalXtraValid(0ULL),
285         mQzssXtraValid(0),
286         mNavicXtraValid(0) {}
287     inline SystemStatusXtra(const SystemStatusPQWP3& nmea);
288     bool equals(const SystemStatusXtra& peer);
289     void dump(void) override;
290 };
291 
292 class SystemStatusPQWP4;
293 class SystemStatusEphemeris : public SystemStatusItemBase
294 {
295 public:
296     uint32_t  mGpsEpheValid;
297     uint32_t  mGloEpheValid;
298     uint64_t  mBdsEpheValid;
299     uint64_t  mGalEpheValid;
300     uint8_t   mQzssEpheValid;
SystemStatusEphemeris()301     inline SystemStatusEphemeris() :
302         mGpsEpheValid(0),
303         mGloEpheValid(0),
304         mBdsEpheValid(0ULL),
305         mGalEpheValid(0ULL),
306         mQzssEpheValid(0) {}
307     inline SystemStatusEphemeris(const SystemStatusPQWP4& nmea);
308     bool equals(const SystemStatusEphemeris& peer);
309     void dump(void) override;
310 };
311 
312 class SystemStatusPQWP5;
313 class SystemStatusSvHealth : public SystemStatusItemBase
314 {
315 public:
316     uint32_t  mGpsUnknownMask;
317     uint32_t  mGloUnknownMask;
318     uint64_t  mBdsUnknownMask;
319     uint64_t  mGalUnknownMask;
320     uint8_t   mQzssUnknownMask;
321     uint32_t  mNavicUnknownMask;
322     uint32_t  mGpsGoodMask;
323     uint32_t  mGloGoodMask;
324     uint64_t  mBdsGoodMask;
325     uint64_t  mGalGoodMask;
326     uint8_t   mQzssGoodMask;
327     uint32_t  mNavicGoodMask;
328     uint32_t  mGpsBadMask;
329     uint32_t  mGloBadMask;
330     uint64_t  mBdsBadMask;
331     uint64_t  mGalBadMask;
332     uint8_t   mQzssBadMask;
333     uint32_t  mNavicBadMask;
SystemStatusSvHealth()334     inline SystemStatusSvHealth() :
335         mGpsUnknownMask(0),
336         mGloUnknownMask(0),
337         mBdsUnknownMask(0ULL),
338         mGalUnknownMask(0ULL),
339         mQzssUnknownMask(0),
340         mNavicUnknownMask(0),
341         mGpsGoodMask(0),
342         mGloGoodMask(0),
343         mBdsGoodMask(0ULL),
344         mGalGoodMask(0ULL),
345         mQzssGoodMask(0),
346         mNavicGoodMask(0),
347         mGpsBadMask(0),
348         mGloBadMask(0),
349         mBdsBadMask(0ULL),
350         mGalBadMask(0ULL),
351         mQzssBadMask(0),
352         mNavicBadMask(0) {}
353     inline SystemStatusSvHealth(const SystemStatusPQWP5& nmea);
354     bool equals(const SystemStatusSvHealth& peer);
355     void dump(void) override;
356 };
357 
358 class SystemStatusPQWP6;
359 class SystemStatusPdr : public SystemStatusItemBase
360 {
361 public:
362     uint32_t  mFixInfoMask;
SystemStatusPdr()363     inline SystemStatusPdr() :
364         mFixInfoMask(0) {}
365     inline SystemStatusPdr(const SystemStatusPQWP6& nmea);
366     bool equals(const SystemStatusPdr& peer);
367     void dump(void) override;
368 };
369 
370 class SystemStatusPQWP7;
371 struct SystemStatusNav
372 {
373     GnssEphemerisType   mType;
374     GnssEphemerisSource mSource;
375     int32_t             mAgeSec;
376 };
377 
378 class SystemStatusNavData : public SystemStatusItemBase
379 {
380 public:
381     SystemStatusNav mNav[SV_ALL_NUM];
SystemStatusNavData()382     inline SystemStatusNavData() {
383         for (uint32_t i=0; i<SV_ALL_NUM; i++) {
384             mNav[i].mType = GNSS_EPH_TYPE_UNKNOWN;
385             mNav[i].mSource = GNSS_EPH_SOURCE_UNKNOWN;
386             mNav[i].mAgeSec = 0;
387         }
388     }
389     inline SystemStatusNavData(const SystemStatusPQWP7& nmea);
390     bool equals(const SystemStatusNavData& peer);
391     void dump(void) override;
392 };
393 
394 class SystemStatusPQWS1;
395 class SystemStatusPositionFailure : public SystemStatusItemBase
396 {
397 public:
398     uint32_t  mFixInfoMask;
399     uint32_t  mHepeLimit;
SystemStatusPositionFailure()400     inline SystemStatusPositionFailure() :
401         mFixInfoMask(0),
402         mHepeLimit(0) {}
403     inline SystemStatusPositionFailure(const SystemStatusPQWS1& nmea);
404     bool equals(const SystemStatusPositionFailure& peer);
405     void dump(void) override;
406 };
407 
408 /******************************************************************************
409  SystemStatus report data structure - from DataItem observer
410 ******************************************************************************/
411 class SystemStatusAirplaneMode : public SystemStatusItemBase,
412         public AirplaneModeDataItemBase
413 {
414 public:
415     inline SystemStatusAirplaneMode(bool mode=false) :
AirplaneModeDataItemBase(mode)416             AirplaneModeDataItemBase(mode) {}
SystemStatusAirplaneMode(const AirplaneModeDataItemBase & itemBase)417     inline SystemStatusAirplaneMode(const AirplaneModeDataItemBase& itemBase) :
418             AirplaneModeDataItemBase(itemBase) {}
equals(const SystemStatusAirplaneMode & peer)419     inline bool equals(const SystemStatusAirplaneMode& peer) {
420         return (mMode == peer.mMode);
421     }
422 };
423 
424 class SystemStatusENH : public SystemStatusItemBase,
425         public ENHDataItemBase
426 {
427 public:
428     inline SystemStatusENH(bool enabled=false) :
ENHDataItemBase(enabled)429             ENHDataItemBase(enabled) {}
SystemStatusENH(const ENHDataItemBase & itemBase)430     inline SystemStatusENH(const ENHDataItemBase& itemBase) :
431             ENHDataItemBase(itemBase) {}
equals(const SystemStatusENH & peer)432     inline bool equals(const SystemStatusENH& peer) {
433         return (mEnabled == peer.mEnabled);
434     }
435 };
436 
437 class SystemStatusGpsState : public SystemStatusItemBase,
438         public GPSStateDataItemBase
439 {
440 public:
441     inline SystemStatusGpsState(bool enabled=false) :
GPSStateDataItemBase(enabled)442             GPSStateDataItemBase(enabled) {}
SystemStatusGpsState(const GPSStateDataItemBase & itemBase)443     inline SystemStatusGpsState(const GPSStateDataItemBase& itemBase) :
444             GPSStateDataItemBase(itemBase) {}
equals(const SystemStatusGpsState & peer)445     inline bool equals(const SystemStatusGpsState& peer) {
446         return (mEnabled == peer.mEnabled);
447     }
dump(void)448     inline void dump(void) override {
449         LOC_LOGD("GpsState: state=%u", mEnabled);
450     }
451 };
452 
453 class SystemStatusNLPStatus : public SystemStatusItemBase,
454         public NLPStatusDataItemBase
455 {
456 public:
457     inline SystemStatusNLPStatus(bool enabled=false) :
NLPStatusDataItemBase(enabled)458             NLPStatusDataItemBase(enabled) {}
SystemStatusNLPStatus(const NLPStatusDataItemBase & itemBase)459     inline SystemStatusNLPStatus(const NLPStatusDataItemBase& itemBase) :
460             NLPStatusDataItemBase(itemBase) {}
equals(const SystemStatusNLPStatus & peer)461     inline bool equals(const SystemStatusNLPStatus& peer) {
462         return (mEnabled == peer.mEnabled);
463     }
464 };
465 
466 class SystemStatusWifiHardwareState : public SystemStatusItemBase,
467         public WifiHardwareStateDataItemBase
468 {
469 public:
470     inline SystemStatusWifiHardwareState(bool enabled=false) :
WifiHardwareStateDataItemBase(enabled)471             WifiHardwareStateDataItemBase(enabled) {}
SystemStatusWifiHardwareState(const WifiHardwareStateDataItemBase & itemBase)472     inline SystemStatusWifiHardwareState(const WifiHardwareStateDataItemBase& itemBase) :
473             WifiHardwareStateDataItemBase(itemBase) {}
equals(const SystemStatusWifiHardwareState & peer)474     inline bool equals(const SystemStatusWifiHardwareState& peer) {
475         return (mEnabled == peer.mEnabled);
476     }
477 };
478 
479 class SystemStatusNetworkInfo : public SystemStatusItemBase,
480         public NetworkInfoDataItemBase
481 {
482     NetworkInfoDataItemBase* mSrcObjPtr;
483 public:
484     inline SystemStatusNetworkInfo(
485             int32_t type=0,
486             std::string typeName="",
487             string subTypeName="",
488             bool connected=false,
489             bool roaming=false,
490             uint64_t networkHandle=NETWORK_HANDLE_UNKNOWN) :
491             NetworkInfoDataItemBase(
492                     (NetworkType)type,
493                     type,
494                     typeName,
495                     subTypeName,
496                     connected && (!roaming),
497                     connected,
498                     roaming,
499                     networkHandle),
500             mSrcObjPtr(nullptr) {}
SystemStatusNetworkInfo(const NetworkInfoDataItemBase & itemBase)501     inline SystemStatusNetworkInfo(const NetworkInfoDataItemBase& itemBase) :
502             NetworkInfoDataItemBase(itemBase),
503             mSrcObjPtr((NetworkInfoDataItemBase*)&itemBase) {
504         mType = itemBase.getType();
505     }
equals(const SystemStatusNetworkInfo & peer)506     inline bool equals(const SystemStatusNetworkInfo& peer) {
507         for (uint8_t i = 0; i < MAX_NETWORK_HANDLES; ++i) {
508              if (!(mAllNetworkHandles[i] == peer.mAllNetworkHandles[i])) {
509                  return false;
510              }
511          }
512         return true;
513     }
collate(SystemStatusItemBase & curInfo)514     inline virtual SystemStatusItemBase& collate(SystemStatusItemBase& curInfo) {
515         uint64_t allTypes = (static_cast<SystemStatusNetworkInfo&>(curInfo)).mAllTypes;
516         uint64_t networkHandle =
517                 (static_cast<SystemStatusNetworkInfo&>(curInfo)).mNetworkHandle;
518         int32_t type = (static_cast<SystemStatusNetworkInfo&>(curInfo)).mType;
519         // Replace current with cached table for now and then update
520         memcpy(mAllNetworkHandles,
521                (static_cast<SystemStatusNetworkInfo&>(curInfo)).getNetworkHandle(),
522                sizeof(mAllNetworkHandles));
523         if (mConnected) {
524             mAllTypes |= allTypes;
525             for (uint8_t i = 0; i < MAX_NETWORK_HANDLES; ++i) {
526                 if (mNetworkHandle == mAllNetworkHandles[i].networkHandle) {
527                     LOC_LOGD("collate duplicate detected, not updating");
528                     break;
529                 }
530                 if (NETWORK_HANDLE_UNKNOWN == mAllNetworkHandles[i].networkHandle) {
531                     mAllNetworkHandles[i].networkHandle = mNetworkHandle;
532                     mAllNetworkHandles[i].networkType = (loc_core::NetworkType) mType;
533                     break;
534                 }
535             }
536         } else if (0 != mAllTypes) {
537             uint8_t deletedIndex = MAX_NETWORK_HANDLES;
538             uint8_t lastValidIndex = 0;
539             uint8_t typeCount = 0;
540             for (; lastValidIndex < MAX_NETWORK_HANDLES &&
541                      NETWORK_HANDLE_UNKNOWN != mAllNetworkHandles[lastValidIndex].networkHandle;
542                  ++lastValidIndex) {
543                 // Maintain count for number of network handles still
544                 // connected for given type
545                 if (mType == mAllNetworkHandles[lastValidIndex].networkType) {
546                     typeCount++;
547                 }
548 
549                 if (mNetworkHandle == mAllNetworkHandles[lastValidIndex].networkHandle) {
550                     deletedIndex = lastValidIndex;
551                     typeCount--;
552                 }
553             }
554             if (MAX_NETWORK_HANDLES == lastValidIndex) {
555                 --lastValidIndex;
556             }
557 
558             if (MAX_NETWORK_HANDLES != deletedIndex) {
559                 LOC_LOGD("deletedIndex:%u, lastValidIndex:%u, typeCount:%u",
560                         deletedIndex, lastValidIndex, typeCount);
561                 mAllNetworkHandles[deletedIndex] = mAllNetworkHandles[lastValidIndex];
562                 mAllNetworkHandles[lastValidIndex].networkHandle = NETWORK_HANDLE_UNKNOWN;
563                 mAllNetworkHandles[lastValidIndex].networkType = TYPE_UNKNOWN;
564             }
565 
566             // If no more handles of given type, set bitmask
567             if (0 == typeCount) {
568                 mAllTypes = (allTypes & (~mAllTypes));
569                 LOC_LOGD("mAllTypes:%" PRIx64, mAllTypes);
570             }
571         } // else (mConnected == false && mAllTypes == 0)
572           // we keep mAllTypes as 0, which means no more connections.
573 
574         if (nullptr != mSrcObjPtr) {
575             // this is critical, changing mAllTypes of the original obj
576             mSrcObjPtr->mAllTypes = mAllTypes;
577             memcpy(mSrcObjPtr->mAllNetworkHandles,
578                    mAllNetworkHandles,
579                    sizeof(mSrcObjPtr->mAllNetworkHandles));
580         }
581         return *this;
582     }
dump(void)583     inline void dump(void) override {
584         LOC_LOGD("NetworkInfo: mAllTypes=%" PRIx64 " connected=%u mType=%x",
585                  mAllTypes, mConnected, mType);
586     }
587 };
588 
589 class SystemStatusServiceInfo : public SystemStatusItemBase,
590         public RilServiceInfoDataItemBase
591 {
592 public:
SystemStatusServiceInfo()593     inline SystemStatusServiceInfo() :
594             RilServiceInfoDataItemBase() {}
SystemStatusServiceInfo(const RilServiceInfoDataItemBase & itemBase)595     inline SystemStatusServiceInfo(const RilServiceInfoDataItemBase& itemBase) :
596             RilServiceInfoDataItemBase(itemBase) {}
equals(const SystemStatusServiceInfo & peer)597     inline bool equals(const SystemStatusServiceInfo& peer) {
598         return static_cast<const RilServiceInfoDataItemBase&>(peer) ==
599                 static_cast<const RilServiceInfoDataItemBase&>(*this);
600     }
601 };
602 
603 class SystemStatusRilCellInfo : public SystemStatusItemBase,
604         public RilCellInfoDataItemBase
605 {
606 public:
SystemStatusRilCellInfo()607     inline SystemStatusRilCellInfo() :
608             RilCellInfoDataItemBase() {}
SystemStatusRilCellInfo(const RilCellInfoDataItemBase & itemBase)609     inline SystemStatusRilCellInfo(const RilCellInfoDataItemBase& itemBase) :
610             RilCellInfoDataItemBase(itemBase) {}
equals(const SystemStatusRilCellInfo & peer)611     inline bool equals(const SystemStatusRilCellInfo& peer) {
612         return static_cast<const RilCellInfoDataItemBase&>(peer) ==
613                 static_cast<const RilCellInfoDataItemBase&>(*this);
614     }
615 };
616 
617 class SystemStatusServiceStatus : public SystemStatusItemBase,
618         public ServiceStatusDataItemBase
619 {
620 public:
621     inline SystemStatusServiceStatus(int32_t mServiceState=0) :
ServiceStatusDataItemBase(mServiceState)622             ServiceStatusDataItemBase(mServiceState) {}
SystemStatusServiceStatus(const ServiceStatusDataItemBase & itemBase)623     inline SystemStatusServiceStatus(const ServiceStatusDataItemBase& itemBase) :
624             ServiceStatusDataItemBase(itemBase) {}
equals(const SystemStatusServiceStatus & peer)625     inline bool equals(const SystemStatusServiceStatus& peer) {
626         return (mServiceState == peer.mServiceState);
627     }
628 };
629 
630 class SystemStatusModel : public SystemStatusItemBase,
631         public ModelDataItemBase
632 {
633 public:
634     inline SystemStatusModel(string name="") :
ModelDataItemBase(name)635             ModelDataItemBase(name) {}
SystemStatusModel(const ModelDataItemBase & itemBase)636     inline SystemStatusModel(const ModelDataItemBase& itemBase) :
637             ModelDataItemBase(itemBase) {}
equals(const SystemStatusModel & peer)638     inline bool equals(const SystemStatusModel& peer) {
639         return (mModel == peer.mModel);
640         }
641 };
642 
643 class SystemStatusManufacturer : public SystemStatusItemBase,
644         public ManufacturerDataItemBase
645 {
646 public:
647     inline SystemStatusManufacturer(string name="") :
ManufacturerDataItemBase(name)648             ManufacturerDataItemBase(name) {}
SystemStatusManufacturer(const ManufacturerDataItemBase & itemBase)649     inline SystemStatusManufacturer(const ManufacturerDataItemBase& itemBase) :
650             ManufacturerDataItemBase(itemBase) {}
equals(const SystemStatusManufacturer & peer)651     inline bool equals(const SystemStatusManufacturer& peer) {
652         return (mManufacturer == peer.mManufacturer);
653     }
654 };
655 
656 class SystemStatusAssistedGps : public SystemStatusItemBase,
657         public AssistedGpsDataItemBase
658 {
659 public:
660     inline SystemStatusAssistedGps(bool enabled=false) :
AssistedGpsDataItemBase(enabled)661             AssistedGpsDataItemBase(enabled) {}
SystemStatusAssistedGps(const AssistedGpsDataItemBase & itemBase)662     inline SystemStatusAssistedGps(const AssistedGpsDataItemBase& itemBase) :
663             AssistedGpsDataItemBase(itemBase) {}
equals(const SystemStatusAssistedGps & peer)664     inline bool equals(const SystemStatusAssistedGps& peer) {
665         return (mEnabled == peer.mEnabled);
666     }
667 };
668 
669 class SystemStatusScreenState : public SystemStatusItemBase,
670         public ScreenStateDataItemBase
671 {
672 public:
673     inline SystemStatusScreenState(bool state=false) :
ScreenStateDataItemBase(state)674             ScreenStateDataItemBase(state) {}
SystemStatusScreenState(const ScreenStateDataItemBase & itemBase)675     inline SystemStatusScreenState(const ScreenStateDataItemBase& itemBase) :
676             ScreenStateDataItemBase(itemBase) {}
equals(const SystemStatusScreenState & peer)677     inline bool equals(const SystemStatusScreenState& peer) {
678         return (mState == peer.mState);
679     }
680 };
681 
682 class SystemStatusPowerConnectState : public SystemStatusItemBase,
683         public PowerConnectStateDataItemBase
684 {
685 public:
686     inline SystemStatusPowerConnectState(bool state=false) :
PowerConnectStateDataItemBase(state)687             PowerConnectStateDataItemBase(state) {}
SystemStatusPowerConnectState(const PowerConnectStateDataItemBase & itemBase)688     inline SystemStatusPowerConnectState(const PowerConnectStateDataItemBase& itemBase) :
689             PowerConnectStateDataItemBase(itemBase) {}
equals(const SystemStatusPowerConnectState & peer)690     inline bool equals(const SystemStatusPowerConnectState& peer) {
691         return (mState == peer.mState);
692     }
693 };
694 
695 class SystemStatusTimeZoneChange : public SystemStatusItemBase,
696         public TimeZoneChangeDataItemBase
697 {
698 public:
699     inline SystemStatusTimeZoneChange(
700             int64_t currTimeMillis=0ULL, int32_t rawOffset=0, int32_t dstOffset=0) :
TimeZoneChangeDataItemBase(currTimeMillis,rawOffset,dstOffset)701             TimeZoneChangeDataItemBase(currTimeMillis, rawOffset, dstOffset) {}
SystemStatusTimeZoneChange(const TimeZoneChangeDataItemBase & itemBase)702     inline SystemStatusTimeZoneChange(const TimeZoneChangeDataItemBase& itemBase) :
703             TimeZoneChangeDataItemBase(itemBase) {}
equals(const SystemStatusTimeZoneChange & peer)704     inline bool equals(const SystemStatusTimeZoneChange& peer) {
705         return ((mCurrTimeMillis == peer.mCurrTimeMillis) &&
706                 (mRawOffsetTZ == peer.mRawOffsetTZ) &&
707                 (mDstOffsetTZ == peer.mDstOffsetTZ));
708     }
709 };
710 
711 class SystemStatusTimeChange : public SystemStatusItemBase,
712         public TimeChangeDataItemBase
713 {
714 public:
715     inline SystemStatusTimeChange(
716             int64_t currTimeMillis=0ULL, int32_t rawOffset=0, int32_t dstOffset=0) :
TimeChangeDataItemBase(currTimeMillis,rawOffset,dstOffset)717             TimeChangeDataItemBase(currTimeMillis, rawOffset, dstOffset) {}
SystemStatusTimeChange(const TimeChangeDataItemBase & itemBase)718     inline SystemStatusTimeChange(const TimeChangeDataItemBase& itemBase) :
719             TimeChangeDataItemBase(itemBase) {}
equals(const SystemStatusTimeChange & peer)720     inline bool equals(const SystemStatusTimeChange& peer) {
721         return ((mCurrTimeMillis == peer.mCurrTimeMillis) &&
722                 (mRawOffsetTZ == peer.mRawOffsetTZ) &&
723                 (mDstOffsetTZ == peer.mDstOffsetTZ));
724     }
725 };
726 
727 class SystemStatusWifiSupplicantStatus : public SystemStatusItemBase,
728         public WifiSupplicantStatusDataItemBase
729 {
730 public:
SystemStatusWifiSupplicantStatus()731     inline SystemStatusWifiSupplicantStatus() :
732             WifiSupplicantStatusDataItemBase() {}
SystemStatusWifiSupplicantStatus(const WifiSupplicantStatusDataItemBase & itemBase)733     inline SystemStatusWifiSupplicantStatus(const WifiSupplicantStatusDataItemBase& itemBase) :
734             WifiSupplicantStatusDataItemBase(itemBase) {}
equals(const SystemStatusWifiSupplicantStatus & peer)735     inline bool equals(const SystemStatusWifiSupplicantStatus& peer) {
736         return ((mState == peer.mState) &&
737                 (mApMacAddressValid == peer.mApMacAddressValid) &&
738                 (mWifiApSsidValid == peer.mWifiApSsidValid) &&
739                 (mWifiApSsid == peer.mWifiApSsid));
740         }
741 };
742 
743 class SystemStatusShutdownState : public SystemStatusItemBase,
744         public ShutdownStateDataItemBase
745 {
746 public:
747     inline SystemStatusShutdownState(bool state=false) :
ShutdownStateDataItemBase(state)748             ShutdownStateDataItemBase(state) {}
SystemStatusShutdownState(const ShutdownStateDataItemBase & itemBase)749     inline SystemStatusShutdownState(const ShutdownStateDataItemBase& itemBase) :
750             ShutdownStateDataItemBase(itemBase) {}
equals(const SystemStatusShutdownState & peer)751     inline bool equals(const SystemStatusShutdownState& peer) {
752         return (mState == peer.mState);
753     }
754 };
755 
756 class SystemStatusTac : public SystemStatusItemBase,
757         public TacDataItemBase
758 {
759 public:
760     inline SystemStatusTac(std::string value="") :
TacDataItemBase(value)761             TacDataItemBase(value) {}
SystemStatusTac(const TacDataItemBase & itemBase)762     inline SystemStatusTac(const TacDataItemBase& itemBase) :
763             TacDataItemBase(itemBase) {}
equals(const SystemStatusTac & peer)764     inline bool equals(const SystemStatusTac& peer) {
765         return (mValue == peer.mValue);
766     }
dump(void)767     inline void dump(void) override {
768         LOC_LOGD("Tac: value=%s", mValue.c_str());
769     }
770 };
771 
772 class SystemStatusMccMnc : public SystemStatusItemBase,
773         public MccmncDataItemBase
774 {
775 public:
776     inline SystemStatusMccMnc(std::string value="") :
MccmncDataItemBase(value)777             MccmncDataItemBase(value) {}
SystemStatusMccMnc(const MccmncDataItemBase & itemBase)778     inline SystemStatusMccMnc(const MccmncDataItemBase& itemBase) :
779             MccmncDataItemBase(itemBase) {}
equals(const SystemStatusMccMnc & peer)780     inline bool equals(const SystemStatusMccMnc& peer) {
781         return (mValue == peer.mValue);
782     }
dump(void)783     inline void dump(void) override {
784         LOC_LOGD("TacMccMnc value=%s", mValue.c_str());
785     }
786 };
787 
788 class SystemStatusBtDeviceScanDetail : public SystemStatusItemBase,
789         public BtDeviceScanDetailsDataItemBase
790 {
791 public:
SystemStatusBtDeviceScanDetail()792     inline SystemStatusBtDeviceScanDetail() :
793             BtDeviceScanDetailsDataItemBase() {}
SystemStatusBtDeviceScanDetail(const BtDeviceScanDetailsDataItemBase & itemBase)794     inline SystemStatusBtDeviceScanDetail(const BtDeviceScanDetailsDataItemBase& itemBase) :
795             BtDeviceScanDetailsDataItemBase(itemBase) {}
equals(const SystemStatusBtDeviceScanDetail & peer)796     inline bool equals(const SystemStatusBtDeviceScanDetail& peer) {
797         return ((mApSrnRssi == peer.mApSrnRssi) &&
798                 (0 == memcmp(mApSrnMacAddress, peer.mApSrnMacAddress, sizeof(mApSrnMacAddress))) &&
799                 (mApSrnTimestamp == peer.mApSrnTimestamp) &&
800                 (mRequestTimestamp == peer.mRequestTimestamp) &&
801                 (mReceiveTimestamp == peer.mReceiveTimestamp));
802     }
803 };
804 
805 class SystemStatusBtleDeviceScanDetail : public SystemStatusItemBase,
806         public BtLeDeviceScanDetailsDataItemBase
807 {
808 public:
SystemStatusBtleDeviceScanDetail()809     inline SystemStatusBtleDeviceScanDetail() :
810             BtLeDeviceScanDetailsDataItemBase() {}
SystemStatusBtleDeviceScanDetail(const BtLeDeviceScanDetailsDataItemBase & itemBase)811     inline SystemStatusBtleDeviceScanDetail(const BtLeDeviceScanDetailsDataItemBase& itemBase) :
812             BtLeDeviceScanDetailsDataItemBase(itemBase) {}
equals(const SystemStatusBtleDeviceScanDetail & peer)813     inline bool equals(const SystemStatusBtleDeviceScanDetail& peer) {
814         return ((mApSrnRssi == peer.mApSrnRssi) &&
815                 (0 == memcmp(mApSrnMacAddress, peer.mApSrnMacAddress, sizeof(mApSrnMacAddress))) &&
816                 (mApSrnTimestamp == peer.mApSrnTimestamp) &&
817                 (mRequestTimestamp == peer.mRequestTimestamp) &&
818                 (mReceiveTimestamp == peer.mReceiveTimestamp));
819     }
820 };
821 
822 /******************************************************************************
823  SystemStatusReports
824 ******************************************************************************/
825 class SystemStatusReports
826 {
827 public:
828     // from QMI_LOC indication
829     std::vector<SystemStatusLocation>         mLocation;
830 
831     // from ME debug NMEA
832     std::vector<SystemStatusTimeAndClock>     mTimeAndClock;
833     std::vector<SystemStatusXoState>          mXoState;
834     std::vector<SystemStatusRfAndParams>      mRfAndParams;
835     std::vector<SystemStatusErrRecovery>      mErrRecovery;
836 
837     // from PE debug NMEA
838     std::vector<SystemStatusInjectedPosition> mInjectedPosition;
839     std::vector<SystemStatusBestPosition>     mBestPosition;
840     std::vector<SystemStatusXtra>             mXtra;
841     std::vector<SystemStatusEphemeris>        mEphemeris;
842     std::vector<SystemStatusSvHealth>         mSvHealth;
843     std::vector<SystemStatusPdr>              mPdr;
844     std::vector<SystemStatusNavData>          mNavData;
845 
846     // from SM debug NMEA
847     std::vector<SystemStatusPositionFailure>  mPositionFailure;
848 
849     // from dataitems observer
850     std::vector<SystemStatusAirplaneMode>     mAirplaneMode;
851     std::vector<SystemStatusENH>              mENH;
852     std::vector<SystemStatusGpsState>         mGPSState;
853     std::vector<SystemStatusNLPStatus>        mNLPStatus;
854     std::vector<SystemStatusWifiHardwareState> mWifiHardwareState;
855     std::vector<SystemStatusNetworkInfo>      mNetworkInfo;
856     std::vector<SystemStatusServiceInfo>      mRilServiceInfo;
857     std::vector<SystemStatusRilCellInfo>      mRilCellInfo;
858     std::vector<SystemStatusServiceStatus>    mServiceStatus;
859     std::vector<SystemStatusModel>            mModel;
860     std::vector<SystemStatusManufacturer>     mManufacturer;
861     std::vector<SystemStatusAssistedGps>      mAssistedGps;
862     std::vector<SystemStatusScreenState>      mScreenState;
863     std::vector<SystemStatusPowerConnectState> mPowerConnectState;
864     std::vector<SystemStatusTimeZoneChange>   mTimeZoneChange;
865     std::vector<SystemStatusTimeChange>       mTimeChange;
866     std::vector<SystemStatusWifiSupplicantStatus> mWifiSupplicantStatus;
867     std::vector<SystemStatusShutdownState>    mShutdownState;
868     std::vector<SystemStatusTac>              mTac;
869     std::vector<SystemStatusMccMnc>           mMccMnc;
870     std::vector<SystemStatusBtDeviceScanDetail> mBtDeviceScanDetail;
871     std::vector<SystemStatusBtleDeviceScanDetail> mBtLeDeviceScanDetail;
872 };
873 
874 /******************************************************************************
875  SystemStatus
876 ******************************************************************************/
877 class SystemStatus
878 {
879 private:
880     static SystemStatus                       *mInstance;
881     SystemStatusOsObserver                    mSysStatusObsvr;
882     // ctor
883     SystemStatus(const MsgTask* msgTask);
884     // dtor
~SystemStatus()885     inline ~SystemStatus() {}
886 
887     // Data members
888     static pthread_mutex_t                    mMutexSystemStatus;
889     SystemStatusReports mCache;
890 
891     template <typename TYPE_REPORT, typename TYPE_ITEM>
892     bool setIteminReport(TYPE_REPORT& report, TYPE_ITEM&& s);
893 
894     // set default dataitem derived item in report cache
895     template <typename TYPE_REPORT, typename TYPE_ITEM>
896     void setDefaultIteminReport(TYPE_REPORT& report, const TYPE_ITEM& s);
897 
898     template <typename TYPE_REPORT, typename TYPE_ITEM>
899     void getIteminReport(TYPE_REPORT& reportout, const TYPE_ITEM& c) const;
900 
901 public:
902     // Static methods
903     static SystemStatus* getInstance(const MsgTask* msgTask);
904     static void destroyInstance();
905     IOsObserver* getOsObserver();
906 
907     // Helpers
908     bool eventPosition(const UlpLocation& location,const GpsLocationExtended& locationEx);
909     bool eventDataItemNotify(IDataItemCore* dataitem);
910     bool setNmeaString(const char *data, uint32_t len);
911     bool getReport(SystemStatusReports& reports, bool isLatestonly = false) const;
912     bool setDefaultGnssEngineStates(void);
913     bool eventConnectionStatus(bool connected, int8_t type,
914                                bool roaming, NetworkHandle networkHandle);
915     bool updatePowerConnectState(bool charging);
916 };
917 
918 } // namespace loc_core
919 
920 #endif //__SYSTEM_STATUS__
921 
922