Subversion Repositories HomeAutomation

Rev

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