Subversion Repositories HomeAutomation

Rev

Rev 758 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 758 Rev 759
Line 7... Line 7...
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
   
13
   
13
    //connects to candaemon on tcp to send and receive packets
14
    //connects to candaemon on tcp to send and receive packets
14
    static DaemonConnection dc;
15
    static DaemonConnection dc;
15
    //a commandline arguments parser
16
    //a commandline arguments parser
16
    static Arguments CommandLine;
17
    static Arguments CommandLine;
Line 23... Line 24...
23
    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
24
    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
25
    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
26
       
27
       
27
    static bool sNodeid=false;      //true when a nodeid has been parsed
28
    static bool sNodeid=false;      //true when a nodeid has been parsed
-
 
29
    static bool sHWid=false;        //true when a hardware id has been parsed
28
    static bool sHexfile=false;     //true when a hexfile has been specified
30
    static bool sHexfile=false;     //true when a hexfile has been specified
29
       
31
       
30
    static string hexfile=null;     //path to a hexfile
32
    static string hexfile=null;     //path to a hexfile
31
    static byte nodeid=0;           //id of current node
33
    static byte nodeid=0;           //id of current node
-
 
34
    static uint HWid=0;             //id of current node
32
    static string host=null;        //address of candaemon-host
35
    static string host=null;        //address of candaemon-host
33
    static int port=1200;           //port of candaemon-host
36
    static int port=1200;           //port of candaemon-host
34
   
37
   
35
    static void Main(string[] args) {
38
    static void Main(string[] args) {
36
        bool error = false;
39
        bool error = false;
Line 65... Line 68...
65
            if (node != null) {
68
            if (node != null) {
66
                if (!parseNodeId(node)) {
69
                if (!parseNodeId(node)) {
67
                    error = true;
70
                    error = true;
68
                }
71
                }
69
            }
72
            }
-
 
73
            //check hwid argument
-
 
74
            string hwid = CommandLine["i"];
-
 
75
            if (hwid != null) {
-
 
76
                if (!parseHWId(hwid)) {
-
 
77
                    error = true;
-
 
78
                }
-
 
79
                HWID_INUSE = true;
-
 
80
            }
70
            if (!aTerminal && !sNodeid) {
81
            if (!aTerminal && (!sNodeid && !sHWid)) {
71
                if (DEBUG_LEVEL>0) { Console.WriteLine("When not in terminal mode a nodeid must be supplied, I quit"); }
82
                if (DEBUG_LEVEL>0) { Console.WriteLine("When not in terminal mode a nodeid or a HWid must be supplied, I quit"); }
72
                error = true;
83
                error = true;
73
            }
84
            }
74
           
85
           
75
            //check host and port arguments
86
            //check host and port arguments
76
            host = CommandLine["h"];
87
            host = CommandLine["h"];
Line 91... Line 102...
91
        if  (!error) {
102
        if  (!error) {
92
            //commandline feedback output
103
            //commandline feedback output
93
            if (DEBUG_LEVEL>2) {
104
            if (DEBUG_LEVEL>2) {
94
                Console.WriteLine("canDude settings:");
105
                Console.WriteLine("canDude settings:");
95
                if (sNodeid) { Console.WriteLine("Nodeaddress: 0x" + String.Format("{0:x2}", nodeid)); }
106
                if (sNodeid) { Console.WriteLine("Nodeaddress: 0x" + String.Format("{0:x2}", nodeid)); }
-
 
107
                if (sHWid) { Console.WriteLine("HW id: 0x" + String.Format("{0:x8}", HWid)); }
96
                Console.WriteLine("Host: {0}:{1}", host, port);
108
                Console.WriteLine("Host: {0}:{1}", host, port);
97
                if (hexfile != null) { Console.WriteLine("Hexfile: {0}", hexfile); }
109
                if (hexfile != null) { Console.WriteLine("Hexfile: {0}", hexfile); }
98
                Console.Write("Parameters: ");
110
                Console.Write("Parameters: ");
99
                if (aBios) { Console.Write("Bios "); }
111
                if (aBios) { Console.Write("Bios "); }
100
                if (aReset) { Console.Write("Reset "); }
112
                if (aReset) { Console.Write("Reset "); }
Line 155... Line 167...
155
                    if (DEBUG_LEVEL>1) { Console.WriteLine("Hexfile loaded"); }
167
                    if (DEBUG_LEVEL>1) { Console.WriteLine("Hexfile loaded"); }
156
                } else {
168
                } else {
157
                    success = false;
169
                    success = false;
158
                    if (DEBUG_LEVEL>0) { Console.WriteLine("Hexfile could not be loaded, I quit"); }
170
                    if (DEBUG_LEVEL>0) { Console.WriteLine("Hexfile could not be loaded, I quit"); }
159
                }
171
                }
160
            }
172
            }
161
           
173
           
162
            //if a hexfile is loaded then start autodownloading sequence
174
            //if a hexfile is loaded then start autodownloading sequence
163
            if (success) {
175
            if (success) {
164
                bool autosuccess = true;
176
                bool autosuccess = true;
165
               
177
               
166
                CanNMT cpn = new CanNMT();
178
                CanNMT cpn = new CanNMT(HWID_INUSE);
167
                if (aReset) {
179
                if (aReset) {
168
                    //send a reset command (and wait for feedback)
180
                    //send a reset command (and wait for feedback)
169
                    if (!cpn.doReset(dc, nodeid)) {
181
                   autosuccess = cpn.doReset(dc, nodeid, HWid);
-
 
182
 
-
 
183
                    if (!autosuccess) {
170
                        if (DEBUG_LEVEL>0) { Console.WriteLine("Target node did not respond to reset, I quit"); }
184
                        if (DEBUG_LEVEL>0) { Console.WriteLine("Target node did not respond to reset, I quit"); }
171
                        autosuccess = false;
-
 
172
                    }
185
                    }
173
                }
186
                }
174
               
187
               
175
                if (sHexfile && autosuccess) {
188
                if (sHexfile && autosuccess) {
176
                    //send application
189
                    //send application
177
                    dl = new Downloader(hf, dc, nodeid, aBios);
190
                    dl = new Downloader(hf, dc, nodeid, aBios, HWID_INUSE, HWid);
178
                    if (!dl.go()) {
191
                    if (!dl.go()) {
179
                        if (DEBUG_LEVEL>0) { Console.WriteLine("Error occured during download"); }
192
                        if (DEBUG_LEVEL>0) { Console.WriteLine("Error occured during download"); }
180
                        autosuccess = false;
193
                        autosuccess = false;
181
                    }
194
                    }
182
                }
195
                }
183
               
196
               
184
                if (autosuccess && aStart) {
197
                if (autosuccess && aStart) {
185
                    //start application
198
                    //start application
186
                    cpn.doStart(dc, nodeid);
199
                    cpn.doStart(dc, nodeid, HWid);
187
                }
200
                }
188
               
201
               
189
            }
202
            }
190
        }
203
        }
191
        if (dc != null) {
204
        if (dc != null) {
Line 223... Line 236...
223
        // this is probably not done very neat
236
        // this is probably not done very neat
224
        sNodeid = false;
237
        sNodeid = false;
225
        if (node.Length > 0) {
238
        if (node.Length > 0) {
226
            try {
239
            try {
227
                nodeid = byte.Parse(node);
240
                nodeid = byte.Parse(node);
228
                sNodeid = true;
241
                sNodeid = true;
229
            } catch {}
242
            } catch {}
230
        }
243
        }
231
        if (node.Length > 2) {
244
        if (node.Length > 2) {
232
            if (node.Substring(0, 2).Equals("0x")) {
245
            if (node.Substring(0, 2).Equals("0x")) {
233
                if (node.Length == 3) {
-
 
234
                    try {
246
                try {
235
                        nodeid = byte.Parse(node.Substring(2, 1), System.Globalization.NumberStyles.HexNumber);
247
                    nodeid = byte.Parse(node.Substring(2, node.Length-2), System.Globalization.NumberStyles.HexNumber);
236
                        sNodeid = true;
248
                    sNodeid = true;
237
                    } catch {
249
                } catch {
238
                        if (DEBUG_LEVEL>0) { Console.WriteLine("nodeadress parse error 1"); }
250
                    if (DEBUG_LEVEL>0) { Console.WriteLine("nodeadress parse error 1"); }
239
                        return false;
251
                    return false;
240
                    }
252
                }
-
 
253
            }
-
 
254
        }
-
 
255
       
-
 
256
        return true;
-
 
257
    }
-
 
258
   
-
 
259
    static private bool parseHWId(string node) {
-
 
260
        // the goal here is to find a uint in a string, format in decimal (0-4294967296) or in hex (0x0-0xffffffff)
-
 
261
        // this is probably not done very neat
-
 
262
        sHWid = false;
241
                } else if (node.Length == 4) {
263
        if (node.Length > 0) {
242
                    try {
264
            try {
243
                        nodeid = byte.Parse(node.Substring(2, 2), System.Globalization.NumberStyles.HexNumber);
265
                HWid = uint.Parse(node);
244
                        sNodeid = true;
266
                sHWid = true;
245
                    } catch {
267
            } catch {}
246
                        if (DEBUG_LEVEL>0) { Console.WriteLine("nodeadress parse error 2"); }
-
 
247
                        return false;
-
 
248
                    }
268
        }
249
                } else if (node.Length > 4) {
269
        if (node.Length > 2) {
-
 
270
            if (node.Substring(0, 2).Equals("0x")) {
250
                    try {
271
                try {
251
                        nodeid = byte.Parse(node.Substring(2, 2), System.Globalization.NumberStyles.HexNumber);
272
                    HWid = uint.Parse(node.Substring(2, node.Length-2), System.Globalization.NumberStyles.HexNumber);
252
                        sNodeid = true;
273
                    sHWid = true;
253
                    } catch {
274
                } catch {
254
                        if (DEBUG_LEVEL>0) { Console.WriteLine("nodeadress parse error 2"); }
-
 
255
                        return false;
-
 
256
                    }
-
 
257
                } else {
-
 
258
                    if (DEBUG_LEVEL>0) { Console.WriteLine("nodeadress parse error 3"); }
275
                    if (DEBUG_LEVEL>0) { Console.WriteLine("HW id parse error 1"); }
259
                    return false;
276
                    return false;
260
                }
277
                }
261
            }
278
            }
262
        }
279
        }
263
       
280
       
Line 267... Line 284...
267
    static private bool parseInput(string instr) {
284
    static private bool parseInput(string instr) {
268
        //parse commands entered on std in
285
        //parse commands entered on std in
269
        if (instr.Equals("reset")) {        //reset command, send a reset to current node
286
        if (instr.Equals("reset")) {        //reset command, send a reset to current node
270
            if (sNodeid) {
287
            if (sNodeid) {
271
                dc.flushData();
288
                dc.flushData();
272
                CanNMT cpn = new CanNMT();
289
                CanNMT cpn = new CanNMT(HWID_INUSE);
273
                cpn.doReset(dc, nodeid);
290
                cpn.doReset(dc, nodeid, HWid);
274
            } else {
291
            } else {
275
                if (DEBUG_LEVEL>0) { Console.WriteLine("No nodeid has been specified"); }
292
                if (DEBUG_LEVEL>0) { Console.WriteLine("No nodeid has been specified"); }
276
            }
293
            }
277
        }
294
        }
278
        else if (instr.Equals("start")) {   //start command, send a start to current node
295
        else if (instr.Equals("start")) {   //start command, send a start to current node
279
            if (sNodeid) {
296
            if (sNodeid) {
280
                CanNMT cpn = new CanNMT();
297
                CanNMT cpn = new CanNMT(HWID_INUSE);
281
                cpn.doStart(dc, nodeid);
298
                cpn.doStart(dc, nodeid, HWid);
282
            } else {
299
            } else {
283
                if (DEBUG_LEVEL>0) { Console.WriteLine("No nodeid has been specified"); }
300
                if (DEBUG_LEVEL>0) { Console.WriteLine("No nodeid has been specified"); }
284
            }
301
            }
285
        }
302
        }
286
        else if (instr.StartsWith("go")) {  //downloading command
303
        else if (instr.StartsWith("go")) {  //downloading command
Line 300... Line 317...
300
            if (!sHexfile) {
317
            if (!sHexfile) {
301
                if (DEBUG_LEVEL>0) { Console.WriteLine("No hexfile has been specified"); }
318
                if (DEBUG_LEVEL>0) { Console.WriteLine("No hexfile has been specified"); }
302
                error = true;
319
                error = true;
303
            }
320
            }
304
           
321
           
305
            CanNMT cpn = new CanNMT();
322
            CanNMT cpn = new CanNMT(HWID_INUSE);
306
            if (!error && aReset) {             //commandline arguments specified that a reset should be done before download
323
            if (!error && aReset) {             //commandline arguments specified that a reset should be done before download
307
                //send a reset command (and wait for feedback)
324
                //send a reset command (and wait for feedback)
308
                if (!cpn.doReset(dc, nodeid)) {
325
                if (!cpn.doReset(dc, nodeid, HWid)) {
309
                    if (DEBUG_LEVEL>0) { Console.WriteLine("Target node did not respond to reset"); }
326
                    if (DEBUG_LEVEL>0) { Console.WriteLine("Target node did not respond to reset"); }
310
                    error = true;
327
                    error = true;
311
                }
328
                }
312
            }
329
            }
313
               
330
               
314
            if (!error) {
331
            if (!error) {
315
                //send application
332
                //send application
316
                dc.flushData();
333
                dc.flushData();
317
                dl = new Downloader(hf, dc, nodeid, dlBios);
334
                dl = new Downloader(hf, dc, nodeid, dlBios, HWID_INUSE, HWid);
318
                if (!dl.go()) {
335
                if (!dl.go()) {
319
                    if (DEBUG_LEVEL>0) { Console.WriteLine("Error occured during download"); }
336
                    if (DEBUG_LEVEL>0) { Console.WriteLine("Error occured during download"); }
320
                    error = true;
337
                    error = true;
321
                }
338
                }
322
            }
339
            }
323
               
340
               
324
            if (!error && aStart) {             //commandline arguments specified that a start should be done after download
341
            if (!error && aStart) {             //commandline arguments specified that a start should be done after download
325
                //start applikation
342
                //start applikation
326
                cpn.doStart(dc, nodeid);
343
                cpn.doStart(dc, nodeid, HWid);
327
            }
344
            }
328
           
345
           
329
        }
346
        }
330
        else if (instr.StartsWith("load")) {    //load hexfile or reload current hexfile
347
        else if (instr.StartsWith("load")) {    //load hexfile or reload current hexfile
331
            if (instr.Length > 5) {
348
            if (instr.Length > 5) {