|
1 /* |
|
2 * Copyright (c) 2005 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: An example implementation for ISC Driver Reference |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 // INCLUDE FILES |
|
21 |
|
22 #include <kernel.h> |
|
23 #ifdef __WINS__ |
|
24 #include <windows.h> |
|
25 #endif |
|
26 |
|
27 #include <IscDefinitions.h> |
|
28 #include "IscQueue.h" |
|
29 #include "IscTrace.h" |
|
30 |
|
31 |
|
32 // EXTERNAL DATA STRUCTURES |
|
33 #ifdef __WINS__ |
|
34 extern CRITICAL_SECTION g_IscCriticalSection; |
|
35 #endif |
|
36 |
|
37 // EXTERNAL FUNCTION PROTOTYPES |
|
38 |
|
39 // CONSTANTS |
|
40 const TInt KIscInterruptLevelTwo( 2 ); |
|
41 |
|
42 // MACROS |
|
43 |
|
44 // LOCAL CONSTANTS AND MACROS |
|
45 |
|
46 // MODULE DATA STRUCTURES |
|
47 |
|
48 // LOCAL FUNCTION PROTOTYPES |
|
49 |
|
50 // FORWARD DECLARATIONS |
|
51 |
|
52 |
|
53 // ============================ MEMBER FUNCTIONS =============================== |
|
54 |
|
55 // ----------------------------------------------------------------------------- |
|
56 // DIscQueue::DIscQueue |
|
57 // C++ default constructor |
|
58 // ----------------------------------------------------------------------------- |
|
59 // |
|
60 DIscQueue::DIscQueue() |
|
61 :iHead( 0 ),iTail( 0 ),iCount( 0 ),iSize( 0 ),iQueue( NULL ) |
|
62 { |
|
63 } |
|
64 |
|
65 // ----------------------------------------------------------------------------- |
|
66 // DIscQueue::DIscQueue |
|
67 // C++ default constructor can NOT contain any code, that |
|
68 // might leave. |
|
69 // ----------------------------------------------------------------------------- |
|
70 // |
|
71 EXPORT_C DIscQueue::DIscQueue( TUint32** aQueue, TUint16 Size ) |
|
72 :iHead( 0 ),iTail( 0 ),iCount( 0 ),iSize( Size ),iQueue( aQueue ) |
|
73 { |
|
74 E_TRACE( ( _T( "IQ:IQ %d 0x%x" ), iSize, iQueue ) ); |
|
75 } |
|
76 |
|
77 // Destructor |
|
78 EXPORT_C DIscQueue::~DIscQueue() |
|
79 { |
|
80 iHead = 0; |
|
81 iTail = 0; |
|
82 iCount = 0; |
|
83 iQueue = NULL; |
|
84 iSize = 0; |
|
85 } |
|
86 |
|
87 |
|
88 // ----------------------------------------------------------------------------- |
|
89 // DIscQueue::Add |
|
90 // Function to add element to queue |
|
91 // ( other items were commented in a header ). |
|
92 // ----------------------------------------------------------------------------- |
|
93 // |
|
94 EXPORT_C TInt DIscQueue::Add( TAny* anEntry ) |
|
95 { |
|
96 E_TRACE( ( _T( "IQ:A %d 0x%x" ), iCount, iQueue ) ); |
|
97 |
|
98 TInt irqLevel = DisableIrqs(); |
|
99 |
|
100 if ( iCount == iSize || iSize == 0 ) |
|
101 { |
|
102 RestoreIrqs( irqLevel ); |
|
103 return KErrNoMemory;//EFalse; |
|
104 } |
|
105 |
|
106 /* place the buffer into the queue */ |
|
107 iQueue[ iTail ] = ( TUint32* )( anEntry ); |
|
108 |
|
109 if ( iSize > 0 ) |
|
110 { |
|
111 /* adjust tail pointer */ |
|
112 iTail = ++iTail % iSize; |
|
113 |
|
114 /* remember the amount of the requests in the queue */ |
|
115 iCount++; |
|
116 } |
|
117 else |
|
118 { |
|
119 ASSERT_RESET_ALWAYS( 0, "IscDriver", EIscBufferAllocationFailure ) |
|
120 } |
|
121 |
|
122 RestoreIrqs( irqLevel ); |
|
123 |
|
124 return KErrNone; |
|
125 } |
|
126 |
|
127 // ----------------------------------------------------------------------------- |
|
128 // DIscQueue::Remove |
|
129 // Removes first element from the queue |
|
130 // ( other items were commented in a header ). |
|
131 // ----------------------------------------------------------------------------- |
|
132 // |
|
133 EXPORT_C TAny* DIscQueue::RemoveFirst() |
|
134 { |
|
135 E_TRACE( ( _T( "IQ:R %d %d" ), iCount, iSize ) ); |
|
136 |
|
137 TAny* result = NULL; |
|
138 TInt irqLevel = DisableIrqs(); |
|
139 |
|
140 if ( iCount == 0 || iSize == 0 ) |
|
141 { |
|
142 RestoreIrqs( irqLevel ); |
|
143 return NULL; |
|
144 } |
|
145 // Get an element from the queue. |
|
146 result = ( TAny* )iQueue[ iHead ]; |
|
147 |
|
148 iQueue[ iHead ] = NULL; |
|
149 |
|
150 // Adjust the head of the queue. |
|
151 iHead = ++iHead % iSize; |
|
152 // Decrease counter. |
|
153 iCount--; |
|
154 |
|
155 RestoreIrqs( irqLevel ); |
|
156 |
|
157 return result; |
|
158 |
|
159 } |
|
160 |
|
161 // ----------------------------------------------------------------------------- |
|
162 // DIscQueue::GetFirst |
|
163 // Fetches first element from the queue |
|
164 // ( other items were commented in a header ). |
|
165 // ----------------------------------------------------------------------------- |
|
166 // |
|
167 EXPORT_C TAny* DIscQueue::GetFirst() |
|
168 { |
|
169 E_TRACE( ( _T( "IQ:G %d %d" ), iCount, iSize ) ); |
|
170 |
|
171 TAny* result; |
|
172 TInt irqLevel = DisableIrqs(); |
|
173 |
|
174 if ( iCount == 0 || iSize == 0 ) |
|
175 { |
|
176 RestoreIrqs( irqLevel ); |
|
177 return NULL; |
|
178 } |
|
179 // Get an element from the queue. |
|
180 result = ( TAny* )iQueue[ iHead ]; |
|
181 |
|
182 RestoreIrqs( irqLevel ); |
|
183 |
|
184 return result; |
|
185 |
|
186 } |
|
187 |
|
188 // ----------------------------------------------------------------------------- |
|
189 // DIscQueue::DeleteFirst |
|
190 // Deletes first element from the queue |
|
191 // ( other items were commented in a header ). |
|
192 // ----------------------------------------------------------------------------- |
|
193 // |
|
194 EXPORT_C void DIscQueue::DeleteFirst() |
|
195 { |
|
196 E_TRACE( ( _T( "IQ:D %d %d" ), iCount, iSize ) ); |
|
197 |
|
198 TInt irqLevel = DisableIrqs(); |
|
199 |
|
200 iQueue[ iHead ] = NULL; |
|
201 |
|
202 if ( iSize > 0 ) |
|
203 { |
|
204 // Adjust the head of the queue. |
|
205 iHead = ++iHead % iSize; |
|
206 // decrease counter. |
|
207 iCount--; |
|
208 } |
|
209 else |
|
210 { |
|
211 ASSERT_RESET_ALWAYS( 0, "IscDriver", EIscBufferAllocationFailure ) |
|
212 } |
|
213 |
|
214 RestoreIrqs( irqLevel ); |
|
215 |
|
216 } |
|
217 |
|
218 // ----------------------------------------------------------------------------- |
|
219 // DIscQueue::Empty |
|
220 // Checks if queue is empty |
|
221 // ( other items were commented in a header ). |
|
222 // ----------------------------------------------------------------------------- |
|
223 // |
|
224 EXPORT_C TBool DIscQueue::Empty() |
|
225 { |
|
226 TInt result; |
|
227 TInt irqLevel = DisableIrqs(); |
|
228 result = iCount == 0 ? ETrue : EFalse; |
|
229 RestoreIrqs( irqLevel ); |
|
230 |
|
231 return result; |
|
232 } |
|
233 |
|
234 // ----------------------------------------------------------------------------- |
|
235 // DIscQueue::NextBufferLenth |
|
236 // Gets length of next frame in queue |
|
237 // ( other items were commented in a header ). |
|
238 // ----------------------------------------------------------------------------- |
|
239 // |
|
240 EXPORT_C TUint16 DIscQueue::NextBufferLength() |
|
241 { |
|
242 TUint16 length; |
|
243 |
|
244 TInt irqLevel = DisableIrqs(); |
|
245 |
|
246 if ( iCount == 0 ) |
|
247 { |
|
248 RestoreIrqs( irqLevel ); |
|
249 return 0; |
|
250 } |
|
251 |
|
252 length = ( TUint16 )( ( ( TDes8* )iQueue[ iHead ] )->Length() ); |
|
253 |
|
254 |
|
255 RestoreIrqs( irqLevel ); |
|
256 |
|
257 return length; |
|
258 } |
|
259 |
|
260 // ----------------------------------------------------------------------------- |
|
261 // DIscQueue::DisableIrqs |
|
262 // Function to disable interrupts |
|
263 // ( other items were commented in a header ). |
|
264 // ----------------------------------------------------------------------------- |
|
265 // |
|
266 TInt DIscQueue::DisableIrqs() |
|
267 { |
|
268 #ifndef __WINS__ |
|
269 return NKern::DisableInterrupts( KIscInterruptLevelTwo ); |
|
270 #else //__WINS__ |
|
271 EnterCriticalSection( &g_IscCriticalSection ); |
|
272 return KErrNone; |
|
273 #endif//__WINS__ |
|
274 } |
|
275 |
|
276 // ----------------------------------------------------------------------------- |
|
277 // DIscQueue::RestoreIrqs |
|
278 // Function to restore interrupts |
|
279 // ( other items were commented in a header ). |
|
280 // ----------------------------------------------------------------------------- |
|
281 // |
|
282 #ifndef __WINS__ |
|
283 void DIscQueue::RestoreIrqs( |
|
284 TInt aLevel ) |
|
285 { |
|
286 NKern::RestoreInterrupts( aLevel ); |
|
287 #else //__WINS__ |
|
288 void DIscQueue::RestoreIrqs( |
|
289 TInt ) |
|
290 { |
|
291 LeaveCriticalSection( &g_IscCriticalSection ); |
|
292 #endif//__WINS__ |
|
293 } |
|
294 |
|
295 // End of File |