1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef STARTUP_TRIGER_MANAGER_H 17 #define STARTUP_TRIGER_MANAGER_H 18 #include <stdint.h> 19 20 #include "cJSON.h" 21 #include "init_cmds.h" 22 #include "init_hashmap.h" 23 #include "list.h" 24 #include "param_message.h" 25 #include "param_utils.h" 26 #include "trigger_checker.h" 27 28 #ifdef __cplusplus 29 #if __cplusplus 30 extern "C" { 31 #endif 32 #endif 33 34 #define TRIGGER_CMD "trigger " 35 #define TRIGGER_ARR_NAME_IN_JSON "jobs" 36 #define CMDS_ARR_NAME_IN_JSON "cmds" 37 #define TRIGGER_MAX_CMD 4096 38 39 #define TRIGGER_EXECUTE_QUEUE 64 40 #define MAX_CONDITION_NUMBER 64 41 42 #define TRIGGER_FLAGS_QUEUE 0x01 43 #define TRIGGER_FLAGS_RELATED 0x02 44 #define TRIGGER_FLAGS_ONCE 0x04 // 执行完成后释放 45 #define TRIGGER_FLAGS_SUBTRIGGER 0x08 // 对init执行后,需要执行的init:xxx=aaa的trigger 46 47 #define CMD_INDEX_FOR_PARA_WAIT 0xfffE 48 #define CMD_INDEX_FOR_PARA_WATCH 0xffff 49 #define CMD_INDEX_FOR_PARA_TEST 0x10000 50 51 #define TRIGGER_IN_QUEUE(trigger) (((trigger)->flags & TRIGGER_FLAGS_QUEUE) == TRIGGER_FLAGS_QUEUE) 52 #define TRIGGER_SET_FLAG(trigger, flag) ((trigger)->flags |= (flag)) 53 #define TRIGGER_CLEAR_FLAG(trigger, flag) ((trigger)->flags &= ~(flag)) 54 #define TRIGGER_TEST_FLAG(trigger, flag) (((trigger)->flags & (flag)) == (flag)) 55 56 typedef enum { 57 TRIGGER_BOOT = 0, 58 TRIGGER_PARAM, 59 TRIGGER_UNKNOW, 60 TRIGGER_PARAM_WAIT, 61 TRIGGER_PARAM_WATCH, 62 TRIGGER_MAX, 63 } TriggerType; 64 65 #define PARAM_TRIGGER_FOR_WAIT 0 66 #define PARAM_TRIGGER_FOR_WATCH 1 67 68 struct tagTriggerNode_; 69 struct TriggerWorkSpace_; 70 typedef struct TriggerExtInfo_ { 71 int8_t type; 72 ParamTaskPtr stream; 73 union { 74 char *name; 75 struct { 76 uint32_t watchId; 77 } watchInfo; 78 struct { 79 uint32_t timeout; 80 uint32_t waitId; 81 } waitInfo; 82 } info; 83 int32_t (*addNode)(struct tagTriggerNode_ *, const struct TriggerExtInfo_ *); 84 } TriggerExtInfo; 85 86 typedef struct TriggerHeader_ { 87 ListNode triggerList; 88 uint32_t triggerCount; 89 uint32_t cmdNodeCount; 90 struct tagTriggerNode_ *(*addTrigger)(const struct TriggerWorkSpace_ *workSpace, 91 const char *condition, const TriggerExtInfo *extInfo); 92 struct tagTriggerNode_ *(*nextTrigger)(const struct TriggerHeader_ *, const struct tagTriggerNode_ *); 93 int32_t (*executeTrigger)(const struct tagTriggerNode_ *trigger, const char *content, uint32_t size); 94 95 int32_t (*checkAndMarkTrigger)(const struct TriggerWorkSpace_ *workSpace, int type, const char *name); 96 int32_t (*checkTriggerMatch)(const struct TriggerWorkSpace_ *workSpace, int type, 97 LogicCalculator *calculator, const char *content, uint32_t contentSize); 98 int32_t (*checkCondition)(LogicCalculator *calculator, 99 const char *condition, const char *content, uint32_t contentSize); 100 101 const char *(*getTriggerName)(const struct tagTriggerNode_ *trigger); 102 const char *(*getCondition)(const struct tagTriggerNode_ *trigger); 103 void (*delTrigger)(const struct TriggerWorkSpace_ *workSpace, struct tagTriggerNode_ *trigger); 104 void (*dumpTrigger)(const struct TriggerWorkSpace_ *workSpace, 105 const struct tagTriggerNode_ *trigger); 106 int32_t (*compareData)(const struct tagTriggerNode_ *trigger, const void *data); 107 } TriggerHeader; 108 109 typedef struct CommandNode_ { 110 struct CommandNode_ *next; 111 ConfigContext cfgContext; 112 uint32_t cmdKeyIndex; 113 char content[0]; 114 } CommandNode; 115 116 #define NODE_BASE \ 117 ListNode node; \ 118 uint32_t flags : 24; \ 119 uint32_t type : 4; \ 120 char *condition 121 122 typedef struct tagTriggerNode_ { 123 NODE_BASE; 124 } TriggerNode; 125 126 typedef struct tagTriggerJobNode_ { 127 NODE_BASE; 128 CommandNode *firstCmd; 129 CommandNode *lastCmd; 130 HashNode hashNode; 131 char name[0]; 132 } JobNode; 133 134 typedef struct WatchNode_ { 135 NODE_BASE; 136 ListNode item; 137 uint32_t watchId; 138 } WatchNode; 139 140 typedef struct WaitNode_ { 141 NODE_BASE; 142 ListNode item; 143 uint32_t timeout; 144 uint32_t waitId; 145 ParamTaskPtr stream; 146 } WaitNode; 147 148 typedef struct { 149 uint32_t queueCount; 150 uint32_t startIndex; 151 uint32_t endIndex; 152 TriggerNode **executeQueue; 153 } TriggerExecuteQueue; 154 155 typedef struct { 156 ListHead triggerHead; 157 ParamTaskPtr stream; 158 } ParamWatcher; 159 160 typedef struct TriggerWorkSpace_ { 161 TriggerExecuteQueue executeQueue; 162 TriggerHeader triggerHead[TRIGGER_MAX]; 163 HashMapHandle hashMap; 164 ParamTaskPtr eventHandle; 165 void (*bootStateChange)(int start, const char *); 166 char cache[PARAM_NAME_LEN_MAX + PARAM_CONST_VALUE_LEN_MAX]; 167 } TriggerWorkSpace; 168 169 int InitTriggerWorkSpace(void); 170 void CloseTriggerWorkSpace(void); 171 TriggerWorkSpace *GetTriggerWorkSpace(void); 172 char *GetTriggerCache(uint32_t *size); 173 TriggerHeader *GetTriggerHeader(const TriggerWorkSpace *workSpace, int type); 174 void InitTriggerHead(const TriggerWorkSpace *workSpace); 175 176 int CheckTrigger(TriggerWorkSpace *workSpace, int type, 177 const char *content, uint32_t contentSize, PARAM_CHECK_DONE triggerCheckDone); 178 int CheckAndMarkTrigger(int type, const char *name); 179 180 TriggerNode *ExecuteQueuePop(TriggerWorkSpace *workSpace); 181 int ExecuteQueuePush(TriggerWorkSpace *workSpace, const TriggerNode *trigger); 182 183 JobNode *UpdateJobTrigger(const TriggerWorkSpace *workSpace, 184 int type, const char *condition, const char *name); 185 JobNode *GetTriggerByName(const TriggerWorkSpace *workSpace, const char *triggerName); 186 void FreeTrigger(const TriggerWorkSpace *workSpace, TriggerNode *trigger); 187 void ClearTrigger(const TriggerWorkSpace *workSpace, int8_t type); 188 int AddCommand(JobNode *trigger, uint32_t cmdIndex, const char *content, const ConfigContext *cfgContext); 189 CommandNode *GetNextCmdNode(const JobNode *trigger, const CommandNode *curr); 190 191 void PostParamTrigger(int type, const char *name, const char *value); 192 193 void ClearWatchTrigger(ParamWatcher *watcher, int type); 194 void DelWatchTrigger(int type, const void *data); 195 int CheckWatchTriggerTimeout(void); 196 197 const char *GetTriggerName(const TriggerNode *trigger); 198 void RegisterTriggerExec(int type, int32_t (*executeTrigger)(const TriggerNode *, const char *, uint32_t)); 199 200 #ifdef STARTUP_INIT_TEST 201 void ProcessBeforeEvent(const ParamTaskPtr stream, uint64_t eventId, const uint8_t *content, uint32_t size); 202 #endif 203 #ifdef __cplusplus 204 #if __cplusplus 205 } 206 #endif 207 #endif 208 #endif // STARTUP_TRIGER_MANAGER_H 209