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