Mistake on this page?
Report an issue in GitHub or email us
Mail.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2006-2017 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 "Queue.h"
29 #include "MemoryPool.h"
30 #include "cmsis_os2.h"
31 #include "mbed_rtos_storage.h"
32 #include "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 namespace rtos {
42 /** \addtogroup rtos */
43 /** @{*/
44 /**
45  * \defgroup rtos_Mail Mail class
46  * @{
47  */
48 
49 /** The Mail class allows you to control, send, receive or wait for mail.
50  * A mail is a memory block that is sent to a thread or interrupt service routine (ISR).
51  * @tparam T Data type of a single mail message element.
52  * @tparam queue_sz Maximum number of mail messages in queue.
53  *
54  * @note
55  * Memory considerations: The mail data store and control structures are part of this class - they do not (themselves)
56  * allocate memory on the heap, both for the Mbed OS and underlying RTOS objects (static or dynamic RTOS memory
57  * pools are not being used).
58  */
59 template<typename T, uint32_t queue_sz>
60 class Mail : private mbed::NonCopyable<Mail<T, queue_sz> > {
61 public:
62  /** Create and initialize Mail queue.
63  *
64  * @note You cannot call this function from ISR context.
65  */
66  Mail() { };
67 
68  /** Check if the mail queue is empty.
69  *
70  * @return State of queue.
71  * @retval true Mail queue is empty.
72  * @retval false Mail queue contains mail.
73  *
74  * @note You may call this function from ISR context.
75  */
76  bool empty() const
77  {
78  return _queue.empty();
79  }
80 
81  /** Check if the mail queue is full.
82  *
83  * @return State of queue.
84  * @retval true Mail queue is full.
85  * @retval false Mail queue is not full.
86  *
87  * @note You may call this function from ISR context.
88  */
89  bool full() const
90  {
91  return _queue.full();
92  }
93 
94  /** Allocate a memory block of type T, without blocking.
95  *
96  * @param millisec Not used (see note).
97  *
98  * @return Pointer to memory block that you can fill with mail or NULL in case error.
99  *
100  * @note You may call this function from ISR context.
101  * @note If blocking is required, use Mail::alloc_for or Mail::alloc_until
102  */
103  T *alloc(MBED_UNUSED uint32_t millisec = 0)
104  {
105  return _pool.alloc();
106  }
107 
108  /** Allocate a memory block of type T, optionally blocking.
109  *
110  * @param millisec Timeout value, or osWaitForever.
111  *
112  * @return Pointer to memory block that you can fill with mail or NULL in case error.
113  *
114  * @note You may call this function from ISR context if the millisec parameter is set to 0.
115  */
116  T *alloc_for(uint32_t millisec)
117  {
118  return _pool.alloc_for(millisec);
119  }
120 
121  /** Allocate a memory block of type T, blocking.
122  *
123  * @param millisec Absolute timeout time, referenced to Kernel::get_ms_count().
124  *
125  * @return Pointer to memory block that you can fill with mail or NULL in case error.
126  *
127  * @note You cannot call this function from ISR context.
128  * @note the underlying RTOS may have a limit to the maximum wait time
129  * due to internal 32-bit computations, but this is guaranteed to work if the
130  * wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded,
131  * the wait will time out earlier than specified.
132  */
133  T *alloc_until(uint64_t millisec)
134  {
135  return _pool.alloc_until(millisec);
136  }
137 
138  /** Allocate a memory block of type T, and set memory block to zero.
139  *
140  * @param millisec Not used (see note).
141  *
142  * @return Pointer to memory block that you can fill with mail or NULL in case error.
143  *
144  * @note You may call this function from ISR context if the millisec parameter is set to 0.
145  * @note If blocking is required, use Mail::calloc_for or Mail::calloc_until
146  */
147  T *calloc(MBED_UNUSED uint32_t millisec = 0)
148  {
149  return _pool.calloc();
150  }
151 
152  /** Allocate a memory block of type T, optionally blocking, and set memory block to zero.
153  *
154  * @param millisec Timeout value, or osWaitForever.
155  *
156  * @return Pointer to memory block that you can fill with mail or NULL in case error.
157  *
158  * @note You may call this function from ISR context if the millisec parameter is set to 0.
159  */
160  T *calloc_for(uint32_t millisec)
161  {
162  return _pool.calloc_for(millisec);
163  }
164 
165  /** Allocate a memory block of type T, blocking, and set memory block to zero.
166  *
167  * @param millisec Absolute timeout time, referenced to Kernel::get_ms_count().
168  *
169  * @return Pointer to memory block that you can fill with mail or NULL in case error.
170  *
171  * @note You cannot call this function from ISR context.
172  * @note the underlying RTOS may have a limit to the maximum wait time
173  * due to internal 32-bit computations, but this is guaranteed to work if the
174  * wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded,
175  * the wait will time out earlier than specified.
176  */
177  T *calloc_until(uint64_t millisec)
178  {
179  return _pool.calloc_until(millisec);
180  }
181 
182  /** Put a mail in the queue.
183  *
184  * @param mptr Memory block previously allocated with Mail::alloc or Mail::calloc.
185  *
186  * @return Status code that indicates the execution status of the function (osOK on success).
187  *
188  * @note You may call this function from ISR context.
189  */
190  osStatus put(T *mptr)
191  {
192  return _queue.put(mptr);
193  }
194 
195  /** Get a mail from the queue.
196  *
197  * @param millisec Timeout value (default: osWaitForever).
198  *
199  * @return Event that contains mail information or error code.
200  * @retval osEventMessage Message received.
201  * @retval osOK No mail is available (and no timeout was specified).
202  * @retval osEventTimeout No mail has arrived during the given timeout period.
203  * @retval osErrorParameter A parameter is invalid or outside of a permitted range.
204  *
205  * @note You may call this function from ISR context if the millisec parameter is set to 0.
206  */
207  osEvent get(uint32_t millisec = osWaitForever)
208  {
209  osEvent evt = _queue.get(millisec);
210  if (evt.status == osEventMessage) {
211  evt.status = osEventMail;
212  }
213  return evt;
214  }
215 
216  /** Free a memory block from a mail.
217  *
218  * @param mptr Pointer to the memory block that was obtained with Mail::get.
219  *
220  * @return Status code that indicates the execution status of the function (osOK on success).
221  *
222  * @note You may call this function from ISR context.
223  */
224  osStatus free(T *mptr)
225  {
226  return _pool.free(mptr);
227  }
228 
229 private:
230  Queue<T, queue_sz> _queue;
232 };
233 
234 /** @}*/
235 /** @}*/
236 
237 }
238 
239 #endif
240 
241 
242 
osStatus put(T *mptr)
Put a mail in the queue.
Definition: Mail.h:190
T * calloc_until(uint64_t millisec)
Allocate a memory block from a memory pool, blocking, and set memory block to zero.
Definition: MemoryPool.h:162
bool empty() const
Check if the mail queue is empty.
Definition: Mail.h:76
The Queue class represents a collection of objects that are stored first by order of priority...
Definition: Queue.h:59
T * alloc_until(uint64_t millisec)
Allocate a memory block from a memory pool, blocking.
Definition: MemoryPool.h:109
T * calloc_for(uint32_t millisec)
Allocate a memory block of type T, optionally blocking, and set memory block to zero.
Definition: Mail.h:160
T * alloc_for(uint32_t millisec)
Allocate a memory block from a memory pool, optionally blocking.
Definition: MemoryPool.h:94
T * calloc_until(uint64_t millisec)
Allocate a memory block of type T, blocking, and set memory block to zero.
Definition: Mail.h:177
T * alloc(MBED_UNUSED uint32_t millisec=0)
Allocate a memory block of type T, without blocking.
Definition: Mail.h:103
Prevents generation of copy constructor and copy assignment operator in derived classes.
Definition: NonCopyable.h:168
Mail()
Create and initialize Mail queue.
Definition: Mail.h:66
T * alloc_until(uint64_t millisec)
Allocate a memory block of type T, blocking.
Definition: Mail.h:133
The Mail class allows you to control, send, receive or wait for mail.
Definition: Mail.h:60
osStatus free(T *block)
Free a memory block.
Definition: MemoryPool.h:179
T * calloc(void)
Allocate a memory block from a memory pool, without blocking, and set memory block to zero...
Definition: MemoryPool.h:128
osStatus free(T *mptr)
Free a memory block from a mail.
Definition: Mail.h:224
T * calloc(MBED_UNUSED uint32_t millisec=0)
Allocate a memory block of type T, and set memory block to zero.
Definition: Mail.h:147
T * alloc_for(uint32_t millisec)
Allocate a memory block of type T, optionally blocking.
Definition: Mail.h:116
bool full() const
Check if the mail queue is full.
Definition: Mail.h:89
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:143
#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:83
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.