Subversion Repositories HomeAutomation

Rev

Rev 1527 | Rev 1717 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

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