• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "system_param_manager.h"
17 
18 #include "iam_check.h"
19 #include "iam_logger.h"
20 
21 #define LOG_TAG "PIN_AUTH_SA"
22 
23 namespace OHOS {
24 namespace UserIam {
25 namespace PinAuth {
26 namespace {
OnParamChg(const char * key,const char * value,void * context)27 void OnParamChg(const char *key, const char *value, void *context)
28 {
29     IF_FALSE_LOGE_AND_RETURN(key != nullptr);
30     IF_FALSE_LOGE_AND_RETURN(value != nullptr);
31     SystemParamManager::GetInstance().OnParamChange(std::string(key), std::string(value));
32 }
33 } // namespace
34 
GetInstance()35 SystemParamManager &SystemParamManager::GetInstance()
36 {
37     static SystemParamManager instance;
38     return instance;
39 }
40 
GetParam(const std::string & key,const std::string & defaultValue)41 std::string SystemParamManager::GetParam(const std::string &key, const std::string &defaultValue)
42 {
43     constexpr uint32_t MAX_VALUE_LEN = 128;
44     char valueBuffer[MAX_VALUE_LEN] = { 0 };
45     int32_t ret = GetParameter(key.c_str(), defaultValue.c_str(), valueBuffer, MAX_VALUE_LEN);
46     if (ret < 0) {
47         IAM_LOGE("get param failed, key %{public}s, ret %{public}d, use default value %{public}s", key.c_str(), ret,
48             defaultValue.c_str());
49         return defaultValue;
50     }
51     IAM_LOGI("get param key %{public}s value %{public}s", key.c_str(), valueBuffer);
52     return std::string(valueBuffer);
53 }
54 
SetParam(const std::string & key,const std::string & value)55 void SystemParamManager::SetParam(const std::string &key, const std::string &value)
56 {
57     std::string currentValue = GetParam(key, "");
58     IAM_LOGI("set parameter: %{public}s, current value: %{public}s, value: %{public}s", key.c_str(),
59         currentValue.c_str(), value.c_str());
60     if (currentValue != value) {
61         int32_t ret = SetParameter(key.c_str(), value.c_str());
62         IF_FALSE_LOGE_AND_RETURN(ret == 0);
63     }
64 }
65 
SetParamTwice(const std::string & key,const std::string & value1,const std::string & value2)66 void SystemParamManager::SetParamTwice(const std::string &key, const std::string &value1, const std::string &value2)
67 {
68     std::string currentValue = GetParam(key, "");
69     IAM_LOGI("set parameter: %{public}s, current value: %{public}s, value1: %{public}s, value2: %{public}s",
70         key.c_str(), currentValue.c_str(), value1.c_str(), value2.c_str());
71     if (currentValue != value1) {
72         int32_t ret1 = SetParameter(key.c_str(), value1.c_str());
73         IF_FALSE_LOGE_AND_RETURN(ret1 == 0);
74     }
75     int32_t ret2 = SetParameter(key.c_str(), value2.c_str());
76     IF_FALSE_LOGE_AND_RETURN(ret2 == 0);
77 }
78 
WatchParam(const std::string & key,SystemParamCallback callback)79 void SystemParamManager::WatchParam(const std::string &key, SystemParamCallback callback)
80 {
81     IF_FALSE_LOGE_AND_RETURN(callback != nullptr);
82 
83     std::lock_guard<std::recursive_mutex> lock(mutex_);
84     bool alreadyWatched = std::find_if(keyCallbackVec_.begin(), keyCallbackVec_.end(),
85         [&key](const auto &item) { return item.first == key; }) != keyCallbackVec_.end();
86     if (!alreadyWatched) {
87         int32_t ret = WatchParameter(key.c_str(), OnParamChg, nullptr);
88         IF_FALSE_LOGE_AND_RETURN(ret == 0);
89     }
90 
91     bool hasSameCallback =
92         std::find_if(keyCallbackVec_.begin(), keyCallbackVec_.end(), [&key, &callback](const auto &item) {
93             return item.first == key && item.second == callback;
94         }) != keyCallbackVec_.end();
95     if (hasSameCallback) {
96         IAM_LOGE("key %{public}s already watched with same callback", key.c_str());
97         return;
98     }
99     keyCallbackVec_.push_back(std::make_pair(key, callback));
100     IAM_LOGI("watch key %{public}s", key.c_str());
101 }
102 
OnParamChange(const std::string & key,const std::string & value)103 void SystemParamManager::OnParamChange(const std::string &key, const std::string &value)
104 {
105     IAM_LOGI("on param change, key %{public}s, value %{public}s", key.c_str(), value.c_str());
106     std::vector<std::pair<std::string, SystemParamCallback>> keyCallbackVec;
107     {
108         std::lock_guard<std::recursive_mutex> lock(mutex_);
109         keyCallbackVec = keyCallbackVec_;
110     }
111 
112     for (const auto &item : keyCallbackVec) {
113         if (item.first == key && item.second != nullptr) {
114             item.second(value);
115         }
116     }
117 }
118 } // namespace PinAuth
119 } // namespace UserIam
120 } // namespace OHOS
121