2013-02-18 15:32:11 +00:00
|
|
|
/* mbed Microcontroller Library
|
2019-07-11 15:23:39 +00:00
|
|
|
* Copyright (c) 2006-2019 ARM Limited
|
2020-02-20 08:45:04 +00:00
|
|
|
* SPDX-License-Identifier: MIT
|
|
|
|
*
|
2013-02-18 15:32:11 +00:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
|
|
|
#ifndef QUEUE_H
|
|
|
|
#define QUEUE_H
|
|
|
|
|
2019-03-14 11:36:02 +00:00
|
|
|
#include "rtos/mbed_rtos_types.h"
|
|
|
|
#include "rtos/mbed_rtos1_types.h"
|
|
|
|
#include "rtos/mbed_rtos_storage.h"
|
2016-10-01 07:11:36 +00:00
|
|
|
#include "platform/mbed_error.h"
|
2017-06-20 12:00:20 +00:00
|
|
|
#include "platform/NonCopyable.h"
|
2013-02-18 15:32:11 +00:00
|
|
|
|
2019-03-14 11:36:02 +00:00
|
|
|
#if MBED_CONF_RTOS_PRESENT || defined(DOXYGEN_ONLY)
|
|
|
|
|
2013-02-18 15:32:11 +00:00
|
|
|
namespace rtos {
|
2019-07-11 15:23:39 +00:00
|
|
|
/** \addtogroup rtos-public-api */
|
2016-10-04 20:02:44 +00:00
|
|
|
/** @{*/
|
2019-09-06 21:58:57 +00:00
|
|
|
|
2017-10-24 15:05:45 +00:00
|
|
|
/**
|
2018-07-31 22:14:31 +00:00
|
|
|
* \defgroup rtos_Queue Queue class
|
2017-10-24 15:05:45 +00:00
|
|
|
* @{
|
|
|
|
*/
|
2018-10-09 19:59:02 +00:00
|
|
|
|
2018-10-24 14:18:02 +00:00
|
|
|
/** The Queue class represents a collection of objects that are stored first by
|
2018-11-01 20:45:55 +00:00
|
|
|
* order of priority, and then in first-in, first-out (FIFO) order.
|
2018-10-24 14:18:02 +00:00
|
|
|
*
|
2018-10-25 14:22:39 +00:00
|
|
|
* You can use a queue when you need to store data and then access it in the same
|
|
|
|
* order that it has been stored. The order in which you retrieve the data is in
|
|
|
|
* order of descending priority. If multiple elements have the same priority,
|
2018-10-24 14:18:02 +00:00
|
|
|
* they are retrieved in FIFO order.
|
|
|
|
*
|
2018-10-25 14:22:39 +00:00
|
|
|
* The object type stored in the queue can be an integer, pointer or a generic
|
2018-10-24 14:18:02 +00:00
|
|
|
* type given by the template parameter T.
|
|
|
|
*
|
2018-10-25 14:22:39 +00:00
|
|
|
* @tparam T Specifies the type of elements stored in the queue.
|
|
|
|
* @tparam queue_sz Maximum number of messages that you can store in the queue.
|
2018-10-24 14:18:02 +00:00
|
|
|
*
|
2018-10-25 14:22:39 +00:00
|
|
|
* @note Memory considerations: The queue control structures are created on the
|
|
|
|
* current thread's stack, both for the Mbed OS and underlying RTOS
|
2018-10-24 14:18:02 +00:00
|
|
|
* objects (static or dynamic RTOS memory pools are not being used).
|
|
|
|
*
|
|
|
|
*/
|
2013-02-18 15:32:11 +00:00
|
|
|
template<typename T, uint32_t queue_sz>
|
2017-06-20 12:00:20 +00:00
|
|
|
class Queue : private mbed::NonCopyable<Queue<T, queue_sz> > {
|
2013-02-18 15:32:11 +00:00
|
|
|
public:
|
2018-10-24 14:18:02 +00:00
|
|
|
/** Create and initialize a message Queue of objects of the parameterized
|
2018-10-26 08:37:40 +00:00
|
|
|
* type `T` and maximum capacity specified by `queue_sz`.
|
2017-12-27 14:46:00 +00:00
|
|
|
*
|
2018-01-08 23:48:11 +00:00
|
|
|
* @note You cannot call this function from ISR context.
|
2017-12-27 14:46:00 +00:00
|
|
|
*/
|
2018-10-09 19:59:02 +00:00
|
|
|
Queue()
|
|
|
|
{
|
2017-11-03 13:15:57 +00:00
|
|
|
osMessageQueueAttr_t attr = { 0 };
|
|
|
|
attr.mq_mem = _queue_mem;
|
|
|
|
attr.mq_size = sizeof(_queue_mem);
|
|
|
|
attr.cb_mem = &_obj_mem;
|
|
|
|
attr.cb_size = sizeof(_obj_mem);
|
2018-10-09 19:59:02 +00:00
|
|
|
_id = osMessageQueueNew(queue_sz, sizeof(T *), &attr);
|
2017-05-15 14:55:45 +00:00
|
|
|
MBED_ASSERT(_id);
|
2013-02-18 15:32:11 +00:00
|
|
|
}
|
2018-10-24 14:18:02 +00:00
|
|
|
|
2017-12-27 14:46:00 +00:00
|
|
|
/** Queue destructor
|
|
|
|
*
|
2018-01-08 23:48:11 +00:00
|
|
|
* @note You cannot call this function from ISR context.
|
2017-12-27 14:46:00 +00:00
|
|
|
*/
|
2018-10-09 19:59:02 +00:00
|
|
|
~Queue()
|
|
|
|
{
|
2017-06-21 10:21:34 +00:00
|
|
|
osMessageQueueDelete(_id);
|
|
|
|
}
|
|
|
|
|
2018-10-25 14:22:39 +00:00
|
|
|
/** Check if the queue is empty.
|
2017-09-21 10:09:45 +00:00
|
|
|
*
|
|
|
|
* @return True if the queue is empty, false if not
|
2017-12-27 14:46:00 +00:00
|
|
|
*
|
2018-01-08 23:48:11 +00:00
|
|
|
* @note You may call this function from ISR context.
|
2017-09-21 10:09:45 +00:00
|
|
|
*/
|
2018-10-09 19:59:02 +00:00
|
|
|
bool empty() const
|
|
|
|
{
|
2017-09-21 10:09:45 +00:00
|
|
|
return osMessageQueueGetCount(_id) == 0;
|
|
|
|
}
|
|
|
|
|
2018-10-25 14:22:39 +00:00
|
|
|
/** Check if the queue is full.
|
2017-09-21 10:09:45 +00:00
|
|
|
*
|
|
|
|
* @return True if the queue is full, false if not
|
2017-12-27 14:46:00 +00:00
|
|
|
*
|
2018-01-08 23:48:11 +00:00
|
|
|
* @note You may call this function from ISR context.
|
2017-09-21 10:09:45 +00:00
|
|
|
*/
|
2018-10-09 19:59:02 +00:00
|
|
|
bool full() const
|
|
|
|
{
|
2017-09-21 10:09:45 +00:00
|
|
|
return osMessageQueueGetSpace(_id) == 0;
|
|
|
|
}
|
|
|
|
|
2019-08-01 09:17:27 +00:00
|
|
|
/** Get number of queued messages in the queue.
|
|
|
|
*
|
|
|
|
* @return Number of items in the queue
|
|
|
|
*
|
|
|
|
* @note You may call this function from ISR context.
|
|
|
|
*/
|
|
|
|
uint32_t count() const
|
|
|
|
{
|
|
|
|
return osMessageQueueGetCount(_id);
|
|
|
|
}
|
|
|
|
|
2018-10-24 14:18:02 +00:00
|
|
|
/** Inserts the given element to the end of the queue.
|
|
|
|
*
|
|
|
|
* This function puts the message pointed to by `data` into the queue. The
|
|
|
|
* parameter `prio` is used to sort the message according to their priority
|
|
|
|
* (higher numbers indicate higher priority) on insertion.
|
|
|
|
*
|
|
|
|
* The timeout indicated by the parameter `millisec` specifies how long the
|
2018-10-25 14:22:39 +00:00
|
|
|
* function blocks waiting for the message to be inserted into the
|
2018-10-24 14:18:02 +00:00
|
|
|
* queue.
|
|
|
|
*
|
|
|
|
* The parameter `millisec` can have the following values:
|
|
|
|
* - When the timeout is 0 (the default), the function returns instantly.
|
2018-10-25 14:22:39 +00:00
|
|
|
* - When the timeout is osWaitForever, the function waits for an
|
2018-10-24 14:18:02 +00:00
|
|
|
* infinite time.
|
2018-10-25 14:22:39 +00:00
|
|
|
* - For all other values, the function waits for the given number of
|
2018-10-24 14:18:02 +00:00
|
|
|
* milliseconds.
|
|
|
|
*
|
|
|
|
* @param data Pointer to the element to insert into the queue.
|
|
|
|
* @param millisec Timeout for the operation to be executed, or 0 in case
|
2018-10-25 14:22:39 +00:00
|
|
|
* of no timeout. (default: 0)
|
2018-10-24 14:18:02 +00:00
|
|
|
* @param prio Priority of the operation or 0 in case of default.
|
|
|
|
* (default: 0)
|
|
|
|
*
|
|
|
|
* @return Status code that indicates the execution status of the function:
|
|
|
|
* @a osOK The message has been successfully inserted
|
|
|
|
* into the queue.
|
|
|
|
* @a osErrorTimeout The message could not be inserted into the
|
|
|
|
* queue in the given time.
|
|
|
|
* @a osErrorResource The message could not be inserted because
|
|
|
|
* the queue is full.
|
2018-10-25 14:22:39 +00:00
|
|
|
* @a osErrorParameter Internal error or nonzero timeout specified
|
2018-10-24 14:18:02 +00:00
|
|
|
* in an ISR.
|
|
|
|
*
|
|
|
|
* @note You may call this function from ISR context if the millisec
|
|
|
|
* parameter is set to 0.
|
|
|
|
*
|
|
|
|
*/
|
2018-11-12 10:33:38 +00:00
|
|
|
osStatus put(T *data, uint32_t millisec = 0, uint8_t prio = 0)
|
|
|
|
{
|
2017-05-15 14:55:45 +00:00
|
|
|
return osMessageQueuePut(_id, &data, prio, millisec);
|
2013-02-18 15:32:11 +00:00
|
|
|
}
|
2014-05-29 13:36:51 +00:00
|
|
|
|
2018-10-24 14:18:02 +00:00
|
|
|
/** Get a message or wait for a message from the queue.
|
|
|
|
*
|
|
|
|
* This function retrieves a message from the queue. The message is stored
|
|
|
|
* in the value field of the returned `osEvent` object.
|
|
|
|
*
|
|
|
|
* The timeout specified by the parameter `millisec` specifies how long the
|
2018-10-25 14:22:39 +00:00
|
|
|
* function waits to retrieve the message from the queue.
|
2018-10-24 14:18:02 +00:00
|
|
|
*
|
|
|
|
* The timeout parameter can have the following values:
|
|
|
|
* - When the timeout is 0, the function returns instantly.
|
2018-10-25 14:22:39 +00:00
|
|
|
* - When the timeout is osWaitForever (default), the function waits
|
2018-10-24 14:18:02 +00:00
|
|
|
* infinite time until the message is retrieved.
|
2018-10-25 14:22:39 +00:00
|
|
|
* - When the timeout is any other value, the function waits for the
|
2018-10-24 14:18:02 +00:00
|
|
|
* specified time before returning a timeout error.
|
|
|
|
*
|
|
|
|
* Messages are retrieved in descending priority order. If two messages
|
2018-10-25 14:22:39 +00:00
|
|
|
* share the same priority level, they are retrieved in first-in, first-out
|
2018-10-24 14:18:02 +00:00
|
|
|
* (FIFO) order.
|
|
|
|
*
|
2019-02-20 23:52:28 +00:00
|
|
|
* @param millisec Timeout value.
|
2018-10-24 14:18:02 +00:00
|
|
|
* (default: osWaitForever).
|
|
|
|
*
|
|
|
|
* @return Event information that includes the message in event. Message
|
|
|
|
* value and the status code in event.status:
|
|
|
|
* @a osEventMessage Message successfully received.
|
2018-10-25 14:22:39 +00:00
|
|
|
* @a osOK No message is available in the queue, and no
|
2018-10-24 14:18:02 +00:00
|
|
|
* timeout was specified.
|
|
|
|
* @a osEventTimeout No message was received before a timeout
|
|
|
|
* event occurred.
|
|
|
|
* @a osErrorParameter A parameter is invalid or outside of a
|
|
|
|
* permitted range.
|
|
|
|
*
|
|
|
|
* @note You may call this function from ISR context if the millisec
|
|
|
|
* parameter is set to 0.
|
|
|
|
*/
|
2018-11-12 10:33:38 +00:00
|
|
|
osEvent get(uint32_t millisec = osWaitForever)
|
|
|
|
{
|
2017-05-15 14:55:45 +00:00
|
|
|
osEvent event;
|
2019-07-04 12:37:47 +00:00
|
|
|
T *data = nullptr;
|
|
|
|
osStatus_t res = osMessageQueueGet(_id, &data, nullptr, millisec);
|
2017-05-15 14:55:45 +00:00
|
|
|
|
|
|
|
switch (res) {
|
|
|
|
case osOK:
|
|
|
|
event.status = (osStatus)osEventMessage;
|
|
|
|
event.value.p = data;
|
|
|
|
break;
|
|
|
|
case osErrorResource:
|
|
|
|
event.status = osOK;
|
|
|
|
break;
|
|
|
|
case osErrorTimeout:
|
|
|
|
event.status = (osStatus)osEventTimeout;
|
|
|
|
break;
|
|
|
|
case osErrorParameter:
|
|
|
|
default:
|
|
|
|
event.status = osErrorParameter;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
event.def.message_id = _id;
|
|
|
|
|
|
|
|
return event;
|
2013-02-18 15:32:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-05-15 14:55:45 +00:00
|
|
|
osMessageQueueId_t _id;
|
2018-10-09 19:59:02 +00:00
|
|
|
char _queue_mem[queue_sz * (sizeof(T *) + sizeof(mbed_rtos_storage_message_t))];
|
2017-05-15 14:55:45 +00:00
|
|
|
mbed_rtos_storage_msg_queue_t _obj_mem;
|
2013-02-18 15:32:11 +00:00
|
|
|
};
|
2017-10-24 15:05:45 +00:00
|
|
|
/** @}*/
|
|
|
|
/** @}*/
|
2013-02-18 15:32:11 +00:00
|
|
|
|
2018-10-24 14:18:02 +00:00
|
|
|
} // namespace rtos
|
2016-10-04 20:02:44 +00:00
|
|
|
|
2019-03-14 11:36:02 +00:00
|
|
|
#endif
|
|
|
|
|
2018-10-24 14:18:02 +00:00
|
|
|
#endif // QUEUE_H
|