2
2
//
3
3
//! \file socket.c
4
4
//! \brief SOCKET APIs Implements file.
5
- //! \details SOCKET APIs like as Berkeley Socket APIs.
5
+ //! \details SOCKET APIs like as Berkeley Socket APIs.
6
6
//! \version 1.0.3
7
7
//! \date 2013/10/21
8
8
//! \par Revision history
9
9
//! <2018/10/09> Nick Moore fixes for CircuitPython
10
10
//! <2014/05/01> V1.0.3. Refer to M20140501
11
11
//! 1. Implicit type casting -> Explicit type casting.
12
12
//! 2. replace 0x01 with PACK_REMAINED in recvfrom()
13
- //! 3. Validation a destination ip in connect() & sendto():
13
+ //! 3. Validation a destination ip in connect() & sendto():
14
14
//! It occurs a fatal error on converting unint32 address if uint8* addr parameter is not aligned by 4byte address.
15
15
//! Copy 4 byte addr value into temporary uint32 variable and then compares it.
16
16
//! <2013/12/20> V1.0.2 Refer to M20131220
23
23
//!
24
24
//! Copyright (c) 2013, WIZnet Co., LTD.
25
25
//! All rights reserved.
26
- //!
27
- //! Redistribution and use in source and binary forms, with or without
28
- //! modification, are permitted provided that the following conditions
29
- //! are met:
30
- //!
31
- //! * Redistributions of source code must retain the above copyright
32
- //! notice, this list of conditions and the following disclaimer.
26
+ //!
27
+ //! Redistribution and use in source and binary forms, with or without
28
+ //! modification, are permitted provided that the following conditions
29
+ //! are met:
30
+ //!
31
+ //! * Redistributions of source code must retain the above copyright
32
+ //! notice, this list of conditions and the following disclaimer.
33
33
//! * Redistributions in binary form must reproduce the above copyright
34
34
//! notice, this list of conditions and the following disclaimer in the
35
- //! documentation and/or other materials provided with the distribution.
36
- //! * Neither the name of the <ORGANIZATION> nor the names of its
37
- //! contributors may be used to endorse or promote products derived
38
- //! from this software without specific prior written permission.
39
- //!
35
+ //! documentation and/or other materials provided with the distribution.
36
+ //! * Neither the name of the <ORGANIZATION> nor the names of its
37
+ //! contributors may be used to endorse or promote products derived
38
+ //! from this software without specific prior written permission.
39
+ //!
40
40
//! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
41
- //! AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41
+ //! AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
42
//! IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43
- //! ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
44
- //! LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
45
- //! CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
43
+ //! ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
44
+ //! LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
45
+ //! CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
46
46
//! SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
47
- //! INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
48
- //! CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49
- //! ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
47
+ //! INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
48
+ //! CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49
+ //! ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
50
50
//! THE POSSIBILITY OF SUCH DAMAGE.
51
51
//
52
52
//*****************************************************************************
@@ -123,7 +123,7 @@ int8_t WIZCHIP_EXPORT(socket)(uint8_t sn, uint8_t protocol, uint16_t port, uint8
123
123
#if _WIZCHIP_ == 5200
124
124
if (flag & 0x10 ) return SOCKERR_SOCKFLAG ;
125
125
#endif
126
-
126
+
127
127
if (flag != 0 )
128
128
{
129
129
switch (protocol )
@@ -154,7 +154,7 @@ int8_t WIZCHIP_EXPORT(socket)(uint8_t sn, uint8_t protocol, uint16_t port, uint8
154
154
port = sock_any_port ++ ;
155
155
if (sock_any_port == 0xFFF0 ) sock_any_port = SOCK_ANY_PORT_NUM ;
156
156
}
157
- setSn_PORT (sn ,port );
157
+ setSn_PORT (sn ,port );
158
158
setSn_CR (sn ,Sn_CR_OPEN );
159
159
while (getSn_CR (sn ));
160
160
sock_io_mode |= ((flag & SF_IO_NONBLOCK ) << sn );
@@ -163,12 +163,12 @@ int8_t WIZCHIP_EXPORT(socket)(uint8_t sn, uint8_t protocol, uint16_t port, uint8
163
163
sock_pack_info [sn ] = 0 ;
164
164
while (getSn_SR (sn ) == SOCK_CLOSED );
165
165
return (int8_t )sn ;
166
- }
166
+ }
167
167
168
168
int8_t WIZCHIP_EXPORT (close )(uint8_t sn )
169
169
{
170
170
CHECK_SOCKNUM ();
171
-
171
+
172
172
setSn_CR (sn ,Sn_CR_CLOSE );
173
173
/* wait to process the command... */
174
174
while ( getSn_CR (sn ) );
@@ -216,18 +216,18 @@ int8_t WIZCHIP_EXPORT(connect)(uint8_t sn, uint8_t * addr, uint16_t port)
216
216
if (taddr == 0xFFFFFFFF || taddr == 0 ) return SOCKERR_IPINVALID ;
217
217
}
218
218
//
219
-
219
+
220
220
if (port == 0 ) return SOCKERR_PORTZERO ;
221
221
setSn_DIPR (sn ,addr );
222
222
setSn_DPORT (sn ,port );
223
- #if _WIZCHIP_ == 5200 // for W5200 ARP errata
223
+ #if _WIZCHIP_ == 5200 // for W5200 ARP errata
224
224
setSUBR (wizchip_getsubn ());
225
225
#endif
226
226
setSn_CR (sn ,Sn_CR_CONNECT );
227
227
while (getSn_CR (sn ));
228
228
if (sock_io_mode & (1 <<sn )) return SOCK_BUSY ;
229
229
while (getSn_SR (sn ) != SOCK_ESTABLISHED )
230
- {
230
+ {
231
231
if (getSn_SR (sn ) == SOCK_CLOSED ) {
232
232
#if _WIZCHIP_ == 5200 // for W5200 ARP errata
233
233
setSUBR ((uint8_t * )"\x00\x00\x00\x00" );
@@ -237,17 +237,17 @@ int8_t WIZCHIP_EXPORT(connect)(uint8_t sn, uint8_t * addr, uint16_t port)
237
237
if (getSn_IR (sn ) & Sn_IR_TIMEOUT )
238
238
{
239
239
setSn_IR (sn , Sn_IR_TIMEOUT );
240
- #if _WIZCHIP_ == 5200 // for W5200 ARP errata
240
+ #if _WIZCHIP_ == 5200 // for W5200 ARP errata
241
241
setSUBR ((uint8_t * )"\x00\x00\x00\x00" );
242
242
#endif
243
243
return SOCKERR_TIMEOUT ;
244
244
}
245
245
MICROPY_THREAD_YIELD ();
246
246
}
247
- #if _WIZCHIP_ == 5200 // for W5200 ARP errata
247
+ #if _WIZCHIP_ == 5200 // for W5200 ARP errata
248
248
setSUBR ((uint8_t * )"\x00\x00\x00\x00" );
249
249
#endif
250
-
250
+
251
251
return SOCK_OK ;
252
252
}
253
253
@@ -275,7 +275,7 @@ int32_t WIZCHIP_EXPORT(send)(uint8_t sn, uint8_t * buf, uint16_t len)
275
275
{
276
276
uint8_t tmp = 0 ;
277
277
uint16_t freesize = 0 ;
278
-
278
+
279
279
CHECK_SOCKNUM ();
280
280
CHECK_SOCKMODE (Sn_MR_TCP );
281
281
CHECK_SOCKDATA ();
@@ -295,7 +295,7 @@ int32_t WIZCHIP_EXPORT(send)(uint8_t sn, uint8_t * buf, uint16_t len)
295
295
return SOCKERR_BUSY ;
296
296
}
297
297
#endif
298
- sock_is_sending &= ~(1 <<sn );
298
+ sock_is_sending &= ~(1 <<sn );
299
299
}
300
300
else if (tmp & Sn_IR_TIMEOUT )
301
301
{
@@ -338,7 +338,7 @@ int32_t WIZCHIP_EXPORT(recv)(uint8_t sn, uint8_t * buf, uint16_t len)
338
338
CHECK_SOCKNUM ();
339
339
CHECK_SOCKMODE (Sn_MR_TCP );
340
340
CHECK_SOCKDATA ();
341
-
341
+
342
342
recvsize = getSn_RxMAX (sn );
343
343
if (recvsize < len ) len = recvsize ;
344
344
while (1 )
@@ -398,9 +398,9 @@ int32_t WIZCHIP_EXPORT(sendto)(uint8_t sn, uint8_t * buf, uint16_t len, uint8_t
398
398
if (port == 0 ) return SOCKERR_PORTZERO ;
399
399
tmp = getSn_SR (sn );
400
400
if (tmp != SOCK_MACRAW && tmp != SOCK_UDP ) return SOCKERR_SOCKSTATUS ;
401
-
401
+
402
402
setSn_DIPR (sn ,addr );
403
- setSn_DPORT (sn ,port );
403
+ setSn_DPORT (sn ,port );
404
404
freesize = getSn_TxMAX (sn );
405
405
if (len > freesize ) len = freesize ; // check size not to exceed MAX size.
406
406
while (1 )
@@ -413,7 +413,7 @@ int32_t WIZCHIP_EXPORT(sendto)(uint8_t sn, uint8_t * buf, uint16_t len, uint8_t
413
413
};
414
414
wiz_send_data (sn , buf , len );
415
415
416
- #if _WIZCHIP_ == 5200 // for W5200 ARP errata
416
+ #if _WIZCHIP_ == 5200 // for W5200 ARP errata
417
417
setSUBR (wizchip_getsubn ());
418
418
#endif
419
419
@@ -433,15 +433,15 @@ int32_t WIZCHIP_EXPORT(sendto)(uint8_t sn, uint8_t * buf, uint16_t len, uint8_t
433
433
else if (tmp & Sn_IR_TIMEOUT )
434
434
{
435
435
setSn_IR (sn , Sn_IR_TIMEOUT );
436
- #if _WIZCHIP_ == 5200 // for W5200 ARP errata
436
+ #if _WIZCHIP_ == 5200 // for W5200 ARP errata
437
437
setSUBR ((uint8_t * )"\x00\x00\x00\x00" );
438
438
#endif
439
439
return SOCKERR_TIMEOUT ;
440
440
}
441
441
////////////
442
442
MICROPY_THREAD_YIELD ();
443
443
}
444
- #if _WIZCHIP_ == 5200 // for W5200 ARP errata
444
+ #if _WIZCHIP_ == 5200 // for W5200 ARP errata
445
445
setSUBR ((uint8_t * )"\x00\x00\x00\x00" );
446
446
#endif
447
447
return len ;
@@ -462,7 +462,7 @@ int32_t WIZCHIP_EXPORT(recvfrom)(uint8_t sn, uint8_t * buf, uint16_t len, uint8_
462
462
case Sn_MR_UDP :
463
463
case Sn_MR_MACRAW :
464
464
break ;
465
- #if ( _WIZCHIP_ < 5200 )
465
+ #if ( _WIZCHIP_ < 5200 )
466
466
case Sn_MR_IPRAW :
467
467
case Sn_MR_PPPoE :
468
468
break ;
@@ -519,7 +519,7 @@ int32_t WIZCHIP_EXPORT(recvfrom)(uint8_t sn, uint8_t * buf, uint16_t len, uint8_
519
519
sock_remained_size [sn ] = head [0 ];
520
520
sock_remained_size [sn ] = (sock_remained_size [sn ] <<8 ) + head [1 ];
521
521
sock_remained_size [sn ] -= 2 ; // len includes 2 len bytes
522
- if (sock_remained_size [sn ] > 1514 )
522
+ if (sock_remained_size [sn ] > 1514 )
523
523
{
524
524
WIZCHIP_EXPORT (close )(sn );
525
525
return SOCKFATAL_PACKLEN ;
@@ -582,7 +582,7 @@ int8_t WIZCHIP_EXPORT(ctlsocket)(uint8_t sn, ctlsock_type cstype, void* arg)
582
582
else if (tmp == SOCK_IO_BLOCK ) sock_io_mode &= ~(1 <<sn );
583
583
else return SOCKERR_ARG ;
584
584
break ;
585
- case CS_GET_IOMODE :
585
+ case CS_GET_IOMODE :
586
586
//M20140501 : implict type casting -> explict type casting
587
587
//*((uint8_t*)arg) = (sock_io_mode >> sn) & 0x0001;
588
588
* ((uint8_t * )arg ) = (uint8_t )((sock_io_mode >> sn ) & 0x0001 );
@@ -591,7 +591,7 @@ int8_t WIZCHIP_EXPORT(ctlsocket)(uint8_t sn, ctlsock_type cstype, void* arg)
591
591
case CS_GET_MAXTXBUF :
592
592
* ((uint16_t * )arg ) = getSn_TxMAX (sn );
593
593
break ;
594
- case CS_GET_MAXRXBUF :
594
+ case CS_GET_MAXRXBUF :
595
595
* ((uint16_t * )arg ) = getSn_RxMAX (sn );
596
596
break ;
597
597
case CS_CLR_INTERRUPT :
@@ -601,11 +601,11 @@ int8_t WIZCHIP_EXPORT(ctlsocket)(uint8_t sn, ctlsock_type cstype, void* arg)
601
601
case CS_GET_INTERRUPT :
602
602
* ((uint8_t * )arg ) = getSn_IR (sn );
603
603
break ;
604
- case CS_SET_INTMASK :
604
+ case CS_SET_INTMASK :
605
605
if ( (* (uint8_t * )arg ) > SIK_ALL ) return SOCKERR_ARG ;
606
606
setSn_IMR (sn ,* (uint8_t * )arg );
607
607
break ;
608
- case CS_GET_INTMASK :
608
+ case CS_GET_INTMASK :
609
609
* ((uint8_t * )arg ) = getSn_IMR (sn );
610
610
default :
611
611
return SOCKERR_ARG ;
@@ -658,11 +658,11 @@ int8_t WIZCHIP_EXPORT(setsockopt)(uint8_t sn, sockopt_type sotype, void* arg)
658
658
CHECK_SOCKMODE (Sn_MR_TCP );
659
659
setSn_KPALVTR (sn ,* (uint8_t * )arg );
660
660
break ;
661
- #endif
662
- #endif
661
+ #endif
662
+ #endif
663
663
default :
664
664
return SOCKERR_ARG ;
665
- }
665
+ }
666
666
return SOCK_OK ;
667
667
}
668
668
@@ -680,20 +680,20 @@ int8_t WIZCHIP_EXPORT(getsockopt)(uint8_t sn, sockopt_type sotype, void* arg)
680
680
case SO_TOS :
681
681
* (uint8_t * ) arg = getSn_TOS (sn );
682
682
break ;
683
- case SO_MSS :
683
+ case SO_MSS :
684
684
* (uint8_t * ) arg = getSn_MSSR (sn );
685
685
case SO_DESTIP :
686
686
getSn_DIPR (sn , (uint8_t * )arg );
687
687
break ;
688
- case SO_DESTPORT :
688
+ case SO_DESTPORT :
689
689
* (uint16_t * ) arg = getSn_DPORT (sn );
690
690
break ;
691
- #if _WIZCHIP_ > 5200
691
+ #if _WIZCHIP_ > 5200
692
692
case SO_KEEPALIVEAUTO :
693
693
CHECK_SOCKMODE (Sn_MR_TCP );
694
694
* (uint16_t * ) arg = getSn_KPALVTR (sn );
695
695
break ;
696
- #endif
696
+ #endif
697
697
case SO_SENDBUF :
698
698
* (uint16_t * ) arg = getSn_TX_FSR (sn );
699
699
case SO_RECVBUF :
0 commit comments