-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathFreeRTOS_IP.c
More file actions
2781 lines (2398 loc) · 107 KB
/
FreeRTOS_IP.c
File metadata and controls
2781 lines (2398 loc) · 107 KB
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
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* FreeRTOS+TCP <DEVELOPMENT BRANCH>
* Copyright (C) 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* SPDX-License-Identifier: MIT
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* http://aws.amazon.com/freertos
* http://www.FreeRTOS.org
*/
/**
* @file FreeRTOS_IP.c
* @brief Implements the basic functionality for the FreeRTOS+TCP network stack.
*/
/* Standard includes. */
#include <stdint.h>
#include <stdio.h>
#include <string.h>
/* FreeRTOS includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"
/* FreeRTOS+TCP includes. */
#include "FreeRTOS_IP.h"
#include "FreeRTOS_ICMP.h"
#include "FreeRTOS_IP_Timers.h"
#include "FreeRTOS_IP_Utils.h"
#include "FreeRTOS_Sockets.h"
#include "FreeRTOS_IP_Private.h"
#include "FreeRTOS_ARP.h"
#include "FreeRTOS_ND.h"
#include "FreeRTOS_UDP_IP.h"
#include "FreeRTOS_DHCP.h"
#if ( ipconfigUSE_DHCPv6 == 1 )
#include "FreeRTOS_DHCPv6.h"
#endif
#include "NetworkInterface.h"
#include "NetworkBufferManagement.h"
#include "FreeRTOS_DNS.h"
#include "FreeRTOS_Routing.h"
#if ( ipconfigIS_ENABLED( ipconfigSUPPORT_IP_MULTICAST ) )
#include "FreeRTOS_IGMP.h"
#endif /* ipconfigIS_ENABLED( ipconfigSUPPORT_IP_MULTICAST ) */
/** @brief Time delay between repeated attempts to initialise the network hardware. */
#ifndef ipINITIALISATION_RETRY_DELAY
#define ipINITIALISATION_RETRY_DELAY ( pdMS_TO_TICKS( 3000U ) )
#endif
#if ( ipconfigUSE_TCP_MEM_STATS != 0 )
#include "tcp_mem_stats.h"
#endif
/** @brief Maximum time to wait for an ARP resolution while holding a packet. */
#ifndef ipARP_RESOLUTION_MAX_DELAY
#define ipARP_RESOLUTION_MAX_DELAY ( pdMS_TO_TICKS( 2000U ) )
#endif
/** @brief Maximum time to wait for a ND resolution while holding a packet. */
#ifndef ipND_RESOLUTION_MAX_DELAY
#define ipND_RESOLUTION_MAX_DELAY ( pdMS_TO_TICKS( 2000U ) )
#endif
/** @brief Defines how often the ARP resolution timer callback function is executed. The time is
* shorter in the Windows simulator as simulated time is not real time. */
#ifndef ipARP_TIMER_PERIOD_MS
#ifdef _WINDOWS_
#define ipARP_TIMER_PERIOD_MS ( 500U ) /* For windows simulator builds. */
#else
#define ipARP_TIMER_PERIOD_MS ( 10000U )
#endif
#endif
/** @brief Defines how often the ND resolution timer callback function is executed. The time is
* shorter in the Windows simulator as simulated time is not real time. */
#ifndef ipND_TIMER_PERIOD_MS
#ifdef _WINDOWS_
#define ipND_TIMER_PERIOD_MS ( 500U ) /* For windows simulator builds. */
#else
#define ipND_TIMER_PERIOD_MS ( 10000U )
#endif
#endif
#if ( ( ipconfigUSE_TCP == 1 ) && !defined( ipTCP_TIMER_PERIOD_MS ) )
/** @brief When initialising the TCP timer, give it an initial time-out of 1 second. */
#define ipTCP_TIMER_PERIOD_MS ( 1000U )
#endif
#ifndef iptraceIP_TASK_STARTING
#define iptraceIP_TASK_STARTING() do {} while( ipFALSE_BOOL ) /**< Empty definition in case iptraceIP_TASK_STARTING is not defined. */
#endif
/** @brief The frame type field in the Ethernet header must have a value greater than 0x0600.
* If the configuration option ipconfigFILTER_OUT_NON_ETHERNET_II_FRAMES is enabled, the stack
* will discard packets with a frame type value less than or equal to 0x0600.
* However, if this option is disabled, the stack will continue to process these packets. */
#define ipIS_ETHERNET_FRAME_TYPE_INVALID( usFrameType ) ( ( usFrameType ) <= 0x0600U )
static void prvCallDHCP_RA_Handler( NetworkEndPoint_t * pxEndPoint );
static void prvIPTask_Initialise( void );
static void prvIPTask_CheckPendingEvents( void );
/*-----------------------------------------------------------*/
/** @brief The pointer to buffer with packet waiting for ARP resolution. */
#if ipconfigIS_ENABLED( ipconfigUSE_IPv4 )
NetworkBufferDescriptor_t * pxARPWaitingNetworkBuffer = NULL;
#endif
/** @brief The pointer to buffer with packet waiting for ND resolution. */
#if ipconfigIS_ENABLED( ipconfigUSE_IPv6 )
NetworkBufferDescriptor_t * pxNDWaitingNetworkBuffer = NULL;
#endif
/*-----------------------------------------------------------*/
static void prvProcessIPEventsAndTimers( void );
/*
* The main TCP/IP stack processing task. This task receives commands/events
* from the network hardware drivers and tasks that are using sockets. It also
* maintains a set of protocol timers.
*/
static void prvIPTask( void * pvParameters );
/*
* Called when new data is available from the network interface.
*/
static void prvProcessEthernetPacket( NetworkBufferDescriptor_t * const pxNetworkBuffer );
#if ( ipconfigPROCESS_CUSTOM_ETHERNET_FRAMES != 0 )
/*
* The stack will call this user hook for all Ethernet frames that it
* does not support, i.e. other than IPv4, IPv6 and ARP ( for the moment )
* If this hook returns eReleaseBuffer or eProcessBuffer, the stack will
* release and reuse the network buffer. If this hook returns
* eReturnEthernetFrame, that means user code has reused the network buffer
* to generate a response and the stack will send that response out.
* If this hook returns eFrameConsumed, the user code has ownership of the
* network buffer and has to release it when it's done.
*/
extern eFrameProcessingResult_t eApplicationProcessCustomFrameHook( NetworkBufferDescriptor_t * const pxNetworkBuffer );
#endif /* ( ipconfigPROCESS_CUSTOM_ETHERNET_FRAMES != 0 ) */
/*
* Process incoming IP packets.
*/
static eFrameProcessingResult_t prvProcessIPPacket( const IPPacket_t * pxIPPacket,
NetworkBufferDescriptor_t * const pxNetworkBuffer );
/*
* The network card driver has received a packet. In the case that it is part
* of a linked packet chain, walk through it to handle every message.
*/
static void prvHandleEthernetPacket( NetworkBufferDescriptor_t * pxBuffer );
/* Handle the 'eNetworkTxEvent': forward a packet from an application to the NIC. */
static void prvForwardTxPacket( NetworkBufferDescriptor_t * pxNetworkBuffer,
BaseType_t xReleaseAfterSend );
static eFrameProcessingResult_t prvProcessUDPPacket( NetworkBufferDescriptor_t * const pxNetworkBuffer );
/*-----------------------------------------------------------*/
/** @brief The queue used to pass events into the IP-task for processing. */
QueueHandle_t xNetworkEventQueue = NULL;
/** @brief The IP packet ID. */
uint16_t usPacketIdentifier = 0U;
/** @brief For convenience, a MAC address of all 0xffs is defined const for quick
* reference. */
const MACAddress_t xBroadcastMACAddress = { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } };
/** @brief Used to ensure network down events cannot be missed when they cannot be
* posted to the network event queue because the network event queue is already
* full. */
static volatile BaseType_t xNetworkDownEventPending = pdFALSE;
/** @brief Stores the handle of the task that handles the stack. The handle is used
* (indirectly) by some utility function to determine if the utility function is
* being called by a task (in which case it is ok to block) or by the IP task
* itself (in which case it is not ok to block). */
static TaskHandle_t xIPTaskHandle = NULL;
/** @brief Set to pdTRUE when the IP task is ready to start processing packets. */
static BaseType_t xIPTaskInitialised = pdFALSE;
#if ( ipconfigCHECK_IP_QUEUE_SPACE != 0 )
/** @brief Keep track of the lowest amount of space in 'xNetworkEventQueue'. */
static UBaseType_t uxQueueMinimumSpace = ipconfigEVENT_QUEUE_LENGTH;
#endif
/*-----------------------------------------------------------*/
/* Coverity wants to make pvParameters const, which would make it incompatible. Leave the
* function signature as is. */
/**
* @brief The IP task handles all requests from the user application and the
* network interface. It receives messages through a FreeRTOS queue called
* 'xNetworkEventQueue'. prvIPTask() is the only task which has access to
* the data of the IP-stack, and so it has no need of using mutexes.
*
* @param[in] pvParameters Not used.
*/
/** @brief Stores interface structures. */
/* MISRA Ref 8.13.1 [Not decorating a pointer to const parameter with const] */
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-813 */
/* coverity[misra_c_2012_rule_8_13_violation] */
static void prvIPTask( void * pvParameters )
{
/* Just to prevent compiler warnings about unused parameters. */
( void ) pvParameters;
prvIPTask_Initialise();
FreeRTOS_debug_printf( ( "prvIPTask started\n" ) );
/* Loop, processing IP events. */
while( ipFOREVER() == pdTRUE )
{
prvProcessIPEventsAndTimers();
}
}
/*-----------------------------------------------------------*/
/**
* @brief Process the events sent to the IP task and process the timers.
*/
static void prvProcessIPEventsAndTimers( void )
{
IPStackEvent_t xReceivedEvent;
TickType_t xNextIPSleep;
FreeRTOS_Socket_t * pxSocket;
struct freertos_sockaddr xAddress;
ipconfigWATCHDOG_TIMER();
/* Check the Resolution, DHCP and TCP timers to see if there is any periodic
* or timeout processing to perform. */
vCheckNetworkTimers();
/* Calculate the acceptable maximum sleep time. */
xNextIPSleep = xCalculateSleepTime();
/* Wait until there is something to do. If the following call exits
* due to a time out rather than a message being received, set a
* 'NoEvent' value. */
if( xQueueReceive( xNetworkEventQueue, ( void * ) &xReceivedEvent, xNextIPSleep ) == pdFALSE )
{
xReceivedEvent.eEventType = eNoEvent;
}
#if ( ipconfigCHECK_IP_QUEUE_SPACE != 0 )
{
if( xReceivedEvent.eEventType != eNoEvent )
{
UBaseType_t uxCount;
uxCount = uxQueueSpacesAvailable( xNetworkEventQueue );
if( uxQueueMinimumSpace > uxCount )
{
uxQueueMinimumSpace = uxCount;
}
}
}
#endif /* ipconfigCHECK_IP_QUEUE_SPACE */
iptraceNETWORK_EVENT_RECEIVED( xReceivedEvent.eEventType );
switch( xReceivedEvent.eEventType )
{
case eNetworkDownEvent:
/* Attempt to establish a connection. */
prvProcessNetworkDownEvent( ( ( NetworkInterface_t * ) xReceivedEvent.pvData ) );
break;
case eNetworkRxEvent:
/* The network hardware driver has received a new packet. A
* pointer to the received buffer is located in the pvData member
* of the received event structure. */
prvHandleEthernetPacket( ( NetworkBufferDescriptor_t * ) xReceivedEvent.pvData );
break;
case eNetworkTxEvent:
/* Send a network packet. The ownership will be transferred to
* the driver, which will release it after delivery. */
prvForwardTxPacket( ( ( NetworkBufferDescriptor_t * ) xReceivedEvent.pvData ), pdTRUE );
break;
case eARPTimerEvent:
/* The ARP Resolution timer has expired, process the cache. */
#if ipconfigIS_ENABLED( ipconfigUSE_IPv4 )
vARPAgeCache();
#endif /* ( ipconfigUSE_IPv4 != 0 ) */
break;
case eNDTimerEvent:
/* The ND Resolution timer has expired, process the cache. */
#if ipconfigIS_ENABLED( ipconfigUSE_IPv6 )
vNDAgeCache();
#endif /* ( ipconfigUSE_IPv6 != 0 ) */
break;
case eSocketBindEvent:
/* FreeRTOS_bind (a user API) wants the IP-task to bind a socket
* to a port. The port number is communicated in the socket field
* usLocalPort. vSocketBind() will actually bind the socket and the
* API will unblock as soon as the eSOCKET_BOUND event is
* triggered. */
pxSocket = ( ( FreeRTOS_Socket_t * ) xReceivedEvent.pvData );
xAddress.sin_len = ( uint8_t ) sizeof( xAddress );
switch( pxSocket->bits.bIsIPv6 ) /* LCOV_EXCL_BR_LINE */
{
#if ( ipconfigUSE_IPv4 != 0 )
case pdFALSE_UNSIGNED:
xAddress.sin_family = FREERTOS_AF_INET;
xAddress.sin_address.ulIP_IPv4 = FreeRTOS_htonl( pxSocket->xLocalAddress.ulIP_IPv4 );
/* 'ulLocalAddress' will be set again by vSocketBind(). */
pxSocket->xLocalAddress.ulIP_IPv4 = 0;
break;
#endif /* ( ipconfigUSE_IPv4 != 0 ) */
#if ( ipconfigUSE_IPv6 != 0 )
case pdTRUE_UNSIGNED:
xAddress.sin_family = FREERTOS_AF_INET6;
( void ) memcpy( xAddress.sin_address.xIP_IPv6.ucBytes, pxSocket->xLocalAddress.xIP_IPv6.ucBytes, sizeof( xAddress.sin_address.xIP_IPv6.ucBytes ) );
/* 'ulLocalAddress' will be set again by vSocketBind(). */
( void ) memset( pxSocket->xLocalAddress.xIP_IPv6.ucBytes, 0, sizeof( pxSocket->xLocalAddress.xIP_IPv6.ucBytes ) );
break;
#endif /* ( ipconfigUSE_IPv6 != 0 ) */
default:
/* MISRA 16.4 Compliance */
break;
}
xAddress.sin_port = FreeRTOS_ntohs( pxSocket->usLocalPort );
/* 'usLocalPort' will be set again by vSocketBind(). */
pxSocket->usLocalPort = 0U;
( void ) vSocketBind( pxSocket, &xAddress, sizeof( xAddress ), pdFALSE );
/* Before 'eSocketBindEvent' was sent it was tested that
* ( xEventGroup != NULL ) so it can be used now to wake up the
* user. */
pxSocket->xEventBits |= ( EventBits_t ) eSOCKET_BOUND;
vSocketWakeUpUser( pxSocket );
break;
case eSocketCloseEvent:
/* The user API FreeRTOS_closesocket() has sent a message to the
* IP-task to actually close a socket. This is handled in
* vSocketClose(). As the socket gets closed, there is no way to
* report back to the API, so the API won't wait for the result */
( void ) vSocketClose( ( ( FreeRTOS_Socket_t * ) xReceivedEvent.pvData ) );
break;
case eStackTxEvent:
/* The network stack has generated a packet to send. A
* pointer to the generated buffer is located in the pvData
* member of the received event structure. */
vProcessGeneratedUDPPacket( ( NetworkBufferDescriptor_t * ) xReceivedEvent.pvData );
break;
case eDHCPEvent:
prvCallDHCP_RA_Handler( ( ( NetworkEndPoint_t * ) xReceivedEvent.pvData ) );
break;
case eSocketSelectEvent:
/* FreeRTOS_select() has got unblocked by a socket event,
* vSocketSelect() will check which sockets actually have an event
* and update the socket field xSocketBits. */
#if ( ipconfigSUPPORT_SELECT_FUNCTION == 1 )
#if ( ipconfigSELECT_USES_NOTIFY != 0 )
{
SocketSelectMessage_t * pxMessage = ( ( SocketSelectMessage_t * ) xReceivedEvent.pvData );
vSocketSelect( pxMessage->pxSocketSet );
( void ) xTaskNotifyGive( pxMessage->xTaskhandle );
}
#else
{
vSocketSelect( ( ( SocketSelect_t * ) xReceivedEvent.pvData ) );
}
#endif /* ( ipconfigSELECT_USES_NOTIFY != 0 ) */
#endif /* ipconfigSUPPORT_SELECT_FUNCTION == 1 */
break;
case eSocketSignalEvent:
#if ( ipconfigSUPPORT_SIGNALS != 0 )
/* Some task wants to signal the user of this socket in
* order to interrupt a call to recv() or a call to select(). */
( void ) FreeRTOS_SignalSocket( ( Socket_t ) xReceivedEvent.pvData );
#endif /* ipconfigSUPPORT_SIGNALS */
break;
case eTCPTimerEvent:
#if ( ipconfigUSE_TCP == 1 )
/* Simply mark the TCP timer as expired so it gets processed
* the next time prvCheckNetworkTimers() is called. */
vIPSetTCPTimerExpiredState( pdTRUE );
#endif /* ipconfigUSE_TCP */
break;
case eTCPAcceptEvent:
/* The API FreeRTOS_accept() was called, the IP-task will now
* check if the listening socket (communicated in pvData) actually
* received a new connection. */
#if ( ipconfigUSE_TCP == 1 )
pxSocket = ( ( FreeRTOS_Socket_t * ) xReceivedEvent.pvData );
if( xTCPCheckNewClient( pxSocket ) != pdFALSE )
{
pxSocket->xEventBits |= ( EventBits_t ) eSOCKET_ACCEPT;
vSocketWakeUpUser( pxSocket );
}
#endif /* ipconfigUSE_TCP */
break;
case eTCPNetStat:
/* FreeRTOS_netstat() was called to have the IP-task print an
* overview of all sockets and their connections */
#if ( ( ipconfigUSE_TCP == 1 ) && ( ipconfigHAS_PRINTF == 1 ) )
vTCPNetStat();
#endif /* ipconfigUSE_TCP */
break;
case eSocketSetDeleteEvent:
#if ( ipconfigSUPPORT_SELECT_FUNCTION == 1 )
{
SocketSelect_t * pxSocketSet = ( SocketSelect_t * ) ( xReceivedEvent.pvData );
iptraceMEM_STATS_DELETE( pxSocketSet );
vEventGroupDelete( pxSocketSet->xSelectGroup );
vPortFree( ( void * ) pxSocketSet );
}
#endif /* ipconfigSUPPORT_SELECT_FUNCTION == 1 */
break;
case eNoEvent:
/* xQueueReceive() returned because of a normal time-out. */
break;
#if ( ipconfigIS_ENABLED( ipconfigSUPPORT_IP_MULTICAST ) )
case eSocketOptAddMembership:
case eSocketOptDropMembership:
{
MulticastAction_t * pxMCA = ( MulticastAction_t * ) xReceivedEvent.pvData;
vModifyMulticastMembership( pxMCA, xReceivedEvent.eEventType );
break;
}
case eMulticastTimerEvent:
vIPMulticast_HandleTimerEvent();
break;
#endif /* ipconfigIS_ENABLED( ipconfigSUPPORT_IP_MULTICAST ) */
default:
/* Should not get here. */
break;
}
prvIPTask_CheckPendingEvents();
}
/*-----------------------------------------------------------*/
/**
* @brief Helper function for prvIPTask, it does the first initializations
* at start-up. No parameters, no return type.
*/
static void prvIPTask_Initialise( void )
{
NetworkInterface_t * pxInterface;
/* A possibility to set some additional task properties. */
iptraceIP_TASK_STARTING();
/* Generate a dummy message to say that the network connection has gone
* down. This will cause this task to initialise the network interface. After
* this it is the responsibility of the network interface hardware driver to
* send this message if a previously connected network is disconnected. */
vNetworkTimerReload( pdMS_TO_TICKS( ipINITIALISATION_RETRY_DELAY ) );
for( pxInterface = pxNetworkInterfaces; pxInterface != NULL; pxInterface = pxInterface->pxNext )
{
/* Post a 'eNetworkDownEvent' for every interface. */
FreeRTOS_NetworkDown( pxInterface );
}
#if ( ipconfigUSE_TCP == 1 )
{
/* Initialise the TCP timer. */
vTCPTimerReload( pdMS_TO_TICKS( ipTCP_TIMER_PERIOD_MS ) );
}
#endif
#if ipconfigIS_ENABLED( ipconfigUSE_IPv4 )
/* Mark the ARP timer as inactive since we are not waiting on any resolution as of now. */
vIPSetARPResolutionTimerEnableState( pdFALSE );
#endif
#if ipconfigIS_ENABLED( ipconfigUSE_IPv6 )
/* Mark the ND timer as inactive since we are not waiting on any resolution as of now. */
vIPSetNDResolutionTimerEnableState( pdFALSE );
#endif
#if ( ( ipconfigDNS_USE_CALLBACKS != 0 ) && ( ipconfigUSE_DNS != 0 ) )
{
/* The following function is declared in FreeRTOS_DNS.c and 'private' to
* this library */
vDNSInitialise();
}
#endif /* ( ipconfigDNS_USE_CALLBACKS != 0 ) && ( ipconfigUSE_DNS != 0 ) */
#if ( ( ipconfigUSE_DNS_CACHE != 0 ) && ( ipconfigUSE_DNS != 0 ) )
{
/* Clear the DNS cache once only. */
FreeRTOS_dnsclear();
}
#endif /* ( ( ipconfigUSE_DNS_CACHE != 0 ) && ( ipconfigUSE_DNS != 0 ) ) */
#if ( ipconfigIS_ENABLED( ipconfigSUPPORT_IP_MULTICAST ) )
/* Init the list that will hold scheduled IGMP reports. */
( void ) vIPMulticast_Init();
#endif /* ipconfigIS_ENABLED( ipconfigSUPPORT_IP_MULTICAST ) */
/* Initialisation is complete and events can now be processed. */
xIPTaskInitialised = pdTRUE;
}
/*-----------------------------------------------------------*/
/**
* @brief Check the value of 'xNetworkDownEventPending'. When non-zero, pending
* network-down events will be handled.
*/
static void prvIPTask_CheckPendingEvents( void )
{
NetworkInterface_t * pxInterface;
if( xNetworkDownEventPending != pdFALSE )
{
/* A network down event could not be posted to the network event
* queue because the queue was full.
* As this code runs in the IP-task, it can be done directly by
* calling prvProcessNetworkDownEvent(). */
xNetworkDownEventPending = pdFALSE;
for( pxInterface = FreeRTOS_FirstNetworkInterface();
pxInterface != NULL;
pxInterface = FreeRTOS_NextNetworkInterface( pxInterface ) )
{
if( pxInterface->bits.bCallDownEvent != pdFALSE_UNSIGNED )
{
prvProcessNetworkDownEvent( pxInterface );
pxInterface->bits.bCallDownEvent = pdFALSE_UNSIGNED;
}
}
}
}
/*-----------------------------------------------------------*/
/**
* @brief Call the state machine of either DHCP, DHCPv6, or RA, whichever is activated.
*
* @param[in] pxEndPoint The end-point for which the state-machine will be called.
*/
static void prvCallDHCP_RA_Handler( NetworkEndPoint_t * pxEndPoint )
{
BaseType_t xIsIPv6 = pdFALSE;
#if ( ( ipconfigUSE_DHCP == 1 ) || ( ipconfigUSE_DHCPv6 == 1 ) || ( ipconfigUSE_RA == 1 ) )
if( pxEndPoint->bits.bIPv6 != pdFALSE_UNSIGNED )
{
xIsIPv6 = pdTRUE;
}
#endif
/* The DHCP state machine needs processing. */
#if ( ipconfigUSE_DHCP == 1 )
{
if( ( pxEndPoint->bits.bWantDHCP != pdFALSE_UNSIGNED ) && ( xIsIPv6 == pdFALSE ) )
{
/* Process DHCP messages for a given end-point. */
vDHCPProcess( pdFALSE, pxEndPoint );
}
}
#endif /* ipconfigUSE_DHCP */
#if ( ( ipconfigUSE_DHCPv6 == 1 ) && ( ipconfigUSE_IPv6 != 0 ) )
{
if( ( xIsIPv6 == pdTRUE ) && ( pxEndPoint->bits.bWantDHCP != pdFALSE_UNSIGNED ) )
{
/* Process DHCPv6 messages for a given end-point. */
vDHCPv6Process( pdFALSE, pxEndPoint );
}
}
#endif /* ipconfigUSE_DHCPv6 */
#if ( ( ipconfigUSE_RA == 1 ) && ( ipconfigUSE_IPv6 != 0 ) )
{
if( ( xIsIPv6 == pdTRUE ) && ( pxEndPoint->bits.bWantRA != pdFALSE_UNSIGNED ) )
{
/* Process RA messages for a given end-point. */
vRAProcess( pdFALSE, pxEndPoint );
}
}
#endif /* ( ( ipconfigUSE_RA == 1 ) && ( ipconfigUSE_IPv6 != 0 ) ) */
/* Mention pxEndPoint and xIsIPv6 in case they have not been used. */
( void ) pxEndPoint;
( void ) xIsIPv6;
}
/*-----------------------------------------------------------*/
/**
* @brief The variable 'xIPTaskHandle' is declared static. This function
* gives read-only access to it.
*
* @return The handle of the IP-task.
*/
TaskHandle_t FreeRTOS_GetIPTaskHandle( void )
{
return xIPTaskHandle;
}
/*-----------------------------------------------------------*/
/**
* @brief Perform all the required tasks when the network gets connected.
*
* @param pxEndPoint The end-point which goes up.
*/
void vIPNetworkUpCalls( struct xNetworkEndPoint * pxEndPoint )
{
if( pxEndPoint->bits.bIPv6 == pdTRUE_UNSIGNED )
{
/* IPv6 end-points have a solicited-node address that needs extra housekeeping. */
#if ( ipconfigIS_ENABLED( ipconfigUSE_IPv6 ) )
vManageSolicitedNodeAddress( pxEndPoint, pdTRUE );
#endif
}
#if ( ipconfigIS_ENABLED( ipconfigSUPPORT_IP_MULTICAST ) )
/* Reschedule all multicast reports associated with this end-point.
* Note: countdown is in increments of ipIGMP_TIMER_PERIOD_MS. It's a good idea to spread out all reports a little.
* 200 to 500ms ( xMaxCountdown of 2 - 5 ) should be a good happy medium. If the network we just connected to has a IGMP/MLD querier,
* they will soon ask us for reports anyways, so sending these unsolicited reports is not required. It simply enhances the user
* experience by shortening the time it takes before we begin receiving the multicasts that we care for. */
vRescheduleAllMulticastReports( pxEndPoint->pxNetworkInterface, 5 );
#endif /* ipconfigIS_ENABLED( ipconfigSUPPORT_IP_MULTICAST ) */
pxEndPoint->bits.bEndPointUp = pdTRUE_UNSIGNED;
#if ( ipconfigUSE_NETWORK_EVENT_HOOK == 1 )
#if ( ipconfigIPv4_BACKWARD_COMPATIBLE == 1 )
{
vApplicationIPNetworkEventHook( eNetworkUp );
}
#else
{
vApplicationIPNetworkEventHook_Multi( eNetworkUp, pxEndPoint );
}
#endif
#endif /* ipconfigUSE_NETWORK_EVENT_HOOK */
/* Set remaining time to 0 so it will become active immediately. */
if( pxEndPoint->bits.bIPv6 == pdTRUE_UNSIGNED )
{
#if ipconfigIS_ENABLED( ipconfigUSE_IPv6 )
vNDTimerReload( pdMS_TO_TICKS( ipND_TIMER_PERIOD_MS ) );
#endif
}
else
{
#if ipconfigIS_ENABLED( ipconfigUSE_IPv4 )
vARPTimerReload( pdMS_TO_TICKS( ipARP_TIMER_PERIOD_MS ) );
#endif
}
}
/*-----------------------------------------------------------*/
/**
* @brief Handle the incoming Ethernet packets.
*
* @param[in] pxBuffer Linked/un-linked network buffer descriptor(s)
* to be processed.
*/
static void prvHandleEthernetPacket( NetworkBufferDescriptor_t * pxBuffer )
{
#if ( ipconfigUSE_LINKED_RX_MESSAGES == 0 )
{
/* When ipconfigUSE_LINKED_RX_MESSAGES is set to 0 then only one
* buffer will be sent at a time. This is the default way for +TCP to pass
* messages from the MAC to the TCP/IP stack. */
if( pxBuffer != NULL )
{
prvProcessEthernetPacket( pxBuffer );
}
}
#else /* ipconfigUSE_LINKED_RX_MESSAGES */
{
NetworkBufferDescriptor_t * pxNextBuffer;
/* An optimisation that is useful when there is high network traffic.
* Instead of passing received packets into the IP task one at a time the
* network interface can chain received packets together and pass them into
* the IP task in one go. The packets are chained using the pxNextBuffer
* member. The loop below walks through the chain processing each packet
* in the chain in turn. */
/* While there is another packet in the chain. */
while( pxBuffer != NULL )
{
/* Store a pointer to the buffer after pxBuffer for use later on. */
pxNextBuffer = pxBuffer->pxNextBuffer;
/* Make it NULL to avoid using it later on. */
pxBuffer->pxNextBuffer = NULL;
prvProcessEthernetPacket( pxBuffer );
pxBuffer = pxNextBuffer;
}
}
#endif /* ipconfigUSE_LINKED_RX_MESSAGES */
}
/*-----------------------------------------------------------*/
/**
* @brief Send a network packet.
*
* @param[in] pxNetworkBuffer The message buffer.
* @param[in] xReleaseAfterSend When true, the network interface will own the buffer and is responsible for it's release.
*/
static void prvForwardTxPacket( NetworkBufferDescriptor_t * pxNetworkBuffer,
BaseType_t xReleaseAfterSend )
{
iptraceNETWORK_INTERFACE_OUTPUT( pxNetworkBuffer->xDataLength, pxNetworkBuffer->pucEthernetBuffer );
if( pxNetworkBuffer->pxInterface != NULL )
{
( void ) pxNetworkBuffer->pxInterface->pfOutput( pxNetworkBuffer->pxInterface, pxNetworkBuffer, xReleaseAfterSend );
}
}
/*-----------------------------------------------------------*/
/**
* @brief Send a network down event to the IP-task. If it fails to post a message,
* the failure will be noted in the variable 'xNetworkDownEventPending'
* and later on a 'network-down' event, it will be executed.
*
* @param[in] pxNetworkInterface The interface that goes down.
*/
void FreeRTOS_NetworkDown( struct xNetworkInterface * pxNetworkInterface )
{
IPStackEvent_t xNetworkDownEvent;
const TickType_t xDontBlock = ( TickType_t ) 0;
pxNetworkInterface->bits.bInterfaceUp = pdFALSE_UNSIGNED;
xNetworkDownEvent.eEventType = eNetworkDownEvent;
xNetworkDownEvent.pvData = pxNetworkInterface;
/* Simply send the network task the appropriate event. */
if( xSendEventStructToIPTask( &xNetworkDownEvent, xDontBlock ) != pdPASS )
{
/* Could not send the message, so it is still pending. */
pxNetworkInterface->bits.bCallDownEvent = pdTRUE;
xNetworkDownEventPending = pdTRUE;
}
else
{
/* Message was sent so it is not pending. */
pxNetworkInterface->bits.bCallDownEvent = pdFALSE;
}
iptraceNETWORK_DOWN();
}
/*-----------------------------------------------------------*/
/**
* @brief Utility function. Process Network Down event from ISR.
* This function is supposed to be called form an ISR. It is recommended
* - * use 'FreeRTOS_NetworkDown()', when calling from a normal task.
*
* @param[in] pxNetworkInterface The interface that goes down.
*
* @return If the event was processed successfully, then return pdTRUE.
* Else pdFALSE.
*/
BaseType_t FreeRTOS_NetworkDownFromISR( struct xNetworkInterface * pxNetworkInterface )
{
IPStackEvent_t xNetworkDownEvent;
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
xNetworkDownEvent.eEventType = eNetworkDownEvent;
xNetworkDownEvent.pvData = pxNetworkInterface;
/* Simply send the network task the appropriate event. */
if( xQueueSendToBackFromISR( xNetworkEventQueue, &xNetworkDownEvent, &xHigherPriorityTaskWoken ) != pdPASS )
{
/* Could not send the message, so it is still pending. */
pxNetworkInterface->bits.bCallDownEvent = pdTRUE;
xNetworkDownEventPending = pdTRUE;
}
else
{
/* Message was sent so it is not pending. */
pxNetworkInterface->bits.bCallDownEvent = pdFALSE;
xNetworkDownEventPending = pdFALSE;
}
iptraceNETWORK_DOWN();
return xHigherPriorityTaskWoken;
}
/*-----------------------------------------------------------*/
#if ( ipconfigIPv4_BACKWARD_COMPATIBLE == 1 )
/**
* @brief Obtain a buffer big enough for a UDP payload of given size.
* NOTE: This function is kept for backward compatibility and will
* only allocate IPv4 payload buffers. Newer designs should use
* FreeRTOS_GetUDPPayloadBuffer_Multi(), which can
* allocate a IPv4 or IPv6 buffer based on ucIPType parameter .
*
* @param[in] uxRequestedSizeBytes The size of the UDP payload.
* @param[in] uxBlockTimeTicks Maximum amount of time for which this call
* can block. This value is capped internally.
*
* @return If a buffer was created then the pointer to that buffer is returned,
* else a NULL pointer is returned.
*/
void * FreeRTOS_GetUDPPayloadBuffer( size_t uxRequestedSizeBytes,
TickType_t uxBlockTimeTicks )
{
return FreeRTOS_GetUDPPayloadBuffer_Multi( uxRequestedSizeBytes, uxBlockTimeTicks, ipTYPE_IPv4 );
}
#endif /* if ( ipconfigIPv4_BACKWARD_COMPATIBLE == 1 ) */
/*-----------------------------------------------------------*/
/**
* @brief Obtain a buffer big enough for a UDP payload of given size and
* given IP type.
*
* @param[in] uxRequestedSizeBytes The size of the UDP payload.
* @param[in] uxBlockTimeTicks Maximum amount of time for which this call
* can block. This value is capped internally.
* @param[in] ucIPType Either ipTYPE_IPv4 (0x40) or ipTYPE_IPv6 (0x60)
*
* @return If a buffer was created then the pointer to that buffer is returned,
* else a NULL pointer is returned.
*/
void * FreeRTOS_GetUDPPayloadBuffer_Multi( size_t uxRequestedSizeBytes,
TickType_t uxBlockTimeTicks,
uint8_t ucIPType )
{
NetworkBufferDescriptor_t * pxNetworkBuffer;
void * pvReturn = NULL;
TickType_t uxBlockTime = uxBlockTimeTicks;
size_t uxPayloadOffset = 0U;
configASSERT( ( ucIPType == ipTYPE_IPv6 ) || ( ucIPType == ipTYPE_IPv4 ) );
/* Cap the block time. The reason for this is explained where
* ipconfigUDP_MAX_SEND_BLOCK_TIME_TICKS is defined (assuming an official
* FreeRTOSIPConfig.h header file is being used). */
if( uxBlockTime > ipconfigUDP_MAX_SEND_BLOCK_TIME_TICKS )
{
uxBlockTime = ipconfigUDP_MAX_SEND_BLOCK_TIME_TICKS;
}
switch( ucIPType ) /* LCOV_EXCL_BR_LINE */
{
#if ( ipconfigUSE_IPv4 != 0 )
case ipTYPE_IPv4:
uxPayloadOffset = sizeof( UDPPacket_t );
break;
#endif /* ( ipconfigUSE_IPv4 != 0 ) */
#if ( ipconfigUSE_IPv6 != 0 )
case ipTYPE_IPv6:
uxPayloadOffset = sizeof( UDPPacket_IPv6_t );
break;
#endif /* ( ipconfigUSE_IPv6 != 0 ) */
default:
/* Shouldn't reach here. */
/* MISRA 16.4 Compliance */
break;
}
if( uxPayloadOffset != 0U )
{
/* Obtain a network buffer with the required amount of storage. */
pxNetworkBuffer = pxGetNetworkBufferWithDescriptor( uxPayloadOffset + uxRequestedSizeBytes, uxBlockTime );
if( pxNetworkBuffer != NULL )
{
uint8_t * pucIPType;
size_t uxIndex = ipUDP_PAYLOAD_IP_TYPE_OFFSET;
BaseType_t xPayloadIPTypeOffset = ( BaseType_t ) uxIndex;
/* Set the actual packet size in case a bigger buffer was returned. */
pxNetworkBuffer->xDataLength = uxPayloadOffset + uxRequestedSizeBytes;
/* Skip 3 headers. */
pvReturn = ( void * ) &( pxNetworkBuffer->pucEthernetBuffer[ uxPayloadOffset ] );
/* Later a pointer to a UDP payload is used to retrieve a NetworkBuffer.
* Store the packet type at 48 bytes before the start of the UDP payload. */
pucIPType = ( uint8_t * ) pvReturn;
pucIPType = &( pucIPType[ -xPayloadIPTypeOffset ] );
/* For a IPv4 packet, pucIPType points to 6 bytes before the
* pucEthernetBuffer, for a IPv6 packet, pucIPType will point to the
* first byte of the IP-header: 'ucVersionTrafficClass'. */
*pucIPType = ucIPType;
}
}
return ( void * ) pvReturn;
}
/*-----------------------------------------------------------*/
/*_RB_ Should we add an error or assert if the task priorities are set such that the servers won't function as expected? */
/*_HT_ There was a bug in FreeRTOS_TCP_IP.c that only occurred when the applications' priority was too high.
* As that bug has been repaired, there is not an urgent reason to warn.
* It is better though to use the advised priority scheme. */
#if ( ipconfigIPv4_BACKWARD_COMPATIBLE == 1 ) && ( ipconfigUSE_IPv4 != 0 )
/* Provide backward-compatibility with the earlier FreeRTOS+TCP which only had
* single network interface. */
BaseType_t FreeRTOS_IPInit( const uint8_t ucIPAddress[ ipIP_ADDRESS_LENGTH_BYTES ],
const uint8_t ucNetMask[ ipIP_ADDRESS_LENGTH_BYTES ],
const uint8_t ucGatewayAddress[ ipIP_ADDRESS_LENGTH_BYTES ],
const uint8_t ucDNSServerAddress[ ipIP_ADDRESS_LENGTH_BYTES ],
const uint8_t ucMACAddress[ ipMAC_ADDRESS_LENGTH_BYTES ] )
{
static NetworkInterface_t xInterfaces[ 1 ];
static NetworkEndPoint_t xEndPoints[ 1 ];
/* IF the following function should be declared in the NetworkInterface.c
* linked in the project. */
( void ) pxFillInterfaceDescriptor( 0, &( xInterfaces[ 0 ] ) );
FreeRTOS_FillEndPoint( &( xInterfaces[ 0 ] ), &( xEndPoints[ 0 ] ), ucIPAddress, ucNetMask, ucGatewayAddress, ucDNSServerAddress, ucMACAddress );
#if ( ipconfigUSE_DHCP != 0 )
{
xEndPoints[ 0 ].bits.bWantDHCP = pdTRUE;
}
#endif /* ipconfigUSE_DHCP */
return FreeRTOS_IPInit_Multi();
}
#endif /* if ( ipconfigIPv4_BACKWARD_COMPATIBLE == 1 ) && ( ipconfigUSE_IPv4 != 0 ) */
/*-----------------------------------------------------------*/
/**
* @brief Initialise the FreeRTOS-Plus-TCP network stack and initialise the IP-task.
* Before calling this function, at least 1 interface and 1 end-point must
* have been set-up.