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