• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "parameters.h"
17 
18 #include <cerrno>
19 #include <unordered_map>
20 #include <vector>
21 
22 #include "parameters_abstractor.h"
23 #include "init_param.h"
24 
25 namespace OHOS {
26 namespace system {
27 namespace {
28 class NullAbstractor : public ParametersAbstractor {
29 public:
GetParameter(const std::string & key,const std::string & def)30     std::string GetParameter(const std::string& key, const std::string& def) override
31     {
32         unsigned int len = 0;
33         int ret = SystemGetParameter(key.c_str(), nullptr, &len);
34         if (ret == 0 && len > 0) {
35             std::vector<char> value(len + 1);
36             ret = SystemGetParameter(key.c_str(), value.data(), &len);
37             if (ret == 0) {
38                 return std::string(value.data());
39             }
40         }
41         return def;
42     }
SetParameter(const std::string & key,const std::string & value)43     bool SetParameter(const std::string& key, const std::string& value) override
44     {
45         return SystemSetParameter(key.c_str(), value.c_str()) == 0;
46     }
47 
WaitParameter(const std::string & key,const std::string & value,int timeout)48     int WaitParameter(const std::string& key, const std::string& value, int timeout) override
49     {
50         return SystemWaitParameter(key.c_str(), value.c_str(), timeout);
51     }
52 
FindParameter(const std::string & key)53     unsigned int FindParameter(const std::string& key) override
54     {
55         unsigned int handle = 0;
56         int ret = SystemFindParameter(key.c_str(), &handle);
57         if (ret != 0) {
58             return static_cast<unsigned int>(-1);
59         }
60         return handle;
61     }
62 
GetParameterCommitId(unsigned int handle)63     unsigned int GetParameterCommitId(unsigned int handle) override
64     {
65         unsigned int commitId = 0;
66         int ret = SystemGetParameterCommitId(handle, &commitId);
67         if (ret != 0) {
68             return static_cast<unsigned int>(-1);
69         }
70         return commitId;
71     }
72 
GetParameterName(unsigned int handle)73     std::string GetParameterName(unsigned int handle) override
74     {
75         std::vector<char> value(PARAM_NAME_LEN_MAX);
76         int ret = SystemGetParameterName(handle, value.data(), PARAM_NAME_LEN_MAX);
77         if (ret == 0) {
78             return std::string(value.data());
79         }
80         return std::string();
81     }
82 
GetParameterValue(unsigned int handle)83     std::string GetParameterValue(unsigned int handle) override
84     {
85         unsigned int len = 0;
86         int ret = SystemGetParameterValue(handle, nullptr, &len);
87         if (ret == 0 && len > 0) {
88             std::vector<char> value(len + 1);
89             ret = SystemGetParameterValue(handle, value.data(), &len);
90             if (ret == 0) {
91                 return std::string(value.data());
92             }
93         }
94         return std::string();
95     }
96 } g_abstractor;
97 
98 ParametersAbstractor& g_abstractorRef = g_abstractor;
99 
100 constexpr unsigned int DECIMAL = 10;
101 constexpr unsigned int HEX = 16;
102 
103 template<typename T>
StringToInt(const std::string & str,T min,T max,T & out)104 bool StringToInt(const std::string& str, T min, T max, T& out)
105 {
106     const char* s = str.c_str();
107     while (isspace(*s)) {
108         s++;
109     }
110 
111     bool positiveHex = (str.size() > 1 && s[0] == '0' && (s[1] == 'x' || s[1] == 'X'));
112     bool negativeHex = (str.size() > 2 && s[0] == '-' && s[1] == '0' && (s[2] == 'x' || s[2] == 'X')); // 2: shorttest
113     int base = (positiveHex || negativeHex) ? HEX : DECIMAL;
114     char* end = nullptr;
115     errno = 0;
116     long long int result = strtoll(s, &end, base);
117     if (errno != 0) {
118         return false;
119     }
120     if (s == end || *end != '\0') {
121         return false;
122     }
123     if (result < min || max < result) {
124         return false;
125     }
126     out = static_cast<T>(result);
127     return true;
128 }
129 
130 template<typename T>
StringToUint(const std::string & str,T max,T & out)131 bool StringToUint(const std::string& str, T max, T& out)
132 {
133     const char* s = str.c_str();
134     while (isspace(*s)) {
135         s++;
136     }
137 
138     if (s[0] == '-') {
139         return false;
140     }
141 
142     int base = (s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) ? HEX : DECIMAL;
143     char* end = nullptr;
144     errno = 0;
145     unsigned long long int result = strtoull(s, &end, base);
146     if (errno != 0) {
147         return false;
148     }
149     if (end == s) {
150         return false;
151     }
152     if (*end != '\0') {
153         return false;
154     }
155     if (max < result) {
156         return false;
157     }
158     out = static_cast<T>(result);
159     return true;
160 }
161 }  // namespace
162 
GetParameter(const std::string & key,const std::string & def)163 std::string GetParameter(const std::string& key, const std::string& def)
164 {
165     return g_abstractorRef.GetParameter(key, def);
166 }
167 
GetBoolParameter(const std::string & key,bool def)168 bool GetBoolParameter(const std::string& key, bool def)
169 {
170     std::string value = GetParameter(key, "");
171     if ((value == "1") || (value == "y") || (value == "yes") || (value == "on") || (value == "true")) {
172         return true;
173     } else if ((value == "0") || (value == "n") || (value == "no") || (value == "off") || (value == "false")) {
174         return false;
175     }
176     return def;
177 }
178 
179 template<typename T>
GetIntParameter(const std::string & key,T def,T min,T max)180 T GetIntParameter(const std::string& key, T def, T min, T max)
181 {
182     if (!std::is_signed<T>::value) {
183         return def;
184     }
185     T result;
186     std::string value = GetParameter(key, "");
187     if (!value.empty() && StringToInt(value, min, max, result)) {
188         return result;
189     }
190     return def;
191 }
192 
193 template int8_t GetIntParameter(const std::string&, int8_t, int8_t, int8_t);
194 template int16_t GetIntParameter(const std::string&, int16_t, int16_t, int16_t);
195 template int32_t GetIntParameter(const std::string&, int32_t, int32_t, int32_t);
196 template int64_t GetIntParameter(const std::string&, int64_t, int64_t, int64_t);
197 
198 template<typename T>
GetUintParameter(const std::string & key,T def,T max)199 T GetUintParameter(const std::string& key, T def, T max)
200 {
201     if (!std::is_unsigned<T>::value) {
202         return def;
203     }
204     T result;
205     std::string value = GetParameter(key, "");
206     if (!value.empty() && StringToUint(value, max, result)) {
207         return result;
208     }
209     return def;
210 }
211 
212 template uint8_t GetUintParameter(const std::string&, uint8_t, uint8_t);
213 template uint16_t GetUintParameter(const std::string&, uint16_t, uint16_t);
214 template uint32_t GetUintParameter(const std::string&, uint32_t, uint32_t);
215 template uint64_t GetUintParameter(const std::string&, uint64_t, uint64_t);
216 
SetParameter(const std::string & key,const std::string & value)217 bool SetParameter(const std::string& key, const std::string& value)
218 {
219     return g_abstractorRef.SetParameter(key, value);
220 }
221 
WaitParameter(const std::string & key,const std::string & value,int timeout)222 int WaitParameter(const std::string& key, const std::string& value, int timeout)
223 {
224     return g_abstractorRef.WaitParameter(key, value, timeout);
225 }
226 
FindParameter(const std::string & key)227 unsigned int FindParameter(const std::string& key)
228 {
229     return g_abstractorRef.FindParameter(key);
230 }
231 
GetParameterCommitId(unsigned int handle)232 unsigned int GetParameterCommitId(unsigned int handle)
233 {
234     return g_abstractorRef.GetParameterCommitId(handle);
235 }
236 
GetParameterName(unsigned int handle)237 std::string GetParameterName(unsigned int handle)
238 {
239     return g_abstractorRef.GetParameterName(handle);
240 }
241 
GetParameterValue(unsigned int handle)242 std::string GetParameterValue(unsigned int handle)
243 {
244     return g_abstractorRef.GetParameterValue(handle);
245 }
246 
SetAbstractor(const ParametersAbstractor & abstractor)247 void SetAbstractor(const ParametersAbstractor& abstractor)
248 {
249     g_abstractorRef = abstractor;
250 }
251 
GetDeviceType(void)252 std::string GetDeviceType(void)
253 {
254     std::unordered_map<std::string, std::string> deviceTypeMap = {
255         { "watch", "wearable" },
256         { "fitnessWatch", "liteWearable" },
257     };
258     std::string deviceType = GetParameter("const.build.characteristics", "");
259     if (deviceTypeMap.count(deviceType) != 0) {
260         return deviceTypeMap[deviceType];
261     }
262     return deviceType;
263 }
264 }  // namespace system
265 }  // namespace OHOS
266