2015-05-01 16:19:52 +08:00
|
|
|
/*
|
2021-03-08 18:19:04 +08:00
|
|
|
* Copyright (c) 2006-2021, RT-Thread Development Team
|
2015-05-01 16:19:52 +08:00
|
|
|
*
|
2018-10-15 01:23:33 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2015-05-01 16:19:52 +08:00
|
|
|
*
|
|
|
|
* 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
|
2018-03-09 08:34:43 +08:00
|
|
|
* 2018-03-09 Bernard Fix the last data issue in poll.
|
2018-07-17 20:29:33 +08:00
|
|
|
* 2018-05-24 ChenYong Add socket abstraction layer
|
2015-05-01 16:19:52 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <rtthread.h>
|
2018-07-17 20:29:33 +08:00
|
|
|
|
2015-05-01 16:19:52 +08:00
|
|
|
#include <dfs.h>
|
2018-07-17 20:29:33 +08:00
|
|
|
#include <dfs_net.h>
|
2017-10-15 22:44:53 +08:00
|
|
|
|
|
|
|
#include <sys/socket.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
|
|
|
{
|
2018-07-17 20:29:33 +08:00
|
|
|
int socket;
|
2023-04-08 22:25:51 +08:00
|
|
|
struct dfs_file *file;
|
2017-10-15 22:44:53 +08:00
|
|
|
|
2023-04-08 22:25:51 +08:00
|
|
|
file = fd_get(fd);
|
|
|
|
if (file == NULL) return -1;
|
2015-05-01 16:19:52 +08:00
|
|
|
|
2023-04-08 22:25:51 +08:00
|
|
|
if (file->vnode->type != FT_SOCKET) socket = -1;
|
|
|
|
else socket = (int)(size_t)file->vnode->data;
|
2017-10-15 22:44:53 +08:00
|
|
|
|
2018-07-17 20:29:33 +08:00
|
|
|
return socket;
|
2015-05-01 16:19:52 +08:00
|
|
|
}
|
|
|
|
|
2023-04-08 22:25:51 +08:00
|
|
|
static int dfs_net_ioctl(struct dfs_file* file, int cmd, void* args)
|
2015-05-01 16:19:52 +08:00
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
int ret;
|
|
|
|
int socket = (int)(size_t)file->vnode->data;
|
|
|
|
|
|
|
|
ret = sal_ioctlsocket(socket, cmd, args);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
ret = rt_get_errno();
|
|
|
|
return (ret > 0) ? (-ret) : ret;
|
|
|
|
}
|
|
|
|
return ret;
|
2015-05-01 16:19:52 +08:00
|
|
|
}
|
|
|
|
|
2023-06-10 21:35:25 +08:00
|
|
|
#ifdef RT_USING_DFS_V2
|
2023-07-24 09:12:35 +08:00
|
|
|
static ssize_t dfs_net_read(struct dfs_file* file, void *buf, size_t count, off_t *pos)
|
2023-06-10 21:35:25 +08:00
|
|
|
#else
|
2023-07-24 09:12:35 +08:00
|
|
|
static ssize_t dfs_net_read(struct dfs_file* file, void *buf, size_t count)
|
2023-06-10 21:35:25 +08:00
|
|
|
#endif
|
2015-05-01 16:19:52 +08:00
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
int ret;
|
|
|
|
int socket = (int)(size_t)file->vnode->data;
|
|
|
|
|
|
|
|
ret = sal_recvfrom(socket, buf, count, 0, NULL, NULL);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
ret = rt_get_errno();
|
|
|
|
return (ret > 0) ? (-ret) : ret;
|
|
|
|
}
|
2023-06-10 21:35:25 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
return ret;
|
2015-05-01 16:19:52 +08:00
|
|
|
}
|
|
|
|
|
2023-06-10 21:35:25 +08:00
|
|
|
#ifdef RT_USING_DFS_V2
|
2023-07-24 09:12:35 +08:00
|
|
|
static ssize_t dfs_net_write(struct dfs_file *file, const void *buf, size_t count, off_t *pos)
|
2023-06-10 21:35:25 +08:00
|
|
|
#else
|
2023-07-24 09:12:35 +08:00
|
|
|
static ssize_t dfs_net_write(struct dfs_file *file, const void *buf, size_t count)
|
2023-06-10 21:35:25 +08:00
|
|
|
#endif
|
2015-05-01 16:19:52 +08:00
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
int ret;
|
|
|
|
int socket = (int)(size_t)file->vnode->data;
|
2023-04-08 22:25:51 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
ret = sal_sendto(socket, buf, count, 0, NULL, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
ret = rt_get_errno();
|
|
|
|
return (ret > 0) ? (-ret) : ret;
|
|
|
|
}
|
2023-06-10 21:35:25 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
return ret;
|
2015-05-01 16:19:52 +08:00
|
|
|
}
|
2023-06-10 21:35:25 +08:00
|
|
|
|
2023-04-08 22:25:51 +08:00
|
|
|
static int dfs_net_close(struct dfs_file* file)
|
2015-05-01 16:19:52 +08:00
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
int socket;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (file->vnode->ref_count == 1)
|
|
|
|
{
|
|
|
|
socket = (int)(size_t)file->vnode->data;
|
|
|
|
ret = sal_closesocket(socket);
|
|
|
|
}
|
|
|
|
return ret;
|
2015-05-01 16:19:52 +08:00
|
|
|
}
|
|
|
|
|
2023-04-08 22:25:51 +08:00
|
|
|
static int dfs_net_poll(struct dfs_file *file, struct rt_pollreq *req)
|
2017-10-15 22:44:53 +08:00
|
|
|
{
|
2023-04-08 22:25:51 +08:00
|
|
|
extern int sal_poll(struct dfs_file *file, struct rt_pollreq *req);
|
2018-07-17 20:29:33 +08:00
|
|
|
|
|
|
|
return sal_poll(file, req);
|
2017-10-15 22:44:53 +08:00
|
|
|
}
|
|
|
|
|
2021-03-08 18:19:04 +08:00
|
|
|
const struct dfs_file_ops _net_fops =
|
2015-05-01 16:19:52 +08:00
|
|
|
{
|
2023-06-10 21:35:25 +08:00
|
|
|
.close = dfs_net_close,
|
|
|
|
.ioctl = dfs_net_ioctl,
|
|
|
|
.read = dfs_net_read,
|
|
|
|
.write = dfs_net_write,
|
|
|
|
.poll = 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
|
|
|
}
|