2626#include " FreeRTOS_DHCP.h"
2727#include " RTOSIface.h"
2828
29- constexpr size_t TcpPlusStackWords = 100 ; // needs to be aroud 240 when debugging with debugPrintf
29+ #include " lpc_phy.h"
30+
31+ constexpr size_t TcpPlusStackWords = 100 ; // needs to be around 240 when debugging with debugPrintf
3032static Task<TcpPlusStackWords> tcpPlusTask;
3133
32- constexpr size_t EmacStackWords = 65 ;/* 170 */ // needs to be bigger (>= 170) if using debugPrinf for testing
34+ constexpr size_t EmacStackWords = 65 ;// needs to be around 170 when debugging with debugPrintf
3335static Task<EmacStackWords> emacTask;
3436
3537static uint32_t lastIPTaskTime = 0 ;
36-
37-
38- RTOSPlusTCPEthernetInterface *rtosTCPEtherInterfacePtr; // pointer to the clas instance so we can call from the c hooks
39-
40- extern " C"
41- {
42-
43- // SD:: use the RFF tasks management to create tasks needed for +tcp
44- TaskHandle_t RRfInitialiseIPTask (TaskFunction_t pxTaskCode)
45- {
46- tcpPlusTask.Create (pxTaskCode, " IP-task" , nullptr , ( UBaseType_t ) TaskPriority::TcpPriority);
47- return tcpPlusTask.GetHandle ();
48- }
49-
50- TaskHandle_t RRfInitialiseEMACTask (TaskFunction_t pxTaskCode)
51- {
52- emacTask.Create (pxTaskCode, " EMAC" , nullptr , TaskPriority::TcpPriority);
53- return emacTask.GetHandle ();
54- }
55-
56- } // end extern "C"
57-
58-
38+ RTOSPlusTCPEthernetInterface *rtosTCPEtherInterfacePtr; // pointer to the clas instance so we can call from the "c" hooks
5939
6040
6141// TODO:: Currently NetworkInferface.c expects ucMACAddress to be externally defined....
@@ -64,8 +44,6 @@ uint8_t ucMACAddress[ 6 ];
6444
6545
6646
67-
68-
6947RTOSPlusTCPEthernetInterface::RTOSPlusTCPEthernetInterface (Platform& p)
7048: platform(p), lastTickMillis(0 ), state(NetworkState::disabled), activated(false ), initialised(false ), linkUp(false ), usingDHCP(false )
7149{
@@ -115,12 +93,9 @@ void RTOSPlusTCPEthernetInterface::Init()
11593 memcpy (macAddress, platform.GetDefaultMacAddress (), sizeof (macAddress));
11694}
11795
118-
119-
120-
12196GCodeResult RTOSPlusTCPEthernetInterface::EnableProtocol (NetworkProtocol protocol, int port, int secure, const StringRef& reply)
12297{
123- if (secure != 0 || secure != -1 )
98+ if (secure != 0 && secure != -1 )
12499 {
125100 reply.copy (" this firmware does not support TLS" );
126101 return GCodeResult::error;
@@ -262,7 +237,6 @@ void RTOSPlusTCPEthernetInterface::ReportOneProtocol(NetworkProtocol protocol, c
262237
263238// This is called at the end of config.g processing.
264239// Start the network if it was enabled
265-
266240void RTOSPlusTCPEthernetInterface::Activate ()
267241{
268242 if (!activated)
@@ -312,15 +286,13 @@ void RTOSPlusTCPEthernetInterface::Start()
312286{
313287 MutexLocker lock (interfaceMutex);
314288
315-
316289 if (initialised == true )
317290 {
318291 platform.Message (NetworkInfoMessage, " FreeRTOS+TCP already started. If changing IP or from static to dhpc then a reset is required.\n " );
319292 FreeRTOS_NetworkDown (); // send a network down event
320293 }
321294 else
322295 {
323-
324296 initialised = true ;
325297
326298 SetIPAddress (platform.GetIPAddress (), platform.NetMask (), platform.GateWay ());
@@ -343,10 +315,10 @@ void RTOSPlusTCPEthernetInterface::Start()
343315 usingDHCP = true ;
344316 }
345317
346-
347318 // FreeRTOS_IPInit should only be called once
348319 BaseType_t ret = FreeRTOS_IPInit ( ip, nm, gw, dns, macAddress );
349- if (ret == pdFALSE){
320+ if (ret == pdFALSE)
321+ {
350322 platform.Message (NetworkInfoMessage, " Failed to Init FreeRTOS+TCP\n " );
351323 state = NetworkState::disabled;
352324 return ;
@@ -359,44 +331,36 @@ void RTOSPlusTCPEthernetInterface::Start()
359331// Stop the network
360332void RTOSPlusTCPEthernetInterface::Stop ()
361333{
362-
363334 if (state != NetworkState::disabled)
364335 {
365336 MutexLocker lock (interfaceMutex);
366337 TerminateSockets ();
367338 state = NetworkState::disabled;
368339 // todo: how to stop FreeRTOS+TCP ? (perhaps suspend the IP-Task and EMAC task
369-
370340 }
371341}
372342
373343// Main spin loop. If 'full' is true then we are being called from the main spin loop. If false then we are being called during HSMCI idle time.
374344void RTOSPlusTCPEthernetInterface::Spin (bool full)
375345{
376-
377346 switch (state)
378347 {
379348 case NetworkState::enabled:
380349 case NetworkState::disabled:
350+ case NetworkState::establishingLink: // establishingLink state is handled by the callback handlers from RTOS +TCP
351+ case NetworkState::obtainingIP: // obtainingIP state is handled by the callback handlers from RTOS +TCP
381352 default :
382353 // Nothing to do
383354 break ;
384355
385-
386-
387- // establishingLink and obtainingIP states are handled by the callback handlers from RTOS +TCP
388- case NetworkState::establishingLink:
389- case NetworkState::obtainingIP:
390- break ;
391-
392356 case NetworkState::connected:
393357 {
394358 MutexLocker lock (interfaceMutex);
395359 InitSockets ();
396360 platform.MessageF (NetworkInfoMessage, " Network running, IP address = %s\n " , IP4String (ipAddress).c_str ());
397361 state = NetworkState::active;
398362 }
399- break ;
363+ break ;
400364
401365 case NetworkState::active:
402366 {
@@ -421,10 +385,9 @@ void RTOSPlusTCPEthernetInterface::Spin(bool full)
421385 {
422386 debugPrintf (" RTOSPlusEthernetInterface: Network Down while active\n " );
423387 }
424-
425388 }
426389 }
427- break ;
390+ break ;
428391 }
429392}
430393
@@ -434,29 +397,34 @@ void RTOSPlusTCPEthernetInterface::Diagnostics(MessageType mtype)
434397 switch (state)
435398 {
436399 case NetworkState::disabled:
437- platform.MessageF (mtype, " disabled\n " );
400+ platform.MessageF (mtype, " disabled" );
438401 return ;
439402 break ;
440403 case NetworkState::enabled:
441- platform.MessageF (mtype, " enabled\n " );
404+ platform.MessageF (mtype, " enabled" );
442405 break ;
443406 case NetworkState::establishingLink:
444- platform.MessageF (mtype, " establishing link\n " );
407+ platform.MessageF (mtype, " establishing link" );
445408 break ;
446409 case NetworkState::obtainingIP:
447- platform.MessageF (mtype, " obtaining IP\n " );
410+ platform.MessageF (mtype, " obtaining IP" );
448411 break ;
449412 case NetworkState::connected:
450- platform.MessageF (mtype, " connected\n " );
413+ platform.MessageF (mtype, " connected" );
451414 break ;
452415 case NetworkState::active:
453- platform.MessageF (mtype, " active\n " );
416+ platform.MessageF (mtype, " active" );
454417 break ;
455418 default :
456-
457419 break ;
458420 }
459421
422+ extern uint32_t ulPHYLinkStatus; // defined in NetworkInterface.c
423+ const char * const linkSpeed = ((ulPHYLinkStatus & PHY_LINK_CONNECTED) == 0 ) ? " down" : ((ulPHYLinkStatus & PHY_LINK_SPEED100) != 0 ) ? " 100Mbps" : " 10Mbps" ;
424+ const char * const linkDuplex = ((ulPHYLinkStatus & PHY_LINK_CONNECTED) == 0 ) ? " " : ((ulPHYLinkStatus & PHY_LINK_FULLDUPLX) != 0 ) ? " full duplex" : " half duplex" ;
425+ platform.MessageF (mtype, " , link %s%s\n " , linkSpeed, linkDuplex);
426+
427+ // Report Socket States
460428 platform.MessageF (mtype, " Socket States: " );
461429 for (RTOSPlusTCPEthernetSocket*& skt : sockets)
462430 {
@@ -468,15 +436,17 @@ void RTOSPlusTCPEthernetInterface::Diagnostics(MessageType mtype)
468436 platform.MessageF (mtype, " NetBuffers: %lu lowest: %lu\n " , uxGetNumberOfFreeNetworkBuffers (), uxGetMinimumFreeNetworkBuffers () );
469437 // Print out the minimum IP Queue space left since boot
470438 platform.MessageF (mtype, " IP Event Queue lowest: %d\n " , (int )uxGetMinimumIPQueueSpace ());
471- # if defined(COLLECT_NETDRIVER_ERROR_STATS)
439+ #endif
440+
441+ #if defined(COLLECT_NETDRIVER_ERROR_STATS)
472442 // defined in driver for debugging
473443 extern uint32_t numNetworkRXIntOverrunErrors; // hardware producted overrun error
474444 extern uint32_t numNetworkDroppedPacketsDueToNoBuffer;
475445 extern uint32_t numRejectedStackPackets;
476446
477- platform.MessageF (mtype, " EthDrv: Rejected packets by IPStack (timeout or full) : %lu\n " , numRejectedStackPackets );
478- platform.MessageF (mtype, " EthDrv: RX IntOverrun Errors: %lu\n " , numNetworkRXIntOverrunErrors);
479- platform.MessageF (mtype, " EthDrv: Dropped packets (no buffer): %lu\n " , numNetworkDroppedPacketsDueToNoBuffer );
447+ if (numRejectedStackPackets> 0 ) platform.MessageF (mtype, " EthDrv: Rejected packets by IPStack: %lu\n " , numRejectedStackPackets );
448+ if (numNetworkRXIntOverrunErrors> 0 ) platform.MessageF (mtype, " EthDrv: RX IntOverrun Errors: %lu\n " , numNetworkRXIntOverrunErrors);
449+ if (numNetworkDroppedPacketsDueToNoBuffer> 0 ) platform.MessageF (mtype, " EthDrv: Dropped packets (no buffer): %lu\n " , numNetworkDroppedPacketsDueToNoBuffer );
480450
481451 extern uint32_t numNetworkCRCErrors;
482452 extern uint32_t numNetworkSYMErrors;
@@ -486,23 +456,22 @@ void RTOSPlusTCPEthernetInterface::Diagnostics(MessageType mtype)
486456
487457 bool errors = false ;
488458 if (numNetworkCRCErrors || numNetworkSYMErrors || numNetworkLENErrors || numNetworkALIGNErrors || numNetworkOVERRUNErrors) errors=true ;
489- platform. MessageF (mtype, " EthDrv RX Errors: %s " , ( errors)? " " : " none " );
490- if (numNetworkCRCErrors > 0 ) platform. MessageF (mtype, " CRC(%lu) " , numNetworkCRCErrors);
491- if (numNetworkSYMErrors > 0 ) platform.MessageF (mtype, " SYM(%lu) " , numNetworkSYMErrors );
492- if (numNetworkLENErrors > 0 ) platform.MessageF (mtype, " LEN (%lu) " , numNetworkLENErrors );
493- if (numNetworkALIGNErrors > 0 ) platform.MessageF (mtype, " ALIGN (%lu) " , numNetworkALIGNErrors );
494- if (numNetworkOVERRUNErrors > 0 ) platform.MessageF (mtype, " OVERRUN (%lu) " , numNetworkOVERRUNErrors );
495- platform.MessageF (mtype, " \n " );
496- # endif
497-
498-
459+ if ( errors)
460+ {
461+ platform.MessageF (mtype, " EthDrv RX Errors: %s " , (errors)? " " : " none " );
462+ if (numNetworkCRCErrors > 0 ) platform.MessageF (mtype, " CRC (%lu) " , numNetworkCRCErrors );
463+ if (numNetworkSYMErrors > 0 ) platform.MessageF (mtype, " SYM (%lu) " , numNetworkSYMErrors );
464+ if (numNetworkLENErrors > 0 ) platform.MessageF (mtype, " LEN (%lu) " , numNetworkLENErrors );
465+ if (numNetworkALIGNErrors > 0 ) platform.MessageF (mtype, " ALIGN(%lu) " , numNetworkALIGNErrors );
466+ if (numNetworkOVERRUNErrors > 0 ) platform. MessageF (mtype, " OVERRUN(%lu) " , numNetworkOVERRUNErrors);
467+ platform. MessageF (mtype, " \n " );
468+ }
499469#endif
500-
470+
501471 if (millis () - lastIPTaskTime > 5000 )
502472 {
503- platform.MessageF (mtype, " IPTask has been stuck for over 5 seconds\n " );
473+ platform.MessageF (mtype, " IPTask has not been running for over 5 seconds\n " );
504474 }
505-
506475}
507476
508477// Enable or disable the network
@@ -542,7 +511,6 @@ void RTOSPlusTCPEthernetInterface::SetIPAddress(IPAddress p_ip, IPAddress p_netm
542511 gateway = p_gateway;
543512}
544513
545-
546514void RTOSPlusTCPEthernetInterface::OpenDataPort (Port port)
547515{
548516#if ENABLE_FTP
@@ -584,11 +552,13 @@ void RTOSPlusTCPEthernetInterface::TerminateSockets()
584552
585553
586554// ******* Handlers called from +TCP callbacks *******
587- // Note:: These hooks are called from the IP Task.
555+ //
556+ // **Note** These hooks are called from within the IP Task.
588557
589558
590559// get the hostname
591- const char *RTOSPlusTCPEthernetInterface::ProcessApplicationHostnameHook ( void ){
560+ const char *RTOSPlusTCPEthernetInterface::ProcessApplicationHostnameHook ( void )
561+ {
592562 return reprap.GetNetwork ().GetHostname ();// printerHostName;
593563}
594564
@@ -607,13 +577,11 @@ void RTOSPlusTCPEthernetInterface::ProcessIPApplication( eIPCallbackEvent_t eNet
607577 /* Print out the network configuration, which may have come from a DHCP
608578 server. */
609579 FreeRTOS_GetAddressConfiguration ( &ulIPAddress, &ulNetMask, &ulGatewayAddress, &ulDNSServerAddress );
610-
611580
612581 if ( ulIPAddress == 0 ) // ulIPAddress is in 32bit format
613582 {
614583 // IP address equals 0.0.0.0 here. DHCP has Failed.
615584 state = NetworkState::disabled;
616- platform.Message (NetworkInfoMessage, " Failed to obtain IP Address from DHCP\n " );
617585 }
618586 else
619587 {
@@ -627,11 +595,9 @@ void RTOSPlusTCPEthernetInterface::ProcessIPApplication( eIPCallbackEvent_t eNet
627595 }
628596 else if (eNetworkEvent == eNetworkDown)
629597 {
630-
631598 if (linkUp == true )
632599 {
633600 linkUp = false ;
634- platform.Message (NetworkInfoMessage, " Lost Network Link\n " );
635601 // Link was previously up but has gone down. We need to close all sockets including server socket
636602 // and Reinitialise after link comes back up
637603 TerminateSockets ();
@@ -640,7 +606,6 @@ void RTOSPlusTCPEthernetInterface::ProcessIPApplication( eIPCallbackEvent_t eNet
640606 state = NetworkState::establishingLink; // back to establishing link
641607 }
642608 else {
643- platform.Message (NetworkInfoMessage, " Network Other MSG\n " );
644609
645610 }
646611
@@ -703,15 +668,44 @@ eDHCPCallbackAnswer_t RTOSPlusTCPEthernetInterface::ProcessDHCPHook( eDHCPCallba
703668
704669}
705670
671+ // Call back to check if the Queried Name from Netbios or LLNMR matches our hostname
672+ BaseType_t RTOSPlusTCPEthernetInterface::ProcessDNSQueryHook (const char *pcName)
673+ {
674+ if (StringEqualsIgnoreCase (pcName, reprap.GetNetwork ().GetHostname ()) ) return pdPASS;
675+ return pdFAIL;
676+ }
677+
678+
679+
680+
681+ // Called by modified FreeRTOS +TCP to create tasks for EMAC and IP using RRF task management methods
682+
683+
684+ extern " C" TaskHandle_t RRfInitialiseIPTask (TaskFunction_t pxTaskCode)
685+ {
686+ tcpPlusTask.Create (pxTaskCode, " IP-task" , nullptr , ( UBaseType_t ) TaskPriority::TcpPriority);
687+ return tcpPlusTask.GetHandle ();
688+ }
689+
690+ extern " C" TaskHandle_t RRfInitialiseEMACTask (TaskFunction_t pxTaskCode)
691+ {
692+ emacTask.Create (pxTaskCode, " EMAC" , nullptr , TaskPriority::TcpPriority);
693+ return emacTask.GetHandle ();
694+ }
695+
696+
697+
706698// FreeRTOS +TCP "C" application Hooks
707699
700+
701+
708702/* Called by FreeRTOS+TCP when the network connects or disconnects. Disconnect events are only received if implemented in the MAC driver. */
709703extern " C" void vApplicationIPNetworkEventHook ( eIPCallbackEvent_t eNetworkEvent )
710704{
711705 (rtosTCPEtherInterfacePtr)->ProcessIPApplication (eNetworkEvent); // call the c++ class to handle the callback
712706}
713707
714- // // RTOS +TCP DHCP Hook.... this allows us to control over the DHCP process.
708+ // FreeRTOS +TCP DHCP Hook.... this allows us to control over the DHCP process.
715709extern " C" eDHCPCallbackAnswer_t xApplicationDHCPHook ( eDHCPCallbackPhase_t eDHCPPhase, uint32_t ulIPAddress )
716710{
717711 return (rtosTCPEtherInterfacePtr)->ProcessDHCPHook (eDHCPPhase, ulIPAddress);
@@ -722,6 +716,11 @@ extern "C" const char *pcApplicationHostnameHook( void )
722716 return (rtosTCPEtherInterfacePtr)->ProcessApplicationHostnameHook ();
723717}
724718
719+ extern " C" BaseType_t xApplicationDNSQueryHook ( const char *pcName )
720+ {
721+ return (rtosTCPEtherInterfacePtr)->ProcessDNSQueryHook (pcName);
722+ }
723+
725724// called every iteration of IP Task
726725extern " C" void IPTaskWatchDogTimer ()
727726{
0 commit comments