1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
/*
* File : dfs_net.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2015-2016, RT-Thread Development Team
*
* 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-07 Bernard Rename dfs_lwip to dfs_net
*/
#include <rtthread.h>
#include <dfs.h>
#include <dfs_fs.h>
#include <dfs_file.h>
#include <dfs_posix.h>
#include <rtdevice.h>
#include <sys/socket.h>
#include <dfs_poll.h>
#include "dfs_net.h"
int dfs_net_getsocket(int fd)
{
int sock;
struct dfs_fd *_dfs_fd;
_dfs_fd = fd_get(fd);
if (_dfs_fd == NULL) return -1;
if (_dfs_fd->type != FT_SOCKET) sock = -1;
else sock = (int)_dfs_fd->data;
fd_put(_dfs_fd); /* put this dfs fd */
return sock;
}
static int dfs_net_ioctl(struct dfs_fd* file, int cmd, void* args)
{
return -EIO;
}
static int dfs_net_read(struct dfs_fd* file, void *buf, size_t count)
{
int sock;
sock = (int)file->data;
count = lwip_read(sock, buf, count);
return count;
}
static int dfs_net_write(struct dfs_fd *file, const void *buf, size_t count)
{
int sock;
sock = (int)file->data;
count = lwip_write(sock, buf, count);
return count;
}
static int dfs_net_close(struct dfs_fd* file)
{
int sock;
int result;
sock = (int)file->data;
result = lwip_close(sock);
if (result == 0) return RT_EOK;
return -result;
}
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 =
{
NULL, /* open */
dfs_net_close,
dfs_net_ioctl,
dfs_net_read,
dfs_net_write,
NULL,
NULL, /* lseek */
NULL, /* getdents */
dfs_net_poll,
};
const struct dfs_file_ops *dfs_net_get_fops(void)
{
return &_net_fops;
}
|