Mistake on this page?
Report an issue in GitHub or email us
Mail.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2006-2019 ARM Limited
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  */
22 #ifndef MAIL_H
23 #define MAIL_H
24 
25 #include <stdint.h>
26 #include <string.h>
27 
28 #include "rtos/Queue.h"
29 #include "rtos/MemoryPool.h"
30 #include "rtos/mbed_rtos_types.h"
31 #include "rtos/mbed_rtos_storage.h"
32 #include "rtos/mbed_rtos1_types.h"
33 
34 #include "platform/mbed_toolchain.h"
35 #include "platform/NonCopyable.h"
36 
37 #ifndef MBED_NO_GLOBAL_USING_DIRECTIVE
38 using namespace rtos;
39 #endif
40 
41 #if MBED_CONF_RTOS_PRESENT || defined(DOXYGEN_ONLY)
42 
43 namespace rtos {
44 /** \addtogroup rtos-public-api */
45 /** @{*/
46 
47 /**
48  * \defgroup rtos_Mail Mail class
49  * @{
50  */
51 
52 /** The Mail class allows you to control, send, receive or wait for mail.
53  * A mail is a memory block that is sent to a thread or interrupt service routine (ISR).
54  * @tparam T Data type of a single mail message element.
55  * @tparam queue_sz Maximum number of mail messages in queue.
56  *
57  * @note
58  * Memory considerations: The mail data store and control structures are part of this class - they do not (themselves)
59  * allocate memory on the heap, both for the Mbed OS and underlying RTOS objects (static or dynamic RTOS memory
60  * pools are not being used).
61  */
62 template<typename T, uint32_t queue_sz>
63 class Mail : private mbed::NonCopyable<Mail<T, queue_sz> > {
64 public:
65  /** Create and initialize Mail queue.
66  *
67  * @note You cannot call this function from ISR context.
68  */
69  Mail() { };
70 
71  /** Check if the mail queue is empty.
72  *
73  * @return State of queue.
74  * @retval true Mail queue is empty.
75  * @retval false Mail queue contains mail.
76  *
77  * @note You may call this function from ISR context.
78  */
79  bool empty() const
80  {
81  return _queue.empty();
82  }
83 
84  /** Check if the mail queue is full.
85  *
86  * @return State of queue.
87  * @retval true Mail queue is full.
88  * @retval false Mail queue is not full.
89  *
90  * @note You may call this function from ISR context.
91  */
92  bool full() const
93  {
94  return _queue.full();
95  }
96 
97  /** Allocate a memory block of type T, without blocking.
98  *
99  * @param millisec Not used (see note).
100  *
101  * @return Pointer to memory block that you can fill with mail or nullptr in case error.
102  *
103  * @note You may call this function from ISR context.
104  * @note If blocking is required, use Mail::alloc_for or Mail::alloc_until
105  */
106  T *alloc(MBED_UNUSED uint32_t millisec = 0)
107  {
108  return _pool.alloc();
109  }
110 
111  /** Allocate a memory block of type T, optionally blocking.
112  *
113  * @param millisec Timeout value, or osWaitForever.
114  *
115  * @return Pointer to memory block that you can fill with mail or nullptr in case error.
116  *
117  * @note You may call this function from ISR context if the millisec parameter is set to 0.
118  */
119  T *alloc_for(uint32_t millisec)
120  {
121  return _pool.alloc_for(millisec);
122  }
123 
124  /** Allocate a memory block of type T, blocking.
125  *
126  * @param millisec Absolute timeout time, referenced to Kernel::get_ms_count().
127  *
128  * @return Pointer to memory block that you can fill with mail or nullptr in case error.
129  *
130  * @note You cannot call this function from ISR context.
131  * @note the underlying RTOS may have a limit to the maximum wait time
132  * due to internal 32-bit computations, but this is guaranteed to work if the
133  * wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded,
134  * the wait will time out earlier than specified.
135  */
136  T *alloc_until(uint64_t millisec)
137  {
138  return _pool.alloc_until(millisec);
139  }
140 
141  /** Allocate a memory block of type T, and set memory block to zero.
142  *
143  * @param millisec Not used (see note).
144  *
145  * @return Pointer to memory block that you can fill with mail or nullptr in case error.
146  *
147  * @note You may call this function from ISR context if the millisec parameter is set to 0.
148  * @note If blocking is required, use Mail::calloc_for or Mail::calloc_until
149  */
150  T *calloc(MBED_UNUSED uint32_t millisec = 0)
151  {
152  return _pool.calloc();
153  }
154 
155  /** Allocate a memory block of type T, optionally blocking, and set memory block to zero.
156  *
157  * @param millisec Timeout value, or osWaitForever.
158  *
159  * @return Pointer to memory block that you can fill with mail or nullptr in case error.
160  *
161  * @note You may call this function from ISR context if the millisec parameter is set to 0.
162  */
163  T *calloc_for(uint32_t millisec)
164  {
165  return _pool.calloc_for(millisec);
166  }
167 
168  /** Allocate a memory block of type T, blocking, and set memory block to zero.
169  *
170  * @param millisec Absolute timeout time, referenced to Kernel::get_ms_count().
171  *
172  * @return Pointer to memory block that you can fill with mail or nullptr in case error.
173  *
174  * @note You cannot call this function from ISR context.
175  * @note the underlying RTOS may have a limit to the maximum wait time
176  * due to internal 32-bit computations, but this is guaranteed to work if the
177  * wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded,
178  * the wait will time out earlier than specified.
179  */
180  T *calloc_until(uint64_t millisec)
181  {
182  return _pool.calloc_until(millisec);
183  }
184 
185  /** Put a mail in the queue.
186  *
187  * @param mptr Memory block previously allocated with Mail::alloc or Mail::calloc.
188  *
189  * @return Status code that indicates the execution status of the function (osOK on success).
190  *
191  * @note You may call this function from ISR context.
192  */
193  osStatus put(T *mptr)
194  {
195  return _queue.put(mptr);
196  }
197 
198  /** Get a mail from the queue.
199  *
200  * @param millisec Timeout value (default: osWaitForever).
201  *
202  * @return Event that contains mail information or error code.
203  * @retval osEventMessage Message received.
204  * @retval osOK No mail is available (and no timeout was specified).
205  * @retval osEventTimeout No mail has arrived during the given timeout period.
206  * @retval osErrorParameter A parameter is invalid or outside of a permitted range.
207  *
208  * @note You may call this function from ISR context if the millisec parameter is set to 0.
209  */
210  osEvent get(uint32_t millisec = osWaitForever)
211  {
212  osEvent evt = _queue.get(millisec);
213  if (evt.status == osEventMessage) {
214  evt.status = osEventMail;
215  }
216  return evt;
217  }
218 
219  /** Free a memory block from a mail.
220  *
221  * @param mptr Pointer to the memory block that was obtained with Mail::get.
222  *
223  * @return Status code that indicates the execution status of the function (osOK on success).
224  *
225  * @note You may call this function from ISR context.
226  */
227  osStatus free(T *mptr)
228  {
229  return _pool.free(mptr);
230  }
231 
232 private:
233  Queue<T, queue_sz> _queue;
235 };
236 
237 /** @}*/
238 /** @}*/
239 
240 }
241 
242 #endif
243 
244 #endif
245 
osStatus put(T *mptr)
Put a mail in the queue.
Definition: Mail.h:193
T * calloc_until(uint64_t millisec)
Allocate a memory block from a memory pool, blocking, and set memory block to zero.
Definition: MemoryPool.h:167
bool empty() const
Check if the mail queue is empty.
Definition: Mail.h:79
The Queue class represents a collection of objects that are stored first by order of priority...
Definition: Queue.h:62
T * alloc_until(uint64_t millisec)
Allocate a memory block from a memory pool, blocking.
Definition: MemoryPool.h:114
T * calloc_for(uint32_t millisec)
Allocate a memory block of type T, optionally blocking, and set memory block to zero.
Definition: Mail.h:163
T * alloc_for(uint32_t millisec)
Allocate a memory block from a memory pool, optionally blocking.
Definition: MemoryPool.h:99
T * calloc_until(uint64_t millisec)
Allocate a memory block of type T, blocking, and set memory block to zero.
Definition: Mail.h:180
T * alloc(MBED_UNUSED uint32_t millisec=0)
Allocate a memory block of type T, without blocking.
Definition: Mail.h:106
Prevents generation of copy constructor and copy assignment operator in derived classes.
Definition: NonCopyable.h:169
Mail()
Create and initialize Mail queue.
Definition: Mail.h:69
T * alloc_until(uint64_t millisec)
Allocate a memory block of type T, blocking.
Definition: Mail.h:136
osStatus status
status code: event or error information
Definition: cmsis_os.h:375
Event structure contains detailed information about an event.
Definition: cmsis_os.h:374
The Mail class allows you to control, send, receive or wait for mail.
Definition: Mail.h:63
osStatus free(T *block)
Free a memory block.
Definition: MemoryPool.h:184
T * calloc(void)
Allocate a memory block from a memory pool, without blocking, and set memory block to zero...
Definition: MemoryPool.h:133
osStatus free(T *mptr)
Free a memory block from a mail.
Definition: Mail.h:227
T * calloc(MBED_UNUSED uint32_t millisec=0)
Allocate a memory block of type T, and set memory block to zero.
Definition: Mail.h:150
T * alloc_for(uint32_t millisec)
Allocate a memory block of type T, optionally blocking.
Definition: Mail.h:119
bool full() const
Check if the mail queue is full.
Definition: Mail.h:92
Definition: TaskBase.h:25
T * calloc_for(uint32_t millisec)
Allocate a memory block from a memory pool, optionally blocking, and set memory block to zero...
Definition: MemoryPool.h:148
#define MBED_UNUSED
MBED_UNUSED Declare a function argument to be unused, suppressing compiler warnings.
T * alloc(void)
Allocate a memory block from a memory pool, without blocking.
Definition: MemoryPool.h:88
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.