22//
33//! \file socket.c
44//! \brief SOCKET APIs Implements file.
5- //! \details SOCKET APIs like as Berkeley Socket APIs.
5+ //! \details SOCKET APIs like as Berkeley Socket APIs.
66//! \version 1.0.3
77//! \date 2013/10/21
88//! \par Revision history
99//! <2018/10/09> Nick Moore fixes for CircuitPython
1010//! <2014/05/01> V1.0.3. Refer to M20140501
1111//! 1. Implicit type casting -> Explicit type casting.
1212//! 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():
1414//! It occurs a fatal error on converting unint32 address if uint8* addr parameter is not aligned by 4byte address.
1515//! Copy 4 byte addr value into temporary uint32 variable and then compares it.
1616//! <2013/12/20> V1.0.2 Refer to M20131220
2323//!
2424//! Copyright (c) 2013, WIZnet Co., LTD.
2525//! 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.
3333//! * Redistributions in binary form must reproduce the above copyright
3434//! 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+ //!
4040//! 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
4242//! 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
4646//! 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
5050//! THE POSSIBILITY OF SUCH DAMAGE.
5151//
5252//*****************************************************************************
@@ -123,7 +123,7 @@ int8_t WIZCHIP_EXPORT(socket)(uint8_t sn, uint8_t protocol, uint16_t port, uint8
123123#if _WIZCHIP_ == 5200
124124 if (flag & 0x10 ) return SOCKERR_SOCKFLAG ;
125125#endif
126-
126+
127127 if (flag != 0 )
128128 {
129129 switch (protocol )
@@ -154,7 +154,7 @@ int8_t WIZCHIP_EXPORT(socket)(uint8_t sn, uint8_t protocol, uint16_t port, uint8
154154 port = sock_any_port ++ ;
155155 if (sock_any_port == 0xFFF0 ) sock_any_port = SOCK_ANY_PORT_NUM ;
156156 }
157- setSn_PORT (sn ,port );
157+ setSn_PORT (sn ,port );
158158 setSn_CR (sn ,Sn_CR_OPEN );
159159 while (getSn_CR (sn ));
160160 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
163163 sock_pack_info [sn ] = 0 ;
164164 while (getSn_SR (sn ) == SOCK_CLOSED );
165165 return (int8_t )sn ;
166- }
166+ }
167167
168168int8_t WIZCHIP_EXPORT (close )(uint8_t sn )
169169{
170170 CHECK_SOCKNUM ();
171-
171+
172172 setSn_CR (sn ,Sn_CR_CLOSE );
173173 /* wait to process the command... */
174174 while ( getSn_CR (sn ) );
@@ -216,18 +216,18 @@ int8_t WIZCHIP_EXPORT(connect)(uint8_t sn, uint8_t * addr, uint16_t port)
216216 if (taddr == 0xFFFFFFFF || taddr == 0 ) return SOCKERR_IPINVALID ;
217217 }
218218 //
219-
219+
220220 if (port == 0 ) return SOCKERR_PORTZERO ;
221221 setSn_DIPR (sn ,addr );
222222 setSn_DPORT (sn ,port );
223- #if _WIZCHIP_ == 5200 // for W5200 ARP errata
223+ #if _WIZCHIP_ == 5200 // for W5200 ARP errata
224224 setSUBR (wizchip_getsubn ());
225225 #endif
226226 setSn_CR (sn ,Sn_CR_CONNECT );
227227 while (getSn_CR (sn ));
228228 if (sock_io_mode & (1 <<sn )) return SOCK_BUSY ;
229229 while (getSn_SR (sn ) != SOCK_ESTABLISHED )
230- {
230+ {
231231 if (getSn_SR (sn ) == SOCK_CLOSED ) {
232232 #if _WIZCHIP_ == 5200 // for W5200 ARP errata
233233 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)
237237 if (getSn_IR (sn ) & Sn_IR_TIMEOUT )
238238 {
239239 setSn_IR (sn , Sn_IR_TIMEOUT );
240- #if _WIZCHIP_ == 5200 // for W5200 ARP errata
240+ #if _WIZCHIP_ == 5200 // for W5200 ARP errata
241241 setSUBR ((uint8_t * )"\x00\x00\x00\x00" );
242242 #endif
243243 return SOCKERR_TIMEOUT ;
244244 }
245245 MICROPY_THREAD_YIELD ();
246246 }
247- #if _WIZCHIP_ == 5200 // for W5200 ARP errata
247+ #if _WIZCHIP_ == 5200 // for W5200 ARP errata
248248 setSUBR ((uint8_t * )"\x00\x00\x00\x00" );
249249 #endif
250-
250+
251251 return SOCK_OK ;
252252}
253253
@@ -275,7 +275,7 @@ int32_t WIZCHIP_EXPORT(send)(uint8_t sn, uint8_t * buf, uint16_t len)
275275{
276276 uint8_t tmp = 0 ;
277277 uint16_t freesize = 0 ;
278-
278+
279279 CHECK_SOCKNUM ();
280280 CHECK_SOCKMODE (Sn_MR_TCP );
281281 CHECK_SOCKDATA ();
@@ -295,7 +295,7 @@ int32_t WIZCHIP_EXPORT(send)(uint8_t sn, uint8_t * buf, uint16_t len)
295295 return SOCKERR_BUSY ;
296296 }
297297 #endif
298- sock_is_sending &= ~(1 <<sn );
298+ sock_is_sending &= ~(1 <<sn );
299299 }
300300 else if (tmp & Sn_IR_TIMEOUT )
301301 {
@@ -338,7 +338,7 @@ int32_t WIZCHIP_EXPORT(recv)(uint8_t sn, uint8_t * buf, uint16_t len)
338338 CHECK_SOCKNUM ();
339339 CHECK_SOCKMODE (Sn_MR_TCP );
340340 CHECK_SOCKDATA ();
341-
341+
342342 recvsize = getSn_RxMAX (sn );
343343 if (recvsize < len ) len = recvsize ;
344344 while (1 )
@@ -398,9 +398,9 @@ int32_t WIZCHIP_EXPORT(sendto)(uint8_t sn, uint8_t * buf, uint16_t len, uint8_t
398398 if (port == 0 ) return SOCKERR_PORTZERO ;
399399 tmp = getSn_SR (sn );
400400 if (tmp != SOCK_MACRAW && tmp != SOCK_UDP ) return SOCKERR_SOCKSTATUS ;
401-
401+
402402 setSn_DIPR (sn ,addr );
403- setSn_DPORT (sn ,port );
403+ setSn_DPORT (sn ,port );
404404 freesize = getSn_TxMAX (sn );
405405 if (len > freesize ) len = freesize ; // check size not to exceed MAX size.
406406 while (1 )
@@ -413,7 +413,7 @@ int32_t WIZCHIP_EXPORT(sendto)(uint8_t sn, uint8_t * buf, uint16_t len, uint8_t
413413 };
414414 wiz_send_data (sn , buf , len );
415415
416- #if _WIZCHIP_ == 5200 // for W5200 ARP errata
416+ #if _WIZCHIP_ == 5200 // for W5200 ARP errata
417417 setSUBR (wizchip_getsubn ());
418418 #endif
419419
@@ -433,15 +433,15 @@ int32_t WIZCHIP_EXPORT(sendto)(uint8_t sn, uint8_t * buf, uint16_t len, uint8_t
433433 else if (tmp & Sn_IR_TIMEOUT )
434434 {
435435 setSn_IR (sn , Sn_IR_TIMEOUT );
436- #if _WIZCHIP_ == 5200 // for W5200 ARP errata
436+ #if _WIZCHIP_ == 5200 // for W5200 ARP errata
437437 setSUBR ((uint8_t * )"\x00\x00\x00\x00" );
438438 #endif
439439 return SOCKERR_TIMEOUT ;
440440 }
441441 ////////////
442442 MICROPY_THREAD_YIELD ();
443443 }
444- #if _WIZCHIP_ == 5200 // for W5200 ARP errata
444+ #if _WIZCHIP_ == 5200 // for W5200 ARP errata
445445 setSUBR ((uint8_t * )"\x00\x00\x00\x00" );
446446 #endif
447447 return len ;
@@ -462,7 +462,7 @@ int32_t WIZCHIP_EXPORT(recvfrom)(uint8_t sn, uint8_t * buf, uint16_t len, uint8_
462462 case Sn_MR_UDP :
463463 case Sn_MR_MACRAW :
464464 break ;
465- #if ( _WIZCHIP_ < 5200 )
465+ #if ( _WIZCHIP_ < 5200 )
466466 case Sn_MR_IPRAW :
467467 case Sn_MR_PPPoE :
468468 break ;
@@ -519,7 +519,7 @@ int32_t WIZCHIP_EXPORT(recvfrom)(uint8_t sn, uint8_t * buf, uint16_t len, uint8_
519519 sock_remained_size [sn ] = head [0 ];
520520 sock_remained_size [sn ] = (sock_remained_size [sn ] <<8 ) + head [1 ];
521521 sock_remained_size [sn ] -= 2 ; // len includes 2 len bytes
522- if (sock_remained_size [sn ] > 1514 )
522+ if (sock_remained_size [sn ] > 1514 )
523523 {
524524 WIZCHIP_EXPORT (close )(sn );
525525 return SOCKFATAL_PACKLEN ;
@@ -582,7 +582,7 @@ int8_t WIZCHIP_EXPORT(ctlsocket)(uint8_t sn, ctlsock_type cstype, void* arg)
582582 else if (tmp == SOCK_IO_BLOCK ) sock_io_mode &= ~(1 <<sn );
583583 else return SOCKERR_ARG ;
584584 break ;
585- case CS_GET_IOMODE :
585+ case CS_GET_IOMODE :
586586 //M20140501 : implict type casting -> explict type casting
587587 //*((uint8_t*)arg) = (sock_io_mode >> sn) & 0x0001;
588588 * ((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)
591591 case CS_GET_MAXTXBUF :
592592 * ((uint16_t * )arg ) = getSn_TxMAX (sn );
593593 break ;
594- case CS_GET_MAXRXBUF :
594+ case CS_GET_MAXRXBUF :
595595 * ((uint16_t * )arg ) = getSn_RxMAX (sn );
596596 break ;
597597 case CS_CLR_INTERRUPT :
@@ -601,11 +601,11 @@ int8_t WIZCHIP_EXPORT(ctlsocket)(uint8_t sn, ctlsock_type cstype, void* arg)
601601 case CS_GET_INTERRUPT :
602602 * ((uint8_t * )arg ) = getSn_IR (sn );
603603 break ;
604- case CS_SET_INTMASK :
604+ case CS_SET_INTMASK :
605605 if ( (* (uint8_t * )arg ) > SIK_ALL ) return SOCKERR_ARG ;
606606 setSn_IMR (sn ,* (uint8_t * )arg );
607607 break ;
608- case CS_GET_INTMASK :
608+ case CS_GET_INTMASK :
609609 * ((uint8_t * )arg ) = getSn_IMR (sn );
610610 default :
611611 return SOCKERR_ARG ;
@@ -658,11 +658,11 @@ int8_t WIZCHIP_EXPORT(setsockopt)(uint8_t sn, sockopt_type sotype, void* arg)
658658 CHECK_SOCKMODE (Sn_MR_TCP );
659659 setSn_KPALVTR (sn ,* (uint8_t * )arg );
660660 break ;
661- #endif
662- #endif
661+ #endif
662+ #endif
663663 default :
664664 return SOCKERR_ARG ;
665- }
665+ }
666666 return SOCK_OK ;
667667}
668668
@@ -680,20 +680,20 @@ int8_t WIZCHIP_EXPORT(getsockopt)(uint8_t sn, sockopt_type sotype, void* arg)
680680 case SO_TOS :
681681 * (uint8_t * ) arg = getSn_TOS (sn );
682682 break ;
683- case SO_MSS :
683+ case SO_MSS :
684684 * (uint8_t * ) arg = getSn_MSSR (sn );
685685 case SO_DESTIP :
686686 getSn_DIPR (sn , (uint8_t * )arg );
687687 break ;
688- case SO_DESTPORT :
688+ case SO_DESTPORT :
689689 * (uint16_t * ) arg = getSn_DPORT (sn );
690690 break ;
691- #if _WIZCHIP_ > 5200
691+ #if _WIZCHIP_ > 5200
692692 case SO_KEEPALIVEAUTO :
693693 CHECK_SOCKMODE (Sn_MR_TCP );
694694 * (uint16_t * ) arg = getSn_KPALVTR (sn );
695695 break ;
696- #endif
696+ #endif
697697 case SO_SENDBUF :
698698 * (uint16_t * ) arg = getSn_TX_FSR (sn );
699699 case SO_RECVBUF :
0 commit comments