1 /* 2 * Copyright (c) 2021 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 #ifndef BASE_STARTUP_PARAM_UTILS_H 17 #define BASE_STARTUP_PARAM_UTILS_H 18 #include <stddef.h> 19 #include <stdint.h> 20 21 #include "beget_ext.h" 22 #include "init_param.h" 23 #include "init_utils.h" 24 25 #ifdef __cplusplus 26 #if __cplusplus 27 extern "C" { 28 #endif 29 #endif 30 31 typedef enum { 32 PARAM_CODE_NOT_INIT = PARAM_CODE_MAX + 1, 33 PARAM_CODE_ERROR_MAP_FILE, 34 } PARAM_INNER_CODE; 35 36 #ifndef PARAM_BUFFER_MAX 37 #define PARAM_BUFFER_MAX (0x01 << 16) 38 #endif 39 40 #define FILENAME_LEN_MAX 255 41 #define MS_UNIT 1000 42 #ifndef UNUSED 43 #define UNUSED(x) (void)(x) 44 #endif 45 #define PARAM_ALIGN(len) (((len) + 0x03) & (~0x03)) 46 #define PARAM_IS_ALIGNED(x) (((x) & 0x03) == 0) 47 #define PARAM_ENTRY(ptr, type, member) (type *)((char *)(ptr)-offsetof(type, member)) 48 49 #define IS_READY_ONLY(name) \ 50 ((strncmp((name), "const.", strlen("const.")) == 0) || \ 51 (strncmp((name), "ro.", strlen("ro.")) == 0) || \ 52 (strncmp((name), "ohos.boot.", strlen("ohos.boot.")) == 0)) 53 54 #define PARAM_PERSIST_PREFIX "persist." 55 56 #define SYS_POWER_CTRL "ohos.startup.powerctrl=" 57 #define OHOS_CTRL_START "ohos.ctl.start=" 58 #define OHOS_CTRL_STOP "ohos.ctl.stop=" 59 #define OHOS_SERVICE_CTRL_PREFIX "ohos.servicectrl." 60 61 #ifdef STARTUP_INIT_TEST 62 #define PARAM_STATIC 63 #else 64 #define PARAM_STATIC static 65 #endif 66 67 #ifdef __LITEOS_M__ 68 #ifndef DATA_PATH 69 #define DATA_PATH "/" 70 #endif 71 #elif defined __LITEOS_A__ 72 #define DATA_PATH STARTUP_INIT_UT_PATH"/storage/data/system/param/" 73 #elif defined __LINUX__ 74 #define DATA_PATH STARTUP_INIT_UT_PATH"/storage/data/system/param/" 75 #else 76 #define DATA_PATH STARTUP_INIT_UT_PATH"/data/service/el1/startup/parameters/" 77 #endif 78 #define PARAM_AREA_SIZE_CFG STARTUP_INIT_UT_PATH"/etc/param/ohos.para.size" 79 80 #define CLIENT_PIPE_NAME "/dev/unix/socket/paramservice" 81 #define PIPE_NAME STARTUP_INIT_UT_PATH "/dev/unix/socket/paramservice" 82 #define PARAM_STORAGE_PATH STARTUP_INIT_UT_PATH "/dev/__parameters__" 83 #define PARAM_PERSIST_SAVE_PATH DATA_PATH "persist_parameters" 84 #define PARAM_PERSIST_SAVE_TMP_PATH DATA_PATH "tmp_persist_parameters" 85 86 #define WORKSPACE_FLAGS_INIT 0x01 87 #define WORKSPACE_FLAGS_LOADED 0x02 88 #define WORKSPACE_FLAGS_UPDATE 0x04 89 #define WORKSPACE_FLAGS_LABEL_LOADED 0x08 90 #define WORKSPACE_FLAGS_NEED_ACCESS 0x10 91 #define WORKSPACE_FLAGS_FOR_INIT 0x20 92 93 #define PARAM_SET_FLAG(node, flag) ((node) |= (flag)) 94 #define PARAM_CLEAR_FLAG(node, flag) ((node) &= ~(flag)) 95 #define PARAM_TEST_FLAG(node, flag) (((node) & (flag)) == (flag)) 96 97 #ifndef PARAN_DOMAIN 98 #define PARAN_DOMAIN (BASE_DOMAIN + 2) 99 #endif 100 #define PARAN_LABEL "PARAM" 101 #ifdef PARAM_BASE 102 INIT_LOCAL_API void ParamWorBaseLog(InitLogLevel logLevel, uint32_t domain, const char *tag, const char *fmt, ...); 103 #define PARAM_LOGV(fmt, ...) \ 104 ParamWorBaseLog(INIT_DEBUG, PARAN_DOMAIN, PARAN_LABEL, "[%s:%d]" fmt, (FILE_NAME), (__LINE__), ##__VA_ARGS__) 105 #define PARAM_LOGI(fmt, ...) \ 106 ParamWorBaseLog(INIT_INFO, PARAN_DOMAIN, PARAN_LABEL, "[%s:%d]" fmt, (FILE_NAME), (__LINE__), ##__VA_ARGS__) 107 #define PARAM_LOGW(fmt, ...) \ 108 ParamWorBaseLog(INIT_WARN, PARAN_DOMAIN, PARAN_LABEL, "[%s:%d]" fmt, (FILE_NAME), (__LINE__), ##__VA_ARGS__) 109 #define PARAM_LOGE(fmt, ...) \ 110 ParamWorBaseLog(INIT_ERROR, PARAN_DOMAIN, PARAN_LABEL, "[%s:%d]" fmt, (FILE_NAME), (__LINE__), ##__VA_ARGS__) 111 #else 112 #define PARAM_LOGI(fmt, ...) STARTUP_LOGI(PARAN_DOMAIN, PARAN_LABEL, fmt, ##__VA_ARGS__) 113 #define PARAM_LOGE(fmt, ...) STARTUP_LOGE(PARAN_DOMAIN, PARAN_LABEL, fmt, ##__VA_ARGS__) 114 #define PARAM_LOGV(fmt, ...) STARTUP_LOGV(PARAN_DOMAIN, PARAN_LABEL, fmt, ##__VA_ARGS__) 115 #define PARAM_LOGW(fmt, ...) STARTUP_LOGW(PARAN_DOMAIN, PARAN_LABEL, fmt, ##__VA_ARGS__) 116 #endif 117 118 #define PARAM_CHECK(retCode, exper, ...) \ 119 if (!(retCode)) { \ 120 PARAM_LOGE(__VA_ARGS__); \ 121 exper; \ 122 } 123 124 #define PARAM_ONLY_CHECK(retCode, exper) \ 125 if (!(retCode)) { \ 126 exper; \ 127 } 128 129 typedef int (*DUMP_PRINTF)(const char *fmt, ...); 130 #define PARAM_DUMP g_printf 131 #define MAX_LABEL_LEN 256 132 #define PARAM_BUFFER_SIZE 256 133 134 #define SUBSTR_INFO_NAME 0 135 #define SUBSTR_INFO_VALUE 1 136 #ifdef PARAM_SUPPORT_SELINUX 137 #define SUBSTR_INFO_LABEL 1 138 #define SUBSTR_INFO_DAC 2 139 #else 140 #define SUBSTR_INFO_LABEL 1 141 #define SUBSTR_INFO_DAC 1 142 #endif 143 144 INIT_LOCAL_API int SplitParamString(char *line, const char *exclude[], uint32_t count, 145 int (*result)(const uint32_t *context, const char *name, const char *value), const uint32_t *context); 146 147 #ifdef __cplusplus 148 #if __cplusplus 149 } 150 #endif 151 #endif 152 #endif