Esempio n. 1
0
        /// <summary>
        /// returns TRUE for device having a valid IP
        /// </summary>
        /// <returns>boolean</returns>
        public static bool _getConnected()
        {
            if (_myConfig._checkConnectIP)
            {
                string      hostname    = Dns.GetHostName(); //local host name
                IPHostEntry _hostEntry  = Dns.GetHostEntry(hostname);
                bool        bLocalsOnly = true;
                foreach (IPAddress a in _hostEntry.AddressList)
                {
                    System.Diagnostics.Debug.WriteLine(a.ToString()); // 169.254.2.1, 127.0.0.1
                    if (a.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                    {
#if DEBUG
                        if (a.ToString() != "127.0.0.1")
#else
                        if (a.ToString() != "169.254.2.1" && a.ToString() != "127.0.0.1")
#endif
                        {
                            bLocalsOnly = false;
                            _currentIP  = a;
                        }
                    }
                }
                if (bLocalsOnly)
                {
                    return(false);
                }
                else
                {
                    return(true);
                }
            }
            else
            {
                string ssid = _wifi.getCurrentProfile().sSSID;
                if (wifi.isAssociated(ssid))
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// this function tries to switch to the preferred network
        /// and falls back to second profile if 1st did not connect
        /// </summary>
        void doSwitch()
        {
            if (bInsideSwitch)
            {
                Logger.WriteLine("doSwitch called although already insideSwitch");
                return;
            }
            lock (syncObject)
                bInsideSwitch = true;

            OnConnectedMessage("DoSwitch() start...");
            Logger.WriteLine("DoSwitch() start...");

            int iConnectTry = 0;
            //is first profile active?
            //USES instance or regKey name!
            string currentProfile = _wifi.getCurrentProfile().sProfileRegKey;
            string desiredSSID    = _wifi.getCurrentProfile().sSSID;

            OnConnectedMessage("current profile=" + currentProfile);
            //is the preferred profile active?
            if (currentProfile == _profiles[0])
            {
                OnConnectedMessage("Current Profile = First profile");
                if (network._getConnected() == false)
                { //not connected
                    OnConnectedMessage("network not connected. Switching to 2nd profile...");
                    _wifi.setProfile(_profiles[1]);
                }
                else
                {
                    OnConnectedMessage("network connected. No profile change.");
                }
            }
            else if (_wifi.getCurrentProfile().sProfileRegKey == _profiles[1])
            {
                //TODO: do not use an INDEX use the names as in App.Config
                desiredSSID = _wifi.SSIDsecondary;
                OnConnectedMessage("Current Profile = Second profile");
                if (network._getConnected() == false)
                {
                    OnConnectedMessage("secondary profile not connected");
                }
                else
                {
                    OnConnectedMessage("secondary profile connected");
                }
                //try first profile, regardless of connect state
                OnConnectedMessage("Trying first Profile. Switching ...");
                _wifi.setProfile(_profiles[0]); //enable first profile

                desiredSSID = _wifi.SSIDprimary;

                iConnectTry = 0;
                //try for 40 seconds or so
                while (!_bStopThread && (iConnectTry < _iSwitchTimeout))
                {
                    Thread.Sleep(1000);
                    iConnectTry++;
                    if (_myConfig._checkConnectIP)
                    {
                        if (network._getConnected() == true)    // do not care about AP association
                        {
                            break;
                        }
                    }
                    else //check AP association but not IP
                    {
                        if (wifi.isAssociated(desiredSSID) == true) // do not care about IP
                        {
                            break;
                        }
                    }
                }
                //another test for being connected
                if (network._getConnected() == false)
                {
                    OnConnectedMessage("First Profile did not connect. Switching to secondary profile...");
                    //switch back
                    _wifi.setProfile(_profiles[1]); //enable second profile
                }
                else
                {
                    OnConnectedMessage("primary network connected.");
                }
            }
            else
            {
                OnConnectedMessage("Current profile not in list!");
            }

            lock (syncObject)
                bInsideSwitch = false;
            OnConnectedMessage("DoSwitch() end.");
            Logger.WriteLine("DoSwitch() end.");
        }