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 = _ssRACapi.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?
            string currentProfile = _ssRACapi.getCurrentProfile().sProfileLabel;
            string desiredSSID    = _ssRACapi.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...");
                    _ssRACapi.enableProfile(_profiles[0], false); //disable first profile
                    _ssRACapi.enableProfile(_profiles[1], true);  //enable second profile
                }
                else
                {
                    OnConnectedMessage("network connected. No profile change.");
                }
            }
            else if (_ssRACapi.getCurrentProfile().sProfileLabel == _profiles[1])
            {
                desiredSSID = _ssRACapi._racProfiles[1].sSSID;
                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 ...");
                _ssRACapi.enableProfile(_profiles[1], false); //disable second profile
                _ssRACapi.enableProfile(_profiles[0], true);  //enable first profile
                desiredSSID = _ssRACapi._racProfiles[0].sSSID;
                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
                    _ssRACapi.enableProfile(_profiles[0], false); //disable first profile
                    _ssRACapi.enableProfile(_profiles[1], true);  //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.");
        }