libraries / Mitov / Mitov_EthernetShield.hon commit Added link to project report (97a3ba0)
   1////////////////////////////////////////////////////////////////////////////////
   2//                                                                            //
   3//     This software is supplied under the terms of a license agreement or    //
   4//     nondisclosure agreement with Mitov Software and may not be copied      //
   5//     or disclosed except in accordance with the terms of that agreement.    //
   6//         Copyright(c) 2002-2016 Mitov Software. All Rights Reserved.        //
   7//                                                                            //
   8////////////////////////////////////////////////////////////////////////////////
   9
  10#ifndef _MITOV_ETHERNET_SHIELD_h
  11#define _MITOV_ETHERNET_SHIELD_h
  12
  13#include <Mitov.h>
  14#include <Mitov_BasicEthernet.h>
  15
  16namespace Mitov
  17{
  18//---------------------------------------------------------------------------
  19        class EthernetShield : public Mitov::BasicEthernetShield
  20        {
  21                typedef Mitov::BasicEthernetShield inherited;
  22
  23        protected:
  24                TMACAddress FMacAddress;
  25
  26        public:
  27                ShieldIPAddress IPAddress;
  28                bool    UseDHCP;
  29
  30        protected:
  31                virtual void StartEthernet()
  32                {
  33//                      int AIndex = ((int)Parity) * 2 * 4 + ( StopBits - 1 ) + ( DataBits - 5);
  34//                      T_SERIAL->begin( Speed );
  35
  36//                      Serial.println( "StartEthernet" );
  37
  38                        if( ! IPAddress.Enabled )
  39                                Ethernet.begin( FMacAddress.FMacAddress );
  40
  41                        else
  42                        {
  43                                if( UseDHCP )
  44                                        if( Ethernet.begin( FMacAddress.FMacAddress ))
  45                                                return;
  46
  47                                if( ! IPAddress.DNS.Enabled )
  48                                {
  49/*
  50                                        Serial.println( "StartEthernet IP" );
  51                                        Serial.print( FMacAddress.FMacAddress[ 0 ] );
  52                                        Serial.print( FMacAddress.FMacAddress[ 1 ] );
  53                                        Serial.print( FMacAddress.FMacAddress[ 2 ] );
  54                                        Serial.print( FMacAddress.FMacAddress[ 3 ] );
  55                                        Serial.println();
  56                                        IPAddress.IP.printTo( Serial );
  57                                        Serial.println();
  58*/
  59                                        Ethernet.begin( FMacAddress.FMacAddress, IPAddress.IP );
  60                                }
  61
  62                                else
  63                                {
  64                                        if( ! IPAddress.DNS.Gateway.Enabled )
  65                                                Ethernet.begin( FMacAddress.FMacAddress, IPAddress.IP, IPAddress.DNS.IP );
  66
  67                                        else
  68                                        {
  69                                                if( ! IPAddress.DNS.Gateway.Subnet.Enabled )
  70                                                        Ethernet.begin( FMacAddress.FMacAddress, IPAddress.IP, IPAddress.DNS.IP, IPAddress.DNS.Gateway.IP );
  71
  72                                                else
  73                                                        Ethernet.begin( FMacAddress.FMacAddress, IPAddress.IP, IPAddress.DNS.IP, IPAddress.DNS.Gateway.IP, IPAddress.DNS.Gateway.Subnet.IP );
  74
  75                                        }
  76                                }
  77                        }
  78                }
  79
  80        public:
  81                virtual bool GetIPFromHostName( String AHostName, ::IPAddress &AAdress )
  82                {
  83                        DNSClient ADNSClient;
  84
  85                        ADNSClient.begin( Ethernet.dnsServerIP() );
  86
  87                        bool AResult = ( ADNSClient.getHostByName( AHostName.c_str(), AAdress ) == 1 );
  88                        if( ! AResult )
  89                                AAdress = INADDR_NONE;
  90
  91                        return AResult;
  92                }
  93
  94        public:
  95                EthernetShield( TMACAddress AMacAddress ) :
  96                        FMacAddress( AMacAddress ),
  97                        UseDHCP( false )
  98                {
  99                }
 100
 101                EthernetShield( TMACAddress AMacAddress, bool UseDHCP, ::IPAddress local_ip) :
 102                        FMacAddress( AMacAddress ),
 103                        UseDHCP( UseDHCP )
 104                {
 105                        IPAddress.Enabled = true;
 106                        IPAddress.IP = local_ip;
 107                }
 108
 109                EthernetShield( TMACAddress AMacAddress, bool UseDHCP, ::IPAddress local_ip, ::IPAddress dns_server) :
 110                        FMacAddress( AMacAddress ),
 111                        UseDHCP( UseDHCP )
 112                {
 113                        IPAddress.Enabled = ! UseDHCP;
 114                        IPAddress.IP = local_ip;
 115                        IPAddress.DNS.Enabled = true;
 116                        IPAddress.DNS.IP = dns_server;
 117                }
 118
 119                EthernetShield( TMACAddress AMacAddress, bool UseDHCP, ::IPAddress local_ip, ::IPAddress dns_server, ::IPAddress gateway) :
 120                        FMacAddress( AMacAddress ),
 121                        UseDHCP( UseDHCP )
 122                {
 123                        IPAddress.Enabled = ! UseDHCP;
 124                        IPAddress.IP = local_ip;
 125                        IPAddress.DNS.Enabled = true;
 126                        IPAddress.DNS.IP = dns_server;
 127                        IPAddress.DNS.Gateway.Enabled = true;
 128                        IPAddress.DNS.Gateway.IP = dns_server;
 129                }
 130
 131                EthernetShield( TMACAddress AMacAddress, bool UseDHCP, ::IPAddress local_ip, ::IPAddress dns_server, ::IPAddress gateway, ::IPAddress subnet) :
 132                        FMacAddress( AMacAddress ),
 133                        UseDHCP( UseDHCP )
 134                {
 135                        IPAddress.Enabled = ! UseDHCP;
 136                        IPAddress.IP = local_ip;
 137                        IPAddress.DNS.Enabled = true;
 138                        IPAddress.DNS.IP = dns_server;
 139                        IPAddress.DNS.Gateway.Enabled = true;
 140                        IPAddress.DNS.Gateway.IP = dns_server;
 141                        IPAddress.DNS.Gateway.Subnet.Enabled = true;
 142                        IPAddress.DNS.Gateway.Subnet.IP = dns_server;
 143                }
 144        };
 145//---------------------------------------------------------------------------
 146}
 147
 148#endif