Subversion Repositories HomeAutomation

Rev

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

Rev 458 Rev 459
Line 3... Line 3...
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;
Line 58... Line 53...
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)) {
Line 109... Line 104...
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");
Line 142... Line 138...
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;
Line 185... Line 169...
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.");