Subversion Repositories HomeAutomation

Rev

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

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