Welcome to mirror list, hosted at ThFree Co, Russian Federation.

socket.cpp « Ethernet « Wiznet « DuetEthernet « DuetNG « src - github.com/Duet3D/RepRapFirmware.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a647d08a28abfb7839ccfe5e18a4a1c0a8ab0240 (plain)
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
//*****************************************************************************
//
//! \file socket.c
//! \brief SOCKET APIs Implements file.
//! \details SOCKET APIs like as Berkeley Socket APIs. 
//! \version 1.0.3
//! \date 2013/10/21
//! \par  Revision history
//!       <2015/02/05> Notice
//!        The version history is not updated after this point.
//!        Download the latest version directly from GitHub. Please visit the our GitHub repository for ioLibrary.
//!        >> https://github.com/Wiznet/ioLibrary_Driver
//!       <2014/05/01> V1.0.3. Refer to M20140501
//!         1. Implicit type casting -> Explicit type casting.
//!         2. replace 0x01 with PACK_REMAINED in recvfrom()
//!         3. Validation a destination ip in connect() & sendto(): 
//!            It occurs a fatal error on converting unint32 address if uint8* addr parameter is not aligned by 4byte address.
//!            Copy 4 byte addr value into temporary uint32 variable and then compares it.
//!       <2013/12/20> V1.0.2 Refer to M20131220
//!                    Remove Warning.
//!       <2013/11/04> V1.0.1 2nd Release. Refer to "20131104".
//!                    In sendto(), Add to clear timeout interrupt status (Sn_IR_TIMEOUT)
//!       <2013/10/21> 1st Release
//! \author MidnightCow
//! \copyright
//!
//! Copyright (c)  2013, WIZnet Co., LTD.
//! All rights reserved.
//! 
//! Redistribution and use in source and binary forms, with or without 
//! modification, are permitted provided that the following conditions 
//! are met: 
//! 
//!     * Redistributions of source code must retain the above copyright 
//! notice, this list of conditions and the following disclaimer. 
//!     * Redistributions in binary form must reproduce the above copyright
//! notice, this list of conditions and the following disclaimer in the
//! documentation and/or other materials provided with the distribution. 
//!     * Neither the name of the <ORGANIZATION> nor the names of its 
//! contributors may be used to endorse or promote products derived 
//! from this software without specific prior written permission. 
//! 
//! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
//! AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
//! IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
//! ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
//! LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
//! CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
//! SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
//! INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
//! CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
//! ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
//! THE POSSIBILITY OF SUCH DAMAGE.
//
//*****************************************************************************
#include "socket.h"

#define _SOCKET_DEBUG_

#ifdef _SOCKET_DEBUG_
extern "C" void debugPrintf(const char *fmt, ...);
extern "C" void delay(uint32_t);
# define DEBUG_PRINTF(...) debugPrintf(__VA_ARGS__)
#else
# define DEBUG_PRINTF(_fmt, ...)
#endif



#define SOCK_ANY_PORT_NUM  0xC000

static uint16_t sock_any_port = SOCK_ANY_PORT_NUM;
static uint16_t sock_io_mode = 0;
static uint16_t sock_is_sending = 0;

static uint16_t sock_remained_size[_WIZCHIP_SOCK_NUM_] = {0,0,};

uint8_t sock_pack_info[_WIZCHIP_SOCK_NUM_] = {0,};

#define CHECK_SOCKMODE(mode)  \
   do{                     \
      if((getSn_MR(sn) & 0x0F) != mode) return SOCKERR_SOCKMODE;  \
   }while(0);              \

#define CHECK_SOCKINIT()   \
   do{                     \
      if((getSn_SR(sn) != SOCK_INIT)) return SOCKERR_SOCKINIT; \
   }while(0);              \

#define CHECK_SOCKDATA()   \
   do{                     \
      if(len == 0) return SOCKERR_DATALEN;   \
   }while(0);              \

static void ExecCommand(uint8_t sn, uint8_t cmd)
{
	setSn_CR(sn, cmd);
	while(getSn_CR(sn) != 0) { }
}

int8_t socket(uint8_t sn, uint8_t protocol, uint16_t port, uint8_t flag)
{
	switch(protocol)
	{
	case Sn_MR_TCP:
		{
			uint32_t taddr;
			getSIPR((uint8_t*)&taddr);
			if (taddr == 0)
			{
				return SOCKERR_SOCKINIT;
			}
		}
		break;

	case Sn_MR_UDP:
	case Sn_MR_MACRAW:
		break;

	default:
		return SOCKERR_SOCKMODE;
	}

	if ((flag & 0x04) != 0)
	{
		return SOCKERR_SOCKFLAG;
	}

	if (flag != 0)
	{
		switch(protocol)
		{
		case Sn_MR_TCP:
			if ((flag & (SF_TCP_NODELAY|SF_IO_NONBLOCK)) == 0)
			{
				return SOCKERR_SOCKFLAG;
			}
			break;
		case Sn_MR_UDP:
			if (flag & SF_IGMP_VER2)
			{
				if ((flag & SF_MULTI_ENABLE) == 0)
				{
					return SOCKERR_SOCKFLAG;
				}
			}
			if (flag & SF_UNI_BLOCK)
			{
				if ((flag & SF_MULTI_ENABLE) == 0)
				{
					return SOCKERR_SOCKFLAG;
				}
			}
			break;
		default:
			break;
		}
	}
	close(sn);
	setSn_MR(sn, (protocol | (flag & 0xF0)));
	if (port == 0)
	{
		port = sock_any_port++;
		if (sock_any_port == 0xFFF0)
		{
			sock_any_port = SOCK_ANY_PORT_NUM;
		}
	}
	setSn_PORT(sn,port);
	ExecCommand(sn, Sn_CR_OPEN);

	// release the previous sock_io_mode
	sock_io_mode &= ~(1 <<sn);
	sock_io_mode |= ((flag & SF_IO_NONBLOCK) << sn);   
	sock_is_sending &= ~(1<<sn);
	sock_remained_size[sn] = 0;
	sock_pack_info[sn] = PACK_COMPLETED;
	while(getSn_SR(sn) == SOCK_CLOSED) { }
	return (int8_t)sn;
}	   

int8_t close(uint8_t sn)
{
	ExecCommand(sn, Sn_CR_CLOSE);

	// Clear all interrupt of the socket
	setSn_IR(sn, 0xFF);
	// Release the sock_io_mode of socket n.
	sock_io_mode &= ~(1<<sn);
	sock_is_sending &= ~(1<<sn);
	sock_remained_size[sn] = 0;
	sock_pack_info[sn] = 0;
	while(getSn_SR(sn) != SOCK_CLOSED) { }
	return SOCK_OK;
}

int8_t listen(uint8_t sn)
{
	CHECK_SOCKMODE(Sn_MR_TCP);
	CHECK_SOCKINIT();
	ExecCommand(sn, Sn_CR_LISTEN);
	while(getSn_SR(sn) != SOCK_LISTEN)
	{
		 close(sn);
		 return SOCKERR_SOCKCLOSED;
	}
	return SOCK_OK;
}


int8_t connect(uint8_t sn, uint8_t * addr, uint16_t port)
{
	CHECK_SOCKMODE(Sn_MR_TCP);
	CHECK_SOCKINIT();
	{
		uint32_t taddr = ((uint32_t)addr[0] & 0x000000FF);
		taddr = (taddr << 8) + ((uint32_t)addr[1] & 0x000000FF);
		taddr = (taddr << 8) + ((uint32_t)addr[2] & 0x000000FF);
		taddr = (taddr << 8) + ((uint32_t)addr[3] & 0x000000FF);
		if (taddr == 0xFFFFFFFF || taddr == 0)
		{
			return SOCKERR_IPINVALID;
		}
	}
	
	if (port == 0)
	{
		return SOCKERR_PORTZERO;
	}
	setSn_DIPR(sn,addr);
	setSn_DPORT(sn,port);
	ExecCommand(sn, Sn_CR_CONNECT);
	if (sock_io_mode & (1<<sn))
	{
		return SOCK_BUSY;
	}
	while(getSn_SR(sn) != SOCK_ESTABLISHED)
	{
		if (getSn_IR(sn) & Sn_IR_TIMEOUT)
		{
			setSn_IR(sn, Sn_IR_TIMEOUT);
			return SOCKERR_TIMEOUT;
		}

		if (getSn_SR(sn) == SOCK_CLOSED)
		{
			return SOCKERR_SOCKCLOSED;
		}
	}

	return SOCK_OK;
}

int8_t disconnect(uint8_t sn)
{
	CHECK_SOCKMODE(Sn_MR_TCP);
	ExecCommand(sn, Sn_CR_DISCON);

	sock_is_sending &= ~(1<<sn);
	if (sock_io_mode & (1<<sn))
	{
		return SOCK_BUSY;
	}
	while(getSn_SR(sn) != SOCK_CLOSED)
	{
		if (getSn_IR(sn) & Sn_IR_TIMEOUT)
		{
			close(sn);
			return SOCKERR_TIMEOUT;
		}
	}
	return SOCK_OK;
}

int32_t send(uint8_t sn, uint8_t * buf, uint16_t len)
{
	CHECK_SOCKMODE(Sn_MR_TCP);
	CHECK_SOCKDATA();
	uint8_t tmp = getSn_SR(sn);
	if (tmp != SOCK_ESTABLISHED && tmp != SOCK_CLOSE_WAIT)
	{
		return SOCKERR_SOCKSTATUS;
	}
	if ( sock_is_sending & (1<<sn) )
	{
		tmp = getSn_IR(sn);
		if (tmp & Sn_IR_SENDOK)
		{
			setSn_IR(sn, Sn_IR_SENDOK);
			sock_is_sending &= ~(1<<sn);
		}
		else if(tmp & Sn_IR_TIMEOUT)
		{
			close(sn);
			return SOCKERR_TIMEOUT;
		}
		else
		{
			return SOCK_BUSY;
		}
	}
	uint16_t freesize = getSn_TxMAX(sn);
	if (len > freesize)
	{
		len = freesize; // check size not to exceed MAX size.
	}
	while(1)
	{
		freesize = getSn_TX_FSR(sn);
		tmp = getSn_SR(sn);
		if ((tmp != SOCK_ESTABLISHED) && (tmp != SOCK_CLOSE_WAIT))
		{
			close(sn);
			return SOCKERR_SOCKSTATUS;
		}
		if ( (sock_io_mode & (1<<sn)) && (len > freesize) )
		{
			return SOCK_BUSY;
		}
		if (len <= freesize)
		{
			break;
		}
		DEBUG_PRINTF("Socket %u need %u free %u\n", sn, len, freesize);
	}
	wiz_send_data(sn, buf, len);

	ExecCommand(sn, Sn_CR_SEND);
	sock_is_sending |= (1 << sn);
	return (int32_t)len;
}


int32_t recv(uint8_t sn, uint8_t * buf, uint16_t len)
{
	CHECK_SOCKMODE(Sn_MR_TCP);
	CHECK_SOCKDATA();

	uint16_t recvsize = getSn_RxMAX(sn);
	if (recvsize < len)
	{
		len = recvsize;
	}

	while(1)
	{
		recvsize = getSn_RX_RSR(sn);
		const uint8_t tmp = getSn_SR(sn);
		if (tmp != SOCK_ESTABLISHED)
		{
			if (tmp == SOCK_CLOSE_WAIT)
			{
				if	(recvsize != 0)
				{
					break;
				}
				else if(getSn_TX_FSR(sn) == getSn_TxMAX(sn))
				{
					close(sn);
					return SOCKERR_SOCKSTATUS;
				}
			}
			else
			{
				close(sn);
				return SOCKERR_SOCKSTATUS;
			}
		}
		if ((sock_io_mode & (1<<sn)) && (recvsize == 0))
		{
			return SOCK_BUSY;
		}
		if (recvsize != 0)
		{
			break;
		}
	};

	if (recvsize < len)
	{
		len = recvsize;
	}
	wiz_recv_data(sn, buf, len);
	ExecCommand(sn, Sn_CR_RECV);

	return (int32_t)len;
}

int32_t sendto(uint8_t sn, const uint8_t * buf, uint16_t len, const uint8_t * addr, uint16_t port)
{
	switch(getSn_MR(sn) & 0x0F)
	{
	case Sn_MR_UDP:
	case Sn_MR_MACRAW:
		break;
	default:
		return SOCKERR_SOCKMODE;
	}

	CHECK_SOCKDATA();
	uint32_t taddr = ((uint32_t)addr[0]) & 0x000000FF;
	taddr = (taddr << 8) + ((uint32_t)addr[1] & 0x000000FF);
	taddr = (taddr << 8) + ((uint32_t)addr[2] & 0x000000FF);
	taddr = (taddr << 8) + ((uint32_t)addr[3] & 0x000000FF);
	if (taddr == 0)
	{
		return SOCKERR_IPINVALID;
	}
	if (port == 0)
	{
		return SOCKERR_PORTZERO;
	}
	const uint8_t tmp = getSn_SR(sn);
	if (tmp != SOCK_MACRAW && tmp != SOCK_UDP)
	{
		return SOCKERR_SOCKSTATUS;
	}

	setSn_DIPR(sn,addr);
	setSn_DPORT(sn,port);
	uint16_t freesize = getSn_TxMAX(sn);
	if (len > freesize)
	{
		len = freesize; // check size not to exceed MAX size.
	}

	while(1)
	{
		freesize = getSn_TX_FSR(sn);
		if (getSn_SR(sn) == SOCK_CLOSED)
		{
			return SOCKERR_SOCKCLOSED;
		}
		if ( (sock_io_mode & (1<<sn)) && (len > freesize) )
		{
			return SOCK_BUSY;
		}
		if (len <= freesize)
		{
			break;
		}
		DEBUG_PRINTF("Socket %u need %u free %u\n", sn, len, freesize);
	};

	wiz_send_data(sn, buf, len);
	ExecCommand(sn, Sn_CR_SEND);

	while(1)
	{
		const uint8_t tmp = getSn_IR(sn);
		if (tmp & Sn_IR_SENDOK)
		{
			setSn_IR(sn, Sn_IR_SENDOK);
			break;
		}
		else if(tmp & Sn_IR_TIMEOUT)
		{
			setSn_IR(sn, Sn_IR_TIMEOUT);
			return SOCKERR_TIMEOUT;
		}
		DEBUG_PRINTF("Socket %u waiting for send to complete, IR=%02x\n", sn, tmp);
#ifdef _SOCKET_DEBUG_
		delay(10);		// to avoid too many messages
#endif
	}
	return (int32_t)len;
}


int32_t recvfrom(uint8_t sn, uint8_t * buf, uint16_t len, uint8_t * addr, uint16_t *port)
{
	const uint8_t mr = getSn_MR(sn);
	switch(mr & 0x0F)
	{
	case Sn_MR_UDP:
	case Sn_MR_MACRAW:
		break;
	default:
		return SOCKERR_SOCKMODE;
	}

	CHECK_SOCKDATA();
	uint16_t pack_len=0;
	if (sock_remained_size[sn] == 0)
	{
		while(1)
		{
			pack_len = getSn_RX_RSR(sn);
			if (getSn_SR(sn) == SOCK_CLOSED)
			{
				return SOCKERR_SOCKCLOSED;
			}
			if ( (sock_io_mode & (1<<sn)) && (pack_len == 0) )
			{
				return SOCK_BUSY;
			}
			if (pack_len != 0)
			{
				break;
			}
		}
	}

	switch (mr & 0x07)
	{
	case Sn_MR_UDP :
		if (sock_remained_size[sn] == 0)
		{
			uint8_t head[8];
			wiz_recv_data(sn, head, 8);
			ExecCommand(sn, Sn_CR_RECV);

			// Read peer's IP address, port number & packet length
			addr[0] = head[0];
			addr[1] = head[1];
			addr[2] = head[2];
			addr[3] = head[3];
			*port = head[4];
			*port = (*port << 8) + head[5];
			sock_remained_size[sn] = head[6];
			sock_remained_size[sn] = (sock_remained_size[sn] << 8) + head[7];
			sock_pack_info[sn] = PACK_FIRST;
		}
		if (len < sock_remained_size[sn])
		{
			pack_len = len;
		}
		else
		{
			pack_len = sock_remained_size[sn];
		}
		len = pack_len;
		//
		// Need to packet length check (default 1472)
		//
		wiz_recv_data(sn, buf, pack_len); // data copy.
		break;

	case Sn_MR_MACRAW :
		if (sock_remained_size[sn] == 0)
		{
			uint8_t head[8];
			wiz_recv_data(sn, head, 2);
			ExecCommand(sn, Sn_CR_RECV);

			// Read peer's IP address, port number & packet length
			sock_remained_size[sn] = head[0];
			sock_remained_size[sn] = (sock_remained_size[sn] <<8) + head[1];
			if (sock_remained_size[sn] > 1514)
			{
				close(sn);
				return SOCKFATAL_PACKLEN;
			}
			sock_pack_info[sn] = PACK_FIRST;
		}
		if (len < sock_remained_size[sn])
		{
			pack_len = len;
		}
		else
		{
			pack_len = sock_remained_size[sn];
		}
		wiz_recv_data(sn,buf,pack_len);
		break;

	default:
		wiz_recv_ignore(sn, pack_len); // data copy.
		sock_remained_size[sn] = pack_len;
		break;
	}
	ExecCommand(sn, Sn_CR_RECV);

	sock_remained_size[sn] -= pack_len;
	if (sock_remained_size[sn] != 0)
	{
		sock_pack_info[sn] |= PACK_REMAINED;
	}
	else
	{
		sock_pack_info[sn] = PACK_COMPLETED;
	}
	return (int32_t)pack_len;
}


int8_t  ctlsocket(uint8_t sn, ctlsock_type cstype, void* arg)
{
	switch(cstype)
	{
	case CS_SET_IOMODE:
		{
			const uint8_t tmp = *((uint8_t*)arg);
			if (tmp == SOCK_IO_NONBLOCK)
			{
				sock_io_mode |= (1<<sn);
			}
			else if (tmp == SOCK_IO_BLOCK)
			{
				sock_io_mode &= ~(1<<sn);
			}
			else
			{
				return SOCKERR_ARG;
			}
		}
		break;
	case CS_GET_IOMODE:
		*((uint8_t*)arg) = (uint8_t)((sock_io_mode >> sn) & 0x0001);
		break;
	case CS_GET_MAXTXBUF:
		*((uint16_t*)arg) = getSn_TxMAX(sn);
		break;
	case CS_GET_MAXRXBUF:
		*((uint16_t*)arg) = getSn_RxMAX(sn);
		break;
	case CS_CLR_INTERRUPT:
		if ((*(uint8_t*)arg) > SIK_ALL)
		{
			return SOCKERR_ARG;
		}
		setSn_IR(sn, *(uint8_t*)arg);
		break;
	case CS_GET_INTERRUPT:
		*((uint8_t*)arg) = getSn_IR(sn);
		break;
	case CS_SET_INTMASK:
		if ((*(uint8_t*)arg) > SIK_ALL)
		{
			return SOCKERR_ARG;
		}
		setSn_IMR(sn, *(uint8_t*)arg);
		break;
	case CS_GET_INTMASK:
		*((uint8_t*)arg) = getSn_IMR(sn);
		break;
	default:
		return SOCKERR_ARG;
	}
	return SOCK_OK;
}

int8_t  setsockopt(uint8_t sn, sockopt_type sotype, void* arg)
{
	switch(sotype)
	{
	case SO_TTL:
		setSn_TTL(sn,*(uint8_t*)arg);
		break;
	case SO_TOS:
		setSn_TOS(sn,*(uint8_t*)arg);
		break;
	case SO_MSS:
		setSn_MSSR(sn,*(uint16_t*)arg);
		break;
	case SO_DESTIP:
		setSn_DIPR(sn, (uint8_t*)arg);
		break;
	case SO_DESTPORT:
		setSn_DPORT(sn, *(uint16_t*)arg);
		break;
	case SO_KEEPALIVESEND:
		CHECK_SOCKMODE(Sn_MR_TCP);
		if (getSn_KPALVTR(sn) != 0)
		{
			return SOCKERR_SOCKOPT;
		}
		setSn_CR(sn, Sn_CR_SEND_KEEP);
		while(getSn_CR(sn) != 0)
		{
			if (getSn_IR(sn) & Sn_IR_TIMEOUT)
			{
				setSn_IR(sn, Sn_IR_TIMEOUT);
				return SOCKERR_TIMEOUT;
			}
		}
		break;
	case SO_KEEPALIVEAUTO:
		CHECK_SOCKMODE(Sn_MR_TCP);
		setSn_KPALVTR(sn,*(uint8_t*)arg);
		break;
	default:
		return SOCKERR_ARG;
	}
	return SOCK_OK;
}

int8_t getsockopt(uint8_t sn, sockopt_type sotype, void* arg)
{
   switch(sotype)
   {
      case SO_FLAG:
         *(uint8_t*)arg = getSn_MR(sn) & 0xF0;
         break;
      case SO_TTL:
         *(uint8_t*) arg = getSn_TTL(sn);
         break;
      case SO_TOS:
         *(uint8_t*) arg = getSn_TOS(sn);
         break;
      case SO_MSS:   
         *(uint8_t*) arg = getSn_MSSR(sn);
         break;
      case SO_DESTIP:
         getSn_DIPR(sn, (uint8_t*)arg);
         break;
      case SO_DESTPORT:  
         *(uint16_t*) arg = getSn_DPORT(sn);
         break;
      case SO_KEEPALIVEAUTO:
         CHECK_SOCKMODE(Sn_MR_TCP);
         *(uint16_t*) arg = getSn_KPALVTR(sn);
         break;
      case SO_SENDBUF:
         *(uint16_t*) arg = getSn_TX_FSR(sn);
         break;
      case SO_RECVBUF:
         *(uint16_t*) arg = getSn_RX_RSR(sn);
         break;
      case SO_STATUS:
         *(uint8_t*) arg = getSn_SR(sn);
         break;
      case SO_REMAINSIZE:
         if (getSn_MR(sn) == Sn_MR_TCP)
         {
            *(uint16_t*)arg = getSn_RX_RSR(sn);
         }
         else
         {
            *(uint16_t*)arg = sock_remained_size[sn];
         }
         break;
      case SO_PACKINFO:
         CHECK_SOCKMODE(Sn_MR_TCP);
         *(uint8_t*)arg = sock_pack_info[sn];
         break;
      default:
         return SOCKERR_SOCKOPT;
   }
   return SOCK_OK;
}

// End