Skip to content

Commit

Permalink
wake-on-lan command line version finished (as well as gui version) #303
Browse files Browse the repository at this point in the history
  • Loading branch information
dannagle committed Oct 29, 2023
1 parent 206ef20 commit b7aa16a
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 23 deletions.
38 changes: 24 additions & 14 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,8 @@ int main(int argc, char *argv[])
"name");
parser.addOption(nameOption);

QCommandLineOption wakeOption(QStringList() << "wake" << "Send Wake-On-LAN / Magic Packet to <mac> and <optional port>.");
parser.addOption(wakeOption);
QCommandLineOption wolOption(QStringList() << "wol", "Send Wake-On-LAN / Magic Packet to <mac> and (optional) <port>.", "mac");
parser.addOption(wolOption);



Expand Down Expand Up @@ -328,7 +328,7 @@ int main(int argc, char *argv[])
bool ipv4 = parser.isSet(bindIPv4Option);
bool http = parser.isSet(httpOption);

bool wake = parser.isSet(wakeOption);
bool wol = parser.isSet(wolOption);

bool okbps = false;
bool okrate = false;
Expand Down Expand Up @@ -372,22 +372,32 @@ int main(int argc, char *argv[])
QString data, dataString;
data.clear();
dataString.clear();
if (argssize >= 1) {
address = args[0];
}

if(wake) {
if (argssize >= 2) {
port = QString(args[1]).toUInt();
if(wol) {
QString targetMAC = parser.value(wolOption).trimmed().toUpper();
if (argssize >= 1) {
port = QString(args[0]).toUInt();
}
if(port < 1) {
port = 7;
}

Packet wakePkt = Packet::generateWakeOnLAN(address, port);
OUTIF() << wakePkt.asciiString();
OUTPUT()
return 0;
Packet wolPkt = Packet::generateWakeOnLAN(targetMAC, port);

if(wolPkt.errorString.isEmpty()) {
OUTIF() << "Sending broadcast Wake-On-LAN to target: " + targetMAC + " on port " + QString::number(port);
udp = true;
tcp = false;
ssl = false;
http = false;
address = wolPkt.toIP;
data = wolPkt.hexString;

} else {
OUTIF() << "Error generating Wake-On-LAN: " + wolPkt.errorString;
OUTPUT();
return -1;
}

}


Expand Down
27 changes: 27 additions & 0 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2788,21 +2788,48 @@ void MainWindow::on_udptcpComboBox_currentIndexChanged(int index)

void MainWindow::on_actionWake_On_LAN_Magic_Packet_triggered()
{
static QString previousMAC = "";
static int previousPortIndex = 0;
WakeOnLAN wol = WakeOnLAN();
wol.setTarget(previousMAC, previousPortIndex);
wol.setModal(true);
wol.exec();
if(!wol.generatedPacket.errorString.isEmpty()) {

QMessageBox msgBox;
msgBox.setWindowIcon(QIcon(":pslogo.png"));
msgBox.setWindowTitle(tr("Invalid MAC Address."));
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setDefaultButton(QMessageBox::Ok);
msgBox.setIcon(QMessageBox::Critical);
msgBox.setText(wol.generatedPacket.errorString);
msgBox.setModal(true);
msgBox.exec();

on_actionWake_On_LAN_Magic_Packet_triggered();
return;
}

if(wol.generatedPacket.toIP.isEmpty()) {
return;
}

previousMAC = wol.mac;
previousPortIndex = wol.portIndex;

ui->packetIPEdit->setText(wol.generatedPacket.toIP);
ui->packetPortEdit->setText(QString::number(wol.generatedPacket.port));
int findtext = ui->udptcpComboBox->findText(wol.generatedPacket.tcpOrUdp);
if (findtext > -1) {
ui->udptcpComboBox->setCurrentIndex(findtext);
}
ui->packetHexEdit->setText(wol.generatedPacket.hexString);
on_packetHexEdit_editingFinished();
on_udptcpComboBox_currentIndexChanged("");

if(ui->packetNameEdit->text().isEmpty()) {
ui->packetNameEdit->setFocus();
}

}

20 changes: 17 additions & 3 deletions src/packet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ void Packet::saveToDB()

}

Packet Packet::generateWakeOnLAN(QString mac, int port)
Packet Packet::generateWakeOnLAN(QString &mac, int port)
{
/*
* The Wake-On-LAN / Magic Packet format is as follows:
Expand All @@ -620,8 +620,22 @@ Packet Packet::generateWakeOnLAN(QString mac, int port)
}

QByteArray macBytes = HEXtoByteArray(mac);
macBytes.truncate(6);
wakeBytes.append(macBytes);

if(macBytes.size() != 6) {
QDEBUG() << "Received an invalid MAC address" << mac;
wakeOnLAN.init();
wakeOnLAN.errorString = "Received an invalid MAC address";
return wakeOnLAN;
}

// Corrected mac format
QString correctedMAC = Packet::byteArrayToHex(macBytes).trimmed();
correctedMAC.replace(" ", ":");
mac = correctedMAC;

for(int i=0; i<16; i++) {
wakeBytes.append(macBytes);
}

wakeOnLAN.hexString = byteArrayToHex(wakeBytes);

Expand Down
2 changes: 1 addition & 1 deletion src/packet.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class Packet

void saveToDB();

static Packet generateWakeOnLAN(QString mac, int port);
static Packet generateWakeOnLAN(QString &mac, int port);

static Packet fetchFromDB(QString thename);
static QList<Packet> fetchAllfromDB(QString importFile);
Expand Down
17 changes: 16 additions & 1 deletion src/wakeonlan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,31 @@ WakeOnLAN::WakeOnLAN(QWidget *parent) :
ui(new Ui::WakeOnLAN)
{
ui->setupUi(this);

mac.clear();
portIndex = 0;


}


WakeOnLAN::~WakeOnLAN()
{
delete ui;
}

void WakeOnLAN::setTarget(QString mac, int portIndex)
{
ui->macEdit->setText(mac);
ui->portBox->setCurrentIndex(portIndex);

}

void WakeOnLAN::on_buttonBox_accepted()
{
generatedPacket = Packet::generateWakeOnLAN(ui->macEdit->text(), ui->portBox->currentText().toUInt());
mac = ui->macEdit->text().trimmed().toUpper();
portIndex = ui->portBox->currentIndex();
generatedPacket = Packet::generateWakeOnLAN(mac, ui->portBox->currentText().toUInt());

}

3 changes: 3 additions & 0 deletions src/wakeonlan.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ class WakeOnLAN : public QDialog
public:
explicit WakeOnLAN(QWidget *parent = nullptr);
~WakeOnLAN();
void setTarget(QString mac, int portIndex);
Packet generatedPacket;
QString mac;
int portIndex;

private slots:
void on_buttonBox_accepted();
Expand Down
33 changes: 29 additions & 4 deletions src/wakeonlan.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<width>454</width>
<height>143</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
<string>Wake-On-LAN</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>MAC</string>
<string>Target MAC</string>
</property>
</widget>
</item>
Expand All @@ -39,15 +39,38 @@
</item>
<item>
<widget class="QComboBox" name="portBox">
<property name="enabled">
<bool>true</bool>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>80</width>
<height>20</height>
</size>
</property>
<item>
<property name="text">
<string>7</string>
</property>
<property name="icon">
<iconset resource="packetsender.qrc">
<normaloff>:/icons/outline_lightbulb_white_24dp.png</normaloff>:/icons/outline_lightbulb_white_24dp.png</iconset>
</property>
</item>
<item>
<property name="text">
<string>9</string>
</property>
<property name="icon">
<iconset resource="packetsender.qrc">
<normaloff>:/icons/outline_lightbulb_white_24dp.png</normaloff>:/icons/outline_lightbulb_white_24dp.png</iconset>
</property>
</item>
</widget>
</item>
Expand All @@ -65,7 +88,9 @@
</item>
</layout>
</widget>
<resources/>
<resources>
<include location="packetsender.qrc"/>
</resources>
<connections>
<connection>
<sender>buttonBox</sender>
Expand Down

0 comments on commit b7aa16a

Please sign in to comment.