Subversion Repositories HomeAutomation

Rev

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

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