2015-01-30 21:09:25 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* net/udp/udp_netpoll.c
|
|
|
|
*
|
2021-02-19 19:45:37 +08: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
|
2015-01-30 21:09:25 +08:00
|
|
|
*
|
2021-02-19 19:45:37 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-01-30 21:09:25 +08:00
|
|
|
*
|
2021-02-19 19:45:37 +08: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.
|
2015-01-30 21:09:25 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdint.h>
|
2019-12-31 23:26:14 +08:00
|
|
|
#include <assert.h>
|
2015-01-30 21:09:25 +08:00
|
|
|
#include <poll.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/net/net.h>
|
2019-12-31 23:26:14 +08:00
|
|
|
#include <nuttx/semaphore.h>
|
2015-01-30 21:09:25 +08:00
|
|
|
|
2018-09-12 02:00:42 +08:00
|
|
|
#include "devif/devif.h"
|
|
|
|
#include "netdev/netdev.h"
|
|
|
|
#include "socket/socket.h"
|
2015-01-30 21:09:25 +08:00
|
|
|
#include "udp/udp.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-08-30 04:08:04 +08:00
|
|
|
* Name: udp_poll_eventhandler
|
2015-01-30 21:09:25 +08:00
|
|
|
*
|
|
|
|
* Description:
|
2017-09-03 00:27:03 +08:00
|
|
|
* This function is called to perform the actual UDP receive operation
|
|
|
|
* via the device interface layer.
|
2015-01-30 21:09:25 +08:00
|
|
|
*
|
2018-03-13 23:52:27 +08:00
|
|
|
* Input Parameters:
|
2017-09-03 00:27:03 +08:00
|
|
|
* dev The structure of the network driver that caused the event
|
2015-01-30 21:09:25 +08: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-03 00:27:03 +08:00
|
|
|
* This function must be called with the network locked.
|
2015-01-30 21:09:25 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-08-30 04:08:04 +08: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 21:09:25 +08:00
|
|
|
{
|
|
|
|
FAR struct udp_poll_s *info = (FAR struct udp_poll_s *)pvpriv;
|
|
|
|
|
2016-06-21 01:59:15 +08:00
|
|
|
ninfo("flags: %04x\n", flags);
|
2015-01-30 21:09:25 +08:00
|
|
|
|
2022-02-08 23:24:04 +08:00
|
|
|
DEBUGASSERT(!info || (info->conn && info->fds));
|
2015-01-30 21:09:25 +08:00
|
|
|
|
|
|
|
/* 'priv' might be null in some race conditions (?) */
|
|
|
|
|
|
|
|
if (info)
|
|
|
|
{
|
|
|
|
pollevent_t eventset = 0;
|
|
|
|
|
|
|
|
/* Check for data or connection availability events. */
|
|
|
|
|
2015-05-30 00:21:06 +08:00
|
|
|
if ((flags & UDP_NEWDATA) != 0)
|
2015-01-30 21:09:25 +08:00
|
|
|
{
|
|
|
|
eventset |= (POLLIN & info->fds->events);
|
|
|
|
}
|
|
|
|
|
2019-11-25 23:59:50 +08:00
|
|
|
/* Check for loss of connection events. */
|
2015-01-30 21:09:25 +08:00
|
|
|
|
2019-11-25 23:59:50 +08:00
|
|
|
if ((flags & NETDEV_DOWN) != 0)
|
2015-01-30 21:09:25 +08:00
|
|
|
{
|
2019-11-25 23:59:50 +08:00
|
|
|
eventset |= (POLLHUP | POLLERR);
|
2015-01-30 21:09:25 +08:00
|
|
|
}
|
|
|
|
|
2019-11-25 23:59:50 +08:00
|
|
|
/* A poll is a sign that we are free to send data. */
|
2015-05-30 00:21:06 +08:00
|
|
|
|
2022-02-08 23:24:04 +08:00
|
|
|
else if (psock_udp_cansend(info->conn) >= 0)
|
2015-05-30 00:21:06 +08:00
|
|
|
{
|
2019-11-25 23:59:50 +08:00
|
|
|
eventset |= (POLLOUT & info->fds->events);
|
2015-05-30 00:21:06 +08:00
|
|
|
}
|
|
|
|
|
2015-01-30 21:09:25 +08:00
|
|
|
/* Awaken the caller of poll() is requested event occurred. */
|
|
|
|
|
|
|
|
if (eventset)
|
|
|
|
{
|
|
|
|
info->fds->revents |= eventset;
|
2017-10-04 05:35:24 +08:00
|
|
|
nxsem_post(info->fds->sem);
|
2015-01-30 21:09:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 06:33:14 +08:00
|
|
|
* Name: udp_pollsetup
|
2015-01-30 21:09:25 +08: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;
|
2020-01-02 22:52:28 +08:00
|
|
|
int ret = OK;
|
2015-01-30 21:09:25 +08:00
|
|
|
|
|
|
|
/* Sanity check */
|
|
|
|
|
2016-06-12 04:14:08 +08:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2018-09-12 00:31:11 +08:00
|
|
|
if (conn == NULL || fds == NULL)
|
2015-01-30 21:09:25 +08:00
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Some of the following must be atomic */
|
|
|
|
|
2016-12-04 06:28:19 +08:00
|
|
|
net_lock();
|
2015-01-30 21:09:25 +08:00
|
|
|
|
2019-12-31 23:26:14 +08:00
|
|
|
/* Find a container to hold the poll information */
|
|
|
|
|
|
|
|
info = conn->pollinfo;
|
2022-02-08 23:24:04 +08:00
|
|
|
while (info->conn != NULL)
|
2019-12-31 23:26:14 +08:00
|
|
|
{
|
|
|
|
if (++info >= &conn->pollinfo[CONFIG_NET_UDP_NPOLLWAITERS])
|
|
|
|
{
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto errout_with_lock;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-30 00:21:06 +08: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);
|
|
|
|
|
2017-10-23 22:45:12 +08:00
|
|
|
/* Allocate a UDP callback structure */
|
2015-01-30 21:09:25 +08:00
|
|
|
|
2015-05-30 00:21:06 +08:00
|
|
|
cb = udp_callback_alloc(info->dev, conn);
|
2017-10-20 01:55:51 +08:00
|
|
|
if (cb == NULL)
|
2015-01-30 21:09:25 +08:00
|
|
|
{
|
|
|
|
ret = -EBUSY;
|
|
|
|
goto errout_with_lock;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the poll info container */
|
|
|
|
|
2022-02-08 23:24:04 +08:00
|
|
|
info->conn = conn;
|
|
|
|
info->fds = fds;
|
|
|
|
info->cb = cb;
|
2015-01-30 21:09:25 +08:00
|
|
|
|
|
|
|
/* Initialize the callback structure. Save the reference to the info
|
|
|
|
* structure as callback private data so that it will be available during
|
|
|
|
* callback processing.
|
|
|
|
*/
|
|
|
|
|
2019-11-25 00:18:08 +08:00
|
|
|
cb->flags = NETDEV_DOWN;
|
2015-01-30 21:09:25 +08:00
|
|
|
cb->priv = (FAR void *)info;
|
2017-08-30 04:08:04 +08:00
|
|
|
cb->event = udp_poll_eventhandler;
|
2015-01-30 21:09:25 +08:00
|
|
|
|
2019-11-25 00:18:08 +08:00
|
|
|
if ((fds->events & POLLOUT) != 0)
|
2015-05-30 00:21:06 +08:00
|
|
|
{
|
|
|
|
cb->flags |= UDP_POLL;
|
|
|
|
}
|
|
|
|
|
2019-11-25 00:18:08 +08:00
|
|
|
if ((fds->events & POLLIN) != 0)
|
2015-05-30 00:21:06 +08:00
|
|
|
{
|
|
|
|
cb->flags |= UDP_NEWDATA;
|
|
|
|
}
|
|
|
|
|
2015-01-30 21:09:25 +08:00
|
|
|
/* Save the reference in the poll info structure as fds private as well
|
2015-05-30 00:21:06 +08:00
|
|
|
* for use during poll teardown as well.
|
2015-01-30 21:09:25 +08:00
|
|
|
*/
|
|
|
|
|
2015-05-30 00:21:06 +08:00
|
|
|
fds->priv = (FAR void *)info;
|
2015-01-30 21:09:25 +08:00
|
|
|
|
|
|
|
/* Check for read data availability now */
|
|
|
|
|
|
|
|
if (!IOB_QEMPTY(&conn->readahead))
|
|
|
|
{
|
|
|
|
/* Normal data may be read without blocking. */
|
|
|
|
|
|
|
|
fds->revents |= (POLLRDNORM & fds->events);
|
|
|
|
}
|
|
|
|
|
2022-02-08 23:24:04 +08:00
|
|
|
if (psock_udp_cansend(conn) >= 0)
|
2018-09-13 00:10:54 +08:00
|
|
|
{
|
|
|
|
/* Normal data may be sent without blocking (at least one byte). */
|
|
|
|
|
|
|
|
fds->revents |= (POLLWRNORM & fds->events);
|
|
|
|
}
|
|
|
|
|
2015-01-30 21:09:25 +08:00
|
|
|
/* Check if any requested events are already in effect */
|
|
|
|
|
|
|
|
if (fds->revents != 0)
|
|
|
|
{
|
|
|
|
/* Yes.. then signal the poll logic */
|
2018-09-12 00:31:11 +08:00
|
|
|
|
2017-10-04 05:35:24 +08:00
|
|
|
nxsem_post(fds->sem);
|
2015-01-30 21:09:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
errout_with_lock:
|
2016-12-04 06:28:19 +08:00
|
|
|
net_unlock();
|
2015-01-30 21:09:25 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 06:33:14 +08:00
|
|
|
* Name: udp_pollteardown
|
2015-01-30 21:09:25 +08:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Teardown monitoring of events on an UDP/IP socket
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
2017-10-23 22:45:12 +08:00
|
|
|
* psock - The UDP socket of interest
|
2015-01-30 21:09:25 +08:00
|
|
|
* 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-12 04:14:08 +08:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2015-01-30 21:09:25 +08: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;
|
2018-09-13 00:10:54 +08:00
|
|
|
DEBUGASSERT(info != NULL && info->fds != NULL && info->cb != NULL);
|
|
|
|
if (info != NULL)
|
2015-01-30 21:09:25 +08:00
|
|
|
{
|
|
|
|
/* Release the callback */
|
|
|
|
|
2015-05-30 00:21:06 +08:00
|
|
|
udp_callback_free(info->dev, conn, info->cb);
|
2015-01-30 21:09:25 +08:00
|
|
|
|
|
|
|
/* Release the poll/select data slot */
|
|
|
|
|
|
|
|
info->fds->priv = NULL;
|
|
|
|
|
|
|
|
/* Then free the poll info container */
|
|
|
|
|
2022-02-08 23:24:04 +08:00
|
|
|
info->conn = NULL;
|
2015-01-30 21:09:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|