• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-2024 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 "edm_parameter_utils.h"
17 
18 #include "netmgr_ext_log_wrapper.h"
19 #include "parameter.h"
20 
21 namespace OHOS {
22 namespace NetManagerStandard {
23 constexpr uint32_t PARAM_BUFFER_LENGTH = 128;
24 
GetInstance()25 EdmParameterUtils &EdmParameterUtils::GetInstance()
26 {
27     static EdmParameterUtils instance;
28     return instance;
29 }
30 
CheckBoolEdmParameter(const char * key,const char * defaultValue)31 bool EdmParameterUtils::CheckBoolEdmParameter(const char *key, const char *defaultValue)
32 {
33     std::lock_guard<std::recursive_mutex> guard(mutex_);
34     char paramOutBuf[PARAM_BUFFER_LENGTH] = {0};
35     int ret = GetParameter(key, defaultValue, paramOutBuf, PARAM_BUFFER_LENGTH);
36     NETMGR_EXT_LOG_I("NetworkShare StartSharing check EDM param %{public}d", ret);
37     if (ret > 0) {
38         if (strcmp(paramOutBuf, "true") == 0) {
39             return true;
40         } else {
41             NETMGR_EXT_LOG_E("NetworkShare StartSharing check EDM param result: %{public}s", paramOutBuf);
42         }
43     }
44     return false;
45 }
46 
RegisterEdmParameterChangeEvent(const char * key,ParameterChgPtr callback,void * context)47 void EdmParameterUtils::RegisterEdmParameterChangeEvent(const char *key, ParameterChgPtr callback, void *context)
48 {
49     std::lock_guard<std::recursive_mutex> guard(mutex_);
50     int ret = WatchParameter(key, callback, context);
51     if (ret != 0) {
52         NETMGR_EXT_LOG_E("RegisterEdmParameterChangeEvent %{public}s failed with %{public}d.",
53             key, ret);
54     }
55 }
56 
UnRegisterEdmParameterChangeEvent(const char * key)57 void EdmParameterUtils::UnRegisterEdmParameterChangeEvent(const char *key)
58 {
59     std::lock_guard<std::recursive_mutex> guard(mutex_);
60     int ret = RemoveParameterWatcher(key, nullptr, nullptr);
61     if (ret != 0) {
62         NETMGR_EXT_LOG_E("UnRegisterEdmParameterChangeEvent %{public}s err: %{public}d", key, ret);
63     }
64 }
65 
ConvertToInt64(const std::string & str,int64_t & value)66 bool EdmParameterUtils::ConvertToInt64(const std::string &str, int64_t &value)
67 {
68     char* end;
69     errno = 0; // 清除 errno
70     if (str.empty()) {
71         NETMGR_EXT_LOG_E("string error. str: %{public}s", str.c_str());
72         return false;
73     }
74 
75     value = std::strtoll(str.c_str(), &end, 10);  // 10:十进制
76 
77     // 检查错误:
78     // 1. 若没有数字被转换
79     if (end == str.c_str()) {
80         NETMGR_EXT_LOG_E("string error. str: %{public}s", str.c_str());
81         return false;
82     }
83     // 2. 若存在范围错误(过大或过小)
84     if (errno == ERANGE && (value == HUGE_VAL || value == HUGE_VALF || value == HUGE_VALL)) {
85         NETMGR_EXT_LOG_E("string error. str: %{public}s", str.c_str());
86         return false;
87     }
88     // 3. 若字符串包含非数字字符
89     if (*end != '\0') {
90         NETMGR_EXT_LOG_E("string error. str: %{public}s", str.c_str());
91         return false;
92     }
93 
94     return true;
95 }
96 
Constrain(int amount,int low,int high)97 uint64_t EdmParameterUtils::Constrain(int amount, int low, int high)
98 {
99     return (amount < low) ? low : (amount > high) ? high : amount;
100 }
101 } // namespace NetManagerStandard
102 } // namespace OHOS