Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5aabc44d172b89328ac1e0882837531c648558d1 (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
/*******************************************************************************
 * 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 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * CONTRIBUTORS:
 * 		Thomas Schuetz (initial contribution)
 *
 *******************************************************************************/


#include "etMessageService.h"


#include "etSystemProtocol.h"
#include "debugging/etLogger.h"
#include "debugging/etMSCLogger.h"
#include "runtime/etRuntime.h"
#include "helpers/etTimeHelpers.h"

static void etMessageService_timerCallback(void* data);
static void etMessageService_deliverAllMessages(etMessageService* self);

/*
 * initialize message service with all needed data and initialize message queue and message pool
 *
 */
void etMessageService_init(
		etMessageService* self,
		etUInt8* buffer,
		etUInt16 maxBlocks,
		etUInt16 blockSize,
		etStacksize stacksize,
		etPriority priority,
		etTime interval,
		etDispatcherReceiveMessage msgDispatcher,
		etMessageService_execmode execmode){
	etMessageService_initx(self, NULL, buffer, maxBlocks, blockSize, stacksize, priority, interval, msgDispatcher, execmode);
}

/*
 * initialize message service with all needed data and initialize message queue and message pool
 *
 */
void etMessageService_initx(
		etMessageService* self,
		const char* name,
		etUInt8* buffer,
		etUInt16 maxBlocks,
		etUInt16 blockSize,
		etStacksize stacksize,
		etPriority priority,
		etTime interval,
		etDispatcherReceiveMessage msgDispatcher,
		etMessageService_execmode execmode){
	ET_MSC_LOGGER_SYNC_ENTRY("etMessageService", "init")

	/* copy init data to self */
	self->name = name;
	self->messageBuffer.buffer = buffer;
	self->messageBuffer.maxBlocks = maxBlocks;
	self->messageBuffer.blockSize = blockSize;
	self->msgDispatcher = msgDispatcher;
	self->execmode = execmode;

	/* init queue and pool */
	etMessageQueue_init( &(self->messagePool) ); 	/* the pool is also a queue*/
	etMessageQueue_init( &(self->messageQueue) );
	etMessageService_initMessagePool(self);

	/* init mutexes and semaphores */
	etMutex_construct( &(self->poolMutex) );
	etMutex_construct( &(self->queueMutex) );
	etSema_construct( &(self->executionSemaphore) );

	/* init high prio functions */
	self->highPrioFuncRoot=NULL;

	/* init thread */
	etThread_construct(&self->thread, stacksize, priority, "MessageService", (etThreadFunction) etMessageService_deliverAllMessages, self);

	if (execmode==EXECMODE_POLLED || execmode==EXECMODE_MIXED) {
		/* init timer */
		etTimer_construct(&self->timer, &interval, etMessageService_timerCallback, self);
	}

	/* init statistics */
	self->statistics.highWaterMark = 0;
	self->statistics.queueStatistics = &self->messageQueue.statistics;
	self->resetStatistics = ET_FALSE;

	/* register with runtime */
	etRuntime_registerMessageService(self);

	ET_MSC_LOGGER_SYNC_EXIT
}

void etMessageService_start(etMessageService* self){
	ET_MSC_LOGGER_SYNC_ENTRY("etMessageService", "start")
	etThread_start( &(self->thread) );
	if (self->execmode==EXECMODE_POLLED || self->execmode==EXECMODE_MIXED) {
		etTimer_start(&self->timer);
	}
	ET_MSC_LOGGER_SYNC_EXIT
}

void etMessageService_stop(etMessageService* self){
	etSystemProtocolConjPort port;
	ET_MSC_LOGGER_SYNC_ENTRY("etMessageService", "stop")

	if (self->execmode==EXECMODE_POLLED || self->execmode==EXECMODE_MIXED) {
		etTimer_stop(&self->timer);
	}

	/* create a temporary port struct and send the terminate message */
	port.localId = 0;
	port.msgService = self;
	port.peerAddress = MESSAGESERVICE_ADDRESS;
	etSystemProtocolConjPort_terminate(&port);

	ET_MSC_LOGGER_SYNC_EXIT
}

void etMessageService_destroy(etMessageService* self){
	ET_MSC_LOGGER_SYNC_ENTRY("etMessageService", "destroy")

	/* unregister from runtime */
	etRuntime_unregisterMessageService(self);

	etThread_destruct( &(self->thread) );
	etMutex_destruct( &(self->poolMutex) );
	etMutex_destruct( &(self->queueMutex) );
	etSema_destruct( &(self->executionSemaphore) );

	ET_MSC_LOGGER_SYNC_EXIT
}


/*
 * initialize message pool with block buffer
 * all blocks are added to pool
 */
void etMessageService_initMessagePool(etMessageService* self){
	ET_MSC_LOGGER_SYNC_ENTRY("etMessageService", "initMessagePool")
	etInt16 i;

	for (i=0; i<self->messageBuffer.maxBlocks; i++){
		etMessage* block = (etMessage*) &self->messageBuffer.buffer[i*self->messageBuffer.blockSize];
		etMessageQueue_push(&self->messagePool, block);
	}
	etMessageQueue_resetLowWaterMark(&self->messagePool);
	ET_MSC_LOGGER_SYNC_EXIT
}

void etMessageService_pushMessage(etMessageService* self, etMessage* msg){
	ET_MSC_LOGGER_SYNC_ENTRY("etMessageService", "pushMessage")
	etMutex_enter(&self->queueMutex);
	etMessageQueue_push(&self->messageQueue, msg);
	etSema_wakeup(&self->executionSemaphore);
	etMutex_leave(&self->queueMutex);
	ET_MSC_LOGGER_SYNC_EXIT
}

etMessage* etMessageService_popMessage(etMessageService* self){
	etMessage* msg;

	ET_MSC_LOGGER_SYNC_ENTRY("etMessageService", "popMessage")
	etMutex_enter(&self->queueMutex);
	msg = etMessageQueue_pop(&self->messageQueue);
	etMutex_leave(&self->queueMutex);
	ET_MSC_LOGGER_SYNC_EXIT
	return msg;
}


etMessage* etMessageService_getMessageBuffer(etMessageService* self, etUInt16 size){
	ET_MSC_LOGGER_SYNC_ENTRY("etMessageService", "getMessageBuffer")
	etMutex_enter(&self->poolMutex);
	if (size<=self->messageBuffer.blockSize){
		if (self->messagePool.size>0){
			etMessage* msg = etMessageQueue_pop(&self->messagePool);
			etMutex_leave(&self->poolMutex);
			ET_MSC_LOGGER_SYNC_EXIT
			return msg;
		}
		else {
			etLogger_logErrorF("etMessageService_getMessageBuffer: message pool empty: %d", etMessageService_getMessagePoolLowWaterMark(self));
		}
	}
	else {
		etLogger_logErrorF("etMessageService_getMessageBuffer: message too big: %d, blockSize: %d", size, self->messageBuffer.blockSize);
	}
	etMutex_leave(&self->poolMutex);
	ET_MSC_LOGGER_SYNC_EXIT
	return NULL;
}

void etMessageService_returnMessageBuffer(etMessageService* self, etMessage* buffer){
	ET_MSC_LOGGER_SYNC_ENTRY("etMessageService", "returnMessageBuffer")
	etMutex_enter(&self->poolMutex);
	etMessageQueue_push(&self->messagePool, buffer);
	etMutex_leave(&self->poolMutex);
	ET_MSC_LOGGER_SYNC_EXIT
}

void etMessageService_registerHighPrioFunc(etMessageService* self,etHighPrioFunc *func){
	ET_MSC_LOGGER_SYNC_ENTRY("etMessageService", "registerHighPrioFunc")
	func->next=self->highPrioFuncRoot;
	self->highPrioFuncRoot=func;
	ET_MSC_LOGGER_SYNC_EXIT
}

void etMessageService_unregisterHighPrioFunc(etMessageService* self,etHighPrioFunc *func){
	ET_MSC_LOGGER_SYNC_ENTRY("etMessageService", "unregisterHighPrioFunc")
	etHighPrioFunc *highPrioFunc;
	if (self->highPrioFuncRoot==func){
		self->highPrioFuncRoot=self->highPrioFuncRoot->next;
	}else{
		highPrioFunc = self->highPrioFuncRoot;
		while(highPrioFunc != NULL){
			if (highPrioFunc->next==func){
				highPrioFunc->next=highPrioFunc->next->next;
				break;
			}
			highPrioFunc=highPrioFunc->next;
		}
	}
	ET_MSC_LOGGER_SYNC_EXIT
}

static void etMessageService_callHighPrioFunc(etMessageService* self){
	ET_MSC_LOGGER_SYNC_ENTRY("etMessageService", "callHighPrioFunc")
	etHighPrioFunc *highPrioFunc;
	highPrioFunc = self->highPrioFuncRoot;
	while (highPrioFunc != NULL){
		(*(highPrioFunc->func))(highPrioFunc->param);
		highPrioFunc=highPrioFunc->next;
	}
	ET_MSC_LOGGER_SYNC_EXIT
}

static etBool etMessageService_dispatchMessage(etMessageService* self, const etMessage* msg) {
	ET_MSC_LOGGER_SYNC_ENTRY("etMessageService", "dispatchMessages")
	etBool success;
	etTime startTime, endTime;
	etTimeDiff delta;

	getTimeFromTarget(&startTime);
	success = self->msgDispatcher(msg);
	getTimeFromTarget(&endTime);
	delta = etTimeHelpers_delta(&endTime, &startTime);

	if (delta > self->statistics.highWaterMark) {
		self->statistics.highWaterMark = delta;
	}

	ET_MSC_LOGGER_SYNC_EXIT
	return success;
}

static void etMessageService_deliverAllMessages(etMessageService* self){
	ET_MSC_LOGGER_SYNC_ENTRY("etMessageService", "deliverAllMessages")
	{
		etBool cont = ET_TRUE;
		while (cont){
			etMessageService_callHighPrioFunc(self);

			if (self->resetStatistics) {
				self->resetStatistics = ET_FALSE;

				self->statistics.highWaterMark = 0;
				etMessageQueue_resetHighWaterMark(&self->messageQueue);
				etMessageQueue_resetLowWaterMark(&self->messageQueue);
			}

			while (etMessageQueue_isNotEmpty(&self->messageQueue) && cont){
				etMessage* msg = etMessageService_popMessage(self);
				cont &= etMessageService_dispatchMessage(self, msg);
				etMessageService_returnMessageBuffer(self, msg);
				etMessageService_callHighPrioFunc(self);
			}
			if (cont)
				etSema_waitForWakeup(&self->executionSemaphore);
		}
	}
	ET_MSC_LOGGER_SYNC_EXIT
}

etInt16 etMessageService_getMessagePoolLowWaterMark(etMessageService* self){
	ET_MSC_LOGGER_SYNC_ENTRY("etMessageService", "getMessagePoolLowWaterMark")
	etInt16 lowWaterMark = etMessageQueue_getLowWaterMark(&self->messagePool);
	ET_MSC_LOGGER_SYNC_EXIT
	return lowWaterMark;
}

static void etMessageService_timerCallback(void* data) {
	ET_MSC_LOGGER_SYNC_ENTRY("etMessageService", "timerCallback")
	{
		etMessageService* self = (etMessageService*) data;

		/* create a temporary port struct and send the poll message */
		etSystemProtocolConjPort port;
		port.localId = 0;
		port.msgService = self;
		port.peerAddress = MESSAGESERVICE_ADDRESS;
		etSystemProtocolConjPort_poll(&port);
	}
	ET_MSC_LOGGER_SYNC_EXIT
}


Back to the top