コード例 #1
0
ファイル: Program.cs プロジェクト: Cougar/HomeAutomation
    static bool sNodeid = false; //true when a nodeid has been parsed

    #endregion Fields

    #region Methods

    static void Main(string[] args)
    {
        bool error = false;
        if (args.Length < 4) {
            //if to few arguments then print info and quit
            printSyntax();
            error = true;
        }

        if (!error) {
            //parse commandline arguments
            CommandLine=new Arguments(args);

            //check single arguments
            if (CommandLine["b"] == "true") { aBios = true; }
            if (CommandLine["r"] == "true") { aReset = true; }
            if (CommandLine["s"] == "true") { aStart = true; }
            if (CommandLine["t"] == "true") { aTerminal = true; }

            //check hexfile argument
            hexfile = CommandLine["f"];
            if (hexfile != null) {
                sHexfile = true;
            }
            if (!aTerminal && !sHexfile && !aReset && !aStart) {
                if (DEBUG_LEVEL>0) { Console.WriteLine("When not in terminal mode a hexfile, a reset or a start parameter must be supplied, I quit"); }
                error = true;
            }

            //check nodeid argument
            string node = CommandLine["n"];
            if (node != null) {
                if (!parseNodeId(node)) {
                    error = true;
                }
            }
            if (!aTerminal && !sNodeid) {
                if (DEBUG_LEVEL>0) { Console.WriteLine("When not in terminal mode a nodeid must be supplied, I quit"); }
                error = true;
            }

            //check host and port arguments
            host = CommandLine["h"];
            string sPort = CommandLine["p"];
            if ((host == null) || (sPort.Length == 0)) {
                if (DEBUG_LEVEL>0) { printSyntax(); }
                error = true;
            } else {
                try {
                    port = int.Parse(sPort);
                } catch {
                    if (DEBUG_LEVEL>0) { Console.WriteLine("Was not able to parse port, I quit"); }
                    error = true;
                }
            }
        }

        if  (!error) {
            //commandline feedback output
            if (DEBUG_LEVEL>2) {
                Console.WriteLine("canDude settings:");
                if (sNodeid) { Console.WriteLine("Nodeaddress: 0x" + String.Format("{0:x2}", nodeid)); }
                Console.WriteLine("Host: {0}:{1}", host, port);
                if (hexfile != null) { Console.WriteLine("Hexfile: {0}", hexfile); }
                Console.Write("Parameters: ");
                if (aBios) { Console.Write("Bios "); }
                if (aReset) { Console.Write("Reset "); }
                if (aStart) { Console.Write("Start "); }
                if (aTerminal) { Console.Write("Terminal"); }
                Console.WriteLine("");
            }
        }

        if (!error) {
            //connect to candaemon
            dc = new DaemonConnection();
            if (dc.init(host, port)) {
                if (DEBUG_LEVEL>1) { Console.WriteLine("Connected to candaemon"); }
            } else {
                if (DEBUG_LEVEL>0) { Console.WriteLine("Connection to candaemon could not be established, I quit"); }
                error = true;
            }
        }

        if (!error && aTerminal) {
            //terminal mode (==interactive mode)
            bool success = true;
            //load hexfile if one has been specified as commandline argument
            hf = new HexFile();
            if (sHexfile && success) {
                if (hf.loadHex(hexfile)) {
                    if (DEBUG_LEVEL>1) { Console.WriteLine("Hexfile loaded"); }
                } else { success = false; }
            }

            if (success) {
                if (DEBUG_LEVEL>1) {
                    Console.WriteLine("");
                    printHelp();		//print available commands
                    Console.WriteLine("");
                }

                string instr;
                do {
                    Console.Write("> ");			//a command has been executed, print a new console char
                    instr = Console.ReadLine();		//read std in
                } while (parseInput(instr));		//parse a line from std in

                //exit command
            }
        }

        if (!error && !aTerminal) {
            //not terminal mode
            bool success = true;

            if (sHexfile) {
                //load hexfile (a hexfile must be supplied as commandline argument)
                hf = new HexFile();
                if (hf.loadHex(hexfile)) {
                    if (DEBUG_LEVEL>1) { Console.WriteLine("Hexfile loaded"); }
                } else {
                    success = false;
                    if (DEBUG_LEVEL>0) { Console.WriteLine("Hexfile could not be loaded, I quit"); }
                }
            }

            //if a hexfile is loaded then start autodownloading sequence
            if (success) {
                bool autosuccess = true;

                CanNMT cpn = new CanNMT();
                if (aReset) {
                    //send a reset command (and wait for feedback)
                    if (!cpn.doReset(dc, nodeid)) {
                        if (DEBUG_LEVEL>0) { Console.WriteLine("Target node did not respond to reset, I quit"); }
                        autosuccess = false;
                    }
                }

                if (sHexfile && autosuccess) {
                    //send application
                    dl = new Downloader(hf, dc, nodeid, aBios);
                    if (!dl.go()) {
                        if (DEBUG_LEVEL>0) { Console.WriteLine("Error occured during download"); }
                        autosuccess = false;
                    }
                }

                if (autosuccess && aStart) {
                    //start application
                    cpn.doStart(dc, nodeid);
                }

            }
        }
        if (dc != null) {
            //stop tcp client thread
            dc.stop();
        }
    }
コード例 #2
0
ファイル: Program.cs プロジェクト: Cougar/HomeAutomation
    private static bool parseInput(string instr)
    {
        //parse commands entered on std in
        if (instr.Equals("reset")) {		//reset command, send a reset to current node
            if (sNodeid) {
                dc.flushData();
                CanNMT cpn = new CanNMT();
                cpn.doReset(dc, nodeid);
            } else {
                if (DEBUG_LEVEL>0) { Console.WriteLine("No nodeid has been specified"); }
            }
        }
        else if (instr.Equals("start")) {	//start command, send a start to current node
            if (sNodeid) {
                CanNMT cpn = new CanNMT();
                cpn.doStart(dc, nodeid);
            } else {
                if (DEBUG_LEVEL>0) { Console.WriteLine("No nodeid has been specified"); }
            }
        }
        else if (instr.StartsWith("go")) {	//downloading command
            bool dlBios = false;
            bool error = true;
            if (instr.Equals("go bios")) {	//if bios should be downloaded
                dlBios = true;
                error = false;
            } else if (instr.Equals("go")) {	//is an application should be downloaded
                error = false;
            }

            if (!sNodeid) {
                if (DEBUG_LEVEL>0) { Console.WriteLine("No nodeid has been specified"); }
                error = true;
            }
            if (!sHexfile) {
                if (DEBUG_LEVEL>0) { Console.WriteLine("No hexfile has been specified"); }
                error = true;
            }

            CanNMT cpn = new CanNMT();
            if (!error && aReset) {				//commandline arguments specified that a reset should be done before download
                //send a reset command (and wait for feedback)
                if (!cpn.doReset(dc, nodeid)) {
                    if (DEBUG_LEVEL>0) { Console.WriteLine("Target node did not respond to reset"); }
                    error = true;
                }
            }

            if (!error) {
                //send application
                dc.flushData();
                dl = new Downloader(hf, dc, nodeid, dlBios);
                if (!dl.go()) {
                    if (DEBUG_LEVEL>0) { Console.WriteLine("Error occured during download"); }
                    error = true;
                }
            }

            if (!error && aStart) {				//commandline arguments specified that a start should be done after download
                //start applikation
                cpn.doStart(dc, nodeid);
            }

        }
        else if (instr.StartsWith("load")) {	//load hexfile or reload current hexfile
            if (instr.Length > 5) {
                //second argument is a hexfile, this file should be loaded
                hexfile = instr.Substring(5);
                if (hf.loadHex(hexfile)) {
                    sHexfile = true;
                    if (DEBUG_LEVEL>1) { Console.WriteLine("Hexfile loaded"); }
                } else {
                    if (DEBUG_LEVEL>0) { Console.WriteLine("Hexfile could not be loaded"); }
                }
            } else {
                //reload current hexfile. if a hexfile already has been specified
                if (sHexfile) {
                    if (hf.loadHex(hexfile)) {
                        if (DEBUG_LEVEL>1) { Console.WriteLine("Hexfile reloaded"); }
                    } else {
                        if (DEBUG_LEVEL>0) { Console.WriteLine("Hexfile could not be reloaded"); }
                    }
                } else {
                    if (DEBUG_LEVEL>0) { Console.WriteLine("No hexfile has been specified"); }
                }

            }
        }
        else if (instr.StartsWith("node")) {
            //change nodeid
            if (instr.Length > 5) {
                //second argument is nodeid
                string node = instr.Substring(5);
                parseNodeId(node);
            }
            if (DEBUG_LEVEL>1) { Console.WriteLine("NodeId is 0x"+ String.Format("{0:x2}", nodeid)); }
        }
        else if (instr.Equals("help")) {
            printHelp();
        }
        else if (instr.Equals("exit") || instr.Equals("quit") || instr.Equals("q")) {
            return false;
        }
        else {
            Console.WriteLine("Unknown command.");
        }

        return true;
    }