-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsocket.c
More file actions
308 lines (290 loc) · 9.42 KB
/
Copy pathsocket.c
File metadata and controls
308 lines (290 loc) · 9.42 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
* Copyright (c) 2009 Braiden Kindt
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
*/
#include "socket.h"
#include "w5100.h"
#include "debug.h"
uint8_t allocated_socks = 0x00;
uint8_t tcp_source_port = 0x00;
#ifdef DEBUG
void sock_dump_netstat()
{
uint8_t n;
log("sock_dump_netstat():\n");
log("## SOFT_STAT SR IR MR SOURCE->DEST\n");
for (n = 0; n < 4; n++) {
uint8_t sockreg = W5100_SOCKET_REG + n;
log_uint8(n);
log(" ");
if (allocated_socks & _BV(n)) {
log("ALLOCATED ");
} else {
log(" FREE ");
}
log_uint8(w5100_mem_read(sockreg, W5100_Sn_SR));
log(" ");
log_uint8(w5100_mem_read(sockreg, W5100_Sn_IR));
log(" ");
log_uint8(w5100_mem_read(sockreg, W5100_Sn_MR));
log(" 0x");
log_uint8(w5100_mem_read(sockreg, W5100_Sn_PORT0));
log_uint8(w5100_mem_read(sockreg, W5100_Sn_PORT1));
log("->");
log_int(w5100_mem_read(sockreg, W5100_Sn_DIPR0), 1, 10);
log(".");
log_int(w5100_mem_read(sockreg, W5100_Sn_DIPR1), 1, 10);
log(".");
log_int(w5100_mem_read(sockreg, W5100_Sn_DIPR2), 1, 10);
log(".");
log_int(w5100_mem_read(sockreg, W5100_Sn_DIPR3), 1, 10);
log(":0x");
log_uint8(w5100_mem_read(sockreg, W5100_Sn_DPORT0));
log_uint8(w5100_mem_read(sockreg, W5100_Sn_DPORT1));
log("\n");
}
}
#else
void sock_dump_netstat();
#endif
#ifndef SOCK_NO_SERVER
void _sock_cleanup_discon(uint8_t listenfd)
{
uint8_t n;
for (n = 0; n < 4 ; n++) {
if (listenfd & _BV(n)) {
uint8_t sockreg = W5100_SOCKET_REG + n;
uint8_t sockint = w5100_mem_read(sockreg, W5100_Sn_IR);
if (sockint & _BV(W5100_Sn_IR_DISCON)) {
// listening sockets don't auto return to LISTEN,
// need to force the state transition
w5100_mem_write(sockreg, W5100_Sn_IR, _BV(W5100_Sn_IR_DISCON));
w5100_mem_write(sockreg, W5100_Sn_CR, W5100_LISTEN);
} else if (sockint & _BV(W5100_Sn_IR_TIMEOUT)) {
// if the socket timed out, it will be in discon, put back into listen
w5100_mem_write(sockreg, W5100_Sn_IR, _BV(W5100_Sn_IR_TIMEOUT));
w5100_mem_write(sockreg, W5100_Sn_CR, W5100_LISTEN);
}
}
}
}
#else
void _sock_cleanup_discon(uint8_t listenfd) {}
#endif
uint8_t _sock_write(uint8_t sockfd, char *data, uint8_t len, uint8_t writetype)
{
uint8_t sockreg = W5100_SOCKET_REG + sockfd;
uint8_t bytes_wrote = 0;
while (w5100_mem_read(sockreg, W5100_Sn_SR) == W5100_SOCK_ESTABLISHED &&
!(bytes_wrote = w5100_sock_rw(sockfd, data, len, writetype)));
return bytes_wrote;
}
#ifndef SOCK_NO_SERVER
uint8_t sock_listen(uint16_t port, uint8_t nconcurrent)
{
uint8_t sockfd = LISTEN_SOCK_MASK;
uint8_t sockbit = 0x01;
uint8_t sockreg = W5100_SOCKET_REG;
uint8_t porth = port >> 8;
uint8_t portl = port;
// try to find ncurrent availible hardware sockets
while ((sockbit & 0x0F) && (nconcurrent)) {
if (!(allocated_socks & sockbit)) {
allocated_socks |= sockbit;
sockfd |= sockbit;
// set type to TCP
w5100_mem_write(sockreg, W5100_Sn_MR, W5100_TCP);
// set the port
w5100_mem_write(sockreg, W5100_Sn_PORT0, porth);
w5100_mem_write(sockreg, W5100_Sn_PORT1, portl);
// let the w5100 know the socket is configured
w5100_mem_write(sockreg, W5100_Sn_CR, W5100_OPEN);
// clear any ints
w5100_mem_write(sockreg, W5100_Sn_IR, 0xff);
// open the socket for connections
w5100_mem_write(sockreg, W5100_Sn_CR, W5100_LISTEN);
nconcurrent--;
}
sockbit<<=1;
sockreg++;
}
// sockfd is a phony file descriptor, the 4 LSB bits
// are one iff that hardware socket is allocated to
// this pseduo fd, we set LISTEN_SOCK_MASK to 4 MSB
// so we can recognize this fd as a listening sock pool.
return sockfd;
}
#endif
#ifndef SOCK_NO_SERVER
uint8_t sock_accept(uint8_t listenfd)
{
// need to clean-up disconnected clients,
// so those hw sockets can be used to connect
_sock_cleanup_discon(listenfd);
uint8_t sock = 0;
// block until we get a connection
while (1) {
uint8_t sockfd = sock & 0x07;
if (listenfd & _BV(sockfd)) {
// read the status reg
uint8_t sockreg = W5100_SOCKET_REG + sockfd;
uint8_t sockint = w5100_mem_read(sockreg, W5100_Sn_IR);
// a new client as connected
if (sockint & _BV(W5100_Sn_IR_CON)) {
// clear the status reg
w5100_mem_write(sockreg, W5100_Sn_IR, _BV(W5100_Sn_IR_CON));
break;
}
}
sock++;
}
// return the sockfd of newly connected client
return sock & 0x07;
}
#endif
#ifndef SOCK_NO_CLIENT
uint8_t sock_connect(uint8_t *addr, uint16_t port)
{
uint8_t sock;
// try to find an unused hardware socket
for (sock = 0; sock < 4; sock++) {
// if the sock, isn't being used, continue
if (!((0x01 << sock) & (allocated_socks))) {
// high byte of this sockets address in w5100
uint8_t sockreg = W5100_SOCKET_REG + sock;
uint8_t ir = 0x00;
uint8_t irmask = _BV(W5100_Sn_IR_CON) | _BV(W5100_Sn_IR_TIMEOUT) | _BV(W5100_Sn_IR_DISCON);
allocated_socks |= 0x01 << sock;
tcp_source_port++;
// use tcp
w5100_mem_write(sockreg, W5100_Sn_MR, W5100_TCP);
// create a source port, we have a pool of 255 (0xFF00-0xFFFF)
// pickup the last recently unused.
// TODO, should really verify that source port is unused0
w5100_mem_write(sockreg, W5100_Sn_PORT0, 0xFF);
w5100_mem_write(sockreg, W5100_Sn_PORT1, tcp_source_port);
// ip destination port
w5100_mem_write(sockreg, W5100_Sn_DPORT0, port >> 8);
w5100_mem_write(sockreg, W5100_Sn_DPORT1, port);
// ip destination address
w5100_mem_write(sockreg, W5100_Sn_DIPR0, addr[0]);
w5100_mem_write(sockreg, W5100_Sn_DIPR1, addr[1]);
w5100_mem_write(sockreg, W5100_Sn_DIPR2, addr[2]);
w5100_mem_write(sockreg, W5100_Sn_DIPR3, addr[3]);
// tell the w5100 configuration is set
w5100_mem_write(sockreg, W5100_Sn_CR, W5100_OPEN);
// clear status reg
w5100_mem_write(sockreg, W5100_Sn_IR, 0xff);
// initiate connection
w5100_mem_write(sockreg, W5100_Sn_CR, W5100_CONNECT);
// wait for something to happen (connect or timeout)
while (!((ir = w5100_mem_read(sockreg, W5100_Sn_IR)) & irmask));
// clear status
w5100_mem_write(sockreg, W5100_Sn_IR, irmask);
if (ir & _BV(W5100_Sn_IR_TIMEOUT)) {
sock = SOCK_ERR_TIMEOUT;
} else if ((ir & _BV(W5100_Sn_IR_DISCON)) && !(ir & _BV(W5100_Sn_IR_CON))) {
sock = SOCK_ERR_CLOSED;
}
break;
}
}
// no hardware sock availible (there are only 4 in w5100)
if (sock == 4) {
sock = SOCK_ERR_UNAVAILIBLE;
}
return sock;
}
#endif
uint8_t sock_write(uint8_t sockfd, char *data, uint8_t len)
{
return _sock_write(sockfd, data, len, W5100_WRITE);
}
uint8_t sock_write_P(uint8_t sockfd, PGM_P data, uint8_t len)
{
return _sock_write(sockfd, (void *)data, len, W5100_WRITE_P);
}
uint8_t sock_read(uint8_t sockfd, char *buffer, uint8_t len)
{
uint8_t sockreg = W5100_SOCKET_REG + sockfd;
uint8_t bytes_read = 0;
// block until at least one byte is read, or connection closed
while (len && !(bytes_read = w5100_sock_rw(sockfd, buffer, len, W5100_READ)) &&
w5100_mem_read(sockreg, W5100_Sn_SR) == W5100_SOCK_ESTABLISHED);
return bytes_read;
}
void sock_close(uint8_t sockfd)
{
if (sockfd & 0xF0) {
// we're closing a listening pseduo socket
// need to close its underlying connections
uint8_t n;
for (n = 0 ; n < 4; n++) {
if (sockfd & _BV(n)) {
sock_close(n);
}
}
} else {
// close the W5100 sock
w5100_mem_write(W5100_SOCKET_REG + sockfd, W5100_Sn_CR, W5100_DISCON);
// drain any unread data
w5100_drain(sockfd);
// mark the socket as avalible for use
// FIXME, broken, don't return the socket if it
// belongs to a pool used by and active listener
allocated_socks &= ~_BV(sockfd);
}
}
uint8_t sock_select(uint8_t readsockfds, uint8_t timeout)
{
_sock_cleanup_discon(readsockfds >> 4);
uint8_t result = 0;
uint8_t n;
// wait for at least one sock to have event
while (!result) {
for (n = 0; n < 8; n++) {
// the 4 LSB represent real hardware sockets
if (readsockfds & _BV(n) & 0x0F) {
uint8_t sockreg = W5100_SOCKET_REG + n;
// event of interest if discon or at least one byte readable
if (w5100_mem_read(sockreg, W5100_Sn_RX_RSR1) > 0 ||
w5100_mem_read(sockreg, W5100_Sn_RX_RSR0) > 0 ||
w5100_mem_read(sockreg, W5100_Sn_IR) & _BV(W5100_Sn_IR_DISCON)) {
result |= _BV(n);
}
// the 4 MSB are pseduo socks were waiting for connect
} else if (readsockfds & _BV(n)) {
uint8_t sockreg = W5100_SOCKET_REG + n - 4;
// has someone connected?
if (w5100_mem_read(sockreg, W5100_Sn_IR) & _BV(W5100_Sn_IR_CON)) {
result |= _BV(n);
}
}
}
if (!timeout--) {
break;
}
}
return result;
}