|
1 /* |
|
2 * Copyright (c) 2006 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 the License "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: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 #include <mpxlog.h> |
|
22 |
|
23 #include "vcxconnutilcommon.h" |
|
24 #include "vcxconnectionutility.hrh" |
|
25 #include "vcxconnutilpubsub.h" |
|
26 #include "vcxconnutilsubscriber.h" |
|
27 #include "vcxconnutilpubsubobserver.h" |
|
28 |
|
29 // CONSTANTS |
|
30 _LIT( KConnUtilCounterSema, "__VcxConnUtillCounterSema__ "); |
|
31 const TInt KSemaStartupValue = 1; |
|
32 |
|
33 // ----------------------------------------------------------------------------- |
|
34 // CVcxConnUtilPubSub::NewL() |
|
35 // ----------------------------------------------------------------------------- |
|
36 // |
|
37 CVcxConnUtilPubSub* CVcxConnUtilPubSub::NewL( MConnUtilPubsubObserver *aObserver ) |
|
38 { |
|
39 CVcxConnUtilPubSub* self = CVcxConnUtilPubSub::NewLC( aObserver ); |
|
40 CleanupStack::Pop( self ); |
|
41 return self; |
|
42 } |
|
43 |
|
44 // ----------------------------------------------------------------------------- |
|
45 // CVcxConnUtilPubSub::NewLC() |
|
46 // ----------------------------------------------------------------------------- |
|
47 // |
|
48 CVcxConnUtilPubSub* CVcxConnUtilPubSub::NewLC( MConnUtilPubsubObserver *aObserver ) |
|
49 { |
|
50 CVcxConnUtilPubSub* self = new (ELeave) CVcxConnUtilPubSub( ); |
|
51 CleanupStack::PushL( self ); |
|
52 self->ConstructL( aObserver ); |
|
53 return self; |
|
54 } |
|
55 |
|
56 // ----------------------------------------------------------------------------- |
|
57 // CVcxConnUtilPubSub::CVcxNsConnUtilEngine() |
|
58 // ----------------------------------------------------------------------------- |
|
59 // |
|
60 CVcxConnUtilPubSub::CVcxConnUtilPubSub( ) |
|
61 { |
|
62 // NOP |
|
63 } |
|
64 |
|
65 // ----------------------------------------------------------------------------- |
|
66 // CVcxConnUtilPubSub::ConstructL() |
|
67 // ----------------------------------------------------------------------------- |
|
68 // |
|
69 void CVcxConnUtilPubSub::ConstructL( MConnUtilPubsubObserver *aObserver ) |
|
70 { |
|
71 MPX_DEBUG1("vcxconnutil ## CVcxConnUtilPubSub::ConstructL() in"); |
|
72 |
|
73 TInt status( KErrNone ); |
|
74 // semafore for protecting P&S -counter value writing |
|
75 status = iSemaCounter.CreateGlobal( KConnUtilCounterSema, KSemaStartupValue ); |
|
76 if( status == KErrAlreadyExists ) |
|
77 { |
|
78 // if semafore exists already, just open handle to existing |
|
79 status = iSemaCounter.OpenGlobal( KConnUtilCounterSema ); |
|
80 } |
|
81 User::LeaveIfError( status ); |
|
82 |
|
83 User::LeaveIfError( |
|
84 iPropertyCurrentIAPId.Attach( KVcxConnUtilPScategory, |
|
85 EVCxPSIapId ) ); |
|
86 |
|
87 User::LeaveIfError( |
|
88 iPropertyCurrentSNAPId.Attach( KVcxConnUtilPScategory, |
|
89 EVCxPSSnapId ) ); |
|
90 |
|
91 User::LeaveIfError( |
|
92 iPropertyNbrOfInst.Attach( KVcxConnUtilPScategory, |
|
93 EVCxPSNbrConnInstances ) ); |
|
94 |
|
95 User::LeaveIfError( |
|
96 iPropertyAccepted.Attach( KVcxConnUtilPScategory, |
|
97 EVCxPSNbRoamAccepted ) ); |
|
98 |
|
99 iConnectionStatePS = |
|
100 CVcxConnUtilSubscriber::NewL( KVcxConnUtilPScategory, |
|
101 EVCxPSConnectionStatus, |
|
102 RProperty::EInt, aObserver ); |
|
103 |
|
104 iMasterExistPS = |
|
105 CVcxConnUtilSubscriber::NewL( KVcxConnUtilPScategory, |
|
106 EVCxPSMasterExists, |
|
107 RProperty::EInt, aObserver ); |
|
108 |
|
109 iRoamingReqStatusPS = |
|
110 CVcxConnUtilSubscriber::NewL( KVcxConnUtilPScategory, |
|
111 EVCxPSRoamingRequestStatus, |
|
112 RProperty::EInt, aObserver ); |
|
113 |
|
114 iNbrInstanceResp = |
|
115 CVcxConnUtilSubscriber::NewL( KVcxConnUtilPScategory, |
|
116 EVCxPSNbrRoamResp, |
|
117 RProperty::EInt, aObserver ); |
|
118 |
|
119 if( GetValue( EVCxPSIapId, status ) == KErrNotFound ) |
|
120 { |
|
121 MPX_DEBUG1("vcxconnutil ## CVcxConnUtilPubSub::ConstructL() no properties"); |
|
122 |
|
123 // define properties |
|
124 DefinePropertiesL(); |
|
125 } |
|
126 |
|
127 MPX_DEBUG1("vcxconnutil ## CVcxConnUtilPubSub::ConstructL() out"); |
|
128 } |
|
129 |
|
130 // ----------------------------------------------------------------------------- |
|
131 // CVcxConnUtilPubSub::~CVcxConnUtilPubSub() |
|
132 // ----------------------------------------------------------------------------- |
|
133 // |
|
134 CVcxConnUtilPubSub::~CVcxConnUtilPubSub() |
|
135 { |
|
136 iSemaCounter.Close(); |
|
137 |
|
138 iPropertyCurrentIAPId.Close(); |
|
139 iPropertyCurrentSNAPId.Close(); |
|
140 iPropertyNbrOfInst.Close(); |
|
141 iPropertyAccepted.Close(); |
|
142 |
|
143 delete iConnectionStatePS; |
|
144 delete iMasterExistPS; |
|
145 delete iRoamingReqStatusPS; |
|
146 delete iNbrInstanceResp; |
|
147 |
|
148 } |
|
149 |
|
150 // ----------------------------------------------------------------------------- |
|
151 // CVcxConnUtilPubSub::DefinePropertiesL() |
|
152 // ----------------------------------------------------------------------------- |
|
153 // |
|
154 void CVcxConnUtilPubSub::DefinePropertiesL() |
|
155 { |
|
156 MPX_DEBUG1("vcxconnutil ## CVcxConnUtilPubSub::DefinePropertiesL() in"); |
|
157 TInt result( KErrNone ); |
|
158 RSemaphore semaphore; |
|
159 result = semaphore.CreateGlobal( KVcxConnUtilPSSema, 0 ); |
|
160 if( result != KErrNone && result != KErrAlreadyExists ) |
|
161 { |
|
162 // kernel cannot create semaphore, or there is no existing |
|
163 User::LeaveIfError( result ); |
|
164 } |
|
165 if( result == KErrAlreadyExists ) |
|
166 { |
|
167 // semaphore exist. Some other process is already defining PS |
|
168 // open semaphore and wait for it to finish |
|
169 User::LeaveIfError( semaphore.OpenGlobal( KVcxConnUtilPSSema ) ); |
|
170 semaphore.Wait(); |
|
171 // check if PS keys do exists |
|
172 TInt value( 0 ); |
|
173 if( GetValue( EVCxPSIapId, value ) == KErrNotFound ) |
|
174 { |
|
175 // other proces has failed to define PS, |
|
176 // this process will try it |
|
177 result = KErrNone; |
|
178 } |
|
179 } |
|
180 if( result == KErrNone ) |
|
181 { |
|
182 const TUidType createPSUid( KNullUid, KNullUid, KVcxConnUtilPScategory ); |
|
183 |
|
184 RProcess createPS; |
|
185 result = createPS.Create( KVcxConnUtilPSWorkerName(), KNullDesC(), createPSUid ); |
|
186 if( result == KErrNone ) |
|
187 { |
|
188 createPS.Resume(); |
|
189 createPS.Close(); |
|
190 semaphore.Wait(); |
|
191 // init default values for keys, whose default differs from 0 |
|
192 SetValue( EVCxPSRoamingRequestStatus, EVCxRoamingNotInit ); |
|
193 SetValue( EVCxPSConnectionStatus, EVCxNotConnected ); |
|
194 } |
|
195 } |
|
196 // need to signal here for other possible |
|
197 // processes to proceed |
|
198 semaphore.Signal(); |
|
199 semaphore.Close(); |
|
200 if( result != KErrNone && result != KErrAlreadyExists ) |
|
201 { |
|
202 // all other erros cause leave: worker process creation fails, or |
|
203 // kernel refuses to open existing semphore (== platform problem) |
|
204 User::LeaveIfError( result ); |
|
205 } |
|
206 MPX_DEBUG1("vcxconnutil ## CVcxConnUtilPubSub::DefinePropertiesL() out"); |
|
207 } |
|
208 |
|
209 |
|
210 RProperty* CVcxConnUtilPubSub::GetProperty( TUint32 aKey ) |
|
211 { |
|
212 switch( aKey ) |
|
213 { |
|
214 case EVCxPSConnectionStatus: |
|
215 return &( iConnectionStatePS->Property() ); |
|
216 |
|
217 case EVCxPSIapId: |
|
218 return &iPropertyCurrentIAPId; |
|
219 |
|
220 case EVCxPSSnapId: |
|
221 return &iPropertyCurrentSNAPId; |
|
222 |
|
223 case EVCxPSMasterExists: |
|
224 return &( iMasterExistPS->Property() ); |
|
225 |
|
226 case EVCxPSNbrConnInstances: |
|
227 return &iPropertyNbrOfInst; |
|
228 |
|
229 case EVCxPSRoamingRequestStatus: |
|
230 return &( iRoamingReqStatusPS->Property() ); |
|
231 |
|
232 case EVCxPSNbrRoamResp: |
|
233 return &( iNbrInstanceResp->Property() ); |
|
234 |
|
235 case EVCxPSNbRoamAccepted: |
|
236 return &iPropertyAccepted; |
|
237 |
|
238 default: |
|
239 break; |
|
240 } |
|
241 return 0; |
|
242 } |
|
243 |
|
244 CVcxConnUtilSubscriber* CVcxConnUtilPubSub::GetSubscriber( TUint32 aKey ) |
|
245 { |
|
246 switch( aKey ) |
|
247 { |
|
248 case EVCxPSConnectionStatus: |
|
249 return iConnectionStatePS; |
|
250 |
|
251 case EVCxPSMasterExists: |
|
252 return iMasterExistPS; |
|
253 |
|
254 case EVCxPSRoamingRequestStatus: |
|
255 return iRoamingReqStatusPS; |
|
256 |
|
257 case EVCxPSNbrRoamResp: |
|
258 return iNbrInstanceResp; |
|
259 |
|
260 default: |
|
261 break; |
|
262 } |
|
263 return 0; |
|
264 } |
|
265 |
|
266 // ----------------------------------------------------------------------------- |
|
267 // CVcxConnUtilPubSub::IncCounterPubSubL() |
|
268 // ----------------------------------------------------------------------------- |
|
269 // |
|
270 void CVcxConnUtilPubSub::IncCounterPubSubL( TUint32 aKey ) |
|
271 { |
|
272 const TInt KIncValue( 1 ); |
|
273 DoPSCounterIncrementL( aKey, KIncValue ); |
|
274 } |
|
275 |
|
276 // ----------------------------------------------------------------------------- |
|
277 // CVcxConnUtilPubSub::DecNbrOfConnUsersPubSub() |
|
278 // ----------------------------------------------------------------------------- |
|
279 // |
|
280 void CVcxConnUtilPubSub::DecCounterPubSubL( TUint32 aKey ) |
|
281 { |
|
282 const TInt KDecValue( -1 ); |
|
283 DoPSCounterIncrementL( aKey, KDecValue ); |
|
284 } |
|
285 |
|
286 // ----------------------------------------------------------------------------- |
|
287 // CVcxConnUtilPubSub::DoPSCounterIncrementL() |
|
288 // ----------------------------------------------------------------------------- |
|
289 // |
|
290 void CVcxConnUtilPubSub::DoPSCounterIncrementL( TUint32 aKey, TInt aCalcFactor ) |
|
291 { |
|
292 if( aKey == EVCxPSNbrConnInstances || |
|
293 aKey == EVCxPSNbrRoamResp || |
|
294 aKey == EVCxPSNbRoamAccepted ) |
|
295 { |
|
296 RProperty* property = GetProperty( aKey ); |
|
297 if( property ) |
|
298 { |
|
299 TInt value( 0 ); |
|
300 TInt err( KErrNone ); |
|
301 // entering critical section |
|
302 iSemaCounter.Wait(); |
|
303 err = property->Get( value ); |
|
304 if( err == KErrNone ) |
|
305 { |
|
306 value += aCalcFactor; |
|
307 err = property->Set( value ); |
|
308 } |
|
309 // leaving critical section |
|
310 iSemaCounter.Signal(); |
|
311 User::LeaveIfError( err ); |
|
312 } |
|
313 } |
|
314 } |
|
315 |
|
316 // ----------------------------------------------------------------------------- |
|
317 // CVcxConnUtilPubSub::SetValue() |
|
318 // ----------------------------------------------------------------------------- |
|
319 // |
|
320 TInt CVcxConnUtilPubSub::SetValue( TUint32 aKey, TInt aValue ) |
|
321 { |
|
322 TInt err( KErrNone ); |
|
323 RProperty* property = GetProperty( aKey ); |
|
324 if( property ) |
|
325 { |
|
326 err = property->Set( aValue ); |
|
327 } |
|
328 return err; |
|
329 } |
|
330 |
|
331 // ----------------------------------------------------------------------------- |
|
332 // CVcxConnUtilPubSub::GetValue() |
|
333 // ----------------------------------------------------------------------------- |
|
334 // |
|
335 TInt CVcxConnUtilPubSub::GetValue( TUint32 aKey, TInt& aValue ) |
|
336 { |
|
337 TInt err( KErrNone ); |
|
338 RProperty* property = GetProperty( aKey ); |
|
339 if( property ) |
|
340 { |
|
341 err = property->Get( aValue ); |
|
342 } |
|
343 return err; |
|
344 } |
|
345 |
|
346 // ----------------------------------------------------------------------------- |
|
347 // CVcxConnUtilPubSub::CancelValueSubscribers() |
|
348 // ----------------------------------------------------------------------------- |
|
349 // |
|
350 void CVcxConnUtilPubSub::StartSubscibers( TUint32 aKey ) |
|
351 { |
|
352 if( !aKey ) |
|
353 { |
|
354 iConnectionStatePS->Start(); |
|
355 iMasterExistPS->Start(); |
|
356 iRoamingReqStatusPS->Start(); |
|
357 iNbrInstanceResp->Start(); |
|
358 } |
|
359 else |
|
360 { |
|
361 CVcxConnUtilSubscriber* subscriber = GetSubscriber( aKey ); |
|
362 if( subscriber ) |
|
363 { |
|
364 subscriber->Start(); |
|
365 } |
|
366 } |
|
367 } |
|
368 |
|
369 // ----------------------------------------------------------------------------- |
|
370 // CVcxConnUtilPubSub::CancelValueSubscribers() |
|
371 // ----------------------------------------------------------------------------- |
|
372 // |
|
373 void CVcxConnUtilPubSub::CancelValueSubscribers( TUint32 aKey ) |
|
374 { |
|
375 if( !aKey ) |
|
376 { |
|
377 iConnectionStatePS->Cancel(); |
|
378 iMasterExistPS->Cancel(); |
|
379 iRoamingReqStatusPS->Cancel(); |
|
380 iNbrInstanceResp->Cancel(); |
|
381 } |
|
382 else |
|
383 { |
|
384 CVcxConnUtilSubscriber* subscriber = GetSubscriber( aKey ); |
|
385 if( subscriber ) |
|
386 { |
|
387 subscriber->Cancel(); |
|
388 } |
|
389 } |
|
390 } |
|
391 // end of file |