Guest User

Untitled

a guest
Dec 15th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.25 KB | None | 0 0
  1. /// <summary>
  2. /// Sets the VLAN mask for the specified port.
  3. /// </summary>
  4. public void SetVlanMask(Port port, Byte[] mask) {
  5.     if (mask.Length != 2)
  6.         throw new ArgumentException("mask must be exactly 2 bytes.");
  7.  
  8.     try {
  9.         lock (objLock) {
  10.             if (blnConnected || Connect()) {
  11.                 Log("Set VLAN data for port: " + port + "Value: " + BitConverter.ToString(mask));
  12.  
  13.                 Int32 intReturn = 0;
  14.                 // enable MDIO
  15.                 Byte[] writeBytes = new Byte[] { 0x02, 0x01 };
  16.                 intReturn = WriteRequest((Byte)Device.Switch, writeBytes, 2);
  17.                 if (intReturn != CP2112_DLL.HID_SMBUS_SUCCESS) {
  18.                     LogUtility.Log.Error("SetVlanMask error: WriteRequest(" + ((Byte)Device.Switch).ToString("X2") + ", " + BitConverter.ToString(writeBytes) + ", 2) == " + intReturn.ToString() + " (Enable MDIO)");
  19.                     return;
  20.                 }
  21.                 // address switch
  22.                 writeBytes = new Byte[] { 0x5F, GetSwitchPortDevice(port) }; // GetSwitchPortDevice() returns either 0x01 or 0x02 depending on which switch that port is on
  23.                 intReturn = WriteRequest((Byte)Device.Switch, writeBytes, 2);
  24.                 if (intReturn != CP2112_DLL.HID_SMBUS_SUCCESS) {
  25.                     LogUtility.Log.Error("SetVlanMask error: WriteRequest(" + ((Byte)Device.Switch).ToString("X2") + ", " + BitConverter.ToString(writeBytes) + ", 2) (address switch)");
  26.                     return;
  27.                 }
  28.  
  29.                 byte bytPort = (byte)(GetSwitchPortRegister(port) + 0x10);
  30.                 ushort smiCommand = (ushort)(1 << 15 | 1 << 12 | 1 << 10 | bytPort << 5 | 0x06); // 0x06 is the VLAN register
  31.                 writeBytes = new Byte[3];
  32.                 writeBytes[0] = 0x61;
  33.                 writeBytes[1] = mask[1];
  34.                 writeBytes[2] = mask[0];
  35.                 intReturn = WriteRequest((Byte)Device.Switch, writeBytes, 3); // write mask first
  36.                 if (intReturn != CP2112_DLL.HID_SMBUS_SUCCESS) {
  37.                     LogUtility.Log.Error("SetVlanMask error: WriteRequest(" + ((Byte)Device.Switch).ToString("X2") + ", " + BitConverter.ToString(writeBytes) + ", 3) (write port vlan data)");
  38.                     return;
  39.                 }
  40.                
  41.                 writeBytes[0] = 0x60;
  42.                 writeBytes[1] = (byte)(smiCommand & 0xFF); // lsb
  43.                 writeBytes[2] = (byte)(smiCommand >> 8); // msb
  44.                 intReturn = WriteRequest((Byte)Device.Switch, writeBytes, 3); // write SMI command
  45.                 if (intReturn != CP2112_DLL.HID_SMBUS_SUCCESS) {
  46.                     LogUtility.Log.Error("SetVlanMask error: WriteRequest(" + ((Byte)Device.Switch).ToString("X2") + ", " + BitConverter.ToString(writeBytes) + ", 3) (write port vlan data)");
  47.                     return;
  48.                 }
  49.             }
  50.         }
  51.     } catch (Exception ex) {
  52.         LogUtility.Log.Error("SetVlanMask exception", ex);
  53.     } finally { Disconnect(); }
  54. }
  55.  
  56. /// <summary>
  57. /// Sets the VLAN scenario 0 (No VLANs).  All ports will be able to
  58. /// communicate with all other ports.
  59. /// </summary>
  60. public Boolean SetVlanScenario0() {
  61.     try {
  62.         lock (objLock) {
  63.             SetVlanMask(Port.Port01,  new Byte[] { 0x07, 0xBF });
  64.             SetVlanMask(Port.Port02,  new Byte[] { 0x07, 0x7F });
  65.             SetVlanMask(Port.Port03,  new Byte[] { 0x07, 0xEF });
  66.             SetVlanMask(Port.Port04,  new Byte[] { 0x07, 0xDF });
  67.             SetVlanMask(Port.Port05,  new Byte[] { 0x07, 0xFB });
  68.             SetVlanMask(Port.Port06,  new Byte[] { 0x07, 0xF7 });
  69.             SetVlanMask(Port.Port07,  new Byte[] { 0x07, 0xFE });
  70.             SetVlanMask(Port.Port08,  new Byte[] { 0x07, 0xFD });
  71.             SetVlanMask(Port.Port09,  new Byte[] { 0x07, 0xBF });
  72.             SetVlanMask(Port.Port10,  new Byte[] { 0x07, 0x7F });
  73.             SetVlanMask(Port.Port11,  new Byte[] { 0x07, 0xEF });
  74.             SetVlanMask(Port.Port12,  new Byte[] { 0x07, 0xDF });
  75.             SetVlanMask(Port.Port13,  new Byte[] { 0x07, 0xFB });
  76.             SetVlanMask(Port.Port14,  new Byte[] { 0x07, 0xF7 });
  77.             SetVlanMask(Port.Port15,  new Byte[] { 0x07, 0xFE });
  78.             SetVlanMask(Port.Port16,  new Byte[] { 0x07, 0xFD });
  79.             SetVlanMask(Port.Uplink1, new Byte[] { 0x06, 0xFF });
  80.             SetVlanMask(Port.Uplink2, new Byte[] { 0x06, 0xFF });
  81.             SetVlanMask(Port.Switch1, new Byte[] { 0x05, 0xFF });
  82.             SetVlanMask(Port.Switch2, new Byte[] { 0x03, 0xFF });
  83.             SetVlanMask(Port.COM_X,   new Byte[] { 0x05, 0xFF });
  84.             SetVlanMask(Port.DSP,     new Byte[] { 0x03, 0xFF });
  85.         }
  86.     } catch {
  87.         return false;
  88.     }
  89.     return true;
  90. }
  91.  
  92. /// <summary>
  93. /// Sets the VLAN scenario 1. Uplink 2 is WAN port, all others
  94. /// are on the same VLAN.
  95. /// </summary>
  96. public Boolean SetVlanScenario1() {
  97.     try {
  98.         lock (objLock) {
  99.             SetVlanMask(Port.Port01,  new Byte[] { 0x07, 0xBF });
  100.             SetVlanMask(Port.Port02,  new Byte[] { 0x07, 0x7F });
  101.             SetVlanMask(Port.Port03,  new Byte[] { 0x07, 0xEF });
  102.             SetVlanMask(Port.Port04,  new Byte[] { 0x07, 0xDF });
  103.             SetVlanMask(Port.Port05,  new Byte[] { 0x07, 0xFB });
  104.             SetVlanMask(Port.Port06,  new Byte[] { 0x07, 0xF7 });
  105.             SetVlanMask(Port.Port07,  new Byte[] { 0x07, 0xFE });
  106.             SetVlanMask(Port.Port08,  new Byte[] { 0x07, 0xFD });
  107.             SetVlanMask(Port.Port09,  new Byte[] { 0x07, 0xBF });
  108.             SetVlanMask(Port.Port10,  new Byte[] { 0x07, 0x7F });
  109.             SetVlanMask(Port.Port11,  new Byte[] { 0x07, 0xEF });
  110.             SetVlanMask(Port.Port12,  new Byte[] { 0x07, 0xDF });
  111.             SetVlanMask(Port.Port13,  new Byte[] { 0x07, 0xFB });
  112.             SetVlanMask(Port.Port14,  new Byte[] { 0x07, 0xF7 });
  113.             SetVlanMask(Port.Port15,  new Byte[] { 0x07, 0xFE });
  114.             SetVlanMask(Port.Port16,  new Byte[] { 0x07, 0xFD });
  115.             SetVlanMask(Port.Uplink1, new Byte[] { 0x06, 0xFF });
  116.             SetVlanMask(Port.Uplink2, new Byte[] { 0x02, 0x00 }); // uplink 2 can only communicate with COM X
  117.             SetVlanMask(Port.Switch1, new Byte[] { 0x05, 0xFF });
  118.             SetVlanMask(Port.Switch2, new Byte[] { 0x03, 0xFF });
  119.             SetVlanMask(Port.COM_X,   new Byte[] { 0x05, 0xFF });
  120.             SetVlanMask(Port.DSP,     new Byte[] { 0x03, 0xFF });
  121.         }
  122.     } catch {
  123.         return false;
  124.     }
  125.     return true;
  126. }
  127.  
  128. /// <summary>
  129. /// Sets the VLAN scenario 2. Uplink 1 is on a VLAN with ports 9-16
  130. /// and Uplink 2 is on a VLAN with ports 1-8.
  131. /// </summary>
  132. public Boolean SetVlanScenario2() {
  133.     try {
  134.         lock (objLock) {
  135.             // ports 1-8 can only talk to ComX and Switch2 and each other, but not Uplink2
  136.             SetVlanMask(Port.Port01,  new Byte[] { 0x06, 0xBF }); // 0110-1011-1111 (06-BF)
  137.             SetVlanMask(Port.Port02,  new Byte[] { 0x06, 0x7F });
  138.             SetVlanMask(Port.Port03,  new Byte[] { 0x06, 0xEF });
  139.             SetVlanMask(Port.Port04,  new Byte[] { 0x06, 0xDF });
  140.             SetVlanMask(Port.Port05,  new Byte[] { 0x06, 0xFB });
  141.             SetVlanMask(Port.Port06,  new Byte[] { 0x06, 0xF7 });
  142.             SetVlanMask(Port.Port07,  new Byte[] { 0x06, 0xFE });
  143.             SetVlanMask(Port.Port08,  new Byte[] { 0x06, 0xFD });
  144.             SetVlanMask(Port.Port09,  new Byte[] { 0x06, 0xBF });
  145.             SetVlanMask(Port.Port10,  new Byte[] { 0x06, 0x7F });
  146.             SetVlanMask(Port.Port11,  new Byte[] { 0x06, 0xEF });
  147.             SetVlanMask(Port.Port12,  new Byte[] { 0x06, 0xDF });
  148.             SetVlanMask(Port.Port13,  new Byte[] { 0x06, 0xFB });
  149.             SetVlanMask(Port.Port14,  new Byte[] { 0x06, 0xF7 });
  150.             SetVlanMask(Port.Port15,  new Byte[] { 0x06, 0xFE });
  151.             SetVlanMask(Port.Port16,  new Byte[] { 0x06, 0xFD });
  152.             SetVlanMask(Port.Uplink1, new Byte[] { 0x06, 0x00 }); // uplink 1 can only talk to Switch1
  153.             SetVlanMask(Port.Uplink2, new Byte[] { 0x06, 0x00 }); // 0110-0000-0000 (06-00) uplink 2 can only talk to COM X and Switch2
  154.             SetVlanMask(Port.Switch1, new Byte[] { 0x05, 0xFF });
  155.             SetVlanMask(Port.Switch2, new Byte[] { 0x03, 0xFF });
  156.             SetVlanMask(Port.COM_X,   new Byte[] { 0x05, 0xFF });
  157.             SetVlanMask(Port.DSP,     new Byte[] { 0x03, 0xFF });
  158.         }
  159.     } catch {
  160.         return false;
  161.     }
  162.     return true;
  163. }
Add Comment
Please, Sign In to add comment