2015-10-03 01:43:18 +02:00
|
|
|
/****************************************************************************
|
2021-03-08 22:39:04 +01:00
|
|
|
* sched/mqueue/mq_receive.c
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2020-03-10 16:26:30 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2020-03-10 16:26:30 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2020-03-10 16:26:30 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2015-10-03 01:43:18 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2015-10-03 01:43:18 +02:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Included Files
|
2015-10-03 01:43:18 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2007-03-29 15:25:18 +02:00
|
|
|
#include <nuttx/config.h>
|
2009-12-14 22:15:18 +01:00
|
|
|
|
2007-03-29 15:25:18 +02:00
|
|
|
#include <sys/types.h>
|
2009-12-14 22:15:18 +01:00
|
|
|
#include <stdbool.h>
|
2021-05-18 08:59:14 +02:00
|
|
|
#include <assert.h>
|
2007-03-28 16:48:42 +02:00
|
|
|
#include <errno.h>
|
2007-02-18 00:21:28 +01:00
|
|
|
#include <mqueue.h>
|
|
|
|
#include <debug.h>
|
2016-02-14 15:17:46 +01:00
|
|
|
|
|
|
|
#include <nuttx/irq.h>
|
2007-02-18 00:21:28 +01:00
|
|
|
#include <nuttx/arch.h>
|
2017-10-10 17:57:40 +02:00
|
|
|
#include <nuttx/mqueue.h>
|
2016-12-10 16:08:26 +01:00
|
|
|
#include <nuttx/cancelpt.h>
|
2009-12-14 22:15:18 +01:00
|
|
|
|
2014-08-08 20:31:23 +02:00
|
|
|
#include "mqueue/mqueue.h"
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2015-10-03 01:43:18 +02:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Public Functions
|
2015-10-03 01:43:18 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2015-10-03 01:43:18 +02:00
|
|
|
/****************************************************************************
|
2020-12-24 13:53:17 +01:00
|
|
|
* Name: file_mq_receive
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2012-07-14 21:30:31 +02:00
|
|
|
* This function receives the oldest of the highest priority messages
|
2020-12-24 13:53:17 +01:00
|
|
|
* from the message queue specified by "mq." This is an internal OS
|
2017-10-10 17:57:40 +02:00
|
|
|
* interface. It is functionally equivalent to mq_receive except that:
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2019-08-23 19:57:35 +02:00
|
|
|
* - It is not a cancellation point, and
|
2017-10-10 17:57:40 +02:00
|
|
|
* - It does not modify the errno value.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2017-10-10 17:57:40 +02:00
|
|
|
* See comments with mq_receive() for a more complete description of the
|
|
|
|
* behavior of this function
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2017-10-10 17:57:40 +02:00
|
|
|
* Input Parameters:
|
2020-12-24 13:53:17 +01:00
|
|
|
* mq - Message Queue Descriptor
|
2017-10-10 17:57:40 +02:00
|
|
|
* msg - Buffer to receive the message
|
2007-02-18 00:21:28 +01:00
|
|
|
* msglen - Size of the buffer in bytes
|
2017-10-10 17:57:40 +02:00
|
|
|
* prio - If not NULL, the location to store message priority.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2017-10-10 17:57:40 +02:00
|
|
|
* Returned Value:
|
|
|
|
* This is an internal OS interface and should not be used by applications.
|
|
|
|
* It follows the NuttX internal error return policy: Zero (OK) is
|
|
|
|
* returned on success. A negated errno value is returned on failure.
|
|
|
|
* (see mq_receive() for the list list valid return values).
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2015-10-03 01:43:18 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2020-12-24 13:53:17 +01:00
|
|
|
ssize_t file_mq_receive(FAR struct file *mq, FAR char *msg, size_t msglen,
|
|
|
|
FAR unsigned int *prio)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2020-12-22 15:02:48 +01:00
|
|
|
FAR struct mqueue_inode_s *msgq;
|
2014-09-29 21:35:32 +02:00
|
|
|
FAR struct mqueue_msg_s *mqmsg;
|
2016-02-14 15:17:46 +01:00
|
|
|
irqstate_t flags;
|
2017-10-10 17:57:40 +02:00
|
|
|
ssize_t ret;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2009-12-14 22:15:18 +01:00
|
|
|
DEBUGASSERT(up_interrupt_context() == false);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2007-03-29 15:25:18 +02:00
|
|
|
/* Verify the input parameters and, in case of an error, set
|
|
|
|
* errno appropriately.
|
|
|
|
*/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2022-06-09 07:37:11 +02:00
|
|
|
ret = nxmq_verify_receive(mq, msg, msglen);
|
2017-10-10 17:57:40 +02:00
|
|
|
if (ret < 0)
|
2007-03-28 16:48:42 +02:00
|
|
|
{
|
2017-10-10 17:57:40 +02:00
|
|
|
return ret;
|
2007-03-28 16:48:42 +02:00
|
|
|
}
|
|
|
|
|
2022-06-09 07:37:11 +02:00
|
|
|
msgq = mq->f_inode->i_private;
|
|
|
|
|
2017-10-09 17:06:46 +02:00
|
|
|
/* Furthermore, nxmq_wait_receive() expects to have interrupts disabled
|
2007-03-29 15:25:18 +02:00
|
|
|
* because messages can be sent from interrupt level.
|
2007-03-28 16:48:42 +02:00
|
|
|
*/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2016-02-14 15:17:46 +01:00
|
|
|
flags = enter_critical_section();
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2007-03-29 15:25:18 +02:00
|
|
|
/* Get the message from the message queue */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2020-12-24 13:53:17 +01:00
|
|
|
ret = nxmq_wait_receive(msgq, mq->f_oflags, &mqmsg);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2007-03-29 15:25:18 +02:00
|
|
|
/* Check if we got a message from the message queue. We might
|
|
|
|
* not have a message if:
|
|
|
|
*
|
2020-12-24 13:53:17 +01:00
|
|
|
* - The message queue is empty and O_NONBLOCK is set in the mq
|
2007-03-29 15:25:18 +02:00
|
|
|
* - The wait was interrupted by a signal
|
|
|
|
*/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2022-06-08 14:39:03 +02:00
|
|
|
if (ret == OK)
|
2007-03-28 16:48:42 +02:00
|
|
|
{
|
2020-12-22 15:02:48 +01:00
|
|
|
ret = nxmq_do_receive(msgq, mqmsg, msg, prio);
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
|
2022-06-08 14:39:03 +02:00
|
|
|
leave_critical_section(flags);
|
|
|
|
|
2017-10-10 17:57:40 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-12-24 13:53:17 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxmq_receive
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function receives the oldest of the highest priority messages
|
|
|
|
* from the message queue specified by "mqdes." This is an internal OS
|
|
|
|
* interface. It is functionally equivalent to mq_receive except that:
|
|
|
|
*
|
|
|
|
* - It is not a cancellation point, and
|
|
|
|
* - It does not modify the errno value.
|
|
|
|
*
|
|
|
|
* See comments with mq_receive() for a more complete description of the
|
|
|
|
* behavior of this function
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* mqdes - Message Queue Descriptor
|
|
|
|
* msg - Buffer to receive the message
|
|
|
|
* msglen - Size of the buffer in bytes
|
|
|
|
* prio - If not NULL, the location to store message priority.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* This is an internal OS interface and should not be used by applications.
|
|
|
|
* It follows the NuttX internal error return policy: Zero (OK) is
|
|
|
|
* returned on success. A negated errno value is returned on failure.
|
|
|
|
* (see mq_receive() for the list list valid return values).
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
ssize_t nxmq_receive(mqd_t mqdes, FAR char *msg, size_t msglen,
|
|
|
|
FAR unsigned int *prio)
|
|
|
|
{
|
|
|
|
FAR struct file *filep;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = fs_getfilep(mqdes, &filep);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return file_mq_receive(filep, msg, msglen, prio);
|
|
|
|
}
|
|
|
|
|
2017-10-10 17:57:40 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: mq_receive
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function receives the oldest of the highest priority messages
|
|
|
|
* from the message queue specified by "mqdes." If the size of the
|
|
|
|
* buffer in bytes (msglen) is less than the "mq_msgsize" attribute of
|
|
|
|
* the message queue, mq_receive will return an error. Otherwise, the
|
|
|
|
* selected message is removed from the queue and copied to "msg."
|
|
|
|
*
|
|
|
|
* If the message queue is empty and O_NONBLOCK was not set,
|
|
|
|
* mq_receive() will block until a message is added to the message
|
|
|
|
* queue. If more than one task is waiting to receive a message, only
|
|
|
|
* the task with the highest priority that has waited the longest will
|
|
|
|
* be unblocked.
|
|
|
|
*
|
|
|
|
* If the queue is empty and O_NONBLOCK is set, ERROR will be returned.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* mqdes - Message Queue Descriptor
|
|
|
|
* msg - Buffer to receive the message
|
|
|
|
* msglen - Size of the buffer in bytes
|
|
|
|
* prio - If not NULL, the location to store message priority.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
2021-05-06 04:18:49 +02:00
|
|
|
* On success, the length of the selected message in bytes is returned.
|
2017-10-10 17:57:40 +02:00
|
|
|
* On failure, -1 (ERROR) is returned and the errno is set appropriately:
|
|
|
|
*
|
|
|
|
* EAGAIN The queue was empty, and the O_NONBLOCK flag was set
|
|
|
|
* for the message queue description referred to by 'mqdes'.
|
|
|
|
* EPERM Message queue opened not opened for reading.
|
|
|
|
* EMSGSIZE 'msglen' was less than the maxmsgsize attribute of the
|
|
|
|
* message queue.
|
|
|
|
* EINTR The call was interrupted by a signal handler.
|
|
|
|
* EINVAL Invalid 'msg' or 'mqdes'
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
ssize_t mq_receive(mqd_t mqdes, FAR char *msg, size_t msglen,
|
2019-02-16 02:18:55 +01:00
|
|
|
FAR unsigned int *prio)
|
2017-10-10 17:57:40 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* mq_receive() is a cancellation point */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
enter_cancellation_point();
|
2017-10-10 17:57:40 +02:00
|
|
|
|
|
|
|
/* Let nxmq_receive do all of the work */
|
|
|
|
|
|
|
|
ret = nxmq_receive(mqdes, msg, msglen, prio);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
set_errno(-ret);
|
|
|
|
ret = ERROR;
|
|
|
|
}
|
|
|
|
|
2016-12-09 23:50:34 +01:00
|
|
|
leave_cancellation_point();
|
2007-02-18 00:21:28 +01:00
|
|
|
return ret;
|
|
|
|
}
|