Subversion Repositories HomeAutomation

Rev

Rev 782 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

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