Rev 782 | Rev 1717 | 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."); |
||
| 1527 | arune | 215 | Console.WriteLine(" -n <nodeid> Id of target node, hex or decimal, depricated."); |
| 216 | Console.WriteLine(" -i <HWid> Id of target node, hex or decimal."); |
||
| 462 | arune | 217 | Console.WriteLine(" -t Enter terminal mode."); |
| 218 | Console.WriteLine(" -r Reset node before loading target."); |
||
| 219 | Console.WriteLine(" -s Start node after loading target."); |
||
| 220 | Console.WriteLine(" -b Program hex as bios, use with caution."); |
||
| 221 | } |
||
| 222 | |||
| 458 | arune | 223 | static private void printHelp() { |
| 224 | Console.WriteLine("canDude commands:"); |
||
| 225 | Console.WriteLine("load [<hexfile>] - reload hexfile or load new hexfile"); |
||
| 1527 | arune | 226 | Console.WriteLine("node <nodeid> - specify id of target node, hex or decimal, depricated"); |
| 227 | Console.WriteLine("hwid <HWid> - specify id of target node, hex or decimal"); |
||
| 458 | arune | 228 | Console.WriteLine("reset - reset node"); |
| 229 | Console.WriteLine("start - start application in node (no feedback yet)"); |
||
| 230 | Console.WriteLine("go - start download application to target"); |
||
| 231 | Console.WriteLine("go bios - start download bios to target, use with caution"); |
||
| 232 | Console.WriteLine("help - prints this help"); |
||
| 233 | Console.WriteLine("exit or quit - exit program"); |
||
| 234 | } |
||
| 235 | |||
| 455 | arune | 236 | static private bool parseNodeId(string node) { |
| 462 | arune | 237 | // the goal here is to find a byte from 0-255 in a string, format in decimal (0-255) or in hex (0x0-0xff) |
| 238 | // this is probably not done very neat |
||
| 782 | arune | 239 | if (node.Length > 2) { |
| 240 | if (node.Substring(node.Length-2, 2).Equals("UL")) { |
||
| 241 | node = node.Substring(0, node.Length-2); |
||
| 242 | } |
||
| 243 | } |
||
| 244 | |||
| 455 | arune | 245 | sNodeid = false; |
| 246 | if (node.Length > 0) { |
||
| 247 | try { |
||
| 248 | nodeid = byte.Parse(node); |
||
| 249 | sNodeid = true; |
||
| 250 | } catch {} |
||
| 251 | } |
||
| 252 | if (node.Length > 2) { |
||
| 253 | if (node.Substring(0, 2).Equals("0x")) { |
||
| 770 | arune | 254 | try { |
| 255 | nodeid = byte.Parse(node.Substring(2, node.Length-2), System.Globalization.NumberStyles.HexNumber); |
||
| 256 | sNodeid = true; |
||
| 257 | } catch { |
||
| 258 | if (DEBUG_LEVEL>0) { Console.WriteLine("nodeadress parse error 1"); } |
||
| 455 | arune | 259 | return false; |
| 260 | } |
||
| 261 | } |
||
| 262 | } |
||
| 263 | |||
| 264 | return true; |
||
| 265 | } |
||
| 266 | |||
| 770 | arune | 267 | static private bool parseHWId(string node) { |
| 268 | // the goal here is to find a uint in a string, format in decimal (0-4294967296) or in hex (0x0-0xffffffff) |
||
| 269 | // this is probably not done very neat |
||
| 782 | arune | 270 | |
| 271 | if (node.Length > 2) { |
||
| 272 | if (node.Substring(node.Length-2, 2).Equals("UL")) { |
||
| 273 | node = node.Substring(0, node.Length-2); |
||
| 274 | } |
||
| 275 | } |
||
| 276 | |||
| 770 | arune | 277 | sHWid = false; |
| 278 | if (node.Length > 0) { |
||
| 279 | try { |
||
| 280 | HWid = uint.Parse(node); |
||
| 281 | sHWid = true; |
||
| 282 | } catch {} |
||
| 283 | } |
||
| 284 | if (node.Length > 2) { |
||
| 285 | if (node.Substring(0, 2).Equals("0x")) { |
||
| 286 | try { |
||
| 287 | HWid = uint.Parse(node.Substring(2, node.Length-2), System.Globalization.NumberStyles.HexNumber); |
||
| 288 | sHWid = true; |
||
| 289 | } catch { |
||
| 290 | if (DEBUG_LEVEL>0) { Console.WriteLine("HW id parse error 1"); } |
||
| 291 | return false; |
||
| 292 | } |
||
| 293 | } |
||
| 294 | } |
||
| 295 | |||
| 296 | return true; |
||
| 297 | } |
||
| 298 | |||
| 455 | arune | 299 | static private bool parseInput(string instr) { |
| 462 | arune | 300 | //parse commands entered on std in |
| 301 | if (instr.Equals("reset")) { //reset command, send a reset to current node |
||
| 1527 | arune | 302 | if (sHWid) { |
| 466 | arune | 303 | dc.flushData(); |
| 770 | arune | 304 | CanNMT cpn = new CanNMT(HWID_INUSE); |
| 305 | cpn.doReset(dc, nodeid, HWid); |
||
| 455 | arune | 306 | } else { |
| 1527 | arune | 307 | if (DEBUG_LEVEL>0) { Console.WriteLine("No HWid has been specified"); } |
| 455 | arune | 308 | } |
| 309 | } |
||
| 462 | arune | 310 | else if (instr.Equals("start")) { //start command, send a start to current node |
| 1527 | arune | 311 | if (sHWid) { |
| 770 | arune | 312 | CanNMT cpn = new CanNMT(HWID_INUSE); |
| 313 | cpn.doStart(dc, nodeid, HWid); |
||
| 455 | arune | 314 | } else { |
| 1527 | arune | 315 | if (DEBUG_LEVEL>0) { Console.WriteLine("No HWid has been specified"); } |
| 455 | arune | 316 | } |
| 317 | } |
||
| 462 | arune | 318 | else if (instr.StartsWith("go")) { //downloading command |
| 455 | arune | 319 | bool dlBios = false; |
| 320 | bool error = true; |
||
| 462 | arune | 321 | if (instr.Equals("go bios")) { //if bios should be downloaded |
| 455 | arune | 322 | dlBios = true; |
| 323 | error = false; |
||
| 462 | arune | 324 | } else if (instr.Equals("go")) { //is an application should be downloaded |
| 455 | arune | 325 | error = false; |
| 326 | } |
||
| 327 | |||
| 1527 | arune | 328 | if (!sHWid) { |
| 329 | if (DEBUG_LEVEL>0) { Console.WriteLine("No HWid has been specified"); } |
||
| 455 | arune | 330 | error = true; |
| 331 | } |
||
| 332 | if (!sHexfile) { |
||
| 462 | arune | 333 | if (DEBUG_LEVEL>0) { Console.WriteLine("No hexfile has been specified"); } |
| 455 | arune | 334 | error = true; |
| 335 | } |
||
| 336 | |||
| 770 | arune | 337 | CanNMT cpn = new CanNMT(HWID_INUSE); |
| 462 | arune | 338 | if (!error && aReset) { //commandline arguments specified that a reset should be done before download |
| 455 | arune | 339 | //send a reset command (and wait for feedback) |
| 770 | arune | 340 | if (!cpn.doReset(dc, nodeid, HWid)) { |
| 462 | arune | 341 | if (DEBUG_LEVEL>0) { Console.WriteLine("Target node did not respond to reset"); } |
| 455 | arune | 342 | error = true; |
| 343 | } |
||
| 344 | } |
||
| 345 | |||
| 346 | if (!error) { |
||
| 347 | //send application |
||
| 466 | arune | 348 | dc.flushData(); |
| 770 | arune | 349 | dl = new Downloader(hf, dc, nodeid, dlBios, HWID_INUSE, HWid); |
| 455 | arune | 350 | if (!dl.go()) { |
| 462 | arune | 351 | if (DEBUG_LEVEL>0) { Console.WriteLine("Error occured during download"); } |
| 455 | arune | 352 | error = true; |
| 353 | } |
||
| 354 | } |
||
| 355 | |||
| 462 | arune | 356 | if (!error && aStart) { //commandline arguments specified that a start should be done after download |
| 455 | arune | 357 | //start applikation |
| 770 | arune | 358 | cpn.doStart(dc, nodeid, HWid); |
| 455 | arune | 359 | } |
| 360 | |||
| 361 | } |
||
| 462 | arune | 362 | else if (instr.StartsWith("load")) { //load hexfile or reload current hexfile |
| 455 | arune | 363 | if (instr.Length > 5) { |
| 364 | //second argument is a hexfile, this file should be loaded |
||
| 365 | hexfile = instr.Substring(5); |
||
| 366 | if (hf.loadHex(hexfile)) { |
||
| 367 | sHexfile = true; |
||
| 462 | arune | 368 | if (DEBUG_LEVEL>1) { Console.WriteLine("Hexfile loaded"); } |
| 455 | arune | 369 | } else { |
| 462 | arune | 370 | if (DEBUG_LEVEL>0) { Console.WriteLine("Hexfile could not be loaded"); } |
| 455 | arune | 371 | } |
| 372 | } else { |
||
| 373 | //reload current hexfile. if a hexfile already has been specified |
||
| 374 | if (sHexfile) { |
||
| 375 | if (hf.loadHex(hexfile)) { |
||
| 462 | arune | 376 | if (DEBUG_LEVEL>1) { Console.WriteLine("Hexfile reloaded"); } |
| 455 | arune | 377 | } else { |
| 462 | arune | 378 | if (DEBUG_LEVEL>0) { Console.WriteLine("Hexfile could not be reloaded"); } |
| 455 | arune | 379 | } |
| 380 | } else { |
||
| 462 | arune | 381 | if (DEBUG_LEVEL>0) { Console.WriteLine("No hexfile has been specified"); } |
| 455 | arune | 382 | } |
| 383 | |||
| 384 | } |
||
| 385 | } |
||
| 386 | else if (instr.StartsWith("node")) { |
||
| 387 | //change nodeid |
||
| 388 | if (instr.Length > 5) { |
||
| 389 | //second argument is nodeid |
||
| 390 | string node = instr.Substring(5); |
||
| 391 | parseNodeId(node); |
||
| 392 | } |
||
| 462 | arune | 393 | if (DEBUG_LEVEL>1) { Console.WriteLine("NodeId is 0x"+ String.Format("{0:x2}", nodeid)); } |
| 455 | arune | 394 | } |
| 1527 | arune | 395 | else if (instr.StartsWith("hwid")) { |
| 396 | //change hwid |
||
| 397 | if (instr.Length > 5) { |
||
| 398 | //second argument is nodeid |
||
| 399 | string node = instr.Substring(5); |
||
| 400 | parseHWId(node); |
||
| 401 | } |
||
| 402 | if (DEBUG_LEVEL>1) { Console.WriteLine("HWid is 0x"+ String.Format("{0:x8}", HWid)); } |
||
| 403 | } |
||
| 458 | arune | 404 | else if (instr.Equals("help")) { |
| 405 | printHelp(); |
||
| 406 | } |
||
| 462 | arune | 407 | else if (instr.Equals("exit") || instr.Equals("quit") || instr.Equals("q")) { |
| 455 | arune | 408 | return false; |
| 409 | } |
||
| 410 | else { |
||
| 411 | Console.WriteLine("Unknown command."); |
||
| 412 | } |
||
| 413 | |||
| 414 | return true; |
||
| 415 | } |
||
| 416 | |||
| 417 | } |