/// <summary>
        /// Enumerates the available ports for .Net Micro Framework devices.
        /// Also, USART or COM ports will be listed even if there is no device attached.
        /// </summary>
        /// <returns>List of MFPortDefinition objects which represent transports for possible MF devices</returns>
        public ReadOnlyCollection <MFPortDefinition> EnumPorts(params TransportType[] types)
        {
            if (types == null || types.Length == 0)
            {
                types = new TransportType[] { TransportType.Serial, TransportType.TCPIP, TransportType.USB };
            }

            m_deviceList.Clear();

            foreach (TransportType type in types)
            {
                ArrayList list = new ArrayList();

                if (type == TransportType.TCPIP)
                {
                    list = new ArrayList(_DBG.PortDefinition_Tcp.EnumeratePorts(m_DiscoveryMulticastAddress, m_DiscoveryMulticastAddressRecv, m_DiscoveryMulticastPort, m_DiscoveryMulticastToken, m_DiscoveryMulticastTimeout, m_DiscoveryTTL));
                }
                else
                {
                    switch (type)
                    {
                    case TransportType.USB:
                        list = _DBG.PortDefinition.Enumerate(EnablePermiscuousWinUsb ? _DBG.PortFilter.LegacyPermiscuousWinUsb : _DBG.PortFilter.Usb);
                        break;

                    case TransportType.Serial:
                        list = _DBG.PortDefinition.Enumerate(_DBG.PortFilter.Serial);
                        break;

                    default:
                        throw new ArgumentException();
                    }
                }

                foreach (_DBG.PortDefinition pd in list)
                {
                    switch (type)
                    {
                    case TransportType.Serial:
                        m_deviceList.Add(new MFSerialPort(pd.DisplayName, pd.Port));
                        break;

                    case TransportType.USB:
                        m_deviceList.Add(new MFUsbPort(pd.DisplayName));
                        break;

                    case TransportType.TCPIP:
                        _DBG.PortDefinition_Tcp prt = pd as _DBG.PortDefinition_Tcp;
                        string[] split = prt.Port.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
                        if (split.Length > 0)
                        {
                            m_deviceList.Add(new MFTcpIpPort(split[0], prt.MacAddress));
                        }
                        break;
                    }
                }
            }
            return(new ReadOnlyCollection <MFPortDefinition>(m_deviceList));
        }
Exemple #2
0
        public PortDefinition[] GetPersistablePortDefinitions()
        {
            PortDefinition[] ports = null;

            switch (m_portFilter)
            {
            case PortFilter.Emulator:
                Debug.Assert(false);
                break;

            case PortFilter.Serial:
                ports = AsyncSerialStream.EnumeratePorts();
                break;

            case PortFilter.Usb:
            {
                if (MonoDevelop.Core.Platform.IsWindows)
                {
                    PortDefinition[] portUSB;
                    PortDefinition[] portWinUSB;

                    portUSB    = AsyncUsbStream.EnumeratePorts();
                    portWinUSB = WinUsb_AsyncUsbStream.EnumeratePorts();

                    int lenUSB    = portUSB != null ? portUSB.Length : 0;
                    int lenWinUSB = portWinUSB != null ? portWinUSB.Length : 0;

                    ports = new PortDefinition[lenUSB + lenWinUSB];

                    if (lenUSB > 0)
                    {
                        Array.Copy(portUSB, ports, lenUSB);
                    }
                    if (lenWinUSB > 0)
                    {
                        Array.Copy(portWinUSB, 0, ports, lenUSB, lenWinUSB);
                    }
                }
                else
                {
                    ports = LibUsb_AsyncUsbStream.EnumeratePorts();
                }
            }
            break;

            case PortFilter.TcpIp:
                ports = PortDefinition_Tcp.EnumeratePorts(false);
                break;

            default:
                Debug.Assert(false);
                throw new ApplicationException();
            }

            return(ports);
        }
Exemple #3
0
        static public PortDefinition CreateInstanceForTcp(string name)
        {
            PortDefinition portDefinition = null;

            //From CorDebug\DebugPort.cs
            string    hostName  = name;
            int       port      = PortDefinition_Tcp.WellKnownPort;
            int       portIndex = hostName.IndexOf(':');
            IPAddress address   = null;

            if (portIndex > 0)
            {
                hostName = name.Substring(0, portIndex);

                if (portIndex < name.Length - 1)
                {
                    string portString = name.Substring(portIndex + 1);

                    int portT;

                    if (int.TryParse(portString, out portT))
                    {
                        port = portT;
                    }
                }
            }

            if (!IPAddress.TryParse(hostName, out address))
            {
                //Does DNS resolution make sense here?

                IPHostEntry iPHostEntry = Dns.GetHostEntry(hostName);

                if (iPHostEntry.AddressList.Length > 0)
                {
                    //choose the first one?
                    address = iPHostEntry.AddressList[0];
                }
            }

            if (address != null)
            {
                IPEndPoint ipEndPoint = new IPEndPoint(address, port);

                portDefinition = new PortDefinition_Tcp(ipEndPoint);

                //ping to see if it is alive?
            }

            return(portDefinition);
        }
Exemple #4
0
        static public ArrayList Enumerate(params PortFilter[] args)
        {
            ArrayList lst = new ArrayList();

            foreach (PortFilter pf in args)
            {
                PortDefinition[] res;

                switch (pf)
                {
                case PortFilter.Emulator:
                    res = Emulator.EnumeratePipes();
                    break;

                case PortFilter.Serial:
                    res = AsyncSerialStream.EnumeratePorts();
                    break;

                case PortFilter.LegacyPermiscuousWinUsb:
                case PortFilter.Usb:
                {
                    res = WinUsb_AsyncUsbStream.EnumeratePorts(pf == PortFilter.LegacyPermiscuousWinUsb);

                    lst.AddRange(res);

                    res = AsyncUsbStream.EnumeratePorts();
                    // res will be added to list below...
                }
                break;

                case PortFilter.TcpIp:
                    res = PortDefinition_Tcp.EnumeratePorts();
                    break;

                default: res = null; break;
                }

                if (res != null)
                {
                    lst.AddRange(res);
                }
            }

            return(lst);
        }
Exemple #5
0
        public static PortDefinition[] EnumeratePorts(
            System.Net.IPAddress DiscoveryMulticastAddress    ,
            System.Net.IPAddress DiscoveryMulticastAddressRecv,
            int       DiscoveryMulticastPort       ,
            string    DiscoveryMulticastToken      ,
            int       DiscoveryMulticastTimeout    ,
            int       DiscoveryTTL                 
        )
        {
            PortDefinition_Tcp []ports = null;
            Dictionary<string, string> addresses = new Dictionary<string, string>();
            
            try
            {
                IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName());

                foreach (IPAddress ip in hostEntry.AddressList)
                {
                    if (ip.AddressFamily == AddressFamily.InterNetwork)
                    {
                        int cnt = 0;
                        int total = 0;
                        byte[] data = new byte[1024];
                        Socket sock = null;
                        Socket recv = null;

                        System.Net.IPEndPoint endPoint    = new System.Net.IPEndPoint(ip, 0);
                        System.Net.EndPoint   epRemote    = new System.Net.IPEndPoint(System.Net.IPAddress.Any, 26001);
                        System.Net.IPEndPoint epRecv      = new System.Net.IPEndPoint(ip, DiscoveryMulticastPort);
                        System.Net.IPEndPoint epMulticast = new System.Net.IPEndPoint(DiscoveryMulticastAddress, DiscoveryMulticastPort);

                        try
                        {
                            sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                            recv = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

                            recv.Bind(epRecv);
                            recv.ReceiveTimeout = DiscoveryMulticastTimeout;
                            recv.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(DiscoveryMulticastAddressRecv, ip));

                            sock.Bind(endPoint);
                            sock.MulticastLoopback = false;
                            sock.Ttl = (short)DiscoveryTTL;
                            sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 64);

                            // send ping
                            sock.SendTo(System.Text.Encoding.ASCII.GetBytes(DiscoveryMulticastToken), SocketFlags.None, epMulticast);

                            while (0 < (cnt = recv.ReceiveFrom(data, total, data.Length - total, SocketFlags.None, ref epRemote)))
                            {
                                addresses[((IPEndPoint)epRemote).Address.ToString()] = "";
                                total += cnt;
                                recv.ReceiveTimeout = DiscoveryMulticastTimeout / 2;
                            }

                            recv.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.DropMembership, new MulticastOption(DiscoveryMulticastAddressRecv));

                        }
                        // SocketException occurs in RecieveFrom if there is no data.
                        catch (SocketException)
                        {
                        }
                        finally
                        {
                            if (recv != null)
                            {
                                recv.Close();
                                recv = null;
                            }
                            if (sock != null)
                            {
                                sock.Close();
                                sock = null;
                            }
                        }

                        // use this if we need to get the MAC address of the device
                        SOCK_discoveryinfo disc = new SOCK_discoveryinfo();
                        disc.ipaddr = 0;
                        disc.macAddressLen = 0;
                        int idx = 0;
                        int c_DiscSize = Marshal.SizeOf(disc);
                        while (total >= c_DiscSize)
                        {
                            byte[] discData = new byte[c_DiscSize];
                            Array.Copy(data, idx, discData, 0, c_DiscSize);
                            GCHandle gch = GCHandle.Alloc(discData, GCHandleType.Pinned);
                            disc = (SOCK_discoveryinfo)Marshal.PtrToStructure(gch.AddrOfPinnedObject(), typeof(SOCK_discoveryinfo));
                            gch.Free();

                            // previously we only displayed the IP address for the device, which doesn't
                            // really tell you which device you are talking to.  The MAC address should be unique.
                            // therefore we will display the MAC address in the device display name to help distinguish
                            // the devices.  
                            if (disc.macAddressLen <= 64 && disc.macAddressLen > 0)
                            {
                                IPAddress ipResp = new IPAddress((long)disc.ipaddr);

                                // only append the MAC if it matches one of the IP address we got responses from
                                if (addresses.ContainsKey(ipResp.ToString()))
                                {
                                    string strMac = "";
                                    for (int mi = 0; mi < disc.macAddressLen - 1; mi++)
                                    {
                                        unsafe
                                        {
                                            strMac += string.Format("{0:x02}-", disc.macAddressBuffer[mi]);
                                        }
                                    }
                                    unsafe
                                    {
                                        strMac += string.Format("{0:x02}", disc.macAddressBuffer[disc.macAddressLen - 1]);
                                    }

                                    addresses[ipResp.ToString()] = strMac;
                                }
                            }
                            total -= c_DiscSize;
                            idx += c_DiscSize;
                        }
                    }
                }
            }
            catch( Exception e2)
            {
                System.Diagnostics.Debug.Print(e2.ToString());
            }

            ports = new PortDefinition_Tcp[addresses.Count];
            int i = 0;

            foreach(string key in addresses.Keys)
            {
                ports[i++] = new PortDefinition_Tcp(IPAddress.Parse(key), addresses[key]);
            }

            return ports;            
        }
        private MFDevice InitializePorts(MFPortDefinition portDefinitionMain, MFPortDefinition portDefinitionTinyBooter)
        {
            MFDevice device = null;

            MFPortDefinition[] portDefs = new MFPortDefinition[2] {
                portDefinitionMain, portDefinitionTinyBooter
            };
            _DBG.PortDefinition[] pds = new Microsoft.SPOT.Debugger.PortDefinition[2];

            for (int i = 0; i < portDefs.Length; i++)
            {
                MFPortDefinition portDefinition = portDefs[i];
                if (portDefinition == null)
                {
                    continue;
                }

                if (portDefinition.Transport == TransportType.TCPIP)
                {
                    System.Net.IPAddress[] addr = System.Net.Dns.GetHostAddresses(portDefinition.Port);

                    pds[i] = new Microsoft.SPOT.Debugger.PortDefinition_Tcp(addr[0]);
                }
                else
                {
                    ArrayList list = _DBG.PortDefinition.Enumerate(_DBG.PortFilter.Usb, _DBG.PortFilter.Serial);
                    foreach (_DBG.PortDefinition pd in list)
                    {
                        if (portDefinition.Port.Length > 0)
                        {
                            if (string.Equals(portDefinition.Port, pd.UniqueId.ToString()))
                            {
                                pds[i] = pd;
                                break;
                            }
                        }
                        if (string.Equals(portDefinition.Name, pd.DisplayName))
                        {
                            pds[i] = pd;
                            break;
                        }
                    }
                }
            }

            if (pds[0] == null && pds[1] != null)
            {
                pds[0] = pds[1];
                pds[1] = null;
            }

            if (pds[0] != null || pds[1] != null)
            {
                device = new MFDevice(pds[0], pds[1]);

                if (!device.Connect(2000, m_tryConnect))
                {
                    throw new MFDeviceNoResponseException();
                }
            }
            else
            {
                throw new MFDeviceUnknownDeviceException();
            }

            return(device);
        }
        static public PortDefinition CreateInstanceForTcp(string name)
        {
            PortDefinition portDefinition = null;

            //From CorDebug\DebugPort.cs
            string hostName = name;
            int port = PortDefinition_Tcp.WellKnownPort;
            int portIndex = hostName.IndexOf(':');
            IPAddress address = null;

            if (portIndex > 0)
            {
                hostName = name.Substring(0, portIndex);

                if (portIndex < name.Length - 1)
                {
                    string portString = name.Substring(portIndex + 1);

                    int portT;

                    if (int.TryParse(portString, out portT))
                    {
                        port = portT;
                    }
                }
            }

            if (!IPAddress.TryParse(hostName, out address))
            {
                //Does DNS resolution make sense here?

                IPHostEntry iPHostEntry = Dns.GetHostEntry(hostName);

                if (iPHostEntry.AddressList.Length > 0)
                {
                    //choose the first one?
                    address = iPHostEntry.AddressList[0];
                }
            }

            if (address != null)
            {
                IPEndPoint ipEndPoint = new IPEndPoint(address, port);

                portDefinition = new PortDefinition_Tcp(ipEndPoint);

                //ping to see if it is alive?
            }

            return portDefinition;
        }
Exemple #8
0
        public static PortDefinition[] EnumeratePorts(
            System.Net.IPAddress DiscoveryMulticastAddress,
            System.Net.IPAddress DiscoveryMulticastAddressRecv,
            int DiscoveryMulticastPort,
            string DiscoveryMulticastToken,
            int DiscoveryMulticastTimeout,
            int DiscoveryTTL
            )
        {
            PortDefinition_Tcp []       ports     = null;
            Dictionary <string, string> addresses = new Dictionary <string, string>();

            try
            {
                IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName());

                foreach (IPAddress ip in hostEntry.AddressList)
                {
                    if (ip.AddressFamily == AddressFamily.InterNetwork)
                    {
                        int    cnt   = 0;
                        int    total = 0;
                        byte[] data  = new byte[1024];
                        Socket sock  = null;
                        Socket recv  = null;

                        System.Net.IPEndPoint endPoint    = new System.Net.IPEndPoint(ip, 0);
                        System.Net.EndPoint   epRemote    = new System.Net.IPEndPoint(System.Net.IPAddress.Any, 26001);
                        System.Net.IPEndPoint epRecv      = new System.Net.IPEndPoint(ip, DiscoveryMulticastPort);
                        System.Net.IPEndPoint epMulticast = new System.Net.IPEndPoint(DiscoveryMulticastAddress, DiscoveryMulticastPort);

                        try
                        {
                            sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                            recv = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

                            recv.Bind(epRecv);
                            recv.ReceiveTimeout = DiscoveryMulticastTimeout;
                            recv.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(DiscoveryMulticastAddressRecv, ip));

                            sock.Bind(endPoint);
                            sock.MulticastLoopback = false;
                            sock.Ttl = (short)DiscoveryTTL;
                            sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 64);

                            // send ping
                            sock.SendTo(System.Text.Encoding.ASCII.GetBytes(DiscoveryMulticastToken), SocketFlags.None, epMulticast);

                            while (0 < (cnt = recv.ReceiveFrom(data, total, data.Length - total, SocketFlags.None, ref epRemote)))
                            {
                                addresses[((IPEndPoint)epRemote).Address.ToString()] = "";
                                total += cnt;
                                recv.ReceiveTimeout = DiscoveryMulticastTimeout / 2;
                            }

                            recv.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.DropMembership, new MulticastOption(DiscoveryMulticastAddressRecv));
                        }
                        // SocketException occurs in RecieveFrom if there is no data.
                        catch (SocketException)
                        {
                        }
                        finally
                        {
                            if (recv != null)
                            {
                                recv.Close();
                                recv = null;
                            }
                            if (sock != null)
                            {
                                sock.Close();
                                sock = null;
                            }
                        }

                        // use this if we need to get the MAC address of the device
                        SOCK_discoveryinfo disc = new SOCK_discoveryinfo();
                        disc.ipaddr        = 0;
                        disc.macAddressLen = 0;
                        int idx        = 0;
                        int c_DiscSize = Marshal.SizeOf(disc);
                        while (total >= c_DiscSize)
                        {
                            byte[] discData = new byte[c_DiscSize];
                            Array.Copy(data, idx, discData, 0, c_DiscSize);
                            GCHandle gch = GCHandle.Alloc(discData, GCHandleType.Pinned);
                            disc = (SOCK_discoveryinfo)Marshal.PtrToStructure(gch.AddrOfPinnedObject(), typeof(SOCK_discoveryinfo));
                            gch.Free();

                            // previously we only displayed the IP address for the device, which doesn't
                            // really tell you which device you are talking to.  The MAC address should be unique.
                            // therefore we will display the MAC address in the device display name to help distinguish
                            // the devices.
                            if (disc.macAddressLen <= 64 && disc.macAddressLen > 0)
                            {
                                IPAddress ipResp = new IPAddress((long)disc.ipaddr);

                                // only append the MAC if it matches one of the IP address we got responses from
                                if (addresses.ContainsKey(ipResp.ToString()))
                                {
                                    string strMac = "";
                                    for (int mi = 0; mi < disc.macAddressLen - 1; mi++)
                                    {
                                        unsafe
                                        {
                                            strMac += string.Format("{0:x02}-", disc.macAddressBuffer[mi]);
                                        }
                                    }
                                    unsafe
                                    {
                                        strMac += string.Format("{0:x02}", disc.macAddressBuffer[disc.macAddressLen - 1]);
                                    }

                                    addresses[ipResp.ToString()] = strMac;
                                }
                            }
                            total -= c_DiscSize;
                            idx   += c_DiscSize;
                        }
                    }
                }
            }
            catch (Exception e2)
            {
                System.Diagnostics.Debug.Print(e2.ToString());
            }

            ports = new PortDefinition_Tcp[addresses.Count];
            int i = 0;

            foreach (string key in addresses.Keys)
            {
                ports[i++] = new PortDefinition_Tcp(IPAddress.Parse(key), addresses[key]);
            }

            return(ports);
        }
Exemple #9
0
        private MFDevice InitializePorts(MFPortDefinition portDefinitionMain, MFPortDefinition portDefinitionTinyBooter)
        {
            MFDevice device = null;
            MFPortDefinition[] portDefs = new MFPortDefinition[2] { portDefinitionMain, portDefinitionTinyBooter };
            _DBG.PortDefinition[] pds = new Microsoft.SPOT.Debugger.PortDefinition[2];

            for (int i = 0;i < portDefs.Length;i++)
            {
                MFPortDefinition portDefinition = portDefs[i];
                if (portDefinition == null) continue;

                if (portDefinition.Transport == TransportType.TCPIP)
                {
                    System.Net.IPAddress[] addr = System.Net.Dns.GetHostAddresses(portDefinition.Port);

                    pds[i] = new Microsoft.SPOT.Debugger.PortDefinition_Tcp(addr[0]);
                }
                else
                {
                    ArrayList list = _DBG.PortDefinition.Enumerate(_DBG.PortFilter.Usb, _DBG.PortFilter.Serial);
                    foreach (_DBG.PortDefinition pd in list)
                    {
                        if (portDefinition.Port.Length > 0)
                        {
                            if (string.Equals(portDefinition.Port, pd.UniqueId.ToString()))
                            {
                                pds[i] = pd;
                                break;
                            }
                        }
                        if (string.Equals(portDefinition.Name, pd.DisplayName))
                        {
                            pds[i] = pd;
                            break;
                        }
                    }
                }
            }

            if (pds[0] == null && pds[1] != null)
            {
                pds[0] = pds[1];
                pds[1] = null;
            }

            if (pds[0] != null || pds[1] != null)
            {
                device = new MFDevice(pds[0], pds[1]);

                if (!device.Connect(2000, m_tryConnect))
                {
                    throw new MFDeviceNoResponseException();
                }
            }
            else
            {
                throw new MFDeviceUnknownDeviceException();
            }

            return device;
        }