Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 027ddf644418943157650dcf0b0b6f30ee2af605 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/**
 * @author generated by eTrice
 *
 * Source File of ActorClass AFileReader
 * 
 */

#include "AFileReader.h"

#include "modelbase/etActor.h"
#include "debugging/etLogger.h"
#include "debugging/etMSCLogger.h"
#include "etUnit/etUnit.h"
#include "platform/etMemory.h"

#include "FileReaderProtocol.h"

/*--------------------- begin user code ---------------------*/
#include <stdio.h>
/*--------------------- end user code ---------------------*/

/* interface item IDs */
enum interface_items {
	IFITEM_outPort = 1
};


/* state IDs */
enum state_ids {
	NO_STATE = 0,
	STATE_TOP = 1,
	STATE_reading = 2
};

/* transition chains */
enum chain_ids {
	CHAIN_TRANS_INITIAL_TO__reading = 1,
	CHAIN_TRANS_tr0_FROM_reading_TO_reading_BY_getNextCharoutPort_tr0 = 2
};

/* triggers */
enum triggers {
	POLLING = 0,
	TRIG_outPort__getNextChar = IFITEM_outPort + EVT_SHIFT*FileReaderProtocol_IN_getNextChar
};


static void setState(AFileReader* self, int new_state) {
	self->state = new_state;
}

/* Entry and Exit Codes */

/* Action Codes */
static void action_TRANS_INITIAL_TO__reading(AFileReader* self) {
	if ((self->f /* ORIG: f */ = fopen("test.txt","r")) != 0) {
		printf("file open ok !\r\n");
		}
		else {
		printf("file not found !\r\n");
		SubSysClass_shutdown();
		}
}
static void action_TRANS_tr0_FROM_reading_TO_reading_BY_getNextCharoutPort_tr0(AFileReader* self, InterfaceItemBase ifitem) {
	int8 c;
	if ((c=fgetc(self->f /* ORIG: f */)) != EOF) {
		FileReaderProtocolPort_nextChar(&self->constData->outPort, c) /* ORIG: outPort.nextChar(c) */;
		}
		else {
		fclose(self->f /* ORIG: f */);
		printf("file closed !\r\n");
		SubSysClass_shutdown();
		}
}

/**
 * calls exit codes while exiting from the current state to one of its
 * parent states while remembering the history
 * @param current - the current state
 * @param to - the final parent state
 * @param handler - entry and exit codes are called only if not handler (for handler TransitionPoints)
 */
static void exitTo(AFileReader* self, int current, int to, boolean handler) {
	while (current!=to) {
		switch (current) {
			case STATE_reading:
				self->history[STATE_TOP] = STATE_reading;
				current = STATE_TOP;
				break;
		}
	}
}

/**
 * calls action, entry and exit codes along a transition chain. The generic data are cast to typed data
 * matching the trigger of this chain. The ID of the final state is returned
 * @param chain - the chain ID
 * @param generic_data - the generic data pointer
 * @return the ID of the final state
 */
static int executeTransitionChain(AFileReader* self, int chain, InterfaceItemBase ifitem, void* generic_data) {
	switch (chain) {
		case CHAIN_TRANS_INITIAL_TO__reading:
		{
			action_TRANS_INITIAL_TO__reading(self);
			return STATE_reading;
		}
		case CHAIN_TRANS_tr0_FROM_reading_TO_reading_BY_getNextCharoutPort_tr0:
		{
			action_TRANS_tr0_FROM_reading_TO_reading_BY_getNextCharoutPort_tr0(self, ifitem);
			return STATE_reading;
		}
	}
	return NO_STATE;
}

/**
 * calls entry codes while entering a state's history. The ID of the final leaf state is returned
 * @param state - the state which is entered
 * @param handler - entry code is executed if not handler
 * @return - the ID of the final leaf state
 */
static int enterHistory(AFileReader* self, int state, boolean handler, boolean skip_entry) {
	while (TRUE) {
		switch (state) {
			case STATE_reading:
				// in leaf state: return state id
				return STATE_reading;
			case STATE_TOP:
				state = self->history[STATE_TOP];
				break;
		}
		skip_entry = FALSE;
	}
	//return NO_STATE; // required by CDT but detected as unreachable by JDT because of while (true)
}

static void executeInitTransition(AFileReader* self) {
	int chain = CHAIN_TRANS_INITIAL_TO__reading;
	int next = executeTransitionChain(self, chain, NULL, NULL);
	next = enterHistory(self, next, FALSE, FALSE);
	setState(self, next);
}

/* receiveEvent contains the main implementation of the FSM */
static void receiveEvent(AFileReader* self, InterfaceItemBase ifitem, int evt, void* generic_data) {
	int trigger = ifitem->localId + EVT_SHIFT*evt;
	int chain = NOT_CAUGHT;
	int catching_state = NO_STATE;
	boolean is_handler = FALSE;
	boolean skip_entry = FALSE;
	
	if (!handleSystemEvent(ifitem, evt, generic_data)) {
		switch (self->state) {
			case STATE_reading:
				switch(trigger) {
					case TRIG_outPort__getNextChar:
						{
							chain = CHAIN_TRANS_tr0_FROM_reading_TO_reading_BY_getNextCharoutPort_tr0;
							catching_state = STATE_TOP;
						}
					break;
				}
				break;
		}
	}
	if (chain != NOT_CAUGHT) {
		exitTo(self, self->state, catching_state, is_handler);
		int next = executeTransitionChain(self, chain, ifitem, generic_data);
		next = enterHistory(self, next, is_handler, skip_entry);
		setState(self, next);
	}
}
	 
//******************************************
// END of generated code for FSM
//******************************************

void AFileReader_init(AFileReader* self){
	ET_MSC_LOGGER_SYNC_ENTRY("AFileReader", "init")
	self->state = STATE_TOP;
	{
		int i;
		for (i=0; i<AFILEREADER_HISTORY_SIZE; ++i)
			self->history[i] = NO_STATE;
	}
	executeInitTransition(self);
	ET_MSC_LOGGER_SYNC_EXIT
}


void AFileReader_receiveMessage(void* self, void* ifitem, const etMessage* msg){
	ET_MSC_LOGGER_SYNC_ENTRY("AFileReader", "_receiveMessage")
	
	receiveEvent(self, (etPort*)ifitem, msg->evtID, (void*)(((char*)msg)+MEM_CEIL(sizeof(etMessage))));
	
	ET_MSC_LOGGER_SYNC_EXIT
}


/*--------------------- operations ---------------------*/

Back to the top