cocoOS  5.0.1
os_msgqueue.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012 Peter Eckstrand
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted (subject to the limitations in the
8  * disclaimer below) provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the
16  * distribution.
17  *
18  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
19  * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
20  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
21  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
29  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
30  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * This file is part of the cocoOS operating system.
33  * Author: Peter Eckstrand <info@cocoos.net>
34  */
35 
36 
37 
38 #ifndef OS_MSGQUEUE_H__
39 #define OS_MSGQUEUE_H__
40 
43 #include "os_defines.h"
44 
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48 
49 
50 typedef struct {
51  uint8_t signal;
52  uint8_t reserved; /* Alignment byte */
53  uint8_t pad0;
54  uint8_t pad1;
55  uint16_t delay; /* Delay of posting in ticks */
56  uint16_t reload; /* Reload value for periodic messages */
57 } Msg_t;
58 
59 
60 typedef uint8_t MsgQ_t;
61 
62 
63 enum {
64  MSG_QUEUE_UNDEF,
65  MSG_QUEUE_DEF,
66  MSG_QUEUE_EMPTY,
67  MSG_QUEUE_FULL,
68  MSG_QUEUE_RECEIVED,
69  MSG_QUEUE_POSTED
70 };
71 
72 
73 
74 
75 #define OS_MSG_Q_POST(task_id, msg, delay, period, async ) do {\
76  uint8_t os_posted;\
77  MsgQ_t queue = os_msgQ_find(task_id);\
78  os_task_set_wait_queue(running_tid, queue);\
79  Evt_t event = os_msgQ_event_get( queue );\
80  os_task_set_change_event(running_tid, event);\
81  do {\
82  os_posted = os_msg_post( (Msg_t*)&msg, os_msgQ_find(task_id), delay, period );\
83  if ( os_posted == MSG_QUEUE_FULL ){\
84  if ( async == 0 ) {\
85  os_task_set_msg_result(running_tid, os_posted);\
86  event_wait(event);\
87  os_posted = os_task_get_msg_result(running_tid);\
88  event = os_task_get_change_event(running_tid);\
89  }\
90  else {\
91  os_posted = MSG_QUEUE_UNDEF;\
92  }\
93  }\
94  } while ( os_posted == MSG_QUEUE_FULL );\
95  if ( MSG_QUEUE_POSTED == os_posted ) {\
96  os_signal_event(event);\
97  os_event_set_signaling_tid( event, running_tid );\
98  }\
99  } while(0)
100 
101 
102 
103 #define OS_MSG_Q_RECEIVE(task_id, pMsg, async) do {\
104  uint8_t os_received;\
105  MsgQ_t queue = os_msgQ_find(task_id);\
106  os_task_set_wait_queue(running_tid, queue);\
107  Evt_t event = os_msgQ_event_get(queue);\
108  os_task_set_change_event(running_tid, event);\
109  do {\
110  os_received = os_msg_receive((Msg_t*)pMsg, os_msgQ_find(task_id));\
111  if ( os_received == MSG_QUEUE_EMPTY ){\
112  if ( async == 0 ) {\
113  os_task_set_msg_result(running_tid, os_received);\
114  event_wait(event);\
115  os_received = os_task_get_msg_result(running_tid);\
116  event = os_task_get_change_event(running_tid);\
117  }\
118  else {\
119  ((Msg_t*)pMsg)->signal = NO_MSG_ID;\
120  os_received = MSG_QUEUE_UNDEF;\
121  }\
122  }\
123  } while ( os_received == MSG_QUEUE_EMPTY );\
124  if ( MSG_QUEUE_RECEIVED == os_received) {\
125  os_signal_event(event);\
126  os_event_set_signaling_tid(event, running_tid );\
127  }\
128  } while(0)
129 
130 
131 
132 
133 void os_msgQ_init();
134 MsgQ_t os_msgQ_create( Msg_t *buffer, uint8_t nMessages, uint16_t msgSize, uint8_t task_id );
135 MsgQ_t os_msgQ_find( uint8_t task_id );
136 //Sem_t os_msgQ_sem_get( MsgQ_t queue );
137 Evt_t os_msgQ_event_get( MsgQ_t queue );
138 void os_msgQ_tick( MsgQ_t queue );
139 
140 uint8_t os_msg_post( Msg_t *msg, MsgQ_t queue, uint16_t delay, uint16_t period );
141 uint8_t os_msg_receive( Msg_t *msg, MsgQ_t queue );
142 
143 
144 #ifdef __cplusplus
145 }
146 #endif
147 
148 #endif
Definition: os_msgqueue.h:50