Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: fb570111e0c70a9266e21e7ca8ad7bd2ddcf892d (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
/*******************************************************************************
 * 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.genmodel.base.ILogger
import org.eclipse.etrice.core.genmodel.etricegen.Root
import org.eclipse.etrice.core.room.CommunicationType
import org.eclipse.etrice.core.room.PrimitiveType
import org.eclipse.etrice.core.room.ProtocolClass
import org.eclipse.etrice.generator.base.AbstractGenerator
import org.eclipse.etrice.generator.base.IGeneratorFileIo
import org.eclipse.etrice.generator.generic.GenericProtocolClassGenerator
import org.eclipse.etrice.generator.generic.ProcedureHelpers
import org.eclipse.etrice.generator.generic.RoomExtensions
import org.eclipse.etrice.generator.generic.TypeHelpers

import static extension org.eclipse.etrice.core.room.util.RoomHelpers.*

@Singleton
class ProtocolClassGen extends GenericProtocolClassGenerator {

	@Inject IGeneratorFileIo fileIO
	@Inject extension CExtensions
	@Inject extension RoomExtensions
	@Inject extension ProcedureHelpers
	@Inject extension TypeHelpers
	@Inject ILogger logger
	
	def doGenerate(Root root) {
		for (pc: root.usedProtocolClasses) {
			val path = pc.generationTargetPath+pc.getPath
			val infopath = pc.generationInfoPath+pc.getPath
			var file = pc.getCHeaderFileName

			// header file
			fileIO.generateFile("generating ProtocolClass header", path, infopath, file, root.generateHeaderFile(pc))

			// source file
			file = pc.getCSourceFileName
			fileIO.generateFile("generating ProtocolClass source", path, infopath, file, root.generateSourceFile(pc))
		}
	}

	
	def private generateHeaderFile(Root root, ProtocolClass pc) {'''
		/**
		 * @author generated by eTrice
		 *
		 * Header File of ProtocolClass «pc.name»
		 * 
		 */

		«generateIncludeGuardBegin(pc)»
		
		#include "etDatatypes.h"
		#include "modelbase/etPort.h"
		
		«pc.userCode(1)»
		
		«FOR dataClass : root.getReferencedDataClasses(pc)»
			#include «dataClass.includePath»
		«ENDFOR»
		
		«IF pc.commType==CommunicationType::EVENT_DRIVEN»
			
			/* message IDs */
			«genMessageIDs(pc)»
			
			/*--------------------- port structs and methods */
			«portClassHeader(pc, false)»
			«portClassHeader(pc, true)»
		«ELSEIF pc.commType==CommunicationType::DATA_DRIVEN»
			/*--------------------- port structs and methods */
			«pc.genDataDrivenPortHeaders»
		«ELSEIF pc.commType==CommunicationType::SYNCHRONOUS»
			#error "synchronoue protocols not implemented yet"
		«ENDIF»
«««			«portClass(pc, false)»
«««			«portClass(pc, true)»

		/*--------------------- debug helpers */
		
		/* get message string for message id */
		const char* «pc.name»_getMessageString(int msg_id);

		«pc.userCode(2)»
		
		«generateIncludeGuardEnd(pc)»
		
	'''
	}
	
	def private generateSourceFile(Root root, ProtocolClass pc) {'''
		/**
		 * @author generated by eTrice
		 *
		 * Source File of ProtocolClass «pc.name»
		 * 
		 */

		#include "«pc.getCHeaderFileName»"
		#include "debugging/etMSCLogger.h"

		«pc.userCode(3)»
		
		/*--------------------- port methods */
		«IF pc.commType==CommunicationType::EVENT_DRIVEN»
			«portClassSource(pc, false)»
			«portClassSource(pc, true)»
			
			/*--------------------- debug helpers */
			«generateDebugHelpersImplementation(root, pc)»
		«ELSEIF pc.commType==CommunicationType::DATA_DRIVEN»
			«pc.genDataDrivenPortSources»
		«ELSEIF pc.commType==CommunicationType::SYNCHRONOUS»
			#error "synchronous protocols not implemented yet"
		«ENDIF»
	'''
	}
	
	def private portClassHeader(ProtocolClass pc, Boolean conj){
		var portClassName = pc.getPortClassName(conj)
		var replPortClassName = pc.getPortClassName(conj, true)
		var messages = if (conj) pc.allIncomingMessages else pc.allOutgoingMessages
		
		'''
		typedef etPort «portClassName»;
		typedef etReplPort «replPortClassName»;
		
		«IF pc.getPortClass(conj)!=null»	
			«IF !(pc.getPortClass(conj).attributes.empty)»
				/* variable part of PortClass (RAM) */
				typedef struct «portClassName»_var «portClassName»_var; 
				struct «portClassName»_var {
					«pc.getPortClass(conj).attributes.attributes»
					};
				«FOR a:pc.getPortClass(conj).attributes»
					«IF a.defaultValueLiteral!=null»
						«logger.logInfo(portClassName+" "+a.name+": Attribute initialization not supported in C")»
					«ENDIF»
				«ENDFOR»
			«ENDIF»
		«ENDIF»
		
		«FOR message : messages»
			«var hasData = message.data!=null»
			«var typeName = if (hasData) message.data.refType.type.typeName else ""»
			«var refp = if (hasData && (!(message.data.refType.type instanceof PrimitiveType)||(message.data.refType.ref))) "*" else ""»
			«var data = if (hasData) ", "+typeName+refp+" data" else ""»
			«messageSignature(portClassName, message.name, "", data)»;
			«messageSignature(replPortClassName, message.name, "_broadcast", data)»;
			«messageSignature(replPortClassName, message.name, "", ", int idx"+data)»;
		«ENDFOR»
		
		«IF (pc.getPortClass(conj) != null)»	
			«pc.getPortClass(conj).operations.operationsDeclaration(portClassName)»
		«ENDIF»
		
		«IF pc.handlesReceive(conj)»
			«FOR h:getReceiveHandlers(pc,conj)»
				void «portClassName»_«h.msg.name»_receiveHandler(«portClassName»* self, const etMessage* msg, void * actor, etActorReceiveMessage receiveMessageFunc);
			«ENDFOR»
		«ENDIF»
		etInt32 «replPortClassName»_getReplication(const «replPortClassName»* self);
		'''
	}


	
	def private genDataDrivenPortHeaders(ProtocolClass pc) {
		var sentMsgs = pc.allIncomingMessages.filter(m|m.data!=null)
		
		'''
			/* data driven send port (conjugated) */
			typedef struct {
				«FOR msg : sentMsgs»
					«var typeName = msg.data.refType.type.typeName»
					«var refp = if (msg.data.refType.ref) "*" else ""»
					«typeName»«refp» «msg.name»;
				«ENDFOR»
			}
			«pc.getPortClassName(true)»;
			
			/* data driven receive port (regular) */
			typedef struct {
				const «pc.getPortClassName(true)»* peer;
			}
			«pc.getPortClassName(false)»;
			
			«FOR message : sentMsgs»
				«var hasData = message.data!=null»
				«var typeName = if (hasData) message.data.refType.type.typeName else ""»
				«var refp = if (hasData && !(message.data.refType.type instanceof PrimitiveType)) "*" else ""»
				«var data = if (hasData) ", "+typeName+refp+" data" else ""»
				«messageSetterSignature(pc.getPortClassName(true), message.name, data)»;
				«messageGetterSignature(pc.getPortClassName(false), message.name, typeName)»;
				
			«ENDFOR»
		'''
	}
	def private genDataDrivenPortSources(ProtocolClass pc) {
		var messages = pc.allIncomingMessages.filter(m|m.data!=null)
	'''
			«FOR message : messages»
				«var typeName =message.data.refType.type.typeName»
				«var refp = if (!(message.data.refType.type instanceof PrimitiveType)) "*" else ""»
				«var data = ", "+typeName+refp+" data"»
				«messageSetterSignature(pc.getPortClassName(true), message.name, data)» {
					self->«message.name» = «refp»data;
				}
				«messageGetterSignature(pc.getPortClassName(false), message.name, typeName)» {
					return self->peer->«message.name»;
				}
				
			«ENDFOR»
	'''
	}
	
	def private portClassSource(ProtocolClass pc, Boolean conj) {
		val pclass = pc.getPortClass(conj)
		val portClassName = pc.getPortClassName(conj)
		val replPortClassName = pc.getPortClassName(conj, true)
		val messages = if (conj) pc.allIncomingMessages else pc.allOutgoingMessages
		val dir = if (conj) "IN_" else "OUT_"

		'''
			«FOR message : messages»
				«var hasData = message.data!=null»
				«var typeName = if (hasData) message.data.refType.type.typeName else ""»
				«var refp = if (hasData && ((message.data.refType.ref))) "*" else ""»
				«var refpd = if (hasData && (!(message.data.refType.type instanceof PrimitiveType)||(message.data.refType.ref))) "*" else ""»
				«var refa = if (hasData && (!(message.data.refType.type instanceof PrimitiveType))&&(!(message.data.refType.ref))) "" else "&"»
				«var data = if (hasData) ", "+typeName+refpd+" data" else ""»
				«var dataCall = if (hasData) ", data" else ""»
				«var hdlr = message.getSendHandler(conj)»
				
				«messageSignature(portClassName, message.name, "", data)» {
					«IF hdlr != null»
						«AbstractGenerator::getInstance().getTranslatedCode(hdlr.detailCode)»
					«ELSE»
						ET_MSC_LOGGER_SYNC_ENTRY("«portClassName»", "«message.name»")
							«sendMessageCall(hasData, "self", memberInUse(pc.name, dir+message.name), typeName+refp, refa+"data")»
							ET_MSC_LOGGER_ASYNC_OUT("Actor1", "«message.name»", "Actor2" )
						ET_MSC_LOGGER_SYNC_EXIT
					«ENDIF»
				}
				
				«messageSignature(replPortClassName, message.name, "_broadcast", data)» {
					«IF hdlr != null»
						int i;
						for (i=0; i<((etReplPort*)self)->size; ++i) {
							«portClassName»_«message.name»((etPort*)&((etReplPort*)self)->ports[i]«dataCall»);
						}					
					«ELSE»
						int i;
						ET_MSC_LOGGER_SYNC_ENTRY("«replPortClassName»", "«message.name»")
						for (i=0; i<((etReplPort*)self)->size; ++i) {
							«sendMessageCall(hasData, "((etPort*)&((etReplPort*)self)->ports[i])", memberInUse(pc.name, dir+message.name), typeName+refp, refa+"data")»
							ET_MSC_LOGGER_ASYNC_OUT("Actor1", "«message.name»", "Actor2" )
						}
						ET_MSC_LOGGER_SYNC_EXIT
					«ENDIF»
				}
				
				«messageSignature(replPortClassName, message.name, "", ", int idx"+data)» {
					«IF hdlr != null»
						«portClassName»_«message.name»((etPort*)&((etReplPort*)self)->ports[idx]«dataCall»);
					«ELSE»					
						ET_MSC_LOGGER_SYNC_ENTRY("«replPortClassName»", "«message.name»")
						if (0<=idx && idx<((etReplPort*)self)->size) {
							«sendMessageCall(hasData, "((etPort*)&((etReplPort*)self)->ports[idx])", memberInUse(pc.name, dir+message.name), typeName+refp, refa+"data")»
							ET_MSC_LOGGER_ASYNC_OUT("Actor1", "«message.name»", "Actor2" )
						}
						ET_MSC_LOGGER_SYNC_EXIT
					«ENDIF»
				}
				
			«ENDFOR»
			«IF pclass!=null»
				/* begin «portClassName» specific */
				«pclass.userCode.userCode»
				
				«pc.getPortClass(conj).operations.operationsImplementation(portClassName)»
				/* end «portClassName» specific */
				
			«ENDIF»
			etInt32 «replPortClassName»_getReplication(const «replPortClassName»* self) {
				return ((etReplPort*)self)->size;
			}
			
			«IF pc.handlesReceive(conj)»
				«genReceiveHandlers(pc,conj)»
			«ENDIF»
			
		'''
	}

	def private sendMessageCall(boolean hasData, String self, String msg, String typeName, String data) {
		if (hasData)
			"etPort_sendMessage("+self+", "+msg+", sizeof("+typeName+"), "+data+");"
		else
			"etPort_sendMessage("+self+", "+msg+", 0, NULL);"
	}
	
	def private messageSignature(String className, String messageName, String methodSuffix, String data) {
		"void "+className+"_"+messageName+methodSuffix+"(const "+className+"* self"+data+")"
	}
	
	def private messageSetterSignature(String className, String messageName, String data) {
		"void "+className+"_"+messageName+"_set("+className+"* self"+data+")"
	}
	
	def private messageGetterSignature(String className, String messageName, String type) {
		type+" "+className+"_"+messageName+"_get(const "+className+"* const self)"
	}

// TODO: can this be deleted?
//	def sendMessage(Message m, boolean conj) {'''
//	«var dir = if (conj) "IN" else "OUT"»
//	«var hdlr = m.getSendHandler(conj)»
//	«messageSignature(m)»{
//		if (getPeerAddress()!=null)
//				«IF m.data==null»getPeerMsgReceiver().receive(new EventMessage(getPeerAddress(), «dir»_«m.name»));
//				«ELSE»getPeerMsgReceiver().receive(new EventWithDataMessage(getPeerAddress(), «dir»_«m.name», «m.data.name»«IF (!m.data.ref)».deepCopy()«ENDIF»));
//			«ENDIF»
//	}
//	'''
//	}
	
	def private genReceiveHandlers(ProtocolClass pc, Boolean conj){
	var portClassName = pc.getPortClassName(conj)
	
	'''
	/* receiver handlers */
	«FOR h:getReceiveHandlers(pc,conj)»
		void «portClassName»_«h.msg.name»_receiveHandler(«portClassName»* self, const etMessage* msg, void * actor, etActorReceiveMessage receiveMessageFunc){
			«AbstractGenerator::getInstance().getTranslatedCode(h.detailCode)»
			/* hand over the message to the actor:      */
			/* (*receiveMessageFunc)(actor, self, msg); */
		}
	«ENDFOR»
	'''}	

	
	
	def private generateDebugHelpersImplementation(Root root, ProtocolClass pc){'''
		
«««		TODO: make this optional or different for smaller footprint
		/* message names as strings for debugging (generate MSC) */
		static const char* const «pc.name»_messageStrings[] = {"MIN", «FOR m : pc.getAllOutgoingMessages()»"«m.name»",«ENDFOR»«FOR m : pc.getAllIncomingMessages()»"«m.name»", «ENDFOR»"MAX"};

		const char* «pc.name»_getMessageString(int msg_id) {
			if (msg_id<«pc.name»_MSG_MIN || msg_id>«pc.name»_MSG_MAX+1){
				/* id out of range */
				return "Message ID out of range";
			}
			else{
				return «pc.name»_messageStrings[msg_id];
			}
		}
		'''
	}
	
}

Back to the top