2020-10-22 19:25:20 +02:00
/*
2020-11-10 10:58:01 +01:00
* © 2020 Gregor Baues . All rights reserved .
2020-10-22 19:25:20 +02:00
*
2020-11-10 10:58:01 +01:00
* This is free software : you can redistribute it and / or modify it under
* the terms of the GNU General Public License as published by the
* Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* 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 .
*
* See the GNU General Public License for more details < https : //www.gnu.org/licenses/>
2020-10-22 19:25:20 +02:00
*/
# include <Arduino.h>
2020-11-10 10:58:01 +01:00
2020-11-09 15:41:17 +01:00
# include "NetworkDiag.h"
2020-11-10 16:36:34 +01:00
# include "NetworkSetup.h"
2020-10-22 19:25:20 +02:00
# include "EthernetSetup.h"
2020-11-09 15:41:17 +01:00
byte EthernetSetup : : setup ( )
2020-10-22 19:25:20 +02:00
{
2020-11-10 16:36:34 +01:00
INFO ( F ( " Initialize MAC Address ... " ) ) ;
NetworkSetup : : genMacAddress ( ) ;
2020-10-22 19:25:20 +02:00
2020-11-09 15:41:17 +01:00
INFO ( F ( " Initialize Ethernet with DHCP " ) ) ;
2020-10-22 19:25:20 +02:00
if ( Ethernet . begin ( mac ) = = 0 )
{
2020-11-09 15:41:17 +01:00
WARN ( F ( " Failed to configure Ethernet using DHCP ... Trying with fixed IP " ) ) ;
2020-10-22 19:25:20 +02:00
Ethernet . begin ( mac , IPAddress ( IP_ADDRESS ) ) ; // default ip address
if ( Ethernet . hardwareStatus ( ) = = EthernetNoHardware )
{
2020-11-09 15:41:17 +01:00
ERR ( F ( " Ethernet shield was not found. Sorry, can't run without hardware. :( " ) ) ;
2020-10-22 19:25:20 +02:00
return 0 ;
} ;
if ( Ethernet . linkStatus ( ) = = LinkOFF )
{
2020-11-09 15:41:17 +01:00
ERR ( F ( " Ethernet cable is not connected. " ) ) ;
2020-10-22 19:25:20 +02:00
return 0 ;
}
}
maxConnections = MAX_SOCK_NUM ;
if ( Ethernet . hardwareStatus ( ) = = EthernetW5100 )
{
2020-11-09 15:41:17 +01:00
INFO ( F ( " W5100 Ethernet controller detected. " ) ) ;
2020-10-22 19:25:20 +02:00
maxConnections = 4 ; // Max supported officaly by the W5100 but i have been running over 8 as well. Perf has to be evaluated though comparing 4 vs. 8 connections
}
else if ( Ethernet . hardwareStatus ( ) = = EthernetW5200 )
{
2020-11-09 15:41:17 +01:00
INFO ( F ( " W5200 Ethernet controller detected. " ) ) ;
2020-10-22 19:25:20 +02:00
maxConnections = 8 ;
}
else if ( Ethernet . hardwareStatus ( ) = = EthernetW5500 )
{
2020-11-09 15:41:17 +01:00
INFO ( F ( " W5500 Ethernet controller detected. " ) ) ;
2020-10-22 19:25:20 +02:00
maxConnections = 8 ;
}
2020-11-10 16:36:34 +01:00
INFO ( F ( " Network Protocol: [%s] " ) , protocol ? " UDP " : " TCP " ) ;
2020-10-22 19:25:20 +02:00
switch ( protocol )
{
2020-11-09 17:00:24 +01:00
case UDPR :
2020-10-22 19:25:20 +02:00
{
2020-11-09 15:41:17 +01:00
udp = new EthernetUDP ( ) ;
byte udpState = udp - > begin ( port ) ;
if ( udpState )
2020-10-22 19:25:20 +02:00
{
2020-11-09 15:41:17 +01:00
TRC ( F ( " UDP status: %d " ) , udpState ) ;
2020-10-22 19:25:20 +02:00
maxConnections = 1 ; // there is only one UDP object listening for incomming data
connected = true ;
}
else
{
2020-11-09 18:13:11 +01:00
ERR ( F ( " UDP failed to start " ) ) ;
2020-10-22 19:25:20 +02:00
connected = false ;
}
break ;
} ;
case TCP :
{
server = new EthernetServer ( port ) ;
server - > begin ( ) ;
connected = true ;
break ;
} ;
case MQTT :
{
// do the MQTT setup stuff ...
} ;
default :
{
2020-11-09 15:41:17 +01:00
ERR ( F ( " \n Unkown Ethernet protocol; Setup failed " ) ) ;
2020-10-22 19:25:20 +02:00
connected = false ;
break ;
}
}
if ( connected )
{
ip = Ethernet . localIP ( ) ;
2020-11-10 16:36:34 +01:00
NetworkSetup : : printMacAddress ( NetworkSetup : : mac ) ;
INFO ( F ( " Local IP address: [%d.%d.%d.%d] " ) , ip [ 0 ] , ip [ 1 ] , ip [ 2 ] , ip [ 3 ] ) ;
INFO ( F ( " Listening on port: [%d] " ) , port ) ;
2020-10-22 19:25:20 +02:00
dnsip = Ethernet . dnsServerIP ( ) ;
2020-11-09 15:41:17 +01:00
INFO ( F ( " DNS server IP address: [%d.%d.%d.%d] " ) , dnsip [ 0 ] , dnsip [ 1 ] , dnsip [ 2 ] , dnsip [ 3 ] ) ;
INFO ( F ( " Number of connections: [%d] " ) , maxConnections ) ;
2020-11-09 18:48:17 +01:00
return true ;
2020-10-22 19:25:20 +02:00
}
2020-11-09 18:48:17 +01:00
return false ; // something went wrong
2020-10-22 19:25:20 +02:00
}
EthernetSetup : : EthernetSetup ( ) { }
EthernetSetup : : EthernetSetup ( uint16_t p , protocolType pt ) { port = p ; protocol = pt ; }
EthernetSetup : : ~ EthernetSetup ( ) { }