Rev 1717 | 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 |
||
| 1701 | arune | 27 | static bool aDefaultBios=false; //if commandline says it is a node with hwid 0xffffffff |
| 455 | arune | 28 | |
| 462 | arune | 29 | static bool sNodeid=false; //true when a nodeid has been parsed |
| 770 | arune | 30 | static bool sHWid=false; //true when a hardware id has been parsed |
| 462 | arune | 31 | static bool sHexfile=false; //true when a hexfile has been specified |
| 455 | arune | 32 | |
| 462 | arune | 33 | static string hexfile=null; //path to a hexfile |
| 34 | static byte nodeid=0; //id of current node |
||
| 770 | arune | 35 | static uint HWid=0; //id of current node |
| 462 | arune | 36 | static string host=null; //address of candaemon-host |
| 37 | static int port=1200; //port of candaemon-host |
||
| 455 | arune | 38 | |
| 39 | static void Main(string[] args) { |
||
| 40 | bool error = false; |
||
| 41 | if (args.Length < 4) { |
||
| 462 | arune | 42 | //if to few arguments then print info and quit |
| 43 | printSyntax(); |
||
| 455 | arune | 44 | error = true; |
| 45 | } |
||
| 46 | |||
| 47 | if (!error) { |
||
| 462 | arune | 48 | //parse commandline arguments |
| 455 | arune | 49 | CommandLine=new Arguments(args); |
| 50 | |||
| 462 | arune | 51 | //check single arguments |
| 455 | arune | 52 | if (CommandLine["b"] == "true") { aBios = true; } |
| 1718 | arune | 53 | if (CommandLine["d"] == "true") { aDefaultBios = true; aBios = true; } |
| 455 | arune | 54 | if (CommandLine["r"] == "true") { aReset = true; } |
| 55 | if (CommandLine["s"] == "true") { aStart = true; } |
||
| 56 | if (CommandLine["t"] == "true") { aTerminal = true; } |
||
| 462 | arune | 57 | |
| 58 | //check hexfile argument |
||
| 455 | arune | 59 | hexfile = CommandLine["f"]; |
| 60 | if (hexfile != null) { |
||
| 61 | sHexfile = true; |
||
| 62 | } |
||
| 473 | arune | 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"); } |
||
| 455 | arune | 65 | error = true; |
| 66 | } |
||
| 67 | |||
| 462 | arune | 68 | //check nodeid argument |
| 455 | arune | 69 | string node = CommandLine["n"]; |
| 456 | arune | 70 | if (node != null) { |
| 71 | if (!parseNodeId(node)) { |
||
| 72 | error = true; |
||
| 73 | } |
||
| 455 | arune | 74 | } |
| 770 | arune | 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"); } |
||
| 455 | arune | 85 | error = true; |
| 86 | } |
||
| 87 | |||
| 462 | arune | 88 | //check host and port arguments |
| 455 | arune | 89 | host = CommandLine["h"]; |
| 90 | string sPort = CommandLine["p"]; |
||
| 91 | if ((host == null) || (sPort.Length == 0)) { |
||
| 462 | arune | 92 | if (DEBUG_LEVEL>0) { printSyntax(); } |
| 455 | arune | 93 | error = true; |
| 94 | } else { |
||
| 95 | try { |
||
| 96 | port = int.Parse(sPort); |
||
| 97 | } catch { |
||
| 462 | arune | 98 | if (DEBUG_LEVEL>0) { Console.WriteLine("Was not able to parse port, I quit"); } |
| 455 | arune | 99 | error = true; |
| 100 | } |
||
| 101 | } |
||
| 102 | } |
||
| 103 | |||
| 104 | if (!error) { |
||
| 462 | arune | 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)); } |
||
| 770 | arune | 109 | if (sHWid) { Console.WriteLine("HW id: 0x" + String.Format("{0:x8}", HWid)); } |
| 462 | arune | 110 | Console.WriteLine("Host: {0}:{1}", host, port); |
| 111 | if (hexfile != null) { Console.WriteLine("Hexfile: {0}", hexfile); } |
||
| 112 | Console.Write("Parameters: "); |
||
| 1701 | arune | 113 | if (aDefaultBios) { Console.Write("Default "); } |
| 462 | arune | 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 | } |
||
| 455 | arune | 120 | } |
| 121 | |||
| 462 | arune | 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 | } |
||
| 586 | arune | 131 | Thread.Sleep(100); |
| 459 | arune | 132 | } |
| 133 | |||
| 455 | arune | 134 | if (!error && aTerminal) { |
| 462 | arune | 135 | //terminal mode (==interactive mode) |
| 455 | arune | 136 | bool success = true; |
| 462 | arune | 137 | //load hexfile if one has been specified as commandline argument |
| 455 | arune | 138 | hf = new HexFile(); |
| 139 | if (sHexfile && success) { |
||
| 140 | if (hf.loadHex(hexfile)) { |
||
| 462 | arune | 141 | if (DEBUG_LEVEL>1) { Console.WriteLine("Hexfile loaded"); } |
| 455 | arune | 142 | } else { success = false; } |
| 143 | } |
||
| 144 | |||
| 145 | if (success) { |
||
| 462 | arune | 146 | if (DEBUG_LEVEL>1) { |
| 455 | arune | 147 | Console.WriteLine(""); |
| 462 | arune | 148 | printHelp(); //print available commands |
| 455 | arune | 149 | Console.WriteLine(""); |
| 150 | } |
||
| 151 | |||
| 152 | string instr; |
||
| 153 | do { |
||
| 462 | arune | 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 |
||
| 455 | arune | 157 | |
| 158 | //exit command |
||
| 159 | } |
||
| 160 | } |
||
| 161 | |||
| 162 | if (!error && !aTerminal) { |
||
| 163 | //not terminal mode |
||
| 164 | bool success = true; |
||
| 165 | |||
| 473 | arune | 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 | } |
||
| 455 | arune | 175 | } |
| 176 | |||
| 462 | arune | 177 | //if a hexfile is loaded then start autodownloading sequence |
| 455 | arune | 178 | if (success) { |
| 179 | bool autosuccess = true; |
||
| 180 | |||
| 770 | arune | 181 | CanNMT cpn = new CanNMT(HWID_INUSE); |
| 455 | arune | 182 | if (aReset) { |
| 183 | //send a reset command (and wait for feedback) |
||
| 1701 | arune | 184 | if (!aDefaultBios) { |
| 185 | autosuccess = cpn.doReset(dc, nodeid, HWid); |
||
| 186 | } else { |
||
| 187 | autosuccess = cpn.doReset(dc, nodeid, 0xffffffff); |
||
| 188 | } |
||
| 189 | |||
| 770 | arune | 190 | |
| 191 | if (!autosuccess) { |
||
| 462 | arune | 192 | if (DEBUG_LEVEL>0) { Console.WriteLine("Target node did not respond to reset, I quit"); } |
| 455 | arune | 193 | } |
| 194 | } |
||
| 195 | |||
| 473 | arune | 196 | if (sHexfile && autosuccess) { |
| 455 | arune | 197 | //send application |
| 1717 | arune | 198 | dl = new Downloader(hf, dc, nodeid, aBios, HWID_INUSE, HWid, aDefaultBios); |
| 459 | arune | 199 | if (!dl.go()) { |
| 462 | arune | 200 | if (DEBUG_LEVEL>0) { Console.WriteLine("Error occured during download"); } |
| 459 | arune | 201 | autosuccess = false; |
| 202 | } |
||
| 455 | arune | 203 | } |
| 204 | |||
| 205 | if (autosuccess && aStart) { |
||
| 462 | arune | 206 | //start application |
| 770 | arune | 207 | cpn.doStart(dc, nodeid, HWid); |
| 455 | arune | 208 | } |
| 209 | |||
| 210 | } |
||
| 211 | } |
||
| 462 | arune | 212 | if (dc != null) { |
| 213 | //stop tcp client thread |
||
| 214 | dc.stop(); |
||
| 215 | } |
||
| 455 | arune | 216 | } |
| 217 | |||
| 462 | arune | 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."); |
||
| 1527 | arune | 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."); |
||
| 462 | arune | 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."); |
||
| 1701 | arune | 229 | Console.WriteLine(" -d Program hex as bios, use with caution. Initially uses hwid 0xffffffff."); |
| 462 | arune | 230 | } |
| 231 | |||
| 458 | arune | 232 | static private void printHelp() { |
| 233 | Console.WriteLine("canDude commands:"); |
||
| 234 | Console.WriteLine("load [<hexfile>] - reload hexfile or load new hexfile"); |
||
| 1527 | arune | 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"); |
||
| 458 | arune | 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 | |||
| 455 | arune | 245 | static private bool parseNodeId(string node) { |
| 462 | arune | 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 |
||
| 782 | arune | 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 | |||
| 455 | arune | 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")) { |
||
| 770 | arune | 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"); } |
||
| 455 | arune | 268 | return false; |
| 269 | } |
||
| 270 | } |
||
| 271 | } |
||
| 272 | |||
| 273 | return true; |
||
| 274 | } |
||
| 275 | |||
| 770 | arune | 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 |
||
| 782 | arune | 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 | |||
| 770 | arune | 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 | |||
| 455 | arune | 308 | static private bool parseInput(string instr) { |
| 462 | arune | 309 | //parse commands entered on std in |
| 310 | if (instr.Equals("reset")) { //reset command, send a reset to current node |
||
| 1527 | arune | 311 | if (sHWid) { |
| 466 | arune | 312 | dc.flushData(); |
| 770 | arune | 313 | CanNMT cpn = new CanNMT(HWID_INUSE); |
| 314 | cpn.doReset(dc, nodeid, HWid); |
||
| 455 | arune | 315 | } else { |
| 1527 | arune | 316 | if (DEBUG_LEVEL>0) { Console.WriteLine("No HWid has been specified"); } |
| 455 | arune | 317 | } |
| 318 | } |
||
| 462 | arune | 319 | else if (instr.Equals("start")) { //start command, send a start to current node |
| 1527 | arune | 320 | if (sHWid) { |
| 770 | arune | 321 | CanNMT cpn = new CanNMT(HWID_INUSE); |
| 322 | cpn.doStart(dc, nodeid, HWid); |
||
| 455 | arune | 323 | } else { |
| 1527 | arune | 324 | if (DEBUG_LEVEL>0) { Console.WriteLine("No HWid has been specified"); } |
| 455 | arune | 325 | } |
| 326 | } |
||
| 462 | arune | 327 | else if (instr.StartsWith("go")) { //downloading command |
| 455 | arune | 328 | bool dlBios = false; |
| 329 | bool error = true; |
||
| 462 | arune | 330 | if (instr.Equals("go bios")) { //if bios should be downloaded |
| 455 | arune | 331 | dlBios = true; |
| 332 | error = false; |
||
| 462 | arune | 333 | } else if (instr.Equals("go")) { //is an application should be downloaded |
| 455 | arune | 334 | error = false; |
| 335 | } |
||
| 336 | |||
| 1527 | arune | 337 | if (!sHWid) { |
| 338 | if (DEBUG_LEVEL>0) { Console.WriteLine("No HWid has been specified"); } |
||
| 455 | arune | 339 | error = true; |
| 340 | } |
||
| 341 | if (!sHexfile) { |
||
| 462 | arune | 342 | if (DEBUG_LEVEL>0) { Console.WriteLine("No hexfile has been specified"); } |
| 455 | arune | 343 | error = true; |
| 344 | } |
||
| 345 | |||
| 770 | arune | 346 | CanNMT cpn = new CanNMT(HWID_INUSE); |
| 462 | arune | 347 | if (!error && aReset) { //commandline arguments specified that a reset should be done before download |
| 455 | arune | 348 | //send a reset command (and wait for feedback) |
| 770 | arune | 349 | if (!cpn.doReset(dc, nodeid, HWid)) { |
| 462 | arune | 350 | if (DEBUG_LEVEL>0) { Console.WriteLine("Target node did not respond to reset"); } |
| 455 | arune | 351 | error = true; |
| 352 | } |
||
| 353 | } |
||
| 354 | |||
| 355 | if (!error) { |
||
| 356 | //send application |
||
| 466 | arune | 357 | dc.flushData(); |
| 1717 | arune | 358 | dl = new Downloader(hf, dc, nodeid, dlBios, HWID_INUSE, HWid, aDefaultBios); |
| 455 | arune | 359 | if (!dl.go()) { |
| 462 | arune | 360 | if (DEBUG_LEVEL>0) { Console.WriteLine("Error occured during download"); } |
| 455 | arune | 361 | error = true; |
| 362 | } |
||
| 363 | } |
||
| 364 | |||
| 462 | arune | 365 | if (!error && aStart) { //commandline arguments specified that a start should be done after download |
| 455 | arune | 366 | //start applikation |
| 770 | arune | 367 | cpn.doStart(dc, nodeid, HWid); |
| 455 | arune | 368 | } |
| 369 | |||
| 370 | } |
||
| 462 | arune | 371 | else if (instr.StartsWith("load")) { //load hexfile or reload current hexfile |
| 455 | arune | 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; |
||
| 462 | arune | 377 | if (DEBUG_LEVEL>1) { Console.WriteLine("Hexfile loaded"); } |
| 455 | arune | 378 | } else { |
| 462 | arune | 379 | if (DEBUG_LEVEL>0) { Console.WriteLine("Hexfile could not be loaded"); } |
| 455 | arune | 380 | } |
| 381 | } else { |
||
| 382 | //reload current hexfile. if a hexfile already has been specified |
||
| 383 | if (sHexfile) { |
||
| 384 | if (hf.loadHex(hexfile)) { |
||
| 462 | arune | 385 | if (DEBUG_LEVEL>1) { Console.WriteLine("Hexfile reloaded"); } |
| 455 | arune | 386 | } else { |
| 462 | arune | 387 | if (DEBUG_LEVEL>0) { Console.WriteLine("Hexfile could not be reloaded"); } |
| 455 | arune | 388 | } |
| 389 | } else { |
||
| 462 | arune | 390 | if (DEBUG_LEVEL>0) { Console.WriteLine("No hexfile has been specified"); } |
| 455 | arune | 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 | } |
||
| 462 | arune | 402 | if (DEBUG_LEVEL>1) { Console.WriteLine("NodeId is 0x"+ String.Format("{0:x2}", nodeid)); } |
| 455 | arune | 403 | } |
| 1527 | arune | 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 | } |
||
| 458 | arune | 413 | else if (instr.Equals("help")) { |
| 414 | printHelp(); |
||
| 415 | } |
||
| 462 | arune | 416 | else if (instr.Equals("exit") || instr.Equals("quit") || instr.Equals("q")) { |
| 455 | arune | 417 | return false; |
| 418 | } |
||
| 419 | else { |
||
| 420 | Console.WriteLine("Unknown command."); |
||
| 421 | } |
||
| 422 | |||
| 423 | return true; |
||
| 424 | } |
||
| 425 | |||
| 426 | } |