Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 54c270feff6f3a8ee01c86779744cc80f947c40c (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
/*******************************************************************************
 * 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 java.util.HashMap
import java.util.ArrayList
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.ActorInstance
import org.eclipse.etrice.generator.etricegen.PortInstance
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))

			file = ssi.subSystemClass.getDispSourceFileName
			logger.logInfo("generating SubSystemClass dispatcher file: '"+file+"' in '"+path+"'")
			fileAccess.setOutputPath(path)
			fileAccess.generateFile(file, root.generateDispatcherFile(ssi, ssi.subSystemClass))
		}
	}

	def generateHeaderFile(Root root, SubSystemInstance ssi, SubSystemClass ssc) {'''
		/**
		 * @author generated by eTrice
		 *
		 * Header File of SubSystemClass ssc.name
		 * 
		 */
		
		generateIncludeGuardBegin(ssc.name)

		helpers.userCode(ssc.userCode1)
		
		
		/* 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 */
		
		helpers.userCode(ssc.userCode2)
		
		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 "ssc.getDispSourceFileName"

		#include "debugging/etLogger.h"
		#include "debugging/etMSCLogger.h"
		

		helpers.userCode(ssc.userCode3)
		
		/* data for SubSysten ssc.name */
		typedef struct ssc.name {
			char *name;
		} ssc.name;
		
		static ssc.name ssc.nameInst = {"ssc.name"};
		
		void ssc.name_initActorInstances(void);

		void ssc.name_init(void){
			ET_MSC_LOGGER_SYNC_ENTRY("SubSys", "init")
			etLogger_logInfoF("%s_init", ssc.nameInst.name);
			
			/* initialization of all message services */
			etMessageService_init(&msgService_Thread1, msgBuffer_Thread1, MESSAGE_POOL_MAX, MESSAGE_BLOCK_SIZE, MsgDispatcher_Thread1_receiveMessage);
			
			/* init all actors */
			ssc.name_initActorInstances();
			
			ET_MSC_LOGGER_SYNC_EXIT
		}
		
		void ssc.name_start(void){
			ET_MSC_LOGGER_SYNC_ENTRY("SubSys", "start")
			etLogger_logInfoF("%s_start", ssc.nameInst.name);
			ET_MSC_LOGGER_SYNC_EXIT
		}
		
		void ssc.name_run(void){
			ET_MSC_LOGGER_SYNC_ENTRY("SubSys", "run")
			int32 i;
			for (i=0; i<100; i++){
				etLogger_logInfoF("%s Scheduler tick %d", ssc.nameInst.name, i);
				etMessageService_execute(&msgService_Thread1);
			}
			ET_MSC_LOGGER_SYNC_EXIT
		}
		
		void ssc.name_stop(void){
			ET_MSC_LOGGER_SYNC_ENTRY("SubSys", "stop")
			etLogger_logInfoF("%s_stop", ssc.nameInst.name);
			ET_MSC_LOGGER_SYNC_EXIT
		}
		
		void ssc.name_destroy(void){
			ET_MSC_LOGGER_SYNC_ENTRY("SubSys", "destroy")
			etLogger_logInfoF("%s_destroy", ssc.nameInst.name);
			ET_MSC_LOGGER_SYNC_EXIT
		}

		void ssc.name_initActorInstances(void){
			ET_MSC_LOGGER_SYNC_ENTRY("ssc.name", "initActorInstances")
			FOR ai : ssi.allContainedInstances
				ai.actorClass.name_init(&ai.path.getPathName());
			ENDFOR
			ET_MSC_LOGGER_SYNC_EXIT
		}
		
	'''
	}

	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 "messaging/etMessageService.h"
		
		/* instantiation of message services */
		
		/* pool and block size */
		#define MESSAGE_POOL_MAX 10
		#define MESSAGE_BLOCK_SIZE 32
		
		/* MessageService for Thread1 */
		static uint8 msgBuffer_Thread1[MESSAGE_POOL_MAX*MESSAGE_BLOCK_SIZE];
		static etMessageService msgService_Thread1;


		/* include all used ActorClasses */
		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 variable actor structs */
		FOR ai : ssi.allContainedInstances
			static ai.actorClass.name ai.path.getPathName();
		ENDFOR

		FOR ai : ssi.allContainedInstances
			
			/* instance ai.path.getPathName() */
			IF ai.ports.empty
				/* no ports, nothing to initialize statically */
			ELSE
				genActorInstanceInitializer(root, ai)
			ENDIF
		ENDFOR
		
	'''
	}

	def private genActorInstanceInitializer(Root root, ActorInstance ai) {
		var instName = ai.path.pathName
		
		// list of replicated ports
		var replPorts = new ArrayList<PortInstance>()
		replPorts.addAll(ai.ports.filter(e|e.kind.literal!="RELAY" && e.port.multiplicity!=1))

		// list of ports, simple first, then replicated
		var ports = new ArrayList<PortInstance>()
		ports.addAll(ai.ports.filter(e|e.kind.literal!="RELAY" && e.port.multiplicity==1).union(replPorts))

		// compute replicated port offsets		
		var offsets = new HashMap<PortInstance, Integer>()
		var offset = 0
		for (p: replPorts) {
			offsets.put(p, offset)
			offset = offset + p.peers.size
		}
		
	'''
		IF !replPorts.empty
			static const etReplSubPort instName_repl_sub_ports[offset] = {
				/* Replicated Sub Ports: {myActor, etReceiveMessage, msgService, peerAddress, localId, idx} */
				FOR pi : replPorts SEPARATOR ","
					genReplSubPortInitializers(root, ai, pi)
				ENDFOR
			};
		ENDIF
		static const ai.actorClass.name_const instName_const = {
			/* Ports: {myActor, etReceiveMessage, msgService, peerAddress, localId} */
			FOR pi : ports SEPARATOR ","
				IF pi.port.multiplicity==1
					genPortInitializer(root, ai, pi)
				ELSE
					{pi.peers.size, instName_repl_sub_ports+offsets.get(pi)}
				ENDIF
			ENDFOR
		};
		static ai.actorClass.name instName = {&instName_const};
	'''}
		
	def private String genPortInitializer(Root root, ActorInstance ai, PortInstance pi) {
		var recvMsg = if (pi.peers.empty) "NULL" else ai.actorClass.name+"_ReceiveMessage"
		var objId = if (pi.peers.empty) 0 else pi.peers.get(0).objId
		var idx = if (pi.peers.empty) 0 else pi.peers.get(0).peers.indexOf(pi)
		
		"{&"+ai.path.getPathName()+", "
		+recvMsg+", " 
		+"&msgService_Thread1, "
		+(objId+idx)+", "
		+(root.getExpandedActorClass(ai).getInterfaceItemLocalId(pi.port)+1)
		+"} /* Port "+pi.name+" */"
	}
	
	def private String genReplSubPortInitializers(Root root, ActorInstance ai, PortInstance pi) {
		var result = ""
		
		for (p: pi.peers) {
			var idx = pi.peers.indexOf(p)
			var comma = if (idx<pi.peers.size-1) "," else ""
			result = result +
				"{&"+ai.path.getPathName()+", "
				+ai.actorClass.name+"_ReceiveMessage, " 
				+"&msgService_Thread1, "
				+p.objId+", "
				+(root.getExpandedActorClass(ai).getInterfaceItemLocalId(pi.port)+1)+", "
				+idx
				+"}"+comma+" /* Repl Sub Port "+pi.name+" idx +"+idx+"*/\n"
		}
		
		return result
	}
	
	def generateDispatcherFile(Root root, SubSystemInstance ssi, SubSystemClass ssc) {'''
		/**
		 * @author generated by eTrice
		 *
		 * Dispatcher File of SubSystemClass ssc.name
		 * - one generated dispatcher for each MessageService (Thread)
		 */

		#include "messaging/etMessageReceiver.h"
		#include "debugging/etLogger.h"
		#include "debugging/etMSCLogger.h"
		
		static void MsgDispatcher_Thread1_receiveMessage(const etMessage* msg){
			ET_MSC_LOGGER_SYNC_ENTRY("MsgDispatcher_Thread1", "receiveMessage")
			switch(msg->address){
			
				FOR ai : ssi.allContainedInstances
					/* interface items of ai.path */
					FOR pi : ai.orderedIfItemInstances
						IF pi instanceof PortInstance && (pi as PortInstance).port.multiplicity!=1
							FOR peer: pi.peers
								case pi.objId+pi.peers.indexOf(peer):
									etPort_receive((etPort*)&ai.path.pathName_const.pi.name.ports[pi.peers.indexOf(peer)], msg);
									break;
							ENDFOR
						ELSE
							case pi.objId:
								etPort_receive(&ai.path.pathName_const.pi.name, msg);
								break;
						ENDIF
					ENDFOR
				ENDFOR

				default:
					etLogger_logErrorF("MessageService_Thread1_ReceiveMessage: address %d does not exist ", msg->address);
					break;
			}
			ET_MSC_LOGGER_SYNC_EXIT
		}
		'''
	}

	
}

Back to the top