Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 96edd5de1a7c73a2c4dc2bff82c9f82aee721b9e (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
/*******************************************************************************
 * 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.SubSystemClass
import org.eclipse.etrice.generator.base.ILogger
import org.eclipse.etrice.generator.etricegen.Root
import org.eclipse.etrice.generator.etricegen.ServiceImplInstance
import org.eclipse.etrice.generator.etricegen.SubSystemInstance
import org.eclipse.xtext.generator.JavaIoFileSystemAccess
import org.eclipse.etrice.generator.extensions.RoomExtensions
import org.eclipse.etrice.generator.generic.ProcedureHelpers

import static extension org.eclipse.etrice.generator.base.Indexed.*


@Singleton
class SubSystemClassGen {
	
	@Inject extension JavaIoFileSystemAccess fileAccess
	@Inject extension CExtensions stdExt
	@Inject extension RoomExtensions roomExt
	@Inject extension ProcedureHelpers helpers
	@Inject ILogger logger
	
	def doGenerate(Root root) {
		for (ssi: root.subSystemInstances) {
			var path = ssi.subSystemClass.generationTargetPath+ssi.subSystemClass.getPath
			var file = ssi.subSystemClass.getCHeaderFileName
			logger.logInfo("generating SubSystemClass declaration: '"+file+"' in '"+path+"'")
			fileAccess.setOutputPath(path)
			fileAccess.generateFile(file, root.generateHeaderFile(ssi, ssi.subSystemClass))
			
			file = ssi.subSystemClass.getCSourceFileName
			logger.logInfo("generating SubSystemClass implementation: '"+file+"' in '"+path+"'")
			fileAccess.setOutputPath(path)
			fileAccess.generateFile(file, root.generateSourceFile(ssi, ssi.subSystemClass))
			
			file = ssi.subSystemClass.getInstSourceFileName
			logger.logInfo("generating SubSystemClass instance file: '"+file+"' in '"+path+"'")
			fileAccess.setOutputPath(path)
			fileAccess.generateFile(file, root.generateInstanceFile(ssi, ssi.subSystemClass))
		}
	}

	def generateHeaderFile(Root root, SubSystemInstance ssi, SubSystemClass ssc) {'''
		/**
		 * @author generated by eTrice
		 *
		 * Header File of SubSystemClass «ssc.name»
		 * 
		 */
		
		«generateIncludeGuardBegin(ssc.name)»
		
		
		/* lifecycle functions
		 * init -> start -> run (loop) -> stop -> destroy
		 */
		
		void «ssc.name»_init(void);		/* lifecycle init  	 */
		void «ssc.name»_start(void);	/* lifecycle start 	 */
		
		void «ssc.name»_run(void);		/* lifecycle run 	 */
		
		void «ssc.name»_stop(void); 	/* lifecycle stop	 */
		void «ssc.name»_destroy(void); 	/* lifecycle destroy */
		
		
		«generateIncludeGuardEnd(ssc.name)»
		
		
	'''
	}
	
	def generateSourceFile(Root root, SubSystemInstance ssi, SubSystemClass ssc) {'''
		/**
		 * @author generated by eTrice
		 *
		 * Source File of SubSystemClass «ssc.name»
		 * 
		 */
		
		#include "«ssc.getCHeaderFileName»"

		/* include instances for all classes */
		#include "«ssc.getInstSourceFileName»"

		#include "etLogger.h"
		
		/* data for SubSysten «ssc.name» */
		typedef struct «ssc.name» {
			char *name;
		} «ssc.name»;
		
		static «ssc.name» «ssc.name»Inst = {"«ssc.name»"};
		
		void «ssc.name»_init(void){
			etLogger_logInfoF("%s_init", «ssc.name»Inst.name);
			
			/* initialization of all message services */
			etMessageService_init(&msgService_Thread1, msgBuffer_Thread1, MESSAGE_POOL_MAX, MESSAGE_BLOCK_SIZE);
			
		}
		
		void «ssc.name»_start(void){
			etLogger_logInfoF("%s_start", «ssc.name»Inst.name);
		}
		
		void «ssc.name»_run(void){
			int32 i;
			for (i=0; i<100; i++){
				etLogger_logInfoF("%s Scheduler tick %d", «ssc.name»Inst.name, i);
			}
		}
		
		void «ssc.name»_stop(void){
			etLogger_logInfoF("%s_stop", «ssc.name»Inst.name);
		}
		
		void «ssc.name»_destroy(void){
			etLogger_logInfoF("%s_destroy", «ssc.name»Inst.name);
		}

		
	'''
	}

	def generateInstanceFile(Root root, SubSystemInstance ssi, SubSystemClass ssc) {'''
		/**
		 * @author generated by eTrice
		 *
		 * Instance File of SubSystemClass «ssc.name»
		 * - instantiation of all actor instances and port instances
		 * - configuration of data and connection of ports
		 */

		#include "etMessageService.h"
		
		/* instantiation of message services */
		#define MESSAGE_POOL_MAX 10
		#define MESSAGE_BLOCK_SIZE 32
		/* MessageService for Thread1 */
		uint8 msgBuffer_Thread1[MESSAGE_POOL_MAX*MESSAGE_BLOCK_SIZE];
		etMessageService msgService_Thread1;


		/* include all used ActorClasses */
		/* TODO: only include used Actor Classes for current SubSystem */
		«FOR actorClass : root.getUsedActorClasses()»#include "«actorClass.name».h"
		«ENDFOR»

		/* include all used ProtcolClasses */
		«FOR protocolClass : root.getUsedProtocolClasses()»#include "«protocolClass.name».h"
		«ENDFOR»
		
		
		/* declarations of all ActorClass instances (const and variable structs) */

		/* forward declaration of varible actor structs */
		«FOR ai : ssi.allContainedInstances»
			static «ai.actorClass.name» «ai.path.getPathName()»;
		«ENDFOR»

		«FOR ai : ssi.allContainedInstances»
			
			/* instance «ai.path.getPathName()» */
			static const «ai.actorClass.name»_const «ai.path.getPathName()»_const = {
				&«ai.path.getPathName()»,
				/* Ports: {myActor, etReceiveMessage, msgService, peerAddress, localId} */
				«FOR port : ai.actorClass.endPorts»
				{&«ai.path.getPathName()», «ai.actorClass.name»_ReceiveMessage, &msgService_Thread1, 1, 123} /* Port «port.name» */
				«ENDFOR»
				
			};
			static «ai.actorClass.name» «ai.path.getPathName()» = {&«ai.path.getPathName()»_const};
		«ENDFOR»
		

«««		«FOR ai : ssi.allContainedInstances»
«««			// actor instance «ai.path» itself => Systemport Address
«««			// TODOTJ: For each Actor, multiple addresses should be generated (actor?, systemport, debugport)
«««			Address addr_item_«ai.path.getPathName()» = new Address(0,«ai.threadId»,«ai.objId»);
«««			// interface items of «ai.path»
«««			«FOR pi : ai.orderedIfItemInstances»
«««				«IF pi instanceof ServiceImplInstance || pi.peers.size>1»
«««					«FOR peer : pi.peers»
«««						«var i = pi.peers.indexOf(peer)»
«««						Address addr_item_«pi.path.getPathName()»_«i» = new Address(0,«pi.threadId»,«pi.objId+i»);
«««					«ENDFOR»
«««				«ELSE»
«««					Address addr_item_«pi.path.getPathName()» = new Address(0,«ai.threadId»,«pi.objId»);
«««				«ENDIF»
«««			«ENDFOR»
«««		«ENDFOR»
		
	'''
	}


//		«var models = root.getReferencedModels(ssc)»
//		«FOR model : models»import «model.name».*;«ENDFOR»
//		
//		
//		«helpers.UserCode(ssc.userCode1)»
//		
//		public class «ssi.name» extends SubSystemClassBase{
//		
//			«helpers.UserCode(ssc.userCode2)»
//			
//			public «ssi.name»(IRTObject parent, String name) {
//				super(parent, name);
//			}
//			
//			@Override
//			public void receiveEvent(InterfaceItemBase ifitem, int evt, Object data){
//			}
//			
//			@Override	
//			public void instantiateMessageServices(){
//			
//				RTServices.getInstance().getMsgSvcCtrl().addMsgSvc(new MessageService(this, new Address(0, 0, 0),"MessageService_Main"));
//				«FOR thread : ssc.threads»
//					RTServices.getInstance().getMsgSvcCtrl().addMsgSvc(new MessageService(this, new Address(0, «ssc.threads.indexOf(thread)+1», 0),"MessageService_«thread.name»", «thread.prio»));
//				«ENDFOR»
//				}
//		
//			@Override
//			public void instantiateActors(){
//				// all addresses
//				// Addresses for the Subsystem Systemport
//				«FOR ai : ssi.allContainedInstances.indexed(ssi.maxObjId)»
//					Address addr_item_SystemPort_«ssi.allContainedInstances.indexOf(ai.value)» = new Address(0,0,«ai.index1»);
//				«ENDFOR»
//				
//				«FOR ai : ssi.allContainedInstances»
//					// actor instance «ai.path» itself => Systemport Address
//					// TODOTJ: For each Actor, multiple addresses should be generated (actor?, systemport, debugport)
//					Address addr_item_«ai.path.getPathName()» = new Address(0,«ai.threadId»,«ai.objId»);
//					// interface items of «ai.path»
//					«FOR pi : ai.orderedIfItemInstances»
//						«IF pi instanceof ServiceImplInstance || pi.peers.size>1»
//							«FOR peer : pi.peers»
//								«var i = pi.peers.indexOf(peer)»
//								Address addr_item_«pi.path.getPathName()»_«i» = new Address(0,«pi.threadId»,«pi.objId+i»);
//							«ENDFOR»
//						«ELSE»
//							Address addr_item_«pi.path.getPathName()» = new Address(0,«ai.threadId»,«pi.objId»);
//						«ENDIF»
//					«ENDFOR»
//				«ENDFOR»
//		
//				// instantiate all actor instances
//				instances = new ActorClassBase[«ssi.allContainedInstances.size»];
//				«FOR ai : ssi.allContainedInstances»
//					instances[«ssi.allContainedInstances.indexOf(ai)»] = new «ai.actorClass.name»(
//						«IF ai.eContainer instanceof SubSystemInstance»
//							this,
//						«ELSE»
//							instances[«ssi.allContainedInstances.indexOf(ai.eContainer)»],
//						«ENDIF»
//						"«ai.name»",
//						// own interface item addresses
//						new Address[][] {{addr_item_«ai.path.getPathName()»}«IF !ai.orderedIfItemInstances.empty»,«ENDIF»
//						«FOR pi : ai.orderedIfItemInstances SEPARATOR ","»
//							{
//								«IF pi instanceof ServiceImplInstance || pi.peers.size>1»
//									«FOR peer : pi.peers SEPARATOR ","»
//										addr_item_«pi.path.getPathName()»_«pi.peers.indexOf(peer)»
//									«ENDFOR»
//								«ELSE»
//									addr_item_«pi.path.getPathName()»
//								«ENDIF»
//							}
//						«ENDFOR»
//						},
//						// peer interface item addresses
//						new Address[][] {{addr_item_SystemPort_«ssi.allContainedInstances.indexOf(ai)»}«IF !ai.orderedIfItemInstances.empty»,«ENDIF»
//							«FOR pi : ai.orderedIfItemInstances SEPARATOR ","»
//							{
//								«IF !(pi instanceof ServiceImplInstance) && pi.peers.isEmpty»
//									null
//								«ELSE»
//									«FOR pp : pi.peers SEPARATOR ","»
//										«IF pp instanceof ServiceImplInstance || pp.peers.size>1»
//											addr_item_«pp.path.getPathName()»_«pp.peers.indexOf(pi)»
//										«ELSE»
//											addr_item_«pp.path.getPathName()»
//										«ENDIF»
//									«ENDFOR»
//								«ENDIF»
//							}
//							«ENDFOR»
//						}
//					); 
//				«ENDFOR»
//		
//				// create the subsystem system port	
//				RTSystemPort = new RTSystemServicesProtocolConjPortRepl(this, "RTSystemPort",
//						0, //local ID
//						// own addresses
//						new Address[]{
//							«FOR ai : ssi.allContainedInstances SEPARATOR ","»
//								addr_item_SystemPort_«ssi.allContainedInstances.indexOf(ai)»
//							«ENDFOR»
//						},
//						// peer addresses
//						new Address[]{
//							«FOR ai : ssi.allContainedInstances SEPARATOR ","»
//								addr_item_«ai.path.getPathName()»
//							«ENDFOR»
//						});
//						
//			}
//		};

	
}

Back to the top