Rev 453 | Rev 1634 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 453 | Rev 770 | ||
|---|---|---|---|
| Line 13... | Line 13... | ||
| 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 | ||
| - | 19 | private bool HWID_INUSE=false; |
|
| 18 | 20 | ||
| 19 | CanPacket cp; |
21 | CanPacket cp; |
| 20 | private bool running = false; |
22 | private bool running = false; |
| 21 | 23 | ||
| 22 | public CanNMT() { |
24 | public CanNMT(bool HWID_INUSE) { |
| 23 | cp = new CanPacket(); |
25 | cp = new CanPacket(); |
| 24 | cp.setExt(1); |
26 | cp.setExt(1); |
| - | 27 | this.HWID_INUSE = HWID_INUSE; |
|
| 25 | } |
28 | } |
| 26 | 29 | ||
| 27 | public CanNMT(CanPacket |
30 | public CanNMT(CanPacket cp, bool HWID_INUSE) { |
| 28 | this.cp = cp; |
31 | this.cp = cp; |
| 29 | this.cp.setExt(1); |
32 | this.cp.setExt(1); |
| - | 33 | this.HWID_INUSE = HWID_INUSE; |
|
| 30 | } |
34 | } |
| 31 | 35 | ||
| 32 | public byte getSender() { |
36 | public byte getSender() { |
| 33 | return (byte)((cp.getId()>>8)&0xFF); |
37 | return (byte)((cp.getId()>>8)&0xFF); |
| 34 | } |
38 | } |
| 35 | 39 | ||
| 36 | public bool isNMTPacket() { |
40 | public bool isNMTPacket() { |
| 37 | if (((cp.getId()>>24)&0xFF) == 0) { |
41 | if (((cp.getId()>>24)&0xFF) == 0) { |
| 38 | return true; |
42 | return true; |
| 39 | } |
43 | } |
| Line 55... | Line 59... | ||
| 55 | cp.setId( ( ( cp.getId()&0x00FFFF00 )|((uint)id) ) ); |
59 | cp.setId( ( ( cp.getId()&0x00FFFF00 )|((uint)id) ) ); |
| 56 | } |
60 | } |
| 57 | 61 | ||
| 58 | public CanPacket getResetPacket() { |
62 | public CanPacket getResetPacket() { |
| 59 | cp.setId( ( cp.getId()&0x0000FFFF )|( CAN_NMT_RESET<<16 ) ); |
63 | cp.setId( ( cp.getId()&0x0000FFFF )|( CAN_NMT_RESET<<16 ) ); |
| - | 64 | if (HWID_INUSE) { |
|
| - | 65 | cp.setDataLength(4); |
|
| - | 66 | } else { |
|
| 60 | cp.setDataLength(0); |
67 | cp.setDataLength(0); |
| - | 68 | } |
|
| 61 | return cp; |
69 | return cp; |
| 62 | } |
70 | } |
| 63 | 71 | ||
| 64 | public CanPacket getStartPacket() { |
72 | public CanPacket getStartPacket() { |
| 65 | 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) { |
|
| - | 75 | cp.setDataLength(4); |
|
| - | 76 | } else { |
|
| 66 | cp.setDataLength(0); |
77 | cp.setDataLength(0); |
| - | 78 | } |
|
| 67 | return cp; |
79 | return cp; |
| 68 | } |
80 | } |
| 69 | 81 | ||
| 70 | public CanPacket getPgmStartPacket(byte[] data) { |
82 | public CanPacket getPgmStartPacket(byte[] data) { |
| 71 | cp.setId( ( cp.getId()&0x0000FFFF )|( CAN_NMT_PGM_START<<16 ) ); |
83 | cp.setId( ( cp.getId()&0x0000FFFF )|( CAN_NMT_PGM_START<<16 ) ); |
| 72 | cp.setData(data); |
84 | cp.setData(data); |
| - | 85 | if (HWID_INUSE) { |
|
| - | 86 | cp.setDataLength(8); |
|
| - | 87 | } else { |
|
| 73 | cp.setDataLength(4); |
88 | cp.setDataLength(4); |
| - | 89 | } |
|
| 74 | return cp; |
90 | return cp; |
| 75 | } |
91 | } |
| 76 | 92 | ||
| 77 | public CanPacket getPgmDataPacket(byte[] data, byte datalen) { |
93 | public CanPacket getPgmDataPacket(byte[] data, byte datalen) { |
| 78 | cp.setId( ( cp.getId()&0x0000FFFF )|( CAN_NMT_PGM_DATA<<16 ) ); |
94 | cp.setId( ( cp.getId()&0x0000FFFF )|( CAN_NMT_PGM_DATA<<16 ) ); |
| 79 | cp.setData(data); |
95 | cp.setData(data); |
| 80 | cp.setDataLength(datalen); |
96 | cp.setDataLength(datalen); |
| Line 82... | Line 98... | ||
| 82 | } |
98 | } |
| 83 | 99 | ||
| 84 | public CanPacket getPgmEndPacket() { |
100 | public CanPacket getPgmEndPacket() { |
| 85 | cp.setId( ( cp.getId()&0x0000FFFF )|( CAN_NMT_PGM_END<<16 ) ); |
101 | cp.setId( ( cp.getId()&0x0000FFFF )|( CAN_NMT_PGM_END<<16 ) ); |
| 86 | cp.setDataLength(0); |
102 | cp.setDataLength(0); |
| 87 | return cp; |
103 | return cp; |
| 88 | } |
104 | } |
| 89 | 105 | ||
| 90 | public CanPacket getPgmCopyPacket(byte[] data) { |
106 | public CanPacket getPgmCopyPacket(byte[] data) { |
| 91 | cp.setId( ( cp.getId()&0x0000FFFF )|( CAN_NMT_PGM_COPY<<16 ) ); |
107 | cp.setId( ( cp.getId()&0x0000FFFF )|( CAN_NMT_PGM_COPY<<16 ) ); |
| 92 | cp.setData(data); |
108 | cp.setData(data); |
| 93 | cp.setDataLength(6); |
109 | cp.setDataLength(6); |
| 94 | return cp; |
110 | return cp; |
| 95 | } |
111 | } |
| 96 | 112 | ||
| 97 | public bool isBiosStart(CanPacket cp, byte |
113 | public bool isBiosStart(CanPacket cp, byte sender, uint hwid) { |
| 98 | bool returnval = false; |
114 | bool returnval = false; |
| 99 | uint id = cp.getId(); |
115 | uint id = cp.getId(); |
| - | 116 | if (HWID_INUSE) { |
|
| - | 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)) { |
|
| - | 119 | returnval = true; |
|
| - | 120 | string appinfo = "Application installed"; |
|
| - | 121 | if (data[2] == 0) { |
|
| - | 122 | appinfo = "No application installed"; |
|
| - | 123 | } |
|
| - | 124 | if (data[3]!=0 && data[3]!=0xff) { |
|
| - | 125 | appinfo += ", application can-id "+data[3]; |
|
| - | 126 | } |
|
| - | 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]) + |
|
| - | 129 | ", " + appinfo |
|
| - | 130 | ); |
|
| - | 131 | } |
|
| - | 132 | } else { |
|
| 100 | 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)) { |
| 101 | returnval = true; |
134 | returnval = true; |
| 102 | byte[] data = cp.getData(); |
135 | byte[] data = cp.getData(); |
| 103 | string appinfo = "Application installed"; |
136 | string appinfo = "Application installed"; |
| 104 | if (data[2] == 0) { |
137 | if (data[2] == 0) { |
| Line 106... | Line 139... | ||
| 106 | } |
139 | } |
| 107 | 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) + |
| 108 | ", 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]) + |
| 109 | ", " + appinfo |
142 | ", " + appinfo |
| 110 | ); |
143 | ); |
| - | 144 | } |
|
| 111 | } |
145 | } |
| 112 | return returnval; |
146 | return returnval; |
| 113 | } |
147 | } |
| 114 | 148 | ||
| 115 | public bool doStart(DaemonConnection dc, byte |
149 | public bool doStart(DaemonConnection dc, byte receiver, uint hwid) { |
| 116 | return doStart(dc, receiver, 0); |
150 | return doStart(dc, receiver, 0, hwid); |
| 117 | } |
151 | } |
| 118 | 152 | ||
| 119 | public bool doStart(DaemonConnection dc, byte receiver, byte |
153 | public bool doStart(DaemonConnection dc, byte receiver, byte sender, uint hwid) { |
| 120 | bool success = true; |
154 | bool success = true; |
| 121 | 155 | ||
| - | 156 | if (HWID_INUSE) { |
|
| - | 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); |
|
| - | 159 | cp.setData(data); |
|
| - | 160 | cp.setDataLength(4); |
|
| - | 161 | } |
|
| 122 | setReceiver(receiver); |
162 | setReceiver(receiver); |
| 123 | dc.sendCanPacket(getStartPacket()); |
163 | dc.sendCanPacket(getStartPacket()); |
| 124 | return success; |
164 | return success; |
| 125 | } |
165 | } |
| 126 | 166 | ||
| 127 | public bool doReset(DaemonConnection dc, byte receiver) { |
167 | public bool doReset(DaemonConnection dc, byte receiver) { |
| 128 | return doReset(dc, receiver, 0); |
168 | return doReset(dc, receiver, 0); |
| 129 | } |
169 | } |
| - | 170 | ||
| - | 171 | public bool doReset(DaemonConnection dc, byte receiver, uint hwid) { |
|
| - | 172 | return doReset(dc, receiver, 0, hwid); |
|
| 130 | 173 | } |
|
| - | 174 | ||
| 131 | public bool doReset(DaemonConnection dc, byte receiver, byte |
175 | public bool doReset(DaemonConnection dc, byte receiver, byte sender, uint hwid) { |
| 132 | bool success = false; |
176 | bool success = false; |
| 133 | 177 | ||
| - | 178 | if (HWID_INUSE) { |
|
| - | 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); |
|
| - | 181 | cp.setData(data); |
|
| - | 182 | cp.setDataLength(4); |
|
| - | 183 | } else { |
|
| 134 | setReceiver(receiver); |
184 | setReceiver(receiver); |
| - | 185 | } |
|
| - | 186 | ||
| 135 | dc.sendCanPacket(getResetPacket()); |
187 | dc.sendCanPacket(getResetPacket()); |
| 136 | CanPacket getcp; |
188 | CanPacket getcp; |
| 137 | running = true; |
189 | running = true; |
| 138 | dc.flushData(); |
190 | dc.flushData(); |
| 139 | for (int i = 0; i < 150; i++) { |
191 | for (int i = 0; i < 150; i++) { |
| 140 | Thread.Sleep(50); |
192 | Thread.Sleep(50); |
| 141 | if (dc.getData(out getcp)) { |
193 | if (dc.getData(out getcp)) { |
| 142 | if (isBiosStart(getcp, |
194 | if (isBiosStart(getcp, receiver, hwid)) { |
| 143 | success = true; |
195 | success = true; |
| 144 | break; |
196 | break; |
| 145 | } |
197 | } |
| 146 | } |
198 | } |
| 147 | 199 | ||