Subversion Repositories HomeAutomation

Rev

Rev 458 | Rev 466 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. using System;
  2. using System.Threading;
  3. using System.Collections.Generic;
  4. using System.Text;
  5.  
  6. class Program {
  7.     const byte DEBUG_LEVEL = 3; //0, 1, 2, 3
  8.    
  9.     //connects to candaemon on tcp to send and receive packets
  10.     static DaemonConnection dc;
  11.     //a commandline arguments parser
  12.     static Arguments CommandLine;
  13.     //functions for downloading hex to target
  14.     static Downloader dl;
  15.     //maintains a hexfile
  16.     private static HexFile hf;
  17.    
  18.     static bool aBios=false;        //if commandline says it is a bios update
  19.     static bool aReset=false;       //if commandline says a reset should be sent before downloading to target
  20.     static bool aStart=false;       //if commandline says a start should be sent after downloading to target
  21.     static bool aTerminal=false;    //if commandline says we should start in interactive mode
  22.        
  23.     static bool sNodeid=false;
  24.     static bool sHexfile=false;
  25.        
  26.     static string hexfile=null;
  27.     static byte nodeid=0;
  28.     static string host=null;
  29.     static int port=1200;
  30.    
  31.     static void Main(string[] args) {
  32.  
  33.         bool error = false;
  34.         if (args.Length < 4) {
  35.             Console.WriteLine("Syntax: program.exe [options] -h <host> -p <port>");
  36.             Console.WriteLine("Host and port are host and port of canDaemon, most likely localhost and 1200.");
  37.             Console.WriteLine("Options:");
  38.             Console.WriteLine("  -f <hexfile>   Specify a file to be loaded to target.");
  39.             Console.WriteLine("  -n <nodeid>    Id of target node, hex or decimal.");
  40.             Console.WriteLine("  -t             Enter terminal mode.");
  41.             Console.WriteLine("  -r             Reset node before loading target.");
  42.             Console.WriteLine("  -s             Start node after loading target.");
  43.             Console.WriteLine("  -b             Program hex as bios, use with caution.");
  44.             error = true;
  45.             return;
  46.         }
  47.  
  48.         if (!error) {
  49.             //Console.WriteLine("arg0: " + args[0] + "\n");
  50.             //argPort = args[0];
  51.             //argBaud = Int32.Parse(args[1]);
  52.             CommandLine=new Arguments(args);
  53.            
  54.             if (CommandLine["b"] == "true") { aBios = true; }
  55.             if (CommandLine["r"] == "true") { aReset = true; }
  56.             if (CommandLine["s"] == "true") { aStart = true; }
  57.             if (CommandLine["t"] == "true") { aTerminal = true; }
  58.  
  59.             hexfile = CommandLine["f"];
  60.            
  61.             if (hexfile != null) {
  62.                 sHexfile = true;
  63.             }
  64.             if (!aTerminal && !sHexfile) {
  65.                 Console.WriteLine("When not in terminal mode a hexfile must be supplied");
  66.                 error = true;
  67.             }
  68.            
  69.             string node = CommandLine["n"];
  70.            
  71.             if (node != null) {
  72.                 if (!parseNodeId(node)) {
  73.                     error = true;
  74.                 }
  75.             }
  76.            
  77.             if (!aTerminal && !sNodeid) {
  78.                 Console.WriteLine("When not in terminal mode a nodeid must be supplied");
  79.                 error = true;
  80.             }
  81.            
  82.             host = CommandLine["h"];
  83.             string sPort = CommandLine["p"];
  84.             if ((host == null) || (sPort.Length == 0)) {
  85.                 error = true;
  86.             } else {
  87.                 try {
  88.                     port = int.Parse(sPort);
  89.                 } catch {
  90.                     error = true;
  91.                 }
  92.             }
  93.         }
  94.        
  95.         if  (!error) {
  96.             //commandline feedback output (use debuglevel for hiding these)
  97.             Console.WriteLine("canDude settings:");
  98.             if (sNodeid) { Console.WriteLine("Nodeaddress: 0x" + String.Format("{0:x2}", nodeid)); }
  99.             Console.WriteLine("Host: {0}:{1}", host, port);
  100.             if (hexfile != null) { Console.WriteLine("Hexfile: {0}", hexfile); }
  101.             Console.Write("Parameters: ");
  102.             if (aBios) { Console.Write("Bios "); }
  103.             if (aReset) { Console.Write("Reset "); }
  104.             if (aStart) { Console.Write("Start "); }
  105.             if (aTerminal) { Console.Write("Terminal"); }
  106.             Console.WriteLine("");
  107.         }
  108.        
  109.         //connect to candaemon
  110.         dc = new DaemonConnection();
  111.         if (dc.init(host, port)) {
  112.             Console.WriteLine("Connected to candaemon");
  113.         } else {
  114.             Console.WriteLine("Connection to candaemon could not be established");
  115.             error = true;
  116.         }
  117.            
  118.         if (!error && aTerminal) {
  119.             bool success = true;
  120.            
  121.             hf = new HexFile();
  122.             if (sHexfile && success) {
  123.                 if (hf.loadHex(hexfile)) {
  124.                     Console.WriteLine("Hexfile loaded");
  125.                 } else { success = false; }
  126.             }
  127.            
  128.             if (success) {
  129.                 if (DEBUG_LEVEL>0) {
  130.                     Console.WriteLine("");
  131.                     printHelp();
  132.                     Console.WriteLine("");
  133.                     //Thread.Sleep(1000);
  134.                 }
  135.                
  136.                 string instr;
  137.                 do {
  138.                     Console.Write("> ");
  139.                     instr = Console.ReadLine();
  140.                 } while (parseInput(instr));
  141.                
  142.                 //exit command
  143.             }
  144.         }
  145.  
  146.         if (!error && !aTerminal) {
  147.             //not terminal mode
  148.             bool success = true;
  149.            
  150.             //load hexfile (a hexfile must be supplied as commandline argument)
  151.             hf = new HexFile();
  152.             if (hf.loadHex(hexfile)) {
  153.                 Console.WriteLine("Hexfile loaded");
  154.             } else {
  155.                 success = false;
  156.                 Console.WriteLine("Hexfile could not be loaded, I quit");
  157.             }
  158.            
  159.             //if a hexfile is loaded and we are connected to candaemon then start autodownloading sequence
  160.             if (success) {
  161.                 bool autosuccess = true;
  162.                
  163.                 CanNMT cpn = new CanNMT();
  164.                 if (aReset) {
  165.                     //send a reset command (and wait for feedback)
  166.                     if (!cpn.doReset(dc, nodeid)) {
  167.                         Console.WriteLine("Target node did not respond to reset, I quit");
  168.                         autosuccess = false;
  169.                     }
  170.                 }
  171.                
  172.                 if (autosuccess) {
  173.                     //send application
  174.                     dl = new Downloader(hf, dc, nodeid, aBios);
  175.                     if (!dl.go()) {
  176.                         Console.WriteLine("Error occured during download");
  177.                         autosuccess = false;
  178.                     }
  179.                 }
  180.                
  181.                 if (autosuccess && aStart) {
  182.                     //start applikation
  183.                     cpn.doStart(dc, nodeid);
  184.                 }
  185.                
  186.             }
  187.         }
  188.         dc.stop();
  189.     }
  190.    
  191.     static private void printHelp() {
  192.         Console.WriteLine("canDude commands:");
  193.         //Console.WriteLine("reset - reset communication.");
  194.         Console.WriteLine("load [<hexfile>] - reload hexfile or load new hexfile");
  195.         Console.WriteLine("node <nodeid>    - specify id of target node, hex or decimal");
  196.         Console.WriteLine("reset            - reset node");
  197.         Console.WriteLine("start            - start application in node (no feedback yet)");
  198.         Console.WriteLine("go               - start download application to target");
  199.         Console.WriteLine("go bios          - start download bios to target, use with caution");
  200.         Console.WriteLine("help             - prints this help");
  201.         Console.WriteLine("exit or quit     - exit program");
  202.     }  
  203.    
  204.     static private bool parseNodeId(string node) {
  205.         sNodeid = false;
  206.         if (node.Length > 0) {
  207.             try {
  208.                 nodeid = byte.Parse(node);
  209.                 sNodeid = true;
  210.             } catch {}
  211.         }
  212.         if (node.Length > 2) {
  213.             if (node.Substring(0, 2).Equals("0x")) {
  214.                 if (node.Length == 3) {
  215.                     try {
  216.                         nodeid = byte.Parse(node.Substring(2, 1), System.Globalization.NumberStyles.HexNumber);
  217.                         sNodeid = true;
  218.                     } catch {
  219.                         Console.WriteLine("nodeadress error!");
  220.                         return false;
  221.                     }
  222.                 } else if (node.Length == 4) {
  223.                     try {
  224.                         nodeid = byte.Parse(node.Substring(2, 2), System.Globalization.NumberStyles.HexNumber);
  225.                         sNodeid = true;
  226.                     } catch {
  227.                         Console.WriteLine("nodeadress error!");
  228.                         return false;
  229.                     }
  230.                 } else {
  231.                     Console.WriteLine("nodeadress error!");
  232.                     return false;
  233.                 }
  234.             }
  235.         }
  236.        
  237.         return true;
  238.     }
  239.    
  240.     static private bool parseInput(string instr) {
  241.         if (instr.Equals("reset")) {
  242.             if (sNodeid) {
  243.                 CanNMT cpn = new CanNMT();
  244.                 cpn.doReset(dc, nodeid);
  245.             } else {
  246.                 Console.WriteLine("No nodeid has been specified");
  247.             }
  248.         }
  249.         else if (instr.Equals("start")) {
  250.             if (sNodeid) {
  251.                 CanNMT cpn = new CanNMT();
  252.                 cpn.doStart(dc, nodeid);
  253.             } else {
  254.                 Console.WriteLine("No nodeid has been specified");
  255.             }
  256.         }
  257.         else if (instr.StartsWith("go")) {
  258.             bool dlBios = false;
  259.             bool error = true;
  260.             if (instr.Equals("go bios")) {
  261.                 dlBios = true;
  262.                 error = false;
  263.             } else if (instr.Equals("go")) {
  264.                 error = false;
  265.             }
  266.            
  267.             if (!sNodeid) {
  268.                 Console.WriteLine("No nodeid has been specified");
  269.                 error = true;
  270.             }
  271.             if (!sHexfile) {
  272.                 Console.WriteLine("No hexfile has been specified");
  273.                 error = true;
  274.             }
  275.            
  276.             CanNMT cpn = new CanNMT();
  277.             if (!error && aReset) {
  278.                 //send a reset command (and wait for feedback)
  279.                 if (!cpn.doReset(dc, nodeid)) {
  280.                     Console.WriteLine("Target node did not respond to reset");
  281.                     error = true;
  282.                 }
  283.             }
  284.                
  285.             if (!error) {
  286.                 //send application
  287.                 dl = new Downloader(hf, dc, nodeid, dlBios);
  288.                 if (!dl.go()) {
  289.                     Console.WriteLine("Error occured during download");
  290.                     error = true;
  291.                 }
  292.             }
  293.                
  294.             if (!error && aStart) {
  295.                 //start applikation
  296.                 cpn.doStart(dc, nodeid);
  297.             }
  298.            
  299.         }
  300.         else if (instr.StartsWith("load")) {
  301.             if (instr.Length > 5) {
  302.                 //second argument is a hexfile, this file should be loaded
  303.                 hexfile = instr.Substring(5);
  304.                 if (hf.loadHex(hexfile)) {
  305.                     sHexfile = true;
  306.                     Console.WriteLine("Hexfile loaded");
  307.                 } else {
  308.                     Console.WriteLine("Hexfile could not be loaded");
  309.                 }
  310.             } else {
  311.                 //reload current hexfile. if a hexfile already has been specified
  312.                 if (sHexfile) {
  313.                     if (hf.loadHex(hexfile)) {
  314.                         Console.WriteLine("Hexfile reloaded");
  315.                     } else {
  316.                         Console.WriteLine("Hexfile could not be reloaded");
  317.                     }
  318.                 } else {
  319.                     Console.WriteLine("No hexfile has been specified");
  320.                 }
  321.                
  322.             }
  323.         }
  324.         else if (instr.StartsWith("node")) {
  325.             //change nodeid
  326.             if (instr.Length > 5) {
  327.                 //second argument is nodeid
  328.                 string node = instr.Substring(5);
  329.                 parseNodeId(node);
  330.             }
  331.             Console.WriteLine("NodeId is 0x"+ String.Format("{0:x2}", nodeid));
  332.         }
  333.         else if (instr.Equals("help")) {
  334.             printHelp();
  335.         }
  336.         else if (instr.Equals("exit") || instr.Equals("quit")) {
  337.             return false;
  338.         }
  339.         else {
  340.             Console.WriteLine("Unknown command.");
  341.         }
  342.        
  343.         return true;
  344.     }
  345.    
  346. }
  347.