Exemplo n.º 1
0
        /// <summary>
        /// Configure and enable the Wireless station interface
        /// </summary>
        /// <param name="ssid"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public static bool Configure(string ssid, string password)
        {
            Wireless80211Configuration wconf = GetConfiguration();


            // Set Options for Network Interface
            //
            // Enable      - Enable the Wireless Station ( Disable to reduce power )
            // AutoConnect - Automatically try to connect on boot.
            //
            wconf.Options = Wireless80211Configuration.ConfigurationOptions.AutoConnect |
                            Wireless80211Configuration.ConfigurationOptions.Enable;

            wconf.Ssid     = ssid;
            wconf.Password = password;
            if (wconf.Password.Length == 0)
            {
                wconf.Authentication = System.Net.NetworkInformation.AuthenticationType.Open;
            }
            else
            {
                wconf.Authentication = System.Net.NetworkInformation.AuthenticationType.WPA2;
            }

            // Save the configuration so on restart it will be running.
            wconf.SaveConfiguration();

            return(false);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Disable the Wireless station interface.
        /// </summary>
        public static void Disable()
        {
            Wireless80211Configuration wconf = GetConfiguration();

            wconf.Options = Wireless80211Configuration.ConfigurationOptions.None;
            wconf.SaveConfiguration();
        }
Exemplo n.º 3
0
        internal static void WorkingThread()
        {
            do
            {
                Debug.WriteLine("Waiting for network available...");

                Thread.Sleep(500);
            }while (!NetworkInterface.GetIsNetworkAvailable());

            NetworkInterface[] nis = NetworkInterface.GetAllNetworkInterfaces();

            if (nis.Length > 0)
            {
                // get the first interface
                NetworkInterface ni = nis[0];

                if (ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
                {
                    // network interface is Wi-Fi
                    Debug.WriteLine("Network connection is: Wi-Fi");

                    Wireless80211Configuration wc = Wireless80211Configuration.GetAllWireless80211Configurations()[ni.SpecificConfigId];

                    // note on checking the 802.11 configuration
                    // on secure devices (like the TI CC3220SF) the password can't be read
                    // so we can't use the code block bellow to automatically set the profile
                    if ((wc.Ssid != c_SSID && wc.Password != c_AP_PASSWORD) &&
                        (wc.Ssid != "" && wc.Password == ""))
                    {
                        // have to update Wi-Fi configuration
                        wc.Ssid     = c_SSID;
                        wc.Password = c_AP_PASSWORD;
                        wc.SaveConfiguration();
                    }
                    else
                    {   // Wi-Fi configuration matches
                    }
                }
                else
                {
                    // network interface is Ethernet
                    Debug.WriteLine("Network connection is: Ethernet");
                }

                // check if we have an IP
                CheckIP();

                if (_requiresDateTime)
                {
                    IpAddressAvailable.WaitOne();

                    SetDateTime();
                }
            }
            else
            {
                throw new NotSupportedException("ERROR: there is no network interface configured.\r\nOpen the 'Edit Network Configuration' in Device Explorer and configure one.");
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Connect to the SSID network and login using the Password
        /// </summary>
        private void ConnectNetwork()
        {
            NetworkInterface[] nis = NetworkInterface.GetAllNetworkInterfaces();

            if (nis.Length > 0)
            {
                // Get the first interface
                NetworkInterface ni = nis[0];


                if (ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
                {
                    // Network interface is Wi-Fi
                    Console.WriteLine("Network connection is: Wi-Fi");

                    Wireless80211Configuration wc = Wireless80211Configuration.GetAllWireless80211Configurations()[ni.SpecificConfigId];

                    // wc.Ssid = _SSID;

                    //  wc.Password = _Password;
                    if (wc.Ssid != _SSID && wc.Password != _Password)
                    {
                        // Updated 9/27/2019
                        wc.Options =
                            Wireless80211Configuration.ConfigurationOptions.Enable
                            | Wireless80211Configuration.ConfigurationOptions.AutoConnect;

                        wc.Ssid     = _SSID;
                        wc.Password = _Password;

                        // Save so when we reboot it will connect automatically
                        wc.SaveConfiguration();


                        // Uncomment to restart
                        // Power.RebootDevice();
                    }
                }

                else
                {
                    // Network interface is Ethernet
                    Console.WriteLine("Network connection is: Ethernet");

                    ni.EnableDhcp();
                }

                // Wait for DHCP to complete
                WaitIP();
            }

            else
            {
                throw new NotSupportedException("ERROR: there is no network interface configured.\r\nOpen the 'Edit Network Configuration' in Device Explorer and configure one.");
            }
        }
Exemplo n.º 5
0
        public static void Main()
        {
            string SSID     = Params.SSID;
            string PASSWORD = Params.AP_PASSWORD;

            try
            {
                Console.WriteLine("Wireless connect AP test - availability");

                NetworkChange.NetworkAvailabilityChanged += NetworkChange_NetworkAvailabilityChanged;

                // Get firt interface for ESP32 ( Wireless AP )
                NetworkInterface wi = NetworkInterface.GetAllNetworkInterfaces()[0];
                wi.EnableDhcp();

                // Get ID of wireless config ( should be 0 )
                uint wirelessConfigId = wi.SpecificConfigId;

                Wireless80211Configuration wirelessApConfig = Wireless80211Configuration.GetAllWireless80211Configurations()[wirelessConfigId];

                // Set up the Access Point detail we want to connect to
                wirelessApConfig.Ssid           = SSID;
                wirelessApConfig.Password       = PASSWORD;
                wirelessApConfig.Authentication = Wireless80211Configuration.AuthenticationType.WPA2;

                // Save config so it will be persisted over a reboot

                wirelessApConfig.SaveConfiguration();

                // Currenlty need to reboot if this is first time run
                while (true)
                {
                    NetworkInterface wi2 = NetworkInterface.GetAllNetworkInterfaces()[0];
                    if (wi2.IPv4Address[0] != 0)
                    {
                        break;
                    }
                    Console.WriteLine("No IP yet, not connected");
                    Thread.Sleep(1000);
                }

                Console.WriteLine("Have IP so must be connected");

                while (true)
                {
                    Thread.Sleep(1000);
                }
                // User code goes here
            }
            catch (Exception ex)
            {
                // Do whatever please you with the exception caught
            }
        }
Exemplo n.º 6
0
        internal static void WorkingThread()
        {
            NetworkInterface[] nis = NetworkInterface.GetAllNetworkInterfaces();

            if (nis.Length > 0)
            {
                // get the first interface
                NetworkInterface ni = nis[0];

                if (ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
                {
                    // network interface is Wi-Fi
                    Console.WriteLine("Network connection is: Wi-Fi");

                    Wireless80211Configuration wc = Wireless80211Configuration.GetAllWireless80211Configurations()[ni.SpecificConfigId];
                    if (wc.Ssid != c_SSID && wc.Password != c_AP_PASSWORD)
                    {
                        // have to update Wi-Fi configuration
                        wc.Ssid     = c_SSID;
                        wc.Password = c_AP_PASSWORD;
                        wc.SaveConfiguration();
                    }
                    else
                    {   // Wi-Fi configuration matches
                    }
                }
                else
                {
                    // network interface is Ethernet
                    Console.WriteLine("Network connection is: Ethernet");
                }

                // check if we have an IP
                if (!CheckIP())
                {
                    if (ni.IsDhcpEnabled)
                    {
                        ni.RenewDhcpLease();
                    }
                }

                if (_requiresDateTime)
                {
                    IpAddressAvailable.WaitOne();

                    //SetDateTime();
                }
            }
            else
            {
                throw new NotSupportedException("ERROR: there is no network interface configured.\r\nOpen the 'Edit Network Configuration' in Device Explorer and configure one.");
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Configure and enable the Wireless station interface
        /// </summary>
        /// <param name="ssid"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public static bool Configure(string ssid, string password)
        {
            // And we have to force connect once here even for a short time
            var success = WiFiNetworkHelper.ConnectDhcp(ssid, password, token: new CancellationTokenSource(10000).Token);

            Debug.WriteLine($"Connection is {success}");
            Wireless80211Configuration wconf = GetConfiguration();

            wconf.Options = Wireless80211Configuration.ConfigurationOptions.AutoConnect | Wireless80211Configuration.ConfigurationOptions.Enable;
            wconf.SaveConfiguration();
            return(true);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Initialise the Wireless parameters and save
        /// </summary>
        public static void SetupAndConnectWifi()
        {
            NetworkInterface[] nis = NetworkInterface.GetAllNetworkInterfaces();
            if (nis.Length > 0)
            {
                NetworkInterface ni = nis[0];       // get Wifi interface
                if (ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
                {
                    Wireless80211Configuration wc = Wireless80211Configuration.GetAllWireless80211Configurations()[ni.SpecificConfigId];
                    if (wc.Ssid != Params.SSID && wc.Password != Params.AP_PASSWORD)
                    {
                        Console.WriteLine("Updating Wifi config");
                        wc.Ssid     = Params.SSID;
                        wc.Password = Params.AP_PASSWORD;
                        wc.SaveConfiguration();

                        ni.EnableDhcp();
                    }
                    else
                    {
                        Console.WriteLine("Wifi config ok");
                    }

                    Console.WriteLine("Wait for IP");
                    while (true)
                    {
                        NetworkInterface ni2 = NetworkInterface.GetAllNetworkInterfaces()[0];
                        if (ni2.IPv4Address != null && ni2.IPv4Address.Length > 0)
                        {
                            if (ni2.IPv4Address[0] != '0')
                            {
                                Console.WriteLine("Have IP " + ni2.IPv4Address);
                                break;
                            }
                        }

                        Thread.Sleep(1000);
                    }
                }
                else
                {
                    throw new NotSupportedException("No Wifi");
                }
            }
            else
            {
                throw new NotSupportedException("No Network");
            }
        }
Exemplo n.º 9
0
        public static void SetupAndConnectNetwork()
        {
            NetworkInterface[] nis = NetworkInterface.GetAllNetworkInterfaces();
            if (nis.Length > 0)
            {
                // get the first interface
                NetworkInterface ni = nis[0];

                if (ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
                {
                    // network interface is Wi-Fi
                    Console.WriteLine("Network connection is: Wi-Fi");

                    Wireless80211Configuration wc = Wireless80211Configuration.GetAllWireless80211Configurations()[ni.SpecificConfigId];
                    if (wc.Ssid != c_SSID && wc.Password != c_AP_PASSWORD)
                    {
                        // have to update Wi-Fi configuration
                        wc.Ssid     = c_SSID;
                        wc.Password = c_AP_PASSWORD;
                        wc.SaveConfiguration();
                    }
                    else
                    {   // Wi-Fi configuration matches
                    }
                }
                else
                {
                    // network interface is Ethernet
                    Console.WriteLine("Network connection is: Ethernet");

                    ni.EnableDhcp();
                }

                // wait for DHCP to complete
                WaitIP();
            }
            else
            {
                throw new NotSupportedException("ERROR: there is no network interface configured.\r\nOpen the 'Edit Network Configuration' in Device Explorer and configure one.");
            }
        }
Exemplo n.º 10
0
        /// <summary>
        /// Initialise the Wireless parameters and save
        /// </summary>
        public static void SetupAndConnectNetwork()
        {
            NetworkInterface[] nis = NetworkInterface.GetAllNetworkInterfaces();
            if (nis.Length > 0)
            {
                NetworkInterface ni = nis[0];       // get Wifi interface
                if (ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
                {
                    Wireless80211Configuration wc = Wireless80211Configuration.GetAllWireless80211Configurations()[ni.SpecificConfigId];
                    if (wc.Ssid != Params.SSID && wc.Password != Params.AP_PASSWORD)
                    {
                        Console.WriteLine("Updating Wifi config");
                        wc.Ssid     = Params.SSID;
                        wc.Password = Params.AP_PASSWORD;
                        wc.SaveConfiguration();

                        ni.EnableDhcp();
                    }
                    else
                    {
                        Console.WriteLine("Wifi config ok");
                    }

                    WaitIP();
                }
                else
                {
                    Console.WriteLine("Not ESP32 ");
                    ni.EnableDhcp();
                    WaitIP();
                }
            }
            else
            {
                throw new NotSupportedException("No Network");
            }
        }
Exemplo n.º 11
0
        internal static void WorkingThread()
        {
            NetworkInterface[] nis = NetworkInterface.GetAllNetworkInterfaces();

            if (nis.Length > 0)
            {
                // get the first interface

                foreach (var n in nis)
                {
                    if (n.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
                    {
                        Console.WriteLine("Network connection is: Ethernet");

                        n.EnableAutomaticDns();
                        n.EnableDhcp();
                        //n.EnableStaticIPv4("10.10.10.131", "255.255.255.0", "10.10.10.1");

                        m_NetworkInterfaces_Lan = n;
                    }

                    if (n.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
                    {
                        if (DisableWifi)
                        {
                            Console.WriteLine("Found Network connection is: Wi-Fi. Disable active");
                            Wireless80211Configuration wc = Wireless80211Configuration.GetAllWireless80211Configurations()[n.SpecificConfigId];
                            Console.WriteLine($"Options:{wc.Options}");
                            if (wc.Options != Wireless80211Configuration.ConfigurationOptions.None)
                            {
                                Console.WriteLine("Found Network connection is: Wi-Fi. Disabling it.");
                                wc.Options = Wireless80211Configuration.ConfigurationOptions.None;
                                wc.SaveConfiguration();
                            }
                            n.EnableAutomaticDns();
                            n.EnableDhcp();

                            // Reboot nötig nach dem Abschalten von Wifi
                        }
                        else
                        {
                            Console.WriteLine("Network connection is: Wi-Fi");
                            Wireless80211Configuration wc = Wireless80211Configuration.GetAllWireless80211Configurations()[n.SpecificConfigId];
                            Console.WriteLine($"Ssid:{wc.Ssid},Encryption:{wc.Encryption},Password:{wc.Password},Authentication:{wc.Authentication}");
                            Console.WriteLine($"Options:{wc.Options}");

                            wc.Options = Wireless80211Configuration.ConfigurationOptions.AutoConnect | Wireless80211Configuration.ConfigurationOptions.Enable;

                            wc.Authentication = AuthenticationType.WPA2;
                            wc.Encryption     = EncryptionType.WPA2_PSK;
                            wc.Ssid           = c_SSID;
                            wc.Password       = c_AP_PASSWORD;

                            //wc.Ssid = "";
                            //wc.Password = "";

                            wc.SaveConfiguration();

                            //n.EnableStaticIPv4("192.168.0.250", "255.255.255.0", "192.168.0.1");
                            n.EnableAutomaticDns();
                            n.EnableDhcp();

                            m_NetworkInterfaces_Wifi = n;
                        }
                    }
                }

                if (m_NetworkInterfaces_Lan == null && m_NetworkInterfaces_Wifi == null)
                {
                    throw new NotSupportedException("ERROR: there is no network interface configured.\r\nOpen the 'Edit Network Configuration' in Device Explorer and configure one.");
                }

                // check if we have an IP
                CheckIP();

                if (_requiresDateTime)
                {
                    IpAddressAvailable.WaitOne();

                    SetDateTime();
                }
            }
            else
            {
                throw new NotSupportedException("ERROR: there is no network interface configured.\r\nOpen the 'Edit Network Configuration' in Device Explorer and configure one.");
            }
        }