Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Decompiled with JetBrains decompiler
- // Type: Rinnai.NetworkerSystem.API.NBWControlWIFIAccess.CWIFIAccess
- // Assembly: API, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
- // MVID: 9E1B7773-F300-4632-970B-75E74F11BA63
- // Assembly location: C:\Users\User\Documents\rinnaitouchDecom\resources\assemblies\API.dll
- using NetworkerSystem.NBWControlWIFIAccess;
- using Rinnai.NetworkerSystem.API.NBWControl;
- using System;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace Rinnai.NetworkerSystem.API.NBWControlWIFIAccess
- {
- public class CWIFIAccess
- {
- private Action<string> _debugLog;
- private const string _CONNECT_STRING = "*HELLO*";
- private CWIFIAccess.TCP_CONNECT_STATE _tcpConnectState;
- private CTCPConnection _tcpConnection;
- private SemaphoreSlim _tcpConnectSignal;
- private bool _hostActivityDetected;
- private bool _userActivityDetected;
- private CancellationTokenSource _cancellationTokenMonitorConnectionClosed;
- private const ushort SPLINKIF__UDPBC_INDEX__DEVICE_ID_START = 0;
- private const ushort SPLINKIF__UDPBC_INDEX__TCPPORT_HI = 32;
- private const ushort SPLINKIF__UDPBC_INDEX__TCPPORT_LO = 33;
- private const ushort SPLINKIF__UDPBC_INDEX__WLAN_MODE = 34;
- private const ushort SPLINKIF__UDPBC_INDEX__OVERALL_VERSION = 35;
- private const ushort SPLINKIF__UDPBC_INDEX__CC3200_VERSION = 36;
- private const ushort SPLINKIF__UDPBC_INDEX__WLAN_ACCESS = 37;
- private const ushort SPLINKIF__UDPBC_INDEX__DFLT_PASSWORD_START = 48;
- private const ushort SPLINKIF__UDPBC_INDEX__HUB_AXS_HASH_START = 64;
- private const ushort SPLINKIF__UDPBC_INDEX__SCANNED_SSIDS = 255;
- private const byte SPLINKIF__MQTT_ACCESS_HASH = 32;
- private CUDPConnection _udpConnection;
- private SemaphoreSlim _udpLocateSignal;
- private string _deviceName;
- public CWIFIAccess(Action<string> debugLog)
- {
- this._debugLog = debugLog;
- this.HostIPAddress = "";
- this.HostPort = 0;
- this._tcpConnectState = CWIFIAccess.TCP_CONNECT_STATE.CLOSED;
- this._tcpConnection = new CTCPConnection();
- this._tcpConnection.Opened += new EventHandler(this.TCPConnection_Opened);
- this._tcpConnection.Closed += new EventHandler(this.TCPConnection_Closed);
- this._tcpConnection.MessageReceived += new EventHandler<CMessageEventArgs>(this.TCPConnection_MessageReceived);
- this._udpConnection = new CUDPConnection();
- this._udpConnection.BroadcastMessageReceived += new EventHandler<CBroadcastMessageReceivedEventArgs>(this.UDPConnection_BroadcastMessageReceived);
- }
- public bool ConnectionActive
- {
- get
- {
- return this._tcpConnectState == CWIFIAccess.TCP_CONNECT_STATE.ACTIVE;
- }
- }
- public string HostIPAddress { get; private set; }
- public int HostPort { get; private set; }
- public bool ModuleInConfigMode { get; private set; }
- public byte ModuleVersion { get; private set; }
- public byte CC3200Version { get; private set; }
- public CWIFIAccess.WLANAccessMode Mode { get; private set; }
- public byte[] HUBAccessHash { get; } = new byte[32];
- public string DeviceDefaultPassord { get; private set; }
- public byte NumberOfScannedSSIDs { get; private set; }
- public CScannedSSID[] ScannedSSID { get; private set; } = new CScannedSSID[10];
- public async Task<bool> EstablishedConnection(CWIFIConnectionDetails wifiConnection)
- {
- this._deviceName = wifiConnection.DeviceName;
- this._tcpConnectState = CWIFIAccess.TCP_CONNECT_STATE.CLOSED;
- this._debugLog("UDP Locating...");
- bool udpHostLocated = await this.HostLocatedUsingUDP(wifiConnection.UDPBroadcastPort, wifiConnection.HostLocateTimeoutTimeSeconds);
- if (!udpHostLocated)
- {
- this._debugLog("UDP NOT Located");
- this._udpConnection.StopBroadcastMonitor();
- EventHandler hostLocateTimeout = this.HostLocateTimeout;
- if (hostLocateTimeout != null)
- hostLocateTimeout((object) this, EventArgs.Empty);
- return false;
- }
- this._debugLog("UDP Located");
- EventHandler hostLocated = this.HostLocated;
- if (hostLocated != null)
- hostLocated((object) this, EventArgs.Empty);
- this._debugLog("TCP Connecting...");
- bool tcpConnectedToHost = await this.HostConnectedToUsingTCP(wifiConnection.HostConnectTimeoutTimeSeconds);
- if (!tcpConnectedToHost)
- {
- this._debugLog("TCP NOT Connected");
- this._tcpConnection.Close();
- EventHandler hostConnectTimeout = this.HostConnectTimeout;
- if (hostConnectTimeout != null)
- hostConnectTimeout((object) this, EventArgs.Empty);
- return false;
- }
- this._debugLog("TCP Connected");
- this._cancellationTokenMonitorConnectionClosed = new CancellationTokenSource();
- if (wifiConnection.HostInactivityTimeoutTimeSeconds > (byte) 0)
- {
- Task A = this.MonitorHostInactivity(wifiConnection.HostInactivityTimeoutTimeSeconds);
- A = (Task) null;
- }
- if (wifiConnection.UserInactivityTimeoutTimeMinutes > (byte) 0)
- {
- Task B = this.MonitorUserInactivity(wifiConnection.UserInactivityTimeoutTimeMinutes);
- B = (Task) null;
- }
- EventHandler hostConnected = this.HostConnected;
- if (hostConnected != null)
- hostConnected((object) this, EventArgs.Empty);
- return true;
- }
- public void CloseConnection()
- {
- this._tcpConnection.Close();
- }
- public async Task SendTextMessage(string message)
- {
- await this.SendBuffer(Encoding.ASCII.GetBytes(message));
- }
- public async Task SendBuffer(byte[] dataBuffer)
- {
- this._userActivityDetected = true;
- await this._tcpConnection.SendBuffer(dataBuffer);
- }
- public event EventHandler HostConnectTimeout;
- public event EventHandler HostConnected;
- public event EventHandler HostInactivityDetected;
- public event EventHandler UserInactivityDetected;
- public event EventHandler HostDisonnected;
- public event EventHandler<CMessageEventArgs> MessageReceived;
- private async Task<bool> HostConnectedToUsingTCP(byte hostConnectTimeoutTimeSeconds)
- {
- Task A = this._tcpConnection.OpenAndListen(this.HostIPAddress, this.HostPort);
- this._tcpConnectSignal = new SemaphoreSlim(0, 1);
- bool tcpConnected = await this._tcpConnectSignal.WaitAsync((int) hostConnectTimeoutTimeSeconds * 1000);
- this._tcpConnectSignal = (SemaphoreSlim) null;
- return tcpConnected;
- }
- private async Task MonitorHostInactivity(byte hostInactivityTimeoutTimeSeconds)
- {
- byte hostInactivityTimeLeft = hostInactivityTimeoutTimeSeconds;
- this._hostActivityDetected = true;
- do
- {
- await Task.Delay(1000, this._cancellationTokenMonitorConnectionClosed.Token);
- if (this._hostActivityDetected)
- {
- hostInactivityTimeLeft = hostInactivityTimeoutTimeSeconds;
- this._hostActivityDetected = false;
- }
- else
- --hostInactivityTimeLeft;
- }
- while (hostInactivityTimeLeft > (byte) 0 && (uint) this._tcpConnectState > 0U);
- if ((uint) this._tcpConnectState <= 0U)
- return;
- EventHandler inactivityDetected = this.HostInactivityDetected;
- if (inactivityDetected != null)
- inactivityDetected((object) this, EventArgs.Empty);
- this._tcpConnection.Close();
- }
- private async Task MonitorUserInactivity(byte userInactivityTimeoutTimeMinutes)
- {
- int userInactivityTimeLeft = 60 * (int) userInactivityTimeoutTimeMinutes;
- this._userActivityDetected = true;
- do
- {
- await Task.Delay(1000, this._cancellationTokenMonitorConnectionClosed.Token);
- if (this._userActivityDetected)
- {
- userInactivityTimeLeft = 60 * (int) userInactivityTimeoutTimeMinutes;
- this._userActivityDetected = false;
- }
- else
- --userInactivityTimeLeft;
- }
- while (userInactivityTimeLeft > 0 && (uint) this._tcpConnectState > 0U);
- if ((uint) this._tcpConnectState <= 0U)
- return;
- EventHandler inactivityDetected = this.UserInactivityDetected;
- if (inactivityDetected != null)
- inactivityDetected((object) this, EventArgs.Empty);
- this._tcpConnection.Close();
- }
- protected virtual void TCPConnection_MessageReceived(object sender, CMessageEventArgs e)
- {
- string str = Encoding.UTF8.GetString(e.Packet.Buffer, 0, e.Packet.BufferLength);
- EventHandler<CMessageEventArgs> messageReceived = this.MessageReceived;
- if (messageReceived != null)
- messageReceived((object) this, e);
- if (this._tcpConnectState == CWIFIAccess.TCP_CONNECT_STATE.ACTIVE)
- {
- this._hostActivityDetected = true;
- }
- else
- {
- if (!str.Contains("*HELLO*"))
- return;
- this._tcpConnectState = CWIFIAccess.TCP_CONNECT_STATE.ACTIVE;
- this._tcpConnectSignal.Release();
- }
- }
- protected virtual void TCPConnection_Closed(object sender, EventArgs e)
- {
- this._tcpConnectState = CWIFIAccess.TCP_CONNECT_STATE.CLOSED;
- if (this._cancellationTokenMonitorConnectionClosed != null)
- {
- this._cancellationTokenMonitorConnectionClosed.Cancel();
- this._cancellationTokenMonitorConnectionClosed = (CancellationTokenSource) null;
- }
- EventHandler hostDisonnected = this.HostDisonnected;
- if (hostDisonnected == null)
- return;
- hostDisonnected((object) this, EventArgs.Empty);
- }
- protected virtual void TCPConnection_Opened(object sender, EventArgs e)
- {
- this._tcpConnectState = CWIFIAccess.TCP_CONNECT_STATE.OPENED;
- }
- public event EventHandler HostLocateTimeout;
- public event EventHandler HostLocated;
- public event EventHandler<CBroadcastMessageReceivedEventArgs> UDPBroadcastMessageReceived;
- private async Task<bool> HostLocatedUsingUDP(
- int udpBroadcastPort,
- byte hostLocateTimeoutTimeSeconds)
- {
- this._udpLocateSignal = new SemaphoreSlim(0, 1);
- Task A = this._udpConnection.MonitorBroadcast(udpBroadcastPort);
- bool udpHostLocated = await this._udpLocateSignal.WaitAsync((int) hostLocateTimeoutTimeSeconds * 1000);
- this._udpLocateSignal = (SemaphoreSlim) null;
- return udpHostLocated;
- }
- protected virtual void UDPConnection_BroadcastMessageReceived(
- object sender,
- CBroadcastMessageReceivedEventArgs e)
- {
- EventHandler<CBroadcastMessageReceivedEventArgs> broadcastMessageReceived = this.UDPBroadcastMessageReceived;
- if (broadcastMessageReceived != null)
- broadcastMessageReceived((object) this, e);
- if (!Encoding.UTF8.GetString(e.RxPacket.Buffer, 0, e.RxPacket.BufferLength).Contains(this._deviceName))
- return;
- this.HostIPAddress = e.RemoteIPAddress;
- this.HostPort = 256 * (int) e.RxPacket.Buffer[32] + (int) e.RxPacket.Buffer[33];
- this.ModuleInConfigMode = e.RxPacket.Buffer[34] == (byte) 170;
- this.ModuleVersion = e.RxPacket.Buffer[35] != (byte) 0 ? e.RxPacket.Buffer[35] : (byte) 172;
- this.CC3200Version = e.RxPacket.Buffer[36] != (byte) 0 ? e.RxPacket.Buffer[36] : (byte) 1;
- this.Mode = (CWIFIAccess.WLANAccessMode) e.RxPacket.Buffer[37];
- this.DeviceDefaultPassord = Encoding.UTF8.GetString(e.RxPacket.Buffer, 48, 10);
- Array.Copy((Array) e.RxPacket.Buffer, 64, (Array) this.HUBAccessHash, 0, 32);
- this.DefineSSIDList(e);
- this._udpConnection.StopBroadcastMonitor();
- this._udpLocateSignal.Release();
- }
- private void DefineSSIDList(CBroadcastMessageReceivedEventArgs e)
- {
- this.NumberOfScannedSSIDs = e.RxPacket.Buffer[(int) byte.MaxValue];
- for (int index1 = 0; index1 < 10; ++index1)
- {
- CScannedSSID cscannedSsid;
- if (index1 < (int) this.NumberOfScannedSSIDs)
- {
- int index2 = 256 + 35 * index1;
- byte num = e.RxPacket.Buffer[index2 + 32];
- string name = Encoding.UTF8.GetString(e.RxPacket.Buffer, index2, (int) num);
- CScannedSSID.SECURITY security;
- switch (e.RxPacket.Buffer[index2 + 32 + 1])
- {
- case 1:
- security = CScannedSSID.SECURITY.WEP;
- break;
- case 2:
- security = CScannedSSID.SECURITY.WPA;
- break;
- case 3:
- security = CScannedSSID.SECURITY.WPA2;
- break;
- default:
- security = CScannedSSID.SECURITY.OPEN;
- break;
- }
- byte signalStrength = e.RxPacket.Buffer[index2 + 32 + 1 + 1];
- cscannedSsid = new CScannedSSID(name, security, signalStrength);
- }
- else
- cscannedSsid = new CScannedSSID("");
- this.ScannedSSID[index1] = cscannedSsid;
- }
- }
- public enum WLANAccessMode
- {
- AP_DEFAULT,
- AP_USER_CONFIGURED,
- STATION,
- }
- private enum TCP_CONNECT_STATE
- {
- CLOSED,
- OPENED,
- ACTIVE,
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment