• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "securec.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     uint32_t cmdKeyIndex;
112     char content[0];
113 } CommandNode;
114 
115 #define NODE_BASE \
116     ListNode node; \
117     uint32_t flags : 24; \
118     uint32_t type : 4; \
119     char *condition
120 
121 typedef struct tagTriggerNode_ {
122     NODE_BASE;
123 } TriggerNode;
124 
125 typedef struct tagTriggerJobNode_ {
126     NODE_BASE;
127     CommandNode *firstCmd;
128     CommandNode *lastCmd;
129     HashNode hashNode;
130     char name[0];
131 } JobNode;
132 
133 typedef struct WatchNode_ {
134     NODE_BASE;
135     ListNode item;
136     uint32_t watchId;
137 } WatchNode;
138 
139 typedef struct WaitNode_ {
140     NODE_BASE;
141     ListNode item;
142     uint32_t timeout;
143     uint32_t waitId;
144     ParamTaskPtr stream;
145 } WaitNode;
146 
147 typedef struct {
148     uint32_t queueCount;
149     uint32_t startIndex;
150     uint32_t endIndex;
151     TriggerNode **executeQueue;
152 } TriggerExecuteQueue;
153 
154 typedef struct {
155     ListHead triggerHead;
156     ParamTaskPtr stream;
157 } ParamWatcher;
158 
159 typedef struct TriggerWorkSpace_ {
160     TriggerExecuteQueue executeQueue;
161     TriggerHeader triggerHead[TRIGGER_MAX];
162     HashMapHandle hashMap;
163     ParamTaskPtr eventHandle;
164     void (*bootStateChange)(const char *);
165     char cache[PARAM_NAME_LEN_MAX + PARAM_CONST_VALUE_LEN_MAX];
166 } TriggerWorkSpace;
167 
168 int InitTriggerWorkSpace(void);
169 void CloseTriggerWorkSpace(void);
170 TriggerWorkSpace *GetTriggerWorkSpace(void);
171 char *GetTriggerCache(uint32_t *size);
172 TriggerHeader *GetTriggerHeader(const TriggerWorkSpace *workSpace, int type);
173 void InitTriggerHead(const TriggerWorkSpace *workSpace);
174 
175 int CheckTrigger(TriggerWorkSpace *workSpace, int type,
176     const char *content, uint32_t contentSize, PARAM_CHECK_DONE triggerCheckDone);
177 int CheckAndMarkTrigger(int type, const char *name);
178 
179 TriggerNode *ExecuteQueuePop(TriggerWorkSpace *workSpace);
180 int ExecuteQueuePush(TriggerWorkSpace *workSpace, const TriggerNode *trigger);
181 
182 JobNode *UpdateJobTrigger(const TriggerWorkSpace *workSpace,
183     int type, const char *condition, const char *name);
184 JobNode *GetTriggerByName(const TriggerWorkSpace *workSpace, const char *triggerName);
185 void FreeTrigger(const TriggerWorkSpace *workSpace, TriggerNode *trigger);
186 void ClearTrigger(const TriggerWorkSpace *workSpace, int8_t type);
187 int AddCommand(JobNode *trigger, uint32_t cmdIndex, const char *content);
188 CommandNode *GetNextCmdNode(const JobNode *trigger, const CommandNode *curr);
189 
190 void DumpTrigger(const TriggerWorkSpace *workSpace);
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 #ifdef __cplusplus
200 #if __cplusplus
201 }
202 #endif
203 #endif
204 #endif // STARTUP_TRIGER_MANAGER_H
205