summaryrefslogtreecommitdiff
path: root/noncore/settings/networksettings2/opietooth2/OTDeviceAddress.cpp
blob: 7fbfaf4f8ed0c960a4129c6ec78742a2f4b6b0eb (plain)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//-*-c++-*-
/***************************************************************************
 *   Copyright (C) 2003 by Fred Schaettgen                                 *
 *   kdebluetooth@schaettgen.de                                            *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/

#include <string.h>
#include <bluezlib.h>
#include <opie2/odebug.h>

#include <OTDeviceAddress.h>

using namespace Opietooth2;

//const bdaddr_t OTDeviceAddress::bdaddr_any = {{0,0,0,0,0,0}};
//const bdaddr_t OTDeviceAddress::bdaddr_local = {{0, 0, 0, 0xff, 0xff, 0xff}};

const OTDeviceAddress OTDeviceAddress::invalid = 
                OTDeviceAddress();
const OTDeviceAddress OTDeviceAddress::any = 
                OTDeviceAddress("00:00:00:00:00:00");
const OTDeviceAddress OTDeviceAddress::local =  
                OTDeviceAddress("00:00:00:FF:FF:FF");

OTDeviceAddress::OTDeviceAddress() {

    IsValid = false;
    memset( &BDaddr, 0, sizeof( BDaddr ) );
}

OTDeviceAddress::OTDeviceAddress( const bdaddr_t& bdaddr, 
                                  bool networkbyteorder) {
    setBDAddr( bdaddr, networkbyteorder );
}

OTDeviceAddress::OTDeviceAddress(const QString& s) {

    bdaddr_t a;
    int ret = str2ba(s.latin1(), &a);
    if (ret==0) {
        IsValid = true;
        bacpy( &BDaddr, &a );
    } else {
        IsValid = false;
        bacpy( &BDaddr, &(OTDeviceAddress::invalid.getBDAddr()) ) ;
    }
}

QString OTDeviceAddress::toString() const {
    char addrbuf[20];
    ba2str(&BDaddr, addrbuf);
    return QString(addrbuf);

}

void OTDeviceAddress::setBDAddr( const bdaddr_t& bdaddr, 
                                 bool networkbyteorder) {
    if (networkbyteorder) {
        baswap(&BDaddr, &bdaddr);
    } else {
        bacpy( &BDaddr, &bdaddr );
    }
    IsValid = true;
}

bdaddr_t OTDeviceAddress::getBDAddrInNetworkByteOrder(void) const {
    bdaddr_t ret;
    baswap(&ret, &BDaddr);
    return ret;
}

bool OTDeviceAddress::operator==(const OTDeviceAddress& b) const {
    if ( ! IsValid && ! b.IsValid )
        return true;
    return memcmp( &BDaddr, &(b.BDaddr), sizeof( b.BDaddr ) ) == 0;
}

bool OTDeviceAddress::operator<(const OTDeviceAddress& b) const {
    if ( ! IsValid &&  ! b.IsValid )
        return false;
    else if ( ! IsValid && b.IsValid )
        return false;
    else if ( IsValid && ! b.IsValid )
        return true;

    if (BDaddr.b[5] != b.BDaddr.b[5]) 
        return (BDaddr.b[5] < b.BDaddr.b[5]);

    if (BDaddr.b[4] != b.BDaddr.b[4]) 
        return (BDaddr.b[4] < b.BDaddr.b[4]);

    if (BDaddr.b[3] != b.BDaddr.b[3]) 
        return (BDaddr.b[3] < b.BDaddr.b[3]);

    if (BDaddr.b[2] != b.BDaddr.b[2]) 
        return (BDaddr.b[2] < b.BDaddr.b[2]);

    if (BDaddr.b[1] != b.BDaddr.b[1]) 
        return (BDaddr.b[1] < b.BDaddr.b[1]);

    if (BDaddr.b[0] != b.BDaddr.b[0]) 
        return (BDaddr.b[0] < b.BDaddr.b[0]);

    return false;
}