2015-01-30 14:09:25 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* net/udp/udp_netpoll.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2008-2009, 2011-2015 Gregory Nutt. All rights reserved.
|
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <poll.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
#include <nuttx/net/net.h>
|
|
|
|
|
|
|
|
#include <devif/devif.h>
|
|
|
|
#include "udp/udp.h"
|
|
|
|
|
|
|
|
#ifdef HAVE_UDP_POLL
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
/* This is an allocated container that holds the poll-related information */
|
|
|
|
|
|
|
|
struct udp_poll_s
|
|
|
|
{
|
|
|
|
FAR struct socket *psock; /* Needed to handle loss of connection */
|
2015-05-29 18:21:06 +02:00
|
|
|
FAR struct net_driver_s *dev; /* Needed to free the callback structure */
|
2015-01-30 14:09:25 +01:00
|
|
|
struct pollfd *fds; /* Needed to handle poll events */
|
|
|
|
FAR struct devif_callback_s *cb; /* Needed to teardown the poll */
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-08-29 22:08:04 +02:00
|
|
|
* Name: udp_poll_eventhandler
|
2015-01-30 14:09:25 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2017-09-02 18:27:03 +02:00
|
|
|
* This function is called to perform the actual UDP receive operation
|
|
|
|
* via the device interface layer.
|
2015-01-30 14:09:25 +01:00
|
|
|
*
|
|
|
|
* Parameters:
|
2017-09-02 18:27:03 +02:00
|
|
|
* dev The structure of the network driver that caused the event
|
2015-01-30 14:09:25 +01:00
|
|
|
* conn The connection structure associated with the socket
|
|
|
|
* flags Set of events describing why the callback was invoked
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
2017-09-02 18:27:03 +02:00
|
|
|
* This function must be called with the network locked.
|
2015-01-30 14:09:25 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-08-29 22:08:04 +02:00
|
|
|
static uint16_t udp_poll_eventhandler(FAR struct net_driver_s *dev,
|
|
|
|
FAR void *conn,
|
|
|
|
FAR void *pvpriv, uint16_t flags)
|
2015-01-30 14:09:25 +01:00
|
|
|
{
|
|
|
|
FAR struct udp_poll_s *info = (FAR struct udp_poll_s *)pvpriv;
|
|
|
|
|
2016-06-20 19:59:15 +02:00
|
|
|
ninfo("flags: %04x\n", flags);
|
2015-01-30 14:09:25 +01:00
|
|
|
|
|
|
|
DEBUGASSERT(!info || (info->psock && info->fds));
|
|
|
|
|
|
|
|
/* 'priv' might be null in some race conditions (?) */
|
|
|
|
|
|
|
|
if (info)
|
|
|
|
{
|
|
|
|
pollevent_t eventset = 0;
|
|
|
|
|
|
|
|
/* Check for data or connection availability events. */
|
|
|
|
|
2015-05-29 18:21:06 +02:00
|
|
|
if ((flags & UDP_NEWDATA) != 0)
|
2015-01-30 14:09:25 +01:00
|
|
|
{
|
|
|
|
eventset |= (POLLIN & info->fds->events);
|
|
|
|
}
|
|
|
|
|
2015-05-29 18:21:06 +02:00
|
|
|
/* poll is a sign that we are free to send data. */
|
2015-01-30 14:09:25 +01:00
|
|
|
|
|
|
|
if ((flags & UDP_POLL) != 0)
|
|
|
|
{
|
|
|
|
eventset |= (POLLOUT & info->fds->events);
|
|
|
|
}
|
|
|
|
|
2015-05-29 18:21:06 +02:00
|
|
|
/* Check for loss of connection events. */
|
|
|
|
|
|
|
|
if ((flags & NETDEV_DOWN) != 0)
|
|
|
|
{
|
2015-06-02 23:04:02 +02:00
|
|
|
eventset |= ((POLLHUP | POLLERR) & info->fds->events);
|
2015-05-29 18:21:06 +02:00
|
|
|
}
|
|
|
|
|
2015-01-30 14:09:25 +01:00
|
|
|
/* Awaken the caller of poll() is requested event occurred. */
|
|
|
|
|
|
|
|
if (eventset)
|
|
|
|
{
|
|
|
|
info->fds->revents |= eventset;
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(info->fds->sem);
|
2015-01-30 14:09:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: udp_pollsetup
|
2015-01-30 14:09:25 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Setup to monitor events on one UDP/IP socket
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* psock - The UDP/IP socket of interest
|
|
|
|
* fds - The structure describing the events to be monitored, OR NULL if
|
|
|
|
* this is a request to stop monitoring events.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0: Success; Negated errno on failure
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int udp_pollsetup(FAR struct socket *psock, FAR struct pollfd *fds)
|
|
|
|
{
|
|
|
|
FAR struct udp_conn_s *conn = psock->s_conn;
|
|
|
|
FAR struct udp_poll_s *info;
|
|
|
|
FAR struct devif_callback_s *cb;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Sanity check */
|
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2015-01-30 14:09:25 +01:00
|
|
|
if (!conn || !fds)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Allocate a container to hold the poll information */
|
|
|
|
|
|
|
|
info = (FAR struct udp_poll_s *)kmm_malloc(sizeof(struct udp_poll_s));
|
|
|
|
if (!info)
|
|
|
|
{
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Some of the following must be atomic */
|
|
|
|
|
2016-12-03 23:28:19 +01:00
|
|
|
net_lock();
|
2015-01-30 14:09:25 +01:00
|
|
|
|
2015-05-29 18:21:06 +02:00
|
|
|
/* Get the device that will provide the provide the NETDEV_DOWN event.
|
|
|
|
* NOTE: in the event that the local socket is bound to INADDR_ANY, the
|
|
|
|
* dev value will be zero and there will be no NETDEV_DOWN notifications.
|
|
|
|
*/
|
|
|
|
|
|
|
|
info->dev = udp_find_laddr_device(conn);
|
|
|
|
|
2015-01-30 14:09:25 +01:00
|
|
|
/* Setup the UDP remote connection */
|
|
|
|
|
|
|
|
ret = udp_connect(conn, NULL);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
goto errout_with_lock;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate a TCP/IP callback structure */
|
|
|
|
|
2015-05-29 18:21:06 +02:00
|
|
|
cb = udp_callback_alloc(info->dev, conn);
|
2015-01-30 14:09:25 +01:00
|
|
|
if (!cb)
|
|
|
|
{
|
|
|
|
ret = -EBUSY;
|
|
|
|
goto errout_with_lock;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the poll info container */
|
|
|
|
|
|
|
|
info->psock = psock;
|
|
|
|
info->fds = fds;
|
|
|
|
info->cb = cb;
|
|
|
|
|
|
|
|
/* Initialize the callback structure. Save the reference to the info
|
|
|
|
* structure as callback private data so that it will be available during
|
|
|
|
* callback processing.
|
|
|
|
*/
|
|
|
|
|
2015-05-29 18:21:06 +02:00
|
|
|
cb->flags = 0;
|
2015-01-30 14:09:25 +01:00
|
|
|
cb->priv = (FAR void *)info;
|
2017-08-29 22:08:04 +02:00
|
|
|
cb->event = udp_poll_eventhandler;
|
2015-01-30 14:09:25 +01:00
|
|
|
|
2015-05-29 18:21:06 +02:00
|
|
|
if ((info->fds->events & POLLOUT) != 0)
|
|
|
|
{
|
|
|
|
cb->flags |= UDP_POLL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((info->fds->events & POLLIN) != 0)
|
|
|
|
{
|
|
|
|
cb->flags |= UDP_NEWDATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((info->fds->events & (POLLHUP | POLLERR)) != 0)
|
|
|
|
{
|
|
|
|
cb->flags |= NETDEV_DOWN;
|
|
|
|
}
|
2015-01-30 14:09:25 +01:00
|
|
|
|
|
|
|
/* Save the reference in the poll info structure as fds private as well
|
2015-05-29 18:21:06 +02:00
|
|
|
* for use during poll teardown as well.
|
2015-01-30 14:09:25 +01:00
|
|
|
*/
|
|
|
|
|
2015-05-29 18:21:06 +02:00
|
|
|
fds->priv = (FAR void *)info;
|
2015-01-30 14:09:25 +01:00
|
|
|
|
|
|
|
/* Check for read data availability now */
|
|
|
|
|
|
|
|
if (!IOB_QEMPTY(&conn->readahead))
|
|
|
|
{
|
|
|
|
/* Normal data may be read without blocking. */
|
|
|
|
|
|
|
|
fds->revents |= (POLLRDNORM & fds->events);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if any requested events are already in effect */
|
|
|
|
|
|
|
|
if (fds->revents != 0)
|
|
|
|
{
|
|
|
|
/* Yes.. then signal the poll logic */
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(fds->sem);
|
2015-01-30 14:09:25 +01:00
|
|
|
}
|
|
|
|
|
2016-12-03 23:28:19 +01:00
|
|
|
net_unlock();
|
2015-01-30 14:09:25 +01:00
|
|
|
return OK;
|
|
|
|
|
|
|
|
errout_with_lock:
|
|
|
|
kmm_free(info);
|
2016-12-03 23:28:19 +01:00
|
|
|
net_unlock();
|
2015-01-30 14:09:25 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: udp_pollteardown
|
2015-01-30 14:09:25 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Teardown monitoring of events on an UDP/IP socket
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* psock - The TCP/IP socket of interest
|
|
|
|
* fds - The structure describing the events to be monitored, OR NULL if
|
|
|
|
* this is a request to stop monitoring events.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0: Success; Negated errno on failure
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int udp_pollteardown(FAR struct socket *psock, FAR struct pollfd *fds)
|
|
|
|
{
|
|
|
|
FAR struct udp_conn_s *conn = psock->s_conn;
|
|
|
|
FAR struct udp_poll_s *info;
|
|
|
|
|
|
|
|
/* Sanity check */
|
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2015-01-30 14:09:25 +01:00
|
|
|
if (!conn || !fds->priv)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Recover the socket descriptor poll state info from the poll structure */
|
|
|
|
|
|
|
|
info = (FAR struct udp_poll_s *)fds->priv;
|
|
|
|
DEBUGASSERT(info && info->fds && info->cb);
|
|
|
|
if (info)
|
|
|
|
{
|
|
|
|
/* Release the callback */
|
|
|
|
|
2016-12-03 23:28:19 +01:00
|
|
|
net_lock();
|
2015-05-29 18:21:06 +02:00
|
|
|
udp_callback_free(info->dev, conn, info->cb);
|
2016-12-03 23:28:19 +01:00
|
|
|
net_unlock();
|
2015-01-30 14:09:25 +01:00
|
|
|
|
|
|
|
/* Release the poll/select data slot */
|
|
|
|
|
|
|
|
info->fds->priv = NULL;
|
|
|
|
|
|
|
|
/* Then free the poll info container */
|
|
|
|
|
|
|
|
kmm_free(info);
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* !HAVE_UDP_POLL */
|