param_hal.c 5.4 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 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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
/*
 * 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 = {};

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 {
        fd = UtilsFileOpen(path, O_RDONLY_FS, 0);
        if (fd < 0) {
            path = PARAM_PERSIST_SAVE_PATH;
            fd = UtilsFileOpen(path, O_RDONLY_FS, 0);
            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;
        int ret = UtilsFileStat(path, &fileSize);
        PARAM_CHECK(ret == 0, break, "Failed to get file state %s", path);
        buffer = malloc(fileSize);
        PARAM_CHECK(buffer != NULL, break, "Failed to get file");
        ret = UtilsFileRead(fd, buffer, fileSize);
        PARAM_CHECK(ret == 0, break, "Failed to get read file %s", path);

        uint32_t currLen = 0;
        while (currLen < fileSize) {
            if (buffer[currLen] == '\n') { // splite line
                buffer[currLen] = '\0';
                int ret = SpliteString(buffer, NULL, 0, LoadOnePersistParam_, NULL);
                PARAM_CHECK(ret == 0, continue, "Failed to set param %d %s", ret, buffer);
                paramNum++;
            }
            currLen++;
        }
    } while (0);
    if (fd > 0) {
        UtilsFileClose(fd);
    }
    if (buffer != NULL) {
        free(buffer);
    }
    PARAM_LOGI("LoadPersistParam from file %s paramNum %d", path, paramNum);
    return 0;
}

static int PersistWrite(int fd, const char *name, const char *value)
{
    int ret = UtilsFileWrite(fd, name, strlen(name));
    if (ret <= 0) {
        PARAM_LOGE("Failed to save persist param %s", name);
    }
    ret = UtilsFileWrite(fd, "=", strlen("="));
    if (ret <= 0) {
        PARAM_LOGE("Failed to save persist param %s", name);
    }
    ret = UtilsFileWrite(fd, value, strlen(value));
    if (ret <= 0) {
        PARAM_LOGE("Failed to save persist param %s", name);
    }
    ret = UtilsFileWrite(fd, "\n", strlen("\n"));
    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;
    int fd = UtilsFileOpen(PARAM_PERSIST_SAVE_PATH, O_RDWR_FS | O_CREAT_FS | O_APPEND_FS, 0);
    if (fd > 0) {
        ret = PersistWrite(fd, name, value);
        UtilsFileClose(fd);
    }
    ParamMutexPost(&g_saveMutex);
    return ret;
}

static int BatchSavePersistParamBegin(PERSIST_SAVE_HANDLE *handle)
{
    ParamMutexPend(&g_saveMutex);
    int fd = UtilsFileOpen(PARAM_PERSIST_SAVE_TMP_PATH, O_RDWR_FS | O_CREAT_FS | O_TRUNC_FS, 0);
    if (fd < 0) {
        ParamMutexPost(&g_saveMutex);
        PARAM_LOGE("Open file %s fail error %d", PARAM_PERSIST_SAVE_TMP_PATH, errno);
        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;
    UtilsFileClose(fd);
    UtilsFileDelete(PARAM_PERSIST_SAVE_PATH);
    ret = UtilsFileMove(PARAM_PERSIST_SAVE_TMP_PATH, PARAM_PERSIST_SAVE_PATH);
    ParamMutexPost(&g_saveMutex);
    PARAM_CHECK(ret == 0, return, "BatchSavePersistParamEnd %s fail error %d", PARAM_PERSIST_SAVE_TMP_PATH, errno);
}

int RegisterPersistParamOps(PersistParamOps *ops)
{
    PARAM_LOGI("RegisterPersistParamOps");
    ParamMutexCeate(&g_saveMutex);
    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;
}