param_hal.c 6.8 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 30
/*
 * 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
static ParamMutex g_saveMutex = {};
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 106 107
#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
#endif
}
M
Mupceet 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124

static int LoadOnePersistParam_(const uint32_t *context, const char *name, const char *value)
{
    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 已提交
125
        fd = ParamFileOpen(path, MODE_READ, 0);
M
Mupceet 已提交
126 127
        if (fd < 0) {
            path = PARAM_PERSIST_SAVE_PATH;
M
Mupceet 已提交
128
            fd = ParamFileOpen(path, MODE_READ, 0);
M
Mupceet 已提交
129 130 131 132 133
            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 已提交
134
        int ret = ParamFileStat(path, &fileSize);
M
Mupceet 已提交
135 136 137
        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 已提交
138
        ret = ParamFileRead(fd, buffer, fileSize);
M
Mupceet 已提交
139 140 141 142
        PARAM_CHECK(ret == 0, break, "Failed to get read file %s", path);

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

static int PersistWrite(int fd, const char *name, const char *value)
{
M
Mupceet 已提交
164
    int ret = ParamFileWrite(fd, name, strlen(name));
M
Mupceet 已提交
165 166 167
    if (ret <= 0) {
        PARAM_LOGE("Failed to save persist param %s", name);
    }
M
Mupceet 已提交
168
    ret = ParamFileWrite(fd, "=", strlen("="));
M
Mupceet 已提交
169 170 171
    if (ret <= 0) {
        PARAM_LOGE("Failed to save persist param %s", name);
    }
M
Mupceet 已提交
172
    ret = ParamFileWrite(fd, value, strlen(value));
M
Mupceet 已提交
173 174 175
    if (ret <= 0) {
        PARAM_LOGE("Failed to save persist param %s", name);
    }
M
Mupceet 已提交
176
    ret = ParamFileWrite(fd, "\n", strlen("\n"));
M
Mupceet 已提交
177 178 179 180 181 182 183 184 185 186
    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 已提交
187
    int fd = ParamFileOpen(PARAM_PERSIST_SAVE_PATH, MODE_APPEND, 0);
M
Mupceet 已提交
188 189
    if (fd > 0) {
        ret = PersistWrite(fd, name, value);
M
Mupceet 已提交
190 191 192 193 194
        ParamFileSync(fd);
        ParamFileClose(fd);
    }
    if (ret != 0) {
        PARAM_LOGE("SavePersistParam %s errno %d", name, errno);
M
Mupceet 已提交
195 196 197 198 199 200 201 202
    }
    ParamMutexPost(&g_saveMutex);
    return ret;
}

static int BatchSavePersistParamBegin(PERSIST_SAVE_HANDLE *handle)
{
    ParamMutexPend(&g_saveMutex);
M
Mupceet 已提交
203
    int fd = ParamFileOpen(PARAM_PERSIST_SAVE_PATH, MODE_CREATE, 0);
M
Mupceet 已提交
204 205
    if (fd < 0) {
        ParamMutexPost(&g_saveMutex);
M
Mupceet 已提交
206
        PARAM_LOGE("Open file %s fail error %d", PARAM_PERSIST_SAVE_PATH, errno);
M
Mupceet 已提交
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
        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 已提交
226 227
    ParamFileSync(fd);
    ParamFileClose(fd);
M
Mupceet 已提交
228
    ParamMutexPost(&g_saveMutex);
M
Mupceet 已提交
229
    PARAM_CHECK(ret == 0, return, "BatchSavePersistParamEnd fail error %d", errno);
M
Mupceet 已提交
230 231 232 233
}

int RegisterPersistParamOps(PersistParamOps *ops)
{
M
Mupceet 已提交
234
    ParamMutexCreate(&g_saveMutex);
M
Mupceet 已提交
235 236 237 238 239 240 241
    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;
242
}