1 #include "macaddress.h"
3 namespace libnutcommon
{
4 QDBusArgument
&operator<< (QDBusArgument
&argument
, const MacAddress
&data
) {
5 argument
<< data
.toString();
8 const QDBusArgument
&operator>> (const QDBusArgument
&argument
, MacAddress
&data
) {
11 data
= MacAddress(addr
);
15 static inline int read_hexdigit(char c
) {
16 if ('0' <= c
&& c
<= '9')
18 if ('a' <= c
&& c
<= 'f')
20 if ('A' <= c
&& c
<= 'F')
25 static inline char* hex2quint8(char* msg
, quint8
&val
) {
28 if (!msg
|| !*msg
) return msg
;
29 if ((i
= read_hexdigit(*msg
)) == -1)
32 if (!*msg
) return msg
;
33 val
= ((quint8
) i
) << 4;
34 if ((i
= read_hexdigit(*msg
)) == -1)
41 MacAddress::MacAddress(const QString
&str
) {
42 if (str
== QLatin1String("any")) {
43 memset(data
, 0, sizeof(data
));
46 QByteArray buf
= str
.toUtf8();
51 for (int i
= 0; i
< 6; i
++) {
53 // qDebug() << QString("Unexpected end of mac");
56 if ((s2
= hex2quint8(s
, val
)) == s
) {
57 // qDebug() << QString("No hexdata found at pos %1, '%2'").arg((int) (s - buf.data())).arg(*s);
61 if (*s
&& *s
++ != ':') {
62 // qDebug() << QString("Unexpected char in mac: '%1'").arg(*s);
67 // qDebug() << QString("-> %1").arg(toString());
70 MacAddress::MacAddress(const quint8
*d
) {
74 for (int i
= 0; i
< 6; 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];