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_hashmap.h" 22 #include "list.h" 23 #include "param_message.h" 24 #include "param_utils.h" 25 #include "trigger_checker.h" 26 27 #ifdef __cplusplus 28 #if __cplusplus 29 extern "C" { 30 #endif 31 #endif 32 33 #define TRIGGER_CMD "trigger " 34 #define TRIGGER_ARR_NAME_IN_JSON "jobs" 35 #define CMDS_ARR_NAME_IN_JSON "cmds" 36 #define TRIGGER_MAX_CMD 4096 37 38 #define TRIGGER_EXECUTE_QUEUE 64 39 #define MAX_CONDITION_NUMBER 64 40 41 #define TRIGGER_FLAGS_QUEUE 0x01 42 #define TRIGGER_FLAGS_RELATED 0x02 43 #define TRIGGER_FLAGS_ONCE 0x04 // 执行完成后释放 44 #define TRIGGER_FLAGS_SUBTRIGGER 0x08 // 对init执行后,需要执行的init:xxx=aaa的trigger 45 46 #define CMD_INDEX_FOR_PARA_WAIT 0xfffE 47 #define CMD_INDEX_FOR_PARA_WATCH 0xffff 48 #define CMD_INDEX_FOR_PARA_TEST 0x10000 49 50 #define TRIGGER_IN_QUEUE(trigger) (((trigger)->flags & TRIGGER_FLAGS_QUEUE) == TRIGGER_FLAGS_QUEUE) 51 #define TRIGGER_SET_FLAG(trigger, flag) ((trigger)->flags |= (flag)) 52 #define TRIGGER_CLEAR_FLAG(trigger, flag) ((trigger)->flags &= ~(flag)) 53 #define TRIGGER_TEST_FLAG(trigger, flag) (((trigger)->flags & (flag)) == (flag)) 54 55 typedef enum { 56 TRIGGER_BOOT = 0, 57 TRIGGER_PARAM, 58 TRIGGER_UNKNOW, 59 TRIGGER_PARAM_WAIT, 60 TRIGGER_PARAM_WATCH, 61 TRIGGER_MAX, 62 } TriggerType; 63 64 #define PARAM_TRIGGER_FOR_WAIT 0 65 #define PARAM_TRIGGER_FOR_WATCH 1 66 67 struct tagTriggerNode_; 68 struct TriggerWorkSpace_; 69 typedef struct TriggerExtInfo_ { 70 int8_t type; 71 ParamTaskPtr stream; 72 union { 73 char *name; 74 struct { 75 uint32_t watchId; 76 } watchInfo; 77 struct { 78 uint32_t timeout; 79 uint32_t waitId; 80 } waitInfo; 81 } info; 82 int32_t (*addNode)(struct tagTriggerNode_ *, const struct TriggerExtInfo_ *); 83 } TriggerExtInfo; 84 85 typedef struct TriggerHeader_ { 86 ListNode triggerList; 87 uint32_t triggerCount; 88 uint32_t cmdNodeCount; 89 struct tagTriggerNode_ *(*addTrigger)(const struct TriggerWorkSpace_ *workSpace, 90 const char *condition, const TriggerExtInfo *extInfo); 91 struct tagTriggerNode_ *(*nextTrigger)(const struct TriggerHeader_ *, const struct tagTriggerNode_ *); 92 int32_t (*executeTrigger)(const struct tagTriggerNode_ *trigger, const char *content, uint32_t size); 93 94 int32_t (*checkAndMarkTrigger)(const struct TriggerWorkSpace_ *workSpace, int type, const char *name); 95 int32_t (*checkTriggerMatch)(const struct TriggerWorkSpace_ *workSpace, int type, 96 LogicCalculator *calculator, const char *content, uint32_t contentSize); 97 int32_t (*checkCondition)(LogicCalculator *calculator, 98 const char *condition, const char *content, uint32_t contentSize); 99 100 const char *(*getTriggerName)(const struct tagTriggerNode_ *trigger); 101 const char *(*getCondition)(const struct tagTriggerNode_ *trigger); 102 void (*delTrigger)(const struct TriggerWorkSpace_ *workSpace, struct tagTriggerNode_ *trigger); 103 void (*dumpTrigger)(const struct TriggerWorkSpace_ *workSpace, 104 const struct tagTriggerNode_ *trigger); 105 int32_t (*compareData)(const struct tagTriggerNode_ *trigger, const void *data); 106 } TriggerHeader; 107 108 typedef struct CommandNode_ { 109 struct CommandNode_ *next; 110 uint32_t cmdKeyIndex; 111 char content[0]; 112 } CommandNode; 113 114 #define NODE_BASE \ 115 ListNode node; \ 116 uint32_t flags : 24; \ 117 uint32_t type : 4; \ 118 char *condition 119 120 typedef struct tagTriggerNode_ { 121 NODE_BASE; 122 } TriggerNode; 123 124 typedef struct tagTriggerJobNode_ { 125 NODE_BASE; 126 CommandNode *firstCmd; 127 CommandNode *lastCmd; 128 HashNode hashNode; 129 char name[0]; 130 } JobNode; 131 132 typedef struct WatchNode_ { 133 NODE_BASE; 134 ListNode item; 135 uint32_t watchId; 136 } WatchNode; 137 138 typedef struct WaitNode_ { 139 NODE_BASE; 140 ListNode item; 141 uint32_t timeout; 142 uint32_t waitId; 143 ParamTaskPtr stream; 144 } WaitNode; 145 146 typedef struct { 147 uint32_t queueCount; 148 uint32_t startIndex; 149 uint32_t endIndex; 150 TriggerNode **executeQueue; 151 } TriggerExecuteQueue; 152 153 typedef struct { 154 ListHead triggerHead; 155 ParamTaskPtr stream; 156 } ParamWatcher; 157 158 typedef struct TriggerWorkSpace_ { 159 TriggerExecuteQueue executeQueue; 160 TriggerHeader triggerHead[TRIGGER_MAX]; 161 HashMapHandle hashMap; 162 ParamTaskPtr eventHandle; 163 void (*bootStateChange)(int start, const char *); 164 char cache[PARAM_NAME_LEN_MAX + PARAM_CONST_VALUE_LEN_MAX]; 165 } TriggerWorkSpace; 166 167 int InitTriggerWorkSpace(void); 168 void CloseTriggerWorkSpace(void); 169 TriggerWorkSpace *GetTriggerWorkSpace(void); 170 char *GetTriggerCache(uint32_t *size); 171 TriggerHeader *GetTriggerHeader(const TriggerWorkSpace *workSpace, int type); 172 void InitTriggerHead(const TriggerWorkSpace *workSpace); 173 174 int CheckTrigger(TriggerWorkSpace *workSpace, int type, 175 const char *content, uint32_t contentSize, PARAM_CHECK_DONE triggerCheckDone); 176 int CheckAndMarkTrigger(int type, const char *name); 177 178 TriggerNode *ExecuteQueuePop(TriggerWorkSpace *workSpace); 179 int ExecuteQueuePush(TriggerWorkSpace *workSpace, const TriggerNode *trigger); 180 181 JobNode *UpdateJobTrigger(const TriggerWorkSpace *workSpace, 182 int type, const char *condition, const char *name); 183 JobNode *GetTriggerByName(const TriggerWorkSpace *workSpace, const char *triggerName); 184 void FreeTrigger(const TriggerWorkSpace *workSpace, TriggerNode *trigger); 185 void ClearTrigger(const TriggerWorkSpace *workSpace, int8_t type); 186 int AddCommand(JobNode *trigger, uint32_t cmdIndex, const char *content); 187 CommandNode *GetNextCmdNode(const JobNode *trigger, const CommandNode *curr); 188 189 void PostParamTrigger(int type, const char *name, const char *value); 190 191 void ClearWatchTrigger(ParamWatcher *watcher, int type); 192 void DelWatchTrigger(int type, const void *data); 193 int CheckWatchTriggerTimeout(void); 194 195 const char *GetTriggerName(const TriggerNode *trigger); 196 void RegisterTriggerExec(int type, int32_t (*executeTrigger)(const TriggerNode *, const char *, uint32_t)); 197 198 #ifdef STARTUP_INIT_TEST 199 void ProcessBeforeEvent(const ParamTaskPtr stream, uint64_t eventId, const uint8_t *content, uint32_t size); 200 #endif 201 #ifdef __cplusplus 202 #if __cplusplus 203 } 204 #endif 205 #endif 206 #endif // STARTUP_TRIGER_MANAGER_H 207