|
1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 #include <e32base.h> |
|
17 #include <random.h> |
|
18 #include <e32math.h> |
|
19 |
|
20 #include "upnpuuid.h" |
|
21 #include "upnpserverconstants.h" |
|
22 |
|
23 |
|
24 TInt UpnpUuid::MakeUuid(TUpnpUuid& aUuid, TUuidVersion aVersion) |
|
25 /** |
|
26 * Generate a UUID of the specified version. |
|
27 * NOTE. As of this writing, only version 4 |
|
28 * (ERandomBased) is supported |
|
29 **/ |
|
30 { |
|
31 // We currently support only version 4 |
|
32 if (aVersion != ERandomBased) |
|
33 { |
|
34 return KErrNotSupported; |
|
35 } |
|
36 |
|
37 /* Fill UUID with random data */ |
|
38 TRAPD(result, RandomizeL(aUuid)); |
|
39 |
|
40 if (result == KErrNone) |
|
41 { |
|
42 /* Brand UUID with version and variant */ |
|
43 Brand(aUuid, aVersion); |
|
44 } |
|
45 |
|
46 return result; |
|
47 } |
|
48 |
|
49 void UpnpUuid::RandomizeL(TUpnpUuid& aUuid) |
|
50 /** |
|
51 * Fill the UUID with pseudo-random data |
|
52 */ |
|
53 { |
|
54 // Delegate the task to the Symbian Random Number Generator |
|
55 // (we trust that it is the best alternative available) |
|
56 |
|
57 TPckg<TUpnpUuid> pckgUuid(aUuid); |
|
58 TRandom::RandomL(pckgUuid); |
|
59 } |
|
60 |
|
61 void UpnpUuid::Brand(TUpnpUuid& aUuid, TUuidVersion aVersion) |
|
62 /** |
|
63 * Set variant and version information to the UUID |
|
64 */ |
|
65 { |
|
66 // We currently support only UUID version 4 |
|
67 if (aVersion != ERandomBased) |
|
68 { |
|
69 return; |
|
70 } |
|
71 |
|
72 // First set the variant information to value: |
|
73 // Msb0 Msb1 Msb2 |
|
74 // 1 0 x |
|
75 // as specified in draft-mealling-uuid-urn-05.txt |
|
76 // (a variable number of the most significant bits |
|
77 // of the eight octet of the UUID, i.e. the |
|
78 // clock_seq_hi_and_reserved octet) |
|
79 aUuid.iClockSeqHiAndReserved &= 0x3F; // Set Msb0 and Msb1 to 0, take the rest "as-is" |
|
80 aUuid.iClockSeqHiAndReserved |= (1 << 7); // Set Msb0 to 1 |
|
81 |
|
82 // Then set the version number to indicate version 4 |
|
83 // Msb0 Msb1 Msb2 Msb3 |
|
84 // 0 1 0 0 |
|
85 // as specified in draft-mealling-uuid-urn-05.txt |
|
86 // (in the most significant four bits of the time |
|
87 // stamp) |
|
88 aUuid.iTimeHiAndVersion &= 0x0FFF; // Set Msb0-Msb3 to 0, take the rest "as-is" |
|
89 aUuid.iTimeHiAndVersion |= (1 << 14); // Set Msb1 to 1 |
|
90 } |
|
91 |
|
92 void UpnpUuid::GenerateUuidL(RBuf8& aUuid) |
|
93 { |
|
94 // uuid generation |
|
95 TUpnpUuid uuid; |
|
96 TUuidString8 uuidString; |
|
97 UpnpUuid::MakeUuid(uuid); |
|
98 |
|
99 TTime now; |
|
100 now.UniversalTime(); |
|
101 TInt64 randSeed = now.Int64(); |
|
102 TInt randomNum = Math::Rand(randSeed); |
|
103 uuid.iTimeLow = static_cast<TUint32>(randomNum); |
|
104 UpnpUuid::UuidToString(uuid, uuidString); |
|
105 aUuid.CreateL(KUuidString.iTypeLength+KUuidStringLen); |
|
106 aUuid.Copy(KUuidString); |
|
107 aUuid.Append(uuidString); |
|
108 } |
|
109 |
|
110 void UpnpUuid::UuidToString(const TUpnpUuid& aUuid, TUuidString8& aUuidString) |
|
111 /** |
|
112 * Create and return a string representation of the given UUID. |
|
113 * E.g. f81d4fae-7dec-11d0-a765-00a0c91e6bf6 |
|
114 */ |
|
115 { |
|
116 aUuidString.Format(KUuidFormatString8, |
|
117 static_cast<TUint>(aUuid.iTimeLow), |
|
118 static_cast<TUint>(aUuid.iTimeMid), |
|
119 static_cast<TUint>(aUuid.iTimeHiAndVersion), |
|
120 static_cast<TUint>(aUuid.iClockSeqHiAndReserved), |
|
121 static_cast<TUint>(aUuid.iClockSeqLow), |
|
122 static_cast<TUint>(aUuid.iNode[0]), |
|
123 static_cast<TUint>(aUuid.iNode[1]), |
|
124 static_cast<TUint>(aUuid.iNode[2]), |
|
125 static_cast<TUint>(aUuid.iNode[3]), |
|
126 static_cast<TUint>(aUuid.iNode[4]), |
|
127 static_cast<TUint>(aUuid.iNode[5])); |
|
128 } |
|
129 |
|
130 |