Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            if (args.Length != 1)
            {
                string[] ports = SerialPort.GetPortNames();
                Console.WriteLine("COM port parameter not provided.");
                Console.WriteLine("Available serial ports: ");
                foreach (var port in ports)
                {
                    Console.WriteLine(port);
                }
                Console.ReadKey();
                return;
            }

            StreamUART uartStream = new StreamUART(args[0]);

            ncpInterface = new NcpInterface();
            ncpInterface.OnLastStatusHandler  += OnLastStatus;
            ncpInterface.OnLowpanStateChanged += OnStateChanged;

            try
            {
                ncpInterface.Open(uartStream);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadKey();
                return;
            }

            //  NetworkingInterface.SetupInterface(ncpInterface);

            while (true)
            {
                Console.Write(">");
                string line = Console.ReadLine().Trim().ToLower();

                if (line == "quit" || line == "exit")
                {
                    isUdpListenerRunning = false;

                    if (threadUdpListening != null)
                    {
                        if (threadUdpListening.IsAlive)
                        {
                            threadUdpListening.Interrupt();
                        }

                        threadUdpListening = null;
                    }

                    return;
                }

                string   cmdName = line.Split(' ').First();
                string[] cmdArgs = line.Split(' ').Skip(1).ToArray();

                switch (cmdName)
                {
                case "help":
                    PrintHelp(cmdArgs);
                    break;

                case "reset":
                    ncpInterface.Reset();
                    break;

                case "form":
                    DoForm(cmdArgs);
                    break;

                case "attach":
                    DoJoin(cmdArgs, false);
                    break;

                case "join":
                    DoJoin(cmdArgs, true);
                    break;

                case "ping":
                    DoPing(cmdArgs);
                    break;

                case "status":
                    Console.WriteLine(ncpInterface.LastStatus.ToString());
                    break;

                case "protocol":
                    Console.WriteLine(ncpInterface.ProtocolVersion);
                    break;

                case "version":
                    Console.WriteLine(ncpInterface.NcpVersion);
                    break;

                case "interface":
                    Console.WriteLine(ncpInterface.InterfaceType.ToString());
                    break;

                case "vendor":
                    Console.WriteLine(ncpInterface.Vendor);
                    break;

                case "connected":
                    Console.WriteLine(ncpInterface.Connected.ToString());
                    break;

                case "caps":
                    DoCaps();
                    break;

                case "networkname":
                    DoNetworkName(cmdArgs);
                    break;

                case "scan":
                    DoScan(cmdArgs);
                    break;

                case "bufferinfo":
                    DoBufferInfo();
                    break;

                case "channel":
                    DoChannel(cmdArgs);
                    break;

                case "ipaddr":
                    DoIPpaddr(cmdArgs);
                    break;

                case "ifconfig":
                    DoIfConfig(cmdArgs);
                    break;

                case "thread":
                    DoThread(cmdArgs);
                    break;

                case "panid":
                    DoPanId(cmdArgs);
                    break;

                case "extpanid":
                    DoXpanId(cmdArgs);
                    break;

                case "state":
                    DoState(cmdArgs);
                    break;

                case "masterkey":
                    DoMasterkey(cmdArgs);
                    break;

                case "partition":
                    Console.WriteLine(ncpInterface.PartitionId.ToString());
                    break;

                case "extaddr":
                    Console.WriteLine(ncpInterface.ExtendedAddress.ToString());
                    break;

                case "macaddr":
                    Console.WriteLine(ncpInterface.HardwareAddress.ToString());
                    break;

                case "udp":
                    DoUdp(cmdArgs);
                    break;
                }
            }
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            if (args.Length != 1)
            {
                string[] ports = SerialPort.GetPortNames();
                Console.WriteLine("COM port parameter not provided.");
                Console.WriteLine("Available serial ports: ");
                foreach (var serialPort in ports)
                {
                    Console.WriteLine(serialPort);
                }
                Console.ReadKey();
                return;
            }

            StreamUART uartStream = new StreamUART(args[0]);

            ncpInterface = new NcpInterface();

            try
            {
                ncpInterface.Open(uartStream);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadKey();
                return;
            }

            // NetworkingInterface.SetupInterface(ncpInterface);

            Console.Write("Networkname:");
            string networkname = Console.ReadLine();

            Console.Write("Channel:");
            byte channel = Convert.ToByte(Console.ReadLine());

            Console.Write("Masterkey:");
            string masterkey = Console.ReadLine();

            Console.Write("Panid:");
            ushort panid = Convert.ToUInt16(Console.ReadLine());

            Console.Write("Listener port:");
            ushort port = Convert.ToUInt16(Console.ReadLine());

            try
            {
                ncpInterface.Form(networkname, channel, masterkey, panid);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadKey();
                return;
            }

            UdpSocket receiver = new UdpSocket();

            receiver.Bind(IPv6Address.IPv6Any, port);
            IPv6EndPoint remoteIp = null;

            while (true)
            {
                if (receiver.Poll(-1, SelectMode.SelectRead))
                {
                    byte[] data    = receiver.Receive(ref remoteIp);
                    string message = Encoding.ASCII.GetString(data);
                    Console.WriteLine("\n");
                    Console.WriteLine("{0} bytes from {1} {2} {3}", message.Length, remoteIp.Address, remoteIp.Port, message);
                    Console.WriteLine(">");
                }
            }
        }