mikemate88

CWIFIAccess.cs

Jun 4th, 2019
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 13.11 KB | None | 0 0
  1. // Decompiled with JetBrains decompiler
  2. // Type: Rinnai.NetworkerSystem.API.NBWControlWIFIAccess.CWIFIAccess
  3. // Assembly: API, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
  4. // MVID: 9E1B7773-F300-4632-970B-75E74F11BA63
  5. // Assembly location: C:\Users\User\Documents\rinnaitouchDecom\resources\assemblies\API.dll
  6.  
  7. using NetworkerSystem.NBWControlWIFIAccess;
  8. using Rinnai.NetworkerSystem.API.NBWControl;
  9. using System;
  10. using System.Text;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13.  
  14. namespace Rinnai.NetworkerSystem.API.NBWControlWIFIAccess
  15. {
  16.   public class CWIFIAccess
  17.   {
  18.     private Action<string> _debugLog;
  19.     private const string _CONNECT_STRING = "*HELLO*";
  20.     private CWIFIAccess.TCP_CONNECT_STATE _tcpConnectState;
  21.     private CTCPConnection _tcpConnection;
  22.     private SemaphoreSlim _tcpConnectSignal;
  23.     private bool _hostActivityDetected;
  24.     private bool _userActivityDetected;
  25.     private CancellationTokenSource _cancellationTokenMonitorConnectionClosed;
  26.     private const ushort SPLINKIF__UDPBC_INDEX__DEVICE_ID_START = 0;
  27.     private const ushort SPLINKIF__UDPBC_INDEX__TCPPORT_HI = 32;
  28.     private const ushort SPLINKIF__UDPBC_INDEX__TCPPORT_LO = 33;
  29.     private const ushort SPLINKIF__UDPBC_INDEX__WLAN_MODE = 34;
  30.     private const ushort SPLINKIF__UDPBC_INDEX__OVERALL_VERSION = 35;
  31.     private const ushort SPLINKIF__UDPBC_INDEX__CC3200_VERSION = 36;
  32.     private const ushort SPLINKIF__UDPBC_INDEX__WLAN_ACCESS = 37;
  33.     private const ushort SPLINKIF__UDPBC_INDEX__DFLT_PASSWORD_START = 48;
  34.     private const ushort SPLINKIF__UDPBC_INDEX__HUB_AXS_HASH_START = 64;
  35.     private const ushort SPLINKIF__UDPBC_INDEX__SCANNED_SSIDS = 255;
  36.     private const byte SPLINKIF__MQTT_ACCESS_HASH = 32;
  37.     private CUDPConnection _udpConnection;
  38.     private SemaphoreSlim _udpLocateSignal;
  39.     private string _deviceName;
  40.  
  41.     public CWIFIAccess(Action<string> debugLog)
  42.     {
  43.       this._debugLog = debugLog;
  44.       this.HostIPAddress = "";
  45.       this.HostPort = 0;
  46.       this._tcpConnectState = CWIFIAccess.TCP_CONNECT_STATE.CLOSED;
  47.       this._tcpConnection = new CTCPConnection();
  48.       this._tcpConnection.Opened += new EventHandler(this.TCPConnection_Opened);
  49.       this._tcpConnection.Closed += new EventHandler(this.TCPConnection_Closed);
  50.       this._tcpConnection.MessageReceived += new EventHandler<CMessageEventArgs>(this.TCPConnection_MessageReceived);
  51.       this._udpConnection = new CUDPConnection();
  52.       this._udpConnection.BroadcastMessageReceived += new EventHandler<CBroadcastMessageReceivedEventArgs>(this.UDPConnection_BroadcastMessageReceived);
  53.     }
  54.  
  55.     public bool ConnectionActive
  56.     {
  57.       get
  58.       {
  59.         return this._tcpConnectState == CWIFIAccess.TCP_CONNECT_STATE.ACTIVE;
  60.       }
  61.     }
  62.  
  63.     public string HostIPAddress { get; private set; }
  64.  
  65.     public int HostPort { get; private set; }
  66.  
  67.     public bool ModuleInConfigMode { get; private set; }
  68.  
  69.     public byte ModuleVersion { get; private set; }
  70.  
  71.     public byte CC3200Version { get; private set; }
  72.  
  73.     public CWIFIAccess.WLANAccessMode Mode { get; private set; }
  74.  
  75.     public byte[] HUBAccessHash { get; } = new byte[32];
  76.  
  77.     public string DeviceDefaultPassord { get; private set; }
  78.  
  79.     public byte NumberOfScannedSSIDs { get; private set; }
  80.  
  81.     public CScannedSSID[] ScannedSSID { get; private set; } = new CScannedSSID[10];
  82.  
  83.     public async Task<bool> EstablishedConnection(CWIFIConnectionDetails wifiConnection)
  84.     {
  85.       this._deviceName = wifiConnection.DeviceName;
  86.       this._tcpConnectState = CWIFIAccess.TCP_CONNECT_STATE.CLOSED;
  87.       this._debugLog("UDP Locating...");
  88.       bool udpHostLocated = await this.HostLocatedUsingUDP(wifiConnection.UDPBroadcastPort, wifiConnection.HostLocateTimeoutTimeSeconds);
  89.       if (!udpHostLocated)
  90.       {
  91.         this._debugLog("UDP NOT Located");
  92.         this._udpConnection.StopBroadcastMonitor();
  93.         EventHandler hostLocateTimeout = this.HostLocateTimeout;
  94.         if (hostLocateTimeout != null)
  95.           hostLocateTimeout((object) this, EventArgs.Empty);
  96.         return false;
  97.       }
  98.       this._debugLog("UDP Located");
  99.       EventHandler hostLocated = this.HostLocated;
  100.       if (hostLocated != null)
  101.         hostLocated((object) this, EventArgs.Empty);
  102.       this._debugLog("TCP Connecting...");
  103.       bool tcpConnectedToHost = await this.HostConnectedToUsingTCP(wifiConnection.HostConnectTimeoutTimeSeconds);
  104.       if (!tcpConnectedToHost)
  105.       {
  106.         this._debugLog("TCP NOT Connected");
  107.         this._tcpConnection.Close();
  108.         EventHandler hostConnectTimeout = this.HostConnectTimeout;
  109.         if (hostConnectTimeout != null)
  110.           hostConnectTimeout((object) this, EventArgs.Empty);
  111.         return false;
  112.       }
  113.       this._debugLog("TCP Connected");
  114.       this._cancellationTokenMonitorConnectionClosed = new CancellationTokenSource();
  115.       if (wifiConnection.HostInactivityTimeoutTimeSeconds > (byte) 0)
  116.       {
  117.         Task A = this.MonitorHostInactivity(wifiConnection.HostInactivityTimeoutTimeSeconds);
  118.         A = (Task) null;
  119.       }
  120.       if (wifiConnection.UserInactivityTimeoutTimeMinutes > (byte) 0)
  121.       {
  122.         Task B = this.MonitorUserInactivity(wifiConnection.UserInactivityTimeoutTimeMinutes);
  123.         B = (Task) null;
  124.       }
  125.       EventHandler hostConnected = this.HostConnected;
  126.       if (hostConnected != null)
  127.         hostConnected((object) this, EventArgs.Empty);
  128.       return true;
  129.     }
  130.  
  131.     public void CloseConnection()
  132.     {
  133.       this._tcpConnection.Close();
  134.     }
  135.  
  136.     public async Task SendTextMessage(string message)
  137.     {
  138.       await this.SendBuffer(Encoding.ASCII.GetBytes(message));
  139.     }
  140.  
  141.     public async Task SendBuffer(byte[] dataBuffer)
  142.     {
  143.       this._userActivityDetected = true;
  144.       await this._tcpConnection.SendBuffer(dataBuffer);
  145.     }
  146.  
  147.     public event EventHandler HostConnectTimeout;
  148.  
  149.     public event EventHandler HostConnected;
  150.  
  151.     public event EventHandler HostInactivityDetected;
  152.  
  153.     public event EventHandler UserInactivityDetected;
  154.  
  155.     public event EventHandler HostDisonnected;
  156.  
  157.     public event EventHandler<CMessageEventArgs> MessageReceived;
  158.  
  159.     private async Task<bool> HostConnectedToUsingTCP(byte hostConnectTimeoutTimeSeconds)
  160.     {
  161.       Task A = this._tcpConnection.OpenAndListen(this.HostIPAddress, this.HostPort);
  162.       this._tcpConnectSignal = new SemaphoreSlim(0, 1);
  163.       bool tcpConnected = await this._tcpConnectSignal.WaitAsync((int) hostConnectTimeoutTimeSeconds * 1000);
  164.       this._tcpConnectSignal = (SemaphoreSlim) null;
  165.       return tcpConnected;
  166.     }
  167.  
  168.     private async Task MonitorHostInactivity(byte hostInactivityTimeoutTimeSeconds)
  169.     {
  170.       byte hostInactivityTimeLeft = hostInactivityTimeoutTimeSeconds;
  171.       this._hostActivityDetected = true;
  172.       do
  173.       {
  174.         await Task.Delay(1000, this._cancellationTokenMonitorConnectionClosed.Token);
  175.         if (this._hostActivityDetected)
  176.         {
  177.           hostInactivityTimeLeft = hostInactivityTimeoutTimeSeconds;
  178.           this._hostActivityDetected = false;
  179.         }
  180.         else
  181.           --hostInactivityTimeLeft;
  182.       }
  183.       while (hostInactivityTimeLeft > (byte) 0 && (uint) this._tcpConnectState > 0U);
  184.       if ((uint) this._tcpConnectState <= 0U)
  185.         return;
  186.       EventHandler inactivityDetected = this.HostInactivityDetected;
  187.       if (inactivityDetected != null)
  188.         inactivityDetected((object) this, EventArgs.Empty);
  189.       this._tcpConnection.Close();
  190.     }
  191.  
  192.     private async Task MonitorUserInactivity(byte userInactivityTimeoutTimeMinutes)
  193.     {
  194.       int userInactivityTimeLeft = 60 * (int) userInactivityTimeoutTimeMinutes;
  195.       this._userActivityDetected = true;
  196.       do
  197.       {
  198.         await Task.Delay(1000, this._cancellationTokenMonitorConnectionClosed.Token);
  199.         if (this._userActivityDetected)
  200.         {
  201.           userInactivityTimeLeft = 60 * (int) userInactivityTimeoutTimeMinutes;
  202.           this._userActivityDetected = false;
  203.         }
  204.         else
  205.           --userInactivityTimeLeft;
  206.       }
  207.       while (userInactivityTimeLeft > 0 && (uint) this._tcpConnectState > 0U);
  208.       if ((uint) this._tcpConnectState <= 0U)
  209.         return;
  210.       EventHandler inactivityDetected = this.UserInactivityDetected;
  211.       if (inactivityDetected != null)
  212.         inactivityDetected((object) this, EventArgs.Empty);
  213.       this._tcpConnection.Close();
  214.     }
  215.  
  216.     protected virtual void TCPConnection_MessageReceived(object sender, CMessageEventArgs e)
  217.     {
  218.       string str = Encoding.UTF8.GetString(e.Packet.Buffer, 0, e.Packet.BufferLength);
  219.       EventHandler<CMessageEventArgs> messageReceived = this.MessageReceived;
  220.       if (messageReceived != null)
  221.         messageReceived((object) this, e);
  222.       if (this._tcpConnectState == CWIFIAccess.TCP_CONNECT_STATE.ACTIVE)
  223.       {
  224.         this._hostActivityDetected = true;
  225.       }
  226.       else
  227.       {
  228.         if (!str.Contains("*HELLO*"))
  229.           return;
  230.         this._tcpConnectState = CWIFIAccess.TCP_CONNECT_STATE.ACTIVE;
  231.         this._tcpConnectSignal.Release();
  232.       }
  233.     }
  234.  
  235.     protected virtual void TCPConnection_Closed(object sender, EventArgs e)
  236.     {
  237.       this._tcpConnectState = CWIFIAccess.TCP_CONNECT_STATE.CLOSED;
  238.       if (this._cancellationTokenMonitorConnectionClosed != null)
  239.       {
  240.         this._cancellationTokenMonitorConnectionClosed.Cancel();
  241.         this._cancellationTokenMonitorConnectionClosed = (CancellationTokenSource) null;
  242.       }
  243.       EventHandler hostDisonnected = this.HostDisonnected;
  244.       if (hostDisonnected == null)
  245.         return;
  246.       hostDisonnected((object) this, EventArgs.Empty);
  247.     }
  248.  
  249.     protected virtual void TCPConnection_Opened(object sender, EventArgs e)
  250.     {
  251.       this._tcpConnectState = CWIFIAccess.TCP_CONNECT_STATE.OPENED;
  252.     }
  253.  
  254.     public event EventHandler HostLocateTimeout;
  255.  
  256.     public event EventHandler HostLocated;
  257.  
  258.     public event EventHandler<CBroadcastMessageReceivedEventArgs> UDPBroadcastMessageReceived;
  259.  
  260.     private async Task<bool> HostLocatedUsingUDP(
  261.       int udpBroadcastPort,
  262.       byte hostLocateTimeoutTimeSeconds)
  263.     {
  264.       this._udpLocateSignal = new SemaphoreSlim(0, 1);
  265.       Task A = this._udpConnection.MonitorBroadcast(udpBroadcastPort);
  266.       bool udpHostLocated = await this._udpLocateSignal.WaitAsync((int) hostLocateTimeoutTimeSeconds * 1000);
  267.       this._udpLocateSignal = (SemaphoreSlim) null;
  268.       return udpHostLocated;
  269.     }
  270.  
  271.     protected virtual void UDPConnection_BroadcastMessageReceived(
  272.       object sender,
  273.       CBroadcastMessageReceivedEventArgs e)
  274.     {
  275.       EventHandler<CBroadcastMessageReceivedEventArgs> broadcastMessageReceived = this.UDPBroadcastMessageReceived;
  276.       if (broadcastMessageReceived != null)
  277.         broadcastMessageReceived((object) this, e);
  278.       if (!Encoding.UTF8.GetString(e.RxPacket.Buffer, 0, e.RxPacket.BufferLength).Contains(this._deviceName))
  279.         return;
  280.       this.HostIPAddress = e.RemoteIPAddress;
  281.       this.HostPort = 256 * (int) e.RxPacket.Buffer[32] + (int) e.RxPacket.Buffer[33];
  282.       this.ModuleInConfigMode = e.RxPacket.Buffer[34] == (byte) 170;
  283.       this.ModuleVersion = e.RxPacket.Buffer[35] != (byte) 0 ? e.RxPacket.Buffer[35] : (byte) 172;
  284.       this.CC3200Version = e.RxPacket.Buffer[36] != (byte) 0 ? e.RxPacket.Buffer[36] : (byte) 1;
  285.       this.Mode = (CWIFIAccess.WLANAccessMode) e.RxPacket.Buffer[37];
  286.       this.DeviceDefaultPassord = Encoding.UTF8.GetString(e.RxPacket.Buffer, 48, 10);
  287.       Array.Copy((Array) e.RxPacket.Buffer, 64, (Array) this.HUBAccessHash, 0, 32);
  288.       this.DefineSSIDList(e);
  289.       this._udpConnection.StopBroadcastMonitor();
  290.       this._udpLocateSignal.Release();
  291.     }
  292.  
  293.     private void DefineSSIDList(CBroadcastMessageReceivedEventArgs e)
  294.     {
  295.       this.NumberOfScannedSSIDs = e.RxPacket.Buffer[(int) byte.MaxValue];
  296.       for (int index1 = 0; index1 < 10; ++index1)
  297.       {
  298.         CScannedSSID cscannedSsid;
  299.         if (index1 < (int) this.NumberOfScannedSSIDs)
  300.         {
  301.           int index2 = 256 + 35 * index1;
  302.           byte num = e.RxPacket.Buffer[index2 + 32];
  303.           string name = Encoding.UTF8.GetString(e.RxPacket.Buffer, index2, (int) num);
  304.           CScannedSSID.SECURITY security;
  305.           switch (e.RxPacket.Buffer[index2 + 32 + 1])
  306.           {
  307.             case 1:
  308.               security = CScannedSSID.SECURITY.WEP;
  309.               break;
  310.             case 2:
  311.               security = CScannedSSID.SECURITY.WPA;
  312.               break;
  313.             case 3:
  314.               security = CScannedSSID.SECURITY.WPA2;
  315.               break;
  316.             default:
  317.               security = CScannedSSID.SECURITY.OPEN;
  318.               break;
  319.           }
  320.           byte signalStrength = e.RxPacket.Buffer[index2 + 32 + 1 + 1];
  321.           cscannedSsid = new CScannedSSID(name, security, signalStrength);
  322.         }
  323.         else
  324.           cscannedSsid = new CScannedSSID("");
  325.         this.ScannedSSID[index1] = cscannedSsid;
  326.       }
  327.     }
  328.  
  329.     public enum WLANAccessMode
  330.     {
  331.       AP_DEFAULT,
  332.       AP_USER_CONFIGURED,
  333.       STATION,
  334.     }
  335.  
  336.     private enum TCP_CONNECT_STATE
  337.     {
  338.       CLOSED,
  339.       OPENED,
  340.       ACTIVE,
  341.     }
  342.   }
  343. }
Advertisement
Add Comment
Please, Sign In to add comment