Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 45e65d5a1ff9f38907da714113de479dff863890 (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
/*******************************************************************************
 * 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.Message
import org.eclipse.etrice.core.room.ProtocolClass
import org.eclipse.etrice.generator.base.ILogger
import org.eclipse.etrice.generator.etricegen.Root
import org.eclipse.xtext.generator.JavaIoFileSystemAccess

import org.eclipse.etrice.generator.extensions.RoomExtensions
import org.eclipse.etrice.generator.generic.ProcedureHelpers
import org.eclipse.etrice.generator.generic.GenericProtocolClassGenerator


@Singleton
class ProtocolClassGen extends GenericProtocolClassGenerator {

	@Inject extension JavaIoFileSystemAccess fileAccess
	@Inject extension CExtensions stdExt
	@Inject extension RoomExtensions roomExt
	@Inject extension ProcedureHelpers helpers
	@Inject ILogger logger
	
	def doGenerate(Root root) {
		for (pc: root.usedProtocolClasses) {
			var path = pc.generationTargetPath+pc.getPath

			logger.logInfo("generating ProtocolClass header '"+pc.getCHeaderFileName+"' in '"+path+"'")
			fileAccess.setOutputPath(path)
			fileAccess.generateFile(pc.getCHeaderFileName, root.generateHeaderFile(pc))

			logger.logInfo("generating ProtocolClass source '"+pc.getCSourceFileName+"' in '"+path+"'")
			fileAccess.setOutputPath(path)
			fileAccess.generateFile(pc.getCSourceFileName, root.generateSourceFile(pc))

		}
	}

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

		«generateIncludeGuardBegin(pc.name)»
		
		#include "etDatatypes.h"
		#include "etPort.h"
		
		«helpers.userCode(pc.userCode1)»
		
		«FOR dataClass : root.getReferencedDataClasses(pc)»
			#include "«dataClass.name».h"
		«ENDFOR»
		
		/* message IDs */
		«genMessageIDs(pc)»

		/*--------------------- port classes */
		«portClassHeader(pc, false)»
		«portClassHeader(pc, true)»
«««			«portClass(pc, false)»
«««			«portClass(pc, true)»

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

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

		#include "«pc.getCHeaderFileName»"
		
		#include "etMSCLogger.h"
		
		/*--------------------- port classes */
		«portClassSource(pc, false)»
		«portClassSource(pc, true)»
		
		/*--------------------- debug helpers */
		«generateDebugHelpersImplementation(root, pc)»
	'''
	}
	
	
	
	def portClass(ProtocolClass pc, Boolean conj) {'''
«««		«var name = pc.getPortClassName(conj)»
«««		«var pclass = pc.getPortClass(conj)»
		
«««		// port class
«««		static public class «name» extends PortBase {
«««			«IF pclass!=null»
«««				«helpers.UserCode(pclass.userCode)»
«««			«ENDIF»
«««			// constructors
«««			public «name»(IEventReceiver actor, String name, int localId, Address addr, Address peerAddress) {
«««				super(actor, name, localId, 0, addr, peerAddress);
«««				DebuggingService.getInstance().addPortInstance(this);
«««			}
«««			public «name»(IEventReceiver actor, String name, int localId, int idx, Address addr, Address peerAddress) {
«««				super(actor, name, localId, idx, addr, peerAddress);
«««				DebuggingService.getInstance().addPortInstance(this);
«««			}
«««		
«««			@Override
«««			public void receive(Message m) {
«««					if (!(m instanceof EventMessage))
«««						return;
«««					EventMessage msg = (EventMessage) m;
«««					if (msg.getEvtId() <= 0 || msg.getEvtId() >= MSG_MAX)
«««						System.out.println("unknown");
«««					else {
«««						if (messageStrings[msg.getEvtId()] != "timerTick"){
«««							// TODOTS: model switch for activation
«««							DebuggingService.getInstance().addMessageAsyncIn(getPeerAddress(), getAddress(), messageStrings[msg.getEvtId()]);
«««						}
«««						«IF pc.handlesReceive(conj)»
«««						switch (msg.getEvtId()) {
«««							«FOR hdlr : pc.getReceiveHandlers(conj)»
«««								case «hdlr.msg.getCodeName()»:
«««								{
«««									«FOR command : hdlr.detailCode.commands»
«««										«command»
«««									«ENDFOR»
«««								}
«««								break;
«««							«ENDFOR»
«««							default:
«««						«ENDIF»
«««							if (msg instanceof EventWithDataMessage)
«««								getActor().receiveEvent(this, msg.getEvtId(), ((EventWithDataMessage)msg).getData());
«««							else
«««								getActor().receiveEvent(this, msg.getEvtId(), null);
«««						«IF pc.handlesReceive(conj)»
«««						}
«««						«ENDIF»
«««					}
«««			}
«««		
«««			«IF pclass!=null»
«««				«helpers.Attributes(pclass.attributes)»
«««				«helpers.OperationsDeclaration(pclass.operations, name)»
«««			«ENDIF»
«««			
«««			// sent messages
«««			«FOR m : pc.getOutgoing(conj)»
«««				«sendMessage(m, conj)»
«««			«ENDFOR»
«««		}
		
«««		// replicated port class
«««		static public class «name»Repl {
«««			private ArrayList<«name»> ports;
«««			private int replication;
«««		
«««			public «name»Repl(IEventReceiver actor, String name, int localId, Address[] addr,
«««					Address[] peerAddress) {
«««				replication = addr.length;
«««				ports = new ArrayList<«pc.name».«name»>(replication);
«««				for (int i=0; i<replication; ++i) {
«««					ports.add(new «name»(
«««							actor, name+i, localId, i, addr[i], peerAddress[i]));
«««				}
«««			}
«««			
«««			public int getReplication() {
«««				return replication;
«««			}
«««			
«««			public int getIndexOf(InterfaceItemBase ifitem){
«««					return ifitem.getIdx();
«««				}
«««			
«««			public «name» get(int i) {
«««				return ports.get(i);
«««			}
«««			
«««			«IF conj»
«««			// incoming messages
«««			«FOR m : pc.getAllIncomingMessages()»
«««			«messageSignature(m)»{
«««				for (int i=0; i<replication; ++i) {
«««					ports.get(i).«messageCall(m)»;
«««				}
«««			}
«««			«ENDFOR»
«««			«ELSE»
«««			// outgoing messages
«««			«FOR m : pc.getAllOutgoingMessages()»
«««			«messageSignature(m)»{
«««				for (int i=0; i<replication; ++i) {
«««					ports.get(i).«messageCall(m)»;
«««				}
«««			}
«««			«ENDFOR»
«««			«ENDIF»
«««		}
		
	'''
	}

	def portClassHeader(ProtocolClass pc, Boolean conj){
		'''
		«var portClassName = pc.getPortClassName(conj)»
		«var pClass = pc.getPortClass(conj)»
		
		typedef etPort «portClassName»;
		
		«IF !conj»
«««			send functions for outgoing messages
			«FOR message : pc.getAllOutgoingMessages()»
				void «portClassName»_«message.name»(const «portClassName»* self);
			«ENDFOR»
		«ELSE»
«««			send functions for incoming messages
			«FOR message : pc.getAllIncomingMessages()»
				void «portClassName»_«message.name»(const «portClassName»* self);
			«ENDFOR»
		«ENDIF»
		
		
«««		«ClassOperationSignature(portClassName, "MyOperation1", "int a, int b", "void", true)»
«««		«ClassOperationSignature(portClassName, "MyOperation2", "", "int", false)»
		
		
		'''
	}
	
	def portClassSource(ProtocolClass pc, Boolean conj){
		'''
		«var portClassName = pc.getPortClassName(conj)»
		«var pClass = pc.getPortClass(conj)»
		
		«IF !conj»
«««			send functions for outgoing messages
			«FOR message : pc.getAllOutgoingMessages()»
				void «portClassName»_«message.name»(const «portClassName»* self){
					ET_MSC_LOGGER_SYNC_ENTRY("«portClassName»", "«message.name»")
					etMessage* msg = etMessageService_getMessageBuffer(self->msgService, sizeof(etMessage));
					msg->address = self->peerAddress;
					msg->evtID = «memberInUse(pc.name, "OUT_"+message.name)»;
					etMessageService_pushMessage(self->msgService, msg);
					ET_MSC_LOGGER_SYNC_EXIT
				}
			«ENDFOR»
		«ELSE»
«««			send functions for incoming messages
			«FOR message : pc.getAllIncomingMessages()»
				void «portClassName»_«message.name»(const «portClassName»* self){
					ET_MSC_LOGGER_SYNC_ENTRY("«portClassName»", "«message.name»")
					etMessage* msg = etMessageService_getMessageBuffer(self->msgService, sizeof(etMessage));
					msg->address = self->peerAddress;
					msg->evtID = «memberInUse(pc.name, "IN_"+message.name)»;
					etMessageService_pushMessage(self->msgService, msg);
					ET_MSC_LOGGER_SYNC_EXIT
				}
			«ENDFOR»
		«ENDIF»
		
		
		'''
	}
	

	def messageSignature(ProtocolClass pc, Message m) {'''
		void «pc.name»_«m.name» («IF m.data!=null»«m.data.refType.type.name» «m.data.name»«ENDIF»)
	'''
	}

	def messageCall(Message m) {'''
	«m.name»(«IF m.data!=null» «m.data.name»«ENDIF»)
	'''}
	
//	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 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* «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