|
1 /* |
|
2 * Copyright (c) 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: Logic for bufferred publish and subcribe notifier |
|
15 * |
|
16 */ |
|
17 |
|
18 #include <e32property.h> |
|
19 #include "spdefaultvalues.h" |
|
20 |
|
21 #include "spsbufferedpublisher.h" |
|
22 |
|
23 const TUint32 KRealGlobalIndex = 0; |
|
24 _LIT( KSemaphore, "CSpsBufferedPublisher" ); |
|
25 |
|
26 // ======== LOCAL FUNCTIONS ======== |
|
27 |
|
28 // --------------------------------------------------------------------------- |
|
29 // ?description |
|
30 // --------------------------------------------------------------------------- |
|
31 // |
|
32 CSpsBufferedPublisher* CSpsBufferedPublisher::NewL( TUid aCategory, TUint aKey ) |
|
33 { |
|
34 CSpsBufferedPublisher* self = new (ELeave) CSpsBufferedPublisher( aCategory, aKey ); |
|
35 CleanupStack::PushL( self ); |
|
36 self->ConstructL(); |
|
37 CleanupStack::Pop( self ); |
|
38 return self; |
|
39 } |
|
40 |
|
41 // --------------------------------------------------------------------------- |
|
42 // ?description |
|
43 // --------------------------------------------------------------------------- |
|
44 // |
|
45 CSpsBufferedPublisher::CSpsBufferedPublisher( TUid aCategory, TUint aKey ): |
|
46 iCategory( aCategory ), |
|
47 iKey( aKey ), |
|
48 iIndex( KErrNotReady ) |
|
49 { |
|
50 |
|
51 } |
|
52 |
|
53 // --------------------------------------------------------------------------- |
|
54 // ?description |
|
55 // --------------------------------------------------------------------------- |
|
56 // |
|
57 CSpsBufferedPublisher::~CSpsBufferedPublisher() |
|
58 { |
|
59 |
|
60 } |
|
61 |
|
62 // --------------------------------------------------------------------------- |
|
63 // ?description |
|
64 // --------------------------------------------------------------------------- |
|
65 // |
|
66 void CSpsBufferedPublisher::ConstructL() |
|
67 { |
|
68 static _LIT_SECURITY_POLICY_PASS( KAllowAllPolicy ); |
|
69 |
|
70 TInt err = RProperty::Define( |
|
71 iCategory, iKey, |
|
72 RProperty::EByteArray, |
|
73 KAllowAllPolicy, KAllowAllPolicy, |
|
74 RProperty::KMaxPropertySize ); |
|
75 |
|
76 if( !err ) |
|
77 { |
|
78 TBuf8<RProperty::KMaxPropertySize> data; |
|
79 data.FillZ( data.MaxLength() ); |
|
80 User::LeaveIfError( RProperty::Set( iCategory, iKey, data ) ); |
|
81 } |
|
82 } |
|
83 |
|
84 // --------------------------------------------------------------------------- |
|
85 // ?description |
|
86 // --------------------------------------------------------------------------- |
|
87 // |
|
88 void CSpsBufferedPublisher::SetL( TUid aCategory, TUint aKey, TUint32 aData ) |
|
89 { |
|
90 RSemaphore wait; |
|
91 TInt err = wait.OpenGlobal( KSemaphore ); |
|
92 |
|
93 if ( err ) |
|
94 { |
|
95 User::LeaveIfError( wait.CreateGlobal( KSemaphore, 1 ) ); |
|
96 } |
|
97 |
|
98 wait.Wait(); |
|
99 TCleanupItem cleanup( CSpsBufferedPublisher::SignalAndCloseSemaphore, &wait ); |
|
100 CleanupStack::PushL( cleanup ); |
|
101 |
|
102 TBuf8<RProperty::KMaxPropertySize> data; |
|
103 User::LeaveIfError( |
|
104 RProperty::Get( aCategory, aKey, data ) ); |
|
105 |
|
106 // Increment index |
|
107 GlobalIndex( data )++; |
|
108 |
|
109 if( Base( data, GlobalIndex( data ) ) == GlobalIndex( data ) ) |
|
110 { |
|
111 // Skip index if real index is same as KRealGlobalIndex |
|
112 GlobalIndex( data )++; |
|
113 } |
|
114 |
|
115 // Write data to index |
|
116 At( data, GlobalIndex( data ) ) = aData; |
|
117 |
|
118 User::LeaveIfError( |
|
119 RProperty::Set( aCategory, aKey, data ) ); |
|
120 |
|
121 CleanupStack::PopAndDestroy(); // wait |
|
122 } |
|
123 |
|
124 // --------------------------------------------------------------------------- |
|
125 // ?description |
|
126 // --------------------------------------------------------------------------- |
|
127 // |
|
128 void CSpsBufferedPublisher::GetL( RArray<TUint32>& aData ) |
|
129 { |
|
130 User::LeaveIfError( iIndex ); |
|
131 |
|
132 TBuf8<RProperty::KMaxPropertySize> data; |
|
133 User::LeaveIfError( |
|
134 RProperty::Get( iCategory, iKey, data ) ); |
|
135 |
|
136 const TUint32 lastModified = GlobalIndex( data ); |
|
137 const TUint32 indexDifference = lastModified - iIndex; |
|
138 if( Max( data ) < indexDifference ) |
|
139 { |
|
140 // If some of data is missed |
|
141 iIndex = lastModified - Max( data ); |
|
142 } |
|
143 |
|
144 // Start reading |
|
145 while( lastModified != iIndex ) |
|
146 { |
|
147 iIndex++; |
|
148 |
|
149 if( Base( data, iIndex ) == iIndex ) |
|
150 { |
|
151 // Skip index if real index is same as KRealGlobalIndex |
|
152 iIndex++; |
|
153 } |
|
154 |
|
155 aData.Append( At(data, iIndex) ); |
|
156 } |
|
157 |
|
158 } |
|
159 |
|
160 // --------------------------------------------------------------------------- |
|
161 // ?description |
|
162 // --------------------------------------------------------------------------- |
|
163 // |
|
164 void CSpsBufferedPublisher::Start() |
|
165 { |
|
166 if( KErrNone > iIndex ) |
|
167 { |
|
168 TBuf8<RProperty::KMaxPropertySize> data; |
|
169 RProperty::Get( iCategory, iKey, data ); |
|
170 if( data.Length() ) |
|
171 { |
|
172 // When no error occurs |
|
173 iIndex = GlobalIndex( data ); |
|
174 } |
|
175 else |
|
176 { |
|
177 iIndex = 0; |
|
178 } |
|
179 } |
|
180 |
|
181 } |
|
182 |
|
183 // --------------------------------------------------------------------------- |
|
184 // ?description |
|
185 // --------------------------------------------------------------------------- |
|
186 // |
|
187 void CSpsBufferedPublisher::Stop() |
|
188 { |
|
189 iIndex = KErrNotReady; |
|
190 } |
|
191 |
|
192 // --------------------------------------------------------------------------- |
|
193 // ?description |
|
194 // --------------------------------------------------------------------------- |
|
195 // |
|
196 void CSpsBufferedPublisher::SignalAndCloseSemaphore( TAny* aPtr ) |
|
197 { |
|
198 RSemaphore* wait = (RSemaphore*)aPtr; |
|
199 wait->Signal(); |
|
200 wait->Close(); |
|
201 } |
|
202 |
|
203 // --------------------------------------------------------------------------- |
|
204 // ?description |
|
205 // --------------------------------------------------------------------------- |
|
206 // |
|
207 TUint32& CSpsBufferedPublisher::At( TDes8& aData, TInt aIndex ) |
|
208 { |
|
209 const TUint32 baseIndex = Base( aData, aIndex ); |
|
210 TUint32 realIndex = aIndex - baseIndex; |
|
211 ASSERT( KRealGlobalIndex != realIndex ); // Check if global index is not used |
|
212 return (TUint32&)aData[ realIndex * sizeof(TUint32) ]; |
|
213 } |
|
214 |
|
215 // --------------------------------------------------------------------------- |
|
216 // ?description |
|
217 // --------------------------------------------------------------------------- |
|
218 // |
|
219 TUint CSpsBufferedPublisher::Max( TDesC8& aData ) |
|
220 { |
|
221 return ( aData.Length() / sizeof(TUint32) ); |
|
222 } |
|
223 |
|
224 // --------------------------------------------------------------------------- |
|
225 // ?description |
|
226 // --------------------------------------------------------------------------- |
|
227 // |
|
228 TInt32 CSpsBufferedPublisher::Base( TDesC8& aData, TInt aIndex ) |
|
229 { |
|
230 const TInt max = Max( aData ); |
|
231 return ( max * (TUint32)( aIndex / max ) ); |
|
232 } |
|
233 |
|
234 // --------------------------------------------------------------------------- |
|
235 // ?description |
|
236 // --------------------------------------------------------------------------- |
|
237 // |
|
238 TUint32& CSpsBufferedPublisher::GlobalIndex( TDes8& aData ) |
|
239 { |
|
240 return (TUint32&)aData[ KRealGlobalIndex ]; |
|
241 } |