Skip to content

Commit

Permalink
- Additional device network options for powering on via WOL
Browse files Browse the repository at this point in the history
  • Loading branch information
JPersson77 committed Jun 12, 2021
1 parent 090d7e9 commit b2f2fa9
Show file tree
Hide file tree
Showing 8 changed files with 233 additions and 26 deletions.
33 changes: 20 additions & 13 deletions LGTV Companion Service/Service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -695,46 +695,53 @@ void InitDeviceSessions()
json j;
if (item.key() == JSON_PREFS_NODE)
break;
string s;
stringstream s;

SESSIONPARAMETERS params;

params.DeviceId = item.key();
s += params.DeviceId; s += ", ";
s << params.DeviceId << ", ";
if (item.value()["Name"].is_string())
params.Name = item.value()["Name"].get<string>();
s += params.Name;
s += ", with IP ";
s << params.Name << ", with IP ";

if(item.value()["IP"].is_string())
params.IP = item.value()["IP"].get<string>();
s += params.IP; s += " initiated (";
s << params.IP << " initiated (";

if (item.value()["Enabled"].is_boolean())
params.Enabled = item.value()["Enabled"].get<bool>();
s += "Enabled:"; s += params.Enabled ? "yes" : "no"; s += ", ";

s << "Enabled:" << (params.Enabled?"yes":"no") << ", ";

if (item.value()["Subnet"].is_string())
params.Subnet = item.value()["Subnet"].get<string>();
if (item.value()["WOL"].is_number())
params.WOLtype = item.value()["WOL"].get<int>();
s << "WOL:" << params.WOLtype << ", ";
if (params.WOLtype == WOL_SUBNETBROADCAST && params.Subnet != "")
s << "SubnetMask:" << params.Subnet << ", ";
if(item.value()["SessionKey"].is_string())
params.SessionKey = item.value()["SessionKey"].get<string>();
s += "Pairing key:"; s += params.SessionKey =="" ? "n/a" : params.SessionKey; s += ", MAC: ";
s << "Pairing key:" << (params.SessionKey =="" ? "n/a" : params.SessionKey) << ", MAC: ";

j = item.value()["MAC"];
if (!j.empty() && j.size() > 0)
{
for (auto& m : j.items())
{
params.MAC.push_back(m.value().get<string>());
s += m.value().get<string>(); s += " ";
s << m.value().get<string>()<< " ";
}
s += ")";
s << ")";
}
else
s += "n/a )";
s << "n/a )";

params.PowerOnTimeout = Prefs.PowerOnTimeout;

CSession S(&params);
DeviceCtrlSessions.push_back(S);
Log(s);
Log(s.str());
}
return;
}
Expand Down
12 changes: 11 additions & 1 deletion LGTV Companion Service/Service.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <AccCtrl.h>
#include <sddl.h>
#include <Aclapi.h>
#include <WinSock2.h>

#include "nlohmann/json.hpp"
#include "Handshake.h"
Expand All @@ -31,7 +32,7 @@
#pragma comment(lib, "Advapi32.lib")

#define APPNAME L"LGTV Companion"
#define APPVERSION L"1.3.0"
#define APPVERSION L"1.4.0"
#define SVCNAME L"LGTVsvc"
#define SVCDISPLAYNAME L"LGTV Companion Service"
#define SERVICE_PORT "3000"
Expand Down Expand Up @@ -65,6 +66,13 @@
#define APP_CMDLINE_AUTOENABLE 3
#define APP_CMDLINE_AUTODISABLE 4

#define WOL_NETWORKBROADCAST 1
#define WOL_IPSEND 2
#define WOL_SUBNETBROADCAST 3

#define WOL_DEFAULTSUBNET L"255.255.255.0"


#define PIPENAME TEXT("\\\\.\\pipe\\LGTVyolo")


Expand Down Expand Up @@ -92,6 +100,8 @@ struct SESSIONPARAMETERS {
int PowerOnTimeout = 40;
std::string Name;
bool Enabled = true;
std::string Subnet;
int WOLtype = 1;
};

class CSession {
Expand Down
88 changes: 83 additions & 5 deletions LGTV Companion Service/Session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ void DisplayPowerOnThread(SESSIONPARAMETERS * CallingSessionParameters, bool * C
handshake.replace(ckf, ck.length(), key);
}

//try waking up the display ten times
//try waking up the display ten times, but not longer than timeout user preference
while (time(0) - origtim < (Timeout+1))
{
time_t looptim = time(0);
Expand Down Expand Up @@ -291,7 +291,11 @@ void WOLthread (SESSIONPARAMETERS* CallingSessionParameters, bool* CallingSessio
return;

vector<string> MACs = CallingSessionParameters->MAC;
string IP = CallingSessionParameters->IP;
string device = CallingSessionParameters->DeviceId;
string subnet = CallingSessionParameters->Subnet;
int WOLtype = CallingSessionParameters->WOLtype;

SOCKET WOLsocket = INVALID_SOCKET;
string logmsg;

Expand All @@ -301,10 +305,68 @@ void WOLthread (SESSIONPARAMETERS* CallingSessionParameters, bool* CallingSessio
struct sockaddr_in LANDestination {};
LANDestination.sin_family = AF_INET;
LANDestination.sin_port = htons(9);
LANDestination.sin_addr.s_addr = 0xFFFFFFFF;

stringstream wolstr;

if (WOLtype == WOL_SUBNETBROADCAST && subnet != "")
{
vector<string> vIP = stringsplit(IP, ".");
vector<string> vSubnet = stringsplit(subnet, ".");
stringstream broadcastaddress;

if (vIP.size() == 4 && vSubnet.size() == 4)
{
for (int i = 0; i < 4; i++)
{
int a = atoi(vIP[i].c_str());
int b = atoi(vSubnet[i].c_str());
int c = 256 + (a | (~b));

broadcastaddress << c;
if (i < 3)
broadcastaddress << ".";
}

stringstream ss;

wolstr << " using broadcast address: " << broadcastaddress.str();

LANDestination.sin_addr.s_addr = inet_addr(broadcastaddress.str().c_str());

}
else
{
stringstream ss;
ss << device;
ss << ", ERROR! WOLthread malformed subnet/IP";
Log(ss.str());
return;
}


LANDestination.sin_addr.s_addr = 0xFFFFFFFF;
}
else if (WOLtype == WOL_IPSEND)
{
LANDestination.sin_addr.s_addr = inet_addr(IP.c_str());
wolstr << " using IP address: " << IP;

}
else
{
LANDestination.sin_addr.s_addr = 0xFFFFFFFF;
wolstr << " using network broadcast: 255.255.255.255";
}

/* test test test
sockaddr_in host_interface;
host_interface.sin_family = AF_INET;
host_interface.sin_port = htons(0);
host_interface.sin_addr.s_addr = inet_addr("192.168.1.100");
*/
time_t origtim = time(0);

WOLsocket= socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
WOLsocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

if (WOLsocket == INVALID_SOCKET)
{
Expand All @@ -318,6 +380,20 @@ void WOLthread (SESSIONPARAMETERS* CallingSessionParameters, bool* CallingSessio
}
else
{
/* test test test
int iRes = ::bind(WOLsocket, (SOCKADDR*)&host_interface, sizeof(host_interface));
if ( iRes == SOCKET_ERROR)
{
closesocket(WOLsocket);
int dw = WSAGetLastError();
stringstream ss;
ss << device;
ss << ", ERROR! WOLthread WS bind(): ";
ss << dw;
Log(ss.str());
return;
}
*/
const bool optval = TRUE;
if (setsockopt(WOLsocket, SOL_SOCKET, SO_BROADCAST, (char*)&optval, sizeof(optval)) == SOCKET_ERROR)
{
Expand All @@ -336,6 +412,8 @@ void WOLthread (SESSIONPARAMETERS* CallingSessionParameters, bool* CallingSessio
logmsg = device;
logmsg += ", repeating WOL broadcast started to MAC: ";
logmsg += MAC;
if (wolstr.str() != "")
logmsg += wolstr.str();
Log(logmsg);

//remove filling from MAC
Expand Down Expand Up @@ -473,7 +551,7 @@ void DisplayPowerOffThread(SESSIONPARAMETERS* CallingSessionParameters, bool* Ca
host += ':' + std::to_string(ep.port());
if (time(0) - origtim > 10) // this thread should not run too long
{
Log("DisplayPowerOffThread() forced exit.");
Log("DisplayPowerOffThread() - forced exit");
goto threadoffend;
}
// Log("DEBUG INFO: DisplayPowerOffThread() setting options...");
Expand All @@ -488,7 +566,7 @@ void DisplayPowerOffThread(SESSIONPARAMETERS* CallingSessionParameters, bool* Ca
}));
if (time(0) - origtim > 10) // this thread should not run too long
{
Log("DisplayPowerOffThread() forced exit.");
Log("DisplayPowerOffThread() - forced exit");
goto threadoffend;
}

Expand Down
2 changes: 1 addition & 1 deletion LGTV Companion Setup/Product.wxs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- When publishing updated version, make sure to generate new GUID for "Product Id", and update "Version" -->
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi"><?define LGTV Companion Service_TargetDir=$(var.LGTV Companion Service.TargetDir)?><?define LGTV Companion UI_TargetDir=$(var.LGTV Companion UI.TargetDir)?>
<Product Id="33587CA8-2E58-4EFE-B8AB-43FB30FD0D5A" Name="LGTV Companion" Language="1033" Version="1.3.0" Manufacturer="J Persson" UpgradeCode="0BA17E5B-11CE-491D-B1A1-05DD2D9F610A">
<Product Id="A2C1A6C7-3CC8-4C3C-A3FD-DCE47F173ACE" Name="LGTV Companion" Language="1033" Version="1.4.0" Manufacturer="J Persson" UpgradeCode="0BA17E5B-11CE-491D-B1A1-05DD2D9F610A">
<Package Id="*" InstallerVersion="301" Compressed="yes" InstallScope="perMachine" Platform='x64' Description="LGTV Companion installer" InstallPrivileges="elevated" AdminImage="yes"/>
<Media Id="1" Cabinet="LGTVapp.cab" EmbedCab="yes" />

Expand Down
Loading

0 comments on commit b2f2fa9

Please sign in to comment.