|
1 // Copyright (c) 1997-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 "CS_STD.H" |
|
17 |
|
18 // |
|
19 // Byte Order |
|
20 // Might make this lot inline... |
|
21 // |
|
22 |
|
23 #pragma warning( disable : 4244 ) // possible loss of data warning |
|
24 |
|
25 |
|
26 EXPORT_C TUint32 ByteOrder::Swap32(TUint32 aVal) |
|
27 /** Reverses the order of the bytes in a 32-bit value. |
|
28 |
|
29 @param aVal The value to reverse. |
|
30 @return The reversed value. */ |
|
31 { |
|
32 return ((aVal>>24)&0xff) | ((aVal>>8)&0xff00) | ((aVal<<8)&0xff0000) | ((aVal<<24)&0xff000000); |
|
33 } |
|
34 |
|
35 EXPORT_C TUint16 ByteOrder::Swap16(TUint16 aVal) |
|
36 /** Reverses the order of the bytes in a 16-bit value. |
|
37 |
|
38 @param aVal The value to reverse. |
|
39 @return The reversed value. */ |
|
40 { |
|
41 return ((aVal>>8) & 0xff) | ((aVal<<8) & 0xff00); |
|
42 } |
|
43 |
|
44 EXPORT_C TUint32 BigEndian::Get32(const TUint8 *aPtr) |
|
45 /** Gets a 32-bit integer value which is in big-endian format from a byte stream. |
|
46 |
|
47 @param aPtr A pointer to a byte stream. |
|
48 @return A 32-bit long integer value in native machine format. */ |
|
49 { |
|
50 return (aPtr[0]<<24) | (aPtr[1]<<16) | (aPtr[2]<<8) | aPtr[3]; |
|
51 } |
|
52 |
|
53 EXPORT_C void BigEndian::Put32(TUint8 *aPtr, TUint32 aVal) |
|
54 /** Inserts a 32-bit value into a byte stream in big-endian format. |
|
55 |
|
56 @param aPtr A pointer to a byte stream. |
|
57 @param aVal A 32-bit long integer value in native machine format. */ |
|
58 { |
|
59 aPtr[0] = aVal >> 24; |
|
60 aPtr[1] = (aVal >> 16) & 0xff; |
|
61 aPtr[2] = (aVal >> 8) & 0xff; |
|
62 aPtr[3] = aVal & 0xff; |
|
63 } |
|
64 |
|
65 EXPORT_C TUint16 BigEndian::Get16(const TUint8 *aPtr) |
|
66 /** Gets a 16-bit value integer which is in big-endian format from a byte stream. |
|
67 |
|
68 @param aPtr A pointer to a byte stream. |
|
69 @return A 16-bit long integer value in native machine format. */ |
|
70 { |
|
71 return (aPtr[0]<<8) | aPtr[1]; |
|
72 } |
|
73 |
|
74 EXPORT_C void BigEndian::Put16(TUint8 *aPtr, TUint16 aVal) |
|
75 /** Inserts a 16-bit value into a byte stream in big-endian format. |
|
76 |
|
77 @param aPtr A pointer to a byte stream. |
|
78 @param aVal A 16-bit long integer value in native machine format. */ |
|
79 { |
|
80 aPtr[0] = aVal >> 8; |
|
81 aPtr[1] = aVal & 0xff; |
|
82 } |
|
83 |
|
84 EXPORT_C TUint32 LittleEndian::Get32(const TUint8 *aPtr) |
|
85 /** Gets a 32-bit integer value which is in little-endian format from a byte stream. |
|
86 |
|
87 @param aPtr A pointer to a byte stream. |
|
88 @return A 32-bit long integer value in native machine format. */ |
|
89 { |
|
90 return (aPtr[3]<<24) | (aPtr[2]<<16) | (aPtr[1]<<8) | aPtr[0]; |
|
91 } |
|
92 |
|
93 EXPORT_C void LittleEndian::Put32(TUint8 *aPtr, TUint32 aVal) |
|
94 /** Inserts a 32-bit value into a byte stream in little-endian format. |
|
95 |
|
96 @param aPtr A pointer to a byte stream |
|
97 @param aVal A 32-bit long integer value in native machine format. */ |
|
98 { |
|
99 aPtr[3] = aVal >> 24; |
|
100 aPtr[2] = (aVal >> 16) & 0xff; |
|
101 aPtr[1] = (aVal >> 8) & 0xff; |
|
102 aPtr[0] = aVal & 0xff; |
|
103 } |
|
104 |
|
105 EXPORT_C TUint16 LittleEndian::Get16(const TUint8 *aPtr) |
|
106 /** Gets a 16-bit value integer which is in little-endian format from a byte stream. |
|
107 |
|
108 @param aPtr A pointer to a byte stream. |
|
109 @return A 16-bit long in native machine format. */ |
|
110 { |
|
111 return (aPtr[1]<<8) | aPtr[0]; |
|
112 } |
|
113 |
|
114 EXPORT_C void LittleEndian::Put16(TUint8 *aPtr, TUint16 aVal) |
|
115 /** Inserts a 16-bit value into a byte stream in little-endian format. |
|
116 |
|
117 @param aPtr A pointer to a byte stream. |
|
118 @param aVal A 32-bit long integer value in native machine format. */ |
|
119 { |
|
120 aPtr[0] = aVal & 0xff; |
|
121 aPtr[1] = aVal >> 8; |
|
122 } |
|
123 |
|
124 #pragma warning( default : 4244 ) // possible loss of data warning |
|
125 |
|
126 |
|
127 // |
|
128 // Socket Address |
|
129 // |
|
130 |
|
131 EXPORT_C TSockAddr::TSockAddr() |
|
132 : TBuf8<KMaxSockAddrSize>() |
|
133 /** Default constructor initialises the address to be zero-filled. */ |
|
134 { |
|
135 SetLength(KMaxSockAddrSize); |
|
136 SetFamily(KAFUnspec); |
|
137 } |
|
138 |
|
139 EXPORT_C TSockAddr::TSockAddr(TUint aFamily) |
|
140 /** |
|
141 Constructs the address with the family member data specified. |
|
142 |
|
143 @released. |
|
144 @param aFamily, Address family of address object. |
|
145 */ |
|
146 : TBuf8<KMaxSockAddrSize>() |
|
147 /** Constructs the address with the family member data specified. |
|
148 |
|
149 @param aFamily Address family Id. */ |
|
150 { |
|
151 SetLength(KMaxSockAddrSize); |
|
152 SetFamily(aFamily); |
|
153 } |
|
154 |
|
155 EXPORT_C TUint TSockAddr::Family() const |
|
156 /** Gets the address family and thereby the type of address. The family may be |
|
157 used to effect 'down-casting'. |
|
158 |
|
159 @return Address family Id. */ |
|
160 { |
|
161 return BasePtr()->iFamily; |
|
162 } |
|
163 |
|
164 EXPORT_C void TSockAddr::SetFamily(TUint aFamily) |
|
165 /** Sets the address family of an address. |
|
166 |
|
167 @param aFamily Address family Id. */ |
|
168 { |
|
169 BasePtr()->iFamily = aFamily; |
|
170 } |
|
171 |
|
172 EXPORT_C void TSockAddr::SetPort(TUint aPort) |
|
173 // |
|
174 // Set Generic Port |
|
175 // |
|
176 /** Sets the address port of an address. |
|
177 |
|
178 @param aPort New port value for address */ |
|
179 { |
|
180 BasePtr()->iPort = aPort; |
|
181 } |
|
182 |
|
183 EXPORT_C TUint TSockAddr::Port() const |
|
184 // |
|
185 // Get Generic Port |
|
186 // |
|
187 /** Gets the port value. |
|
188 |
|
189 @return Port value */ |
|
190 { |
|
191 return BasePtr()->iPort; |
|
192 } |
|
193 |
|
194 EXPORT_C TBool TSockAddr::CmpPort(const TSockAddr &anAddr) const |
|
195 /** |
|
196 Compare two ports. |
|
197 |
|
198 Compares ports in two addresses |
|
199 |
|
200 @param anAddr, socket address. |
|
201 @return ETrue if the ports match and EFalse if they do not |
|
202 */ |
|
203 { |
|
204 return (BasePtr()->iFamily==anAddr.BasePtr()->iFamily) |
|
205 && (BasePtr()->iPort==anAddr.BasePtr()->iPort); |
|
206 } |
|
207 |
|
208 EXPORT_C void TSockAddr::SetUserLen(TInt aLen) |
|
209 // |
|
210 // Set descriptor length to actual length of |
|
211 // subclass. |
|
212 // |
|
213 /** Sets the length of data it appends after the base class' data. This function |
|
214 is mainly intended for use in derived classes. |
|
215 |
|
216 @param aLen New length for appended data. */ |
|
217 { |
|
218 SetLength(aLen+Align4(sizeof(SSockAddr))); |
|
219 } |
|
220 |
|
221 EXPORT_C TInt TSockAddr::GetUserLen() |
|
222 // |
|
223 // Get length of user part of TSockAddr |
|
224 // |
|
225 /** Gets the number of bytes a derived class has set beyond the SSockAddr part |
|
226 of the address. This function is mainly intended for use in derived classes. |
|
227 |
|
228 @return User length */ |
|
229 { |
|
230 return Length()-Align4(sizeof(SSockAddr)); |
|
231 } |