AKOS  v1.0.0
Documentation
Loading...
Searching...
No Matches
message.c
Go to the documentation of this file.
1/****************************************************************************/
12
13#include "message.h"
14#include "memory.h"
15#include "core.h"
16
17#include <stdint.h>
18#include <string.h>
19
20static msg_t msg_pool[OS_CFG_MSG_POOL_SIZE];
21static msg_t *free_list_msg_pool;
22static uint8_t msg_pool_used;
23
27static void msg_pool_init(void)
28{
29 AKOS_CORE_ENTER_CRITICAL();
30 uint8_t index;
31
32 free_list_msg_pool = (msg_t *)msg_pool;
33
34 for (index = 0; index < OS_CFG_MSG_POOL_SIZE; index++)
35 {
36 if (index == (OS_CFG_MSG_POOL_SIZE - 1))
37 {
38 msg_pool[index].next = NULL;
39 }
40 else
41 {
42 msg_pool[index].next = (msg_t *)&msg_pool[index + 1];
43 }
44 }
45
46 msg_pool_used = 0;
47
48 AKOS_CORE_EXIT_CRITICAL();
49}
54{
55 msg_pool_init();
56}
57
62void akos_message_free(msg_t *p_msg)
63{
64 AKOS_CORE_ENTER_CRITICAL();
65
66 p_msg->next = free_list_msg_pool;
67 free_list_msg_pool = p_msg;
68 if (p_msg->type == MSG_TYPE_DYNAMIC)
69 {
70 akos_memory_free(p_msg->content_ptr);
71 }
72 msg_pool_used--;
73
74 AKOS_CORE_EXIT_CRITICAL();
75}
76
82void akos_message_queue_init(msg_queue_t *p_msg_q,
83 uint8_t size)
84{
85 p_msg_q->head_ptr = NULL;
86 p_msg_q->tail_ptr = NULL;
87 p_msg_q->size_max = size;
88 p_msg_q->size_curr = 0u;
89}
90
98void akos_message_queue_put_dynamic(msg_queue_t *p_msg_q,
99 int32_t sig,
100 void *p_content,
101 uint8_t size)
102{
103 AKOS_CORE_ENTER_CRITICAL();
104 msg_t *p_msg;
105 msg_t *p_msg_tail;
106 if (p_msg_q->size_curr >= p_msg_q->size_max)
107 {
108 // OSUniversalError = OS_ERR_MSG_QUEUE_IS_FULL;
109 core_assert(0, "OS_ERR_MSG_QUEUE_IS_FULL");
110 AKOS_CORE_EXIT_CRITICAL();
111 return;
112 }
113 if (msg_pool_used >= OS_CFG_MSG_POOL_SIZE)
114 {
115 // OSUniversalError = OS_ERR_MSG_POOL_IS_FULL;
116 core_assert(0, "OS_ERR_MSG_POOL_IS_FULL");
117 AKOS_CORE_EXIT_CRITICAL();
118 return;
119 }
120
121 p_msg = free_list_msg_pool;
122 free_list_msg_pool = p_msg->next;
123 msg_pool_used++;
124
125 p_msg->type = MSG_TYPE_DYNAMIC;
126 p_msg->sig = sig;
127 p_msg->size = size;
128 p_msg->content_ptr = (uint8_t *)akos_memory_malloc(size);
129 if (p_msg->content_ptr == NULL)
130 {
131 p_msg->next = free_list_msg_pool;
132 free_list_msg_pool = p_msg;
133 msg_pool_used--;
134 core_assert(0, "OS_ERR_MEM_NO_BLOCK");
135 AKOS_CORE_EXIT_CRITICAL();
136 return;
137 }
138 memcpy(p_msg->content_ptr, p_content, size);
139
140 if (p_msg_q->size_curr == 0u) /* Is this first message placed in the queue? */
141 {
142 p_msg_q->head_ptr = p_msg; /* Yes */
143 p_msg_q->tail_ptr = p_msg;
144 p_msg_q->size_curr = 1u;
145 p_msg->next = NULL;
146 }
147 else
148 {
149 p_msg_tail = p_msg_q->tail_ptr;
150 p_msg_tail->next = p_msg;
151 p_msg_q->tail_ptr = p_msg;
152 p_msg->next = NULL;
153
154 p_msg_q->size_curr++;
155 }
156
157 AKOS_CORE_EXIT_CRITICAL();
158}
159
165void akos_message_queue_put_pure(msg_queue_t *p_msg_q, int32_t sig)
166{
167 AKOS_CORE_ENTER_CRITICAL();
168 msg_t *p_msg;
169 msg_t *p_msg_tail;
170 if (p_msg_q->size_curr >= p_msg_q->size_max)
171 {
172 // OSUniversalError = OS_ERR_MSG_QUEUE_IS_FULL;
173 core_assert(0, "OS_ERR_MSG_QUEUE_IS_FULL");
174 AKOS_CORE_EXIT_CRITICAL();
175 return;
176 }
177 if (msg_pool_used >= OS_CFG_MSG_POOL_SIZE)
178 {
179 /* This states that u forget to free msg somewhere.*/
180 // OSUniversalError = OS_ERR_MSG_POOL_IS_FULL;
181 core_assert(0, "OS_ERR_MSG_POOL_IS_FULL");
182 AKOS_CORE_EXIT_CRITICAL();
183 return;
184 }
185
186 p_msg = free_list_msg_pool;
187 free_list_msg_pool = p_msg->next;
188 msg_pool_used++;
189
190 if (p_msg_q->size_curr == 0u) /* Is this first message placed in the queue? */
191 {
192 p_msg_q->head_ptr = p_msg; /* Yes */
193 p_msg_q->tail_ptr = p_msg;
194 p_msg_q->size_curr = 1u;
195 p_msg->next = NULL;
196 }
197 else
198 {
199 p_msg_tail = p_msg_q->tail_ptr;
200 p_msg_tail->next = p_msg;
201 p_msg_q->tail_ptr = p_msg;
202 p_msg->next = NULL;
203
204 p_msg_q->size_curr++;
205 }
206
207 p_msg->type = MSG_TYPE_PURE;
208 p_msg->sig = sig;
209
210 AKOS_CORE_EXIT_CRITICAL();
211}
212
218msg_t *akos_message_queue_get(msg_queue_t *p_msg_q)
219{
220 msg_t *p_msg;
221
222 if (p_msg_q->size_curr == 0u)
223 {
224 // OSUniversalError = OS_ERR_MSG_QUEUE_IS_EMPTY;
225 // core_assert(0);
226 return NULL;
227 }
228
229 p_msg = p_msg_q->head_ptr;
230
231 p_msg_q->head_ptr = p_msg->next;
232
233 if (p_msg_q->size_curr == 1u) /* Are there any more messages in the queue? */
234 {
235 p_msg_q->head_ptr = NULL;
236 p_msg_q->tail_ptr = NULL;
237 p_msg_q->size_curr = 0u;
238 }
239 else
240 {
241 p_msg_q->size_curr--; /* Yes, One less message in the queue */
242 }
243
244 return (p_msg);
245}
246
254 uint8_t *p_msg_size)
255{
256 *p_msg_size = p_msg->size;
257 return p_msg->content_ptr;
258}
259
265msg_t *akos_message_queue_get_pure(msg_queue_t *p_msg_q)
266{
267 msg_t *p_msg;
268
269 if (p_msg_q->size_curr == 0u)
270 {
271 // OSUniversalError = OS_ERR_MSG_QUEUE_IS_EMPTY;
272 // core_assert(0);
273 return NULL;
274 }
275
276 p_msg = p_msg_q->head_ptr;
277
278 p_msg_q->head_ptr = p_msg->next;
279
280 if (p_msg_q->size_curr == 1u) /* Are there any more messages in the queue? */
281 {
282 p_msg_q->head_ptr = NULL;
283 p_msg_q->tail_ptr = NULL;
284 p_msg_q->size_curr = 0u;
285 }
286 else
287 {
288 p_msg_q->size_curr--; /* Yes, One less message in the queue */
289 }
290
291 return (p_msg);
292}
293
299int32_t akos_message_get_pure_data(msg_t *p_msg)
300{
301 return p_msg->sig;
302}
#define OS_CFG_MSG_POOL_SIZE
Definition config.h:35
Kernel control and critical-section API.
Static-heap allocator APIs.
void akos_memory_free(void *p_addr)
Free previously allocated memory block.
Definition memory.c:139
void * akos_memory_malloc(size_t size)
Allocate memory from OS heap.
Definition memory.c:65
void * akos_message_get_dynamic_data(msg_t *p_msg, uint8_t *p_msg_size)
Access payload pointer and payload size from dynamic message.
Definition message.c:253
msg_t * akos_message_queue_get(msg_queue_t *p_msg_q)
Dequeue next message from queue.
Definition message.c:218
msg_t * akos_message_queue_get_pure(msg_queue_t *p_msg_q)
Dequeue next pure message from queue.
Definition message.c:265
void akos_message_init(void)
Initialize message subsystem.
Definition message.c:53
void akos_message_free(msg_t *p_msg)
Free a message and return resources to pools.
Definition message.c:62
int32_t akos_message_get_pure_data(msg_t *p_msg)
Read signal value from pure message.
Definition message.c:299
void akos_message_queue_put_dynamic(msg_queue_t *p_msg_q, int32_t sig, void *p_content, uint8_t size)
Enqueue dynamic message and copy payload.
Definition message.c:98
void akos_message_queue_init(msg_queue_t *p_msg_q, uint8_t size)
Initialize a message queue.
Definition message.c:82
void akos_message_queue_put_pure(msg_queue_t *p_msg_q, int32_t sig)
Enqueue pure message (signal only).
Definition message.c:165
Message object and message queue APIs.
@ MSG_TYPE_PURE
Definition message.h:39
@ MSG_TYPE_DYNAMIC
Definition message.h:40