Rtos API example

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Mail.h Source File

Mail.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2017 ARM Limited
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to deal
00006  * in the Software without restriction, including without limitation the rights
00007  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008  * copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00020  * SOFTWARE.
00021  */
00022 #ifndef MAIL_H
00023 #define MAIL_H
00024 
00025 #include <stdint.h>
00026 #include <string.h>
00027 
00028 #include "Queue.h"
00029 #include "MemoryPool.h"
00030 #include "cmsis_os2.h"
00031 #include "mbed_rtos_storage.h"
00032 #include "mbed_rtos1_types.h"
00033 
00034 #include "platform/NonCopyable.h"
00035 
00036 using namespace rtos;
00037 
00038 namespace rtos {
00039 /** \addtogroup rtos */
00040 /** @{*/
00041 /**
00042  * \defgroup rtos_Mail Mail class
00043  * @{
00044  */
00045  
00046 /** The Mail class allow to control, send, receive, or wait for mail.
00047  A mail is a memory block that is send to a thread or interrupt service routine.
00048   @tparam  T         data type of a single message element.
00049   @tparam  queue_sz  maximum number of messages in queue.
00050 
00051  @note
00052  Memory considerations: The mail data store and control structures will be created on current thread's stack,
00053  both for the mbed OS and underlying RTOS objects (static or dynamic RTOS memory pools are not being used).
00054 */
00055 template<typename T, uint32_t queue_sz>
00056 class Mail : private mbed::NonCopyable<Mail<T, queue_sz> > {
00057 public:
00058     /** Create and Initialise Mail queue. */
00059     Mail() { };
00060 
00061     /** Check if the mail queue is empty
00062      *
00063      * @return True if the mail queue is empty, false if not
00064      */
00065     bool empty() const {
00066         return _queue.empty();
00067     }
00068 
00069     /** Check if the mail queue is full
00070      *
00071      * @return True if the mail queue is full, false if not
00072      */
00073     bool full() const {
00074         return _queue.full();
00075     }
00076 
00077     /** Allocate a memory block of type T
00078       @param   millisec  timeout value or 0 in case of no time-out. (default: 0).
00079       @return  pointer to memory block that can be filled with mail or NULL in case error.
00080     */
00081     T* alloc(uint32_t millisec=0) {
00082         return _pool.alloc();
00083     }
00084 
00085     /** Allocate a memory block of type T and set memory block to zero.
00086       @param   millisec  timeout value or 0 in case of no time-out.  (default: 0).
00087       @return  pointer to memory block that can be filled with mail or NULL in case error.
00088     */
00089     T* calloc(uint32_t millisec=0) {
00090         return _pool.calloc();
00091     }
00092 
00093     /** Put a mail in the queue.
00094       @param   mptr  memory block previously allocated with Mail::alloc or Mail::calloc.
00095       @return  status code that indicates the execution status of the function.
00096     */
00097     osStatus put(T *mptr) {
00098         return _queue.put(mptr);
00099     }
00100 
00101     /** Get a mail from a queue.
00102       @param   millisec  timeout value or 0 in case of no time-out. (default: osWaitForever).
00103       @return  event that contains mail information or error code.
00104     */
00105     osEvent get(uint32_t millisec=osWaitForever) {
00106         osEvent evt = _queue.get(millisec);
00107         if (evt.status == osEventMessage) {
00108             evt.status = osEventMail;
00109         }
00110         return evt;
00111     }
00112 
00113     /** Free a memory block from a mail.
00114       @param   mptr  pointer to the memory block that was obtained with Mail::get.
00115       @return  status code that indicates the execution status of the function.
00116     */
00117     osStatus free(T *mptr) {
00118         return _pool.free(mptr);
00119     }
00120 
00121 private:
00122     Queue<T, queue_sz> _queue;
00123     MemoryPool<T, queue_sz>  _pool;
00124 };
00125 
00126 /** @}*/
00127 /** @}*/
00128 
00129 }
00130 
00131 #endif
00132 
00133 
00134