1d9f0492fSopenharmony_ci/* 2d9f0492fSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 3d9f0492fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4d9f0492fSopenharmony_ci * you may not use this file except in compliance with the License. 5d9f0492fSopenharmony_ci * You may obtain a copy of the License at 6d9f0492fSopenharmony_ci * 7d9f0492fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8d9f0492fSopenharmony_ci * 9d9f0492fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10d9f0492fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11d9f0492fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12d9f0492fSopenharmony_ci * See the License for the specific language governing permissions and 13d9f0492fSopenharmony_ci * limitations under the License. 14d9f0492fSopenharmony_ci */ 15d9f0492fSopenharmony_ci#include "init_log.h" 16d9f0492fSopenharmony_ci#include "init_param.h" 17d9f0492fSopenharmony_ci#include "param_init.h" 18d9f0492fSopenharmony_ci#include "param_manager.h" 19d9f0492fSopenharmony_ci 20d9f0492fSopenharmony_ci#define MIN_SLEEP (100 * 1000) 21d9f0492fSopenharmony_ci 22d9f0492fSopenharmony_ci#ifdef __LITEOS_A__ 23d9f0492fSopenharmony_cistatic int g_flags = 0; 24d9f0492fSopenharmony_ci__attribute__((constructor)) static int ClientInit(void) 25d9f0492fSopenharmony_ci{ 26d9f0492fSopenharmony_ci if (PARAM_TEST_FLAG(g_flags, WORKSPACE_FLAGS_INIT)) { 27d9f0492fSopenharmony_ci return 0; 28d9f0492fSopenharmony_ci } 29d9f0492fSopenharmony_ci EnableInitLog(INIT_INFO); 30d9f0492fSopenharmony_ci PARAM_LOGV("InitParamClient"); 31d9f0492fSopenharmony_ci int ret = InitParamWorkSpace(1, NULL); 32d9f0492fSopenharmony_ci PARAM_CHECK(ret == 0, return -1, "Failed to init param workspace"); 33d9f0492fSopenharmony_ci PARAM_SET_FLAG(g_flags, WORKSPACE_FLAGS_INIT); 34d9f0492fSopenharmony_ci // init persist to save 35d9f0492fSopenharmony_ci InitPersistParamWorkSpace(); 36d9f0492fSopenharmony_ci return 0; 37d9f0492fSopenharmony_ci} 38d9f0492fSopenharmony_ci 39d9f0492fSopenharmony_ci__attribute__((destructor)) static void ClientDeinit(void) 40d9f0492fSopenharmony_ci{ 41d9f0492fSopenharmony_ci if (PARAM_TEST_FLAG(g_flags, WORKSPACE_FLAGS_INIT)) { 42d9f0492fSopenharmony_ci CloseParamWorkSpace(); 43d9f0492fSopenharmony_ci } 44d9f0492fSopenharmony_ci PARAM_SET_FLAG(g_flags, 0); 45d9f0492fSopenharmony_ci} 46d9f0492fSopenharmony_ci#endif 47d9f0492fSopenharmony_ci 48d9f0492fSopenharmony_ciint SystemSetParameter(const char *name, const char *value) 49d9f0492fSopenharmony_ci{ 50d9f0492fSopenharmony_ci PARAM_CHECK(name != NULL && value != NULL, return -1, "Invalid name or value %s", name); 51d9f0492fSopenharmony_ci int ctrlService = 0; 52d9f0492fSopenharmony_ci int ret = CheckParameterSet(name, value, GetParamSecurityLabel(), &ctrlService); 53d9f0492fSopenharmony_ci PARAM_CHECK(ret == 0, return ret, "Forbid to set parameter %s", name); 54d9f0492fSopenharmony_ci PARAM_LOGV("SystemSetParameter name %s value: %s ctrlService %d", name, value, ctrlService); 55d9f0492fSopenharmony_ci if ((ctrlService & PARAM_CTRL_SERVICE) != PARAM_CTRL_SERVICE) { // ctrl param 56d9f0492fSopenharmony_ci uint32_t dataIndex = 0; 57d9f0492fSopenharmony_ci ret = WriteParam(name, value, &dataIndex, 0); 58d9f0492fSopenharmony_ci PARAM_CHECK(ret == 0, return ret, "Failed to set param %d name %s %s", ret, name, value); 59d9f0492fSopenharmony_ci ret = WritePersistParam(name, value); 60d9f0492fSopenharmony_ci PARAM_CHECK(ret == 0, return ret, "Failed to set persist param name %s", name); 61d9f0492fSopenharmony_ci } else { 62d9f0492fSopenharmony_ci PARAM_LOGE("SystemSetParameter can not support service ctrl parameter name %s", name); 63d9f0492fSopenharmony_ci } 64d9f0492fSopenharmony_ci return ret; 65d9f0492fSopenharmony_ci} 66d9f0492fSopenharmony_ci 67d9f0492fSopenharmony_ciint SystemWaitParameter(const char *name, const char *value, int32_t timeout) 68d9f0492fSopenharmony_ci{ 69d9f0492fSopenharmony_ci PARAM_CHECK(name != NULL && value != NULL, return PARAM_CODE_INVALID_PARAM, 70d9f0492fSopenharmony_ci "SystemWaitParameter failed! name is: %s, errNum is: %d", name, PARAM_CODE_INVALID_PARAM); 71d9f0492fSopenharmony_ci long long globalCommit = 0; 72d9f0492fSopenharmony_ci // first check permission 73d9f0492fSopenharmony_ci int ret = CheckParamPermission(GetParamSecurityLabel(), name, DAC_READ); 74d9f0492fSopenharmony_ci PARAM_CHECK(ret == 0, return ret, "SystemWaitParameter failed! name is: %s, errNum is: %d", name, ret); 75d9f0492fSopenharmony_ci uint32_t diff = 0; 76d9f0492fSopenharmony_ci struct timespec startTime = {0}; 77d9f0492fSopenharmony_ci if (timeout <= 0) { 78d9f0492fSopenharmony_ci timeout = DEFAULT_PARAM_WAIT_TIMEOUT; 79d9f0492fSopenharmony_ci } 80d9f0492fSopenharmony_ci (void)clock_gettime(CLOCK_MONOTONIC, &startTime); 81d9f0492fSopenharmony_ci do { 82d9f0492fSopenharmony_ci long long commit = GetSystemCommitId(); 83d9f0492fSopenharmony_ci if (commit != globalCommit) { 84d9f0492fSopenharmony_ci ParamNode *param = SystemCheckMatchParamWait(name, value); 85d9f0492fSopenharmony_ci if (param != NULL) { 86d9f0492fSopenharmony_ci ret = 0; 87d9f0492fSopenharmony_ci break; 88d9f0492fSopenharmony_ci } 89d9f0492fSopenharmony_ci } 90d9f0492fSopenharmony_ci globalCommit = commit; 91d9f0492fSopenharmony_ci 92d9f0492fSopenharmony_ci usleep(MIN_SLEEP); 93d9f0492fSopenharmony_ci struct timespec finishTime = {0}; 94d9f0492fSopenharmony_ci (void)clock_gettime(CLOCK_MONOTONIC, &finishTime); 95d9f0492fSopenharmony_ci diff = IntervalTime(&finishTime, &startTime); 96d9f0492fSopenharmony_ci if (diff >= timeout) { 97d9f0492fSopenharmony_ci ret = PARAM_CODE_TIMEOUT; 98d9f0492fSopenharmony_ci break; 99d9f0492fSopenharmony_ci } 100d9f0492fSopenharmony_ci } while (1); 101d9f0492fSopenharmony_ci PARAM_LOGI("SystemWaitParameter name %s value: %s diff %d timeout %d", name, value, diff, diff); 102d9f0492fSopenharmony_ci if (ret != 0) { 103d9f0492fSopenharmony_ci PARAM_LOGE("SystemWaitParameter failed! name is:%s, errNum is %d", name, ret); 104d9f0492fSopenharmony_ci } 105d9f0492fSopenharmony_ci return ret; 106d9f0492fSopenharmony_ci} 107d9f0492fSopenharmony_ci 108d9f0492fSopenharmony_ciint SystemSaveParameters(void) 109d9f0492fSopenharmony_ci{ 110d9f0492fSopenharmony_ci CheckAndSavePersistParam(); 111d9f0492fSopenharmony_ci return PARAM_CODE_SUCCESS; 112d9f0492fSopenharmony_ci} 113d9f0492fSopenharmony_ci 114d9f0492fSopenharmony_ciint WatchParamCheck(const char *keyprefix) 115d9f0492fSopenharmony_ci{ 116d9f0492fSopenharmony_ci (void)keyprefix; 117d9f0492fSopenharmony_ci return PARAM_CODE_NOT_SUPPORT; 118d9f0492fSopenharmony_ci} 119d9f0492fSopenharmony_ci 120d9f0492fSopenharmony_ciint SystemCheckParamExist(const char *name) 121d9f0492fSopenharmony_ci{ 122d9f0492fSopenharmony_ci return SysCheckParamExist(name); 123d9f0492fSopenharmony_ci} 124