Subversion Repositories HomeAutomation

Rev

Rev 770 | Rev 1635 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  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.  
  19.     private bool HWID_INUSE=false;
  20.  
  21.     CanPacket cp;
  22.     private bool running = false;
  23.    
  24.     public CanNMT(bool HWID_INUSE) {
  25.         cp = new CanPacket();
  26.         cp.setExt(1);
  27.         this.HWID_INUSE = HWID_INUSE;
  28.     }
  29.  
  30.     public CanNMT(CanPacket cp, bool HWID_INUSE) {
  31.         this.cp = cp;
  32.         this.cp.setExt(1);
  33.         this.HWID_INUSE = HWID_INUSE;
  34.     }
  35.    
  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.    
  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 ) );
  64.         if (HWID_INUSE) {
  65.            cp.setDataLength(4);
  66.         } else {
  67.            cp.setDataLength(0);
  68.         }
  69.         return cp;
  70.     }
  71.  
  72.     public CanPacket getStartPacket() {
  73.         cp.setId( ( cp.getId()&0x0000FFFF )|( CAN_NMT_START_APP<<16 ) );
  74.         if (HWID_INUSE) {
  75.            cp.setDataLength(4);
  76.         } else {
  77.            cp.setDataLength(0);
  78.         }
  79.         return cp;
  80.     }
  81.  
  82.     public CanPacket getPgmStartPacket(byte[] data) {
  83.         cp.setId( ( cp.getId()&0x0000FFFF )|( CAN_NMT_PGM_START<<16 ) );
  84.         cp.setData(data);
  85.         if (HWID_INUSE) {
  86.            cp.setDataLength(8);
  87.         } else {
  88.            cp.setDataLength(4);
  89.         }
  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.     }
  112.    
  113.     public bool isBiosStart(CanPacket cp, byte sender, uint hwid) {
  114.         bool returnval = false;
  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) {
  125.                    appinfo += ", device type unkown";
  126.                 }
  127.                 if (data[3]!=0) {
  128.                    appinfo += ", device type "+data[3];
  129.                 }
  130.                 Console.WriteLine("Bios started on node 0x" + String.Format("{0:x2}", sender) +
  131.                                     ", bios version 0x" + String.Format("{0:x2}", data[1]) + String.Format("{0:x2}", data[0]) +
  132.                                     ", " + appinfo
  133.                                     );
  134.             }
  135.         } else {
  136.             if ((id&0x00FF0000) == (CAN_NMT_BIOS_START<<16) && (id&0x0000FF00) == (sender<<8)) {
  137.                 returnval = true;
  138.                 byte[] data = cp.getData();
  139.                 string appinfo = "Application installed";
  140.                 if (data[2] == 0) {
  141.                     appinfo = "No application installed";
  142.                 }
  143.                 Console.WriteLine("Bios started on node 0x" + String.Format("{0:x2}", sender) +
  144.                                     ", bios version 0x" + String.Format("{0:x2}", data[1]) + String.Format("{0:x2}", data[0]) +
  145.                                     ", " + appinfo
  146.                                     );
  147.             }
  148.         }
  149.         return returnval;
  150.     }
  151.  
  152.     public bool doStart(DaemonConnection dc, byte receiver, uint hwid) {
  153.         return doStart(dc, receiver, 0, hwid);
  154.     }
  155.    
  156.     public bool doStart(DaemonConnection dc, byte receiver, byte sender, uint hwid) {
  157.         bool success = true;
  158.  
  159.           if (HWID_INUSE) {
  160.            byte[] data = new byte[8];
  161.            data[0] = (byte)(hwid&0xff); data[1] = (byte)((hwid>>8)&0xff); data[2] = (byte)((hwid>>16)&0xff); data[3] = (byte)((hwid>>24)&0xff);  
  162.            cp.setData(data);
  163.            cp.setDataLength(4);
  164.           }    
  165.         setReceiver(receiver);
  166.         dc.sendCanPacket(getStartPacket());
  167.         return success;
  168.     }
  169.  
  170.     public bool doReset(DaemonConnection dc, byte receiver) {
  171.         return doReset(dc, receiver, 0);
  172.     }
  173.  
  174.     public bool doReset(DaemonConnection dc, byte receiver, uint hwid) {
  175.         return doReset(dc, receiver, 0, hwid);
  176.     }
  177.  
  178.     public bool doReset(DaemonConnection dc, byte receiver, byte sender, uint hwid) {
  179.         bool success = false;
  180.  
  181.       if (HWID_INUSE) {
  182.        byte[] data = new byte[8];
  183.        data[0] = (byte)(hwid&0xff); data[1] = (byte)((hwid>>8)&0xff); data[2] = (byte)((hwid>>16)&0xff); data[3] = (byte)((hwid>>24)&0xff);  
  184.        cp.setData(data);
  185.        cp.setDataLength(4);
  186.       } else {
  187.         setReceiver(receiver);
  188.       }      
  189.        
  190.         dc.sendCanPacket(getResetPacket());
  191.         CanPacket getcp;
  192.         running = true;
  193.         dc.flushData();
  194.         for (int i = 0; i < 150; i++) {
  195.             Thread.Sleep(50);
  196.             if (dc.getData(out getcp)) {
  197.                 if (isBiosStart(getcp, receiver, hwid)) {
  198.                     success = true;
  199.                     break;
  200.                 }
  201.             }
  202.            
  203.             if (!running) {
  204.                 break;
  205.             }
  206.         }
  207.         if (!success) {
  208.             Console.WriteLine("Timed out while waiting for node to reset");
  209.         }
  210.         return success;
  211.     }
  212.    
  213.     public void stop() {
  214.         running = false;
  215.     }
  216.    
  217. }
  218.