1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <Constants.h>
20 #include <android/hardware/gnss/1.0/IGnss.h>
21 #include <android/hardware/gnss/2.0/IGnss.h>
22 #include <hidl/Status.h>
23 #include <ctime>
24 #include <string>
25 namespace android {
26 namespace hardware {
27 namespace gnss {
28 namespace common {
29 
30 constexpr char GPGA_RECORD_TAG[] = "$GPGGA";
31 constexpr char GPRMC_RECORD_TAG[] = "$GPRMC";
32 constexpr char LINE_SEPARATOR = '\n';
33 constexpr char COMMA_SEPARATOR = ',';
34 constexpr double TIMESTAMP_EPSILON = 0.001;
35 constexpr int MIN_COL_NUM = 13;
36 
37 /** Helper class to parse and store the GNSS fix details information. */
38 class NmeaFixInfo {
39   private:
40     float altitudeMeters;
41     float bearingDegrees;
42     uint32_t fixId;
43     bool hasGMCRecord;
44     bool hasGGARecord;
45     float hDop;
46     float vDop;
47     float latDeg;
48     float lngDeg;
49     uint32_t satelliteCount;
50     float speedMetersPerSec;
51     int64_t timestamp;
52 
53   public:
54     static std::unique_ptr<V2_0::GnssLocation> getLocationFromInputStr(const std::string& inputStr);
55 
56   private:
57     static void splitStr(const std::string& line, const char& delimiter,
58                          std::vector<std::string>& out);
59     static float checkAndConvertToFloat(const std::string& sentence);
60     static int64_t nmeaPartsToTimestamp(const std::string& timeStr, const std::string& dateStr);
61 
62     NmeaFixInfo();
63     void parseGGALine(const std::vector<std::string>& sentenceValues);
64     void parseRMCLine(const std::vector<std::string>& sentenceValues);
65     std::unique_ptr<V2_0::GnssLocation> toGnssLocation() const;
66 
67     // Getters
68     float getAltitudeMeters() const;
69     float getBearingAccuracyDegrees() const;
70     float getBearingDegrees() const;
71     uint32_t getFixId() const;
72     float getHorizontalAccuracyMeters() const;
73     float getLatDeg() const;
74     float getLngDeg() const;
75     float getSpeedAccuracyMetersPerSecond() const;
76     float getSpeedMetersPerSec() const;
77     int64_t getTimestamp() const;
78     float getVerticalAccuracyMeters() const;
79 
80     bool isValidFix() const;
81     void reset();
82     NmeaFixInfo& operator=(const NmeaFixInfo& rhs);
83 };
84 
85 }  // namespace common
86 }  // namespace gnss
87 }  // namespace hardware
88 }  // namespace android