-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqmlwakeonlan.cpp
47 lines (36 loc) · 1.26 KB
/
qmlwakeonlan.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "qmlwakeonlan.h"
QmlWakeOnLan::QmlWakeOnLan(QObject *parent) :
QObject(parent)
{
this->udpSocket = new QUdpSocket(this);
}
bool QmlWakeOnLan::sendMagicPacket(QString macAddress)
{
if (!this->isValidMacAddress(macAddress)) {
this->error = QObject::tr("Keine gültige MAC-Adresseee: %1").arg(macAddress);
return false;
}
QByteArray macDest = this->cleanMac(macAddress).toLocal8Bit();
QByteArray magicSequence = QByteArray::fromHex("ffffffffffff");
for (int i=0; i<16; i++)
magicSequence.append(QByteArray::fromHex(macDest));
qint64 byteCount = this->udpSocket->writeDatagram(magicSequence.data(), magicSequence.size(), QHostAddress::Broadcast, 40000);
if (byteCount == -1)
this->error = QObject::tr("Magisches Paket konnte nicht gesendet werden");
return (byteCount > -1);
}
bool QmlWakeOnLan::isValidMacAddress(QString macAddress)
{
QRegExp r("^([0-9a-f]{2}([:-]|$)){6}$", Qt::CaseInsensitive);
return r.exactMatch(macAddress);
}
QString QmlWakeOnLan::getError()
{
return this->error;
}
QString QmlWakeOnLan::cleanMac(QString macAddress)
{
if (!this->isValidMacAddress(macAddress))
return QString::null;
return macAddress.replace(QRegExp("[:-]", Qt::CaseInsensitive), "");
}