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
66 } // namespace NetManagerStandard
67 } // namespace OHOS