Subversion Repositories HomeAutomation

Rev

Rev 770 | Rev 1527 | 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. ///TODO:
  7. // argument -v # (debuglevel)
  8. // argument till konstruktor på alla objekt: debuglevel
  9.  
  10. class Program {
  11.     const byte DEBUG_LEVEL = 3; //0, 1, 2, 3
  12.     static bool HWID_INUSE = false;
  13.    
  14.     //connects to candaemon on tcp to send and receive packets
  15.     static DaemonConnection dc;
  16.     //a commandline arguments parser
  17.     static Arguments CommandLine;
  18.     //functions for downloading hex to target
  19.     static Downloader dl;
  20.     //maintains a hexfile
  21.     private static HexFile hf;
  22.    
  23.     static bool aBios=false;        //if commandline says it is a bios update
  24.     static bool aReset=false;       //if commandline says a reset should be sent before downloading to target
  25.     static bool aStart=false;       //if commandline says a start should be sent after downloading to target
  26.     static bool aTerminal=false;    //if commandline says we should start in interactive mode
  27.        
  28.     static bool sNodeid=false;      //true when a nodeid has been parsed
  29.     static bool sHWid=false;        //true when a hardware id has been parsed
  30.     static bool sHexfile=false;     //true when a hexfile has been specified
  31.        
  32.     static string hexfile=null;     //path to a hexfile
  33.     static byte nodeid=0;           //id of current node
  34.     static uint HWid=0;             //id of current node
  35.     static string host=null;        //address of candaemon-host
  36.     static int port=1200;           //port of candaemon-host
  37.    
  38.     static void Main(string[] args) {
  39.         bool error = false;
  40.         if (args.Length < 4) {
  41.             //if to few arguments then print info and quit
  42.             printSyntax();
  43.             error = true;
  44.         }
  45.  
  46.         if (!error) {
  47.             //parse commandline arguments
  48.             CommandLine=new Arguments(args);
  49.            
  50.             //check single arguments
  51.             if (CommandLine["b"] == "true") { aBios = true; }
  52.             if (CommandLine["r"] == "true") { aReset = true; }
  53.             if (CommandLine["s"] == "true") { aStart = true; }
  54.             if (CommandLine["t"] == "true") { aTerminal = true; }
  55.            
  56.             //check hexfile argument
  57.             hexfile = CommandLine["f"];
  58.             if (hexfile != null) {
  59.                 sHexfile = true;
  60.             }
  61.             if (!aTerminal && !sHexfile && !aReset && !aStart) {
  62.                 if (DEBUG_LEVEL>0) { Console.WriteLine("When not in terminal mode a hexfile, a reset or a start parameter must be supplied, I quit"); }
  63.                 error = true;
  64.             }
  65.            
  66.             //check nodeid argument
  67.             string node = CommandLine["n"];
  68.             if (node != null) {
  69.                 if (!parseNodeId(node)) {
  70.                     error = true;
  71.                 }
  72.             }
  73.             //check hwid argument
  74.             string hwid = CommandLine["i"];
  75.             if (hwid != null) {
  76.                 if (!parseHWId(hwid)) {
  77.                     error = true;
  78.                 }
  79.                 HWID_INUSE = true;
  80.             }
  81.             if (!aTerminal && (!sNodeid && !sHWid)) {
  82.                 if (DEBUG_LEVEL>0) { Console.WriteLine("When not in terminal mode a nodeid or a HWid must be supplied, I quit"); }
  83.                 error = true;
  84.             }
  85.            
  86.             //check host and port arguments
  87.             host = CommandLine["h"];
  88.             string sPort = CommandLine["p"];
  89.             if ((host == null) || (sPort.Length == 0)) {
  90.                 if (DEBUG_LEVEL>0) { printSyntax(); }
  91.                 error = true;
  92.             } else {
  93.                 try {
  94.                     port = int.Parse(sPort);
  95.                 } catch {
  96.                     if (DEBUG_LEVEL>0) { Console.WriteLine("Was not able to parse port, I quit"); }
  97.                     error = true;
  98.                 }
  99.             }
  100.         }
  101.        
  102.         if  (!error) {
  103.             //commandline feedback output
  104.             if (DEBUG_LEVEL>2) {
  105.                 Console.WriteLine("canDude settings:");
  106.                 if (sNodeid) { Console.WriteLine("Nodeaddress: 0x" + String.Format("{0:x2}", nodeid)); }
  107.                 if (sHWid) { Console.WriteLine("HW id: 0x" + String.Format("{0:x8}", HWid)); }
  108.                 Console.WriteLine("Host: {0}:{1}", host, port);
  109.                 if (hexfile != null) { Console.WriteLine("Hexfile: {0}", hexfile); }
  110.                 Console.Write("Parameters: ");
  111.                 if (aBios) { Console.Write("Bios "); }
  112.                 if (aReset) { Console.Write("Reset "); }
  113.                 if (aStart) { Console.Write("Start "); }
  114.                 if (aTerminal) { Console.Write("Terminal"); }
  115.                 Console.WriteLine("");
  116.             }
  117.         }
  118.        
  119.         if (!error) {
  120.             //connect to candaemon
  121.             dc = new DaemonConnection();
  122.             if (dc.init(host, port)) {
  123.                 if (DEBUG_LEVEL>1) { Console.WriteLine("Connected to candaemon"); }
  124.             } else {
  125.                 if (DEBUG_LEVEL>0) { Console.WriteLine("Connection to candaemon could not be established, I quit"); }
  126.                 error = true;
  127.             }
  128.             Thread.Sleep(100);
  129.         }
  130.            
  131.         if (!error && aTerminal) {
  132.             //terminal mode (==interactive mode)
  133.             bool success = true;
  134.             //load hexfile if one has been specified as commandline argument
  135.             hf = new HexFile();
  136.             if (sHexfile && success) {
  137.                 if (hf.loadHex(hexfile)) {
  138.                     if (DEBUG_LEVEL>1) { Console.WriteLine("Hexfile loaded"); }
  139.                 } else { success = false; }
  140.             }
  141.            
  142.             if (success) {
  143.                 if (DEBUG_LEVEL>1) {
  144.                     Console.WriteLine("");
  145.                     printHelp();        //print available commands
  146.                     Console.WriteLine("");
  147.                 }
  148.                
  149.                 string instr;
  150.                 do {
  151.                     Console.Write("> ");            //a command has been executed, print a new console char
  152.                     instr = Console.ReadLine();     //read std in
  153.                 } while (parseInput(instr));        //parse a line from std in
  154.                
  155.                 //exit command
  156.             }
  157.         }
  158.  
  159.         if (!error && !aTerminal) {
  160.             //not terminal mode
  161.             bool success = true;
  162.            
  163.             if (sHexfile) {
  164.                 //load hexfile (a hexfile must be supplied as commandline argument)
  165.                 hf = new HexFile();
  166.                 if (hf.loadHex(hexfile)) {
  167.                     if (DEBUG_LEVEL>1) { Console.WriteLine("Hexfile loaded"); }
  168.                 } else {
  169.                     success = false;
  170.                     if (DEBUG_LEVEL>0) { Console.WriteLine("Hexfile could not be loaded, I quit"); }
  171.                 }
  172.             }
  173.            
  174.             //if a hexfile is loaded then start autodownloading sequence
  175.             if (success) {
  176.                 bool autosuccess = true;
  177.                
  178.                 CanNMT cpn = new CanNMT(HWID_INUSE);
  179.                 if (aReset) {
  180.                     //send a reset command (and wait for feedback)
  181.                    autosuccess = cpn.doReset(dc, nodeid, HWid);
  182.  
  183.                     if (!autosuccess) {
  184.                         if (DEBUG_LEVEL>0) { Console.WriteLine("Target node did not respond to reset, I quit"); }
  185.                     }
  186.                 }
  187.                
  188.                 if (sHexfile && autosuccess) {
  189.                     //send application
  190.                     dl = new Downloader(hf, dc, nodeid, aBios, HWID_INUSE, HWid);
  191.                     if (!dl.go()) {
  192.                         if (DEBUG_LEVEL>0) { Console.WriteLine("Error occured during download"); }
  193.                         autosuccess = false;
  194.                     }
  195.                 }
  196.                
  197.                 if (autosuccess && aStart) {
  198.                     //start application
  199.                     cpn.doStart(dc, nodeid, HWid);
  200.                 }
  201.                
  202.             }
  203.         }
  204.         if (dc != null) {
  205.             //stop tcp client thread
  206.             dc.stop();
  207.         }
  208.     }
  209.    
  210.     static private void printSyntax() {
  211.         Console.WriteLine("Syntax: program.exe [options] -h <host> -p <port>");
  212.         Console.WriteLine("Host and port are host and port of canDaemon, most likely localhost and 1200.");
  213.         Console.WriteLine("Options:");
  214.         Console.WriteLine("  -f <hexfile>   Specify a file to be loaded to target.");
  215.         Console.WriteLine("  -n <nodeid>    Id of target node, hex or decimal.");
  216.         Console.WriteLine("  -t             Enter terminal mode.");
  217.         Console.WriteLine("  -r             Reset node before loading target.");
  218.         Console.WriteLine("  -s             Start node after loading target.");
  219.         Console.WriteLine("  -b             Program hex as bios, use with caution.");
  220.     }
  221.    
  222.     static private void printHelp() {
  223.         Console.WriteLine("canDude commands:");
  224.         Console.WriteLine("load [<hexfile>] - reload hexfile or load new hexfile");
  225.         Console.WriteLine("node <nodeid>    - specify id of target node, hex or decimal");
  226.         Console.WriteLine("reset            - reset node");
  227.         Console.WriteLine("start            - start application in node (no feedback yet)");
  228.         Console.WriteLine("go               - start download application to target");
  229.         Console.WriteLine("go bios          - start download bios to target, use with caution");
  230.         Console.WriteLine("help             - prints this help");
  231.         Console.WriteLine("exit or quit     - exit program");
  232.     }  
  233.    
  234.     static private bool parseNodeId(string node) {
  235.         // the goal here is to find a byte from 0-255 in a string, format in decimal (0-255) or in hex (0x0-0xff)
  236.         // this is probably not done very neat
  237.         if (node.Length > 2) {
  238.             if (node.Substring(node.Length-2, 2).Equals("UL")) {
  239.                node = node.Substring(0, node.Length-2);
  240.             }
  241.         }
  242.        
  243.         sNodeid = false;
  244.         if (node.Length > 0) {
  245.             try {
  246.                 nodeid = byte.Parse(node);
  247.                 sNodeid = true;
  248.             } catch {}
  249.         }
  250.         if (node.Length > 2) {
  251.             if (node.Substring(0, 2).Equals("0x")) {
  252.                 try {
  253.                     nodeid = byte.Parse(node.Substring(2, node.Length-2), System.Globalization.NumberStyles.HexNumber);
  254.                     sNodeid = true;
  255.                 } catch {
  256.                     if (DEBUG_LEVEL>0) { Console.WriteLine("nodeadress parse error 1"); }
  257.                     return false;
  258.                 }
  259.             }
  260.         }
  261.        
  262.         return true;
  263.     }
  264.    
  265.     static private bool parseHWId(string node) {
  266.         // the goal here is to find a uint in a string, format in decimal (0-4294967296) or in hex (0x0-0xffffffff)
  267.         // this is probably not done very neat
  268.  
  269.         if (node.Length > 2) {
  270.             if (node.Substring(node.Length-2, 2).Equals("UL")) {
  271.                node = node.Substring(0, node.Length-2);
  272.             }
  273.         }
  274.  
  275.         sHWid = false;
  276.         if (node.Length > 0) {
  277.             try {
  278.                 HWid = uint.Parse(node);
  279.                 sHWid = true;
  280.             } catch {}
  281.         }
  282.         if (node.Length > 2) {
  283.             if (node.Substring(0, 2).Equals("0x")) {
  284.                 try {
  285.                     HWid = uint.Parse(node.Substring(2, node.Length-2), System.Globalization.NumberStyles.HexNumber);
  286.                     sHWid = true;
  287.                 } catch {
  288.                     if (DEBUG_LEVEL>0) { Console.WriteLine("HW id parse error 1"); }
  289.                     return false;
  290.                 }
  291.             }
  292.         }
  293.        
  294.         return true;
  295.     }
  296.    
  297.     static private bool parseInput(string instr) {
  298.         //parse commands entered on std in
  299.         if (instr.Equals("reset")) {        //reset command, send a reset to current node
  300.             if (sNodeid) {
  301.                 dc.flushData();
  302.                 CanNMT cpn = new CanNMT(HWID_INUSE);
  303.                 cpn.doReset(dc, nodeid, HWid);
  304.             } else {
  305.                 if (DEBUG_LEVEL>0) { Console.WriteLine("No nodeid has been specified"); }
  306.             }
  307.         }
  308.         else if (instr.Equals("start")) {   //start command, send a start to current node
  309.             if (sNodeid) {
  310.                 CanNMT cpn = new CanNMT(HWID_INUSE);
  311.                 cpn.doStart(dc, nodeid, HWid);
  312.             } else {
  313.                 if (DEBUG_LEVEL>0) { Console.WriteLine("No nodeid has been specified"); }
  314.             }
  315.         }
  316.         else if (instr.StartsWith("go")) {  //downloading command
  317.             bool dlBios = false;
  318.             bool error = true;
  319.             if (instr.Equals("go bios")) {  //if bios should be downloaded
  320.                 dlBios = true;
  321.                 error = false;
  322.             } else if (instr.Equals("go")) {    //is an application should be downloaded
  323.                 error = false;
  324.             }
  325.            
  326.             if (!sNodeid) {
  327.                 if (DEBUG_LEVEL>0) { Console.WriteLine("No nodeid has been specified"); }
  328.                 error = true;
  329.             }
  330.             if (!sHexfile) {
  331.                 if (DEBUG_LEVEL>0) { Console.WriteLine("No hexfile has been specified"); }
  332.                 error = true;
  333.             }
  334.            
  335.             CanNMT cpn = new CanNMT(HWID_INUSE);
  336.             if (!error && aReset) {             //commandline arguments specified that a reset should be done before download
  337.                 //send a reset command (and wait for feedback)
  338.                 if (!cpn.doReset(dc, nodeid, HWid)) {
  339.                     if (DEBUG_LEVEL>0) { Console.WriteLine("Target node did not respond to reset"); }
  340.                     error = true;
  341.                 }
  342.             }
  343.                
  344.             if (!error) {
  345.                 //send application
  346.                 dc.flushData();
  347.                 dl = new Downloader(hf, dc, nodeid, dlBios, HWID_INUSE, HWid);
  348.                 if (!dl.go()) {
  349.                     if (DEBUG_LEVEL>0) { Console.WriteLine("Error occured during download"); }
  350.                     error = true;
  351.                 }
  352.             }
  353.                
  354.             if (!error && aStart) {             //commandline arguments specified that a start should be done after download
  355.                 //start applikation
  356.                 cpn.doStart(dc, nodeid, HWid);
  357.             }
  358.            
  359.         }
  360.         else if (instr.StartsWith("load")) {    //load hexfile or reload current hexfile
  361.             if (instr.Length > 5) {
  362.                 //second argument is a hexfile, this file should be loaded
  363.                 hexfile = instr.Substring(5);
  364.                 if (hf.loadHex(hexfile)) {
  365.                     sHexfile = true;
  366.                     if (DEBUG_LEVEL>1) { Console.WriteLine("Hexfile loaded"); }
  367.                 } else {
  368.                     if (DEBUG_LEVEL>0) { Console.WriteLine("Hexfile could not be loaded"); }
  369.                 }
  370.             } else {
  371.                 //reload current hexfile. if a hexfile already has been specified
  372.                 if (sHexfile) {
  373.                     if (hf.loadHex(hexfile)) {
  374.                         if (DEBUG_LEVEL>1) { Console.WriteLine("Hexfile reloaded"); }
  375.                     } else {
  376.                         if (DEBUG_LEVEL>0) { Console.WriteLine("Hexfile could not be reloaded"); }
  377.                     }
  378.                 } else {
  379.                     if (DEBUG_LEVEL>0) { Console.WriteLine("No hexfile has been specified"); }
  380.                 }
  381.                
  382.             }
  383.         }
  384.         else if (instr.StartsWith("node")) {
  385.             //change nodeid
  386.             if (instr.Length > 5) {
  387.                 //second argument is nodeid
  388.                 string node = instr.Substring(5);
  389.                 parseNodeId(node);
  390.             }
  391.             if (DEBUG_LEVEL>1) { Console.WriteLine("NodeId is 0x"+ String.Format("{0:x2}", nodeid)); }
  392.         }
  393.         else if (instr.Equals("help")) {
  394.             printHelp();
  395.         }
  396.         else if (instr.Equals("exit") || instr.Equals("quit") || instr.Equals("q")) {
  397.             return false;
  398.         }
  399.         else {
  400.             Console.WriteLine("Unknown command.");
  401.         }
  402.        
  403.         return true;
  404.     }
  405.    
  406. }
  407.