Subversion Repositories HomeAutomation

Rev

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

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