~ubuntu-branches/ubuntu/intrepid/kdebluetooth/intrepid-proposed

« back to all changes in this revision

Viewing changes to kdebluetooth/libkbluetooth/servicerecord.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Mercatante
  • Date: 2008-08-07 09:49:47 UTC
  • mto: This revision was merged to the branch mainline in revision 56.
  • Revision ID: james.westby@ubuntu.com-20080807094947-pj6q3uxwuv7l844q
Tags: upstream-0.1
ImportĀ upstreamĀ versionĀ 0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *
3
 
 *  XML Service Record parser of BlueZ D-Bus API
4
 
 *
5
 
 *  Copyright (C) 2006  Daniel Gollub <dgollub@suse.de>
6
 
 *
7
 
 *
8
 
 *  This file is part of libkbluetooth.
9
 
 *
10
 
 *  libkbluetooth is free software; you can redistribute it and/or modify
11
 
 *  it under the terms of the GNU General Public License as published by
12
 
 *  the Free Software Foundation; either version 2 of the License, or
13
 
 *  (at your option) any later version.
14
 
 *
15
 
 *  libkbluetooth is distributed in the hope that it will be useful,
16
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 
 *  GNU General Public License for more details.
19
 
 *
20
 
 *  You should have received a copy of the GNU General Public License
21
 
 *  along with libkbluetooth; if not, write to the Free Software
22
 
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
23
 
 *
24
 
 */
25
 
 
26
 
#include "servicerecord.h"
27
 
#include <kdebug.h>
28
 
 
29
 
namespace KBluetooth
30
 
{
31
 
 
32
 
ServiceRecord::ServiceRecord(const QString &xml) : mXml(xml)
33
 
{
34
 
        QDomDocument doc;
35
 
 
36
 
        doc.setContent( xml );
37
 
 
38
 
        QDomElement docElement = doc.documentElement();
39
 
 
40
 
        for(QDomNode n = docElement.firstChild(); !n.isNull(); n = n.nextSibling() ) {
41
 
                QDomElement e = n.toElement();
42
 
 
43
 
                kdDebug() << e.text() << endl;
44
 
                
45
 
                if ( e.attribute("id") == "0x0001" ) {
46
 
                        mUuid = parseAttributeUuid(e);
47
 
                } else if ( e.attribute("id") == "0x0009" ) {
48
 
                        mServId = parseAttributeUuid(e);
49
 
                } else if ( e.attribute("id") == "0x0004" ) {
50
 
                        mRfcommChannel = parseAttributeRfcomm(e);
51
 
                } else if ( e.attribute("id") == "0x0100" ) {
52
 
                        mName = parseAttributeName(e);
53
 
                } else if ( e.attribute("id") == "0x0101" ) {
54
 
                        if (mTyp == "")
55
 
                                mTyp = parseAttributeName(e);
56
 
                }
57
 
 
58
 
        }
59
 
}
60
 
 
61
 
ServiceRecord::~ServiceRecord()
62
 
{
63
 
}
64
 
 
65
 
QString ServiceRecord::parseAttributeName(const QDomElement &e)
66
 
{       
67
 
        kdDebug() << k_funcinfo << mXml << endl;
68
 
        QDomNodeList list = e.elementsByTagName("text");
69
 
        QString serviceName = list.item(0).toElement().attribute("value");
70
 
        if (list.item(0).toElement().attribute("encoding") == "hex") {
71
 
                QString localValue = list.item(0).toElement().attribute("value");
72
 
                QString a = localValue.mid(0, 2);
73
 
                bool ok;
74
 
 
75
 
                serviceName.insert(0, (char) a.toInt(&ok, 16));
76
 
                for (uint i=2; i < localValue.length(); i = i + 2) {
77
 
                        a = localValue.mid(i, 2);
78
 
                        serviceName.insert(i/2, (char) a.toInt(&ok, 16));
79
 
                }
80
 
                serviceName.setLength(localValue.length()/2 - 1);
81
 
        }
82
 
 
83
 
        kdDebug() << "serviceName: " << serviceName << endl;
84
 
        return serviceName;
85
 
}
86
 
 
87
 
QStringList ServiceRecord::parseAttributeUuid(const QDomElement &e)
88
 
{
89
 
        QStringList uuid;
90
 
 
91
 
        QDomNodeList list = e.elementsByTagName("sequence").item(0).toElement().elementsByTagName("uuid");
92
 
        for (uint i=0;i <list.count(); ++i) {
93
 
                QString id = list.item(i).toElement().attribute("value");
94
 
                uuid.append( id );
95
 
        }
96
 
 
97
 
        return uuid;
98
 
}
99
 
 
100
 
int ServiceRecord::parseAttributeRfcomm(const QDomElement &e)
101
 
{
102
 
        int rfcommChannelValue = -1;
103
 
 
104
 
        QDomNodeList list = e.elementsByTagName("sequence");
105
 
        for (uint i=0;i <list.count(); ++i) {
106
 
                QDomNode m;
107
 
                for( m = list.item(i).firstChild(); !m.isNull(); m = m.nextSibling() ) {
108
 
                        QDomNodeList list = e.elementsByTagName("uuid");
109
 
                        bool isRfcomm = false;
110
 
                        for (uint i=0;i <list.count(); ++i) {
111
 
                                if (list.item(i).toElement().tagName() == "uuid"
112
 
                                                && list.item(i).toElement().attribute("value") == "0x0003")
113
 
                                        isRfcomm = true;
114
 
                        }
115
 
 
116
 
                        if (!isRfcomm)
117
 
                                break;
118
 
 
119
 
                        // get RFCOMM Channel
120
 
                        list = e.elementsByTagName("uint8");
121
 
                        for (uint i=0;i <list.count(); ++i) {
122
 
                                rfcommChannelValue = list.item(i).toElement().attribute("value").toInt(0, 16);
123
 
                        }
124
 
                }
125
 
        }
126
 
 
127
 
        return rfcommChannelValue;
128
 
}
129
 
 
130
 
 
131
 
}