blob: bffe40ffd3ba02e59c3832c477ddd81b80e06a83 [file] [log] [blame]
rjw6c1fd8f2022-11-30 14:33:01 +08001/*************************************************************
2*
3* This Software is the property of VIA Telecom, Inc. and may only be used pursuant to a license from VIA Telecom, Inc.
4*
5* Any unauthorized use inconsistent with the terms of such license is strictly prohibited.
6*
7* Copyright (c) 1999-2010 VIA Telecom, Inc. All rights reserved.
8*
9*************************************************************/
10/*************************************************************************
11 *
12 * File Name: fsm.h
13 * Project: ISOLIB C Library
14 * Original Author: Dale Ehman
15 * Creation Date: Aug. 26, 1997
16 *
17 * Description: Header for Generic FSM C Language implementation.
18 *
19 * Restrictions:
20 *
21 * Dependencies:
22 *
23 ****************************************************************************
24 ****************************************************************************
25 *
26 * RCS Log Information
27 *
28 * $Log: FSM.H $
29 *
30 * 06 29 2017 sue.zhong
31 * [MOLY00259241] [6293][C2K]Replace with KAL data type
32 * ::KAL type - folder inc
33 *
34 * 05 17 2017 peijun.zheng
35 * [MOLY00249027] [BIACNO][MT6763][New feature]C2K code release code modification
36 *
37 * C2k code release.
38 * Revision 1.3 2006/01/11 15:15:19 blee
39 * Added new state machine handler
40 * Revision 1.2 2004/03/25 12:08:47 fpeng
41 * Updated from 6.0 CP 2.5.0
42 * Revision 1.1 2003/05/12 15:38:44 fpeng
43 * Initial revision
44 * Revision 1.7 2003/05/02 14:35:59 sulinlow
45 * To add RLP BLOB params received from PS
46 * Revision 1.6 2002/06/06 13:12:20 chinh
47 * Changed and Added Copyright
48 * Revision 1.5 2002/05/30 09:31:36 mshaver
49 * The ARM 2.51 tools the compiler supports the alignment options :
50 * -zap1 which specifies that pointers to structs are assumed to be
51 * aligned on at least <struct minimal alignment> boundaries.
52 * -zat4 which specifies the minimal byte alignment for top-level static
53 * objects, such as global variables is 4 bytes.
54 * The ADS compiler does not support either of these alignment
55 * options. The only alignment option it supports is :
56 * -zas which specifies the minimum byte alignment for structures. This option will not be supported in future versions of the ADS
57 * tools.
58 * Therefore, to be compatible with both the ARM 2.51 tools and the
59 * ADS tools, the PACKED qualifier was removed from the FSM
60 * Descriptor Structure.
61 * Revision 1.4.1.2 2002/02/11 10:51:02 srodenba
62 * remove PACKED designator to make it work with ADS
63 * --- Added comments --- mshaver [2002/05/22 16:20:08Z]
64 * For the ARM 2.51 tools the compiler supports the alignment options :
65 * -zap1 which specifies that pointers to structs are assumed to be aligned on at least
66 * <struct minimal alignment> boundaries.
67 * -zat4 which specifies the minimal byte alignment for top-level static objects, such as global
68 * variables is 4 bytes.
69 * The ADS compiler does not support either of these alignment options. The only alignment
70 * option it supports is :
71 * -zas which specifies the minimum byte alignment for structures. This option will not be
72 * supported in future versions of the ADS tools.
73 *
74 * Therefore, to be compatible with both the ARM 2.51 tools and the ADS tools, the
75 * PACKED qualifier was removed from the FSM Descriptor Structure.
76 * Revision 1.4.1.1 2002/02/01 11:59:15 srodenba
77 * Duplicate revision
78 * Revision 1.4 2002/02/01 11:59:15 ameya
79 * Moved OTTS related definitions from this file to ottsdefs.h
80 * Revision 1.3 2001/10/19 13:54:49 ameya
81 * Changed all occurences of _packed to PACKED. Added definition of macro PACKED if undefined.
82 * Revision 1.2 2001/06/21 09:42:38 yfang
83 * supported RLP3
84 * Revision 1.1 2001/01/17 10:58:37 plabarbe
85 * Initial revision
86 * Revision 1.1 2000/10/24 14:11:13 fpeng
87 * Initial revision
88 * Revision 1.3 2000/08/24 00:07:10Z hhong
89 * Add #ifndef pair for COND_ST
90 * Revision 1.2 2000/05/12 00:53:51Z hhong
91 * Make FSM.h available to all data engines across the board from this spot.
92 * Revision 1.20 1999/07/21 21:17:37 msmith
93 * Corrected FSMEXTTRACEINFO for TOKEN tracing
94 *
95 * Revision 1.19 1999/06/26 00:26:03 ddoucett
96 * fixed the FSM_EXTTRACEINFO for case when TRACEON is not defined
97 *
98 * Revision 1.18 1999/06/14 23:06:08 bcassidy
99 * Changed to distinguish between token tracing and string tracing
100 *
101 * Revision 1.17 1999/06/09 23:04:44 msmith
102 * Reduced parm_struct size
103 *
104 * Revision 1.16 1999/06/07 22:50:39 ddoucett
105 * increaced the size of the MAX_PARM_STRUCT to 83
106 *
107 * Revision 1.15 1999/05/13 15:12:57 msmith
108 * Added support for tracing when TRACEON defined
109 *
110 *
111 * Revision 1.13 1998/10/06 17:08:13 bcassidy
112 * 1. Mods for circular queue operation.
113 * 2. Put queue size back to 5.
114 *
115 * Revision 1.12 1998/10/04 01:39:02 pjiang
116 * 1. Bump event Q size.
117 * [Temporary fix until FSM changed to circular Q operation.]
118 *
119 * Revision 1.11 1998/09/25 20:28:21 bcassidy
120 * 1. Mod setFSMTraceInfo proto.
121 *
122 * Revision 1.10 1998/08/28 14:57:42 bcassidy
123 * 1. Mods for multi-engine tracing system.
124 *
125 * Revision 1.9 1998/07/14 19:24:53 bcassidy
126 * 1. Simplify MakeFSMTable maro.
127 * 2. Add ROM keyword for constant data.
128 *
129 * Revision 1.8 1998/05/15 21:28:08 bcassidy
130 * 1. Modify tracing system to avoid string fields.
131 *
132 * Revision 1.7 1998/03/05 17:50:10 bcassidy
133 * 1. Add compile switch for conditional state support.
134 *
135 * Revision 1.6 1998/03/04 01:13:46 dehman
136 * Adjusted MAX_PARM_STRUCT_SIZE to 15 so that we get word alignment. (For now)
137 *
138 * Revision 1.5 1998/02/07 22:07:53 bcassidy
139 * 1. Remove current_event and busy elements from FSMDescriptor.
140 * [Efficiency improvement in processEventWithParmStruct.]
141 *
142 * Revision 1.4 1998/02/07 04:03:44 dehman
143 * Fixed structure alignment problem/bug by changing the order
144 * of member declaration in FSMTblEvent.
145 *
146 * Revision 1.3 1998/02/04 01:08:12 bcassidy
147 * 1. Fix const keywords for FSM name, event_name and state_name.
148 *
149 * Revision 1.2 1998/02/03 22:23:47 bcassidy
150 * 1. Add tracing support.
151 *
152 * Revision 1.1 1998/01/31 22:37:35 bcassidy
153 * Initial revision
154 *
155 *
156 ************************************************************************/
157
158#ifndef __FSM_H_
159#define __FSM_H_
160
161#include "ottsdefs.h"
162#include "sysdefs.h"
163
164
165
166
167/* Constants */
168/* <TBD: Due to a GNU compiler-bug, the MAX_PARM_STRUCT_SIZE must be set
169 to: ((a multiple of 4) - 1).> */
170#define MAX_PARM_STRUCT_SIZE 39
171
172#define EVENTQ_SIZE 5
173
174#define UNEXPECTED_EVENT_AC 0xFE
175#define NO_EVENT 0xFF
176#define NO_ACTION 0
177
178#ifndef FSM_COND_ST
179#define FSM_COND_ST 1
180#endif
181
182#ifdef FSM_COND_ST
183#define CONDITIONAL_STATE 0x7F
184#define CHANGE_STATE_ALWAYS KAL_TRUE
185#define ACTION_DRIVES_CHANGE KAL_FALSE
186#endif
187
188/* Define FSM Base Elements */
189typedef kal_uint8 FSMState;
190typedef kal_uint8 FSMEvent;
191typedef kal_uint8 FSMAction;
192
193/* <TBD: Due to a GNU compiler-bug, 'plist' must be the first entry in this structure.> */
194typedef struct
195{
196 kal_uint8 plist[ MAX_PARM_STRUCT_SIZE ];
197 FSMEvent event;
198} FSMTBLEvent;
199
200typedef kal_uint8 ProtocolEngineID;
201
202/* Define FSM Descriptor Structure */
203
204struct FSMDescriptorStruct
205{
206 FSMTBLEvent eventQ[EVENTQ_SIZE]; /* Event Queue */
207 kal_uint8 put_index; /* Put Index for Q */
208 kal_uint8 get_index; /* Get Index for Q */
209 kal_uint8 num_in_queue; /* Current number of entries in the queue */
210
211 FSMState state; /* Current State */
212
213#ifdef FSM_COND_ST
214 FSMState conditional_state; /* Conditional State */
215 kal_bool change_flag; /* If TRUE, always change state */
216#endif
217
218 FSMState num_state; /* Number of States */
219 FSMEvent num_event; /* Number of Events */
220 FSMAction num_action; /* Number of Actions */
221
222 FSMState* state_table; /* State Table */
223 kal_uint16 state_size; /* State Entry Size */
224 kal_uint16 event_size; /* Event Entry Size */
225
226 /* Ptr to Action Handler Function */
227 kal_bool ( *action_handler )( struct FSMDescriptorStruct* fsm, FSMAction action );
228
229 /* Instance Related User Parameters */
230 void* user_parms;
231
232 kal_uint8 id;
233 ProtocolEngineID engineId;
234 kal_bool extendedTracing;
235
236#if (ITRACE_TYPE == TOKEN_TRACING)
237 kal_uint8 fsmNameTableId;
238 kal_uint8 stateNameTableId;
239 kal_uint8 eventNameTableId;
240
241#else /* !TOKEN_TRACING */
242 const kal_char** fsmNameTable;
243 const kal_char** stateNameTable;
244 const kal_char** eventNameTable;
245#endif /* ITRACE_TYPE */
246};
247
248/* Useful Typedefs */
249typedef struct FSMDescriptorStruct FSMDescriptor;
250typedef FSMDescriptor* FSMPtr;
251typedef kal_bool (*FSMActionFuncPtr) (FSMPtr fsm, FSMAction action);
252
253
254/* Generic FSM Processing Functions */
255void initializeFSM( FSMPtr fsm, FSMState* table,
256 kal_uint8 num_state, kal_uint8 num_event, kal_uint8 num_action,
257 FSMState initial_state, FSMActionFuncPtr action_handler,
258 void* user_parms );
259
260void processEventWithNoParms( FSMPtr fsm, FSMEvent event );
261void processEventWithParmStruct( FSMPtr fsm, FSMEvent event, void* parm_struct, kal_uint8 size );
262
263#ifdef FSM_COND_ST
264void setConditionalState( FSMPtr fsm, FSMState cond_state );
265void setChangeFlag( FSMPtr fsm, kal_bool change_always );
266#endif
267
268/* Event Parameter Query */
269void* getEventParameters( FSMPtr fsm );
270FSMEvent getCurrentEvent( FSMPtr fsm );
271
272/* FSM Table Generation Macros */
273
274/* The macro MAKEFSMTable creates a FSM table, just provide the name of the
275 * table, the number of states, the number of events and the number of
276 * actions that are to be executed to get to that state.
277 */
278
279/*
280 * Example of make fsm table macro
281 * Intialize table
282 * MAKEFSMTable(callFSM,2,3,1) = {{1,b},{2,c},{3,d},{4,e},{5,f},{0,a}};
283 * where 0-5 represent the different states
284 * where a-e represent different actions
285 * Note this assumes that the states and actions are the same size
286 * The + 1 is for the state entry
287 */
288#define MAKEFSMTable(tableName,numStates,numEvents,numActions) \
289static CONST kal_uint8 ROM tableName[numStates][numEvents][numActions + 1]
290
291
292/* The macro FSMTable is used to Instantiate a FSMTable
293 * It just does the required type casting
294 *
295 * Example
296 * Instantiate a FSMTBL with FSMTable callFSM
297 * FSMTBL callFSMTable(FSMTable(callFSM),2,3,1,0);
298 */
299#define FSMTable(tableName) (FSMState*)&tableName
300
301#ifdef TRACEON
302void setFSMTraceInfo (FSMPtr fsm, kal_uint8 fsmId, ProtocolEngineID engineId);
303
304#define FSMTRACE_INFO(fsm, id, engineId) setFSMTraceInfo(fsm, id, engineId)
305
306#if (ITRACE_TYPE == TOKEN_TRACING)
307void setFSMExtendedTraceInfo (FSMPtr fsm, kal_uint8 fsmNameTableId, kal_uint8 fsmId,
308 ProtocolEngineID engineId,
309 kal_uint8 stateTableId, kal_uint8 eventTableId);
310
311#define FSMEXTTRACE_INFO(fsm, engineId, fsmNameTable, fsmNameTableId, fsmId, \
312 stateNameTable, stateNameTableId, \
313 eventNameTable, eventNameTableId) \
314 setFSMExtendedTraceInfo(fsm, fsmNameTableId, fsmId, engineId, \
315 stateNameTableId, eventNameTableId)
316
317#else /* !TOKEN_TRACING */
318void setFSMExtendedTraceInfo (FSMPtr fsm, const kal_char* fsmNameTable[],
319 kal_uint8 fsmId, ProtocolEngineID engineId,
320 const kal_char* stateTable[],
321 const kal_char* eventTable[]);
322
323#define FSMEXTTRACE_INFO(fsm, engineId, fsmNameTable, fsmNameTableId, fsmId, \
324 stateNameTable, stateNameTableId, \
325 eventNameTable, eventNameTableId) \
326 setFSMExtendedTraceInfo(fsm, fsmNameTable, fsmId, engineId, \
327 stateNameTable, eventNameTable)
328#endif /* ITRACE_TYPE */
329
330#else /* !TRACEON */
331#define FSMTRACE_INFO(fsm, id, engineId)
332#define FSMEXTTRACE_INFO(fsm, engineId, fsmNameTable, fsmNameTableId, fsmId, \
333 stateNameTable, stateNameTableId, \
334 eventNameTable, eventNameTableId)
335#endif /* TRACEON */
336
337
338
339
340
341
342#endif /* __FSM_H_ */