Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cd729b9a559ed1ce54ee50e39fd7181ab6246d22 (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
/*******************************************************************************
 * 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:
 * 		Thomas Schuetz (initial contribution)
 *
 *******************************************************************************/

/**
 * \file etMessageService.h
 *
 * this component implements a message service that delivers messages asynchronously
 *
 * \author Thomas Schuetz
 */

#ifndef _ETMESSAGESERVICE_H_
#define _ETMESSAGESERVICE_H_

#include <stddef.h>
#include "etDatatypes.h"
#include "messaging/etMessageQueue.h"
#include "messaging/etMessageReceiver.h"

#include "osal/etMutex.h"
#include "osal/etThread.h"
#include "osal/etSema.h"
#include "osal/etTimer.h"

/** the address of the message service */
#define MESSAGESERVICE_ADDRESS		1
/** the base address for other receivers */
#define BASE_ADDRESS				32

/** enumeration for execution modes */
typedef enum etMessageService_execmode {
	EXECMODE_POLLED,		/**< polled execution for data driven systems */
	EXECMODE_BLOCKED,		/**< blocked execution for asynchronous systems */
	EXECMODE_MIXED			/**< mixed execution for mixed systems */
} etMessageService_execmode;

/**
 * the data structure for the message buffer (chunks of equal size)
 */
typedef struct etBuffer{
	etUInt8 *buffer;		/**< buffer points to the actual memory position for the message pool */
	etUInt16 maxBlocks;		/**< number of blocks for the message pool */
	etUInt16 blockSize;		/**< size of blocks for the message pool */
} etBuffer;

typedef struct etMessageService {
	etMessageQueue messageQueue;				/**< message queue that holds all used messages */
	etMessageQueue messagePool;					/**< message pool that holds all free messages */
	etBuffer messageBuffer;						/**< information about the message buffer that holds information about
													the actual memory position and size for the message pool */
	etDispatcherReceiveMessage msgDispatcher;	/**< function pointer to the generated message dispatcher function */
	etThread thread;							/**< thread for the execution of the message service */
	etMutex poolMutex;							/**< mutex for synchronizing the access to the message pool */
	etMutex queueMutex;							/**< mutex for synchronizing the access to the message queue */
	etSema executionSemaphore;					/**< semaphore for waiting and waking up the execution */
	etTimer timer;								/**< timer for cyclic calls */
	etMessageService_execmode execmode;			/**< execution mode*/
} etMessageService;

/**
 * initialization (construction)
 *
 * \param self the this pointer
 * \param buffer the buffer for the message pool
 * \param maxBlocks the number of (equal sized) message blocks in the buffer
 * \param blockSize the size of each message block
 * \param stackSize the stack size for the thread in which this service will run
 * \param priority the thread priority
 * \param interval the polling interval
 * \param msgDispatcher the dispatcher method
 * \param execmdoe the execution mode for this message service
 */
void etMessageService_init(
		etMessageService* self,
		etUInt8* buffer,
		etUInt16 maxBlocks,
		etUInt16 blockSize,
		etStacksize stacksize,
		etPriority priority,
		etTime interval,
		etDispatcherReceiveMessage msgDispatcher,
		etMessageService_execmode execmode);

/**
 * start of the message service (starts the associated thread)
 *
 * \param self the this pointer
 */
void etMessageService_start(etMessageService* self);

/**
 * the polling method to be called cyclically
 *
 * \param self the this pointer
 */
void etMessageService_execute(etMessageService* self);

/**
 * stops the message service thread
 *
 * \param self the this pointer
 */
void etMessageService_stop(etMessageService* self);

/**
 * destroys the message service
 *
 * \param self the this pointer
 */
void etMessageService_destroy(etMessageService* self);

/**
 * initialization of message pool
 *
 * \param self the this pointer
 */
void etMessageService_initMessagePool(etMessageService* self);

/**
 * push a message to the queue. The queue access is synchronized. The method notifies the dispatch thread.
 *
 * \param self the this pointer
 * \msg the message to push
 */
void etMessageService_pushMessage(etMessageService* self, etMessage* msg);

/**
 * pop a message from the queue. The queue access is synchronized.
 *
 * \param self the this pointer
 * \return the extracted message
 */
etMessage* etMessageService_popMessage(etMessageService* self);

/**
 * get a chunk of uninitialized memory interpreted as \ref etMessage from the services memory management
 *
 * \param self the this pointer
 * \param size the size of the message in bytes
 * \return a message pointer to a chunk of memory with at least the required size
 */
etMessage* etMessageService_getMessageBuffer(etMessageService* self, etUInt16 size);

/**
 * return a chunk of previously obtained memory to the message service memory management
 *
 * \param self the this pointer
 * \param buffer the memory to be freed
 */
void etMessageService_returnMessageBuffer(etMessageService* self, etMessage* buffer);

/**
 * returns the low water mark of the message pool
 *
 * \param self the this pointer
 * \return the low water mark of the message pool
 */
etInt16 etMessageService_getMessagePoolLowWaterMark(etMessageService* self);


#endif /* RMESSAGESERVICE_H_ */

Back to the top