Subversion Repositories HomeAutomation

Rev

Rev 456 | Rev 459 | 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.                     printHelp();
  136.                     Console.WriteLine("");
  137.                     //Thread.Sleep(1000);
  138.                 }
  139.                
  140.                 string instr;
  141.                 do {
  142.                     Console.Write("> ");
  143.                     instr = Console.ReadLine();
  144.                 } while (parseInput(instr));
  145.                
  146.                 //exit command
  147.                 dc.stop();
  148.             }
  149.         }
  150.  
  151.         if (!error && !aTerminal) {
  152.             //not terminal mode
  153.             bool success = true;
  154.            
  155.             //load hexfile (a hexfile must be supplied as commandline argument)
  156.             hf = new HexFile();
  157.             if (hf.loadHex(hexfile)) {
  158.                 Console.WriteLine("Hexfile loaded");
  159.             } else {
  160.                 success = false;
  161.                 Console.WriteLine("Hexfile could not be loaded, I quit");
  162.             }
  163.            
  164.             //connect to candaemon
  165.             dc = new DaemonConnection();
  166.             if (success) {
  167.                 if (dc.init(host, port)) {
  168.                     Console.WriteLine("Connected to candaemon");
  169.                 } else {
  170.                     Console.WriteLine("Connection to candaemon could not be established, I quit");
  171.                     success = false;
  172.                 }
  173.             }
  174.            
  175.             //if a hexfile is loaded and we are connected to candaemon then start autodownloading sequence
  176.             if (success) {
  177.                 bool autosuccess = true;
  178.                
  179.                 CanNMT cpn = new CanNMT();
  180.                 if (aReset) {
  181.                     //send a reset command (and wait for feedback)
  182.                     if (!cpn.doReset(dc, nodeid)) {
  183.                         Console.WriteLine("Target node did not respond to reset, I quit");
  184.                         autosuccess = false;
  185.                     }
  186.                 }
  187.                
  188.                 if (autosuccess) {
  189.                     //send application
  190.                     Console.WriteLine("Programming not implemented yet, I quit");
  191.                     autosuccess = false;
  192.                 }
  193.                
  194.                 if (autosuccess && aStart) {
  195.                     //start applikation
  196.                     cpn.doStart(dc, nodeid);
  197.                 }
  198.                
  199.             }
  200.         }
  201.     }
  202.    
  203.     static private void printHelp() {
  204.         Console.WriteLine("canDude commands:");
  205.         //Console.WriteLine("reset - reset communication.");
  206.         Console.WriteLine("load [<hexfile>] - reload hexfile or load new hexfile");
  207.         Console.WriteLine("node <nodeid>    - specify id of target node, hex or decimal");
  208.         Console.WriteLine("reset            - reset node");
  209.         Console.WriteLine("start            - start application in node (no feedback yet)");
  210.         Console.WriteLine("go               - start download application to target");
  211.         Console.WriteLine("go bios          - start download bios to target, use with caution");
  212.         Console.WriteLine("help             - prints this help");
  213.         Console.WriteLine("exit or quit     - exit program");
  214.     }  
  215.    
  216.     static private bool parseNodeId(string node) {
  217.         sNodeid = false;
  218.         if (node.Length > 0) {
  219.             try {
  220.                 nodeid = byte.Parse(node);
  221.                 sNodeid = true;
  222.             } catch {}
  223.         }
  224.         if (node.Length > 2) {
  225.             if (node.Substring(0, 2).Equals("0x")) {
  226.                 if (node.Length == 3) {
  227.                     try {
  228.                         nodeid = byte.Parse(node.Substring(2, 1), System.Globalization.NumberStyles.HexNumber);
  229.                         sNodeid = true;
  230.                     } catch {
  231.                         Console.WriteLine("nodeadress error!");
  232.                         return false;
  233.                     }
  234.                 } else if (node.Length == 4) {
  235.                     try {
  236.                         nodeid = byte.Parse(node.Substring(2, 2), System.Globalization.NumberStyles.HexNumber);
  237.                         sNodeid = true;
  238.                     } catch {
  239.                         Console.WriteLine("nodeadress error!");
  240.                         return false;
  241.                     }
  242.                 } else {
  243.                     Console.WriteLine("nodeadress error!");
  244.                     return false;
  245.                 }
  246.             }
  247.         }
  248.        
  249.         return true;
  250.     }
  251.    
  252.     static private bool parseInput(string instr) {
  253.         if (instr.Equals("reset")) {
  254.             if (sNodeid) {
  255.                 CanNMT cpn = new CanNMT();
  256.                 cpn.doReset(dc, nodeid);
  257.             } else {
  258.                 Console.WriteLine("No nodeid has been specified");
  259.             }
  260.         }
  261.         else if (instr.Equals("start")) {
  262.             if (sNodeid) {
  263.                 CanNMT cpn = new CanNMT();
  264.                 cpn.doStart(dc, nodeid);
  265.             } else {
  266.                 Console.WriteLine("No nodeid has been specified");
  267.             }
  268.         }
  269.         else if (instr.StartsWith("go")) {
  270.             bool dlBios = false;
  271.             bool error = true;
  272.             if (instr.Equals("go bios")) {
  273.                 dlBios = true;
  274.                 error = false;
  275.             } else if (instr.Equals("go")) {
  276.                 error = false;
  277.             }
  278.            
  279.             if (!sNodeid) {
  280.                 Console.WriteLine("No nodeid has been specified");
  281.                 error = true;
  282.             }
  283.             if (!sHexfile) {
  284.                 Console.WriteLine("No hexfile has been specified");
  285.                 error = true;
  286.             }
  287.            
  288.             CanNMT cpn = new CanNMT();
  289.             if (!error && aReset) {
  290.                 //send a reset command (and wait for feedback)
  291.                 if (!cpn.doReset(dc, nodeid)) {
  292.                     Console.WriteLine("Target node did not respond to reset");
  293.                     error = true;
  294.                 }
  295.             }
  296.                
  297.             if (!error) {
  298.                 //send application
  299.                 dl = new Downloader(hf, dc, nodeid, dlBios);
  300.                 if (!dl.go()) {
  301.                     Console.WriteLine("Error occured during download");
  302.                     error = true;
  303.                 }
  304.             }
  305.                
  306.             if (!error && aStart) {
  307.                 //start applikation
  308.                 cpn.doStart(dc, nodeid);
  309.             }
  310.            
  311.         }
  312.         else if (instr.StartsWith("load")) {
  313.             if (instr.Length > 5) {
  314.                 //second argument is a hexfile, this file should be loaded
  315.                 hexfile = instr.Substring(5);
  316.                 if (hf.loadHex(hexfile)) {
  317.                     sHexfile = true;
  318.                     Console.WriteLine("Hexfile loaded");
  319.                 } else {
  320.                     Console.WriteLine("Hexfile could not be loaded");
  321.                 }
  322.             } else {
  323.                 //reload current hexfile. if a hexfile already has been specified
  324.                 if (sHexfile) {
  325.                     if (hf.loadHex(hexfile)) {
  326.                         Console.WriteLine("Hexfile reloaded");
  327.                     } else {
  328.                         Console.WriteLine("Hexfile could not be reloaded");
  329.                     }
  330.                 } else {
  331.                     Console.WriteLine("No hexfile has been specified");
  332.                 }
  333.                
  334.             }
  335.         }
  336.         else if (instr.StartsWith("node")) {
  337.             //change nodeid
  338.             if (instr.Length > 5) {
  339.                 //second argument is nodeid
  340.                 string node = instr.Substring(5);
  341.                 parseNodeId(node);
  342.             }
  343.             Console.WriteLine("NodeId is 0x"+ String.Format("{0:x2}", nodeid));
  344.         }
  345.         else if (instr.Equals("help")) {
  346.             printHelp();
  347.         }
  348.         else if (instr.Equals("exit") || instr.Equals("quit")) {
  349.             return false;
  350.         }
  351.         else {
  352.             Console.WriteLine("Unknown command.");
  353.         }
  354.        
  355.         return true;
  356.     }
  357.    
  358. }
  359.