Esempio n. 1
0
        /// <summary>
        /// Joins a wireless network
        /// </summary>
        /// <param name="SSID">The name of the wireless network</param>
        /// <param name="Channel">The channel the AP is listening on (0 for autodetect)</param>
        /// <param name="Authentication">The method for authentication</param>
        /// <param name="Key">The shared key required to join the network (WEP / WPA)</param>
        /// <param name="KeyIndex">The index of the key (WEP only)</param>
        public bool JoinNetwork(string SSID, int Channel = 0, AuthMode Authentication = AuthMode.Open, string Key = "", int KeyIndex = 1)
        {
            // Enterring command mode
            this._CommandMode_Start();
            // Configures the network
            if (!this._CommandMode_Exec("set wlan ssid " + SSID)) throw new SystemException(this._CommandMode_Response);
            if (!this._CommandMode_Exec("set wlan channel " + Channel)) throw new SystemException(this._CommandMode_Response);
            if (!this._CommandMode_Exec("set wlan auth " + Authentication.ToString())) throw new SystemException(this._CommandMode_Response);
            if (Authentication == AuthMode.WEP_128)
            {
                if (!this._CommandMode_Exec("set wlan key " + Key)) throw new SystemException(this._CommandMode_Response);
                if (!this._CommandMode_Exec("set wlan num " + KeyIndex.ToString())) throw new SystemException(this._CommandMode_Response);
            }
            else if (Authentication != AuthMode.Open)
            {
                if (!this._CommandMode_Exec("set wlan phrase " + Key)) throw new SystemException(this._CommandMode_Response);
            }

            // Auto-Assoc Rightpoint chan=6 mode=MIXED SCAN OK\r\n
            // Auto-Assoc Rightpoint!! chan=6 mode=NONE FAILED\r\n
            var joinComplete = false;
            var joinSuccessful = false;
            var joinHandler = new Tools.StringEventHandler((text, time) =>
            {
                if (text.IndexOf("AUTH-ERR") >= 0)
                {
                    joinComplete = true;
                    joinSuccessful = false;
                    _DebugPrint('D', "Authentication error");
                }

                if (text.IndexOf("Auto-Assoc") >= 0 && text.IndexOf("NONE FAILED") >= 0)
                {
                    joinComplete = true;
                    joinSuccessful = false;
                    _DebugPrint('D', "Couldn't find network");
                }

                if (text.IndexOf("Associated!") >= 0)
                {
                    joinComplete = true;
                    joinSuccessful = true;
                }
            });

            // Actually joins the network
            this._lineRecieved += joinHandler;
            this._SerialPort_Write("join\r");

            for(var i =0; i< 40 && !joinComplete; i++)
            {
                Thread.Sleep(250);
            }

            this._lineRecieved -= joinHandler;

            // Closes down command mode
            this._CommandMode_Stop();

            return joinSuccessful;
        }
Esempio n. 2
0
        /// <summary>
        /// Joins a wireless network
        /// </summary>
        /// <param name="SSID">The name of the wireless network</param>
        /// <param name="Channel">The channel the AP is listening on (0 for autodetect)</param>
        /// <param name="Authentication">The method for authentication</param>
        /// <param name="Key">The shared key required to join the network (WEP / WPA)</param>
        /// <param name="KeyIndex">The index of the key (WEP only)</param>
        public bool JoinNetwork(string SSID, int Channel = 0, AuthMode Authentication = AuthMode.Open, string Key = "", int KeyIndex = 1)
        {
            // Enterring command mode
            this._CommandMode_Start();
            // Configures the network
            if (!this._CommandMode_Exec("set wlan ssid " + SSID))
            {
                throw new SystemException(this._CommandMode_Response);
            }
            if (!this._CommandMode_Exec("set wlan channel " + Channel))
            {
                throw new SystemException(this._CommandMode_Response);
            }
            if (!this._CommandMode_Exec("set wlan auth " + Authentication.ToString()))
            {
                throw new SystemException(this._CommandMode_Response);
            }
            if (Authentication == AuthMode.WEP_128)
            {
                if (!this._CommandMode_Exec("set wlan key " + Key))
                {
                    throw new SystemException(this._CommandMode_Response);
                }
                if (!this._CommandMode_Exec("set wlan num " + KeyIndex.ToString()))
                {
                    throw new SystemException(this._CommandMode_Response);
                }
            }
            else if (Authentication != AuthMode.Open)
            {
                if (!this._CommandMode_Exec("set wlan phrase " + Key))
                {
                    throw new SystemException(this._CommandMode_Response);
                }
            }

            // Auto-Assoc Rightpoint chan=6 mode=MIXED SCAN OK\r\n
            // Auto-Assoc Rightpoint!! chan=6 mode=NONE FAILED\r\n
            var joinComplete   = false;
            var joinSuccessful = false;
            var joinHandler    = new Tools.StringEventHandler((text, time) =>
            {
                if (text.IndexOf("AUTH-ERR") >= 0)
                {
                    joinComplete   = true;
                    joinSuccessful = false;
                    _DebugPrint('D', "Authentication error");
                }

                if (text.IndexOf("Auto-Assoc") >= 0 && text.IndexOf("NONE FAILED") >= 0)
                {
                    joinComplete   = true;
                    joinSuccessful = false;
                    _DebugPrint('D', "Couldn't find network");
                }

                if (text.IndexOf("Associated!") >= 0)
                {
                    joinComplete   = true;
                    joinSuccessful = true;
                }
            });

            // Actually joins the network
            this._lineRecieved += joinHandler;
            this._SerialPort_Write("join\r");

            for (var i = 0; i < 40 && !joinComplete; i++)
            {
                Thread.Sleep(250);
            }

            this._lineRecieved -= joinHandler;

            // Closes down command mode
            this._CommandMode_Stop();

            return(joinSuccessful);
        }