1 /*
2  * Copyright (c) 2021-2022 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 "param_wrapper.h"
17 
18 #include <unordered_map>
19 #include <vector>
20 #include <climits>
21 
22 #include "beget_ext.h"
23 #include "param_comm.h"
24 #include "init_param.h"
25 #include "init_utils.h"
26 #include "sysparam_errno.h"
27 #include "securec.h"
28 #include "parameter.h"
29 #include "parameters.h"
30 
31 namespace OHOS {
32 namespace system {
33 static constexpr int MAX_VALUE_LEN = 128;
SetParameter(const std::string& key, const std::string& value)34 bool SetParameter(const std::string& key, const std::string& value)
35 {
36     int ret = SystemSetParameter(key.c_str(), value.c_str());
37     return (ret == 0) ? true : false;
38 }
39 
40 template<typename T>
StringToInt(const std::string& str, T min, T max, T& out)41 bool StringToInt(const std::string& str, T min, T max, T& out)
42 {
43     long long int result = 0;
44     if (StringToLL(str.c_str(), &result) != 0) {
45         return false;
46     }
47     if (result < min || max < result) {
48         return false;
49     }
50     out = static_cast<T>(result);
51     return true;
52 }
53 
54 template<typename T>
StringToUint(const std::string& str, T max, T& out)55 bool StringToUint(const std::string& str, T max, T& out)
56 {
57     unsigned long long int result = 0;
58     if (StringToULL(str.c_str(), &result) != 0) {
59         return false;
60     }
61     if (max < result) {
62         return false;
63     }
64     out = static_cast<T>(result);
65     return true;
66 }
67 
GetParameter(const std::string& key, const std::string& def)68 std::string GetParameter(const std::string& key, const std::string& def)
69 {
70     uint32_t size = 0;
71     int ret = SystemReadParam(key.c_str(), NULL, &size);
72     if (ret == 0) {
73         std::vector<char> value(MAX_VALUE_LEN);
74         ret = SystemReadParam(key.c_str(), value.data(), &size);
75         if (ret == 0) {
76             return std::string(value.data());
77         }
78     }
79     if (IsValidParamValue(def.c_str(), MAX_VALUE_LEN) == 1) {
80         return std::string(def);
81     }
82     return "";
83 }
84 
GetBoolParameter(const std::string& key, bool def)85 bool GetBoolParameter(const std::string& key, bool def)
86 {
87     static const std::string trueMap[] = { "1", "y", "yes", "on", "true" };
88     static const std::string falseMap[] = { "0", "off", "n", "no", "false" };
89     std::string value = GetParameter(key, "");
90     for (size_t i = 0; i < sizeof(trueMap) / sizeof(trueMap[0]); i++) {
91         if (trueMap[i] == value) {
92             return true;
93         }
94     }
95     for (size_t i = 0; i < sizeof(falseMap) / sizeof(falseMap[0]); i++) {
96         if (falseMap[i] == value) {
97             return false;
98         }
99     }
100     return def;
101 }
102 
GetStringParameter(const std::string &key, std::string &value, const std::string def)103 int GetStringParameter(const std::string &key, std::string &value, const std::string def)
104 {
105     uint32_t size = 0;
106     int ret = SystemReadParam(key.c_str(), NULL, &size);
107     if (ret == 0) {
108         std::vector<char> data(size + 1);
109         ret = SystemReadParam(key.c_str(), data.data(), &size);
110         if (ret == 0) {
111             value = std::string(data.data());
112             return EC_SUCCESS;
113         }
114     }
115     if (IsValidParamValue(def.c_str(), MAX_VALUE_LEN) == 1) {
116         value = std::string(def);
117         return EC_SUCCESS;
118     }
119     return EC_FAILURE;
120 }
121 
122 template<typename T>
GetIntParameter(const std::string& key, T def, T min, T max)123 T GetIntParameter(const std::string& key, T def, T min, T max)
124 {
125     if (!std::is_signed<T>::value) {
126         return def;
127     }
128     T result;
129     std::string value = GetParameter(key, "");
130     if (!value.empty() && StringToInt(value, min, max, result)) {
131         return result;
132     }
133     return def;
134 }
135 
136 template int8_t GetIntParameter(const std::string&, int8_t, int8_t, int8_t);
137 template int16_t GetIntParameter(const std::string&, int16_t, int16_t, int16_t);
138 template int32_t GetIntParameter(const std::string&, int32_t, int32_t, int32_t);
139 template int64_t GetIntParameter(const std::string&, int64_t, int64_t, int64_t);
140 
141 template<typename T>
GetUintParameter(const std::string& key, T def, T max)142 T GetUintParameter(const std::string& key, T def, T max)
143 {
144     if (!std::is_unsigned<T>::value) {
145         return def;
146     }
147     T result;
148     std::string value = GetParameter(key, "");
149     if (!value.empty() && StringToUint(value, max, result)) {
150         return result;
151     }
152     return def;
153 }
154 
155 template uint8_t GetUintParameter(const std::string&, uint8_t, uint8_t);
156 template uint16_t GetUintParameter(const std::string&, uint16_t, uint16_t);
157 template uint32_t GetUintParameter(const std::string&, uint32_t, uint32_t);
158 template uint64_t GetUintParameter(const std::string&, uint64_t, uint64_t);
159 
GetDeviceType(void)160 std::string GetDeviceType(void)
161 {
162     std::unordered_map<std::string, std::string> deviceTypeMap = {
163         {"watch", "wearable"},
164         {"fitnessWatch", "liteWearable"},
165     };
166     static const char *productType = nullptr;
167     const char *type = GetProperty("const.product.devicetype", &productType);
168     if (type == nullptr) {
169         type = GetProperty("const.build.characteristics", &productType);
170     }
171     if (type == nullptr) {
172         return std::string("");
173     }
174     if (deviceTypeMap.count(type) != 0) {
175         return deviceTypeMap[type];
176     }
177     return std::string(type);
178 }
179 
GetIntParameter(const std::string &key, int def)180 int GetIntParameter(const std::string &key, int def)
181 {
182     return GetIntParameter(key, def, INT_MIN, INT_MAX);
183 }
184 }  // namespace system
185 }  // namespace OHOS
186