Subversion Repositories HomeAutomation

Rev

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