forked from Wiznet-OpenHardware/w5500-eth-driver
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathWIZnetInterface.cpp
742 lines (637 loc) · 21.6 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
/**
******************************************************************************
* @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"
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_ACCEPT_TIMEOUT 300000 //5 mins timeout, retrun NSAPI_ERROR_WOULD_BLOCK if there is no connection during 5 mins
#define WIZNET_INTF_DBG 0
#if WIZNET_INTF_DBG
#define DBG(...) do{debug("[%s:%d]", __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
/**
* @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 (not defined TARGET_WIZwiki_W7500) && (not defined TARGET_WIZwiki_W7500P) && (not defined TARGET_WIZwiki_W7500ECO)
WIZnetInterface::WIZnetInterface(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset) :
_wiznet(mosi, miso, sclk, cs, reset)
{
ip_set = false;
_dhcp_enable = true;
// _w5500.attach(this, &W5500Interface::event);
thread_read_socket.start(callback(this, &WIZnetInterface::socket_check_read));
}
#endif
/*
W5500Interface::W5500Interface(SPI* spi, PinName cs, PinName reset) :
_w5500(spi, cs, reset)
{
ip_set = false;
}
*/
wiznet_socket* WIZnetInterface::get_sock(int fd)
{
for (int i=0; i<MAX_SOCK_NUM ; i++) {
if (wiznet_sockets[i].fd == -1) {
wiznet_sockets[i].fd = fd;
wiznet_sockets[i].proto = NSAPI_TCP;
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();
_wiznet.reset();
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();
_wiznet.reset(); // reset chip and write mac address
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);
_wiznet.reset();
// @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);
_wiznet.reset();
// @Jul. 8. 2014 add code. should be called to write chip.
_wiznet.setmac();
_wiznet.setip();
init_socks();
return 0;
}
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();
}
wait_ms(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);
}
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 sock_fd = _wiznet.new_socket();
if (sock_fd < 0) {
return NSAPI_ERROR_NO_SOCKET;
}
wiznet_socket *h = get_sock(sock_fd);
if (!h) {
return NSAPI_ERROR_NO_SOCKET;
}
h->proto = proto;
h->connected = false;
h->callback = NULL;
h->callback_data = NULL;
//new up an int to store the socket fd
*handle = h;
DBG("fd: %d\n", sock_fd);
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 (handle < 0) {
return NSAPI_ERROR_DEVICE_ERROR;
}
DBG("fd: %d, port: %d\n", SKT(handle)->fd, address.get_port());
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, udp_local_port );
}
// set udp protocol
_wiznet.setProtocol(SKT(handle)->fd, UDP);
_wiznet.scmd(SKT(handle)->fd, OPEN);
/*
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.setProtocol(SKT(handle)->fd, TCP);
// 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;
}
*/
_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;
}
//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_TIMEOUT)) {
_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);
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*)(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;
nsapi_size_t _size;
nsapi_size_or_error_t err;
DBG("fd: %d\n", SKT(handle)->fd);
//INFO("fd: %d\n", SKT(handle)->fd);
// add to cover exception.
_mutex.lock();
if ((SKT(handle)->fd < 0) || !SKT(handle)->connected) {
_mutex.unlock();
return -1;
}
DBG("fd: connected is %d\n", SKT(handle)->connected);
while(1) {
_size = _wiznet.wait_readable(SKT(handle)->fd, WIZNET_WAIT_TIMEOUT);
DBG("fd: _size %d\n", _size);
if (_size < 0) {
if(recved_size > 0){
err = recved_size;
//INFO("recved_size : %d\n",recved_size);
break;
}
_mutex.unlock();
return NSAPI_ERROR_WOULD_BLOCK;
}
if (_size > (size - recved_size)) {
_size = (size - recved_size);
}
if (_size == 0 && recved_size !=0 ){
_mutex.unlock();
return recved_size;
}
err = _wiznet.recv(SKT(handle)->fd, (char*)(data + recved_size), (int)_size);
// printf("[TEST 400] : %d\r\n",recved_size);
// for(idx=0; idx<16; idx++)
// {
// printf(" %02x",((uint8_t*)data)[idx+recved_size]);
// }
// printf("\r\n");
DBG("rv: %d\n", err);
//INFO("rv: %d\n",err);
recved_size += _size;
}
#if WIZNET_INTF_DBG
if (err > 0) {
debug("[socket_recv] buffer:");
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_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)) {
nsapi_error_t err = socket_bind(handle, address);
if (err < 0 ) {
DBG("failed to bind socket: %d\n", err);
_mutex.unlock();
return err;
}
}
//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));
if (len < 0) {
DBG("error: NSAPI_ERROR_WOULD_BLOCK\n");
_mutex.unlock();
return NSAPI_ERROR_WOULD_BLOCK;
}
//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;
}
//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)
{
DBG("DNS process %s", 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;
}