|
1 // Copyright (c) 2004-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 /** |
|
17 @file |
|
18 @internalComponent |
|
19 */ |
|
20 |
|
21 #include "bttypes.h" |
|
22 |
|
23 enum TBTDevAddrPanics |
|
24 { |
|
25 EBTDevAddrBadDescriptor = 0, |
|
26 EBTDevAddrBadTInt64 = 1, |
|
27 }; |
|
28 |
|
29 void Panic(TBTDevAddrPanics aCode) |
|
30 { |
|
31 _LIT(KPanicName, "BTDevAddr"); |
|
32 User::Panic(KPanicName, aCode); |
|
33 } |
|
34 |
|
35 /** |
|
36 Default constructor. |
|
37 The address is initialised to 0. |
|
38 */ |
|
39 EXPORT_C TBTDevAddr::TBTDevAddr() |
|
40 { |
|
41 Reset(); |
|
42 } |
|
43 |
|
44 /** |
|
45 Constructs a device address from a data buffer. |
|
46 |
|
47 The buffer is copied directly into the object. The function panics if aDes |
|
48 does not have a length of 6 bytes (48 bits). |
|
49 |
|
50 @param aDes Data buffer for device address |
|
51 */ |
|
52 EXPORT_C TBTDevAddr::TBTDevAddr(const TDesC8 &aDes) |
|
53 { |
|
54 __ASSERT_ALWAYS(aDes.Length() == KBTDevAddrSize, Panic(EBTDevAddrBadDescriptor)); |
|
55 Mem::Copy(&iAddr[0], aDes.Ptr(), KBTDevAddrSize); |
|
56 } |
|
57 |
|
58 /** |
|
59 Constructs a device address from a TInt64. |
|
60 |
|
61 The function panics if the most significant 16 bits of aInt are non-zero, |
|
62 as device addresses are 48 bits in size. |
|
63 |
|
64 @param aInt Value for device address. |
|
65 */ |
|
66 EXPORT_C TBTDevAddr::TBTDevAddr(const TInt64 &aInt) |
|
67 { |
|
68 __ASSERT_ALWAYS( (I64HIGH(aInt) & 0xffff0000u) == 0, Panic(EBTDevAddrBadTInt64)); |
|
69 TInt64 int64 (aInt); |
|
70 for (TInt i = KBTDevAddrSize-1; i>=0; --i) |
|
71 { |
|
72 iAddr[i] = TUint8(I64LOW(int64)); |
|
73 int64 >>= 8; |
|
74 } |
|
75 } |
|
76 |
|
77 /** |
|
78 Convert Readable string into a dev addr. |
|
79 |
|
80 Address may have leading space and "0x" characters. |
|
81 @param aSource Readable Hex representation of address |
|
82 @return EPOC error code on fail, or else number of characters used from aSource in conversion |
|
83 **/ |
|
84 EXPORT_C TInt TBTDevAddr::SetReadable(const TDesC& aSource) |
|
85 { |
|
86 TLex lex(aSource); |
|
87 |
|
88 lex.SkipSpaceAndMark(); |
|
89 |
|
90 if (lex.Get() != '0' || lex.Get() != 'x') |
|
91 lex.UnGetToMark(); |
|
92 |
|
93 TInt64 addr; |
|
94 |
|
95 TInt ret = lex.Val(addr, EHex); |
|
96 if (ret) |
|
97 return ret; |
|
98 if (addr > MAKE_TINT64(0x0000ffffu,0xffffffffu)) |
|
99 return KErrOverflow; |
|
100 |
|
101 *this=TBTDevAddr(addr); |
|
102 return lex.Offset(); |
|
103 } |
|
104 |
|
105 /** |
|
106 Extract the Bluetooth device address into a human-readable format, in aDest. |
|
107 |
|
108 Output is in hexadecimal bigendian format, with optional padding characters. |
|
109 |
|
110 Note: aDest must be large enough to hold the entire output, or else a |
|
111 a panic will occur. |
|
112 |
|
113 @param aDest Descriptor where result is placed. |
|
114 @param aPrepend Added onto front of output. |
|
115 @param aByteSeperator Inserted between each byte of address. |
|
116 @param aAppend Added onto end of output. |
|
117 */ |
|
118 EXPORT_C void TBTDevAddr::GetReadable(TDes& aDest, const TDesC& aPrepend, const TDesC& aByteSeperator, const TDesC& aAppend) const |
|
119 { |
|
120 __ASSERT_ALWAYS(aDest.MaxLength() >= aPrepend.Length() + (KBTDevAddrSize * (2 + aByteSeperator.Length())) - aByteSeperator.Length() + aAppend.Length(), Panic(EBTDevAddrBadDescriptor)); |
|
121 _LIT(KReadableFormat, "%S%02x%S%02x%S%02x%S%02x%S%02x%S%02x%S"); |
|
122 aDest.Format(KReadableFormat, &aPrepend, |
|
123 iAddr[0], &aByteSeperator, |
|
124 iAddr[1], &aByteSeperator, |
|
125 iAddr[2], &aByteSeperator, |
|
126 iAddr[3], &aByteSeperator, |
|
127 iAddr[4], &aByteSeperator, |
|
128 iAddr[5], &aAppend); |
|
129 } |
|
130 |
|
131 /** |
|
132 Comparison operator. |
|
133 @param aAddr The device address to compare to this object. |
|
134 @return ETrue if the addresses are equal, EFalse if not. |
|
135 */ |
|
136 EXPORT_C TBool TBTDevAddr::operator==(const TBTDevAddr& aAddr) const |
|
137 { |
|
138 return Mem::Compare(&iAddr[0],KBTDevAddrSize,&aAddr.iAddr[0],KBTDevAddrSize) == 0; |
|
139 } |
|
140 |
|
141 /** |
|
142 Inequality operator. |
|
143 @param aAddr The device address to compare to this object. |
|
144 @return EFalse if the addresses are equal, ETrue if not. |
|
145 */ |
|
146 EXPORT_C TBool TBTDevAddr::operator!=(const TBTDevAddr& aAddr) const |
|
147 { |
|
148 return !(*this==aAddr); |
|
149 } |
|
150 |
|
151 /** |
|
152 Access a single element of the address. |
|
153 @param aIndex The index of the element to access. |
|
154 @return The element. The reference remains valid as long as this object is in scope. |
|
155 */ |
|
156 EXPORT_C const TUint8 &TBTDevAddr::operator[](TInt aIndex) const |
|
157 { |
|
158 return iAddr[aIndex]; |
|
159 } |
|
160 |
|
161 /** |
|
162 Access a single element of the address. |
|
163 @param aIndex The index of the element to access. |
|
164 @return The element. The reference remains valid as long as this object is in scope. |
|
165 */ |
|
166 EXPORT_C TUint8 &TBTDevAddr::operator[](TInt aIndex) |
|
167 { |
|
168 return iAddr[aIndex]; |
|
169 } |
|
170 |
|
171 /** |
|
172 Reset the data contained within the object. |
|
173 */ |
|
174 EXPORT_C void TBTDevAddr::Reset() |
|
175 { |
|
176 iAddr.Reset(); |
|
177 } |
|
178 |
|
179 /** |
|
180 Access value as a bigendian descriptor. |
|
181 @return A non-const descriptor. |
|
182 */ |
|
183 EXPORT_C TPtr8 TBTDevAddr::Des() |
|
184 { |
|
185 return TPtr8(&iAddr[0], KBTDevAddrSize, KBTDevAddrSize); |
|
186 } |
|
187 |
|
188 /** |
|
189 Access value as a bigendian descriptor. |
|
190 @return A const descriptor. |
|
191 */ |
|
192 EXPORT_C const TPtrC8 TBTDevAddr::Des() const |
|
193 { |
|
194 return TPtrC8(&iAddr[0], KBTDevAddrSize); |
|
195 } |
|
196 |
|
197 |
|
198 /** |
|
199 Extract the Bluetooth device address into a human-readable format, in aDest. |
|
200 |
|
201 Output is in hexadecimal bigendian format, with no padding characters. |
|
202 |
|
203 Note: aDest must be large enough to hold the entire output, or else a |
|
204 a panic will occur. |
|
205 |
|
206 @param aDest Descriptor where result is placed. |
|
207 **/ |
|
208 EXPORT_C void TBTDevAddr::GetReadable(TDes& aDest) const |
|
209 { |
|
210 GetReadable(aDest, KNullDesC, KNullDesC, KNullDesC); |
|
211 } |
|
212 |
|
213 /** |
|
214 Less than operator. |
|
215 @param aAddr The device address to compare to this object. |
|
216 @return EFalse if the aAddr is greater than this TBTDevAddr, ETrue if not. |
|
217 */ |
|
218 EXPORT_C TBool TBTDevAddr::operator<=(const TBTDevAddr& aAddr) const |
|
219 { |
|
220 for (TUint index = 0; index < KBTDevAddrSize; index++) |
|
221 { |
|
222 if (iAddr[index] < aAddr.iAddr[index]) |
|
223 { |
|
224 return ETrue; |
|
225 } |
|
226 if (iAddr[index] > aAddr.iAddr[index]) |
|
227 { |
|
228 return EFalse; |
|
229 } |
|
230 } |
|
231 // addresses are the same |
|
232 return ETrue; |
|
233 } |
|
234 |
|
235 // EOF |