Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 499bcffe16f9677a24d636a760d20d1fc78e2c92 (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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*******************************************************************************
 * Copyright (c) 2011 protos software gmbh (http://www.protos.de).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * CONTRIBUTORS:
 * 		Henrik Rentz-Reichert (initial contribution)
 * 		Thomas Schuetz (changed for C code generator)
 *
 *******************************************************************************/

package org.eclipse.etrice.generator.c.gen

import com.google.inject.Inject
import com.google.inject.Singleton
import org.eclipse.etrice.core.fsm.fSM.ComponentCommunicationType
import org.eclipse.etrice.core.genmodel.etricegen.ExpandedActorClass
import org.eclipse.etrice.core.genmodel.etricegen.Root
import org.eclipse.etrice.core.genmodel.fsm.FsmGenExtensions
import org.eclipse.etrice.core.room.CommunicationType
import org.eclipse.etrice.core.room.Operation
import org.eclipse.etrice.core.room.ProtocolClass
import org.eclipse.etrice.core.room.RoomModel
import org.eclipse.etrice.generator.c.Main
import org.eclipse.etrice.generator.generic.GenericActorClassGenerator
import org.eclipse.etrice.generator.generic.ILanguageExtension
import org.eclipse.etrice.generator.generic.ProcedureHelpers
import org.eclipse.etrice.generator.generic.RoomExtensions
import org.eclipse.etrice.generator.base.io.IGeneratorFileIO
import org.eclipse.etrice.generator.c.setup.GeneratorOptionsHelper
import org.eclipse.etrice.generator.base.logging.ILogger

@Singleton
class ActorClassGen extends GenericActorClassGenerator {

	@Inject protected extension RoomExtensions
	@Inject protected extension CExtensions
	@Inject protected extension ProcedureHelpers
	@Inject protected extension StateMachineGen
	@Inject protected extension GeneratorOptionsHelper

	@Inject protected ILanguageExtension langExt
	@Inject protected IGeneratorFileIO fileIO
	@Inject protected ILogger logger

	def doGenerate(Root root) {
		for (xpac: root.actorClasses.map[root.getExpandedActorClass(it)]) {
			val path = xpac.actorClass.generationTargetPath+xpac.actorClass.getPath
			val infopath = xpac.actorClass.generationInfoPath+xpac.actorClass.getPath
			var file = xpac.actorClass.getCHeaderFileName

			// header file
			fileIO.generateFile("generating ActorClass header", path, infopath, file, root.generateHeaderFile(xpac))

			// utils file
			file = xpac.actorClass.getCUtilsFileName
			fileIO.generateFile("generating ActorClass utils", path, infopath, file, root.generateUtilsFile(xpac))

			// source file
			if (xpac.actorClass.isBehaviorAnnotationPresent("BehaviorManual")) {
				logger.logInfo("omitting ActorClass source for '"+xpac.actorClass.name+"' since @BehaviorManual is specified")
			}
			else {
				file = xpac.actorClass.getCSourceFileName
				fileIO.generateFile("generating ActorClass source", path, infopath, file, root.generateSourceFile(xpac))
			}
		}
	}

	def protected generateHeaderFile(Root root, ExpandedActorClass xpac) {
		val ac = xpac.actorClass
		val eventPorts = ac.allEndPorts.filter(p|(p.protocol as ProtocolClass).commType==CommunicationType::EVENT_DRIVEN)
		val sendPorts = ac.allEndPorts.filter(p|(p.protocol as ProtocolClass).commType==CommunicationType::DATA_DRIVEN && p.conjugated)
		val recvPorts = ac.allEndPorts.filter(p|(p.protocol as ProtocolClass).commType==CommunicationType::DATA_DRIVEN && !p.conjugated)
		val dataDriven = ac.commType==ComponentCommunicationType::DATA_DRIVEN
		val async = ac.commType==ComponentCommunicationType::ASYNCHRONOUS
		val hasConstData = !(eventPorts.empty && recvPorts.empty && ac.allSAPs.empty && ac.allServiceImplementations.empty)
							|| Main::settings.generateMSCInstrumentation
		val isEmptyStateGraph = FsmGenExtensions.isEmpty(xpac.graphContainer.graph)
		val hasVarData = !(sendPorts.empty && ac.allAttributes.empty && isEmptyStateGraph && !hasConstData)

	'''
		/**
		 * @author generated by eTrice
		 *
		 * Header File of ActorClass ac.name
		 *
		 */

		generateIncludeGuardBegin(ac)

		#include "etDatatypes.h"
		#include "messaging/etMessage.h"

		/* include all referenced room classes */
		FOR rc : (root.getReferencedDataClasses(ac) + root.getReferencedEnumClasses(ac) + root.getReferencedProtocolClasses(ac)).sortBy[name]
			#include rc.includePath
		ENDFOR

		ac.userCode(1, true)

		typedef struct ac.name ac.name;

		/* const part of ActorClass (ROM) */
		IF hasConstData
			typedef struct ac.name_const {
				IF Main::settings.generateMSCInstrumentation
					const char* instName;

				ENDIF
				/* simple ports */
				FOR ep : eventPorts
					IF ep.multiplicity==1
						const ep.getPortClassName() ep.name;
					ENDIF
				ENDFOR

				/* data receive ports */
				FOR ep : recvPorts
					IF ep.multiplicity==1
						const ep.getPortClassName() ep.name;
					ENDIF
				ENDFOR

				/* saps */
				FOR sap: ac.allSAPs
					const sap.getPortClassName() sap.name;
				ENDFOR

				/* replicated ports */
				FOR ep : ac.allEndPorts
					IF ep.multiplicity!=1
						const etReplPort ep.name;
					ENDIF
				ENDFOR

				/* services */
				FOR svc : ac.allServiceImplementations
					const etReplPort svc.spp.name;
				ENDFOR
			} ac.name_const;
		ELSE
			/* this actor class has no ports and thus no constant data */
		ENDIF

		IF !isEmptyStateGraph

			xpac.genHeaderConstants
		ENDIF

		/* variable part of ActorClass (RAM) */
		IF hasVarData
			struct ac.name {
				IF hasConstData
					const ac.name_const* const constData;

				ENDIF
				/* data send ports */
				FOR ep : sendPorts
					IF ep.multiplicity==1
						ep.getPortClassName() ep.name;
					ENDIF
				ENDFOR

				ac.allAttributes.attributes

				IF !isEmptyStateGraph

					xpac.genDataMembers
				ENDIF
			};
		ELSE
			struct ac.name {
				/* This actor class has no data at all.
				   But the private actor instance data is passed to all life cycle functions.
				   By introducing the dummy data we keep this case simple
				*/
				int dummy;
			};
		ENDIF

		void ac.name_init(ac.name* self);

		void ac.name_receiveMessage(void* self, const void* ifitem, const etMessage* msg);

		IF dataDriven || async
			void ac.name_execute(ac.name* self);
		ENDIF

		ac.userStructorsDeclaration

		ac.latestOperations.operationsDeclaration(ac.name)

		ac.userCode(2, true)

		generateIncludeGuardEnd(ac)

	'''
	}

	def protected generateUtilsFile(Root root, ExpandedActorClass xpac) {
		val ac = xpac.actorClass
		val eventPorts = ac.allEndPorts.filter(p|(p.protocol as ProtocolClass).commType==CommunicationType::EVENT_DRIVEN)
		val replEventPorts = eventPorts.filter[multiplicity!=1]
		val sendPorts = ac.allEndPorts.filter(p|(p.protocol as ProtocolClass).commType==CommunicationType::DATA_DRIVEN &&  p.conjugated && p.multiplicity==1)
		val recvPorts = ac.allEndPorts.filter(p|(p.protocol as ProtocolClass).commType==CommunicationType::DATA_DRIVEN && !p.conjugated && p.multiplicity==1)
		val portsWithOperations = ac.allInterfaceItems.filter(p|p.portClass!==null && p.portClass.operations.size>0)
		val filename = (ac.eContainer as RoomModel).name.replaceAll("\\.","_")+"_"+ac.name+"_Utils"

	'''
		/**
		 * @author generated by eTrice
		 *
		 * Utils File of ActorClass ac.name
		 *
		 */

		generateIncludeGuardBegin(filename)

		#include ac.includePath

		/*
		 * access macros for ports, operations and attributes
		*/

		/* simple event ports */
		FOR ep : eventPorts.filter[multiplicity==1]
			FOR msg : ep.outgoing
				val data1 = if (msg.data!==null) "data" else ""
				val data2 = if (msg.data!==null) ", data" else ""
				#define ep.name_msg.name(data1) ep.portClassName_msg.name(&self->constData->ep.namedata2)
			ENDFOR
		ENDFOR

		/* data receive ports */
		FOR ep : recvPorts
			FOR msg : ep.incoming
				#define ep.name_msg.name ep.portClassName_msg.name_get(&self->constData->ep.name)
			ENDFOR
		ENDFOR

		/* data send ports */
		FOR ep : sendPorts
			FOR msg : ep.outgoing
				val data1 = if (msg.data!==null) "data" else ""
				val data2 = if (msg.data!==null) ", data" else ""
				#define ep.name_msg.name(data1) ep.portClassName_msg.name_set(&self->ep.namedata2)
			ENDFOR
		ENDFOR

		/* saps */
		FOR sap: ac.allSAPs
			FOR msg : sap.outgoing
				val data1 = if (msg.data!==null) "data" else ""
				val data2 = if (msg.data!==null) ", data" else ""
				#define sap.name_msg.name(data1) sap.portClassName_msg.name(&self->constData->sap.namedata2)
			ENDFOR
		ENDFOR

		/* replicated event ports */
		IF !replEventPorts.empty
			#define ifitem_index (((etReplSubPort*)ifitem)->index)
		ENDIF
		FOR ep : replEventPorts
			FOR msg : ep.outgoing
				val data1 = if (msg.data!==null) "data" else ""
				val data2 = if (msg.data!==null) ", data" else ""
				#define ep.name_msg.name_broadcast(data1) ep.portClassName_msg.name_broadcast(&self->constData->ep.namedata2)
				#define ep.name_msg.name(idxdata2) ep.portClassName_msg.name(&self->constData->ep.name, idxdata2)
			ENDFOR
		ENDFOR

		/* services */
		FOR svc : ac.allServiceImplementations
			FOR msg : svc.spp.outgoing
				val data1 = if (msg.data!==null) "data" else ""
				val data2 = if (msg.data!==null) ", data" else ""
				#define svc.spp.name_msg.name_broadcast(data1) svc.spp.portClassName_msg.name_broadcast(&self->constData->svc.spp.namedata2)
				#define svc.spp.name_msg.name(idxdata2) svc.spp.portClassName_msg.name(&self->constData->svc.spp.name, idxdata2)
			ENDFOR
		ENDFOR

		/* operations */
		FOR op : ac.latestOperations
			val args = op.argList
			#define op.name(args) ac.name_op.name(selfIF !op.arguments.empty, argsENDIF)
		ENDFOR

		/* attributes */
		FOR a : ac.allAttributes
			#define a.name (self->a.name)
		ENDFOR

		/* port operations */
		FOR p : portsWithOperations
			FOR op : p.portClass.operations
				val args = op.argList
				#define p.name_op.name(args) p.portClassName_op.name((p.portClassName*)&self->constData->p.nameIF !op.arguments.empty, argsENDIF)
			ENDFOR
		ENDFOR

		generateIncludeGuardEnd(filename)

	'''
	}

	private def argList(Operation op) {
		'''FOR a : op.arguments SEPARATOR ", "a.nameENDFOR'''
	}

	def protected generateSourceFile(Root root, ExpandedActorClass xpac) {
		val ac = xpac.actorClass
		val async = ac.commType==ComponentCommunicationType::ASYNCHRONOUS
		val eventDriven = ac.commType==ComponentCommunicationType::EVENT_DRIVEN
		val dataDriven = ac.commType==ComponentCommunicationType::DATA_DRIVEN
		val handleEvents = async || eventDriven
		val isEmptyStateGraph = FsmGenExtensions.isEmpty(xpac.graphContainer.graph)

	'''
		/**
		 * @author generated by eTrice
		 *
		 * Source File of ActorClass ac.name
		 *
		 */

		#include "ac.getCHeaderFileName"

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

		FOR pc : root.getReferencedProtocolClasses(ac)
			#include pc.includePath
		ENDFOR

		#include "ac.getCUtilsFileName"

		ac.userCode(3, true)

		/* interface item IDs */
		xpac.genInterfaceItemConstants

		IF !isEmptyStateGraph
			xpac.graphContainer.genStateMachine
		ENDIF

		void ac.name_init(ac.name* self){
			ET_MSC_LOGGER_SYNC_ENTRY("ac.name", "init")
			IF !isEmptyStateGraph
				xpac.genInitialization
			ENDIF
			ET_MSC_LOGGER_SYNC_EXIT
		}


		void ac.name_receiveMessage(void* self, const void* ifitem, const etMessage* msg){
			ET_MSC_LOGGER_SYNC_ENTRY("ac.name", "_receiveMessage")
			IF !isEmptyStateGraph
				IF handleEvents
					langExt.operationScope(ac.name, false)receiveEvent(self, (etPort*)ifitem, msg->evtID, (void*)(((char*)msg)+MEM_CEIL(sizeof(etMessage))));
				ELSE
					langExt.operationScope(ac.name, false)receiveEventInternal(self);
				ENDIF
			ENDIF

			ET_MSC_LOGGER_SYNC_EXIT
		}

		IF dataDriven || async
			void ac.name_execute(ac.name* self) {
				ET_MSC_LOGGER_SYNC_ENTRY("ac.name", "_execute")
				IF !isEmptyStateGraph

					IF handleEvents
						langExt.operationScope(ac.name, false)receiveEvent(self, NULL, 0, NULL);
					ELSE
						langExt.operationScope(ac.name, false)receiveEventInternal(self);
					ENDIF
				ENDIF

				ET_MSC_LOGGER_SYNC_EXIT
			}
		ENDIF

		ac.userStructorsImplementation

		operationsImplementation(ac.latestOperations, ac.name)

		'''
	}
}

Back to the top