-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathraw_queue_buffer.c
624 lines (428 loc) · 14.2 KB
/
raw_queue_buffer.c
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
/*
raw os - Copyright (C) Lingjun Chen(jorya_txj).
This file is part of raw os.
raw os is free software; you can redistribute it it under the terms of the
GNU General Public License as published by the Free Software Foundation;
either version 3 of the License, or (at your option) any later version.
raw os is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. if not, write email to [email protected]
---
A special exception to the LGPL can be applied should you wish to distribute
a combined work that includes raw os, without being obliged to provide
the source code for any proprietary components. See the file exception.txt
for full details of how and when the exception can be applied.
*/
/* 2013-6 Created by jorya_txj
* xxxxxx please added here
*/
#include <raw_api.h>
#if (CONFIG_RAW_QUEUE_BUFFER > 0)
/*
* Check message buffer free space
* If 'msgsz' message is able to be stored, return TRUE.
*/
RAW_INLINE RAW_BOOLEAN is_queue_buffer_free(RAW_QUEUE_BUFFER *q_b, MSG_SIZE_TYPE msg_size)
{
return ((HEADERSZ + msg_size) <= q_b->frbufsz);
}
/*
* If message buffer is empty, return TRUE.
*/
RAW_INLINE RAW_BOOLEAN is_buffer_empty(RAW_QUEUE_BUFFER *q_b)
{
return (q_b->frbufsz == q_b->bufsz);
}
/*
************************************************************************************************************************
* Create a queue buffer
*
* Description: This function is called to create a queue buffer.
*
* Arguments :p_q is the address of queue buffer object want to be initialized
* -----
* p_name is the queue buffer object name
* -----
* msg_buffer is the start address of queue buffer.
* ------
* buffer_size is the size of of the queue buffer.
* Returns
* RAW_SUCCESS: raw os return success
* RAW_QUEUE_BUFFER_SIZE_0: queue buffer size 0.
* RAW_QUEUE_BUFFER_INVALID_SIZE:invalid buffer size.
* Note(s)
*
*
************************************************************************************************************************
*/
RAW_OS_ERROR raw_queue_buffer_create(RAW_QUEUE_BUFFER *q_b, RAW_U8 *p_name, void *msg_buffer, MSG_SIZE_TYPE buffer_size, MSG_SIZE_TYPE max_msg_size)
{
MSG_SIZE_TYPE bufsz;
RAW_U8 queue_buffer_align_mask;
#if (RAW_QUEUE_BUFFER_FUNCTION_CHECK > 0)
if (q_b == 0) {
return RAW_NULL_OBJECT;
}
if (msg_buffer == 0) {
return RAW_NULL_POINTER;
}
#endif
bufsz = ROUND_BUFFER_SIZE(buffer_size);
if (bufsz == 0) {
return RAW_QUEUE_BUFFER_SIZE_0;
}
if (bufsz != buffer_size) {
return RAW_QUEUE_BUFFER_INVALID_SIZE;
}
queue_buffer_align_mask = HEADERSZ - 1u;
if (((RAW_U32)msg_buffer & queue_buffer_align_mask)){
return RAW_INVALID_ALIGN;
}
/*init the queue blocked list*/
list_init(&q_b->common_block_obj.block_list);
q_b->bufsz = bufsz;
q_b->frbufsz = bufsz;
q_b->buffer = msg_buffer;
q_b->maxmsz = max_msg_size;
q_b->head = 0;
q_b->tail = 0;
q_b->common_block_obj.name = p_name;
q_b->common_block_obj.block_way = RAW_BLOCKED_WAY_PRIO;
q_b->common_block_obj.object_type = RAW_QUEUE_BUFFER_OBJ_TYPE;
TRACE_QUEUE_BUFFER_CREATE(raw_task_active, q_b);
return RAW_SUCCESS;
}
static void msg_to_end_buffer(RAW_QUEUE_BUFFER *q_b, void *msg, MSG_SIZE_TYPE msgsz)
{
MSG_SIZE_TYPE tail = q_b->tail;
RAW_U8 *buffer = q_b->buffer;
MSG_SIZE_TYPE remsz;
q_b->frbufsz -= (HEADERSZ + ROUND_BUFFER_SIZE(msgsz));
*(HEADER*)&buffer[tail] = msgsz;
tail += HEADERSZ;
if (tail >= q_b->bufsz) {
tail = 0;
}
if ((remsz = q_b->bufsz - tail) < msgsz) {
raw_memcpy(&buffer[tail], msg, remsz);
msg = (RAW_U8 *)msg + remsz;
msgsz -= remsz;
tail = 0;
}
raw_memcpy(&buffer[tail], msg, msgsz);
tail += ROUND_BUFFER_SIZE(msgsz);
if (tail >= q_b->bufsz) {
tail = 0;
}
q_b->tail = tail;
}
static RAW_U32 buffer_to_msg(RAW_QUEUE_BUFFER *q_b, void *msg)
{
MSG_SIZE_TYPE head = q_b->head;
RAW_U8 *buffer = q_b->buffer;
MSG_SIZE_TYPE msgsz, actsz;
MSG_SIZE_TYPE remsz;
actsz = msgsz = *(HEADER*)&buffer[head];
q_b->frbufsz += (HEADERSZ + ROUND_BUFFER_SIZE(msgsz));
head += HEADERSZ;
if (head >= q_b->bufsz) {
head = 0;
}
if ((remsz = q_b->bufsz - head) < msgsz) {
raw_memcpy(msg, &buffer[head], remsz);
msg = (RAW_U8 *)msg + remsz;
msgsz -= remsz;
head = 0;
}
raw_memcpy(msg, &buffer[head], msgsz);
head += ROUND_BUFFER_SIZE(msgsz);
if (head >= q_b->bufsz) {
head = 0;
}
q_b->head = head;
return actsz;
}
RAW_OS_ERROR queue_buffer_post(RAW_QUEUE_BUFFER *q_b, void *p_void, MSG_SIZE_TYPE msg_size)
{
LIST *block_list_head;
RAW_TASK_OBJ *task_ptr;
RAW_SR_ALLOC();
RAW_CRITICAL_ENTER();
if (q_b->common_block_obj.object_type != RAW_QUEUE_BUFFER_OBJ_TYPE) {
RAW_CRITICAL_EXIT();
return RAW_ERROR_OBJECT_TYPE;
}
block_list_head = &q_b->common_block_obj.block_list;
if (!is_queue_buffer_free(q_b, msg_size)) {
RAW_CRITICAL_EXIT();
TRACE_QUEUE_BUFFER_MAX(raw_task_active, q_b, p_void, msg_size);
return RAW_QUEUE_BUFFER_FULL;
}
/*Queue buffer is not full here, if there is no blocked receive task*/
if (is_list_empty(block_list_head)) {
msg_to_end_buffer(q_b, p_void, msg_size);
RAW_CRITICAL_EXIT();
TRACE_QUEUE_BUFFER_POST(raw_task_active, q_b, p_void, msg_size);
return RAW_SUCCESS;
}
task_ptr = raw_list_entry(block_list_head->next, RAW_TASK_OBJ, task_list);
raw_memcpy(task_ptr->msg, p_void, msg_size);
task_ptr->qb_msg_size = msg_size;
raw_wake_object(task_ptr);
RAW_CRITICAL_EXIT();
TRACE_QUEUE_BUFFER_WAKE_TASK(raw_task_active, raw_list_entry(block_list_head->next, RAW_TASK_OBJ, task_list), p_void, msg_size);
raw_sched();
return RAW_SUCCESS;
}
/*
************************************************************************************************************************
* Post a msg to the queue buffer
*
* Description: This function is called to post a msg to the end of the queue buffer and inplemented fifo.
*
* Arguments :p_q is the address of the queue object
* -----
* p_void is the address of the message.
* if you want to use the extension memcpy, make sure the address is 4 bytes aligned.
* -----
* msg_size is the message size.
*
* Returns
* RAW_SUCCESS: raw os return success
* RAW_EXCEED_QUEUE_BUFFER_MSG_SIZE: message size exceed the max defined message size.
* RAW_QUEUE_BUFFER_FULL:queue is full
*
*
*
*
*
*
************************************************************************************************************************
*/
RAW_OS_ERROR raw_queue_buffer_end_post(RAW_QUEUE_BUFFER *q_b, void *p_void, MSG_SIZE_TYPE msg_size)
{
#if (RAW_QUEUE_BUFFER_FUNCTION_CHECK > 0)
if (q_b == 0) {
return RAW_NULL_OBJECT;
}
if (p_void == 0) {
return RAW_NULL_POINTER;
}
if (msg_size > q_b->maxmsz) {
return RAW_EXCEED_QUEUE_BUFFER_MSG_SIZE;
}
#endif
return queue_buffer_post(q_b, p_void, msg_size);
}
/*
************************************************************************************************************************
* Receive a msg
*
* Description: This function is called to receive a msg
*
* Arguments :q_b is the address of the queue buffer object
* -----
* msg is the address of a point, and it will be filled data lwithin this api.
* if you want to use the extension memcpy, make sure the msg address is 4 bytes aligned.
* -----
* wait_option: is how the service behaves if the msg queue is full.
* The wait options are
* defined as follows:
* RAW_NO_WAIT (0x00000000)
* RAW_WAIT_FOREVER (0xFFFFFFFF)
* timeout value (0x00000001
* through
* 0xFFFFFFFE)
* receive_size:is the msg size received.
*
* Returns
* RAW_SUCCESS: raw os return success
* RAW_BLOCK_DEL: if this queue is deleted.
* RAW_BLOCK_TIMEOUT: queue is still full during waiting time when sending msg.
* RAW_BLOCK_ABORT:queue is aborted during waiting time when sending msg.
* RAW_STATE_UNKNOWN: possibly system error.
* Note(s)
*
*
************************************************************************************************************************
*/
RAW_OS_ERROR raw_queue_buffer_receive(RAW_QUEUE_BUFFER *q_b, RAW_TICK_TYPE wait_option, void *msg, MSG_SIZE_TYPE *receive_size)
{
RAW_OS_ERROR result;
RAW_SR_ALLOC();
#if (RAW_QUEUE_BUFFER_FUNCTION_CHECK > 0)
if (raw_int_nesting) {
return RAW_NOT_CALLED_BY_ISR;
}
if (q_b == 0) {
return RAW_NULL_OBJECT;
}
if (msg == 0) {
return RAW_NULL_POINTER;
}
#endif
RAW_CRITICAL_ENTER();
if (q_b->common_block_obj.object_type != RAW_QUEUE_BUFFER_OBJ_TYPE) {
RAW_CRITICAL_EXIT();
return RAW_ERROR_OBJECT_TYPE;
}
if (!is_buffer_empty(q_b)) {
*receive_size = buffer_to_msg(q_b, msg);
RAW_CRITICAL_EXIT();
return RAW_SUCCESS;
}
if (wait_option == RAW_NO_WAIT) {
RAW_CRITICAL_EXIT();
return RAW_NO_PEND_WAIT;
}
SYSTEM_LOCK_PROCESS();
raw_task_active->msg = msg;
raw_pend_object((RAW_COMMON_BLOCK_OBJECT *)q_b, raw_task_active, wait_option);
RAW_CRITICAL_EXIT();
raw_sched();
result = block_state_post_process(raw_task_active, 0);
/*if get the msg successful then take it*/
if (result == RAW_SUCCESS) {
*receive_size = raw_task_active->qb_msg_size;
}
return result;
}
/*
************************************************************************************************************************
* Flush a queue buffer
*
* Description: This service deletes all messages stored in the specified message queue buffer.
*
* If the queue is empty, this service does nothing.
*
* Arguments :p_q is the address of the queue buffer object
* -----
*
* Returns
* RAW_SUCCESS: raw os return success
*
*
* Note(s)
*
*
************************************************************************************************************************
*/
#if (CONFIG_RAW_QUEUE_BUFFER_FLUSH > 0)
RAW_OS_ERROR raw_queue_buffer_flush(RAW_QUEUE_BUFFER *q_b)
{
RAW_SR_ALLOC();
#if (RAW_QUEUE_BUFFER_FUNCTION_CHECK > 0)
if (raw_int_nesting) {
return RAW_NOT_CALLED_BY_ISR;
}
if (q_b == 0) {
return RAW_NULL_OBJECT;
}
#endif
RAW_CRITICAL_ENTER();
if (q_b->common_block_obj.object_type != RAW_QUEUE_BUFFER_OBJ_TYPE) {
RAW_CRITICAL_EXIT();
return RAW_ERROR_OBJECT_TYPE;
}
q_b->frbufsz = q_b->bufsz;
q_b->head = 0;
q_b->tail = 0;
RAW_CRITICAL_EXIT();
return RAW_SUCCESS;
}
#endif
/*
************************************************************************************************************************
* Delete a queue buffer
*
* Description: This function is called to delete a queue buffer.
*
* Arguments :p_q is the address of this queue buffer object
* -----
* All blocked task will be waken up and return RAW_BLOCK_DEL.
*
* Returns
* RAW_SUCCESS: raw os return success
* Note(s)
*
*
************************************************************************************************************************
*/
#if (CONFIG_RAW_QUEUE_BUFFER_DELETE > 0)
RAW_OS_ERROR raw_queue_buffer_delete(RAW_QUEUE_BUFFER *q_b)
{
LIST *block_list_head;
RAW_SR_ALLOC();
#if (RAW_QUEUE_FUNCTION_CHECK > 0)
if (q_b == 0) {
return RAW_NULL_OBJECT;
}
if (raw_int_nesting) {
return RAW_NOT_CALLED_BY_ISR;
}
#endif
RAW_CRITICAL_ENTER();
if (q_b->common_block_obj.object_type != RAW_QUEUE_BUFFER_OBJ_TYPE) {
RAW_CRITICAL_EXIT();
return RAW_ERROR_OBJECT_TYPE;
}
block_list_head = &q_b->common_block_obj.block_list;
q_b->common_block_obj.object_type = RAW_OBJ_TYPE_NONE;
/*All task blocked on this queue is waken up*/
while (!is_list_empty(block_list_head)) {
delete_pend_obj(raw_list_entry(block_list_head->next, RAW_TASK_OBJ, task_list));
}
RAW_CRITICAL_EXIT();
raw_sched();
return RAW_SUCCESS;
}
#endif
/*
************************************************************************************************************************
* Get queue buffer information
*
* Description: This function is called to get information form specified queue buffer.
*
* Arguments :q_b is the address of this queue buffer object
* -----
* queue_buffer_free_size is the pointer of free size of the queue buffer, which will be filled within this api.
* -----
* queue_buffer_size is s the pointer of total size of the queue buffer, which will be filled within this api.
*
*
* Returns
* RAW_SUCCESS: raw os return success
* Note(s) Commonly for debug purpose
*
*
************************************************************************************************************************
*/
#if (CONFIG_RAW_QUEUE_BUFFER_GET_INFORMATION > 0)
RAW_OS_ERROR raw_queue_buffer_info_get(RAW_QUEUE_BUFFER *q_b, RAW_U32 *queue_buffer_free_size, RAW_U32 *queue_buffer_size)
{
RAW_SR_ALLOC();
#if (RAW_QUEUE_BUFFER_FUNCTION_CHECK > 0)
if (q_b == 0) {
return RAW_NULL_OBJECT;
}
if (queue_buffer_free_size == 0) {
return RAW_NULL_OBJECT;
}
if (queue_buffer_size == 0) {
return RAW_NULL_OBJECT;
}
#endif
RAW_CRITICAL_ENTER();
if (q_b->common_block_obj.object_type != RAW_QUEUE_BUFFER_OBJ_TYPE) {
RAW_CRITICAL_EXIT();
return RAW_ERROR_OBJECT_TYPE;
}
*queue_buffer_free_size = q_b->frbufsz;
*queue_buffer_size = q_b->bufsz;
RAW_CRITICAL_EXIT();
return RAW_SUCCESS;
}
#endif
#endif