ueventd_parameter.c 4.8 KB
Newer Older
X
xionglei6 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * 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.
 */
X
xionglei6 已提交
15 16
#include "ueventd_parameter.h"

X
xionglei6 已提交
17 18 19
#include <errno.h>
#include <pthread.h>
#include <sys/time.h>
X
xionglei6 已提交
20

X
xionglei6 已提交
21 22 23
#define INIT_LOG_TAG "ueventd"
#include "init_log.h"
#include "list.h"
M
Mupceet 已提交
24
#include "init_param.h"
X
xionglei6 已提交
25
#include "ueventd.h"
X
xionglei6 已提交
26 27 28
#include "ueventd_read_cfg.h"
#include "ueventd_utils.h"

X
xionglei6 已提交
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
typedef struct {
    int inited;
    int shutdown;
    int empty;
    pthread_mutex_t lock;
    pthread_cond_t hasData;
    pthread_mutex_t parameterLock;
    struct ListNode parameterList;
    pthread_t threadId;
} DeviceParameterCtrl;

static DeviceParameterCtrl g_parameterCtrl = {
    .inited = 1,
    .shutdown = 0,
    .empty = 1,
    .lock = PTHREAD_MUTEX_INITIALIZER,
    .hasData = PTHREAD_COND_INITIALIZER,
    .parameterLock = PTHREAD_MUTEX_INITIALIZER,
    .parameterList = {&g_parameterCtrl.parameterList, &g_parameterCtrl.parameterList},
    .threadId = 0
};

static struct DeviceUdevConf *GetFristParameter(DeviceParameterCtrl *parameterCtrl)
{
    struct DeviceUdevConf *conf = NULL;
    pthread_mutex_lock(&(parameterCtrl->parameterLock));
    if (!ListEmpty(parameterCtrl->parameterList)) {
        conf = ListEntry(parameterCtrl->parameterList.next, struct DeviceUdevConf, paramNode);
        ListRemove(&conf->paramNode);
        ListInit(&conf->paramNode);
    }
    pthread_mutex_unlock(&(parameterCtrl->parameterLock));
    return conf;
}

static void *ThreadRun(void *data)
{
    DeviceParameterCtrl *parameterCtrl = (DeviceParameterCtrl *)data;
    INIT_LOGV("[uevent] ThreadRun %d %d", parameterCtrl->empty, parameterCtrl->shutdown);
    while (1) {
        pthread_mutex_lock(&(parameterCtrl->lock));
        while (parameterCtrl->empty) {
            struct timespec abstime = {};
            struct timeval now = {};
            const long timeout = 5000; // wait time 5000ms
            gettimeofday(&now, NULL);
            long nsec = now.tv_usec * 1000 + (timeout % 1000) * 1000000; // 1000 unit 1000000 unit nsec
            abstime.tv_sec = now.tv_sec + nsec / 1000000000 + timeout / 1000; // 1000 unit 1000000000 unit nsec
            abstime.tv_nsec = nsec % 1000000000; // 1000000000 unit nsec
            pthread_cond_timedwait(&(parameterCtrl->hasData), &(parameterCtrl->lock), &abstime);
            break;
        }
        if (parameterCtrl->shutdown) {
            break;
        }
        pthread_mutex_unlock(&(parameterCtrl->lock));
        struct DeviceUdevConf *config = GetFristParameter(parameterCtrl);
        if (config == NULL) {
            parameterCtrl->empty = 1;
            continue;
        }
X
xionglei6 已提交
90
        parameterCtrl->empty = 0;
X
xionglei6 已提交
91 92 93 94 95
        const char *paramValue = (config->action == ACTION_ADD) ? "added" : "removed";
        INIT_LOGI("[uevent] SystemSetParameter %s act %s", config->parameter, paramValue);
        if (SystemSetParameter(config->parameter, paramValue) != 0) {
            INIT_LOGE("[uevent] SystemSetParameter %s failed", config->parameter);
            pthread_mutex_lock(&(parameterCtrl->parameterLock));
X
xionglei6 已提交
96
            ListAddTail(&parameterCtrl->parameterList, &config->paramNode);
X
xionglei6 已提交
97 98 99 100 101 102 103 104
            pthread_mutex_unlock(&(parameterCtrl->parameterLock));
            parameterCtrl->empty = 1;
        }
    }
    return NULL;
}

static void AddParameter(DeviceParameterCtrl *parameterCtrl, struct DeviceUdevConf *config)
X
xionglei6 已提交
105
{
X
xionglei6 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
    pthread_mutex_lock(&(parameterCtrl->parameterLock));
    if (ListEmpty(config->paramNode)) {
        ListAddTail(&parameterCtrl->parameterList, &config->paramNode);
    }
    pthread_mutex_unlock(&(parameterCtrl->parameterLock));
    if (parameterCtrl->threadId == 0) {
        (void)pthread_create(&(parameterCtrl->threadId), NULL, ThreadRun, (void *)parameterCtrl);
    }
    pthread_mutex_unlock(&(parameterCtrl->lock));
    parameterCtrl->empty = 0;
    pthread_cond_signal(&(parameterCtrl->hasData));
    pthread_mutex_unlock(&(parameterCtrl->lock));
}

int SetUeventDeviceParameter(const char *devNode, int action)
{
    if (devNode == NULL) {
X
xionglei6 已提交
123 124 125 126
        return -1;
    }
    struct DeviceUdevConf *config = GetDeviceUdevConfByDevNode(devNode);
    if ((config != NULL) && (config->parameter != NULL)) {
X
xionglei6 已提交
127 128 129
        INIT_LOGI("[uevent] SetUeventDeviceParameter parameter %s", config->parameter);
        config->action = action;
        AddParameter(&g_parameterCtrl, config);
X
xionglei6 已提交
130 131 132
    }
    return 0;
}