Subversion Repositories HomeAutomation

Rev

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