1 /*
2  * Copyright (C) 2021 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 #include <ParseUtils.h>
18 #include <sstream>
19 #include <stdexcept>
20 
21 namespace android {
22 namespace hardware {
23 namespace gnss {
24 namespace common {
25 
tryParseInt(const std::string & s,int defaultVal)26 int ParseUtils::tryParseInt(const std::string& s, int defaultVal) {
27     if (s.empty()) {
28         return defaultVal;
29     } else {
30         return std::stoi(s);
31     }
32 }
33 
tryParsefloat(const std::string & s,float defaultVal)34 float ParseUtils::tryParsefloat(const std::string& s, float defaultVal) {
35     if (s.empty()) {
36         return defaultVal;
37     } else {
38         return std::stof(s);
39     }
40 }
41 
tryParseDouble(const std::string & s,double defaultVal)42 double ParseUtils::tryParseDouble(const std::string& s, double defaultVal) {
43     if (s.empty()) {
44         return defaultVal;
45     } else {
46         return std::stod(s);
47     }
48 }
49 
tryParseLong(const std::string & s,long defaultVal)50 long ParseUtils::tryParseLong(const std::string& s, long defaultVal) {
51     if (s.empty()) {
52         return defaultVal;
53     } else {
54         return std::stol(s);
55     }
56 }
57 
tryParseLongLong(const std::string & s,long long defaultVal)58 long long ParseUtils::tryParseLongLong(const std::string& s, long long defaultVal) {
59     if (s.empty()) {
60         return defaultVal;
61     } else {
62         return std::stoll(s);
63     }
64 }
65 
splitStr(const std::string & line,const char & delimiter,std::vector<std::string> & out)66 void ParseUtils::splitStr(const std::string& line, const char& delimiter,
67                           std::vector<std::string>& out) {
68     std::istringstream iss(line);
69     std::string item;
70     while (std::getline(iss, item, delimiter)) {
71         out.push_back(item);
72     }
73 }
74 
isValidHeader(const std::unordered_map<std::string,int> & columnNameIdMapping)75 bool ParseUtils::isValidHeader(const std::unordered_map<std::string, int>& columnNameIdMapping) {
76     std::vector<std::string> requiredHeaderColumns = {"Raw",
77                                                       "utcTimeMillis",
78                                                       "TimeNanos",
79                                                       "LeapSecond",
80                                                       "TimeUncertaintyNanos",
81                                                       "FullBiasNanos",
82                                                       "BiasNanos",
83                                                       "BiasUncertaintyNanos",
84                                                       "DriftNanosPerSecond",
85                                                       "DriftUncertaintyNanosPerSecond",
86                                                       "HardwareClockDiscontinuityCount",
87                                                       "Svid",
88                                                       "TimeOffsetNanos",
89                                                       "State",
90                                                       "ReceivedSvTimeNanos",
91                                                       "ReceivedSvTimeUncertaintyNanos",
92                                                       "Cn0DbHz",
93                                                       "PseudorangeRateMetersPerSecond",
94                                                       "PseudorangeRateUncertaintyMetersPerSecond",
95                                                       "AccumulatedDeltaRangeState",
96                                                       "AccumulatedDeltaRangeMeters",
97                                                       "AccumulatedDeltaRangeUncertaintyMeters",
98                                                       "CarrierFrequencyHz",
99                                                       "CarrierCycles",
100                                                       "CarrierPhase",
101                                                       "CarrierPhaseUncertainty",
102                                                       "MultipathIndicator",
103                                                       "SnrInDb",
104                                                       "ConstellationType",
105                                                       "AgcDb",
106                                                       "BasebandCn0DbHz",
107                                                       "FullInterSignalBiasNanos",
108                                                       "FullInterSignalBiasUncertaintyNanos",
109                                                       "SatelliteInterSignalBiasNanos",
110                                                       "SatelliteInterSignalBiasUncertaintyNanos",
111                                                       "CodeType",
112                                                       "ChipsetElapsedRealtimeNanos"};
113 
114     for (const auto& columnName : requiredHeaderColumns) {
115         if (columnNameIdMapping.find(columnName) == columnNameIdMapping.end()) {
116             ALOGE("Missing column %s in header.", columnName.c_str());
117             return false;
118         }
119     }
120 
121     return true;
122 }
123 
124 }  // namespace common
125 }  // namespace gnss
126 }  // namespace hardware
127 }  // namespace android
128