00001 /*************************************************************************** 00002 * Copyright (C) 2003 by Fred Schaettgen * 00003 * kdebluetooth@schaettgen.de * 00004 * * 00005 * This program is free software; you can redistribute it and/or modify * 00006 * it under the terms of the GNU General Public License as published by * 00007 * the Free Software Foundation; either version 2 of the License, or * 00008 * (at your option) any later version. * 00009 ***************************************************************************/ 00010 00011 #include <opie2/odebug.h> 00012 00013 #include <OTSDPAttribute.h> 00014 #include <OTSDPService.h> 00015 00016 using namespace Opietooth2; 00017 00018 OTSDPService::OTSDPService() : attributeList() { 00019 } 00020 00021 OTSDPService::~OTSDPService() { 00022 for (unsigned int n=0; n < attributeList.count(); ++n) { 00023 delete attributeList[n].attr; 00024 } 00025 } 00026 00027 void OTSDPService::addAttribute(int id, OTSDPAttribute * attr) { 00028 attributeList.resize( attributeList.size() + 1 ); 00029 00030 attributeList[attributeList.size() - 1].id = id; 00031 attributeList[attributeList.size() - 1].attr = attr; 00032 } 00033 00034 const OTSDPAttribute & OTSDPService::attribute(int index) { 00035 return *(attributeList[index].attr); 00036 } 00037 00038 int OTSDPService::attributeID(int index) { 00039 return attributeList[index].id; 00040 00041 } 00042 00043 OTSDPAttribute * OTSDPService::attributeByID(int id) { 00044 00045 for (unsigned int n=0; n < attributeList.count(); ++n) { 00046 if (attributeList[n].id == id) { 00047 return attributeList[n].attr; 00048 } 00049 } 00050 return 0; 00051 } 00052 00053 bool OTSDPService::recordHandle(uint32_t *handle) { 00054 OTSDPAttribute * attrib; 00055 attrib = attributeByID(0x00); 00056 if( attrib && attrib->getType() == OTSDPAttribute::UINT) { 00057 *handle = (uint32_t)attrib->getUInt().lo; 00058 return true; 00059 } 00060 return false; 00061 } 00062 00063 QString OTSDPService::name(void) { 00064 QString S; 00065 OTSDPAttribute * attrib; 00066 attrib = attributeByID(0x100); 00067 if( attrib && attrib->getType() == OTSDPAttribute::STRING) { 00068 S = attrib->getString(); 00069 } 00070 return S; 00071 } 00072 00073 QString OTSDPService::description() { 00074 QString S; 00075 OTSDPAttribute * attrib; 00076 attrib = attributeByID(0x101); 00077 if ( attrib && attrib->getType() == OTSDPAttribute::STRING) { 00078 S = attrib->getString(); 00079 } 00080 return S; 00081 } 00082 00083 UUIDVector OTSDPService::allUUIDs() { 00084 UUIDVector uuidList; 00085 00086 for ( unsigned int i = 0; 00087 i < attributeList.count(); 00088 i ++ ) { 00089 int os; 00090 UUIDVector subList = attributeList[i].attr->getAllUUIDs(); 00091 os = uuidList.size(); 00092 uuidList.resize( uuidList.size()+subList.count() ); 00093 00094 for( unsigned int k = 0; k < subList.count(); k++ ) { 00095 uuidList[os + k] = subList[k]; 00096 } 00097 } 00098 return uuidList; 00099 } 00100 00101 bool OTSDPService::rfcommChannel(unsigned int &n) { 00102 // Get the rfcomm channel 00103 OTSDPAttribute * protoDescAttr; 00104 // Get the the protocol descriptor list attribute (0x04) 00105 protoDescAttr = attributeByID(0x04); 00106 if( ! protoDescAttr ) { 00107 return false; 00108 } 00109 00110 AttributeVector & protoDescList = *(protoDescAttr->getSequence()); 00111 00112 for( unsigned int i = 0; 00113 i < protoDescList.count(); 00114 i ++ ) { 00115 AttributeVector & attrList = *(protoDescList[i]->getSequence()); 00116 00117 //The List must have at least 2 Attributes 00118 //Example: 00119 // UUID16 : 0x0003 - RFCOMM 00120 // Channel/Port (Integer) : 0x6 00121 if(attrList.size() >= 2) { 00122 // The first Attribute of the list must be an UUID 00123 if( attrList[0]->getType() != OTSDPAttribute::UUID) 00124 continue; 00125 // The UUID must have the value of "0x0003" that's the RFCOMM UUID 00126 OTUUID rfcommUUID( "0x0003" ); 00127 if( attrList[0]->getUUID() != rfcommUUID) //RFCOMM UUID 00128 continue; 00129 //If the UUID is ok we get the rfcomm channel number 00130 if( attrList[1]->getType() != OTSDPAttribute::UINT) 00131 continue; 00132 00133 n = attrList[1]->getUInt().lo; 00134 00135 return true; 00136 } 00137 } 00138 // If we're here, we haven't found a correct Rfcomm channel, so we return false 00139 return false; 00140 } 00141 00142 bool OTSDPService::hasClassID(const OTUUID & uuid) { 00143 OTSDPAttribute * ClassIDAttr; 00144 00145 // Get the the ClassID descriptor list attribute (0x01) 00146 ClassIDAttr = attributeByID( 0x01); 00147 if( ! ClassIDAttr ) { 00148 return false; 00149 } 00150 00151 AttributeVector & ClassIDList = *(ClassIDAttr->getSequence()); 00152 for( unsigned int i = 0 ; 00153 i < ClassIDList.count() ; 00154 i ++ ) { 00155 if( ClassIDList[i]->getType() != OTSDPAttribute::UUID) 00156 continue; 00157 if( ClassIDList[i]->getUUID() == uuid) 00158 return true; 00159 } 00160 return false; 00161 } 00162 00164 UUIDVector OTSDPService::classIDList( void ) { 00165 00166 UUIDVector uuidList; 00167 OTSDPAttribute * ClassIDAttr; 00168 00169 // Get the the ClassID descriptor list attribute (0x01) 00170 ClassIDAttr = attributeByID( 0x01); 00171 if( ! ClassIDAttr ) { 00172 return uuidList; 00173 } 00174 00175 AttributeVector & ClassIDList = *(ClassIDAttr->getSequence()); 00176 00177 for( unsigned int i = 0 ; 00178 i < ClassIDList.count() ; 00179 i ++ ) { 00180 if( ClassIDList[i]->getType() != OTSDPAttribute::UUID) 00181 continue; 00182 00183 uuidList.resize( uuidList.size() + 1 ); 00184 uuidList[ uuidList.size() - 1 ] = ClassIDList[i]->getUUID(); 00185 } 00186 00187 return uuidList; 00188 } 00189
1.4.2