libraries / Mitov / Mitov_WiFiShield.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_WIFI_SHIELD_h
  11#define _MITOV_WIFI_SHIELD_h
  12
  13#include <Mitov.h>
  14#include <Mitov_BasicEthernet.h>
  15
  16namespace Mitov
  17{
  18//      class BasicEthernetSocket;
  19//---------------------------------------------------------------------------
  20        class WiFiRemoteAccessPoint
  21        {
  22        public:
  23                bool    Enabled = true;
  24                String  SSID;
  25
  26        public:
  27                bool Connect()
  28                {
  29                        if( ! Enabled )
  30                                return false;
  31
  32//                      if( SSID == "" )
  33//                              return ( WiFi.begin() == WL_CONNECTED );
  34
  35                        return IntConnect();
  36                }
  37
  38        protected:
  39                virtual bool IntConnect()
  40                {
  41                        return ( WiFi.begin( (char *)SSID.c_str() ) == WL_CONNECTED );
  42                }
  43
  44        };
  45//---------------------------------------------------------------------------
  46        class WiFiRemoteAccessPointWPA : public WiFiRemoteAccessPoint
  47        {
  48        public:
  49                String  Password;
  50
  51        protected:
  52                virtual bool IntConnect()
  53                {
  54                        return ( WiFi.begin( (char *)SSID.c_str(), (char *)Password.c_str() ) == WL_CONNECTED );
  55                }
  56        };
  57//---------------------------------------------------------------------------
  58        class WiFiRemoteAccessPointWEP : public WiFiRemoteAccessPoint
  59        {
  60        public:
  61                String  Key;
  62                int     KeyIndex = 0;
  63
  64        protected:
  65                virtual bool IntConnect()
  66                {
  67                        return ( WiFi.begin( (char *)SSID.c_str(), KeyIndex, (char *)Key.c_str() ) == WL_CONNECTED );
  68                }
  69
  70        };
  71//---------------------------------------------------------------------------
  72        class WiFiShield : public Mitov::BasicEthernetShield
  73        {
  74                typedef Mitov::BasicEthernetShield inherited;
  75
  76        public:
  77                OpenWire::SinkPin       ScanNetworksInputPin;
  78                OpenWire::SourcePin     FoundSignalStrengthOutputPin;
  79                OpenWire::SourcePin     FoundSSIDOutputPin;
  80
  81                OpenWire::SourcePin     AddressOutputPin;
  82                OpenWire::SourcePin     MACOutputPin;
  83
  84                OpenWire::SourcePin     SignalStrengthOutputPin;
  85                OpenWire::SourcePin     BSSIDOutputPin;
  86                OpenWire::SourcePin     GatewayIPOutputPin;
  87                OpenWire::SourcePin     SubnetMaskIPOutputPin;
  88
  89                OpenWire::SinkPin       CheckSignalStrengthInputPin;
  90
  91        public:
  92                ShieldIPDNS2Address     IPAddress;
  93
  94        public:
  95                Mitov::SimpleObjectList<WiFiRemoteAccessPoint*> AccessPoints;
  96
  97        public:
  98                bool IsStarted = false;
  99
 100        public:
 101                virtual bool GetIPFromHostName( String AHostName, ::IPAddress &AAdress )
 102                {
 103                        bool AResult = ( WiFi.hostByName( AHostName.c_str(), AAdress ) == 1 );
 104                        if( ! AResult )
 105                                AAdress = INADDR_NONE;
 106
 107                        return AResult;
 108                }
 109
 110        protected:
 111                virtual void StartEthernet()
 112                {
 113//                      int AIndex = ((int)Parity) * 2 * 4 + ( StopBits - 1 ) + ( DataBits - 5);
 114//                      T_SERIAL->begin( Speed );
 115
 116//                      Serial.println( "StartEthernet" );
 117                        for( int i = 0; i < AccessPoints.size(); ++i )
 118                                if( AccessPoints[ i ]->Connect() )
 119                                {
 120                                        IsStarted = true;
 121                                        if( IPAddress.Enabled )
 122                                        {
 123                                                if( ! IPAddress.DNS.Enabled )
 124                                                        WiFi.config( IPAddress.IP );
 125
 126                                                else
 127                                                {
 128                                                        if( ! IPAddress.DNS.Gateway.Enabled )
 129                                                                WiFi.config( IPAddress.IP, IPAddress.DNS.IP );
 130
 131                                                        else
 132                                                        {
 133                                                                if( ! IPAddress.DNS.Gateway.Subnet.Enabled )
 134                                                                        WiFi.config( IPAddress.IP, IPAddress.DNS.IP, IPAddress.DNS.Gateway.IP );
 135
 136                                                                else
 137                                                                        WiFi.config( IPAddress.IP, IPAddress.DNS.IP, IPAddress.DNS.Gateway.IP, IPAddress.DNS.Gateway.Subnet.IP );
 138                                                        }
 139
 140                                                        if( IPAddress.DNS2.Enabled )
 141                                                                WiFi.setDNS( IPAddress.DNS.IP, IPAddress.DNS2.IP );
 142
 143                                                }
 144                                        }
 145
 146                                        if( MACOutputPin.IsConnected() )
 147                                        {
 148                                                uint8_t AMACAddress[6] = {0};
 149
 150                                                WiFi.macAddress( AMACAddress );
 151
 152                                                char AMACString[ 6 * 3 + 1 ];
 153                                                sprintf( AMACString, "%02X-%02X-%02X-%02X-%02X-%02X", AMACString[ 0 ], AMACString[ 1 ], AMACString[ 2 ], AMACString[ 3 ], AMACString[ 4 ], AMACString[ 5 ] );
 154                                                MACOutputPin.Notify( AMACString );
 155                                        }
 156
 157                                        if( AddressOutputPin.IsConnected() )
 158                                        {
 159                                                String AIPString = IPAdressToString( WiFi.localIP());
 160                                                AddressOutputPin.Notify( (void *)AIPString.c_str() );
 161                                        }
 162
 163                                        if( SignalStrengthOutputPin.IsConnected() )
 164                                        {
 165                                                int32_t ASetrength = WiFi.RSSI();
 166                                                SignalStrengthOutputPin.Notify( &ASetrength );
 167                                        }
 168
 169                                        if( BSSIDOutputPin.IsConnected() )
 170                                        {
 171                                                uint8_t AMACAddress[6] = {0};
 172
 173                                                WiFi.BSSID( AMACAddress );
 174
 175                                                char AMACString[ 6 * 3 + 1 ];
 176                                                sprintf( AMACString, "%02X-%02X-%02X-%02X-%02X-%02X", AMACString[ 0 ], AMACString[ 1 ], AMACString[ 2 ], AMACString[ 3 ], AMACString[ 4 ], AMACString[ 5 ] );
 177                                                BSSIDOutputPin.Notify( AMACString );
 178                                        }
 179
 180                                        if( GatewayIPOutputPin.IsConnected() )
 181                                        {
 182                                                String AIPString = IPAdressToString( WiFi.gatewayIP());
 183                                                AddressOutputPin.Notify( (void *)AIPString.c_str() );
 184                                        }
 185
 186                                        if( SubnetMaskIPOutputPin.IsConnected() )
 187                                        {
 188                                                String AIPString = IPAdressToString( WiFi.subnetMask());
 189                                                SubnetMaskIPOutputPin.Notify( (void *)AIPString.c_str() );
 190                                        }
 191
 192                                        break;
 193                                }
 194                        
 195                }
 196
 197                virtual void StopEthernet()
 198                {
 199                        inherited::StopEthernet();
 200                        WiFi.disconnect();
 201                        IsStarted = false;
 202                }
 203
 204        protected:
 205                void DoScanNetworks( void *_Data )
 206                {
 207                        int nearbyAccessPointCount = WiFi.scanNetworks();
 208                        for( int i = 0; i < nearbyAccessPointCount; i ++ )
 209                        {
 210                                FoundSSIDOutputPin.Notify( WiFi.SSID( i ));
 211
 212                                int32_t ASetrength = WiFi.RSSI( i );
 213                                FoundSignalStrengthOutputPin.Notify( &ASetrength );
 214                        }
 215                }
 216
 217                void DoCheckSignalStrength( void *_Data )
 218                {
 219                        if( IsStarted )
 220                                if( SignalStrengthOutputPin.IsConnected() )
 221                                {
 222                                        int32_t ASetrength = WiFi.RSSI();
 223                                        SignalStrengthOutputPin.Notify( &ASetrength );
 224                                }
 225                }
 226
 227        public:
 228                WiFiShield()
 229                {
 230                        ScanNetworksInputPin.SetCallback( this, (OpenWire::TOnPinReceive)&WiFiShield::DoScanNetworks );
 231                }
 232
 233                WiFiShield( ::IPAddress local_ip) :
 234                        WiFiShield()
 235                {
 236                        IPAddress.Enabled = true;
 237                        IPAddress.IP = local_ip;
 238
 239                        CheckSignalStrengthInputPin.SetCallback( this, (OpenWire::TOnPinReceive)&WiFiShield::DoCheckSignalStrength );
 240                }
 241
 242                WiFiShield( ::IPAddress local_ip, ::IPAddress dns_server) :
 243                        WiFiShield( local_ip )
 244                {
 245
 246                        IPAddress.DNS.Enabled = true;
 247                        IPAddress.DNS.IP = dns_server;
 248                }
 249
 250                WiFiShield( ::IPAddress local_ip, ::IPAddress dns_server, ::IPAddress gateway) :
 251                        WiFiShield( local_ip, dns_server )
 252                {
 253                        IPAddress.DNS.Gateway.Enabled = true;
 254                        IPAddress.DNS.Gateway.IP = gateway;
 255                }
 256
 257                WiFiShield( ::IPAddress local_ip, ::IPAddress dns_server, ::IPAddress gateway, ::IPAddress subnet) :
 258                        WiFiShield( local_ip, dns_server, gateway )
 259                {
 260                        IPAddress.DNS.Gateway.Subnet.Enabled = true;
 261                        IPAddress.DNS.Gateway.Subnet.IP = subnet;
 262                }
 263        };
 264//---------------------------------------------------------------------------
 265}
 266
 267#endif