Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1b856f888f6000f74d20036d6ba5b3c86f65541b (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
/*******************************************************************************
 * 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.room.ActorClass
import org.eclipse.etrice.core.room.ProtocolClass
import org.eclipse.etrice.core.room.ActorCommunicationType
import org.eclipse.etrice.core.room.CommunicationType
import static extension org.eclipse.etrice.core.room.util.RoomHelpers.*
import org.eclipse.etrice.core.genmodel.base.ILogger
import org.eclipse.etrice.core.genmodel.etricegen.ExpandedActorClass
import org.eclipse.etrice.core.genmodel.etricegen.Root
import org.eclipse.xtext.generator.JavaIoFileSystemAccess

import org.eclipse.etrice.generator.generic.RoomExtensions
import org.eclipse.etrice.generator.generic.ProcedureHelpers
import org.eclipse.etrice.generator.generic.TypeHelpers
import org.eclipse.etrice.generator.generic.GenericActorClassGenerator


@Singleton
class ActorClassGen extends GenericActorClassGenerator {
	
	@Inject extension JavaIoFileSystemAccess fileAccess
	@Inject extension CExtensions stdExt
	@Inject extension RoomExtensions roomExt
	
	@Inject extension ProcedureHelpers helpers
	@Inject extension TypeHelpers
	@Inject extension StateMachineGen stateMachineGen
	@Inject ILogger logger
	
	def doGenerate(Root root) {
		for (xpac: root.xpActorClasses) {
			var path = xpac.actorClass.generationTargetPath+xpac.actorClass.getPath
			
			// header file
			logger.logInfo("generating ActorClass header '"+xpac.actorClass.getCHeaderFileName+"' in '"+path+"'")
			fileAccess.setOutputPath(path)
			fileAccess.generateFile(xpac.actorClass.getCHeaderFileName, root.generateHeaderFile(xpac, xpac.actorClass))

			// source file
			if (hasBehaviorAnnotation(xpac, "BehaviorManual")) {
				logger.logInfo("omitting ActorClass source for '"+xpac.actorClass.name+"' since @BehaviorManual is specified")
			}
			else {
				logger.logInfo("generating ActorClass source '"+xpac.actorClass.getCSourceFileName +"' in '"+path+"'")
				fileAccess.setOutputPath(path)
				fileAccess.generateFile(xpac.actorClass.getCSourceFileName , root.generateSourceFile(xpac, xpac.actorClass))
			}
		}
	}
	
	def private hasBehaviorAnnotation(ExpandedActorClass xpac, String annotation) {
		if (xpac.actorClass.behaviorAnnotations != null){
			if(xpac.actorClass.behaviorAnnotations.findFirst(e|e.name == annotation) != null){
				return true;
			}
		}
		return false;		
	}
	
	def private generateHeaderFile(Root root, ExpandedActorClass xpac, ActorClass ac) {
		var eventPorts = ac.allEndPorts.filter(p|(p.protocol as ProtocolClass).commType==CommunicationType::EVENT_DRIVEN)
		var sendPorts = ac.allEndPorts.filter(p|(p.protocol as ProtocolClass).commType==CommunicationType::DATA_DRIVEN && p.conjugated)
		var recvPorts = ac.allEndPorts.filter(p|(p.protocol as ProtocolClass).commType==CommunicationType::DATA_DRIVEN && !p.conjugated)
		var dataDriven = ac.commType==ActorCommunicationType::DATA_DRIVEN
		var async = ac.commType==ActorCommunicationType::ASYNCHRONOUS
		
	'''
		/**
		 * @author generated by eTrice
		 *
		 * Header File of ActorClass «ac.name»
		 * 
		 */

		«generateIncludeGuardBegin(ac.name)»
		
		#include "etDatatypes.h"
		#include "messaging/etMessage.h"
		
		«FOR dataClass : root.getReferencedDataClasses(ac)»
			#include "«dataClass.name».h"
		«ENDFOR»
		«FOR pc : root.getReferencedProtocolClasses(ac)»
			#include "«pc.name».h"
		«ENDFOR»
		
		«helpers.userCode(ac.userCode1)»
		
		typedef struct «ac.name» «ac.name»;
		
		/* const part of ActorClass (ROM) */
		«IF eventPorts.empty && recvPorts.empty && ac.allSAPs.empty && ac.allServiceImplementations.empty»
			/* this actor class has no ports and thus no constant data */
		«ELSE»
			typedef struct «ac.name»_const {
				/* 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;
		«ENDIF»
		
		«IF !xpac.stateMachine.empty»
			
			«stateMachineGen.genHeaderConstants(xpac, ac)»
		«ENDIF»
		
		/* variable part of ActorClass (RAM) */
		struct «ac.name» {
			«IF !(eventPorts.empty && recvPorts.empty && ac.allSAPs.empty && ac.allServiceImplementations.empty)»
				const «ac.name»_const* const constData;
				
			«ENDIF»
			/* data send ports */
			«FOR ep : sendPorts»
				«IF ep.multiplicity==1»
					«ep.getPortClassName()» «ep.name»;
				«ENDIF»
			«ENDFOR»

			«helpers.attributes(ac.allAttributes)»

		«FOR a:ac.allAttributes»
			«IF a.defaultValueLiteral!=null»
				«logger.logInfo(ac.name+" "+a.name+": Attribute initialization not supported in C")»
			«ENDIF»
		«ENDFOR»
			
			«IF !xpac.stateMachine.empty»
			
				«stateMachineGen.genDataMembers(xpac, ac)»
			«ENDIF»
		};

		void «ac.name»_init(«ac.name»* self);

		void «ac.name»_receiveMessage(void* self, void* ifitem, const etMessage* msg);
		
		«IF dataDriven || async»
			void «ac.name»_execute(«ac.name»* self);
		«ENDIF»
		
		«helpers.operationsDeclaration(ac.operations, ac.name)»
		
		«helpers.userCode(ac.userCode2)»
		
		«generateIncludeGuardEnd(ac.name)»
		
	'''
	}
	
	def private generateSourceFile(Root root, ExpandedActorClass xpac, ActorClass ac) {
		var async = ac.commType==ActorCommunicationType::ASYNCHRONOUS
		var eventDriven = ac.commType==ActorCommunicationType::EVENT_DRIVEN
		var dataDriven = ac.commType==ActorCommunicationType::DATA_DRIVEN
		var handleEvents = async || eventDriven
		
	'''
		/**
		 * @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 "platform/etMemory.h"

		«FOR pc : root.getReferencedProtocolClasses(ac)»
			#include "«pc.getCHeaderFileName»"
		«ENDFOR»
		
		«helpers.userCode(ac.userCode3)»

		/* interface item IDs */
		«genInterfaceItemConstants(xpac, ac)»

		«IF !xpac.stateMachine.empty»
			«stateMachineGen.genStateMachine(xpac, ac)»
		«ENDIF»
		
		void «ac.name»_init(«ac.name»* self){
			ET_MSC_LOGGER_SYNC_ENTRY("«ac.name»", "init")
			«IF !xpac.stateMachine.empty»
				«stateMachineGen.genInitialization(xpac, ac)»
			«ENDIF»
			ET_MSC_LOGGER_SYNC_EXIT
		}
		
		
		void «ac.name»_receiveMessage(void* self, void* ifitem, const etMessage* msg){
			ET_MSC_LOGGER_SYNC_ENTRY("«ac.name»", "_receiveMessage")
			«IF !xpac.stateMachine.empty»
				
				receiveEvent(self«IF handleEvents», (etPort*)ifitem, msg->evtID, (void*)(((char*)msg)+MEM_CEIL(sizeof(etMessage)))«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 !xpac.stateMachine.empty»
					
					receiveEvent(self«IF handleEvents», NULL, 0, NULL«ENDIF»);
				«ENDIF»
				
				ET_MSC_LOGGER_SYNC_EXIT
			}
		«ENDIF»
		
		«helpers.operationsImplementation(ac)»
		
		'''
	}
}

Back to the top