trigger_checker.c 12.2 KB
Newer Older
Z
zhong_ning 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * 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 "trigger_checker.h"
#include <ctype.h>
#include "trigger_manager.h"
Z
zhong_ning 已提交
19
#include "init_param.h"
Z
zhong_ning 已提交
20

Z
zhong_ning 已提交
21
#define LABEL "Trigger"
Z
zhong_ning 已提交
22 23 24
// 申请整块能存作为计算的节点
int CalculatorInit(LogicCalculator *calculator, int dataNumber, int dataUnit, int needCondition)
{
Z
zhong_ning 已提交
25
    PARAM_CHECK(calculator != NULL, return -1, "Invalid param");
Z
zhong_ning 已提交
26 27 28 29 30
    int dataSize = dataUnit * dataNumber;
    if (needCondition) {
        dataSize += 5 * SUPPORT_DATA_BUFFER_MAX;
    }
    calculator->data = (char *)malloc(dataSize);
Z
zhong_ning 已提交
31
    PARAM_CHECK(calculator->data != NULL, return -1, "Failed to malloc for calculator");
Z
zhong_ning 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45
    calculator->dataNumber = dataNumber;
    calculator->endIndex = 0;
    calculator->dataUnit = dataUnit;

    dataSize = dataUnit * dataNumber;
    calculator->conditionName = calculator->data + dataSize;
    dataSize += SUPPORT_DATA_BUFFER_MAX;
    calculator->conditionContent = calculator->data + dataSize;
    dataSize += SUPPORT_DATA_BUFFER_MAX;
    calculator->inputName = calculator->data + dataSize;
    dataSize += SUPPORT_DATA_BUFFER_MAX;
    calculator->inputContent = calculator->data + dataSize;
    dataSize += SUPPORT_DATA_BUFFER_MAX;
    calculator->readContent = calculator->data + dataSize;
Z
zhong_ning 已提交
46 47
    return memset_s(calculator->triggerContent,
        sizeof(calculator->triggerContent), 0, sizeof(calculator->triggerContent));
Z
zhong_ning 已提交
48 49 50 51
}

void CalculatorFree(LogicCalculator *calculator)
{
Z
zhong_ning 已提交
52
    PARAM_CHECK(calculator != NULL, return, "Invalid param");
Z
zhong_ning 已提交
53 54 55 56 57 58
    free(calculator->data);
    calculator->data = NULL;
}

static void CalculatorClear(LogicCalculator *calculator)
{
Z
zhong_ning 已提交
59
    PARAM_CHECK(calculator != NULL, return, "Invalid param");
Z
zhong_ning 已提交
60 61 62 63 64
    calculator->endIndex = 0;
}

static int CalculatorPushChar(LogicCalculator *calculator, char data)
{
Z
zhong_ning 已提交
65 66 67
    PARAM_CHECK(calculator != NULL, return -1, "Invalid param");
    PARAM_CHECK(calculator->endIndex < calculator->dataNumber, return -1, "More data for calculator support");
    PARAM_CHECK(sizeof(char) == calculator->dataUnit, return -1, "More data for calculator support");
Z
zhong_ning 已提交
68 69 70 71 72 73
    calculator->data[calculator->endIndex++] = data;
    return 0;
}

static int CalculatorPopChar(LogicCalculator *calculator, char *data)
{
Z
zhong_ning 已提交
74 75
    PARAM_CHECK(calculator != NULL, return -1, "Invalid param");
    PARAM_CHECK(calculator->endIndex < calculator->dataNumber, return -1, "More data for calculator support");
Z
zhong_ning 已提交
76 77 78 79 80 81 82 83 84
    if (calculator->endIndex == 0) {
        return -1;
    }
    *data = calculator->data[--calculator->endIndex];
    return 0;
}

static int CalculatorPush(LogicCalculator *calculator, void *data)
{
Z
zhong_ning 已提交
85 86
    PARAM_CHECK(calculator != NULL, return -1, "Invalid param");
    PARAM_CHECK(calculator->endIndex < calculator->dataNumber, return -1, "More data for calculator support");
Z
zhong_ning 已提交
87 88
    char *tmpData = (calculator->data + calculator->dataUnit * calculator->endIndex);
    int ret = memcpy_s(tmpData, calculator->dataUnit, data, calculator->dataUnit);
Z
zhong_ning 已提交
89
    PARAM_CHECK(ret == 0, return -1, "Failed to copy logic data");
Z
zhong_ning 已提交
90 91 92 93 94 95
    calculator->endIndex++;
    return 0;
}

static int CalculatorPop(LogicCalculator *calculator, void *data)
{
Z
zhong_ning 已提交
96 97
    PARAM_CHECK(calculator != NULL || data == NULL, return -1, "Invalid param");
    PARAM_CHECK(calculator->endIndex < calculator->dataNumber, return -1, "More data for calculator support");
Z
zhong_ning 已提交
98 99 100 101 102
    if (calculator->endIndex == 0) {
        return -1;
    }
    char *tmpData = calculator->data + calculator->dataUnit * (calculator->endIndex - 1);
    int ret = memcpy_s(data, calculator->dataUnit, tmpData, calculator->dataUnit);
Z
zhong_ning 已提交
103
    PARAM_CHECK(ret == 0, return -1, "Failed to copy logic data");
Z
zhong_ning 已提交
104 105 106 107 108 109
    calculator->endIndex--;
    return 0;
}

static int CalculatorLength(const LogicCalculator *calculator)
{
Z
zhong_ning 已提交
110
    PARAM_CHECK(calculator != NULL, return 0, "Invalid param");
Z
zhong_ning 已提交
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
    return calculator->endIndex;
}

static int PrefixAdd(char *prefix, u_int32_t *prefixIndex, u_int32_t prefixLen, char op)
{
    if ((*prefixIndex + 3) >= prefixLen) {
        return -1;
    }
    prefix[(*prefixIndex)++] = ' ';
    prefix[(*prefixIndex)++] = op;
    prefix[(*prefixIndex)++] = ' ';
    return 0;
}

static int HandleOperationOr(LogicCalculator *calculator, char *prefix, u_int32_t *prefixIndex, u_int32_t prefixLen)
{
    int ret = 0;
    char e;
    prefix[(*prefixIndex)++] = ' ';
    if(CalculatorLength(calculator) == 0) {
        CalculatorPushChar(calculator, '|');
    } else {
        do {
            CalculatorPopChar(calculator, &e);
            if (e == '(') {
                CalculatorPushChar(calculator, e);
            } else {
                ret = PrefixAdd(prefix, prefixIndex, prefixLen, e);
Z
zhong_ning 已提交
139
                PARAM_CHECK(ret == 0, return -1, "Invalid prefix");
Z
zhong_ning 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152
            }
        } while (CalculatorLength(calculator) > 0 && e != '(');
        CalculatorPushChar(calculator, '|');
    }
    return 0;
}

static int ComputeSubCondition(LogicCalculator *calculator, LogicData *data, const char *condition)
{
    if (!LOGIC_DATA_TEST_FLAG(data, LOGIC_DATA_FLAGS_ORIGINAL)) {
        return LOGIC_DATA_TEST_FLAG(data, LOGIC_DATA_FLAGS_TRUE);
    }
    // 解析条件
Z
zhong_ning 已提交
153 154 155
    char *subStr = strstr(condition + data->startIndex, "=");
    if (subStr != NULL && ((u_int32_t)(subStr - condition) > data->endIndex)) {
        if (strncmp(condition + data->startIndex, calculator->triggerContent, strlen(calculator->triggerContent)) == 0) {
Z
zhong_ning 已提交
156 157
            return 1;
        }
Z
zhong_ning 已提交
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
    } else {
        int ret = GetValueFromContent(condition + data->startIndex,
            data->endIndex - data->startIndex, 0, calculator->conditionName, SUPPORT_DATA_BUFFER_MAX);
        PARAM_CHECK(ret == 0, return -1, "Failed parse content name");
        ret = GetValueFromContent(condition + data->startIndex, data->endIndex - data->startIndex,
            strlen(calculator->conditionName) + 1, calculator->conditionContent, SUPPORT_DATA_BUFFER_MAX);
        PARAM_CHECK(ret == 0, return -1, "Failed parse content value");
        // check name
        if (calculator->inputName && strcmp(calculator->conditionName, calculator->inputName) == 0) {
            if (strcmp(calculator->conditionContent, "*") == 0) {
                return 1;
            }
            if (strcmp(calculator->conditionContent, calculator->inputContent) == 0) {
                return 1;
            }
        } else {
            u_int32_t len = SUPPORT_DATA_BUFFER_MAX;
            ret = SystemReadParam(calculator->conditionName, calculator->readContent, &len);
            if (ret == 0 && (strcmp(calculator->conditionContent, "*") == 0 ||
                    strcmp(calculator->conditionContent, calculator->readContent) == 0)) {
                return 1;
            }
Z
zhong_ning 已提交
180
        }
Z
zhong_ning 已提交
181
    }
Z
zhong_ning 已提交
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
    return 0;
}

int GetValueFromContent(const char *content, u_int32_t contentSize, u_int32_t start, char *value, u_int32_t valueSize)
{
    u_int32_t contentIndex = start;
    u_int32_t currIndex = 0;
    while (contentIndex < contentSize && currIndex < valueSize) {
        if (content[contentIndex] == '=') {
            value[currIndex++] = '\0';
            return 0;
        }
        value[currIndex++] = content[contentIndex++];
    }
    if (currIndex < valueSize) {
        value[currIndex] = '\0';
        return 0;
    }
    return -1;
}

int ComputeCondition(LogicCalculator *calculator, const char *condition)
{
    u_int32_t currIndex = 0;
    u_int32_t start = 0;
    int noneOper = 1;
    CalculatorClear(calculator);
    LogicData data1 = {};
    LogicData data2 = {};
    while (currIndex < strlen(condition)) {
        if (condition[currIndex] == '|' || condition[currIndex] == '&') {
            noneOper = 0;
            int ret = CalculatorPop(calculator, (void*)&data2);
            ret |= CalculatorPop(calculator, (void*)&data1);
Z
zhong_ning 已提交
216
            PARAM_CHECK(ret == 0, return -1, "Failed to pop data");
Z
zhong_ning 已提交
217 218 219 220 221 222 223 224 225 226 227

            ret = ComputeSubCondition(calculator, &data1, condition);
            data1.flags = 0;
            if (condition[currIndex] == '|' && ret == 1) {
                LOGIC_DATA_SET_FLAG(&data1, LOGIC_DATA_FLAGS_TRUE);
            } else if (condition[currIndex] == '|' || ret == 1) {
                if (ComputeSubCondition(calculator, &data2, condition) == 1) {
                    LOGIC_DATA_SET_FLAG(&data1, LOGIC_DATA_FLAGS_TRUE);
                }
            }
            ret = CalculatorPush(calculator, (void*)&data1);
Z
zhong_ning 已提交
228
            PARAM_CHECK(ret == 0, return -1, "Failed to push data");
Z
zhong_ning 已提交
229 230 231 232 233 234 235 236 237 238
            start = currIndex + 1; // 跳过符号
        } else if (isspace(condition[currIndex])) {
            if (start == currIndex) {
                start = ++currIndex;
                continue;
            }
            data1.flags = LOGIC_DATA_FLAGS_ORIGINAL;
            data1.startIndex = start;
            data1.endIndex = currIndex;
            int ret = CalculatorPush(calculator, (void*)&data1);
Z
zhong_ning 已提交
239
            PARAM_CHECK(ret == 0, return -1, "Failed to push data");
Z
zhong_ning 已提交
240 241 242 243 244 245 246 247 248 249
            start = currIndex + 1;
        }
        currIndex++;
    }
    if (noneOper) {
        data1.flags = LOGIC_DATA_FLAGS_ORIGINAL;
        data1.startIndex = start;
        data1.endIndex = strlen(condition);
    } else {
        int ret = CalculatorPop(calculator, &data1);
Z
zhong_ning 已提交
250
        PARAM_CHECK(ret == 0, return -1, "Invalid calculator");
Z
zhong_ning 已提交
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
    }
    return ComputeSubCondition(calculator, &data1, condition);
}

int ConvertInfixToPrefix(const char *condition, char *prefix, u_int32_t prefixLen)
{
    char e;
    int ret = 0;
    u_int32_t curr = 0;
    u_int32_t prefixIndex = 0;
    LogicCalculator calculator;
    CalculatorInit(&calculator, 100, 1, 0);

    while (curr < strlen(condition)) {
        if (condition[curr] == ')') {
            CalculatorPopChar(&calculator, &e);
            while (e != '(') {
                ret = PrefixAdd(prefix, &prefixIndex, prefixLen, e);
Z
zhong_ning 已提交
269
                PARAM_CHECK(ret == 0, CalculatorFree(&calculator); return -1, "Invalid prefix");
Z
zhong_ning 已提交
270
                ret = CalculatorPopChar(&calculator, &e);
Z
zhong_ning 已提交
271
                PARAM_CHECK(ret == 0, CalculatorFree(&calculator); return -1, "Invalid calculator");
Z
zhong_ning 已提交
272 273
            }
        } else if (condition[curr] == '|') {
Z
zhong_ning 已提交
274
            PARAM_CHECK(condition[curr + 1] == '|', CalculatorFree(&calculator); return -1, "Invalid condition");
Z
zhong_ning 已提交
275
            ret = HandleOperationOr(&calculator, prefix, &prefixIndex, prefixLen);
Z
zhong_ning 已提交
276
            PARAM_CHECK(ret == 0, CalculatorFree(&calculator); return -1, "Invalid prefix");
Z
zhong_ning 已提交
277 278
            curr++;
        } else if (condition[curr] == '&') {
Z
zhong_ning 已提交
279
            PARAM_CHECK(condition[curr + 1] == '&', CalculatorFree(&calculator); return -1, "Invalid condition");
Z
zhong_ning 已提交
280 281 282 283 284 285 286 287 288
            prefix[prefixIndex++] = ' ';
            CalculatorPushChar(&calculator, condition[curr]);
            curr++;
        } else if (condition[curr] == '(') {
            CalculatorPushChar(&calculator, condition[curr]);
        } else {
            prefix[prefixIndex++] = condition[curr];
        }
        curr++;
Z
zhong_ning 已提交
289
        PARAM_CHECK(prefixIndex < prefixLen, CalculatorFree(&calculator); return -1, "Invalid prefixIndex");
Z
zhong_ning 已提交
290 291 292 293 294
    }

    while (CalculatorLength(&calculator) > 0) {
        CalculatorPopChar(&calculator, &e);
        ret = PrefixAdd(prefix, &prefixIndex, prefixLen, e);
Z
zhong_ning 已提交
295
        PARAM_CHECK(ret == 0, CalculatorFree(&calculator);
Z
zhong_ning 已提交
296 297 298 299 300
            return -1, "Invalid prefix %u %u", prefixIndex, prefixLen);
    }
    prefix[prefixIndex] = '\0';
    CalculatorFree(&calculator);
    return 0;
Z
zhong_ning 已提交
301 302 303 304 305 306 307 308 309 310 311
}

char *GetMatchedSubCondition(const char *condition, const char *input, int length)
{
    const char *p = condition;
    for(;(p = strchr(p, *input)) != 0; p++) {
        if(strncmp(p, input, length) == 0) {
            return (char*)p;
        }
    }
    return NULL;
Z
zhong_ning 已提交
312
}