2015-05-01 16:19:52 +08:00
|
|
|
/*
|
2016-05-10 09:22:01 +08:00
|
|
|
* File : dfs_net.c
|
2015-05-01 16:19:52 +08:00
|
|
|
* This file is part of RT-Thread RTOS
|
2016-05-10 09:22:01 +08:00
|
|
|
* COPYRIGHT (C) 2015-2016, RT-Thread Development Team
|
2015-05-01 16:19:52 +08:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2015-02-17 Bernard First version
|
2016-05-10 09:22:01 +08:00
|
|
|
* 2016-05-07 Bernard Rename dfs_lwip to dfs_net
|
2015-05-01 16:19:52 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <rtthread.h>
|
|
|
|
#include <dfs.h>
|
|
|
|
#include <dfs_fs.h>
|
2017-10-15 22:44:53 +08:00
|
|
|
#include <dfs_file.h>
|
|
|
|
#include <dfs_posix.h>
|
|
|
|
|
|
|
|
#include <rtdevice.h>
|
|
|
|
#include <sys/socket.h>
|
2015-05-01 16:19:52 +08:00
|
|
|
|
2017-10-17 22:27:06 +08:00
|
|
|
#include <dfs_poll.h>
|
2016-05-10 09:22:01 +08:00
|
|
|
#include "dfs_net.h"
|
2015-05-01 16:19:52 +08:00
|
|
|
|
2016-05-10 09:22:01 +08:00
|
|
|
int dfs_net_getsocket(int fd)
|
2015-05-01 16:19:52 +08:00
|
|
|
{
|
2017-10-15 22:44:53 +08:00
|
|
|
int sock;
|
2015-05-01 16:19:52 +08:00
|
|
|
struct dfs_fd *_dfs_fd;
|
2017-10-15 22:44:53 +08:00
|
|
|
|
2015-05-01 16:19:52 +08:00
|
|
|
_dfs_fd = fd_get(fd);
|
2017-10-15 22:44:53 +08:00
|
|
|
if (_dfs_fd == NULL) return -1;
|
2015-05-01 16:19:52 +08:00
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
if (_dfs_fd->type != FT_SOCKET) sock = -1;
|
|
|
|
else sock = (int)_dfs_fd->data;
|
|
|
|
|
|
|
|
fd_put(_dfs_fd); /* put this dfs fd */
|
|
|
|
return sock;
|
2015-05-01 16:19:52 +08:00
|
|
|
}
|
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
static int dfs_net_ioctl(struct dfs_fd* file, int cmd, void* args)
|
2015-05-01 16:19:52 +08:00
|
|
|
{
|
2017-10-15 22:44:53 +08:00
|
|
|
return -EIO;
|
2015-05-01 16:19:52 +08:00
|
|
|
}
|
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
static int dfs_net_read(struct dfs_fd* file, void *buf, size_t count)
|
2015-05-01 16:19:52 +08:00
|
|
|
{
|
|
|
|
int sock;
|
|
|
|
|
|
|
|
sock = (int)file->data;
|
|
|
|
count = lwip_read(sock, buf, count);
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
static int dfs_net_write(struct dfs_fd *file, const void *buf, size_t count)
|
2015-05-01 16:19:52 +08:00
|
|
|
{
|
|
|
|
int sock;
|
|
|
|
|
|
|
|
sock = (int)file->data;
|
|
|
|
count = lwip_write(sock, buf, count);
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
static int dfs_net_close(struct dfs_fd* file)
|
2015-05-01 16:19:52 +08:00
|
|
|
{
|
|
|
|
int sock;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
sock = (int)file->data;
|
|
|
|
result = lwip_close(sock);
|
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
if (result == 0) return RT_EOK;
|
2015-05-01 16:19:52 +08:00
|
|
|
|
|
|
|
return -result;
|
|
|
|
}
|
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
static int dfs_net_poll(struct dfs_fd *file, struct rt_pollreq *req)
|
|
|
|
{
|
|
|
|
int sfd;
|
|
|
|
int mask = 0;
|
|
|
|
struct lwip_sock *sock;
|
|
|
|
|
|
|
|
sfd = (int)file->data;
|
|
|
|
|
|
|
|
sock = lwip_tryget_socket(sfd);
|
|
|
|
if (sock != NULL)
|
|
|
|
{
|
|
|
|
rt_base_t level;
|
|
|
|
|
|
|
|
rt_poll_add(&sock->wait_head, req);
|
|
|
|
|
|
|
|
level = rt_hw_interrupt_disable();
|
|
|
|
if (sock->rcvevent)
|
|
|
|
{
|
|
|
|
mask |= POLLIN;
|
|
|
|
}
|
|
|
|
if (sock->sendevent)
|
|
|
|
{
|
|
|
|
mask |= POLLOUT;
|
|
|
|
}
|
|
|
|
if (sock->errevent)
|
|
|
|
{
|
|
|
|
mask |= POLLERR;
|
|
|
|
}
|
|
|
|
rt_hw_interrupt_enable(level);
|
|
|
|
}
|
|
|
|
|
|
|
|
return mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct dfs_file_ops _net_fops =
|
2015-05-01 16:19:52 +08:00
|
|
|
{
|
2017-10-15 22:44:53 +08:00
|
|
|
NULL, /* open */
|
2016-05-10 09:22:01 +08:00
|
|
|
dfs_net_close,
|
|
|
|
dfs_net_ioctl,
|
|
|
|
dfs_net_read,
|
|
|
|
dfs_net_write,
|
2017-10-15 22:44:53 +08:00
|
|
|
NULL,
|
|
|
|
NULL, /* lseek */
|
|
|
|
NULL, /* getdents */
|
|
|
|
dfs_net_poll,
|
2015-05-01 16:19:52 +08:00
|
|
|
};
|
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
const struct dfs_file_ops *dfs_net_get_fops(void)
|
2015-05-01 16:19:52 +08:00
|
|
|
{
|
2017-10-15 22:44:53 +08:00
|
|
|
return &_net_fops;
|
2015-05-01 16:19:52 +08:00
|
|
|
}
|
|
|
|
|