• 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 LNN_STATE_MACHINE_H
17 #define LNN_STATE_MACHINE_H
18 
19 #include "common_list.h"
20 #include "message_handler.h"
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 #define FSM_FLAG_RUNNING 0x1
27 
28 #define FSM_CTRL_MSG_START 0
29 #define FSM_CTRL_MSG_DATA 1
30 #define FSM_CTRL_MSG_STOP 2
31 #define FSM_CTRL_MSG_DEINIT 3
32 
33 struct tagFsmStateMachine;
34 
35 typedef void (*StateEnterFunc)(struct tagFsmStateMachine *fsm);
36 typedef void (*StateExitFunc)(struct tagFsmStateMachine *fsm);
37 typedef bool (*StateProcessFunc)(struct tagFsmStateMachine *fsm, int32_t msgType, void *para);
38 
39 typedef struct {
40     ListNode list;
41     StateEnterFunc enter;
42     StateProcessFunc process;
43     StateExitFunc exit;
44 } FsmState;
45 
46 typedef void (*FsmDeinitCallback)(struct tagFsmStateMachine *fsm);
47 
48 typedef struct tagFsmStateMachine {
49     FsmState *curState;
50     uint32_t flag;
51 
52     ListNode stateList;
53     SoftBusLooper *looper;
54     SoftBusHandler handler;
55 
56     FsmDeinitCallback deinitCallback;
57 } FsmStateMachine;
58 
59 typedef struct {
60     FsmStateMachine *fsm;
61     void *obj;
62 } FsmCtrlMsgObj;
63 
64 int32_t LnnFsmInit(FsmStateMachine *fsm, SoftBusLooper *looper, char *name, FsmDeinitCallback cb);
65 int32_t LnnFsmDeinit(FsmStateMachine *fsm);
66 
67 int32_t LnnFsmAddState(FsmStateMachine *fsm, FsmState *state);
68 
69 int32_t LnnFsmStart(FsmStateMachine *fsm, FsmState *initialState);
70 int32_t LnnFsmStop(FsmStateMachine *fsm);
71 
72 int32_t LnnFsmPostMessage(FsmStateMachine *fsm, uint32_t msgType, void *data);
73 int32_t LnnFsmPostMessageDelay(FsmStateMachine *fsm, uint32_t msgType, void *data, uint64_t delayMillis);
74 
75 int32_t LnnFsmRemoveMessageByType(FsmStateMachine *fsm, int32_t what);
76 
77 /* msgType value should not be 0 */
78 int32_t LnnFsmRemoveMessage(FsmStateMachine *fsm, int32_t msgType);
79 int32_t LnnFsmRemoveMessageSpecific(FsmStateMachine *fsm,
80     int32_t (*customFunc)(const SoftBusMessage*, void*), void *args);
81 
82 int32_t LnnFsmTransactState(FsmStateMachine *fsm, FsmState *state);
83 
84 #ifdef __cplusplus
85 }
86 #endif
87 
88 #endif
89