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

OTUUID.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 
00018 using namespace Opietooth2;
00019 
00020 OTUUID::OTUUID(QString s) {
00021         fromString(s);
00022 }
00023 
00024 OTUUID::OTUUID(uint64_t l, uint64_t h) {
00025         this->hi = h;
00026         this->lo = l;
00027 }
00028 
00029 OTUUID::OTUUID( const OTUUID & O ) {
00030         this->hi = O.hi;
00031         this->lo = O.lo;
00032 }
00033 
00034 OTUUID & OTUUID::operator =( const OTUUID & O ) {
00035         hi = O.hi;
00036         lo = O.lo;
00037         return *this;
00038 }
00039 
00040 bool OTUUID::fromString(QString s) {
00041 
00042     // Strip leading "0x"
00043     if (s.startsWith("0x"))
00044     {
00045         s = s.right(s.length()-2);
00046     }
00047     // Remove separators
00048     s.replace( QRegExp(":"), "" );
00049 
00050     bool bOk = false;
00051     if( s.length() == 4 || s.length() == 8) {
00052 
00053         uint32_t u32 = s.toUInt(&bOk, 16);
00054         setUUID32(u32);
00055         return bOk;
00056 
00057     } else if (s.length() == 32) {
00058 
00059 #if (QT_VERSION >= 0x030200)
00060         uint64_t u64hi = s.left(16).toULongLong(&bOk, 16);
00061 #else
00062         uint64_t u64hi = s.left(16).toULong(&bOk, 16);
00063 #endif
00064         if (!bOk)
00065             return false;
00066 
00067 #if (QT_VERSION >= 0x030200)
00068         uint64_t u64lo = s.right(16).toULongLong(&bOk, 16);
00069 #else
00070         uint64_t u64lo = s.right(16).toULong(&bOk, 16);
00071 #endif
00072         if (!bOk)
00073             return false;
00074         hi = u64hi;
00075         lo = u64lo;
00076         return true;
00077 
00078     } else {
00079         hi = 0;
00080         lo = 0;
00081         return true;
00082     }
00083 }
00084 
00085 void OTUUID::setUUID32(uint32_t v) {
00086 
00087     hi = uint64_t(0x1000) | (uint64_t(v) << 32);
00088     // see BT 1.1 Core spec p2.7.1
00089     lo = (uint64_t(0x80000080) << 32) | uint64_t(0x5F9B34FB);
00090 
00091 }
00092 
00093 void OTUUID::setUUID128(uint64_t _hi, uint64_t _lo) {
00094     hi = _hi;
00095     lo = _lo;
00096 }
00097 
00098 QString OTUUID::toString() const {
00099     QString ret;
00100     uint32_t v1 = uint32_t(hi >> 32);
00101     uint32_t v2 = uint32_t(hi & 0xFFFFFFFF);
00102     uint32_t v3 = uint32_t(lo >> 32);
00103     uint32_t v4 = uint32_t(lo & 0xFFFFFFFF);
00104     ret.sprintf("0x%08lx:%08lx:%08lx:%08lx",
00105                      (unsigned long)v1, (unsigned long)v2,
00106                      (unsigned long)v3, (unsigned long)v4);
00107 
00108     return ret;
00109 }
00110 
00111 OTUUID::operator QString() const {
00112     return toString();
00113 }
00114 
00115 OTUUID::operator ::uuid_t() const {
00116 
00117     //kdDebug() << "uuid_t() -> " << QString(*this) << endl;
00118     ::uuid_t ret;
00119     if ((lo == (uint64_t(0x80000080) << 32) | uint64_t(0x5F9B34FB)) &&
00120         ((hi&0xFFFFFFFF) == 0x1000)) {
00121         uint32_t uuid32val = uint32_t(hi >> 32);    
00122         if (uuid32val > 0xFFFF) {
00123             ret.type = SDP_UUID16;
00124             ret.value.uuid16 = uint16_t(uuid32val);
00125         } else {
00126             ret.type = SDP_UUID32;
00127             ret.value.uuid32 = uuid32val;
00128         }
00129     } else {
00130         ret.value.uuid128.data[0] = (lo >> 0) && 0xFF;
00131         ret.value.uuid128.data[1] = (lo >> 8) && 0xFF;
00132         ret.value.uuid128.data[2] = (lo >> 16) && 0xFF;
00133         ret.value.uuid128.data[3] = (lo >> 24) && 0xFF;
00134         ret.value.uuid128.data[4] = (lo >> 32) && 0xFF;
00135         ret.value.uuid128.data[5] = (lo >> 40) && 0xFF;
00136         ret.value.uuid128.data[6] = (lo >> 48) && 0xFF;
00137         ret.value.uuid128.data[7] = (lo >> 56) && 0xFF;
00138     
00139         ret.value.uuid128.data[8] = (hi >> 0) && 0xFF;
00140         ret.value.uuid128.data[9] = (hi >> 8) && 0xFF;
00141         ret.value.uuid128.data[10] = (hi >> 16) && 0xFF;
00142         ret.value.uuid128.data[11] = (hi >> 24) && 0xFF;
00143         ret.value.uuid128.data[12] = (hi >> 32) && 0xFF;
00144         ret.value.uuid128.data[13] = (hi >> 40) && 0xFF;
00145         ret.value.uuid128.data[14] = (hi >> 48) && 0xFF;
00146         ret.value.uuid128.data[15] = (hi >> 56) && 0xFF;
00147     
00148         ret.type = SDP_UUID128;
00149     }
00150     return ret;
00151 }
00152 
00153 bool OTUUID::operator<(const OTUUID & other) const {
00154     if (hi != other.hi) 
00155        return hi < other.hi;
00156 
00157     return lo<other.lo;
00158 }
00159 
00160 bool OTUUID::operator ==(const OTUUID& u) const {
00161     return (u.hi == hi) && (u.lo == lo);
00162 }

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