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 #include "ohos.systemParameterEnhance.proj.hpp"
16 #include "ohos.systemParameterEnhance.impl.hpp"
17 #include "taihe/runtime.hpp"
18 #include "stdexcept"
19
20 #include "beget_ext.h"
21 #include "parameter.h"
22 #include "sysparam_errno.h"
23
24 static constexpr int MAX_VALUE_LENGTH = PARAM_CONST_VALUE_LEN_MAX;
25
26 using namespace taihe;
27 // using namespace ohos::systemParameterEnhance::systemParameterEnhance;
28
29 namespace {
30 // To be implemented.
GetParam(std::string key,std::string def)31 string GetParam(std::string key, std::string def)
32 {
33 if (key.empty()) {
34 taihe::set_business_error(SYSPARAM_INVALID_INPUT, "input is invalid");
35 }
36 char value[MAX_VALUE_LENGTH] = {0};
37 int result = GetParameter(key.c_str(), def.c_str(), value, MAX_VALUE_LENGTH);
38 if (result < 0) {
39 taihe::set_business_error(result, "getSync failed");
40 }
41 return value;
42 }
43
getSync(string_view key,optional_view<string> def)44 string getSync(string_view key, optional_view<string> def)
45 {
46 string defValue = def.value_or("");
47 return GetParam(std::string(key), std::string(defValue));
48 }
49
getParam(string_view key,optional_view<string> def)50 string getParam(string_view key, optional_view<string> def)
51 {
52 string defValue = def.value_or("");
53 return GetParam(std::string(key), std::string(defValue));
54 }
55
getParamNodef(string_view key)56 string getParamNodef(string_view key)
57 {
58 return GetParam(std::string(key), nullptr);
59 }
60
SetParam(std::string key,std::string value)61 void SetParam(std::string key, std::string value)
62 {
63 if (key.empty() || value.empty()) {
64 taihe::set_business_error(SYSPARAM_INVALID_INPUT, "input is invalid");
65 }
66 if (value.size() >= MAX_VALUE_LENGTH) {
67 taihe::set_business_error(SYSPARAM_INVALID_VALUE, "input value is invalid");
68 }
69 int result = SetParameter(key.c_str(), value.c_str());
70 if (result < 0) {
71 taihe::set_business_error(result, "set failed");
72 }
73 return;
74 }
setSync(string_view key,string_view value)75 void setSync(string_view key, string_view value)
76 {
77 SetParam(std::string(key), std::string(value));
78 }
79
setParam(string_view key,string_view value)80 void setParam(string_view key, string_view value)
81 {
82 SetParam(std::string(key), std::string(value));
83 }
84 } // namespace
85
86 TH_EXPORT_CPP_API_getSync(getSync);
87 TH_EXPORT_CPP_API_getParam(getParam);
88 TH_EXPORT_CPP_API_getParamNodef(getParamNodef);
89 TH_EXPORT_CPP_API_setSync(setSync);
90 TH_EXPORT_CPP_API_setParam(setParam);
91