G400 Network management

Hello all,

I am attempting to have a reliable and autoreconnect system based on G400.
The G400 based device is sending and receiving data from a computer using udp and tcp stream and ftp.
This involves to launch an event if the ip adress change, if the cable is connected etc…

Is there a way to do it? (Networkavailability changed seems not working)

Network up/down cable disconnect / connect works like a charm on my G400 Header with build in ethernet. Independent if the board starts with or without network cable attached.

I was also struggling with this subject but problems dissapeared when i moved some code before nic.open() is called.

tested and worked on spider / emx / g400 with enc28, RS9110 and build in ethernet
Although RS9110 does not fire a disconnect event but that will be resolved in the next SDK

Here’s a copy paste of some code to get an idea.


            _nic = new EthernetBuiltIn();

            // Remark 2014-12-01 : It seems to be important to set IP before nic open
            if (_useDHCP)
            {
                if (!_nic.IsDhcpEnabled)
                {
                    _nic.EnableDhcp();
                    _nic.EnableDynamicDns();
                }
            }
            else
            {
                _nic.EnableStaticIP(_deviceIP, _deviceSubNetMask, _deviceGateWayIP);
                _nic.EnableStaticDns(_dnsServerIPs);
            }

            NetworkChange.NetworkAddressChanged += NetworkChangeNetworkAddressChanged;
            NetworkChange.NetworkAvailabilityChanged += NetworkChangeNetworkAvailabilityChanged;

            if (!_nic.Opened)
                _nic.Open();
            /*
            _wifiConnectThread = new Thread(EstablishWiFiConnection) { Priority = ThreadPriority.Highest };
            _wifiConnectThread.Start();
            */
            DebugHelper.Print("IDLE..");


 private void NetworkChangeNetworkAddressChanged(object sender, EventArgs e)
        {
            //_addressChange = true;
            if (IPAddress.GetDefaultLocalAddress().Equals(IPAddress.Any))
            {
                DebugHelper.DebugPrint("IP=" + IPAddress.GetDefaultLocalAddress());
            }
            else
            {
                DebugHelper.DebugPrint("IP=" + IPAddress.GetDefaultLocalAddress());
                DebugHelper.Print("Start network services >>>");

                if (_dhcpServer != null) _dhcpServer.Start();

                if (_dnsServer != null) _dnsServer.Start(IPAddress.GetDefaultLocalAddress());

                if (_mdnsServer != null) _mdnsServer.Start(IPAddress.GetDefaultLocalAddress());

                if (_llmnr != null) _llmnr.Start(IPAddress.GetDefaultLocalAddress());

                if (_httpServer != null) _httpServer.Start();
            }
        }

        private void NetworkChangeNetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
        {
            if (e.IsAvailable)
            {
                DebugHelper.Print("Connected");
            }
            else
            {
                DebugHelper.Print("Lost connection. request services to stop if network in infrastructure mode");
                if (!_one2oneConnection)
                {
                    if (_dhcpServer != null) _dhcpServer.Stop();

                    if (_dnsServer != null) _dnsServer.Stop();

                    if (_mdnsServer != null) _mdnsServer.Stop();

                    if (_llmnr != null) _llmnr.Stop();

                    if (_httpServer != null) _httpServer.Stop();
                }

                /*
                // WiFi lost connection with router in case of infra structure 
                if (_wifiConnectThread.IsAlive) // TODO 2014-12-01 : Test if this is correct
                    return;

               
                _wifiConnectThread = new Thread(EstablishWiFiConnection) {Priority = ThreadPriority.Highest};
                _wifiConnectThread.Start();
                 */
            }
        }

3 Likes

Hi all

I am trying to precisely understand how the stack works and to be honest my opinion is that it is a bit messy. I don’t understand the opened property.

After instantiating the ethernet built in I check if it is opened and it returns false. So far so good.

Then I claim for an Ip address to the dhcp by enabling it

if (!netif.IsDhcpEnabled)
                        netif.EnableDhcp();

I am surprising to see dhcp transaction on wireshark while the interface is still supposed to be closed…

I tested your code and it is working properly. But as soon as I insert your code in my entire application events are not fired…
:’(

1 Like

I moved the code at the beginning of main function and now I get the following exception :

Edit I only had this exception one time.

Now there’s no exception but event are not fired if I connect or disconnect the cable :wall:

I am going on investigating… It seems that the bug is related with Usb host instanciation

It seems that when I call the Start method of the controller it stops firing ethernet events.

This occurs only if a Memory Key is inserted. If the memory key is not present the events are fired properly.

@ leforban - Could you create a small program then with USB host that shows the issue for us to look into?

Hi John I finally succeed to have something working. I think that this was due to a bad sync of the autoreset event or the sleep(-1) in usb sample.

I recode this part in the entire application and will let you know.