-
Notifications
You must be signed in to change notification settings - Fork 93
/
eth_w5500.cc
1532 lines (1389 loc) · 63.5 KB
/
eth_w5500.cc
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ########################################################################
PICSimLab - Programmable IC Simulator Laboratory
########################################################################
Copyright (c) : 2010-2024 Luis Claudio Gambôa Lopes <[email protected]>
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, 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., 675 Mass Ave, Cambridge, MA 02139, USA.
For e-mail suggestions : [email protected]
######################################################################## */
#include "eth_w5500.h"
#ifndef _WIN_
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#else
#include <winsock2.h>
#include <ws2tcpip.h>
typedef unsigned char u_int8_t;
typedef unsigned short u_int16_t;
typedef unsigned int u_int32_t;
#define SHUT_RDWR SD_BOTH
#define MSG_NOSIGNAL 0
#endif
#define INVALID_SOCKET_VALUE -1
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
// #define DEBUG
// #define DUMP
// #define TEXTDUMP
#define dprintf \
if (1) { \
} else \
printf
#define dsprintf \
if (1) { \
} else \
printf
#define BSB ((eth->control & 0xF8) >> 3)
#define RWB ((eth->control & 0x04) >> 2)
#define OM b(eth->control & 0x03)
#define readWord(x, addr) ((x[addr] << 8) | x[addr + 1])
#define writeWord(x, addr, val) \
x[addr] = (val) >> 8; \
x[addr + 1] = (val) & 0x00FF
void setblock(int sock_descriptor);
void setnblock(int sock_descriptor);
#define CONN_TIMEOUT 100000L // 10 seconds
#ifdef DEBUG
static const char* debug_BCOMMON(eth_w5500_t* eth) {
switch (eth->addr) {
case CR_MR:
return "B_COMMON CR_MR";
break;
case CR_GAR0:
return "B_COMMON CR_GAR0";
break;
case CR_GAR1:
return "B_COMMON CR_GAR1";
break;
case CR_GAR2:
return "B_COMMON CR_GAR2";
break;
case CR_GAR3:
return "B_COMMON CR_GAR3";
break;
case CR_SUBR0:
return "B_COMMON CR_SUBR0";
break;
case CR_SUBR1:
return "B_COMMON CR_SUBR1";
break;
case CR_SUBR2:
return "B_COMMON CR_SUBR2";
break;
case CR_SUBR3:
return "B_COMMON CR_SUBR3";
break;
case CR_SHAR0:
return "B_COMMON CR_SHAR0";
break;
case CR_SHAR1:
return "B_COMMON CR_SHAR1";
break;
case CR_SHAR2:
return "B_COMMON CR_SHAR2";
break;
case CR_SHAR3:
return "B_COMMON CR_SHAR3";
break;
case CR_SHAR4:
return "B_COMMON CR_SHAR4";
break;
case CR_SHAR5:
return "B_COMMON CR_SHAR5";
break;
case CR_SIPR0:
return "B_COMMON CR_SIPR0";
break;
case CR_SIPR1:
return "B_COMMON CR_SIPR1";
break;
case CR_SIPR2:
return "B_COMMON CR_SIPR2";
break;
case CR_SIPR3:
return "B_COMMON CR_SIPR3";
break;
case CR_INTLEVEL0:
return "B_COMMON CR_INTLEVEL0";
break;
case CR_INTLEVEL1:
return "B_COMMON CR_INTLEVEL1";
break;
case CR_IR:
return "B_COMMON CR_IR";
break;
case CR_IMR:
return "B_COMMON CR_IMR";
break;
case CR_SIR:
return "B_COMMON CR_SIR";
break;
case CR_SIMR:
return "B_COMMON CR_SIMR";
break;
case CR_RTR0:
return "B_COMMON CR_RTR0";
break;
case CR_RTR1:
return "B_COMMON CR_RTR1";
break;
case CR_RCR:
return "B_COMMON CR_RCR";
break;
case CR_PTIMER:
return "B_COMMON CR_PTIMER";
break;
case CR_PMAGIC:
return "B_COMMON CR_PMAGIC";
break;
case CR_PHAR0:
return "B_COMMON CR_PHAR0";
break;
case CR_PHAR1:
return "B_COMMON CR_PHAR1";
break;
case CR_PHAR2:
return "B_COMMON CR_PHAR2";
break;
case CR_PHAR3:
return "B_COMMON CR_PHAR3";
break;
case CR_PHAR4:
return "B_COMMON CR_PHAR4";
break;
case CR_PHAR5:
return "B_COMMON CR_PHAR5";
break;
case CR_PSID0:
return "B_COMMON CR_PSID0";
break;
case CR_PSID1:
return "B_COMMON CR_PSID1";
break;
case CR_PMRU0:
return "B_COMMON CR_PMRU0";
break;
case CR_PMRU1:
return "B_COMMON CR_PMRU1";
break;
case CR_UIPR0:
return "B_COMMON CR_UIPR0";
break;
case CR_UIPR1:
return "B_COMMON CR_UIPR1";
break;
case CR_UIPR2:
return "B_COMMON CR_UIPR2";
break;
case CR_UIPR3:
return "B_COMMON CR_UIPR3";
break;
case CR_UPORTR0:
return "B_COMMON CR_UPORTR0";
break;
case CR_UPORTR1:
return "B_COMMON CR_UPORTR1";
break;
case CR_PHYCFGR:
return "B_COMMON CR_PHYCFGR";
break;
case CR_VERSIONR:
return "B_COMMON CR_VERSIONR";
break;
default:
return "B_COMMON ERROR";
break;
}
}
static const char* debug_B_SCKnRG(unsigned char n, eth_w5500_t* eth) {
static char buff[100];
sprintf(buff, "B_SCK%iRG ", n);
switch (eth->addr) {
case Sn_MR:
strcat(buff, "Sn_MR");
break;
case Sn_CR:
strcat(buff, "Sn_CR");
break;
case Sn_IR:
strcat(buff, "Sn_IR");
break;
case Sn_SR:
strcat(buff, "Sn_SR");
break;
case Sn_PORT0:
strcat(buff, "Sn_PORT0");
break;
case Sn_PORT1:
strcat(buff, "Sn_PORT1");
break;
case Sn_DHAR0:
strcat(buff, "Sn_DHAR0");
break;
case Sn_DHAR1:
strcat(buff, "Sn_DHAR1");
break;
case Sn_DHAR2:
strcat(buff, "Sn_DHAR2");
break;
case Sn_DHAR3:
strcat(buff, "Sn_DHAR3");
break;
case Sn_DHAR4:
strcat(buff, "Sn_DHAR4");
break;
case Sn_DHAR5:
strcat(buff, "Sn_DHAR5");
break;
case Sn_DIPR0:
strcat(buff, "Sn_DIPR0");
break;
case Sn_DIPR1:
strcat(buff, "Sn_DIPR1");
break;
case Sn_DIPR2:
strcat(buff, "Sn_DIPR2");
break;
case Sn_DIPR3:
strcat(buff, "Sn_DIPR3");
break;
case Sn_DPORT0:
strcat(buff, "Sn_DPORT0");
break;
case Sn_DPORT1:
strcat(buff, "Sn_DPORT1");
break;
case Sn_MSSR0:
strcat(buff, "Sn_MSSR0");
break;
case Sn_MSSR1:
strcat(buff, "Sn_MSSR1");
break;
case Sn_TOS:
strcat(buff, "Sn_TOS");
break;
case Sn_TTL:
strcat(buff, "Sn_TTL");
break;
case Sn_RXBUF_SIZE:
strcat(buff, "Sn_RXBUF_SIZE");
break;
case Sn_TXBUF_SIZE:
strcat(buff, "Sn_TXBUF_SIZE");
break;
case Sn_TX_FSR0:
strcat(buff, "Sn_TX_FSR0");
break;
case Sn_TX_FSR1:
strcat(buff, "Sn_TX_FSR1");
break;
case Sn_TX_RD0:
strcat(buff, "Sn_TX_RD0");
break;
case Sn_TX_RD1:
strcat(buff, "Sn_TX_RD1");
break;
case Sn_TX_WR0:
strcat(buff, "Sn_TX_WR0");
break;
case Sn_TX_WR1:
strcat(buff, "Sn_TX_WR1");
break;
case Sn_RX_RSR0:
strcat(buff, "Sn_RX_RSR0");
break;
case Sn_RX_RSR1:
strcat(buff, "Sn_RX_RSR1");
break;
case Sn_RX_RD0:
strcat(buff, "Sn_RX_RD0");
break;
case Sn_RX_RD1:
strcat(buff, "Sn_RX_RD1");
break;
case Sn_RX_WR0:
strcat(buff, "Sn_RX_WR0");
break;
case Sn_RX_WR1:
strcat(buff, "Sn_RX_WR1");
break;
case Sn_IMR:
strcat(buff, "Sn_IMR");
break;
case Sn_FRAG0:
strcat(buff, "Sn_FRAG0");
break;
case Sn_FRAG1:
strcat(buff, "Sn_FRAG1");
break;
case Sn_KPALVTR:
strcat(buff, "Sn_KPALVTR");
break;
default:
strcat(buff, "ERROR");
break;
}
return buff;
}
static const char* debug_BSB(eth_w5500_t* eth) {
switch ((eth->control & 0xF8) >> 3) {
case B_COMMON:
return debug_BCOMMON(eth);
break;
case B_SCK0RG:
return debug_B_SCKnRG(0, eth);
break;
case B_SCK0TX:
return "B_SCK0TX";
break;
case B_SCK0RX:
return "B_SCK0RX";
break;
// case B_RESERV: return "B_RESERV";break;
case B_SCK1RG:
return debug_B_SCKnRG(1, eth);
break;
case B_SCK1TX:
return "B_SCK1TX";
break;
case B_SCK1RX:
return "B_SCK1RX";
break;
// case B_RESERV: return "B_RESERV";break;
case B_SCK2RG:
return debug_B_SCKnRG(2, eth);
break;
case B_SCK2TX:
return "B_SCK2TX";
break;
case B_SCK2RX:
return "B_SCK2RX";
break;
// case B_RESERV: return "B_RESERV";break;
case B_SCK3RG:
return debug_B_SCKnRG(3, eth);
break;
case B_SCK3TX:
return "B_SCK3TX";
break;
case B_SCK3RX:
return "B_SCK3RX";
break;
// case B_RESERV: return "B_RESERV";break;
case B_SCK4RG:
return debug_B_SCKnRG(4, eth);
break;
case B_SCK4TX:
return "B_SCK4TX";
break;
case B_SCK4RX:
return "B_SCK4RX";
break;
// case B_RESERV: return "B_RESERV";break;
case B_SCK5RG:
return debug_B_SCKnRG(5, eth);
break;
case B_SCK5TX:
return "B_SCK5TX";
break;
case B_SCK5RX:
return "B_SCK5RX";
break;
// case B_RESERV: return "B_RESERV";break;
case B_SCK6RG:
return debug_B_SCKnRG(6, eth);
break;
case B_SCK6TX:
return "B_SCK6TX";
break;
case B_SCK6RX:
return "B_SCK6RX";
break;
// case B_RESERV: return "B_RESERV;break;
case B_SCK7RG:
return debug_B_SCKnRG(7, eth);
break;
case B_SCK7TX:
return "B_SCK7TX";
break;
case B_SCK7RX:
return "B_SCK7RX";
break;
default:
return "ERROR";
break;
}
}
#endif
void eth_w5500_rst(eth_w5500_t* eth) {
bitbang_spi_rst(ð->bb_spi);
eth->control = 0;
eth->addr = 0;
eth->active = 0;
memset(eth->Common, 0, 0x40);
memset(eth->Socket, 0, 0x30 * 8);
memset(eth->status, 0, 8);
memset(eth->bindp, 0, 8 * 2);
eth->Common[CR_VERSIONR] = 0x04; // version
if (eth->link) {
eth->Common[CR_PHYCFGR] |= 0x01; // link on
} else {
eth->Common[CR_PHYCFGR] &= ~0x01; // link off
}
// TODO add support to buffer size different of 2k
for (int n = 0; n < 8; n++) {
eth->sockfd[n] = INVALID_SOCKET_VALUE;
eth->Socket[n][Sn_TXBUF_SIZE] = 2;
eth->Socket[n][Sn_RXBUF_SIZE] = 2;
eth->RX_size[n] = eth->Socket[n][Sn_RXBUF_SIZE] * 1024;
eth->TX_size[n] = eth->Socket[n][Sn_TXBUF_SIZE] * 1024;
eth->RX_mask[n] = eth->RX_size[n] - 1;
eth->TX_mask[n] = eth->TX_size[n] - 1;
writeWord(eth->Socket[n], Sn_TX_FSR0, eth->TX_size[n]);
eth->RX_ptr[n] = eth->RX_size[n] * n;
eth->TX_ptr[n] = eth->TX_size[n] * n;
eth->listenfd_map[n] = 0;
}
dprintf("rst w5500\n");
}
void eth_w5500_init(eth_w5500_t* eth, unsigned char linkon) {
eth->link = linkon;
bitbang_spi_init(ð->bb_spi);
eth_w5500_rst(eth);
for (int n = 0; n < 8; n++) {
eth->listenfd[n] = INVALID_SOCKET_VALUE;
eth->listenfd_port[n] = 0;
}
dprintf("init w5500\n");
}
void eth_w5500_end(eth_w5500_t* eth) {
for (int n = 0; n < 8; n++) {
if (eth->sockfd[n] != INVALID_SOCKET_VALUE) {
close(eth->sockfd[n]);
}
if (eth->listenfd[n] != INVALID_SOCKET_VALUE) {
close(eth->listenfd[n]);
}
}
}
void eth_w5500_set_link(eth_w5500_t* eth, unsigned char on) {
eth->link = on;
if (on) {
eth->Common[CR_PHYCFGR] |= 0x01;
} else {
eth->Common[CR_PHYCFGR] &= ~0x01;
}
}
unsigned char eth_w5500_get_leds(eth_w5500_t* eth) {
return (eth->Common[CR_PHYCFGR] & 0x03) | ((eth->active > 0) << 3);
}
#ifdef DUMP
static int scont = 0;
#endif
// http://www.tcpipguide.com/free/t_DHCPMessageFormat.htm
typedef struct dhcp {
// fixed 240 bytes
u_int8_t opcode; // Operation Code
u_int8_t htype; // Hardware Type
u_int8_t hlen; // Hardware Address Length
u_int8_t hops; // Hops
u_int32_t xid; // Transaction Identifier
u_int16_t secs; // Seconds
u_int16_t flags;
u_int8_t ciaddr[4]; // Client IP Address
u_int8_t yiaddr[4]; // Your IP Address
u_int8_t siaddr[4]; // Server IP Address
u_int8_t giaddr[4]; // Gateway IP Address
u_int8_t chaddr[16]; // Client Hardware Address
char bp_sname[64]; // Server Name
char bp_file[128]; // Boot Filename
u_int32_t magic_cookie;
// variable
u_int8_t bp_options[35]; // Options
} dhcp_t;
/* DHCP message type */
#define DHCP_DISCOVER 1
#define DHCP_OFFER 2
#define DHCP_REQUEST 3
#define DHCP_DECLINE 4
#define DHCP_ACK 5
#define DHCP_NAK 6
#define DHCP_RELEASE 7
#define DHCP_INFORM 8
#define dhcppadOption 0
#define dhcpsubnetMask 1
#define dhcptimerOffset 2
#define dhcproutersOnSubnet 3
#define dhcpdns 6
#define dhcphostName 12
#define dhcpdomainName 15
#define dhcpRequestedIPaddr 50
#define dhcpIPaddrLeaseTime 51
#define dhcpMessageType 53
#define dhcpServerIdentifier 54
#define dhcpParamRequest 55
#define dhcpT1value 58
#define dhcpT2value 59
#define dhcpClientIdentifier 61
#define dhcpendOption 255
static void eth_w5500_fake_dhcp_reply(eth_w5500_t* eth, int n) {
char temp_buff[0x4000];
char skt_addr[30];
dhcp_t* dhcp_pack = (dhcp_t*)ð->TX_Mem[eth->TX_ptr[n]];
int size;
unsigned short addr;
unsigned short addr_base;
int s;
int i;
#ifdef DUMP
char sfname[30];
FILE* fouts;
#endif
if (!eth->link)
return;
dhcp_pack->opcode = 2; // reply
dhcp_pack->yiaddr[0] = 192;
dhcp_pack->yiaddr[1] = 168;
dhcp_pack->yiaddr[2] = 0;
dhcp_pack->yiaddr[3] = 10;
dhcp_pack->siaddr[0] = 192;
dhcp_pack->siaddr[1] = 168;
dhcp_pack->siaddr[2] = 0;
dhcp_pack->siaddr[3] = 1;
dhcp_pack->giaddr[0] = 192;
dhcp_pack->giaddr[1] = 168;
dhcp_pack->giaddr[2] = 0;
dhcp_pack->giaddr[3] = 1;
int op = 0;
dhcp_pack->bp_options[op++] = dhcpMessageType;
dhcp_pack->bp_options[op++] = 0x01;
switch (dhcp_pack->bp_options[2]) {
case DHCP_DISCOVER:
dhcp_pack->bp_options[op++] = DHCP_OFFER;
break;
case DHCP_REQUEST:
dhcp_pack->bp_options[op++] = DHCP_ACK;
break;
}
dhcp_pack->bp_options[op++] = dhcpsubnetMask;
dhcp_pack->bp_options[op++] = 4;
dhcp_pack->bp_options[op++] = 255;
dhcp_pack->bp_options[op++] = 255;
dhcp_pack->bp_options[op++] = 255;
dhcp_pack->bp_options[op++] = 0;
dhcp_pack->bp_options[op++] = dhcproutersOnSubnet;
dhcp_pack->bp_options[op++] = 4;
dhcp_pack->bp_options[op++] = 192;
dhcp_pack->bp_options[op++] = 168;
dhcp_pack->bp_options[op++] = 0;
dhcp_pack->bp_options[op++] = 1;
dhcp_pack->bp_options[op++] = dhcpdns; // need to be real to work
dhcp_pack->bp_options[op++] = 4;
dhcp_pack->bp_options[op++] = 8;
dhcp_pack->bp_options[op++] = 8;
dhcp_pack->bp_options[op++] = 8;
dhcp_pack->bp_options[op++] = 8;
/*
dhcp_pack->bp_options[op++] = dhcpdomainName;
dhcp_pack->bp_options[op++] = 15;
dhcp_pack->bp_options[op++] = 'P';
dhcp_pack->bp_options[op++] = 'I';
dhcp_pack->bp_options[op++] = 'C';
dhcp_pack->bp_options[op++] = 'S';
dhcp_pack->bp_options[op++] = 'i';
dhcp_pack->bp_options[op++] = 'm';
dhcp_pack->bp_options[op++] = 'L';
dhcp_pack->bp_options[op++] = 'a';
dhcp_pack->bp_options[op++] = 'b';
dhcp_pack->bp_options[op++] = 0x12;
dhcp_pack->bp_options[op++] = 0x23;
dhcp_pack->bp_options[op++] = 0x34;
dhcp_pack->bp_options[op++] = 0x45;
dhcp_pack->bp_options[op++] = 0x56;
dhcp_pack->bp_options[op++] = 0x67;
*/
dhcp_pack->bp_options[op++] = dhcpT1value;
dhcp_pack->bp_options[op++] = 4;
dhcp_pack->bp_options[op++] = 0x00;
dhcp_pack->bp_options[op++] = 0x00;
dhcp_pack->bp_options[op++] = 0x03;
dhcp_pack->bp_options[op++] = 0x84;
dhcp_pack->bp_options[op++] = dhcpT2value;
dhcp_pack->bp_options[op++] = 4;
dhcp_pack->bp_options[op++] = 0x00;
dhcp_pack->bp_options[op++] = 0x00;
dhcp_pack->bp_options[op++] = 0x03;
dhcp_pack->bp_options[op++] = 0x84;
dhcp_pack->bp_options[op++] = dhcpendOption;
// reply fake receive
sprintf(skt_addr, "%i.%i.%i.%i", eth->Socket[n][Sn_DIPR0], eth->Socket[n][Sn_DIPR1], eth->Socket[n][Sn_DIPR2],
eth->Socket[n][Sn_DIPR3]);
eth->active = 2;
s = 240 + op;
memcpy(temp_buff + 8, dhcp_pack, s);
dsprintf("Socket %i Received %i from %s:%i\n", n, s, skt_addr, readWord(eth->Socket[n], Sn_DPORT0));
temp_buff[0] = eth->Socket[n][Sn_DIPR0];
temp_buff[1] = eth->Socket[n][Sn_DIPR1];
temp_buff[2] = eth->Socket[n][Sn_DIPR2];
temp_buff[3] = eth->Socket[n][Sn_DIPR3];
temp_buff[4] = eth->Socket[n][Sn_DPORT0];
temp_buff[5] = eth->Socket[n][Sn_DPORT1];
temp_buff[6] = s >> 8;
temp_buff[7] = s & 0x00FF;
s += 8; // add header size to total size
addr_base = (readWord(eth->Socket[n], Sn_RX_WR0));
for (i = 0; i < s; i++) {
addr = (addr_base + i) & eth->RX_mask[n];
eth->RX_Mem[eth->RX_ptr[n] + addr] = temp_buff[i];
}
writeWord(eth->Socket[n], Sn_RX_WR0, addr_base + s);
size = readWord(eth->Socket[n], Sn_RX_WR0);
size -= readWord(eth->Socket[n], Sn_RX_RD0);
if (size < 0) {
size += eth->RX_size[n];
}
writeWord(eth->Socket[n], Sn_RX_RSR0, size);
#ifdef DUMP
sprintf(sfname, "/tmp/%03i_recv_udp.bin", scont++);
fouts = fopen_UTF8(sfname, "wb");
fwrite(temp_buff, s, 1, fouts);
fclose(fouts);
#endif
#ifdef TEXTDUMP
temp_buff[s] = 0;
printf(temp_buff);
#endif
}
static unsigned int conn_timeout[8] = {0, 0, 0, 0, 0, 0, 0, 0};
void eth_w5500_process(eth_w5500_t* eth) {
char temp_buff[0x4000];
char skt_addr[30];
unsigned short skt_port;
struct sockaddr_in cli;
socklen_t clilen = sizeof(cli);
int size;
unsigned short addr;
unsigned short addr_base;
int s;
int i;
int j;
socklen_t len;
struct sockaddr_in serv;
#ifdef DUMP
char sfname[30];
FILE* fouts;
#endif
if (eth->active)
eth->active--;
if (!eth->link)
return;
for (int n = 0; n < 8; n++) {
switch (eth->Socket[n][Sn_SR]) {
case SOCK_SYNSENT:
sprintf(skt_addr, "%i.%i.%i.%i", eth->Socket[n][Sn_DIPR0], eth->Socket[n][Sn_DIPR1],
eth->Socket[n][Sn_DIPR2], eth->Socket[n][Sn_DIPR3]);
skt_port = readWord(eth->Socket[n], Sn_DPORT0);
memset(&serv, 0, sizeof(serv));
serv.sin_family = AF_INET;
serv.sin_addr.s_addr = inet_addr(skt_addr);
serv.sin_port = htons(skt_port);
if (connect(eth->sockfd[n], (sockaddr*)&serv, sizeof(serv)) < 0) {
#ifndef _WIN_
if ((errno == EINPROGRESS) || (errno == EALREADY))
#else
if ((WSAGetLastError() == WSAEINPROGRESS) || (WSAGetLastError() == WSAEALREADY) ||
(WSAGetLastError() == WSAEWOULDBLOCK))
#endif
{
conn_timeout[n]++;
if (conn_timeout[n] < CONN_TIMEOUT) {
break;
}
}
#ifdef _WIN_
if (WSAGetLastError() != WSAEISCONN) {
#endif
printf("eth_w5500: connect error : %s \n", strerror(errno));
eth->Socket[n][Sn_SR] = SOCK_CLOSED;
eth->status[n] = ER_CONN;
break;
#ifdef _WIN_
}
#endif
}
dsprintf("Socket %i Connected to %s:%i\n", n, skt_addr, skt_port);
eth->Socket[n][Sn_SR] = SOCK_ESTABLISHED;
eth->Socket[n][Sn_IR] |= 0x01;
writeWord(eth->Socket[n], Sn_TX_RD0, 0);
writeWord(eth->Socket[n], Sn_TX_WR0, 0);
writeWord(eth->Socket[n], Sn_RX_RD0, 0);
writeWord(eth->Socket[n], Sn_RX_WR0, 0);
writeWord(eth->Socket[n], Sn_TX_FSR0, eth->Socket[n][Sn_TXBUF_SIZE] * 1024);
writeWord(eth->Socket[n], Sn_RX_RSR0, 0);
setnblock(eth->sockfd[n]);
break;
case SOCK_LISTEN:
if ((eth->sockfd[n] = accept(eth->listenfd[eth->listenfd_map[n]], (sockaddr*)&cli, &clilen)) < 0) {
// printf ("eth_w5500: accept error : %s \n", strerror (errno));
eth->sockfd[n] = INVALID_SOCKET_VALUE;
} else {
setnblock(eth->sockfd[n]);
eth->Socket[n][Sn_SR] = SOCK_ESTABLISHED;
dsprintf("Socket %i Connected\n", n);
writeWord(eth->Socket[n], Sn_TX_RD0, 0);
writeWord(eth->Socket[n], Sn_TX_WR0, 0);
writeWord(eth->Socket[n], Sn_RX_RD0, 0);
writeWord(eth->Socket[n], Sn_RX_WR0, 0);
writeWord(eth->Socket[n], Sn_TX_FSR0, eth->Socket[n][Sn_TXBUF_SIZE] * 1024);
writeWord(eth->Socket[n], Sn_RX_RSR0, 0);
eth->bindp[n] = 0;
}
break;
case SOCK_ESTABLISHED:
size = eth->RX_size[n] - readWord(eth->Socket[n], Sn_RX_RSR0);
if (size < 0)
size = 0;
if (size) {
if ((s = recv(eth->sockfd[n], temp_buff, size, 0)) < 0) {
#ifndef _WIN_
if (errno != EAGAIN)
#else
if (WSAGetLastError() != WSAEWOULDBLOCK)
#endif
{
printf("eth_w5500: recv tcp error :[%i] %s \n", errno, strerror(errno));
eth->Socket[n][Sn_SR] = SOCK_CLOSED;
eth->status[n] = ER_RECV;
}
}
if (s > 0) {
eth->active = 2;
eth->Socket[n][Sn_IR] |= 0x04;
addr_base = (readWord(eth->Socket[n], Sn_RX_WR0));
for (i = 0; i < s; i++) {
addr = (addr_base + i) & eth->RX_mask[n];
eth->RX_Mem[eth->RX_ptr[n] + addr] = temp_buff[i];
}
dsprintf("eth_w5500: Socket %i Received %i\n", n, s);
writeWord(eth->Socket[n], Sn_RX_WR0, readWord(eth->Socket[n], Sn_RX_WR0) + s);
size = readWord(eth->Socket[n], Sn_RX_WR0);
size -= readWord(eth->Socket[n], Sn_RX_RD0);
if (size < 0) {
size += eth->RX_size[n];
}
writeWord(eth->Socket[n], Sn_RX_RSR0, size);
#ifdef DUMP
sprintf(sfname, "/tmp/%03i_recv_tcp.bin", scont++);
fouts = fopen_UTF8(sfname, "wb");
fwrite(temp_buff, s, 1, fouts);
fclose(fouts);
#endif
#ifdef TEXTDUMP
temp_buff[s] = 0;
printf(temp_buff);
#endif
}
}
break;
case SOCK_UDP:
sprintf(skt_addr, "%i.%i.%i.%i", eth->Socket[n][Sn_DIPR0], eth->Socket[n][Sn_DIPR1],
eth->Socket[n][Sn_DIPR2], eth->Socket[n][Sn_DIPR3]);
skt_port = readWord(eth->Socket[n], Sn_DPORT0);
memset(&serv, 0, sizeof(serv));
// Filling server information
serv.sin_family = AF_INET;
serv.sin_port = htons(skt_port);
serv.sin_addr.s_addr = inet_addr(skt_addr);
for (j = 0; j < 8; j++) {
if ((eth->listenfd_port[j] == 0) || (eth->listenfd_port[j] == skt_port)) {
eth->listenfd_map[n] = j;
eth->listenfd_port[j] = skt_port;
break;
}
}
if ((eth->listenfd[eth->listenfd_map[n]] == INVALID_SOCKET_VALUE) && !strcmp(skt_addr, "0.0.0.0") &&
(skt_port == 0)) {
skt_port = readWord(eth->Socket[n], Sn_PORT0);
if (skt_port < 2000) // avoid system services
{
skt_port += 2000;
}
eth->bindp[n] = skt_port;
memset(&serv, 0, sizeof(serv));
// Filling server information
serv.sin_family = AF_INET;
serv.sin_port = htons(skt_port);
serv.sin_addr.s_addr = htonl(INADDR_ANY);
eth->listenfd[eth->listenfd_map[n]] = eth->sockfd[n];
if (bind(eth->listenfd[eth->listenfd_map[n]], (sockaddr*)&serv, sizeof(serv))) {
printf("eth_w5500: bind error : %s \n", strerror(errno));
close(eth->sockfd[n]);
eth->sockfd[n] = INVALID_SOCKET_VALUE;
eth->Socket[n][Sn_SR] = SOCK_CLOSED;
eth->status[n] = ER_BIND;
eth->bindp[n] = 0;
eth->listenfd[eth->listenfd_map[n]] = INVALID_SOCKET_VALUE;
eth->listenfd_port[eth->listenfd_map[n]] = 0;
break;
}
dsprintf("Socket %i listen on port %i \n", n, skt_port);
}
size = (eth->Socket[n][Sn_RXBUF_SIZE] * 1024);
if (size < 0)
size = 0;
if (size) {
len = sizeof(serv);
if ((s = recvfrom(eth->sockfd[n], &temp_buff[8],
eth->RX_size[n] - readWord(eth->Socket[n], Sn_RX_RSR0),
0 /*MSG_DONTWAIT MSG_WAITALL*/, (struct sockaddr*)&serv, &len)) < 0) {
#ifndef _WIN_
if (errno != EAGAIN)
#else
if ((WSAGetLastError() != WSAEWOULDBLOCK) && (WSAGetLastError() != WSAEINVAL))
#endif
{
printf("eth_w5500: recv udp error : %s \n", strerror(errno));
eth->Socket[n][Sn_SR] = SOCK_CLOSED;
eth->status[n] = ER_RECV;
}
}
if (s > 0) {
eth->active = 2;
eth->Socket[n][Sn_IR] |= 0x04;
strncpy(skt_addr, inet_ntoa(serv.sin_addr), 29);
skt_port = ntohs(serv.sin_port);
eth->Socket[n][Sn_DIPR0] = (serv.sin_addr.s_addr & 0x000000FF);
eth->Socket[n][Sn_DIPR1] = (serv.sin_addr.s_addr & 0x0000FF00) >> 8;
eth->Socket[n][Sn_DIPR2] = (serv.sin_addr.s_addr & 0x00FF0000) >> 16;
eth->Socket[n][Sn_DIPR3] = (serv.sin_addr.s_addr & 0xFF000000) >> 24;
eth->Socket[n][Sn_DPORT0] = skt_port >> 8;
eth->Socket[n][Sn_DPORT1] = skt_port & 0xFF;
dsprintf("eth_w5500: Socket %i Received %i from %s:%i\n", n, s, skt_addr, skt_port);
temp_buff[0] = eth->Socket[n][Sn_DIPR0];
temp_buff[1] = eth->Socket[n][Sn_DIPR1];
temp_buff[2] = eth->Socket[n][Sn_DIPR2];
temp_buff[3] = eth->Socket[n][Sn_DIPR3];
temp_buff[4] = eth->Socket[n][Sn_DPORT0];
temp_buff[5] = eth->Socket[n][Sn_DPORT1];
temp_buff[6] = s >> 8;
temp_buff[7] = s & 0x00FF;
s += 8; // add header size to total size
addr_base = (readWord(eth->Socket[n], Sn_RX_WR0));
for (i = 0; i < s; i++) {
addr = (addr_base + i) & eth->RX_mask[n];
eth->RX_Mem[eth->RX_ptr[n] + addr] = temp_buff[i];
}
writeWord(eth->Socket[n], Sn_RX_WR0, readWord(eth->Socket[n], Sn_RX_WR0) + s);
size = readWord(eth->Socket[n], Sn_RX_WR0);
size -= readWord(eth->Socket[n], Sn_RX_RD0);
if (size < 0) {
size += eth->RX_size[n];