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 "parameter.h"
17
18 #include <securec.h>
19 #include <string.h>
20
21 #include "parameter_hal.h"
22 #include "sysparam_errno.h"
23 #include "sysversion.h"
24
25 #define OS_FULL_NAME_LEN 128
26 #define VERSION_ID_MAX_LEN 256
27
28 static const int DEV_UUID_LENGTH = 65;
29
30 static const char EMPTY_STR[] = { "" };
31
GetParameter(const char * key,const char * def,char * value,unsigned int len)32 int GetParameter(const char *key, const char *def, char *value, unsigned int len)
33 {
34 if ((key == NULL) || (value == NULL)) {
35 return EC_INVALID;
36 }
37 int ret = HalGetParameter(key, def, value, len);
38 return (ret < 0) ? ret : strlen(value);
39 }
40
SetParameter(const char * key,const char * value)41 int SetParameter(const char *key, const char *value)
42 {
43 if ((key == NULL) || (value == NULL)) {
44 return EC_INVALID;
45 }
46 return HalSetParameter(key, value);
47 }
48
WaitParameter(const char * key,const char * value,int timeout)49 int WaitParameter(const char *key, const char *value, int timeout)
50 {
51 if ((key == NULL) || (value == NULL)) {
52 return EC_INVALID;
53 }
54 return HalWaitParameter(key, value, timeout);
55 }
56
GetDeviceType(void)57 const char *GetDeviceType(void)
58 {
59 return HalGetDeviceType();
60 }
61
GetProductModel(void)62 const char *GetProductModel(void)
63 {
64 return HalGetProductModel();
65 }
66
GetManufacture(void)67 const char *GetManufacture(void)
68 {
69 return HalGetManufacture();
70 }
71
GetBrand(void)72 const char *GetBrand(void)
73 {
74 return HalGetBrand();
75 }
76
GetMarketName(void)77 const char *GetMarketName(void)
78 {
79 return HalGetMarketName();
80 }
81
GetProductSeries(void)82 const char *GetProductSeries(void)
83 {
84 return HalGetProductSeries();
85 }
86
GetSoftwareModel(void)87 const char *GetSoftwareModel(void)
88 {
89 return HalGetSoftwareModel();
90 }
91
GetHardwareModel(void)92 const char *GetHardwareModel(void)
93 {
94 return HalGetHardwareModel();
95 }
96
GetHardwareProfile(void)97 const char *GetHardwareProfile(void)
98 {
99 return HalGetHardwareProfile();
100 }
101
GetSerial(void)102 const char *GetSerial(void)
103 {
104 return HalGetSerial();
105 }
106
GetSecurityPatchTag(void)107 const char *GetSecurityPatchTag(void)
108 {
109 return HalGetSecurityPatchTag();
110 }
111
GetAbiList(void)112 const char *GetAbiList(void)
113 {
114 return HalGetAbiList();
115 }
116
GetBootloaderVersion(void)117 const char *GetBootloaderVersion(void)
118 {
119 return HalGetBootloaderVersion();
120 }
121
GetOSName(void)122 static const char *GetOSName(void)
123 {
124 return HalGetOSName();
125 }
126
BuildOSFullName(void)127 static const char *BuildOSFullName(void)
128 {
129 const char release[] = "Release";
130 char value[OS_FULL_NAME_LEN] = {0};
131 const char *releaseType = GetOsReleaseType();
132 int length;
133 if (strncmp(releaseType, release, sizeof(release) - 1) == 0) {
134 length = sprintf_s(value, OS_FULL_NAME_LEN, "%s-%d.%d.%d.%d",
135 GetOSName(), GetMajorVersion(), GetSeniorVersion(), GetFeatureVersion(), GetBuildVersion());
136 } else {
137 length = sprintf_s(value, OS_FULL_NAME_LEN, "%s-%d.%d.%d.%d(%s)",
138 GetOSName(), GetMajorVersion(), GetSeniorVersion(), GetFeatureVersion(), GetBuildVersion(), releaseType);
139 }
140 if (length < 0) {
141 return EMPTY_STR;
142 }
143 const char *osFullName = strdup(value);
144 return osFullName;
145 }
146
GetOSFullName(void)147 const char *GetOSFullName(void)
148 {
149 static const char *osFullName = NULL;
150 if (osFullName != NULL) {
151 return osFullName;
152 }
153 osFullName = BuildOSFullName();
154 if (osFullName == NULL) {
155 return EMPTY_STR;
156 }
157 return osFullName;
158 }
159
GetSdkApiVersion(void)160 int GetSdkApiVersion(void)
161 {
162 return atoi(HalGetSdkApiVersion());
163 }
164
GetFirstApiVersion(void)165 int GetFirstApiVersion(void)
166 {
167 return HalGetFirstApiVersion();
168 }
169
GetDisplayVersion(void)170 const char *GetDisplayVersion(void)
171 {
172 return HalGetDisplayVersion();
173 }
174
GetIncrementalVersion(void)175 const char *GetIncrementalVersion(void)
176 {
177 return HalGetIncrementalVersion();
178 }
GetSdkApiLevel(void)179 static int GetSdkApiLevel(void)
180 {
181 return atoi(HalGetSdkApiLevel());
182 }
BuildVersionId(void)183 static const char *BuildVersionId(void)
184 {
185 char value[VERSION_ID_MAX_LEN] = {0};
186 int len = sprintf_s(value, VERSION_ID_MAX_LEN, "%s/%s/%s/%s/%s/%s/%s/%d/%s/%s",
187 GetDeviceType(), GetManufacture(), GetBrand(), GetProductSeries(),
188 GetOSFullName(), GetProductModel(), GetSoftwareModel(),
189 GetSdkApiLevel(), GetIncrementalVersion(), GetBuildType());
190 if (len < 0) {
191 return EMPTY_STR;
192 }
193 const char *versionId = strdup(value);
194 return versionId;
195 }
196
GetVersionId(void)197 const char *GetVersionId(void)
198 {
199 static const char *ohosVersionId = NULL;
200 if (ohosVersionId != NULL) {
201 return ohosVersionId;
202 }
203 ohosVersionId = BuildVersionId();
204 if (ohosVersionId == NULL) {
205 return EMPTY_STR;
206 }
207 return ohosVersionId;
208 }
209
GetBuildType(void)210 const char *GetBuildType(void)
211 {
212 return HalGetBuildType();
213 }
214
GetBuildUser(void)215 const char *GetBuildUser(void)
216 {
217 return HalGetBuildUser();
218 }
219
GetBuildHost(void)220 const char *GetBuildHost(void)
221 {
222 return HalGetBuildHost();
223 }
224
GetBuildTime(void)225 const char *GetBuildTime(void)
226 {
227 return HalGetBuildTime();
228 }
229
GetBuildRootHash(void)230 const char *GetBuildRootHash(void)
231 {
232 return HalGetBuildRootHash();
233 }
234
GetOsReleaseType(void)235 const char *GetOsReleaseType(void)
236 {
237 return HalGetOsReleaseType();
238 }
239
GetDevUdid(char * udid,int size)240 int GetDevUdid(char *udid, int size)
241 {
242 if (size < DEV_UUID_LENGTH) {
243 return EC_INVALID;
244 }
245 return HalGetDevUdid(udid, size);
246 }
247
FindParameter(const char * name)248 unsigned int FindParameter(const char *name)
249 {
250 if (name == NULL) {
251 return EC_INVALID;
252 }
253 return HalFindParameter(name);
254 }
255
GetParameterCommitId(unsigned int handle)256 unsigned int GetParameterCommitId(unsigned int handle)
257 {
258 return HalGetParameterCommitId(handle);
259 }
260
GetParameterName(unsigned int handle,char * name,unsigned int len)261 int GetParameterName(unsigned int handle, char *name, unsigned int len)
262 {
263 if (name == NULL) {
264 return EC_INVALID;
265 }
266 int ret = HalGetParameterName(handle, name, len);
267 return (ret != 0) ? EC_FAILURE : strlen(name);
268 }
269
GetParameterValue(unsigned int handle,char * value,unsigned int len)270 int GetParameterValue(unsigned int handle, char *value, unsigned int len)
271 {
272 if (value == NULL) {
273 return EC_INVALID;
274 }
275 int ret = HalGetParameterValue(handle, value, len);
276 return (ret != 0) ? EC_FAILURE : strlen(value);
277 }
278