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