add alternate WOL function

This commit is contained in:
Vincent Maas committed 2021-02-07 10:45:53 +01:00
1 parent 5268e6679e
commit fd51bcca99
7 files changed
+139 -27

No files matched your search

+1 -1
View File
@@ -27,7 +27,7 @@
<ProductName>ColorControl</ProductName>
<PublisherName>Maassoft</PublisherName>
<ApplicationRevision>0</ApplicationRevision>
<ApplicationVersion>1.5.4.0</ApplicationVersion>
<ApplicationVersion>1.5.5.0</ApplicationVersion>
<UseApplicationTrust>false</UseApplicationTrust>
<PublishWizardCompleted>true</PublishWizardCompleted>
<BootstrapperEnabled>true</BootstrapperEnabled>
+1 -5
View File
@@ -1,17 +1,13 @@
using LgTv;
using Newtonsoft.Json;
using NLog.Config;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
using System.Windows.Forms;
using Windows.Data.Json;
namespace ColorControl
{
@@ -392,7 +388,7 @@ namespace ColorControl
macAddress = macAddress == null ? SelectedDevice?.MacAddress : macAddress;
if (macAddress != null)
{
WOL.WakeFunction(macAddress);
WOL.WakeFunction(macAddress, !Config.UseAlternateWol);
_justWokeUp = true;
}
else
+2
View File
@@ -16,6 +16,8 @@
public string DeviceSearchKey { get; set; }
public bool UseAlternateWol { get; set; }
public LgServiceConfig()
{
PowerOnDelayAfterResume = 5000;
+30 -16
View File
@@ -42,6 +42,7 @@
this.btnChange = new System.Windows.Forms.Button();
this.mnuNvPresets = new System.Windows.Forms.ContextMenuStrip(this.components);
this.miNvApply = new System.Windows.Forms.ToolStripMenuItem();
this.miNvPresetApplyOnStartup = new System.Windows.Forms.ToolStripMenuItem();
this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
this.mnuNvDisplay = new System.Windows.Forms.ToolStripMenuItem();
this.miNvPrimaryDisplay = new System.Windows.Forms.ToolStripMenuItem();
@@ -133,7 +134,7 @@
this.lbPlugins = new System.Windows.Forms.ListBox();
this.label7 = new System.Windows.Forms.Label();
this.lblInfo = new System.Windows.Forms.Label();
this.miNvPresetApplyOnStartup = new System.Windows.Forms.ToolStripMenuItem();
this.chkLgAlternateWolMechanism = new System.Windows.Forms.CheckBox();
this.tcMain.SuspendLayout();
this.tabNVIDIA.SuspendLayout();
this.mnuNvPresets.SuspendLayout();
@@ -298,27 +299,35 @@
this.miNvPresetDithering,
this.miNvHDR});
this.mnuNvPresets.Name = "mnuNvPresets";
this.mnuNvPresets.Size = new System.Drawing.Size(181, 186);
this.mnuNvPresets.Size = new System.Drawing.Size(163, 164);
this.mnuNvPresets.Opening += new System.ComponentModel.CancelEventHandler(this.mnuNvPresets_Opening);
//
// miNvApply
//
this.miNvApply.Name = "miNvApply";
this.miNvApply.Size = new System.Drawing.Size(180, 22);
this.miNvApply.Size = new System.Drawing.Size(162, 22);
this.miNvApply.Text = "Apply";
this.miNvApply.Click += new System.EventHandler(this.miNvApply_Click);
//
// miNvPresetApplyOnStartup
//
this.miNvPresetApplyOnStartup.CheckOnClick = true;
this.miNvPresetApplyOnStartup.Name = "miNvPresetApplyOnStartup";
this.miNvPresetApplyOnStartup.Size = new System.Drawing.Size(162, 22);
this.miNvPresetApplyOnStartup.Text = "Apply on startup";
this.miNvPresetApplyOnStartup.Click += new System.EventHandler(this.miNvPresetApplyOnStartup_Click);
//
// toolStripSeparator1
//
this.toolStripSeparator1.Name = "toolStripSeparator1";
this.toolStripSeparator1.Size = new System.Drawing.Size(177, 6);
this.toolStripSeparator1.Size = new System.Drawing.Size(159, 6);
//
// mnuNvDisplay
//
this.mnuNvDisplay.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.miNvPrimaryDisplay});
this.mnuNvDisplay.Name = "mnuNvDisplay";
this.mnuNvDisplay.Size = new System.Drawing.Size(180, 22);
this.mnuNvDisplay.Size = new System.Drawing.Size(162, 22);
this.mnuNvDisplay.Text = "Display";
//
// miNvPrimaryDisplay
@@ -333,7 +342,7 @@
this.mnuNvPresetsColorSettings.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.miNvPresetColorSettings});
this.mnuNvPresetsColorSettings.Name = "mnuNvPresetsColorSettings";
this.mnuNvPresetsColorSettings.Size = new System.Drawing.Size(180, 22);
this.mnuNvPresetsColorSettings.Size = new System.Drawing.Size(162, 22);
this.mnuNvPresetsColorSettings.Text = "Color settings";
//
// miNvPresetColorSettings
@@ -348,7 +357,7 @@
this.mnuRefreshRate.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.miRefreshRateIncluded});
this.mnuRefreshRate.Name = "mnuRefreshRate";
this.mnuRefreshRate.Size = new System.Drawing.Size(180, 22);
this.mnuRefreshRate.Size = new System.Drawing.Size(162, 22);
this.mnuRefreshRate.Text = "Refresh Rate";
//
// miRefreshRateIncluded
@@ -366,7 +375,7 @@
this.mnuNvDitheringBitDepth,
this.mnuNvDitheringMode});
this.miNvPresetDithering.Name = "miNvPresetDithering";
this.miNvPresetDithering.Size = new System.Drawing.Size(180, 22);
this.miNvPresetDithering.Size = new System.Drawing.Size(162, 22);
this.miNvPresetDithering.Text = "Dithering";
//
// miNvPresetApplyDithering
@@ -476,7 +485,7 @@
this.miToggleHDR,
this.miHDREnabled});
this.miNvHDR.Name = "miNvHDR";
this.miNvHDR.Size = new System.Drawing.Size(180, 22);
this.miNvHDR.Size = new System.Drawing.Size(162, 22);
this.miNvHDR.Text = "HDR";
//
// miHDRIncluded
@@ -1023,6 +1032,7 @@
//
// grpLGOptions
//
this.grpLGOptions.Controls.Add(this.chkLgAlternateWolMechanism);
this.grpLGOptions.Controls.Add(this.btnLGTestPower);
this.grpLGOptions.Controls.Add(this.label9);
this.grpLGOptions.Controls.Add(this.edtLgPowerOnAfterResumeDelay);
@@ -1062,7 +1072,7 @@
0});
this.edtLgPowerOnAfterResumeDelay.Location = new System.Drawing.Point(299, 17);
this.edtLgPowerOnAfterResumeDelay.Maximum = new decimal(new int[] {
5000,
10000,
0,
0,
0});
@@ -1287,13 +1297,16 @@
this.lblInfo.TabIndex = 1;
this.lblInfo.Text = "Info";
//
// miNvPresetApplyOnStartup
// chkLgAlternateWolMechanism
//
this.miNvPresetApplyOnStartup.CheckOnClick = true;
this.miNvPresetApplyOnStartup.Name = "miNvPresetApplyOnStartup";
this.miNvPresetApplyOnStartup.Size = new System.Drawing.Size(180, 22);
this.miNvPresetApplyOnStartup.Text = "Apply on startup";
this.miNvPresetApplyOnStartup.Click += new System.EventHandler(this.miNvPresetApplyOnStartup_Click);
this.chkLgAlternateWolMechanism.AutoSize = true;
this.chkLgAlternateWolMechanism.Location = new System.Drawing.Point(158, 72);
this.chkLgAlternateWolMechanism.Name = "chkLgAlternateWolMechanism";
this.chkLgAlternateWolMechanism.Size = new System.Drawing.Size(173, 17);
this.chkLgAlternateWolMechanism.TabIndex = 7;
this.chkLgAlternateWolMechanism.Text = "Use alternate WOL mechanism";
this.chkLgAlternateWolMechanism.UseVisualStyleBackColor = true;
this.chkLgAlternateWolMechanism.CheckedChanged += new System.EventHandler(this.chkLgAlternateWolMechanism_CheckedChanged);
//
// MainForm
//
@@ -1449,6 +1462,7 @@
private System.Windows.Forms.ToolStripMenuItem temporalToolStripMenuItem;
private System.Windows.Forms.Button btnLGRemoteControl;
private System.Windows.Forms.ToolStripMenuItem miNvPresetApplyOnStartup;
private System.Windows.Forms.CheckBox chkLgAlternateWolMechanism;
}
}
+8
View File
@@ -175,6 +175,7 @@ namespace ColorControl
clbLgPower.SetItemChecked(3, _lgService.Config.PowerOffOnStandby);
edtLgPowerOnAfterResumeDelay.Value = _lgService.Config.PowerOnDelayAfterResume;
edtLgDeviceFilter.Text = _lgService.Config.DeviceSearchKey;
chkLgAlternateWolMechanism.Checked = _lgService.Config.UseAlternateWol;
}
catch (Exception e)
{
@@ -1886,6 +1887,8 @@ See Options to test this functionality."
private void btnLGTestPower_Click(object sender, EventArgs e)
{
//_lgService.WakeSelectedDevice();
//return;
var text =
@"The TV will now power off. Please wait for the TV to be powered off completely (relay click) and press ENTER to wake it again.
For waking up to work, you need to activate the following setting on the TV:
@@ -2072,5 +2075,10 @@ Do you want to continue?";
AddOrUpdateItem();
}
private void chkLgAlternateWolMechanism_CheckedChanged(object sender, EventArgs e)
{
_lgService.Config.UseAlternateWol = chkLgAlternateWolMechanism.Checked;
}
}
}
+2 -2
View File
@@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.5.4.0")]
[assembly: AssemblyFileVersion("1.5.4.0")]
[assembly: AssemblyVersion("1.5.5.0")]
[assembly: AssemblyFileVersion("1.5.5.0")]
+95 -3
View File
@@ -5,20 +5,112 @@ using System.Net.NetworkInformation;
using SharpPcap;
using SharpPcap.Npcap;
using System.Net;
using System.Net.Sockets;
namespace ColorControl
{
class WOL
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private const string ArgumentExceptionInvalidMacAddressLength = "Invalid MAC address length.";
private const string ArgumentExceptionInvalidPasswordLength = "Invalid password length.";
private const int DefaultWolPort = 9;
public static void WakeFunction2(string macAddress)
public static void WakeFunction(string macAddress, bool usePcap = false)
{
//PhysicalAddress.Parse(macAddress).SendWol();
if (usePcap)
{
WakeFunctionPcap(macAddress);
}
else
{
WakeFunctionToAllNics(macAddress);
}
}
public static void WakeFunctionToAllNics(string macAddress)
{
try
{
var address = PhysicalAddress.Parse(macAddress);
var addressBytes = address.GetAddressBytes();
var data = GetWolPacket(addressBytes);
foreach (var ni in NetworkInterface.GetAllNetworkInterfaces())
{
Logger.Debug($"Found network: {ni.Name} ({ni.Description})");
try
{
if (/*ni.OperationalStatus == OperationalStatus.Up &&*/ ni.SupportsMulticast && ni.GetIPProperties().GetIPv4Properties() != null)
{
var id = ni.GetIPProperties().GetIPv4Properties().Index;
if (NetworkInterface.LoopbackInterfaceIndex != id)
{
foreach (var uip in ni.GetIPProperties().UnicastAddresses)
{
if (uip.Address.AddressFamily == AddressFamily.InterNetwork)
{
Logger.Debug($"Broadcast WOL in network: {ni.Name} ({ni.Description}), local address: {uip.Address}");
var local = new IPEndPoint(uip.Address, 0);
var udpc = new UdpClient(local);
udpc.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
udpc.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontRoute, 1);
var target = new IPEndPoint(IPAddress.Broadcast, DefaultWolPort);
udpc.Send(data, data.Length, target);
}
}
}
}
}
catch (Exception ex)
{
Logger.Error($"WakeFunctionToAllNics: while sending to specific network: {ni.Name} ({ni.Description}): {ex.ToLogString()}");
}
}
}
catch (Exception ex)
{
Logger.Error($"WakeFunctionToAllNics: {ex.ToLogString()}");
}
}
/// <exception cref="ArgumentNullException"><paramref name="macAddress"/> is null.</exception>
/// <exception cref="ArgumentException">The length of the <see cref="T:System.Byte" /> array <paramref name="macAddress"/> is not 6.</exception>
/// <exception cref="ArgumentException">The length of the <see cref="T:System.Byte" /> array <paramref name="password"/> is not 0 or 6.</exception>
private static byte[] GetWolPacket(byte[] macAddress, byte[] password = null)
{
if (macAddress == null)
throw new ArgumentNullException(nameof(macAddress));
if (macAddress.Length != 6)
throw new ArgumentException(ArgumentExceptionInvalidMacAddressLength);
password = password ?? new byte[0];
if (password.Length != 0 && password.Length != 6)
throw new ArgumentException(ArgumentExceptionInvalidPasswordLength);
var packet = new byte[17 * 6 + password.Length];
int offset, i;
for (offset = 0; offset < 6; ++offset)
packet[offset] = 0xFF;
for (offset = 6; offset < 17 * 6; offset += 6)
for (i = 0; i < 6; ++i)
packet[i + offset] = macAddress[i];
if (password.Length > 0)
{
for (offset = 16 * 6 + 6; offset < (17 * 6 + password.Length); offset += 6)
for (i = 0; i < 6; ++i)
packet[i + offset] = password[i];
}
return packet;
}
//You need SharpPcap for this to work
public static void WakeFunction(string macAddress)
private static void WakeFunctionPcap(string macAddress)
{
/* Retrieve the device list */
var devices = CaptureDeviceList.Instance;