1 /* 2 * Copyright (c) 2022 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 SEC_UTILS_FINITE_STATE_MACHINE_H 17 #define SEC_UTILS_FINITE_STATE_MACHINE_H 18 19 #include <stdbool.h> 20 #include <stdint.h> 21 22 #include "utils_list.h" 23 #include "utils_mutex.h" 24 25 #ifdef __cplusplus 26 extern "C" { 27 #endif 28 29 typedef struct StateMachine StateMachine; 30 31 typedef bool (*StateMachineProcessFunc)(const StateMachine *machine, uint32_t event, const void *para); 32 33 typedef struct { 34 uint32_t state; 35 uint32_t event; 36 StateMachineProcessFunc process; 37 uint32_t nextStateT; 38 uint32_t nextStateF; 39 } StateNode; 40 41 typedef struct StateMachine { 42 uint32_t currState; 43 uint32_t machineId; 44 bool isScheduling; 45 ListHead pendingEventList; 46 Mutex mutex; 47 } StateMachine; 48 49 void InitStateMachine(StateMachine *machine, uint32_t machineId, uint32_t initState); 50 51 void ScheduleMachine(const StateNode *nodes, uint32_t nodeCnt, StateMachine *machine, uint32_t event, const void *para); 52 53 #define STATE_MACHINE_ENTRY(item, type, member) ((type *)((char *)(item) - (char *)(&((type *)0)->member))) 54 55 #ifdef __cplusplus 56 } 57 #endif 58 59 #endif // SEC_UTILS_FINITE_STATE_MACHINE_H 60