param_hal.c 7.0 KB
Newer Older
M
Mupceet 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <time.h>
#include <unistd.h>

#include "init_utils.h"
#include "param_manager.h"
#include "param_persist.h"
#include "param_utils.h"
#include "securec.h"
#include "utils_file.h"

// for linux, no mutex
王liangliang's avatar
王liangliang 已提交
30
static ParamMutex g_saveMutex = {0};
M
Mupceet 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
#ifdef PARAM_SUPPORT_POSIX
#define MODE_READ O_RDONLY
#define MODE_APPEND (O_RDWR | O_CREAT | O_APPEND)
#define MODE_CREATE (O_RDWR | O_CREAT | O_TRUNC)
#else
#define MODE_READ O_RDONLY_FS
#define MODE_APPEND (O_RDWR_FS | O_CREAT_FS | O_APPEND_FS)
#define MODE_CREATE (O_RDWR_FS | O_CREAT_FS | O_TRUNC_FS)
#endif

static int ParamFileOpen(const char* path, int oflag, int mode)
{
#ifdef PARAM_SUPPORT_POSIX
    return open(path, oflag, mode);
#else
    return UtilsFileOpen(path, oflag, mode);
#endif
}

static int ParamFileClose(int fd)
{
#ifdef PARAM_SUPPORT_POSIX
    return close(fd);
#else
    return UtilsFileClose(fd);
#endif
}

static int ParamFileRead(int fd, char* buf, unsigned int len)
{
#ifdef PARAM_SUPPORT_POSIX
    return read(fd, buf, len);
#else
    return UtilsFileRead(fd, buf, len);
#endif
}

static int ParamFileWrite(int fd, const char* buf, unsigned int len)
{
#ifdef PARAM_SUPPORT_POSIX
    return write(fd, buf, len);
#else
    return UtilsFileWrite(fd, buf, len);
#endif
}

static int ParamFileDelete(const char* path)
{
#ifdef PARAM_SUPPORT_POSIX
    return unlink(path);
#else
    return UtilsFileDelete(path);
#endif
}

static int ParamFileStat(const char* path, unsigned int* fileSize)
{
#ifdef PARAM_SUPPORT_POSIX
    int fd = open(path, O_RDONLY);
    if (fd < 0) {
        return -1;
    }
	*fileSize = lseek(fd, 0, SEEK_END);
    lseek(fd, 0, SEEK_SET);
    return 0;
#else
    return UtilsFileStat(path, fileSize);
#endif
}

static void ParamFileSync(int ft)
{
#ifdef PARAM_SUPPORT_POSIX
    fsync(ft);
#else
106
    (void)ft;
M
Mupceet 已提交
107 108
#endif
}
M
Mupceet 已提交
109 110 111

static int LoadOnePersistParam_(const uint32_t *context, const char *name, const char *value)
{
112
    (void)context;
M
Mupceet 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126
    uint32_t dataIndex = 0;
    int ret = WriteParam(name, value, &dataIndex, 0);
    PARAM_CHECK(ret == 0, return ret, "Failed to write param %d name:%s %s", ret, name, value);
    return 0;
}

static int LoadPersistParam(void)
{
    const char *path = PARAM_PERSIST_SAVE_TMP_PATH;
    CheckAndCreateDir(path);
    char *buffer = NULL;
    int fd = -1;
    uint32_t paramNum = 0;
    do {
M
Mupceet 已提交
127
        fd = ParamFileOpen(path, MODE_READ, 0);
M
Mupceet 已提交
128 129
        if (fd < 0) {
            path = PARAM_PERSIST_SAVE_PATH;
M
Mupceet 已提交
130
            fd = ParamFileOpen(path, MODE_READ, 0);
M
Mupceet 已提交
131 132 133 134 135
            PARAM_LOGI("LoadPersistParam open file %s", path);
        }
        PARAM_CHECK(fd >= 0, break, "No valid persist parameter file %s", path);
        // read file
        uint32_t fileSize = 0;
M
Mupceet 已提交
136
        int ret = ParamFileStat(path, &fileSize);
M
Mupceet 已提交
137 138 139
        PARAM_CHECK(ret == 0, break, "Failed to get file state %s", path);
        buffer = malloc(fileSize);
        PARAM_CHECK(buffer != NULL, break, "Failed to get file");
M
Mupceet 已提交
140
        ret = ParamFileRead(fd, buffer, fileSize);
C
Fix bug  
cheng_jinsong 已提交
141
        PARAM_CHECK(ret >= 0, break, "Failed to read file %s", path);
M
Mupceet 已提交
142 143

        uint32_t currLen = 0;
144
        char *tmp = buffer;
M
Mupceet 已提交
145
        while (currLen < fileSize) {
146
            if (buffer[currLen] == '\n') { // split line
M
Mupceet 已提交
147
                buffer[currLen] = '\0';
148
                ret = SplitParamString(tmp, NULL, 0, LoadOnePersistParam_, NULL);
M
Mupceet 已提交
149 150
                PARAM_CHECK(ret == 0, continue, "Failed to set param %d %s", ret, buffer);
                paramNum++;
151 152 153 154
                if (currLen + 1 >= fileSize) {
                    break;
                }
                tmp = buffer + currLen + 1;
M
Mupceet 已提交
155 156 157 158 159
            }
            currLen++;
        }
    } while (0);
    if (fd > 0) {
M
Mupceet 已提交
160
        ParamFileClose(fd);
M
Mupceet 已提交
161 162 163 164
    }
    if (buffer != NULL) {
        free(buffer);
    }
M
Mupceet 已提交
165
    PARAM_LOGI("LoadPersistParam paramNum %d", paramNum);
M
Mupceet 已提交
166 167 168 169 170
    return 0;
}

static int PersistWrite(int fd, const char *name, const char *value)
{
M
Mupceet 已提交
171
    int ret = ParamFileWrite(fd, name, strlen(name));
M
Mupceet 已提交
172 173 174
    if (ret <= 0) {
        PARAM_LOGE("Failed to save persist param %s", name);
    }
M
Mupceet 已提交
175
    ret = ParamFileWrite(fd, "=", strlen("="));
M
Mupceet 已提交
176 177 178
    if (ret <= 0) {
        PARAM_LOGE("Failed to save persist param %s", name);
    }
M
Mupceet 已提交
179
    ret = ParamFileWrite(fd, value, strlen(value));
M
Mupceet 已提交
180 181 182
    if (ret <= 0) {
        PARAM_LOGE("Failed to save persist param %s", name);
    }
M
Mupceet 已提交
183
    ret = ParamFileWrite(fd, "\n", strlen("\n"));
M
Mupceet 已提交
184 185 186 187 188 189 190 191 192 193
    if (ret <= 0) {
        PARAM_LOGE("Failed to save persist param %s", name);
    }
    return 0;
}

static int SavePersistParam(const char *name, const char *value)
{
    ParamMutexPend(&g_saveMutex);
    int ret = -1;
M
Mupceet 已提交
194
    int fd = ParamFileOpen(PARAM_PERSIST_SAVE_PATH, MODE_APPEND, 0);
M
Mupceet 已提交
195 196
    if (fd > 0) {
        ret = PersistWrite(fd, name, value);
M
Mupceet 已提交
197 198 199 200 201
        ParamFileSync(fd);
        ParamFileClose(fd);
    }
    if (ret != 0) {
        PARAM_LOGE("SavePersistParam %s errno %d", name, errno);
M
Mupceet 已提交
202 203 204 205 206 207 208 209
    }
    ParamMutexPost(&g_saveMutex);
    return ret;
}

static int BatchSavePersistParamBegin(PERSIST_SAVE_HANDLE *handle)
{
    ParamMutexPend(&g_saveMutex);
M
Mupceet 已提交
210
    int fd = ParamFileOpen(PARAM_PERSIST_SAVE_PATH, MODE_CREATE, 0);
M
Mupceet 已提交
211 212
    if (fd < 0) {
        ParamMutexPost(&g_saveMutex);
M
Mupceet 已提交
213
        PARAM_LOGE("Open file %s fail error %d", PARAM_PERSIST_SAVE_PATH, errno);
M
Mupceet 已提交
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
        return -1;
    }
    *handle = (PERSIST_SAVE_HANDLE)fd;
    return 0;
}

static int BatchSavePersistParam(PERSIST_SAVE_HANDLE handle, const char *name, const char *value)
{
    int fd = (int)handle;
    int ret = PersistWrite(fd, name, value);
    PARAM_CHECK(ret == 0, return -1, "Failed to write param %s", name);
    PARAM_LOGV("BatchSavePersistParam %s=%s", name, value);
    return 0;
}

static void BatchSavePersistParamEnd(PERSIST_SAVE_HANDLE handle)
{
    int ret;
    int fd = (int)handle;
M
Mupceet 已提交
233
    ParamFileSync(fd);
234
    ret = ParamFileClose(fd);
M
Mupceet 已提交
235
    ParamMutexPost(&g_saveMutex);
M
Mupceet 已提交
236
    PARAM_CHECK(ret == 0, return, "BatchSavePersistParamEnd fail error %d", errno);
M
Mupceet 已提交
237 238 239 240
}

int RegisterPersistParamOps(PersistParamOps *ops)
{
M
Mupceet 已提交
241
    ParamMutexCreate(&g_saveMutex);
M
Mupceet 已提交
242 243 244 245 246 247 248
    PARAM_CHECK(ops != NULL, return -1, "Invalid ops");
    ops->save = SavePersistParam;
    ops->load = LoadPersistParam;
    ops->batchSaveBegin = BatchSavePersistParamBegin;
    ops->batchSave = BatchSavePersistParam;
    ops->batchSaveEnd = BatchSavePersistParamEnd;
    return 0;
249
}