summaryrefslogtreecommitdiff
path: root/noncore/settings/networksettings2/opietooth2/OTDeviceAddress.h
blob: 8395f37369ab81aa0fbe7a00fb2fefb21ed62494 (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
//-*-c++-*-
/***************************************************************************
 *   Copyright (C) 2003 by Fred Schaettgen                                 *
 *   kdebluetooth@0xF.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.                                   *
 ***************************************************************************/

#ifndef OTDEVICEADDRESS_H
#define OTDEVICEADDRESS_H

#include <qstring.h>
#include <bluezlib.h>

namespace Opietooth2 {

/** Represents a Bluetooth device address.
 * This class allows easy conversion of Bluetooth addresses
 * from and to strings. It also works with the bdaddr_t type
 * used by BlueZ.
 * The class offers a comparsion and less-than operator, so
 * that it can be used a key in an STL map or similar.
 * The string conversion functions of BlueZ are used internally,
 * so the string format is the same than for BlueZ's ba2str().
 */
class OTDeviceAddress {

public:

    /** Default constructor.
    The resulting address is equal to DeviceAddress::invalid.
    */
    OTDeviceAddress();

    /** Initialize the object with a BlueZ bdaddr_t.
    @param bdaddr address
    @param networkbyteorder if true, bdaddr is assumed to be in
    network byte order and converted to host byte order first.
    */
    explicit OTDeviceAddress( const bdaddr_t& bdaddr,
                              bool networkbyteorder = false
                            );

    /** Initializes the object with an address given as a string.
    The string must be in a format which is understood by the
    BlueZ str2ba function, like 00:61:58:4C:E6:AD. Case doesn't matter.
    @param s address string
    */
    explicit OTDeviceAddress(const QString& s);

    /** convert the address to a string.
    @return address string
    */
    QString toString() const;

    /** Converts the DeviceAddress into a BlueZ bdaddr_t.
    @param networkbyteorder if true, the address is returned
        in network byte order.
    @return the address as bdaddr_t
    */
    inline const bdaddr_t & getBDAddr( void ) const
      { return BDaddr; }
    bdaddr_t getBDAddrInNetworkByteOrder() const;
    void setBDAddr( const bdaddr_t& bdaddr,
                    bool networkbyteorder = false
                  );

    /** Less-than-operator.
    Mainly there to use DeviceAddress inside STL containers,
    like STL sets or as a key in a STL map.
    @param b the DeviceAddress to compare to (right hand side)
    @return true if this < b, false otherwise.
    */
    bool operator<(const OTDeviceAddress& b) const;

    /** Equality operator.
    Tests two device addresses for equality.
    @param b the DeviceAddress to compare to (right hand side)
    @return true if this and b have the same address or
    if both are invalid, false otherwise
    */
    bool operator==(const OTDeviceAddress& b) const;

    inline bool operator!=(const OTDeviceAddress& b) const
      { return ! ( *this == b ); }

    /** The address 00:00:00:FF:FF:FF */
    static const OTDeviceAddress local;

    /** The address 00:00:00:00:00:00 */
    static const OTDeviceAddress any;

    /** An address tagged as invalid */
    static const OTDeviceAddress invalid;

protected:

    bdaddr_t BDaddr;
    bool     IsValid;

};

}

#endif