Main Site Documentation

Advise on understanding NetworkInterface Members


#1

For the Wi-FI interface, using .EnableDhcp() is a blocking function.

when i look here i dont see how they know its blocking or not.

http://msdn.microsoft.com/en-us/library/ee435204.aspx

Is there a way around this ?


#2

DHCP and DNS.GetHostEntry() are blocking in the current firmware version. This might be taken care of in NETMF 4.2


#3

Just for my own learning purposes,

How would i know its blocking or not by looking at that page ?
should that page have something on it that says which are blocking or not ?
or do i need to look elsewhere for that info?


#4

All methods are blocking, there is a pattern for asynchronous methods that usually comes in pairs Begin[italic]MethdoName[/italic] End[italic]MethodName[/italic].


#5

true, let me change the question.

Even if i put that .EnableDhcp() into a thread so i can have other threads running so the whole system can do other things while waiting for DHCP. it doesn’t allow other threads to run. my guess is that there is nothing like a Thread.Sleep(x); in there while its waiting for DHCP.

I dont want to have the whole systems hosed while waiting for DHCP. I want it to be able to do other things while waiting. is there a way around this.


#6

Are you sure about that?

Show your threading code.


#7

here ya go, when it starts the led blinks, when i get “Enable DHCP” in the debug window the blinking led stops.
In my case the DHCP never works. it hangs there for like 20-30 seconds then i get an exception

Exception System.Exception - CLR_E_FAIL (4)

#### Message: 
#### Microsoft.SPOT.Net.NetworkInformation.NetworkInterface::UpdateConfiguration [IP: 0000] ####
#### Microsoft.SPOT.Net.NetworkInformation.NetworkInterface::RenewDhcpLease [IP: 0006] ####
#### RTIP_RedPine_test.Program::StartWi_Fi [IP: 0188] ####

A first chance exception of type ‘System.Exception’ occurred in Microsoft.SPOT.Net.dll
An unhandled exception of type ‘System.Exception’ occurred in Microsoft.SPOT.Net.dll

still while its waiting for DHCP the led does not blink.



using System;
using Microsoft.SPOT;

using System.Threading;
using System.Net.Sockets;
using Microsoft.SPOT.Net.NetworkInformation;
using GHIElectronics.NETMF.Net;
using Microsoft.SPOT.Hardware;
using System.Net;
using GHIElectronics.NETMF.FEZ;


namespace RTIP_RedPine_test
{
    public class Program
    {
        static public bool network_is_read = false;


        public static void Main()
        {
            NetworkChange.NetworkAvailabilityChanged += new NetworkAvailabilityChangedEventHandler(NetworkChange_NetworkAvailabilityChanged);

            Thread thread1 = new Thread(new ThreadStart(LedBlinker));
            thread1.Start();

            Thread thread2 = new Thread(new ThreadStart(StartWi_Fi));
            thread2.Start();

            while (true)
            {
                Thread.Sleep(Timeout.Infinite);
            }
        }

        //Just blink the onboard led so i know we are running 
        static void LedBlinker()
        {
            bool ledState = false;
            OutputPort led = new OutputPort((Microsoft.SPOT.Hardware.Cpu.Pin)FEZ_Pin.Digital.LED, ledState);

            while (true)
            {
                // Sleep for 500 milliseconds
                Thread.Sleep(125);

                // toggle LED state
                ledState = !ledState;
                led.Write(ledState);
            }
        }


        static void StartWi_Fi()
        {

            try
            {
                WiFi.Enable(WiFi.HardwareModule.RS9110_N_11_21_1_Compatible, SPI.SPI_module.SPI2, (Cpu.Pin)2, (Cpu.Pin)26, (Cpu.Pin)3);// FEZ Cobra
            }
            catch (WiFi.HardwareException e)
            {

                if (e.errorCode == WiFi.HardwareException.ErrorCode.CommunicationTimeout || e.errorCode == WiFi.HardwareException.ErrorCode.FirmwareVersionMismatch)
                {
                    WiFi.UpdateFirmware(WiFi.HardwareModule.RS9110_N_11_21_1_Compatible, SPI.SPI_module.SPI2, (Cpu.Pin)2, (Cpu.Pin)26, (Cpu.Pin)3);// FEZ Cobra
                    WiFi.Enable(WiFi.HardwareModule.RS9110_N_11_21_1_Compatible, SPI.SPI_module.SPI2, (Cpu.Pin)2, (Cpu.Pin)26, (Cpu.Pin)3);// FEZ Cobra
                }
                else if (e.errorCode == WiFi.HardwareException.ErrorCode.CommunicationFailure)
                {
                    Debug.Print("Check WiFi module hardware connections and SPI/signals configurations");
                    Thread.Sleep(Timeout.Infinite);
                }

            }

            if (WiFi.IsEnabled)
            {
                Debug.Print("Searching for WiFi APs");
                AccessPointInfo[] ScanResp = WiFi.Scan();
                if (ScanResp != null)
                {
                    Debug.Print("Total Available Network are " + ScanResp.Length.ToString());
                    foreach (AccessPointInfo x in ScanResp)
                    {
                        Debug.Print(x.SSID);
                    }
                }
                int i = 0;
                for (i = 0; i < ScanResp.Length; i++)
                {
                    if (string.Compare(ScanResp[i].SSID, "WHOS_LOOKING") == 0)
                    {
                        break;
                    }
                }
                try
                {
                    if (i < ScanResp.Length)
                    {
                        Debug.Print("Connecting to " + ScanResp[i].SSID);
                        WiFi.ConnectInfrastructure(ScanResp[i], "01928374651029384756abcdef");
                        network_is_read = true;
                    }
                    else
                        Debug.Print("The netwrok you are looking for is not there");
                }
                catch
                {
                    Debug.Print("Faild to connect to the Wireless AP");
                }

                if (network_is_read)
                {
                    Debug.Print("WiFi link is ready!");

                    Debug.Print("Enable DHCP");
                    try
                    {
                        NetworkInterface[] netif = NetworkInterface.GetAllNetworkInterfaces();

                        // Static IP
                        /*****************************/
                        //netif[0].EnableStaticIP("192.168.1.222", "255.255.255.0", "192.168.1.1");
                        //netif[0].EnableStaticDns(new string[] { "10.1.10.1" });

                        // Dynamic IP
                        /*****************************/
                        if (!netif[0].IsDhcpEnabled)
                            netif[0].EnableDhcp();// This function is blocking
                        else
                        {
                            netif[0].RenewDhcpLease();// This function is blocking
                        }
                        network_is_read = true;
                        Debug.Print("Network settings:");
                        Debug.Print("IP Address: " + netif[0].IPAddress);
                        Debug.Print("Subnet Mask: " + netif[0].SubnetMask);
                        Debug.Print("Default Getway: " + netif[0].GatewayAddress);
                        Debug.Print("DNS Server: " + netif[0].DnsAddresses[0]);


                    }
                    catch (SocketException e)
                    {
                        Debug.Print("DHCP Faild");
                        if (e.ErrorCode == 11003)
                            Debug.Print(" WiFi module stopped responding. Re-Enable");
                    }

                    Debug.Print("Wi-Fi Ready");
                }
            }
        }



        static void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
        {
            if (WiFi.IsEnabled)
            {
                if (e.IsAvailable)
                {
                    if (WiFi.IsLinkConnected)
                    {
                        Debug.Print("WiFi connection was established!");
                    }
                }
                else
                {
                    if (!WiFi.IsLinkConnected)
                    {
                        Debug.Print("WiFi connection was dropped or disconnected!");
                        network_is_read = false;
                    }
                }
            }
        }
    }
}



#8

the explanation is a bit advanced.
DHCP.Enable() is blocking at the managed code level that’s why all other threads stops with Dhcp.Enable() executed.

In NETMF 4.2 we will try to resolve this to it becomes not blocking at the native code level.


#9

That explains it. Thanks