summaryrefslogtreecommitdiff
path: root/noncore/settings/networksettings2/opietooth2/OTSDPService.cpp
blob: 07b505ce153928433f4fc09917c31f72c33a872f (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/***************************************************************************
 *   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 <opie2/odebug.h>

#include <OTSDPAttribute.h>
#include <OTSDPService.h>

using namespace Opietooth2;

OTSDPService::OTSDPService() : attributeList() {
}

OTSDPService::~OTSDPService() {
    for (unsigned int n=0; n < attributeList.count(); ++n) {
      delete attributeList[n].attr;
    }
}

void OTSDPService::addAttribute(int id, OTSDPAttribute * attr) {
    attributeList.resize( attributeList.size() + 1 );

    attributeList[attributeList.size() - 1].id = id;
    attributeList[attributeList.size() - 1].attr = attr;
}

const OTSDPAttribute & OTSDPService::attribute(int index) {
    return *(attributeList[index].attr);
}

int OTSDPService::attributeID(int index) {
    return attributeList[index].id;

}

OTSDPAttribute * OTSDPService::attributeByID(int id) {

    for (unsigned int n=0; n < attributeList.count(); ++n) {
      if (attributeList[n].id == id) {
        return attributeList[n].attr;
      }
    }
    return 0;
}

bool OTSDPService::recordHandle(uint32_t *handle) {
    OTSDPAttribute * attrib;
    attrib = attributeByID(0x00);
    if( attrib && attrib->getType() == OTSDPAttribute::UINT) {
      *handle = (uint32_t)attrib->getUInt().lo;
      return true;
    }
    return false;
}

QString OTSDPService::name(void) {
    QString S;
    OTSDPAttribute * attrib;
    attrib = attributeByID(0x100);
    if( attrib && attrib->getType() == OTSDPAttribute::STRING) {
      S = attrib->getString();
    }
    return S;
}

QString OTSDPService::description() {
    QString S;
    OTSDPAttribute * attrib;
    attrib = attributeByID(0x101);
    if ( attrib && attrib->getType() == OTSDPAttribute::STRING) {
      S = attrib->getString();
    }
    return S;
}

UUIDVector OTSDPService::allUUIDs() {
    UUIDVector uuidList;

    for ( unsigned int i = 0;
          i < attributeList.count();
          i ++ ) {
      int os;
      UUIDVector subList = attributeList[i].attr->getAllUUIDs();
      os = uuidList.size();
      uuidList.resize( uuidList.size()+subList.count() );

      for( unsigned int k = 0; k < subList.count(); k++ ) {
        uuidList[os + k] = subList[k];
      }
    }
    return uuidList;
}

bool OTSDPService::rfcommChannel(unsigned int &n) {
    // Get the rfcomm channel
    OTSDPAttribute * protoDescAttr;
    // Get the the protocol descriptor list attribute (0x04)
    protoDescAttr = attributeByID(0x04);
    if( ! protoDescAttr ) {
        return false;
    }

    AttributeVector & protoDescList = *(protoDescAttr->getSequence());

    for( unsigned int i = 0;
         i < protoDescList.count();
         i ++ ) {
      AttributeVector & attrList = *(protoDescList[i]->getSequence());

      //The List must have at least 2 Attributes
      //Example:
      //  UUID16 : 0x0003 - RFCOMM
      //  Channel/Port (Integer) : 0x6
      if(attrList.size() >= 2) {
        // The first Attribute of the list must be an UUID
        if( attrList[0]->getType() != OTSDPAttribute::UUID)
          continue;
        // The UUID must have the value of "0x0003" that's the RFCOMM UUID
        OTUUID rfcommUUID( "0x0003" );
        if( attrList[0]->getUUID() != rfcommUUID) //RFCOMM UUID
          continue;
        //If the UUID is ok we get the rfcomm channel number
        if( attrList[1]->getType() != OTSDPAttribute::UINT)
          continue;

        n = attrList[1]->getUInt().lo;

        return true;
      }
    }
	// If we're here, we haven't found a correct Rfcomm channel, so we return false
    return false;
}

bool OTSDPService::hasClassID(const OTUUID & uuid) { 
    OTSDPAttribute * ClassIDAttr;

    // Get the the ClassID descriptor list attribute (0x01)
    ClassIDAttr = attributeByID( 0x01);
    if( ! ClassIDAttr ) {
      return false;
    }

    AttributeVector & ClassIDList = *(ClassIDAttr->getSequence());
    for( unsigned int i = 0 ;
         i < ClassIDList.count() ;
         i ++ ) {
      if( ClassIDList[i]->getType() != OTSDPAttribute::UUID)
          continue;
      if( ClassIDList[i]->getUUID() == uuid)
        return true;
    }
    return false;
}

/** Get a vector of UUID of the services class Id List */
UUIDVector OTSDPService::classIDList( void ) {

    UUIDVector uuidList;
    OTSDPAttribute * ClassIDAttr;

    // Get the the ClassID descriptor list attribute (0x01)
    ClassIDAttr = attributeByID( 0x01);
    if( ! ClassIDAttr ) {
      return uuidList;
    }

    AttributeVector & ClassIDList = *(ClassIDAttr->getSequence());

    for( unsigned int i = 0 ;
         i < ClassIDList.count() ;
         i ++ ) {
        if( ClassIDList[i]->getType() != OTSDPAttribute::UUID)
            continue;

        uuidList.resize( uuidList.size() + 1 );
        uuidList[ uuidList.size() - 1 ] = ClassIDList[i]->getUUID();
    }

    return uuidList;
}