-
Notifications
You must be signed in to change notification settings - Fork 2
/
WIZnetInterface.cpp
907 lines (796 loc) · 26.7 KB
/
WIZnetInterface.cpp
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
/**
******************************************************************************
* @file WIZnetInterface.h
* @author Justin Kim (modified version from Sergei G (https://os.mbed.com/users/sgnezdov/))
* @brief Implementation file of the NetworkStack for the WIZnet Ethernet Device
******************************************************************************
* @attention
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, WIZnet SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
* <h2><center>© COPYRIGHT 2019 WIZnet Co.,Ltd.</center></h2>
******************************************************************************
*/
#include "mbed.h"
#include "WIZnetInterface.h"
#include "ip6string.h"
static uint8_t WIZNET_DEFAULT_TESTMAC[6] = {0x00, 0x08, 0xdc, 0x19, 0x85, 0xa8};
//static int udp_local_port = 0;
#define SKT(h) ((wiznet_socket*)h)
#define WIZNET_WAIT_TIMEOUT 400
#define WIZNET_WAIT_TIMEOUT1 1000
#define WIZNET_ACCEPT_TIMEOUT 300000 //5 mins timeout, retrun NSAPI_ERROR_WOULD_BLOCK if there is no connection during 5 mins
#if MBED_CONF_WIZNET_DEBUG
#define DBG(...) do{debug("[%s:%d] \n", __PRETTY_FUNCTION__,__LINE__);debug(__VA_ARGS__);} while(0);
#else
#define DBG(...) while(0);
#define INFO(...) do{debug("[%s:%d]", __PRETTY_FUNCTION__,__LINE__);debug(__VA_ARGS__);} while(0);
#endif
#include "DHCPClient.h"
DHCPClient dhcp;
/**
* @brief Defines a custom MAC address
* @note Have to be unique within the connected network!
* Modify the mac array items as needed.
* @param mac A 6-byte array defining the MAC address
* @retval
*/
/* Interface implementation */
#if defined(TARGET_WIZwiki_W7500) || defined(TARGET_WIZwiki_W7500ECO) || defined(TARGET_WIZwiki_W7500P)
NetworkInterface *NetworkInterface::get_default_instance()
{
WIZnetInterface eth;
return ð
}
#else
#if defined MBED_CONF_WIZNET_DEFAULT_NETWORK
NetworkInterface *NetworkInterface::get_default_instance()
{
//static WIZnetInterface eth(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS, D15);
static WIZnetInterface eth(WIZNET_MOSI, WIZNET_MISO, WIZNET_SCK, WIZNET_CS, WIZNET_RESET);
return ð
}
#endif
WIZnetInterface::WIZnetInterface(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset) :
_wiznet(mosi, miso, sclk, cs, reset)
{
_wiznet.reset();
ip_set = false;
_dhcp_enable = true;
thread_read_socket.start(callback(this, &WIZnetInterface::socket_check_read));
}
#endif
wiznet_socket* WIZnetInterface::get_sock(int fd, nsapi_protocol_t proto)
{
for (int i=0; i<MAX_SOCK_NUM ; i++) {
if (wiznet_sockets[i].fd == -1) {
wiznet_sockets[i].fd = fd;
wiznet_sockets[i].proto = proto;
wiznet_sockets[i].connected = false;
wiznet_sockets[i].callback = NULL;
wiznet_sockets[i].callback_data = NULL;
return &wiznet_sockets[i];
}
}
return NULL;
}
void WIZnetInterface::init_socks()
{
for (int i=0; i<MAX_SOCK_NUM ; i++) {
wiznet_sockets[i].fd = -1;
wiznet_sockets[i].proto = NSAPI_TCP;
wiznet_sockets[i].connected = false;
wiznet_sockets[i].callback = NULL;
wiznet_sockets[i].callback_data = NULL;
}
dns.setup(get_stack());
//initialize the socket isr
//_daemon = new Thread(osPriorityNormal, 1024);
//_daemon->start(callback(this, &W5500Interface::daemon));
}
uint32_t WIZnetInterface:: str_to_ip(const char* str)
{
uint32_t ip = 0;
char* p = (char*)str;
for(int i = 0; i < 4; i++) {
ip |= atoi(p);
p = strchr(p, '.');
if (p == NULL) {
break;
}
ip <<= 8;
p++;
}
return ip;
}
int WIZnetInterface::init()
{
_dhcp_enable = true;
_wiznet.reg_wr<uint32_t>(SIPR, 0x00000000); // local ip "0.0.0.0"
//_w5500.reg_wr<uint8_t>(SIMR, 0xFF); //
for (int i =0; i < 6; i++) _wiznet.mac[i] = WIZNET_DEFAULT_TESTMAC[i];
_wiznet.setmac();
init_socks();
return 0;
}
int WIZnetInterface::init(uint8_t * mac)
{
_dhcp_enable = true;
_wiznet.reg_wr<uint32_t>(SIPR, 0x00000000); // local ip "0.0.0.0"
// should set the mac address and keep the value in this class
for (int i =0; i < 6; i++) _wiznet.mac[i] = mac[i];
_wiznet.setmac();
init_socks();
return 0;
}
// add this function, because sometimes no needed MAC address in init calling.
int WIZnetInterface::init(const char* ip, const char* mask, const char* gateway)
{
_dhcp_enable = false;
_wiznet.ip = str_to_ip(ip);
strcpy(ip_string, ip);
ip_set = true;
_wiznet.netmask = str_to_ip(mask);
_wiznet.gateway = str_to_ip(gateway);
// @Jul. 8. 2014 add code. should be called to write chip.
_wiznet.setip();
init_socks();
return 0;
}
int WIZnetInterface::init(uint8_t * mac, const char* ip, const char* mask, const char* gateway)
{
_dhcp_enable = false;
//
for (int i =0; i < 6; i++) _wiznet.mac[i] = mac[i];
//
_wiznet.ip = str_to_ip(ip);
strcpy(ip_string, ip);
ip_set = true;
_wiznet.netmask = str_to_ip(mask);
_wiznet.gateway = str_to_ip(gateway);
#ifdef USE_W6100
_wiznet.init_chip();
#endif
// @Jul. 8. 2014 add code. should be called to write chip.
_wiznet.setmac();
_wiznet.setip();
init_socks();
return 0;
}
#ifdef USE_W6100
bool WIZnetInterface::init_ipv6(const char *lla, const char *gua, const char *sn6, const char *gw6)
{
//to do
if(stoip6(lla, strlen(lla), _wiznet.lla) == false)
{
DBG("[Error] lla trans data error \n");
}
if(stoip6(gua, strlen(gua), _wiznet.gua) == false)
{
DBG("[Error] gua trans data error \n");
}
if(stoip6(sn6, strlen(sn6), _wiznet.sn6) == false)
{
DBG("[Error] sn6 trans data error \n");
}
if(stoip6(gw6, strlen(gw6), _wiznet.gw6) == false)
{
DBG("[Error] gw6 trans data error \n");
}
_wiznet.setip6();
return true;
}
bool WIZnetInterface::set_dns6(uint8_t *dns6)
{
//to do
return true;
}
#endif
void WIZnetInterface::socket_check_read()
{
while (1) {
for (int i = 0; i < MAX_SOCK_NUM; i++) {
_mutex.lock();
for (int i=0; i<MAX_SOCK_NUM ; i++) {
if (wiznet_sockets[i].fd >= 0 && wiznet_sockets[i].callback) {
int size = _wiznet.sreg<uint16_t>(wiznet_sockets[i].fd, Sn_RX_RSR);
if (size > 0) {
//led1 = !led1;
wiznet_sockets[i].callback(wiznet_sockets[i].callback_data);
}
}
}
_mutex.unlock();
}
thread_sleep_for(1);
}
}
int WIZnetInterface::IPrenew(int timeout_ms)
{
DBG("[EasyConnect] DHCP start\n");
int err = dhcp.setup(get_stack(), _wiznet.mac, timeout_ms);
if (err == (-1)) {
DBG("[EasyConnect] Timeout.\n");
return NSAPI_ERROR_DHCP_FAILURE;
}
DBG("[EasyConnect] DHCP completed\n");
DBG("[EasyConnect] Connected, IP: %d.%d.%d.%d\r\n", dhcp.yiaddr[0], dhcp.yiaddr[1], dhcp.yiaddr[2], dhcp.yiaddr[3]);
char ip[24], gateway[24], netmask[24], dnsaddr[24];
sprintf(ip, "%d.%d.%d.%d", dhcp.yiaddr[0], dhcp.yiaddr[1], dhcp.yiaddr[2], dhcp.yiaddr[3]);
sprintf(gateway, "%d.%d.%d.%d", dhcp.gateway[0], dhcp.gateway[1], dhcp.gateway[2], dhcp.gateway[3]);
sprintf(netmask, "%d.%d.%d.%d", dhcp.netmask[0], dhcp.netmask[1], dhcp.netmask[2], dhcp.netmask[3]);
sprintf(dnsaddr, "%d.%d.%d.%d", dhcp.dnsaddr[0], dhcp.dnsaddr[1], dhcp.dnsaddr[2], dhcp.dnsaddr[3]);
init(ip, netmask, gateway);
setDnsServerIP(dnsaddr);
_dhcp_enable = true; // because this value was changed in init(ip, netmask, gateway).
return 0;
}
int WIZnetInterface::connect()
{
if (_dhcp_enable) {
init(); // init default mac address
int err = IPrenew(15000);
if (err < 0) return err;
}
if (_wiznet.setip() == false) return NSAPI_ERROR_DHCP_FAILURE;
return 0;
}
bool WIZnetInterface::setDnsServerIP(const char* ip_address)
{
return dns.set_server(ip_address);
}
bool WIZnetInterface::setMode(ProtocolMode mode)
{
temp_WizMode = mode;
DBG("Protocol Mode: %d \n", temp_WizMode);
return true;
}
int WIZnetInterface::disconnect()
{
_wiznet.disconnect();
return 0;
}
const char *WIZnetInterface::get_ip_address()
{
uint32_t ip = _wiznet.reg_rd<uint32_t>(SIPR);
snprintf(ip_string, sizeof(ip_string), "%d.%d.%d.%d", (int)((ip>>24)&0xff), (int)((ip>>16)&0xff), (int)((ip>>8)&0xff), (int)(ip&0xff));
return ip_string;
}
const char *WIZnetInterface::get_netmask()
{
uint32_t netmask = _wiznet.reg_rd<uint32_t>(SUBR);
snprintf(netmask_string, sizeof(netmask_string), "%d.%d.%d.%d", (int)((netmask>>24)&0xff), (int)((netmask>>16)&0xff), (int)((netmask>>8)&0xff), (int)(netmask&0xff));
return netmask_string;
}
const char *WIZnetInterface::get_gateway()
{
uint32_t gateway = _wiznet.reg_rd<uint32_t>(GAR);
snprintf(gateway_string, sizeof(gateway_string), "%d.%d.%d.%d", (int)((gateway>>24)&0xff), (int)((gateway>>16)&0xff), (int)((gateway>>8)&0xff), (int)(gateway&0xff));
return gateway_string;
}
const char *WIZnetInterface::get_mac_address()
{
uint8_t mac[6];
_wiznet.reg_rd_mac(SHAR, mac);
snprintf(mac_string, sizeof(mac_string), "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
return mac_string;
}
void WIZnetInterface::get_mac(uint8_t mac[6])
{
_wiznet.reg_rd_mac(SHAR, mac);
}
nsapi_error_t WIZnetInterface::socket_open(nsapi_socket_t *handle, nsapi_protocol_t proto)
{
//a socket is created the same way regardless of the protocol
int temp_port;
int sock_fd = _wiznet.new_socket();
if (sock_fd < 0) {
return NSAPI_ERROR_NO_SOCKET;
}
wiznet_socket *h = get_sock(sock_fd, proto);
if (!h) {
return NSAPI_ERROR_NO_SOCKET;
}
h->proto = proto;
h->connected = false;
h->server = false;
h->callback = NULL;
h->callback_data = NULL;
//new up an int to store the socket fd
h->Protocol_Mode = (ProtocolMode)(proto + 1);
#ifdef USE_W6100
if(temp_WizMode != 0)
{
h->Protocol_Mode = temp_WizMode;
temp_WizMode = CLOSED;
}
#endif
*handle = h;
temp_port = _wiznet.new_port();
DBG("Protocol Mode : %d , %d \n", SKT(*handle)->Protocol_Mode, temp_WizMode);
_wiznet.setLocalPort(SKT(*handle)->fd, temp_port);
_wiznet.setProtocolMode(SKT(*handle)->fd, SKT(*handle)->Protocol_Mode);
while(_wiznet.is_closed(SKT(*handle)->fd) == true)
{
_wiznet.scmd(SKT(*handle)->fd, OPEN);
}
DBG("fd: %d, port: %d \n", SKT(*handle)->fd, temp_port);
return 0;
}
//void W5500Interface::signal_event(nsapi_socket_t handle)
//{
//// DBG("fd: %d\n", SKT(handle)->fd);
//// if (SKT(handle)->callback != NULL) {
//// SKT(handle)->callback(SKT(handle)->callback_data);
//// }
// if (handle == NULL) return;
// w5500_socket *socket = (w5500_socket *)handle;
// w5500_sockets[socket->fd].callback(w5500_sockets[socket->fd].callback_data);
//}
nsapi_error_t WIZnetInterface::socket_close(nsapi_socket_t handle)
{
if (handle == NULL) return 0;
DBG("fd: %d\n", SKT(handle)->fd);
_wiznet.close(SKT(handle)->fd);
SKT(handle)->fd = -1;
return 0;
}
nsapi_error_t WIZnetInterface::socket_bind(nsapi_socket_t handle, const SocketAddress &address)
{
if ((int)handle < 0) {
return NSAPI_ERROR_DEVICE_ERROR;
}
DBG("fd: %d, port: %d\n", SKT(handle)->fd, address.get_port());
if(_wiznet.is_closed(SKT(handle)->fd) == false)
{
_wiznet.close(SKT(handle)->fd);
}
switch (SKT(handle)->proto) {
case NSAPI_UDP:
// set local port
if (address.get_port() != 0) {
_wiznet.setLocalPort( SKT(handle)->fd, address.get_port() );
} else {
//udp_local_port++;
_wiznet.setLocalPort( SKT(handle)->fd, _wiznet.new_port() );
}
// set udp protocol
_wiznet.setProtocolMode(SKT(handle)->fd, SKT(handle)->Protocol_Mode);
_wiznet.scmd(SKT(handle)->fd, OPEN);
SKT(handle)->connected = true;
/*
uint8_t tmpSn_SR;
tmpSn_SR = _w5500.sreg<uint8_t>(SKT(handle)->fd, Sn_SR);
DBG("open socket status: %2x\n", tmpSn_SR);
*/
return 0;
case NSAPI_TCP:
listen_port = address.get_port();
// set TCP protocol
_wiznet.setProtocolMode(SKT(handle)->fd, SKT(handle)->Protocol_Mode);
// set local port
_wiznet.setLocalPort( SKT(handle)->fd, address.get_port() );
// connect the network
_wiznet.scmd(SKT(handle)->fd, OPEN);
return 0;
}
return NSAPI_ERROR_DEVICE_ERROR;
}
nsapi_error_t WIZnetInterface::socket_listen(nsapi_socket_t handle, int backlog)
{
DBG("fd: %d\n", SKT(handle)->fd);
if (SKT(handle)->fd < 0) {
return NSAPI_ERROR_NO_SOCKET;
}
/* if (backlog != 1) {
return NSAPI_ERROR_NO_SOCKET;
}
*/
SKT(handle)->server = true;
_mutex.lock();
_wiznet.scmd(SKT(handle)->fd, LISTEN);
_mutex.unlock();
return 0;
}
nsapi_size_or_error_t WIZnetInterface::socket_connect(nsapi_socket_t handle, const SocketAddress &address)
{
DBG("fd: %d\n", SKT(handle)->fd);
//check for a valid socket
_mutex.lock();
if (SKT(handle)->fd < 0) {
_mutex.unlock();
return NSAPI_ERROR_NO_SOCKET;
}
DBG("Wiz socket_connect Addres[%s] Port: %d \n", address.get_ip_address(), address.get_port());
//before we attempt to connect, we are not connected
SKT(handle)->connected = false;
//try to connect
if (!_wiznet.connect(SKT(handle)->fd, address.get_ip_address(), address.get_port(), WIZNET_WAIT_TIMEOUT1)) {
_mutex.unlock();
return -1;
}
//we are now connected
SKT(handle)->connected = true;
_mutex.unlock();
return 0;
}
nsapi_error_t WIZnetInterface::socket_accept(nsapi_socket_t server, nsapi_socket_t *handle, SocketAddress *address)
{
SocketAddress _addr;
DBG("fd: %d\n", SKT(handle)->fd);
if (SKT(server)->fd < 0) {
return NSAPI_ERROR_NO_SOCKET;
}
SKT(server)->connected = false;
Timer t;
t.reset();
t.start();
while(1) {
if (t.read_ms() > WIZNET_ACCEPT_TIMEOUT) {
DBG("WIZnetInterface::socket_accept, timed out\r\n");
return NSAPI_ERROR_WOULD_BLOCK;
}
if (_wiznet.is_connected(SKT(server)->fd)) break;
}
//get socket for the connection
*handle = get_sock(SKT(server)->fd, SKT(server)->proto);
if (!(*handle)) {
error("No more sockets for binding");
return NSAPI_ERROR_NO_SOCKET;
}
//give it all of the socket info from the server
SKT(*handle)->proto = SKT(server)->proto;
SKT(*handle)->connected = true;
if (address) {
uint32_t ip = _wiznet.sreg<uint32_t>(SKT(*handle)->fd, Sn_DIPR);
char host[17];
snprintf(host, sizeof(host), "%d.%d.%d.%d", (int)((ip>>24)&0xff), (int)((ip>>16)&0xff), (int)((ip>>8)&0xff), (int)(ip&0xff));
int port = _wiznet.sreg<uint16_t>(SKT(*handle)->fd, Sn_DPORT);
_addr.set_ip_address(host);
_addr.set_port(port);
*address = _addr;
}
//create a new tcp socket for the server
SKT(server)->fd = _wiznet.new_socket();
if (SKT(server)->fd < 0) {
//error("No more sockets for listening");
//return NSAPI_ERROR_NO_SOCKET;
// already accepted socket, so return 0, but there is no listen socket anymore.
return 0;
}
SKT(server)->proto = NSAPI_TCP;
SKT(server)->connected = false;
_addr.set_port(listen_port);
// and then, for the next connection, server socket should be assigned new one.
if (socket_bind(server, _addr) < 0) {
//error("No more sockets for listening");
//return NSAPI_ERROR_NO_SOCKET;
// already accepted socket, so return 0, but there is no listen socket anymore.
return 0;
}
if (socket_listen(server, 1) < 0) {
//error("No more sockets for listening");
// already accepted socket, so return 0, but there is no listen socket anymore.
return 0;
}
return 0;
}
nsapi_size_or_error_t WIZnetInterface::socket_send(nsapi_socket_t handle, const void *data, nsapi_size_t size)
{
DBG("fd: %d\n", SKT(handle)->fd);
//INFO("fd: %d\n", SKT(handle)->fd);
nsapi_size_t writtenLen = 0;
int ret;
_mutex.lock();
while (writtenLen < size) {
int _size = _wiznet.wait_writeable(SKT(handle)->fd, WIZNET_WAIT_TIMEOUT);
if (_size < 0) {
_mutex.unlock();
return NSAPI_ERROR_WOULD_BLOCK;
}
if (_size > (size-writtenLen)) {
_size = (size-writtenLen);
}
ret = _wiznet.send(SKT(handle)->fd, (char*)((uint32_t *)data+writtenLen), (int)_size);
if (ret < 0) {
DBG("returning error -1\n");
_mutex.unlock();
return -1;
}
writtenLen += ret;
}
_mutex.unlock();
return writtenLen;
}
nsapi_size_or_error_t WIZnetInterface::socket_recv(nsapi_socket_t handle, void *data, nsapi_size_t size)
{
int recved_size = 0;
//int idx;
int _size;
nsapi_size_or_error_t retsize;
uint8_t socket_status;
#ifdef USE_W6100
DBG("fd: %d port: %d\n", SKT(handle)->fd, _wiznet.sreg<uint16_t>(SKT(handle)->fd, Sn_PORTR));
#endif
//INFO("fd: %d\n", SKT(handle)->fd);
// add to cover exception.
_mutex.lock();
if(SKT(handle)->connected == false)
{
SKT(handle)->connected = true;
}
#if 0//#ifdef USE_W6100
bool temp_server = SKT(handle)->server;
uint16_t temp_port;
switch(_wiznet.socket_status(SKT(handle)->fd))
{
case WIZ_SOCK_CLOSED :
break;
case WIZ_SOCK_INIT:
if(SKT(handle)->server == true)
{
_mutex.lock();
_wiznet.scmd(SKT(handle)->fd, LISTEN);
_mutex.unlock();
}
break;
case WIZ_SOCK_LISTEN:
break;
case WIZ_SOCK_SYNSENT:
break;
case WIZ_SOCK_ESTABLISHED:
if(SKT(handle)->connected == false)
{
SKT(handle)->connected = true;
}
break;
case WIZ_SOCK_CLOSE_WAIT:
_wiznet.disconnect(SKT(handle)->fd);
/*
temp_port = _wiznet.sreg<uint16_t>(SKT(handle)->fd, Sn_PORTR);
DBG("fd: port %d \n", temp_port);
_wiznet.close(SKT(handle)->fd);
_wiznet.setLocalPort(SKT(handle)->fd, temp_port);
_wiznet.setProtocol(SKT(handle)->fd, (Protocol)(SKT(handle)->proto + 1));
_wiznet.scmd(SKT(handle)->fd, OPEN);
DBG("fd: server %d || socket status [%X]\n", SKT(handle)->server, _wiznet.socket_status(SKT(handle)->fd));
if(temp_server == true)
{
SKT(handle)->server = true;
_mutex.lock();
_wiznet.scmd(SKT(handle)->fd, LISTEN);
_mutex.unlock();
}
*/
break;
default :
break;
}
#endif
#ifdef USE_W6100
socket_status = _wiznet.socket_status(SKT(handle)->fd);
DBG("fd: connected is %d || socket status [%X]\n", SKT(handle)->connected, socket_status);
#endif
if ((SKT(handle)->fd < 0) || !SKT(handle)->connected) {
_mutex.unlock();
return NSAPI_ERROR_NO_CONNECTION;
}
else if(socket_status == 0x1c)
{
_mutex.unlock();
return NSAPI_ERROR_CONNECTION_LOST;
}
while(1) {
_size = _wiznet.wait_readable(SKT(handle)->fd, WIZNET_WAIT_TIMEOUT);
DBG("fd: _size %d recved_size %d\n", _size, recved_size);
if (_size <= 0) {
if(recved_size >= 0){
retsize = recved_size;
//INFO("recved_size : %d\n",recved_size);
break;
}
_mutex.unlock();
return NSAPI_ERROR_WOULD_BLOCK;
}
if(_size > size)
{
_size = size;
}
retsize = _wiznet.recv(SKT(handle)->fd, (char*)((uint32_t *)data + recved_size), (int)_size);
DBG("rv: %d\n", retsize);
recved_size += _size;
//if(recved_size >= size)
break;
}
#if WIZNET_INTF_DBG
if (retsize > 0) {
debug("[socket_recv] buffer:");
for(int i = 0; i < retsize; i++) {
if ((i%16) == 0) {
debug("\n");
}
debug(" %02x", ((uint8_t*)data)[i]);
}
if ((retsize-1%16) != 0) {
debug("\n");
}
}
#endif
_mutex.unlock();
return retsize;
}
nsapi_size_or_error_t WIZnetInterface::socket_sendto(nsapi_socket_t handle, const SocketAddress &address,
const void *data, nsapi_size_t size)
{
_mutex.lock();
DBG("fd: %d, ip: %s:%d\n", SKT(handle)->fd, address.get_ip_address(), address.get_port());
if (_wiznet.is_closed(SKT(handle)->fd)) {
return NSAPI_ERROR_NO_SOCKET;
}
//compare with original: int size = eth->wait_writeable(_sock_fd, _blocking ? -1 : _timeout, length-1);
int len = _wiznet.wait_writeable(SKT(handle)->fd, WIZNET_WAIT_TIMEOUT, size-1);
if (len < 0) {
DBG("error: NSAPI_ERROR_WOULD_BLOCK\n");
_mutex.unlock();
return NSAPI_ERROR_WOULD_BLOCK;
}
// set remote host
_wiznet.sreg_ip(SKT(handle)->fd, Sn_DIPR, address.get_ip_address());
// set remote port
_wiznet.sreg<uint16_t>(SKT(handle)->fd, Sn_DPORT, address.get_port());
nsapi_size_or_error_t err = _wiznet.send(SKT(handle)->fd, (const char*)data, size);
DBG("rv: %d, size: %d\n", err, size);
#if WIZNET_INTF_DBG
if (err > 0) {
debug("[socket_sendto] data: ");
for(int i = 0; i < err; i++) {
if ((i%16) == 0) {
debug("\n");
}
debug(" %02x", ((uint8_t*)data)[i]);
}
if ((err-1%16) != 0) {
debug("\n");
}
}
#endif
_mutex.unlock();
return err;
}
nsapi_size_or_error_t WIZnetInterface::socket_recvfrom(nsapi_socket_t handle, SocketAddress *address,
void *buffer, nsapi_size_t size)
{
DBG("fd: %d\n", SKT(handle)->fd);
//check for null pointers
if (buffer == NULL) {
DBG("buffer is NULL; receive is ABORTED\n");
return -1;
}
_mutex.lock();
uint8_t info[8];
int len = _wiznet.wait_readable(SKT(handle)->fd, WIZNET_WAIT_TIMEOUT, sizeof(info));
DBG("Recieve len %d \n", len);
if(len == 0 )
{
_mutex.unlock();
return 0;
}
else if (len < 0) {
DBG("error: NSAPI_ERROR_WOULD_BLOCK\n");
_mutex.unlock();
return NSAPI_ERROR_WOULD_BLOCK;
}
#ifdef USE_W6100
uint8_t Temp_Head[2];
uint16_t port;
_wiznet.recv(SKT(handle)->fd, (char*)Temp_Head, sizeof(Temp_Head));
DBG("Head : %X%X\n", Temp_Head[0], Temp_Head[1]);
nsapi_size_t udp_size = ((Temp_Head[0]&0x07)<<8)|Temp_Head[1];
if (udp_size > (len-sizeof(info))) {
DBG("error: udp_size[%d] > (len[%d]-sizeof(info)[%d])\n", udp_size, len, sizeof(info) );
_mutex.unlock();
return -1;
}
if(Temp_Head[0]&0x80)
{
//ipv6
uint8_t Temp_info[18];
_wiznet.recv(SKT(handle)->fd, (char*)Temp_info, sizeof(Temp_info));
//to do next address and port
}
else
{
//ipv4
char addr[17];
uint8_t Temp_info[6];
_wiznet.recv(SKT(handle)->fd, (char*)Temp_info, sizeof(Temp_info));
snprintf(addr, sizeof(addr), "%d.%d.%d.%d", Temp_info[0], Temp_info[1], Temp_info[2], Temp_info[3]);
uint16_t port = Temp_info[4]<<8|Temp_info[5];
if (address != NULL) {
//DBG("[socket_recvfrom] warn: addressis NULL");
address->set_ip_address(addr);
address->set_port(port);
}
}
#else
//receive endpoint information
_wiznet.recv(SKT(handle)->fd, (char*)info, sizeof(info));
char addr[17];
snprintf(addr, sizeof(addr), "%d.%d.%d.%d", info[0], info[1], info[2], info[3]);
uint16_t port = info[4]<<8|info[5];
// original behavior was to terminate execution if address is NULL
if (address != NULL) {
//DBG("[socket_recvfrom] warn: addressis NULL");
address->set_ip_address(addr);
address->set_port(port);
}
nsapi_size_t udp_size = info[6]<<8|info[7];
if (udp_size > (len-sizeof(info))) {
DBG("error: udp_size > (len-sizeof(info))\n");
_mutex.unlock();
return -1;
}
#endif
//receive from socket
nsapi_size_or_error_t err = _wiznet.recv(SKT(handle)->fd, (char*)buffer, udp_size);
DBG("rv: %d\n", err);
#if WIZNET_INTF_DBG
if (err > 0) {
debug("[socket_recvfrom] buffer:");
for(int i = 0; i < err; i++) {
if ((i%16) == 0) {
debug("\n");
}
debug(" %02x", ((uint8_t*)buffer)[i]);
}
if ((err-1%16) != 0) {
debug("\n");
}
}
#endif
_mutex.unlock();
return err;
}
void WIZnetInterface::socket_attach(void *handle, void (*callback)(void *), void *data)
{
// if (handle == NULL) return;
// DBG("fd: %d, callback: %p\n", SKT(handle)->fd, callback);
// SKT(handle)->callback = callback;
// SKT(handle)->callback_data = data;
if (handle == NULL) return;
_mutex.lock();
wiznet_socket *socket = (wiznet_socket *)handle;
wiznet_sockets[socket->fd].callback = callback;
wiznet_sockets[socket->fd].callback_data = data;
_mutex.unlock();
}
void WIZnetInterface::event()
{
for(int i=0; i<MAX_SOCK_NUM; i++){
if (wiznet_sockets[i].callback) {
wiznet_sockets[i].callback(wiznet_sockets[i].callback_data);
}
}
}
nsapi_error_t WIZnetInterface::gethostbyname(const char *host,
SocketAddress *address, nsapi_version_t version, const char *interface_name)
{
DBG("DNS process %s \n", host);
bool isOK = dns.lookup(host);
if (isOK) {
DBG("is ok\n");
DBG(" IP: %s\n", dns.get_ip_address());
} else {
DBG(" IP is not ok\n");
return NSAPI_ERROR_DNS_FAILURE;
}
if ( !address->set_ip_address(dns.get_ip_address()) ) {
return NSAPI_ERROR_DNS_FAILURE;
}
return NSAPI_ERROR_OK;
}