Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

OTSDPAttribute.cpp

Go to the documentation of this file.
00001 //-*-c++-*-
00002 /***************************************************************************
00003  *   Copyright (C) 2003 by Fred Schaettgen                                 *
00004  *   kdebluetooth@schaettgen.de                                            *
00005  *                                                                         *
00006  *   This program is free software; you can redistribute it and/or modify  *
00007  *   it under the terms of the GNU General Public License as published by  *
00008  *   the Free Software Foundation; either version 2 of the License, or     *
00009  *   (at your option) any later version.                                   *
00010  ***************************************************************************/
00011 
00012 #include <assert.h>
00013 #include <qregexp.h>
00014 #include <opie2/odebug.h>
00015 
00016 #include <OTUUID.h>
00017 #include <OTSDPAttribute.h>
00018 
00019 using namespace Opietooth2;
00020 
00021 OTSDPAttribute::OTSDPAttribute() {
00022     type = INVALID;
00023     memset( &Value, 0, sizeof( Value )  );
00024 }
00025 
00026 OTSDPAttribute::~OTSDPAttribute() {
00027     if( type == INT ) {
00028       delete Value.intVal;
00029     } else if( type == UUID ) {
00030       delete Value.uuidVal;
00031     } else if( type == UINT ) {
00032       delete Value.uintVal;
00033     } else if( type == STRING ||
00034                type == URL
00035              ) {
00036       delete Value.stringVal;
00037     } else if( type == ALTERNATIVE ||
00038                type == SEQUENCE
00039              ) {
00040       delete Value.sequenceVal;
00041     }
00042 }
00043 
00044 OTSDPAttribute::OTSDPAttribute( sdp_data_t * attrib ) {
00045     setNil();
00046     switch( attrib->dtd ) {
00047       case SDP_DATA_NIL: // Nil type
00048         { setNil();
00049           break;
00050         }
00051       case SDP_UINT8: // Unsigned integer
00052         setUInt(attrib->val.uint8);
00053         break;
00054       case SDP_UINT16: // Unsigned integer
00055         setUInt(attrib->val.uint16);
00056         break;
00057       case SDP_UINT32: // Unsigned integer
00058         setUInt(attrib->val.uint32);
00059         break;
00060       case SDP_UINT64: // Unsigned integer
00061         setUInt(attrib->val.uint64);
00062         break;
00063       case SDP_UINT128: // Unsigned integer
00064         // setUInt(attrib->val.uint16);
00065         assert(false); // BUG/TODO: uint128 integers not supported
00066         break;
00067       case SDP_INT8: // Unsigned integer
00068         setInt(attrib->val.int8);
00069         break;
00070       case SDP_INT16: // Unsigned integer
00071         setInt(attrib->val.int16);
00072         break;
00073       case SDP_INT32: // Unsigned integer
00074         setInt(attrib->val.int32);
00075         break;
00076       case SDP_INT64: // Unsigned integer
00077         setInt(attrib->val.int64);
00078         break;
00079       case SDP_INT128: // Unsigned integer
00080         // newAttr.setInt(attrib->val.uint16);
00081         assert(false); // BUG/TODO: uint128 integers not supported
00082         break;
00083       case SDP_UUID16:
00084         { OTUUID id;
00085           ::uuid_t uuidVal = attrib->val.uuid;
00086           id.setUUID32(uuidVal.value.uuid16);
00087           setUUID(id );
00088         }
00089         break;
00090       case SDP_UUID32:
00091         { OTUUID id;
00092           ::uuid_t uuidVal = attrib->val.uuid;
00093           id.setUUID32(uuidVal.value.uuid32);
00094           setUUID(id );
00095         }
00096         break;
00097       case SDP_UUID128:
00098         { OTUUID id;
00099           ::uuid_t uuidVal = attrib->val.uuid;
00100           uint64_t* v128;
00101           v128 = reinterpret_cast<uint64_t*>(&(uuidVal.value.uuid128));
00102           id.setUUID128(v128[0], v128[1]);
00103           setUUID(id );
00104         }
00105         break;
00106       case SDP_TEXT_STR_UNSPEC :
00107       case SDP_TEXT_STR8 :
00108       case SDP_TEXT_STR16 :
00109       case SDP_TEXT_STR32 :
00110         setString( QString(attrib->val.str) );
00111         break;
00112       case SDP_URL_STR_UNSPEC :
00113       case SDP_URL_STR8 :
00114       case SDP_URL_STR16 :
00115       case SDP_URL_STR32 :
00116         setURL( QString(attrib->val.str) );
00117         break;
00118       case SDP_BOOL:
00119         setBool( attrib->val.int8 != 0);
00120         break;
00121       case SDP_SEQ_UNSPEC :
00122       case SDP_SEQ8 :
00123       case SDP_SEQ16 :
00124       case SDP_SEQ32 :
00125       case SDP_ALT_UNSPEC :
00126       case SDP_ALT8 :
00127       case SDP_ALT16 :
00128       case SDP_ALT32 :
00129         { AttributeVector subAttribs;
00130           OTSDPAttribute * Attr;
00131           sdp_data_t* subAttrib = attrib->val.dataseq;
00132 
00133           for (; subAttrib; subAttrib = subAttrib->next) {
00134 
00135             Attr = new OTSDPAttribute(subAttrib);
00136             subAttribs.resize( subAttribs.size() + 1 );
00137             subAttribs.insert( subAttribs.size() - 1, Attr );
00138           }
00139 
00140           if( attrib->dtd == SDP_ALT_UNSPEC  ||
00141               attrib->dtd == SDP_ALT8 ||
00142               attrib->dtd == SDP_ALT16 ||
00143               attrib->dtd == SDP_ALT32 ) {
00144             setAlternative(subAttribs);
00145           } else {
00146             setSequence(subAttribs);
00147           }
00148           break;
00149         }
00150     } // end case
00151 }
00152 
00153 QString OTSDPAttribute::toString( void ) {
00154     QString S;
00155     switch( type ) {
00156       case INVALID :
00157         S = "invalid";
00158         break;
00159       case NIL :
00160         S = "NIL";
00161         break;
00162       case UINT :
00163         S = Value.uintVal->toString();
00164         break;
00165       case INT :
00166         S = Value.intVal->toString();
00167         break;
00168       case UUID :
00169         S = Value.uuidVal->toString();
00170         break;
00171       case BOOLEAN :
00172         S = (Value.boolVal) ? "true" : "false";
00173         break;
00174       case STRING :
00175         S = *(Value.stringVal);
00176         break;
00177       case URL :
00178         S = *(Value.stringVal);
00179         break;
00180       case SEQUENCE :
00181         S.sprintf( "Sequence(%d)", Value.sequenceVal->count() );
00182         break;
00183       case ALTERNATIVE :
00184         S.sprintf( "Alternative(%d)", Value.sequenceVal->count() );
00185         break;
00186       case UNKNOWN :
00187         S = "unknown";
00188         break;
00189     }
00190     return S;
00191 }
00192 
00193 void OTSDPAttribute::setNil() {
00194     type = NIL;
00195 }
00196 
00197 void OTSDPAttribute::setInt(const OTSDPAttribute::int128_t & val) {
00198     type = INT;
00199     Value.intVal = new int128_t( val );
00200 }
00201 
00202 void OTSDPAttribute::setUInt(const uint128_t & val) {
00203     type = UINT;
00204     Value.uintVal = new uint128_t(val);
00205 }
00206 
00207 void OTSDPAttribute::setUUID(const OTUUID & val) {
00208     type = UUID;
00209     Value.uuidVal = new OTUUID( val );
00210 }
00211 
00212 void OTSDPAttribute::setBool(bool val) {
00213     type = BOOLEAN;
00214     Value.boolVal = val;
00215 }
00216 
00217 void OTSDPAttribute::setString( const QString & val) {
00218     type = STRING;
00219     Value.stringVal = new QString( val );
00220 }
00221 
00222 void OTSDPAttribute::setURL( const QString & val) {
00223     type = URL;
00224     Value.stringVal = new QString(val);
00225 }
00226 
00227 void OTSDPAttribute::setSequence(const AttributeVector& val) {
00228     type = SEQUENCE;
00229     Value.sequenceVal = new AttributeVector();
00230     Value.sequenceVal->setAutoDelete( TRUE );
00231     *Value.sequenceVal = val;
00232 }
00233 
00234 void OTSDPAttribute::setAlternative(const AttributeVector& val) {
00235     type = ALTERNATIVE;
00236     Value.sequenceVal = new AttributeVector();
00237     Value.sequenceVal->setAutoDelete( TRUE );
00238     *Value.sequenceVal = val;
00239 }
00240 
00241 QString OTSDPAttribute::getString() {
00242     assert(type == STRING);
00243     return *Value.stringVal;
00244 }
00245 
00246 QString OTSDPAttribute::getURL() {
00247     assert(type == URL);
00248     return *Value.stringVal;
00249 }
00250 
00251 const OTSDPAttribute::int128_t & OTSDPAttribute::getInt() {
00252     assert(type == INT);
00253     return *Value.intVal;
00254 }
00255 
00256 
00257 const OTSDPAttribute::uint128_t & OTSDPAttribute::getUInt() {
00258     assert(type == UINT);
00259     return *Value.uintVal;
00260 }
00261 
00262 const OTUUID & OTSDPAttribute::getUUID() {
00263     assert(type == UUID);
00264     return *Value.uuidVal;
00265 }
00266 
00267 bool OTSDPAttribute::getBool() {
00268     assert(type == BOOLEAN);
00269     return Value.boolVal;
00270 }
00271 
00272 AttributeVector * OTSDPAttribute::getSequence() {
00273     assert(type == SEQUENCE);
00274     return Value.sequenceVal;
00275 }
00276 
00277 AttributeVector * OTSDPAttribute::getAlternative() {
00278     assert(type == ALTERNATIVE);
00279     return Value.sequenceVal;
00280 }
00281 
00282 UUIDVector OTSDPAttribute::getAllUUIDs() {
00283 
00284     UUIDVector uuids;
00285 
00286     if (getType() == UUID) {
00287       uuids.resize( uuids.size()+1 );
00288       uuids[uuids.size()-1] = getUUID();
00289     } else {
00290       AttributeVector * subAttributes = 0 ;
00291 
00292       if (getType() == SEQUENCE) {
00293           subAttributes = getSequence();
00294       } else if (getType() == ALTERNATIVE) {
00295           subAttributes = getAlternative();
00296       }
00297 
00298       int os;
00299       for( unsigned int i = 0; i < subAttributes->count(); i++ ) {
00300         UUIDVector subUUIDs = (*subAttributes)[i]->getAllUUIDs();
00301 
00302         os = uuids.size();
00303         uuids.resize( uuids.size()+subUUIDs.count() );
00304 
00305         for( unsigned int k = 0; k < subUUIDs.count(); k++ ) {
00306           uuids[os + k] = subUUIDs[k];
00307         }
00308       }
00309     }
00310     return uuids;
00311 }
00312 
00313 static char * Attr2String[] = {
00314         "Invalid",
00315         "Nil",
00316         "UInt",
00317         "int",
00318         "UUID",
00319         "Boolean",
00320         "String",
00321         "Sequence",
00322         "Alternative",
00323         "URL",
00324         "Unknown"
00325 };
00326 
00327 const char * OTSDPAttribute::getTypeString() {
00328     return Attr2String[type];
00329 }

Generated on Sat Nov 5 16:17:55 2005 for OPIE by  doxygen 1.4.2