2017-03-31 16:58:14 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* net/usrsock/usrsock_dev.c
|
|
|
|
*
|
2021-11-15 07:53:26 +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
|
2017-03-31 16:58:14 +02:00
|
|
|
*
|
2021-11-15 07:53:26 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2017-03-31 16:58:14 +02:00
|
|
|
*
|
2021-11-15 07:53:26 +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.
|
2017-03-31 16:58:14 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
#if defined(CONFIG_NET) && defined(CONFIG_NET_USRSOCK)
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2020-11-27 06:17:04 +01:00
|
|
|
#include <inttypes.h>
|
2017-03-31 16:58:14 +02:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <poll.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <arch/irq.h>
|
|
|
|
|
|
|
|
#include <nuttx/random.h>
|
|
|
|
#include <nuttx/fs/fs.h>
|
2017-10-25 15:31:14 +02:00
|
|
|
#include <nuttx/semaphore.h>
|
2017-03-31 16:58:14 +02:00
|
|
|
#include <nuttx/net/net.h>
|
|
|
|
#include <nuttx/net/usrsock.h>
|
|
|
|
|
|
|
|
/****************************************************************************
|
2020-01-13 21:26:04 +01:00
|
|
|
* Pre-processor Definitions
|
2017-03-31 16:58:14 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_NET_USRSOCKDEV_NPOLLWAITERS
|
|
|
|
# define CONFIG_NET_USRSOCKDEV_NPOLLWAITERS 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct usrsockdev_s
|
|
|
|
{
|
2022-08-21 23:10:47 +02:00
|
|
|
sem_t devsem; /* Lock for device node */
|
|
|
|
uint8_t ocount; /* The number of times the device has been opened */
|
2017-03-31 16:58:14 +02:00
|
|
|
struct
|
|
|
|
{
|
|
|
|
FAR const struct iovec *iov; /* Pending request buffers */
|
2022-01-04 08:53:22 +01:00
|
|
|
int iovcnt; /* Number of request buffers */
|
|
|
|
size_t pos; /* Reader position on request buffer */
|
2017-03-31 16:58:14 +02:00
|
|
|
} req;
|
|
|
|
struct pollfd *pollfds[CONFIG_NET_USRSOCKDEV_NPOLLWAITERS];
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Character driver methods */
|
|
|
|
|
|
|
|
static ssize_t usrsockdev_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t len);
|
|
|
|
|
|
|
|
static ssize_t usrsockdev_write(FAR struct file *filep,
|
|
|
|
FAR const char *buffer, size_t len);
|
|
|
|
|
|
|
|
static off_t usrsockdev_seek(FAR struct file *filep, off_t offset,
|
|
|
|
int whence);
|
|
|
|
|
|
|
|
static int usrsockdev_open(FAR struct file *filep);
|
|
|
|
|
|
|
|
static int usrsockdev_close(FAR struct file *filep);
|
|
|
|
|
|
|
|
static int usrsockdev_poll(FAR struct file *filep, FAR struct pollfd *fds,
|
|
|
|
bool setup);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static const struct file_operations g_usrsockdevops =
|
|
|
|
{
|
|
|
|
usrsockdev_open, /* open */
|
|
|
|
usrsockdev_close, /* close */
|
|
|
|
usrsockdev_read, /* read */
|
|
|
|
usrsockdev_write, /* write */
|
|
|
|
usrsockdev_seek, /* seek */
|
2019-05-22 02:57:54 +02:00
|
|
|
NULL, /* ioctl */
|
|
|
|
usrsockdev_poll /* poll */
|
2017-03-31 16:58:14 +02:00
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
|
|
|
, NULL /* unlink */
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct usrsockdev_s g_usrsockdev;
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usrsockdev_semtake() and usrsockdev_semgive()
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Take/give semaphore
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-03-29 19:57:53 +02:00
|
|
|
static int usrsockdev_semtake(FAR sem_t *sem)
|
2017-03-31 16:58:14 +02:00
|
|
|
{
|
2020-03-29 19:57:53 +02:00
|
|
|
return nxsem_wait_uninterruptible(sem);
|
2017-03-31 16:58:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void usrsockdev_semgive(FAR sem_t *sem)
|
|
|
|
{
|
2020-01-02 17:49:34 +01:00
|
|
|
nxsem_post(sem);
|
2017-03-31 16:58:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usrsockdev_is_opened
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static bool usrsockdev_is_opened(FAR struct usrsockdev_s *dev)
|
|
|
|
{
|
|
|
|
bool ret = true;
|
|
|
|
|
|
|
|
if (dev->ocount == 0)
|
|
|
|
{
|
|
|
|
ret = false; /* No usrsock daemon running. */
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usrsockdev_pollnotify
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-03-11 19:48:17 +01:00
|
|
|
static void usrsockdev_pollnotify(FAR struct usrsockdev_s *dev,
|
|
|
|
pollevent_t eventset)
|
2017-03-31 16:58:14 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < ARRAY_SIZE(dev->pollfds); i++)
|
|
|
|
{
|
|
|
|
struct pollfd *fds = dev->pollfds[i];
|
|
|
|
if (fds)
|
|
|
|
{
|
|
|
|
fds->revents |= (fds->events & eventset);
|
|
|
|
if (fds->revents != 0)
|
|
|
|
{
|
2022-04-01 14:59:55 +02:00
|
|
|
ninfo("Report events: %08" PRIx32 "\n", fds->revents);
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(fds->sem);
|
2017-03-31 16:58:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usrsockdev_read
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t usrsockdev_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t len)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct usrsockdev_s *dev;
|
2020-03-29 19:57:53 +02:00
|
|
|
int ret;
|
2017-03-31 16:58:14 +02:00
|
|
|
|
|
|
|
if (len == 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buffer == NULL)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUGASSERT(inode);
|
|
|
|
|
|
|
|
dev = inode->i_private;
|
|
|
|
|
|
|
|
DEBUGASSERT(dev);
|
|
|
|
|
2020-03-29 19:57:53 +02:00
|
|
|
ret = usrsockdev_semtake(&dev->devsem);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-03-31 16:58:14 +02:00
|
|
|
/* Is request available? */
|
|
|
|
|
|
|
|
if (dev->req.iov)
|
|
|
|
{
|
|
|
|
ssize_t rlen;
|
|
|
|
|
|
|
|
/* Copy request to user-space. */
|
|
|
|
|
2022-09-01 04:11:44 +02:00
|
|
|
rlen = usrsock_iovec_get(buffer, len, dev->req.iov, dev->req.iovcnt,
|
|
|
|
dev->req.pos);
|
2017-03-31 16:58:14 +02:00
|
|
|
if (rlen < 0)
|
|
|
|
{
|
|
|
|
/* Tried reading beyond buffer. */
|
|
|
|
|
|
|
|
len = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dev->req.pos += rlen;
|
|
|
|
len = rlen;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
len = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
usrsockdev_semgive(&dev->devsem);
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usrsockdev_seek
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-03-29 19:57:53 +02:00
|
|
|
static off_t usrsockdev_seek(FAR struct file *filep, off_t offset,
|
|
|
|
int whence)
|
2017-03-31 16:58:14 +02:00
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct usrsockdev_s *dev;
|
|
|
|
off_t pos;
|
2020-03-29 19:57:53 +02:00
|
|
|
int ret;
|
2017-03-31 16:58:14 +02:00
|
|
|
|
|
|
|
if (whence != SEEK_CUR && whence != SEEK_SET)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUGASSERT(inode);
|
|
|
|
|
|
|
|
dev = inode->i_private;
|
|
|
|
|
|
|
|
DEBUGASSERT(dev);
|
|
|
|
|
2020-03-29 19:57:53 +02:00
|
|
|
ret = usrsockdev_semtake(&dev->devsem);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-03-31 16:58:14 +02:00
|
|
|
/* Is request available? */
|
|
|
|
|
|
|
|
if (dev->req.iov)
|
|
|
|
{
|
|
|
|
ssize_t rlen;
|
|
|
|
|
|
|
|
if (whence == SEEK_CUR)
|
|
|
|
{
|
|
|
|
pos = dev->req.pos + offset;
|
|
|
|
}
|
2020-04-29 12:15:18 +02:00
|
|
|
else
|
2017-03-31 16:58:14 +02:00
|
|
|
{
|
|
|
|
pos = offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy request to user-space. */
|
|
|
|
|
2022-09-01 04:11:44 +02:00
|
|
|
rlen = usrsock_iovec_get(NULL, 0, dev->req.iov, dev->req.iovcnt,
|
|
|
|
pos);
|
2017-03-31 16:58:14 +02:00
|
|
|
if (rlen < 0)
|
|
|
|
{
|
|
|
|
/* Tried seek beyond buffer. */
|
|
|
|
|
|
|
|
pos = -EINVAL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dev->req.pos = pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pos = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
usrsockdev_semgive(&dev->devsem);
|
|
|
|
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usrsockdev_write
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-03-11 19:48:17 +01:00
|
|
|
static ssize_t usrsockdev_write(FAR struct file *filep,
|
|
|
|
FAR const char *buffer, size_t len)
|
2017-03-31 16:58:14 +02:00
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct usrsockdev_s *dev;
|
|
|
|
ssize_t ret = 0;
|
2022-09-01 04:11:44 +02:00
|
|
|
bool req_done = false;
|
2017-03-31 16:58:14 +02:00
|
|
|
|
|
|
|
if (len == 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buffer == NULL)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUGASSERT(inode);
|
|
|
|
|
|
|
|
dev = inode->i_private;
|
|
|
|
|
|
|
|
DEBUGASSERT(dev);
|
|
|
|
|
2020-03-29 19:57:53 +02:00
|
|
|
ret = (ssize_t)usrsockdev_semtake(&dev->devsem);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2017-03-31 16:58:14 +02:00
|
|
|
|
2022-09-01 04:11:44 +02:00
|
|
|
ret = usrsock_response(buffer, len, &req_done);
|
|
|
|
if (req_done && dev->req.iov)
|
2017-03-31 16:58:14 +02:00
|
|
|
{
|
2022-09-01 04:11:44 +02:00
|
|
|
dev->req.iov = NULL;
|
|
|
|
dev->req.pos = 0;
|
|
|
|
dev->req.iovcnt = 0;
|
2017-03-31 16:58:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
usrsockdev_semgive(&dev->devsem);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usrsockdev_open
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int usrsockdev_open(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct usrsockdev_s *dev;
|
2019-03-11 19:48:17 +01:00
|
|
|
int ret;
|
|
|
|
int tmp;
|
2017-03-31 16:58:14 +02:00
|
|
|
|
|
|
|
DEBUGASSERT(inode);
|
|
|
|
|
|
|
|
dev = inode->i_private;
|
|
|
|
|
|
|
|
DEBUGASSERT(dev);
|
|
|
|
|
2020-03-29 19:57:53 +02:00
|
|
|
ret = usrsockdev_semtake(&dev->devsem);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2017-03-31 16:58:14 +02:00
|
|
|
|
2019-06-03 13:43:43 +02:00
|
|
|
ninfo("opening /dev/usrsock\n");
|
2017-03-31 16:58:14 +02:00
|
|
|
|
|
|
|
/* Increment the count of references to the device. */
|
|
|
|
|
|
|
|
tmp = dev->ocount + 1;
|
|
|
|
if (tmp > 1)
|
|
|
|
{
|
|
|
|
/* Only one reference is allowed. */
|
|
|
|
|
|
|
|
nwarn("failed to open\n");
|
|
|
|
|
|
|
|
ret = -EPERM;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dev->ocount = tmp;
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
usrsockdev_semgive(&dev->devsem);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usrsockdev_close
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int usrsockdev_close(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct usrsockdev_s *dev;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
DEBUGASSERT(inode);
|
|
|
|
|
|
|
|
dev = inode->i_private;
|
|
|
|
|
|
|
|
DEBUGASSERT(dev);
|
|
|
|
|
2020-03-29 19:57:53 +02:00
|
|
|
ret = usrsockdev_semtake(&dev->devsem);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2017-03-31 16:58:14 +02:00
|
|
|
|
|
|
|
ninfo("closing /dev/usrsock\n");
|
|
|
|
|
|
|
|
/* Decrement the references to the driver. */
|
|
|
|
|
|
|
|
dev->ocount--;
|
|
|
|
DEBUGASSERT(dev->ocount == 0);
|
|
|
|
ret = OK;
|
2022-09-01 04:11:44 +02:00
|
|
|
dev->req.iov = NULL;
|
|
|
|
dev->req.iovcnt = 0;
|
|
|
|
dev->req.pos = 0;
|
2017-03-31 16:58:14 +02:00
|
|
|
|
|
|
|
usrsockdev_semgive(&dev->devsem);
|
2022-09-01 04:11:44 +02:00
|
|
|
usrsock_abort();
|
2017-03-31 16:58:14 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2019-02-18 22:32:00 +01:00
|
|
|
* Name: usrsockdev_poll
|
2017-03-31 16:58:14 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int usrsockdev_poll(FAR struct file *filep, FAR struct pollfd *fds,
|
|
|
|
bool setup)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct usrsockdev_s *dev;
|
|
|
|
pollevent_t eventset;
|
2020-03-29 19:57:53 +02:00
|
|
|
int ret;
|
2017-03-31 16:58:14 +02:00
|
|
|
int i;
|
|
|
|
|
|
|
|
DEBUGASSERT(inode);
|
|
|
|
|
|
|
|
dev = inode->i_private;
|
|
|
|
|
|
|
|
DEBUGASSERT(dev);
|
|
|
|
|
|
|
|
/* Some sanity checking */
|
|
|
|
|
|
|
|
if (!dev || !fds)
|
|
|
|
{
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Are we setting up the poll? Or tearing it down? */
|
|
|
|
|
2020-03-29 19:57:53 +02:00
|
|
|
ret = usrsockdev_semtake(&dev->devsem);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-03-31 16:58:14 +02:00
|
|
|
if (setup)
|
|
|
|
{
|
|
|
|
/* This is a request to set up the poll. Find an available
|
|
|
|
* slot for the poll structure reference
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(dev->pollfds); i++)
|
|
|
|
{
|
|
|
|
/* Find an available slot */
|
|
|
|
|
|
|
|
if (!dev->pollfds[i])
|
|
|
|
{
|
|
|
|
/* Bind the poll structure and this slot */
|
|
|
|
|
|
|
|
dev->pollfds[i] = fds;
|
|
|
|
fds->priv = &dev->pollfds[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i >= ARRAY_SIZE(dev->pollfds))
|
|
|
|
{
|
|
|
|
fds->priv = NULL;
|
|
|
|
ret = -EBUSY;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Should immediately notify on any of the requested events? */
|
|
|
|
|
|
|
|
eventset = 0;
|
|
|
|
|
|
|
|
/* Notify the POLLIN event if pending request. */
|
|
|
|
|
|
|
|
if (dev->req.iov != NULL &&
|
2022-09-01 04:11:44 +02:00
|
|
|
!(usrsock_iovec_get(NULL, 0, dev->req.iov,
|
|
|
|
dev->req.iovcnt, dev->req.pos) < 0))
|
2017-03-31 16:58:14 +02:00
|
|
|
{
|
|
|
|
eventset |= POLLIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (eventset)
|
|
|
|
{
|
|
|
|
usrsockdev_pollnotify(dev, eventset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* This is a request to tear down the poll. */
|
|
|
|
|
|
|
|
FAR struct pollfd **slot = (FAR struct pollfd **)fds->priv;
|
|
|
|
|
|
|
|
if (!slot)
|
|
|
|
{
|
|
|
|
ret = -EIO;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove all memory of the poll setup */
|
|
|
|
|
|
|
|
*slot = NULL;
|
|
|
|
fds->priv = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
errout:
|
|
|
|
usrsockdev_semgive(&dev->devsem);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2022-09-01 04:11:44 +02:00
|
|
|
* Name: usrsock_request
|
2017-03-31 16:58:14 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2022-09-01 04:11:44 +02:00
|
|
|
int usrsock_request(FAR struct iovec *iov, unsigned int iovcnt)
|
2017-03-31 16:58:14 +02:00
|
|
|
{
|
2022-08-21 22:24:31 +02:00
|
|
|
FAR struct usrsockdev_s *dev = &g_usrsockdev;
|
2022-09-01 04:11:44 +02:00
|
|
|
int ret = 0;
|
2017-03-31 16:58:14 +02:00
|
|
|
|
|
|
|
/* Set outstanding request for daemon to handle. */
|
|
|
|
|
2022-09-01 04:11:44 +02:00
|
|
|
net_lockedwait_uninterruptible(&dev->devsem);
|
2017-03-31 16:58:14 +02:00
|
|
|
|
|
|
|
if (usrsockdev_is_opened(dev))
|
|
|
|
{
|
|
|
|
DEBUGASSERT(dev->req.iov == NULL);
|
|
|
|
dev->req.iov = iov;
|
|
|
|
dev->req.pos = 0;
|
|
|
|
dev->req.iovcnt = iovcnt;
|
|
|
|
|
|
|
|
/* Notify daemon of new request. */
|
|
|
|
|
|
|
|
usrsockdev_pollnotify(dev, POLLIN);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-09-01 04:11:44 +02:00
|
|
|
ninfo("daemon abruptly closed /dev/usrsock.\n");
|
|
|
|
ret = -ENETDOWN;
|
2017-03-31 16:58:14 +02:00
|
|
|
}
|
|
|
|
|
2022-09-01 04:11:44 +02:00
|
|
|
usrsockdev_semgive(&dev->devsem);
|
2017-03-31 16:58:14 +02:00
|
|
|
|
2022-09-01 04:11:44 +02:00
|
|
|
return ret;
|
2017-03-31 16:58:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2022-09-01 04:11:44 +02:00
|
|
|
* Name: usrsock_register
|
2017-03-31 16:58:14 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Register /dev/usrsock
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-09-01 04:11:44 +02:00
|
|
|
void usrsock_register(void)
|
2017-03-31 16:58:14 +02:00
|
|
|
{
|
|
|
|
/* Initialize device private structure. */
|
|
|
|
|
|
|
|
g_usrsockdev.ocount = 0;
|
2017-10-03 20:51:15 +02:00
|
|
|
nxsem_init(&g_usrsockdev.devsem, 0, 1);
|
2017-03-31 16:58:14 +02:00
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
register_driver("/dev/usrsock", &g_usrsockdevops, 0666,
|
|
|
|
&g_usrsockdev);
|
2017-03-31 16:58:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_NET && CONFIG_NET_USRSOCK */
|