• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_comm.h"
17 
18 #include <stdlib.h>
19 #include <string.h>
20 
21 #include "init_param.h"
22 #include "parameter.h"
23 #include "sysparam_errno.h"
24 #include "securec.h"
25 #include "beget_ext.h"
26 
GetSystemError(int err)27 INIT_LOCAL_API int GetSystemError(int err)
28 {
29     switch (err) {
30         case 0:
31             return 0;
32         case PARAM_CODE_INVALID_PARAM:
33         case PARAM_CODE_INVALID_NAME:
34         case PARAM_CODE_READ_ONLY:
35             return EC_INVALID;
36         case PARAM_CODE_INVALID_VALUE:
37             return SYSPARAM_INVALID_VALUE;
38         case PARAM_CODE_NOT_FOUND:
39         case PARAM_CODE_NODE_EXIST:
40             return SYSPARAM_NOT_FOUND;
41         case DAC_RESULT_FORBIDED:
42             return SYSPARAM_PERMISSION_DENIED;
43         case PARAM_CODE_REACHED_MAX:
44         case PARAM_CODE_FAIL_CONNECT:
45         case PARAM_CODE_INVALID_SOCKET:
46         case PARAM_CODE_NOT_SUPPORT:
47             return SYSPARAM_SYSTEM_ERROR;
48         case PARAM_CODE_TIMEOUT:
49             return SYSPARAM_WAIT_TIMEOUT;
50         default:
51             return SYSPARAM_SYSTEM_ERROR;
52     }
53 }
54 
IsValidParamValue(const char * value,uint32_t len)55 INIT_LOCAL_API int IsValidParamValue(const char *value, uint32_t len)
56 {
57     if ((value == NULL) || (strlen(value) + 1 > len)) {
58         return 0;
59     }
60     return 1;
61 }
62 
GetParameter_(const char * key,const char * def,char * value,uint32_t len)63 INIT_LOCAL_API int GetParameter_(const char *key, const char *def, char *value, uint32_t len)
64 {
65     if ((key == NULL) || (value == NULL) || (len > (uint32_t)PARAM_BUFFER_MAX)) {
66         return EC_INVALID;
67     }
68     uint32_t size = len;
69     int ret = SystemGetParameter(key, NULL, &size);
70     if (ret != 0) {
71         if (def == NULL) {
72             return GetSystemError(ret);
73         }
74         if (strlen(def) > len) {
75             return EC_INVALID;
76         }
77         ret = strcpy_s(value, len, def);
78         return (ret == 0) ? 0 : EC_FAILURE;
79     } else if (size > len) {
80         return EC_INVALID;
81     }
82 
83     size = len;
84     ret = SystemGetParameter(key, value, &size);
85     return GetSystemError(ret);
86 }
87 
88 static PropertyValueProcessor g_propertyGetProcessor = NULL;
89 
GetProperty(const char * key,const char ** paramHolder)90 INIT_LOCAL_API const char *GetProperty(const char *key, const char **paramHolder)
91 {
92     BEGET_CHECK(paramHolder != NULL, return NULL);
93     if (*paramHolder != NULL) {
94         return *paramHolder;
95     }
96     uint32_t len = 0;
97     int ret = SystemGetParameter(key, NULL, &len);
98     if (ret == 0 && len > 0) {
99         char *res = (char *)malloc(len + 1);
100         BEGET_CHECK(res != NULL, return NULL);
101         ret = SystemGetParameter(key, res, &len);
102         if (ret != 0) {
103             free(res);
104             return NULL;
105         }
106         if (g_propertyGetProcessor != NULL) {
107             res = g_propertyGetProcessor(key, res);
108         }
109         *paramHolder = res;
110     }
111     return *paramHolder;
112 }
113 
SetPropertyGetProcessor(PropertyValueProcessor processor)114 INIT_LOCAL_API PropertyValueProcessor SetPropertyGetProcessor(PropertyValueProcessor processor)
115 {
116     PropertyValueProcessor prev = g_propertyGetProcessor;
117     g_propertyGetProcessor = processor;
118     return prev;
119 }
120 
GetProductModel_(void)121 INIT_LOCAL_API const char *GetProductModel_(void)
122 {
123     static const char *productModel = NULL;
124     return GetProperty("const.product.model", &productModel);
125 }
126 
GetManufacture_(void)127 INIT_LOCAL_API const char *GetManufacture_(void)
128 {
129     static const char *productManufacture = NULL;
130     return GetProperty("const.product.manufacturer", &productManufacture);
131 }
132 
GetFullName_(void)133 INIT_LOCAL_API const char *GetFullName_(void)
134 {
135     static const char *fillname = NULL;
136     return GetProperty("const.ohos.fullname", &fillname);
137 }
138