Rev 655 | Rev 782 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 455 | arune | 1 | using System; |
| 2 | using System.Threading; |
||
| 3 | using System.Collections.Generic; |
||
| 4 | using System.Text; |
||
| 5 | |||
| 473 | arune | 6 | ///TODO: |
| 7 | // argument -v # (debuglevel) |
||
| 8 | // argument till konstruktor på alla objekt: debuglevel |
||
| 9 | |||
| 455 | arune | 10 | class Program { |
| 11 | const byte DEBUG_LEVEL = 3; //0, 1, 2, 3 |
||
| 770 | arune | 12 | static bool HWID_INUSE = false; |
| 459 | arune | 13 | |
| 14 | //connects to candaemon on tcp to send and receive packets |
||
| 455 | arune | 15 | static DaemonConnection dc; |
| 459 | arune | 16 | //a commandline arguments parser |
| 455 | arune | 17 | static Arguments CommandLine; |
| 459 | arune | 18 | //functions for downloading hex to target |
| 455 | arune | 19 | static Downloader dl; |
| 459 | arune | 20 | //maintains a hexfile |
| 21 | private static HexFile hf; |
||
| 455 | arune | 22 | |
| 459 | arune | 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 |
||
| 455 | arune | 27 | |
| 462 | arune | 28 | static bool sNodeid=false; //true when a nodeid has been parsed |
| 770 | arune | 29 | static bool sHWid=false; //true when a hardware id has been parsed |
| 462 | arune | 30 | static bool sHexfile=false; //true when a hexfile has been specified |
| 455 | arune | 31 | |
| 462 | arune | 32 | static string hexfile=null; //path to a hexfile |
| 33 | static byte nodeid=0; //id of current node |
||
| 770 | arune | 34 | static uint HWid=0; //id of current node |
| 462 | arune | 35 | static string host=null; //address of candaemon-host |
| 36 | static int port=1200; //port of candaemon-host |
||
| 455 | arune | 37 | |
| 38 | static void Main(string[] args) { |
||
| 39 | bool error = false; |
||
| 40 | if (args.Length < 4) { |
||
| 462 | arune | 41 | //if to few arguments then print info and quit |
| 42 | printSyntax(); |
||
| 455 | arune | 43 | error = true; |
| 44 | } |
||
| 45 | |||
| 46 | if (!error) { |
||
| 462 | arune | 47 | //parse commandline arguments |
| 455 | arune | 48 | CommandLine=new Arguments(args); |
| 49 | |||
| 462 | arune | 50 | //check single arguments |
| 455 | arune | 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; } |
||
| 462 | arune | 55 | |
| 56 | //check hexfile argument |
||
| 455 | arune | 57 | hexfile = CommandLine["f"]; |
| 58 | if (hexfile != null) { |
||
| 59 | sHexfile = true; |
||
| 60 | } |
||
| 473 | arune | 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"); } |
||
| 455 | arune | 63 | error = true; |
| 64 | } |
||
| 65 | |||
| 462 | arune | 66 | //check nodeid argument |
| 455 | arune | 67 | string node = CommandLine["n"]; |
| 456 | arune | 68 | if (node != null) { |
| 69 | if (!parseNodeId(node)) { |
||
| 70 | error = true; |
||
| 71 | } |
||
| 455 | arune | 72 | } |
| 770 | arune | 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"); } |
||
| 455 | arune | 83 | error = true; |
| 84 | } |
||
| 85 | |||
| 462 | arune | 86 | //check host and port arguments |
| 455 | arune | 87 | host = CommandLine["h"]; |
| 88 | string sPort = CommandLine["p"]; |
||
| 89 | if ((host == null) || (sPort.Length == 0)) { |
||
| 462 | arune | 90 | if (DEBUG_LEVEL>0) { printSyntax(); } |
| 455 | arune | 91 | error = true; |
| 92 | } else { |
||
| 93 | try { |
||
| 94 | port = int.Parse(sPort); |
||
| 95 | } catch { |
||
| 462 | arune | 96 | if (DEBUG_LEVEL>0) { Console.WriteLine("Was not able to parse port, I quit"); } |
| 455 | arune | 97 | error = true; |
| 98 | } |
||
| 99 | } |
||
| 100 | } |
||
| 101 | |||
| 102 | if (!error) { |
||
| 462 | arune | 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)); } |
||
| 770 | arune | 107 | if (sHWid) { Console.WriteLine("HW id: 0x" + String.Format("{0:x8}", HWid)); } |
| 462 | arune | 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 | } |
||
| 455 | arune | 117 | } |
| 118 | |||
| 462 | arune | 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 | } |
||
| 586 | arune | 128 | Thread.Sleep(100); |
| 459 | arune | 129 | } |
| 130 | |||
| 455 | arune | 131 | if (!error && aTerminal) { |
| 462 | arune | 132 | //terminal mode (==interactive mode) |
| 455 | arune | 133 | bool success = true; |
| 462 | arune | 134 | //load hexfile if one has been specified as commandline argument |
| 455 | arune | 135 | hf = new HexFile(); |
| 136 | if (sHexfile && success) { |
||
| 137 | if (hf.loadHex(hexfile)) { |
||
| 462 | arune | 138 | if (DEBUG_LEVEL>1) { Console.WriteLine("Hexfile loaded"); } |
| 455 | arune | 139 | } else { success = false; } |
| 140 | } |
||
| 141 | |||
| 142 | if (success) { |
||
| 462 | arune | 143 | if (DEBUG_LEVEL>1) { |
| 455 | arune | 144 | Console.WriteLine(""); |
| 462 | arune | 145 | printHelp(); //print available commands |
| 455 | arune | 146 | Console.WriteLine(""); |
| 147 | } |
||
| 148 | |||
| 149 | string instr; |
||
| 150 | do { |
||
| 462 | arune | 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 |
||
| 455 | arune | 154 | |
| 155 | //exit command |
||
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | if (!error && !aTerminal) { |
||
| 160 | //not terminal mode |
||
| 161 | bool success = true; |
||
| 162 | |||
| 473 | arune | 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 | } |
||
| 455 | arune | 172 | } |
| 173 | |||
| 462 | arune | 174 | //if a hexfile is loaded then start autodownloading sequence |
| 455 | arune | 175 | if (success) { |
| 176 | bool autosuccess = true; |
||
| 177 | |||
| 770 | arune | 178 | CanNMT cpn = new CanNMT(HWID_INUSE); |
| 455 | arune | 179 | if (aReset) { |
| 180 | //send a reset command (and wait for feedback) |
||
| 770 | arune | 181 | autosuccess = cpn.doReset(dc, nodeid, HWid); |
| 182 | |||
| 183 | if (!autosuccess) { |
||
| 462 | arune | 184 | if (DEBUG_LEVEL>0) { Console.WriteLine("Target node did not respond to reset, I quit"); } |
| 455 | arune | 185 | } |
| 186 | } |
||
| 187 | |||
| 473 | arune | 188 | if (sHexfile && autosuccess) { |
| 455 | arune | 189 | //send application |
| 770 | arune | 190 | dl = new Downloader(hf, dc, nodeid, aBios, HWID_INUSE, HWid); |
| 459 | arune | 191 | if (!dl.go()) { |
| 462 | arune | 192 | if (DEBUG_LEVEL>0) { Console.WriteLine("Error occured during download"); } |
| 459 | arune | 193 | autosuccess = false; |
| 194 | } |
||
| 455 | arune | 195 | } |
| 196 | |||
| 197 | if (autosuccess && aStart) { |
||
| 462 | arune | 198 | //start application |
| 770 | arune | 199 | cpn.doStart(dc, nodeid, HWid); |
| 455 | arune | 200 | } |
| 201 | |||
| 202 | } |
||
| 203 | } |
||
| 462 | arune | 204 | if (dc != null) { |
| 205 | //stop tcp client thread |
||
| 206 | dc.stop(); |
||
| 207 | } |
||
| 455 | arune | 208 | } |
| 209 | |||
| 462 | arune | 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 | |||
| 458 | arune | 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 | |||
| 455 | arune | 234 | static private bool parseNodeId(string node) { |
| 462 | arune | 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 |
||
| 455 | arune | 237 | sNodeid = false; |
| 238 | if (node.Length > 0) { |
||
| 239 | try { |
||
| 240 | nodeid = byte.Parse(node); |
||
| 241 | sNodeid = true; |
||
| 242 | } catch {} |
||
| 243 | } |
||
| 244 | if (node.Length > 2) { |
||
| 245 | if (node.Substring(0, 2).Equals("0x")) { |
||
| 770 | arune | 246 | try { |
| 247 | nodeid = byte.Parse(node.Substring(2, node.Length-2), System.Globalization.NumberStyles.HexNumber); |
||
| 248 | sNodeid = true; |
||
| 249 | } catch { |
||
| 250 | if (DEBUG_LEVEL>0) { Console.WriteLine("nodeadress parse error 1"); } |
||
| 455 | arune | 251 | return false; |
| 252 | } |
||
| 253 | } |
||
| 254 | } |
||
| 255 | |||
| 256 | return true; |
||
| 257 | } |
||
| 258 | |||
| 770 | arune | 259 | static private bool parseHWId(string node) { |
| 260 | // the goal here is to find a uint in a string, format in decimal (0-4294967296) or in hex (0x0-0xffffffff) |
||
| 261 | // this is probably not done very neat |
||
| 262 | sHWid = false; |
||
| 263 | if (node.Length > 0) { |
||
| 264 | try { |
||
| 265 | HWid = uint.Parse(node); |
||
| 266 | sHWid = true; |
||
| 267 | } catch {} |
||
| 268 | } |
||
| 269 | if (node.Length > 2) { |
||
| 270 | if (node.Substring(0, 2).Equals("0x")) { |
||
| 271 | try { |
||
| 272 | HWid = uint.Parse(node.Substring(2, node.Length-2), System.Globalization.NumberStyles.HexNumber); |
||
| 273 | sHWid = true; |
||
| 274 | } catch { |
||
| 275 | if (DEBUG_LEVEL>0) { Console.WriteLine("HW id parse error 1"); } |
||
| 276 | return false; |
||
| 277 | } |
||
| 278 | } |
||
| 279 | } |
||
| 280 | |||
| 281 | return true; |
||
| 282 | } |
||
| 283 | |||
| 455 | arune | 284 | static private bool parseInput(string instr) { |
| 462 | arune | 285 | //parse commands entered on std in |
| 286 | if (instr.Equals("reset")) { //reset command, send a reset to current node |
||
| 455 | arune | 287 | if (sNodeid) { |
| 466 | arune | 288 | dc.flushData(); |
| 770 | arune | 289 | CanNMT cpn = new CanNMT(HWID_INUSE); |
| 290 | cpn.doReset(dc, nodeid, HWid); |
||
| 455 | arune | 291 | } else { |
| 462 | arune | 292 | if (DEBUG_LEVEL>0) { Console.WriteLine("No nodeid has been specified"); } |
| 455 | arune | 293 | } |
| 294 | } |
||
| 462 | arune | 295 | else if (instr.Equals("start")) { //start command, send a start to current node |
| 455 | arune | 296 | if (sNodeid) { |
| 770 | arune | 297 | CanNMT cpn = new CanNMT(HWID_INUSE); |
| 298 | cpn.doStart(dc, nodeid, HWid); |
||
| 455 | arune | 299 | } else { |
| 462 | arune | 300 | if (DEBUG_LEVEL>0) { Console.WriteLine("No nodeid has been specified"); } |
| 455 | arune | 301 | } |
| 302 | } |
||
| 462 | arune | 303 | else if (instr.StartsWith("go")) { //downloading command |
| 455 | arune | 304 | bool dlBios = false; |
| 305 | bool error = true; |
||
| 462 | arune | 306 | if (instr.Equals("go bios")) { //if bios should be downloaded |
| 455 | arune | 307 | dlBios = true; |
| 308 | error = false; |
||
| 462 | arune | 309 | } else if (instr.Equals("go")) { //is an application should be downloaded |
| 455 | arune | 310 | error = false; |
| 311 | } |
||
| 312 | |||
| 313 | if (!sNodeid) { |
||
| 462 | arune | 314 | if (DEBUG_LEVEL>0) { Console.WriteLine("No nodeid has been specified"); } |
| 455 | arune | 315 | error = true; |
| 316 | } |
||
| 317 | if (!sHexfile) { |
||
| 462 | arune | 318 | if (DEBUG_LEVEL>0) { Console.WriteLine("No hexfile has been specified"); } |
| 455 | arune | 319 | error = true; |
| 320 | } |
||
| 321 | |||
| 770 | arune | 322 | CanNMT cpn = new CanNMT(HWID_INUSE); |
| 462 | arune | 323 | if (!error && aReset) { //commandline arguments specified that a reset should be done before download |
| 455 | arune | 324 | //send a reset command (and wait for feedback) |
| 770 | arune | 325 | if (!cpn.doReset(dc, nodeid, HWid)) { |
| 462 | arune | 326 | if (DEBUG_LEVEL>0) { Console.WriteLine("Target node did not respond to reset"); } |
| 455 | arune | 327 | error = true; |
| 328 | } |
||
| 329 | } |
||
| 330 | |||
| 331 | if (!error) { |
||
| 332 | //send application |
||
| 466 | arune | 333 | dc.flushData(); |
| 770 | arune | 334 | dl = new Downloader(hf, dc, nodeid, dlBios, HWID_INUSE, HWid); |
| 455 | arune | 335 | if (!dl.go()) { |
| 462 | arune | 336 | if (DEBUG_LEVEL>0) { Console.WriteLine("Error occured during download"); } |
| 455 | arune | 337 | error = true; |
| 338 | } |
||
| 339 | } |
||
| 340 | |||
| 462 | arune | 341 | if (!error && aStart) { //commandline arguments specified that a start should be done after download |
| 455 | arune | 342 | //start applikation |
| 770 | arune | 343 | cpn.doStart(dc, nodeid, HWid); |
| 455 | arune | 344 | } |
| 345 | |||
| 346 | } |
||
| 462 | arune | 347 | else if (instr.StartsWith("load")) { //load hexfile or reload current hexfile |
| 455 | arune | 348 | if (instr.Length > 5) { |
| 349 | //second argument is a hexfile, this file should be loaded |
||
| 350 | hexfile = instr.Substring(5); |
||
| 351 | if (hf.loadHex(hexfile)) { |
||
| 352 | sHexfile = true; |
||
| 462 | arune | 353 | if (DEBUG_LEVEL>1) { Console.WriteLine("Hexfile loaded"); } |
| 455 | arune | 354 | } else { |
| 462 | arune | 355 | if (DEBUG_LEVEL>0) { Console.WriteLine("Hexfile could not be loaded"); } |
| 455 | arune | 356 | } |
| 357 | } else { |
||
| 358 | //reload current hexfile. if a hexfile already has been specified |
||
| 359 | if (sHexfile) { |
||
| 360 | if (hf.loadHex(hexfile)) { |
||
| 462 | arune | 361 | if (DEBUG_LEVEL>1) { Console.WriteLine("Hexfile reloaded"); } |
| 455 | arune | 362 | } else { |
| 462 | arune | 363 | if (DEBUG_LEVEL>0) { Console.WriteLine("Hexfile could not be reloaded"); } |
| 455 | arune | 364 | } |
| 365 | } else { |
||
| 462 | arune | 366 | if (DEBUG_LEVEL>0) { Console.WriteLine("No hexfile has been specified"); } |
| 455 | arune | 367 | } |
| 368 | |||
| 369 | } |
||
| 370 | } |
||
| 371 | else if (instr.StartsWith("node")) { |
||
| 372 | //change nodeid |
||
| 373 | if (instr.Length > 5) { |
||
| 374 | //second argument is nodeid |
||
| 375 | string node = instr.Substring(5); |
||
| 376 | parseNodeId(node); |
||
| 377 | } |
||
| 462 | arune | 378 | if (DEBUG_LEVEL>1) { Console.WriteLine("NodeId is 0x"+ String.Format("{0:x2}", nodeid)); } |
| 455 | arune | 379 | } |
| 458 | arune | 380 | else if (instr.Equals("help")) { |
| 381 | printHelp(); |
||
| 382 | } |
||
| 462 | arune | 383 | else if (instr.Equals("exit") || instr.Equals("quit") || instr.Equals("q")) { |
| 455 | arune | 384 | return false; |
| 385 | } |
||
| 386 | else { |
||
| 387 | Console.WriteLine("Unknown command."); |
||
| 388 | } |
||
| 389 | |||
| 390 | return true; |
||
| 391 | } |
||
| 392 | |||
| 393 | } |