Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c1e56cd717c8c8f539194c259be2ba7ef7f063db (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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
RoomModel room.basic.service.timing {

	import room.basic.types.* from "Types.room"

	async ActorClass ATimingService {
		Interface {
			SPP timer: PTimer
		}
		Structure {
			usercode1 {
				"#include \"osal/etTime.h\" "
				"#define ET_NB_OF_TCBS 30"
				"typedef struct etTCB etTimerControlBlock;"
				"struct etTCB {"
				"	etTime expTime;"
				"	etTime pTime;"
				"	int32 portIdx;"
				"	etTimerControlBlock* next;"
				"};"
			}
			usercode2 {
				"// uc2"
			} usercode3 {
				"// uc3"
			}
			Attribute tcbs [ 30 ]: tcb
			Attribute usedTcbsRoot: tcb ref
			Attribute freeTcbsRoot: tcb ref
			ServiceImplementation of timer
		}
		Behavior {
			ctor {
				"memset(tcbs.getData(), sizeof(tcbs), 0);"
			}
			Operation getTcb(): tcb ref {
				"etTimerControlBlock* temp = freeTcbsRoot;"
				""
				"if (freeTcbsRoot != 0) {"
				"	freeTcbsRoot = freeTcbsRoot->next;"
				"	temp->next = 0;"
				"}"
				"return temp;"
				""
			}
			Operation returnTcb(block: tcb ref) {
				"block->next = freeTcbsRoot;"
				"freeTcbsRoot = block;"
			}
			Operation removeTcbFromUsedList(idx: int32) {
				"etTimerControlBlock* temp = usedTcbsRoot;"
				"etTimerControlBlock* temp2 = usedTcbsRoot;"
				""
				"if (temp == 0)"
				"	return;"
				""
				"if (usedTcbsRoot->portIdx == idx) {"
				"	/* element found, the first one */"
				"	usedTcbsRoot = usedTcbsRoot->next;"
				"	returnTcb(temp);"
				"	return;"
				"}"
				""
				"temp = temp->next;"
				"while (temp != 0) {"
				"	if (temp->portIdx == idx) {"
				"		temp2->next = temp->next;"
				"		returnTcb(temp);"
				"		return;"
				"	} else {"
				"		/* try next */"
				"		temp2 = temp;"
				"		temp = temp->next;"
				"	}"
				"}"
			}
			Operation putTcbToUsedList(block: tcb ref) {
				"etTimerControlBlock* temp = usedTcbsRoot;"
				"etTimerControlBlock* temp2 = usedTcbsRoot;"
				""
				"if (temp == 0) {"
				"	/* list empty put new block to root */"
				"	block->next = 0;"
				"	usedTcbsRoot = block;"
				"	return;"
				"}"
				""
				"while (1) {"
				"	if (temp != 0) {"
				"		if (isTimeGreater(&block->expTime, &temp->expTime)) {"
				"			/* try next position */"
				"			temp2 = temp;"
				"			temp = temp->next;"
				"		} else {"
				"			/* right position found */"
				"			block->next = temp;"
				"			if (temp == usedTcbsRoot) {"
				"				usedTcbsRoot = block;"
				"			} else {"
				"				temp2->next = block;"
				"			}"
				"			return;"
				"		}"
				"	} else {"
				"		/* end of list reached */"
				"		block->next = 0;"
				"		temp2->next = block;"
				"		return;"
				"	}"
				"}"
			}
			Operation isTimeGreater(t1: targetTime ref, t2: targetTime ref): boolean {
				"if (t1->sec > t2->sec)"
				"	return ET_TRUE;"
				"if (t1->sec < t2->sec)"
				"	return ET_FALSE;"
				"if (t1->nSec > t2->nSec)"
				"	return ET_TRUE;"
				"return ET_FALSE;"
			}
			Operation addTime(t1: targetTime ref, t2: targetTime ref) {
				"t1->sec += t2->sec;"
				"t1->nSec += t2->nSec;"
				"while (t1->nSec >= 1000000000L) {"
				"	t1->sec++;"
				"	t1->nSec -= 1000000000L;"
				"}"
			}

			//			Operation printList(){
			//				"etTimerControlBlock* temp=usedTcbsRoot;"
			//				"	printf(\"list: \");"
			//				"	while (temp!=0){"
			//				"		printf(\"(%ld,%ld),\",temp->expTime.sec,temp->expTime.nSec);"
			//				"		temp=temp->next;"
			//				"	}"
			//				"	printf(\"\\n\");"
			//			}

			StateMachine {
				Transition tr0: initial -> Operational {
					action {
						"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++) {"
						"\ttcbs[i].next = &tcbs[i + 1];"
						"}"
					}
				}
				Transition tr1: Operational -> Operational {
					triggers {
						<startTimeout: timer>
					}
					action {
						"etTimerControlBlock* timer = getTcb();"
						"etTime t;"
						"if (timer != 0) {"
						"\tt.sec = time / 1000;"
						"\tt.nSec = (time % 1000) * 1000000L;"
						"\ttimer->pTime.sec = 0;"
						"\ttimer->pTime.nSec = 0;"
						"\ttimer->portIdx = ifitem->getIdx();"
						"\tgetTimeFromTarget(&(timer->expTime));"
						"\taddTime(&(timer->expTime), &t);"
						"\tputTcbToUsedList(timer);"
						"}"
					}
				}
				Transition tr3: Operational -> Operational {
					triggers {
						<startTimer: timer>
					}
					action {
						"etTimerControlBlock* timer = getTcb();"
						"etTime t;"
						"if (timer != 0) {"
						"\tt.sec = time / 1000;"
						"\tt.nSec = (time % 1000) * 1000000L;"
						"\ttimer->pTime = t;"
						"\ttimer->portIdx = ifitem->getIdx();"
						"\tgetTimeFromTarget(&(timer->expTime));"
						"\taddTime(&(timer->expTime), &t);"
						"\tputTcbToUsedList(timer);"
						"}"
					}
				}
				Transition tr4: Operational -> Operational {
					triggers {
						<kill: timer>
					}
					action {
						"removeTcbFromUsedList(ifitem->getIdx());"
					}
				}
				State Operational {
					entry {
						"// prepare"
					} do {
						"// maintain timers"
						"etTimerControlBlock* temp;"
						"etTime t;"
						""
						"getTimeFromTarget(&t);"
						"while (usedTcbsRoot != 0) {"
						"\tif (isTimeGreater(&t, &(usedTcbsRoot->expTime))) {"
						"\t\ttimer.get(usedTcbsRoot->portIdx).timeout();"
						"\t\ttemp = usedTcbsRoot;"
						"\t\tusedTcbsRoot = usedTcbsRoot->next;"
						"\t\tif ((temp->pTime.sec == 0) && (temp->pTime.nSec == 0)) {"
						"\t\t\t// single shot timer"
						"\t\t\treturnTcb(temp);"
						"\t\t} else {"
						"\t\t\t// periodic timer"
						"\t\t\taddTime(&temp->expTime, &temp->pTime);"
						"\t\t\tputTcbToUsedList(temp);"
						"\t\t}"
						"\t} else {"
						"\t\tbreak;"
						"\t}"
						"}"
					}
				}
			}
		}
	}

	ProtocolClass PTimer {
		usercode1 {
			"#define ET_TIMER_RUNNING	0x01"
			"#define ET_TIMER_PERIODIC	0x02"
		}
		usercode2 {
			"//uc2		"
		}
		incoming {
			Message startTimer(time: uint32)
			Message startTimeout(time: uint32)
			Message kill()
		}
		outgoing {
			Message timeout()
		}
		conjugated PortClass
		{
			Attribute status: int8 = "0"
			handle
			incoming startTimer {
			"if (status==0){
				status=ET_TIMER_RUNNING | ET_TIMER_PERIODIC;
				startTimer_impl(time);
			}
			"
			}
			handle
			incoming startTimeout {
			"if (status==0){
				status = ET_TIMER_RUNNING;
				startTimeout_impl(time);
			}
			"
			}
			handle
			outgoing timeout {
			"
			//TODO: clear active bit in case of single shot timer
			if (status!=0){
				if (status==ET_TIMER_RUNNING){
					// single shot timer
					status=0;
				}
				// msg to fsm
				getActor()->receiveEvent(this, msg->getEvtId(),	msg->getData());
			}
			"
			}
			handle
			incoming kill {
			"
			if (status!=0){
				status=0;
				kill_impl();
			}
			"
			}
		}
	}

	ExternalType tcb -> "etTimerControlBlock"
	ExternalType targetTime -> "etTime"
}

Back to the top