Interface for abstracting the network adapter

I am trying to abstract my program so that it will run on both a G120E and a G400S without modification and have made good progress but in order to make things easier I am wondering why the J11D, ENC28 and wifi adapter drivers don’t share a common interface, e.g.


INetworkAdapter networkAdapter;

switch (Mainboard.MainboardName) //SystemInfo.SystemID.Model
            {
                case "GHI Electronics FEZ Spider II": //14
                    factory = new Mainboards.G120_SpiderIIFactory();
                    networkAdapter = new GTM.GHIElectronics.EthernetJ11D(factory.GetMainboard().Ethernet);
                    break;
                case "GHI Electronics FEZ Raptor": //9
                    factory = new Mainboards.G400_RaptorFactory();
                    networkAdapter = new GTM.GHIElectronics.EthernetENC28(factory.GetMainboard().Ethernet);
                    break;
                default:
                    break;
            }
//...

                    if (networkAdapter.NetworkInterface.Opened)
                    {
                        networkAdapter.NetworkInterface.Close();
                    }
                    networkAdapter.UseThisNetworkInterface(); 

or am I looking at this in fundamentally the wrong way?!

@ networkfusion - The underlying drivers in GHI.Networking all derive from BaseInterface. The Gadgeteer modules do not since they compose instead of inheriting from the underlying drivers. They do derive from Gadgeteer’s NetworkModule, though.

@ John - private Gadgeteer.Modules.Module.NetworkModule ethernet; would get me part way there but the problem is initialising the driver since there is no access to any of the following commands:


                    if (networkAdapter.NetworkInterface.Opened)
                    {
                        networkAdapter.NetworkInterface.Close();
                    }
                    networkAdapter.UseThisNetworkInterface();

specifically the last one… I am thinking of adding a wrapper to the module drivers, but this adds extra complexity:

using System;
using Microsoft.SPOT;
using SensorNetNode.Mainboards;

namespace Gadgeteer.Modules.NetworkFusion
{
    public class EthernetENC28 : INetworkInterface
    {
        Gadgeteer.Modules.GHIElectronics.EthernetENC28 ethernet;
        public EthernetENC28(int socket)
        {
            ethernet = new Gadgeteer.Modules.GHIElectronics.EthernetENC28(socket);
        }

        public void UseThisNetworkInterface()
        {
            ethernet.UseThisNetworkInterface();
        }

        public void Close()
        {
            ethernet.NetworkInterface.Close();
        }

        public bool Opened { 
            get 
            {
                return ethernet.NetworkInterface.Opened;
            }
            set{}
        }
    }
}

as everything else I use can be accessed through:

NetworkInterface[] netif = NetworkInterface.GetAllNetworkInterfaces();

but I don’t think this is a pretty solution and I cant be the only one to have done this…

@ networkfusion - Unfortunately we do not control Gadgeteer.Modules.Module.NetworkModule, which the Gadgeteer networking drivers derive from.

You do not have to use the Gadgeteer module drivers, though. You can use the classes in GHI.Networking yourself which all derive from BaseInterface, provided you pass them the correct pins.

or you might want to build a virtual driver that instantiate the right code based on the board attached to it.

@ John - ok, thanks for your input. I have gone down the route of doing it like this:


class G400_Raptor : Mainboard
    {

        public override Sockets Socket
        {
            get { return new G400Sockets(); }
        }

        public override void ConfigureNetworkInterface(byte[] macAddress)
        {
            var eth = new Gadgeteer.Modules.GHIElectronics.EthernetENC28(Socket.Ethernet);
            eth.NetworkInterface.PhysicalAddress = macAddress;
            if (!eth.NetworkInterface.IsDhcpEnabled)
            {
                eth.NetworkInterface.EnableDhcp();
                eth.NetworkInterface.EnableDynamicDns();
            }
            eth.UseThisNetworkInterface();
        }

It may mean a repeat of code for all drivers (except new Gadgeteer.Modules.GHIElectronics.EthernetENC28(…), however it is the only way I can find to enable the adapter without getting into a chicken and egg situation with the MAC.

@ andre.m - so given that the spiderII and Raptor are based on these SOM’s, are you absolutely sure :whistle:

@ andre.m - Okay thanks for pointing out that I used the wrong G400 type, changed the G400D to a G400S in the first post, but since all of my code examples had “Raptor” and “Spider” in them hopefully everyone understood what I meant.

I plan on moving away from the gadgeteer platform to a more pure NETMF based one once I have finished prototyping but the sockets and their mappings are convenient and since I require the ability to swap sensors that can use different types of interface (SPI, UART or GPIO) so when I do, I may well have to reinvent the wheel anyway.