Subversion Repositories HomeAutomation

Rev

Rev 459 | Rev 466 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

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