Subversion Repositories HomeAutomation

Rev

Rev 770 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 770 Rev 800
1
using System.Threading;
1
using System.Threading;
2
using System;
2
using System;
3
 
3
 
4
public class CanNMT {
4
public class CanNMT {
5
    private const byte CAN_NMT              = 0x00;
5
    private const byte CAN_NMT              = 0x00;
6
   
6
   
7
    private const byte CAN_NMT_RESET        = 0x04;
7
    private const byte CAN_NMT_RESET        = 0x04;
8
    private const byte CAN_NMT_BIOS_START   = 0x08;
8
    private const byte CAN_NMT_BIOS_START   = 0x08;
9
    private const byte CAN_NMT_PGM_START    = 0x0C;
9
    private const byte CAN_NMT_PGM_START    = 0x0C;
10
    private const byte CAN_NMT_PGM_DATA     = 0x10;
10
    private const byte CAN_NMT_PGM_DATA     = 0x10;
11
    private const byte CAN_NMT_PGM_END      = 0x14;
11
    private const byte CAN_NMT_PGM_END      = 0x14;
12
    private const byte CAN_NMT_PGM_COPY     = 0x18;
12
    private const byte CAN_NMT_PGM_COPY     = 0x18;
13
    private const byte CAN_NMT_PGM_ACK      = 0x1C;
13
    private const byte CAN_NMT_PGM_ACK      = 0x1C;
14
    private const byte CAN_NMT_PGM_NACK     = 0x20;
14
    private const byte CAN_NMT_PGM_NACK     = 0x20;
15
    private const byte CAN_NMT_START_APP    = 0x24;
15
    private const byte CAN_NMT_START_APP    = 0x24;
16
    private const byte CAN_NMT_APP_START    = 0x28;
16
    private const byte CAN_NMT_APP_START    = 0x28;
17
    private const byte CAN_NMT_HEARTBEAT    = 0x2C;
17
    private const byte CAN_NMT_HEARTBEAT    = 0x2C;
18
 
18
 
19
    private bool HWID_INUSE=false;
19
    private bool HWID_INUSE=false;
20
 
20
 
21
    CanPacket cp;
21
    CanPacket cp;
22
    private bool running = false;
22
    private bool running = false;
23
   
23
   
24
    public CanNMT(bool HWID_INUSE) {
24
    public CanNMT(bool HWID_INUSE) {
25
        cp = new CanPacket();
25
        cp = new CanPacket();
26
        cp.setExt(1);
26
        cp.setExt(1);
27
        this.HWID_INUSE = HWID_INUSE;
27
        this.HWID_INUSE = HWID_INUSE;
28
    }
28
    }
29
 
29
 
30
    public CanNMT(CanPacket cp, bool HWID_INUSE) {
30
    public CanNMT(CanPacket cp, bool HWID_INUSE) {
31
        this.cp = cp;
31
        this.cp = cp;
32
        this.cp.setExt(1);
32
        this.cp.setExt(1);
33
        this.HWID_INUSE = HWID_INUSE;
33
        this.HWID_INUSE = HWID_INUSE;
34
    }
34
    }
35
   
35
   
36
    public byte getSender() {
36
    public byte getSender() {
37
        return (byte)((cp.getId()>>8)&0xFF);
37
        return (byte)((cp.getId()>>8)&0xFF);
38
    }
38
    }
39
   
39
   
40
    public bool isNMTPacket() {
40
    public bool isNMTPacket() {
41
        if (((cp.getId()>>24)&0xFF) == 0) {
41
        if (((cp.getId()>>24)&0xFF) == 0) {
42
            return true;
42
            return true;
43
        }
43
        }
44
        return false;
44
        return false;
45
    }
45
    }
46
 
46
 
47
    public byte getType() {
47
    public byte getType() {
48
        return (byte)((cp.getId()>>16)&0xFF);
48
        return (byte)((cp.getId()>>16)&0xFF);
49
    }
49
    }
50
   
50
   
51
    public byte getReceiver() {
51
    public byte getReceiver() {
52
        return (byte)((cp.getId()>>0)&0xFF);
52
        return (byte)((cp.getId()>>0)&0xFF);
53
    }
53
    }
54
   
54
   
55
    public void setSender(byte id) {
55
    public void setSender(byte id) {
56
        cp.setId( ( ( cp.getId()&0x00FF00FF )|((uint)id<<8) ) );
56
        cp.setId( ( ( cp.getId()&0x00FF00FF )|((uint)id<<8) ) );
57
    }
57
    }
58
    public void setReceiver(byte id) {
58
    public void setReceiver(byte id) {
59
        cp.setId( ( ( cp.getId()&0x00FFFF00 )|((uint)id) ) );
59
        cp.setId( ( ( cp.getId()&0x00FFFF00 )|((uint)id) ) );
60
    }
60
    }
61
   
61
   
62
    public CanPacket getResetPacket() {
62
    public CanPacket getResetPacket() {
63
        cp.setId( ( cp.getId()&0x0000FFFF )|( CAN_NMT_RESET<<16 ) );
63
        cp.setId( ( cp.getId()&0x0000FFFF )|( CAN_NMT_RESET<<16 ) );
64
        if (HWID_INUSE) {
64
        if (HWID_INUSE) {
65
           cp.setDataLength(4);
65
           cp.setDataLength(4);
66
        } else {
66
        } else {
67
           cp.setDataLength(0);
67
           cp.setDataLength(0);
68
        }
68
        }
69
        return cp;
69
        return cp;
70
    }
70
    }
71
 
71
 
72
    public CanPacket getStartPacket() {
72
    public CanPacket getStartPacket() {
73
        cp.setId( ( cp.getId()&0x0000FFFF )|( CAN_NMT_START_APP<<16 ) );
73
        cp.setId( ( cp.getId()&0x0000FFFF )|( CAN_NMT_START_APP<<16 ) );
74
        if (HWID_INUSE) {
74
        if (HWID_INUSE) {
75
           cp.setDataLength(4);
75
           cp.setDataLength(4);
76
        } else {
76
        } else {
77
           cp.setDataLength(0);
77
           cp.setDataLength(0);
78
        }
78
        }
79
        return cp;
79
        return cp;
80
    }
80
    }
81
 
81
 
82
    public CanPacket getPgmStartPacket(byte[] data) {
82
    public CanPacket getPgmStartPacket(byte[] data) {
83
        cp.setId( ( cp.getId()&0x0000FFFF )|( CAN_NMT_PGM_START<<16 ) );
83
        cp.setId( ( cp.getId()&0x0000FFFF )|( CAN_NMT_PGM_START<<16 ) );
84
        cp.setData(data);
84
        cp.setData(data);
85
        if (HWID_INUSE) {
85
        if (HWID_INUSE) {
86
           cp.setDataLength(8);
86
           cp.setDataLength(8);
87
        } else {
87
        } else {
88
           cp.setDataLength(4);
88
           cp.setDataLength(4);
89
        }
89
        }
90
        return cp;
90
        return cp;
91
    }
91
    }
92
 
92
 
93
    public CanPacket getPgmDataPacket(byte[] data, byte datalen) {
93
    public CanPacket getPgmDataPacket(byte[] data, byte datalen) {
94
        cp.setId( ( cp.getId()&0x0000FFFF )|( CAN_NMT_PGM_DATA<<16 ) );
94
        cp.setId( ( cp.getId()&0x0000FFFF )|( CAN_NMT_PGM_DATA<<16 ) );
95
        cp.setData(data);
95
        cp.setData(data);
96
        cp.setDataLength(datalen);
96
        cp.setDataLength(datalen);
97
        return cp;
97
        return cp;
98
    }
98
    }
99
 
99
 
100
    public CanPacket getPgmEndPacket() {
100
    public CanPacket getPgmEndPacket() {
101
        cp.setId( ( cp.getId()&0x0000FFFF )|( CAN_NMT_PGM_END<<16 ) );
101
        cp.setId( ( cp.getId()&0x0000FFFF )|( CAN_NMT_PGM_END<<16 ) );
102
        cp.setDataLength(0);
102
        cp.setDataLength(0);
103
        return cp;
103
        return cp;
104
    }
104
    }
105
 
105
 
106
    public CanPacket getPgmCopyPacket(byte[] data) {
106
    public CanPacket getPgmCopyPacket(byte[] data) {
107
        cp.setId( ( cp.getId()&0x0000FFFF )|( CAN_NMT_PGM_COPY<<16 ) );
107
        cp.setId( ( cp.getId()&0x0000FFFF )|( CAN_NMT_PGM_COPY<<16 ) );
108
        cp.setData(data);
108
        cp.setData(data);
109
        cp.setDataLength(6);
109
        cp.setDataLength(6);
110
        return cp;
110
        return cp;
111
    }
111
    }
112
   
112
   
113
    public bool isBiosStart(CanPacket cp, byte sender, uint hwid) {
113
    public bool isBiosStart(CanPacket cp, byte sender, uint hwid) {
114
        bool returnval = false;
114
        bool returnval = false;
115
        uint id = cp.getId();
115
        uint id = cp.getId();
116
        if (HWID_INUSE) {
116
        if (HWID_INUSE) {
117
            byte[] data = cp.getData();
117
            byte[] data = cp.getData();
118
            if ((id&0x00FF0000) == (CAN_NMT_BIOS_START<<16) && data[4]==((hwid)&0xff) && data[5]==((hwid>>8)&0xff) && data[6]==((hwid>>16)&0xff) && data[7]==((hwid>>24)&0xff)) {
118
            if ((id&0x00FF0000) == (CAN_NMT_BIOS_START<<16) && data[4]==((hwid)&0xff) && data[5]==((hwid>>8)&0xff) && data[6]==((hwid>>16)&0xff) && data[7]==((hwid>>24)&0xff)) {
119
                returnval = true;
119
                returnval = true;
120
                string appinfo = "Application installed";
120
                string appinfo = "Application installed";
121
                if (data[2] == 0) {
121
                if (data[2] == 0) {
122
                    appinfo = "No application installed";
122
                    appinfo = "No application installed";
123
                }
123
                }
124
                if (data[3]!=0 && data[3]!=0xff) {
124
                if (data[3]!=0 && data[3]!=0xff) {
125
                   appinfo += ", application can-id "+data[3];
125
                   appinfo += ", application can-id "+data[3];
126
                }
126
                }
127
                Console.WriteLine("Bios started on node 0x" + String.Format("{0:x2}", sender) +
127
                Console.WriteLine("Bios started on node 0x" + String.Format("{0:x2}", sender) +
128
                                    ", bios version 0x" + String.Format("{0:x2}", data[1]) + String.Format("{0:x2}", data[0]) +
128
                                    ", bios version 0x" + String.Format("{0:x2}", data[1]) + String.Format("{0:x2}", data[0]) +
129
                                    ", " + appinfo
129
                                    ", " + appinfo
130
                                    );
130
                                    );
131
            }
131
            }
132
        } else {
132
        } else {
133
            if ((id&0x00FF0000) == (CAN_NMT_BIOS_START<<16) && (id&0x0000FF00) == (sender<<8)) {
133
            if ((id&0x00FF0000) == (CAN_NMT_BIOS_START<<16) && (id&0x0000FF00) == (sender<<8)) {
134
                returnval = true;
134
                returnval = true;
135
                byte[] data = cp.getData();
135
                byte[] data = cp.getData();
136
                string appinfo = "Application installed";
136
                string appinfo = "Application installed";
137
                if (data[2] == 0) {
137
                if (data[2] == 0) {
138
                    appinfo = "No application installed";
138
                    appinfo = "No application installed";
139
                }
139
                }
140
                Console.WriteLine("Bios started on node 0x" + String.Format("{0:x2}", sender) +
140
                Console.WriteLine("Bios started on node 0x" + String.Format("{0:x2}", sender) +
141
                                    ", bios version 0x" + String.Format("{0:x2}", data[1]) + String.Format("{0:x2}", data[0]) +
141
                                    ", bios version 0x" + String.Format("{0:x2}", data[1]) + String.Format("{0:x2}", data[0]) +
142
                                    ", " + appinfo
142
                                    ", " + appinfo
143
                                    );
143
                                    );
144
            }
144
            }
145
        }
145
        }
146
        return returnval;
146
        return returnval;
147
    }
147
    }
148
 
148
 
149
    public bool doStart(DaemonConnection dc, byte receiver, uint hwid) {
149
    public bool doStart(DaemonConnection dc, byte receiver, uint hwid) {
150
        return doStart(dc, receiver, 0, hwid);
150
        return doStart(dc, receiver, 0, hwid);
151
    }
151
    }
152
   
152
   
153
    public bool doStart(DaemonConnection dc, byte receiver, byte sender, uint hwid) {
153
    public bool doStart(DaemonConnection dc, byte receiver, byte sender, uint hwid) {
154
        bool success = true;
154
        bool success = true;
155
 
155
 
156
          if (HWID_INUSE) {
156
          if (HWID_INUSE) {
157
           byte[] data = new byte[8];
157
           byte[] data = new byte[8];
158
           data[0] = (byte)(hwid&0xff); data[1] = (byte)((hwid>>8)&0xff); data[2] = (byte)((hwid>>16)&0xff); data[3] = (byte)((hwid>>24)&0xff);  
158
           data[0] = (byte)(hwid&0xff); data[1] = (byte)((hwid>>8)&0xff); data[2] = (byte)((hwid>>16)&0xff); data[3] = (byte)((hwid>>24)&0xff);  
159
           cp.setData(data);
159
           cp.setData(data);
160
           cp.setDataLength(4);
160
           cp.setDataLength(4);
161
          }    
161
          }    
162
        setReceiver(receiver);
162
        setReceiver(receiver);
163
        dc.sendCanPacket(getStartPacket());
163
        dc.sendCanPacket(getStartPacket());
164
        return success;
164
        return success;
165
    }
165
    }
166
 
166
 
167
    public bool doReset(DaemonConnection dc, byte receiver) {
167
    public bool doReset(DaemonConnection dc, byte receiver) {
168
        return doReset(dc, receiver, 0);
168
        return doReset(dc, receiver, 0);
169
    }
169
    }
170
 
170
 
171
    public bool doReset(DaemonConnection dc, byte receiver, uint hwid) {
171
    public bool doReset(DaemonConnection dc, byte receiver, uint hwid) {
172
        return doReset(dc, receiver, 0, hwid);
172
        return doReset(dc, receiver, 0, hwid);
173
    }
173
    }
174
 
174
 
175
    public bool doReset(DaemonConnection dc, byte receiver, byte sender, uint hwid) {
175
    public bool doReset(DaemonConnection dc, byte receiver, byte sender, uint hwid) {
176
        bool success = false;
176
        bool success = false;
177
 
177
 
178
      if (HWID_INUSE) {
178
      if (HWID_INUSE) {
179
       byte[] data = new byte[8];
179
       byte[] data = new byte[8];
180
       data[0] = (byte)(hwid&0xff); data[1] = (byte)((hwid>>8)&0xff); data[2] = (byte)((hwid>>16)&0xff); data[3] = (byte)((hwid>>24)&0xff);  
180
       data[0] = (byte)(hwid&0xff); data[1] = (byte)((hwid>>8)&0xff); data[2] = (byte)((hwid>>16)&0xff); data[3] = (byte)((hwid>>24)&0xff);  
181
       cp.setData(data);
181
       cp.setData(data);
182
       cp.setDataLength(4);
182
       cp.setDataLength(4);
183
      } else {
183
      } else {
184
        setReceiver(receiver);
184
        setReceiver(receiver);
185
      }      
185
      }      
186
       
186
       
187
        dc.sendCanPacket(getResetPacket());
187
        dc.sendCanPacket(getResetPacket());
188
        CanPacket getcp;
188
        CanPacket getcp;
189
        running = true;
189
        running = true;
190
        dc.flushData();
190
        dc.flushData();
191
        for (int i = 0; i < 150; i++) {
191
        for (int i = 0; i < 150; i++) {
192
            Thread.Sleep(50);
192
            Thread.Sleep(50);
193
            if (dc.getData(out getcp)) {
193
            if (dc.getData(out getcp)) {
194
                if (isBiosStart(getcp, receiver, hwid)) {
194
                if (isBiosStart(getcp, receiver, hwid)) {
195
                    success = true;
195
                    success = true;
196
                    break;
196
                    break;
197
                }
197
                }
198
            }
198
            }
199
           
199
           
200
            if (!running) {
200
            if (!running) {
201
                break;
201
                break;
202
            }
202
            }
203
        }
203
        }
204
        if (!success) {
204
        if (!success) {
205
            Console.WriteLine("Timed out while waiting for node to reset");
205
            Console.WriteLine("Timed out while waiting for node to reset");
206
        }
206
        }
207
        return success;
207
        return success;
208
    }
208
    }
209
   
209
   
210
    public void stop() {
210
    public void stop() {
211
        running = false;
211
        running = false;
212
    }
212
    }
213
   
213
   
214
}
214
}
215
 
215