[qnut] fixed main window show/hide on tray icon click event
[nut.git] / libnutcommon / macaddress.cpp
blobfd18813225671b7606c640113936cfc57ade943a
1 #include "macaddress.h"
3 namespace libnutcommon {
4 QDBusArgument &operator<< (QDBusArgument &argument, const MacAddress &data) {
5 argument << data.toString();
6 return argument;
8 const QDBusArgument &operator>> (const QDBusArgument &argument, MacAddress &data) {
9 QString addr;
10 argument >> addr;
11 data = MacAddress(addr);
12 return argument;
15 static inline int read_hexdigit(char c) {
16 if ('0' <= c && c <= '9')
17 return c - '0';
18 if ('a' <= c && c <= 'f')
19 return c - 'a' + 10;
20 if ('A' <= c && c <= 'F')
21 return c - 'A' + 10;
22 return -1;
25 static inline char* hex2quint8(char* msg, quint8 &val) {
26 int i;
27 val = 0;
28 if (!msg || !*msg) return msg;
29 if ((i = read_hexdigit(*msg)) == -1)
30 return msg;
31 msg++;
32 if (!*msg) return msg;
33 val = ((quint8) i) << 4;
34 if ((i = read_hexdigit(*msg)) == -1)
35 return msg;
36 msg++;
37 val |= i;
38 return msg;
41 MacAddress::MacAddress(const QString &str) {
42 if (str == QLatin1String("any")) {
43 memset(data, 0, sizeof(data));
44 return;
46 QByteArray buf = str.toUtf8();
47 char *s = buf.data();
48 // qDebug(s);
49 quint8 val;
50 char *s2;
51 for (int i = 0; i < 6; i++) {
52 if (!*s) {
53 // qDebug() << QString("Unexpected end of mac");
54 return;
56 if ((s2 = hex2quint8(s, val)) == s) {
57 // qDebug() << QString("No hexdata found at pos %1, '%2'").arg((int) (s - buf.data())).arg(*s);
58 return;
60 s = s2;
61 if (*s && *s++ != ':') {
62 // qDebug() << QString("Unexpected char in mac: '%1'").arg(*s);
63 return;
65 data[i] = val;
67 // qDebug() << QString("-> %1").arg(toString());
70 MacAddress::MacAddress(const quint8 *d) {
71 if (d == 0) {
72 MacAddress();
73 } else {
74 for (int i = 0; i < 6; i++)
75 data[i] = d[i];
78 MacAddress::MacAddress(const ether_addr * eth) {
79 data[0] = (quint8) eth->ether_addr_octet[0];
80 data[1] = (quint8) eth->ether_addr_octet[1];
81 data[2] = (quint8) eth->ether_addr_octet[2];
82 data[3] = (quint8) eth->ether_addr_octet[3];
83 data[4] = (quint8) eth->ether_addr_octet[4];
84 data[5] = (quint8) eth->ether_addr_octet[5];