Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7ac206db5dbb6c0d3aedaff779562f341183dba7 (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
/*******************************************************************************
 * Copyright (c) 2011 Draeger Medical GmbH (http://www.draeger.com).
 * 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:
 * 		Peter Karlitschek (initial contribution)
 *
 *******************************************************************************/

package org.eclipse.etrice.generator.cpp.gen

import com.google.inject.Inject
import com.google.inject.Singleton
import java.util.Collection
import java.util.Map
import org.eclipse.etrice.core.common.converter.TimeConverter
import org.eclipse.etrice.core.etmap.util.ETMapUtil
import org.eclipse.etrice.core.etphys.eTPhys.ExecMode
import org.eclipse.etrice.core.etphys.eTPhys.PhysicalThread
import org.eclipse.etrice.core.genmodel.etricegen.Root
import org.eclipse.etrice.core.genmodel.etricegen.SubSystemInstance
import org.eclipse.etrice.core.genmodel.etricegen.WiredSubSystemClass
import org.eclipse.etrice.core.room.SubSystemClass
import org.eclipse.etrice.generator.cpp.Main
import org.eclipse.etrice.generator.fsm.base.FileSystemHelpers
import org.eclipse.etrice.generator.fsm.base.IGeneratorFileIo
import org.eclipse.etrice.generator.generic.ProcedureHelpers
import org.eclipse.etrice.generator.generic.RoomExtensions

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

@Singleton
class NodeGen {

	@Inject extension CppExtensions
	@Inject extension RoomExtensions
	@Inject extension ProcedureHelpers
	@Inject extension FileSystemHelpers
	
	@Inject IGeneratorFileIo fileIO
	@Inject Initialization initHelper

	def doGenerate(Root root) {
		val Map<SubSystemClass, WiredSubSystemClass> sscc2wired = newHashMap
		root.wiredInstances.filter(typeof(WiredSubSystemClass)).forEach[sscc2wired.put(subSystemClass, it)]

		for (nr : ETMapUtil::getNodeRefs()) {
			for (instpath : ETMapUtil::getSubSystemInstancePaths(nr)) {
				val ssi = root.getInstance(instpath) as SubSystemInstance
				if (ssi!=null && ssi.subSystemClass.validGenerationLocation) {
					val wired = sscc2wired.get(ssi.subSystemClass)
					val path = ssi.subSystemClass.generationTargetPath+ssi.subSystemClass.getPath
					val infopath = ssi.subSystemClass.generationInfoPath+ssi.subSystemClass.getPath

					//checkDataPorts(ssi)

					val usedThreads = ETMapUtil::getUsedThreads(nr, ssi)

					fileIO.generateFile("generating Node declaration", path, infopath, getCppHeaderFileName(nr, ssi), root.generateHeaderFile(ssi, wired, usedThreads))
					fileIO.generateFile("generating Node implementation", path, infopath, getCppSourceFileName(nr, ssi), root.generateSourceFile(ssi, wired, usedThreads))
				}
			}
		}
	}

	def generateHeaderFile(Root root, SubSystemInstance comp, WiredSubSystemClass wired, Collection<PhysicalThread> usedThreads) {
		val cc = comp.subSystemClass
		val models = root.getReferencedModels(cc)
		val nr = ETMapUtil::getNodeRef(comp)
		val clsname = nr.getCppClassName(comp)
		val threads = nr.type.threads.filter(t|usedThreads.contains(t))

	'''
		/**
		 * @author generated by eTrice
		 *
		 * Header File of SubSystemClass «clsname»
		 *
		 */

		«generateIncludeGuardBegin(cc, '')»

		#include "common/messaging/IMessageService.h"
		#include "common/modelbase/SubSystemClassBase.h"
		«FOR ai : comp.actorInstances»
			#include "«ai.actorClass.actorIncludePath»"
		«ENDFOR»

«««		«FOR model : root.getReferencedModels(cc)»
«««		«««			#include "«model.name».h"
«««		«ENDFOR»
		«cc.userCode1.userCode»

		«cc.generateNamespaceBegin»

		class «clsname» : public etRuntime::SubSystemClassBase{

			«cc.userCode2.userCode»

			public:
				«FOR thread : threads.indexed»
					static const int «thread.value.threadId»;
				«ENDFOR»
				
				// sub actors
				«FOR sub : cc.actorRefs»
					«IF sub.multiplicity>1»
						Replicated«sub.type.implementationClassName» «sub.name»;
					«ELSE»
						«sub.type.implementationClassName» «sub.name»;
					«ENDIF»
				«ENDFOR»

				«clsname»(IRTObject* parent, const std::string& name);
				~«clsname»();

				virtual void receiveEvent(etRuntime::InterfaceItemBase* ifitem, int evt, void* data);
				virtual void instantiateMessageServices();
				virtual void mapThreads(void);
				virtual void initialize(void);
				«IF Main::settings.generateMSCInstrumentation»
					virtual void setProbesActive(bool recursive, bool active);
				«ENDIF»

				virtual void init();

				«IF Main::settings.generateMSCInstrumentation»
					etBool hasGeneratedMSCInstrumentation() const { return true; }
					virtual void destroy();
				«ENDIF»

			private:
				// MessageServices
				«FOR thread : threads»
					IMessageService* msgSvc_«thread.name»;
				«ENDFOR»

				«clsname»();
				«clsname»(«clsname» const&);
				«clsname»& operator=(«clsname» const&);
		};

		«cc.generateNamespaceEnd»

		«generateIncludeGuardEnd(cc, '')»
	'''
	}

	def private getThreadId(PhysicalThread thread) {
		"THREAD_"+thread.name.toUpperCase
	}

	def private generateConstructorInitalizerList(SubSystemClass cc) {
		val extension initHelper = initHelper
		var initList = <CharSequence>newArrayList
		
		// super class
		initList += '''SubSystemClassBase(parent, name)'''
	    // own sub actors
	    initList += cc.actorRefs.map['''«name»(this, "«name»")''']

		initList.generateCtorInitializerList
	}

	def generateSourceFile(Root root, SubSystemInstance comp, WiredSubSystemClass wired, Collection<PhysicalThread> usedThreads) {
		val cc = comp.subSystemClass
		val models = root.getReferencedModels(cc)
		val nr = ETMapUtil::getNodeRef(comp)
		val clsname = nr.getCppClassName(comp)
		val threads = nr.type.threads.filter(t|usedThreads.contains(t))

	'''
		/**
		 * @author generated by eTrice
		 *
		 * Source File of SubsystemClass «clsname»
		 *
		 */

		#include "«getCppHeaderFileName(nr, comp)»"

		#include "common/debugging/DebuggingService.h"
		#include "common/debugging/MSCFunctionObject.h"
		#include "common/messaging/IMessageService.h"
		#include "common/messaging/MessageService.h"
		#include "common/messaging/MessageServiceController.h"
		#include "common/messaging/RTServices.h"
		#include "common/messaging/StaticMessageMemory.h"
		#include "common/modelbase/InterfaceItemBase.h"

		using namespace etRuntime;

		«cc.generateNamespaceBegin»

		«FOR thread : threads.indexed»
			const int «clsname»::«thread.value.threadId» = «thread.index0»;
		«ENDFOR»

		«clsname»::«clsname»(IRTObject* parent, const std::string& name)
				«cc.generateConstructorInitalizerList»
				«FOR thread : threads»
					, msgSvc_«thread.name»(NULL)
				«ENDFOR»
		{
			«IF Main::settings.generateMSCInstrumentation»
				MSCFunctionObject mscFunctionObject(getInstancePathName(), "Constructor");
			«ENDIF»
			«FOR sub : cc.actorRefs»
				«IF sub.multiplicity>1»
					«sub.name».createSubActors(«sub.multiplicity»);
				«ENDIF»
			«ENDFOR»
		}
		
		«clsname»::~«clsname»() {
			«IF Main::settings.generateMSCInstrumentation»
				MSCFunctionObject mscFunctionObject(getInstancePathName(), "Destructor");
			«ENDIF»
			«FOR thread : threads»
				delete msgSvc_«thread.name»;
			«ENDFOR»
		}

		void «clsname»::receiveEvent(InterfaceItemBase* ifitem, int evt, void* data){
		}

		void «clsname»::instantiateMessageServices(){

			«IF Main::settings.generateMSCInstrumentation»
				MSCFunctionObject mscFunctionObject(getInstancePathName(), "instantiateMessageServices()");
			«ENDIF»

			IMessageMemory* msgMemory;
			«FOR thread: threads»
				{
					msgMemory = new StaticMessageMemory(this, "MessageMemory_«thread.name»", «thread.msgblocksize», «thread.msgpoolsize»);
					«IF thread.execmode==ExecMode::POLLED || thread.execmode==ExecMode::MIXED»
						etTime interval;
						interval.sec = «TimeConverter::split(thread.time, TimeConverter.SEC, true)»;
						interval.nSec = «TimeConverter::split(thread.time, TimeConverter.MILLI_SEC, false)»L;
						
						msgSvc_«thread.name» = new MessageService(this, IMessageService::«thread.execmode.getName», interval, 0, «thread.threadId», "MessageService_«thread.name»", msgMemory, «thread.prio»);
					«ELSE»
						msgSvc_«thread.name» = new MessageService(this, IMessageService::«thread.execmode.getName», 0, «thread.threadId», "MessageService_«thread.name»", msgMemory, «thread.prio»);
					«ENDIF»
					RTServices::getInstance().getMsgSvcCtrl().addMsgSvc(*msgSvc_«thread.name»);
				}
			«ENDFOR»
		}

		void «clsname»::mapThreads() {
			// thread mappings
			«FOR ai : comp.allContainedInstances»
				«val mapped = ETMapUtil::getMappedThread(ai)»
				«IF !(mapped.implicit || mapped.asParent)»
					addPathToThread("«ai.path»", «mapped.thread.threadId»);
				«ENDIF»
			«ENDFOR»
		}

		void «clsname»::initialize() {
			«IF Main::settings.generateMSCInstrumentation»
				DebuggingService::getInstance().getSyncLogger().addVisibleComment("starting initialization");
				MSCFunctionObject mscFunctionObject(getInstancePathName(), "initialize()");
				«FOR sub : cc.actorRefs»
					«IF sub.multiplicity>1»
						for (int i=0; i<«sub.multiplicity»; ++i) {
							DebuggingService::getInstance().addMessageActorCreate(*this, «sub.name».getSubActor(i)->getName());
						}
					«ELSE»
						DebuggingService::getInstance().addMessageActorCreate(*this, "«sub.name»");
					«ENDIF»
				«ENDFOR»			
			«ENDIF»
			
			// wiring
			«FOR wire: wired.wires»
				«if (wire.dataDriven) "DataPortBase" else "InterfaceItemBase"»::connect(this, "«wire.path1.join('/')»", "«wire.path2.join('/')»");
			«ENDFOR»
			
			// call initialize of sub actors
			«FOR sub : cc.actorRefs»
				«sub.name».initialize();
			«ENDFOR»
		}
		
		«IF Main::settings.generateMSCInstrumentation»
			void «clsname»::setProbesActive(bool recursive, bool active) {
				for(int i = 0; i < m_RTSystemPort.getNInterfaceItems(); i++)
					DebuggingService::getInstance().addPortInstance(*(m_RTSystemPort.getInterfaceItem(i)));
				if(recursive) {
					«FOR sub : cc.actorRefs»
						«sub.name».setProbesActive(recursive, active);
					«ENDFOR»
				}
			}
		«ENDIF»

		void «clsname»::init(){
			SubSystemClassBase::init();
			«IF Main::settings.generateMSCInstrumentation»
				DebuggingService::getInstance().addVisibleComment("done sub system initialization");
			«ENDIF»
		}
		«IF Main::settings.generateMSCInstrumentation»

			void «clsname»::destroy() {
				«IF Main::settings.generateMSCInstrumentation»
					DebuggingService::getInstance().getSyncLogger().addVisibleComment("starting destruction");
					MSCFunctionObject mscFunctionObject(getInstancePathName(), "destroy()");
					DebuggingService::getInstance().addVisibleComment("begin sub system destruction");
				«ENDIF»
				SubSystemClassBase::destroy();
				«IF Main::settings.generateMSCInstrumentation»
					DebuggingService::getInstance().addVisibleComment("done sub system destruction");
				«ENDIF»
			}
		«ENDIF»

		«cc.generateNamespaceEnd»

	'''
	}


}

Back to the top