1 /*
2  * FreeRTOS+TCP <DEVELOPMENT BRANCH>
3  * Copyright (C) 2022 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
5  * SPDX-License-Identifier: MIT
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy of
8  * this software and associated documentation files (the "Software"), to deal in
9  * the Software without restriction, including without limitation the rights to
10  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11  * the Software, and to permit persons to whom the Software is furnished to do so,
12  * subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in all
15  * copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  * http://aws.amazon.com/freertos
25  * http://www.FreeRTOS.org
26  */
27 
28 
29 /* Include Unity header */
30 #include <unity.h>
31 
32 /* Include standard libraries */
33 #include <stdlib.h>
34 #include <string.h>
35 #include <stdint.h>
36 #include "FreeRTOS.h"
37 #include "task.h"
38 #include "list.h"
39 
40 /* ===========================  EXTERN VARIABLES  =========================== */
41 
42 extern const uint32_t ulDefaultIPv4Address;
43 extern BaseType_t xIsIfOutCalled;
44 
45 /* ======================== Stub Callback Functions ========================= */
46 
UDPReceiveHandlerChecker(Socket_t xSocket,void * pData,size_t xLength,const struct freertos_sockaddr * pxFrom,const struct freertos_sockaddr * pxDest)47 void UDPReceiveHandlerChecker( Socket_t xSocket,
48                                void * pData,
49                                size_t xLength,
50                                const struct freertos_sockaddr * pxFrom,
51                                const struct freertos_sockaddr * pxDest )
52 {
53     uint8_t * pucData = ( uint8_t * ) pData;
54     UDPPacket_t * pxUDPPacket = ( UDPPacket_t * ) ( pucData - ( ipSIZE_OF_ETH_HEADER + ipSIZE_OF_IPv4_HEADER + ipSIZE_OF_UDP_HEADER ) );
55 
56     TEST_ASSERT_EQUAL( pxUDPPacket->xIPHeader.ulSourceIPAddress, pxFrom->sin_address.ulIP_IPv4 );
57     TEST_ASSERT_EQUAL( pxUDPPacket->xIPHeader.ulDestinationIPAddress, pxDest->sin_address.ulIP_IPv4 );
58     TEST_ASSERT_EQUAL( pxUDPPacket->xUDPHeader.usSourcePort, pxFrom->sin_port );
59     TEST_ASSERT_EQUAL( pxUDPPacket->xUDPHeader.usDestinationPort, pxDest->sin_port );
60     TEST_ASSERT_EQUAL( FREERTOS_AF_INET4, pxFrom->sin_family );
61     TEST_ASSERT_EQUAL( FREERTOS_AF_INET4, pxDest->sin_family );
62     TEST_ASSERT_EQUAL( sizeof( struct freertos_sockaddr ), pxFrom->sin_len );
63     TEST_ASSERT_EQUAL( sizeof( struct freertos_sockaddr ), pxDest->sin_len );
64     TEST_ASSERT_EQUAL( pxUDPPacket->xIPHeader.usLength - ipSIZE_OF_IPv4_HEADER - ipSIZE_OF_UDP_HEADER, xLength );
65 }
66 
xStubUDPReceiveHandler_Pass(Socket_t xSocket,void * pData,size_t xLength,const struct freertos_sockaddr * pxFrom,const struct freertos_sockaddr * pxDest)67 BaseType_t xStubUDPReceiveHandler_Pass( Socket_t xSocket,
68                                         void * pData,
69                                         size_t xLength,
70                                         const struct freertos_sockaddr * pxFrom,
71                                         const struct freertos_sockaddr * pxDest )
72 {
73     UDPReceiveHandlerChecker( xSocket, pData, xLength, pxFrom, pxDest );
74     return 0;
75 }
76 
xStubUDPReceiveHandler_Fail(Socket_t xSocket,void * pData,size_t xLength,const struct freertos_sockaddr * pxFrom,const struct freertos_sockaddr * pxDest)77 BaseType_t xStubUDPReceiveHandler_Fail( Socket_t xSocket,
78                                         void * pData,
79                                         size_t xLength,
80                                         const struct freertos_sockaddr * pxFrom,
81                                         const struct freertos_sockaddr * pxDest )
82 {
83     UDPReceiveHandlerChecker( xSocket, pData, xLength, pxFrom, pxDest );
84     return -1;
85 }
86 
xNetworkInterfaceOutput(struct xNetworkInterface * pxDescriptor,NetworkBufferDescriptor_t * const pxNetworkBuffer,BaseType_t xReleaseAfterSend)87 BaseType_t xNetworkInterfaceOutput( struct xNetworkInterface * pxDescriptor,
88                                     NetworkBufferDescriptor_t * const pxNetworkBuffer,
89                                     BaseType_t xReleaseAfterSend )
90 {
91     xIsIfOutCalled = 1;
92 
93     return pdPASS;
94 }
95 
prvPrepareDefaultNetworkbuffer(uint8_t ucProtocol)96 NetworkBufferDescriptor_t * prvPrepareDefaultNetworkbuffer( uint8_t ucProtocol )
97 {
98     static NetworkBufferDescriptor_t xNetworkBuffer;
99     static uint8_t pucEthernetBuffer[ ipconfigTCP_MSS ];
100     uint16_t usSrcPort = 2048U;
101     uint16_t usDestPort = 1024U;
102     UDPPacket_t * pxUDPPacket;
103     ICMPPacket_t * pxICMPPacket;
104 
105     memset( &xNetworkBuffer, 0, sizeof( xNetworkBuffer ) );
106     memset( pucEthernetBuffer, 0, sizeof( pucEthernetBuffer ) );
107 
108     xNetworkBuffer.pucEthernetBuffer = pucEthernetBuffer;
109     xNetworkBuffer.usBoundPort = FreeRTOS_htons( usSrcPort );
110     xNetworkBuffer.usPort = FreeRTOS_htons( usDestPort );
111     xNetworkBuffer.xDataLength = ipconfigTCP_MSS;
112 
113     if( ucProtocol == ipPROTOCOL_UDP )
114     {
115         pxUDPPacket = ( UDPPacket_t * ) pucEthernetBuffer;
116         pxUDPPacket->xEthernetHeader.usFrameType = ipIPv4_FRAME_TYPE;
117     }
118     else if( ucProtocol == ipPROTOCOL_ICMP )
119     {
120         pxICMPPacket = ( ICMPPacket_t * ) pucEthernetBuffer;
121     }
122 
123     return &xNetworkBuffer;
124 }
125 
prvPrepareDefaultIPv4EndPoint()126 NetworkEndPoint_t * prvPrepareDefaultIPv4EndPoint()
127 {
128     static NetworkEndPoint_t xEndpoint;
129     static NetworkInterface_t xNetworkInterface;
130     NetworkEndPoint_t * pxEndpoint = &xEndpoint;
131 
132     memset( &xEndpoint, 0, sizeof( xEndpoint ) );
133     memset( &xNetworkInterface, 0, sizeof( xNetworkInterface ) );
134 
135     xNetworkInterface.pfOutput = xNetworkInterfaceOutput;
136 
137     xEndpoint.pxNetworkInterface = &xNetworkInterface;
138     xEndpoint.ipv4_settings.ulIPAddress = ulDefaultIPv4Address;
139     xEndpoint.bits.bIPv6 = pdFALSE;
140 
141     return pxEndpoint;
142 }
143 
vPortEnterCritical(void)144 void vPortEnterCritical( void )
145 {
146 }
147 
vPortExitCritical(void)148 void vPortExitCritical( void )
149 {
150 }
151