Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e8efe9edf0b07742f9134f6b12c79e1e78ec2829 (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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/**
 * @author generated by eTrice
 *
 * Source File of ActorClass ATimingService
 * 
 */

#include "ATimingService.h"
#include "common/debugging/DebuggingService.h"
#include <iostream>

using namespace etRuntime;


ATimingService::ATimingService(etRuntime::IRTObject* parent, std::string name, const std::vector<std::vector<etRuntime::Address> >& port_addr, 
 						  											 const std::vector<std::vector<etRuntime::Address> >& peer_addr)
:  ActorClassBase( parent, name, port_addr[0][0], peer_addr[0][0]),
timer(*this, this, "timer", IFITEM_timer, port_addr[IFITEM_timer], peer_addr[IFITEM_timer]),
tcbs(),
usedTcbsRoot(0),
freeTcbsRoot(0)
{
	setClassName("ATimingService");
	// initialize attributes
	for (int i=0;i<10;i++){
		tcbs[i] = etTimerControlBlock();
	}

	getMsgsvc()->addAsyncActor(*this);
}

void ATimingService::init(){
	initUser();
}
	
void ATimingService::start(){
	startUser();
}
	
void ATimingService::stop(){
	stopUser();
}

void ATimingService::destroy(){
}


std::string ATimingService::s_stateStrings[] = {"<no state>","<top>","Operational"
};

		
void ATimingService::setState(int new_state) {
	DebuggingService::getInstance().addActorState(*this, s_stateStrings[new_state]);
	if (s_stateStrings[new_state]!="Idle") {
		std::cout << getInstancePath() << " -> " << s_stateStrings[new_state] << std::endl;
	}	
	m_state = new_state;
}

/* Entry and Exit Codes */
void ATimingService::entry_Operational() {
	// prepare
}
 void ATimingService::do_Operational() {
	// maintain timers
	etTimerControlBlock* temp;
	etTargetTime_t t;
	
	getTimeFromTarget(&t);
	while (usedTcbsRoot !=0 ){
		if (isTimeGreater(&t,&(usedTcbsRoot->expTime))){
			timer.get(usedTcbsRoot->portIdx).timeout();
			temp=usedTcbsRoot;
			usedTcbsRoot=usedTcbsRoot->next;
			if((temp->pTime.sec==0)&&(temp->pTime.nSec==0)){
				// single shot timer
				returnTcb(temp);
			}else{
				// periodic timer
				addTime(&temp->expTime,&temp->pTime);
				putTcbToUsedList(temp);
				}
			}else{
				break;
				}
		}
}

/* Action Codes */
void ATimingService::action_TRANS_INITIAL_TO__Operational() {
	int i;
	usedTcbsRoot=0;
	freeTcbsRoot=&tcbs[0];
	tcbs[ET_NB_OF_TCBS-1].next=0;
	for (i=0;i<ET_NB_OF_TCBS-1;i++){
		tcbs[i].next=&tcbs[i+1];
		}
}
void ATimingService::action_TRANS_tr1_FROM_Operational_TO_Operational_BY_startTimeouttimer_tr1(const InterfaceItemBase* ifitem, uint32 time) {
	etTimerControlBlock* timer = getTcb();
	etTargetTime_t t;
	if (timer!= 0){
		t.sec=time/1000;
		t.nSec=(time%1000)*1000000L;
		timer->pTime.sec = 0;
		timer->pTime.nSec = 0;
		timer->portIdx=ifitem->getIdx();
		getTimeFromTarget(&(timer->expTime));
		addTime(&(timer->expTime),&t);
		putTcbToUsedList(timer);
		}
}
void ATimingService::action_TRANS_tr3_FROM_Operational_TO_Operational_BY_startTimertimer_tr3(const InterfaceItemBase* ifitem, uint32 time) {
	etTimerControlBlock* timer = getTcb();
	etTargetTime_t t;
	if (timer!= 0){
		t.sec=time/1000;
		t.nSec=(time%1000)*1000000L;
		timer->pTime = t;
		timer->portIdx=ifitem->getIdx();
		getTimeFromTarget(&(timer->expTime));
		addTime(&(timer->expTime),&t);
		putTcbToUsedList(timer);
		}
}
void ATimingService::action_TRANS_tr4_FROM_Operational_TO_Operational_BY_killtimer_tr4(const InterfaceItemBase* ifitem) {
	removeTcbFromUsedList(ifitem->getIdx());
}

/**
 * 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)
 */
void ATimingService::exitTo(int current, int to, bool handler) {
	while (current!=to) {
		switch (current) {
			case STATE_Operational:
				this->history[STATE_TOP] = STATE_Operational;
				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
 */
int ATimingService::executeTransitionChain(int chain, const InterfaceItemBase* ifitem, void* generic_data) {
	switch (chain) {
		case CHAIN_TRANS_INITIAL_TO__Operational:
		{
			action_TRANS_INITIAL_TO__Operational();
			return STATE_Operational;
		}
		case CHAIN_TRANS_tr1_FROM_Operational_TO_Operational_BY_startTimeouttimer_tr1:
		{
			uint32 time = ((uint32) generic_data);
			action_TRANS_tr1_FROM_Operational_TO_Operational_BY_startTimeouttimer_tr1(ifitem, time);
			return STATE_Operational;
		}
		case CHAIN_TRANS_tr3_FROM_Operational_TO_Operational_BY_startTimertimer_tr3:
		{
			uint32 time = ((uint32) generic_data);
			action_TRANS_tr3_FROM_Operational_TO_Operational_BY_startTimertimer_tr3(ifitem, time);
			return STATE_Operational;
		}
		case CHAIN_TRANS_tr4_FROM_Operational_TO_Operational_BY_killtimer_tr4:
		{
			action_TRANS_tr4_FROM_Operational_TO_Operational_BY_killtimer_tr4(ifitem);
			return STATE_Operational;
		}
	}
	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
 */
int ATimingService::enterHistory(int state, bool handler, bool skip_entry) {
	while (true) {
		switch (state) {
			case STATE_Operational:
				if (!(skip_entry || handler)) entry_Operational();
				// in leaf state: return state id
				return STATE_Operational;
			case STATE_TOP:
				state = this->history[STATE_TOP];
				break;
		}
		skip_entry = false;
	}
	//return NO_STATE; // required by CDT but detected as unreachable by JDT because of while (true)
}

void ATimingService::executeInitTransition() {
	int chain = CHAIN_TRANS_INITIAL_TO__Operational;
	int next = executeTransitionChain(chain, 0, 0);
	next = enterHistory(next, false, false);
	setState(next);
}

/* receiveEvent contains the main implementation of the FSM */
void ATimingService::receiveEvent(InterfaceItemBase* ifitem, int evt, void* generic_data) {
	int trigger = (ifitem==0)? POLLING : ifitem->getLocalId() + EVT_SHIFT*evt;
	int chain = NOT_CAUGHT;
	int catching_state = NO_STATE;
	bool is_handler = false;
	bool skip_entry = false;
	
	if (!handleSystemEvent(ifitem, evt, generic_data)) {
		switch (getState()) {
			case STATE_Operational:
				switch(trigger) {
				case POLLING:
					do_Operational();
					break;
					case TRIG_timer__startTimeout:
						{
							chain = CHAIN_TRANS_tr1_FROM_Operational_TO_Operational_BY_startTimeouttimer_tr1;
							catching_state = STATE_TOP;
						}
					break;
					case TRIG_timer__startTimer:
						{
							chain = CHAIN_TRANS_tr3_FROM_Operational_TO_Operational_BY_startTimertimer_tr3;
							catching_state = STATE_TOP;
						}
					break;
					case TRIG_timer__kill:
						{
							chain = CHAIN_TRANS_tr4_FROM_Operational_TO_Operational_BY_killtimer_tr4;
							catching_state = STATE_TOP;
						}
					break;
				}
				break;
		}
	}
	if (chain != NOT_CAUGHT) {
		exitTo(getState(), catching_state, is_handler);
		int next = executeTransitionChain(chain, ifitem, generic_data);
		next = enterHistory(next, is_handler, skip_entry);
		setState(next);
	}
}
	 
//******************************************
// END of generated code for FSM
//******************************************

Back to the top