|
1 /* |
|
2 * Copyright (c) 2006-2008 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: CSensrvConditionHandler implementation |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 #include "sensrvconditionhandler.h" |
|
21 #include "sensrvtrace.h" |
|
22 #include "sensrvconditionhandlerobserver.h" |
|
23 #include "sensrvdefines.h" |
|
24 |
|
25 |
|
26 |
|
27 // ======== MEMBER FUNCTIONS ======== |
|
28 |
|
29 // --------------------------------------------------------------------------- |
|
30 // C++ constructor |
|
31 // --------------------------------------------------------------------------- |
|
32 // |
|
33 CSensrvConditionHandler::CSensrvConditionHandler( RSensrvClient& aSensrvClient, |
|
34 const TSensrvChannelInfo& aChannelInfo ) |
|
35 : CActive( EPriorityStandard ), |
|
36 iSensrvClient( aSensrvClient ), |
|
37 iChannelInfo( aChannelInfo ) |
|
38 { |
|
39 |
|
40 CActiveScheduler::Add( this ); |
|
41 |
|
42 } |
|
43 |
|
44 |
|
45 // --------------------------------------------------------------------------- |
|
46 // 2nd phase of construction |
|
47 // --------------------------------------------------------------------------- |
|
48 // |
|
49 void CSensrvConditionHandler::ConstructL() |
|
50 { |
|
51 iWriteBuffer = HBufC8::NewL(iChannelInfo.iDataItemSize); |
|
52 iWriteBufferPtr = new (ELeave) TPtr8(iWriteBuffer->Des()); |
|
53 } |
|
54 |
|
55 |
|
56 // --------------------------------------------------------------------------- |
|
57 // 2-phase constructor |
|
58 // --------------------------------------------------------------------------- |
|
59 // |
|
60 CSensrvConditionHandler* CSensrvConditionHandler::NewL( RSensrvClient& aSensrvClient, |
|
61 const TSensrvChannelInfo& aChannelInfo ) |
|
62 { |
|
63 CSensrvConditionHandler* self = CSensrvConditionHandler::NewLC( aSensrvClient, aChannelInfo ); |
|
64 CleanupStack::Pop( self ); |
|
65 return self; |
|
66 } |
|
67 |
|
68 |
|
69 // --------------------------------------------------------------------------- |
|
70 // 2-phase constructor |
|
71 // --------------------------------------------------------------------------- |
|
72 // |
|
73 CSensrvConditionHandler* CSensrvConditionHandler::NewLC( RSensrvClient& aSensrvClient, |
|
74 const TSensrvChannelInfo& aChannelInfo ) |
|
75 { |
|
76 CSensrvConditionHandler* self = new( ELeave ) CSensrvConditionHandler( aSensrvClient, aChannelInfo ); |
|
77 CleanupStack::PushL( self ); |
|
78 self->ConstructL(); |
|
79 return self; |
|
80 } |
|
81 |
|
82 |
|
83 // --------------------------------------------------------------------------- |
|
84 // Destructor |
|
85 // --------------------------------------------------------------------------- |
|
86 // |
|
87 CSensrvConditionHandler::~CSensrvConditionHandler() |
|
88 { |
|
89 Cancel(); |
|
90 |
|
91 delete iWriteBuffer; |
|
92 delete iWriteBufferPtr; |
|
93 } |
|
94 |
|
95 |
|
96 // --------------------------------------------------------------------------- |
|
97 // From class CActive |
|
98 // --------------------------------------------------------------------------- |
|
99 // |
|
100 void CSensrvConditionHandler::DoCancel() |
|
101 { |
|
102 COMPONENT_TRACE( ( _L( "Sensrv Client - CSensrvConditionHandler::DoCancel()" ) ) ); |
|
103 |
|
104 iListening = EFalse; |
|
105 iSensrvClient.SendReceiveSync( ESensrvSrvReqStopConditionListening, |
|
106 TIpcArgs( iChannelInfo.iChannelId ) ); |
|
107 |
|
108 COMPONENT_TRACE( ( _L( "Sensrv Client - CSensrvConditionHandler::DoCancel - return void" ) ) ); |
|
109 } |
|
110 |
|
111 // --------------------------------------------------------------------------- |
|
112 // From class CActive |
|
113 // --------------------------------------------------------------------------- |
|
114 // |
|
115 void CSensrvConditionHandler::RunL() |
|
116 { |
|
117 COMPONENT_TRACE( ( _L( "Sensrv Client - CSensrvConditionHandler::RunL - iStatus %d" ), iStatus.Int() ) ); |
|
118 Deque(); |
|
119 CActiveScheduler::Add(this); |
|
120 if( iStatus.Int() == KErrNone ) |
|
121 { |
|
122 if( iErrorCount > 0 ) |
|
123 { |
|
124 iErrorCount = 0; |
|
125 } |
|
126 |
|
127 // Extract id from response |
|
128 TInt metSetId = iMetSetIdBuf(); |
|
129 |
|
130 // Notify to listener |
|
131 // Note: Callback is done intentionally before resending async request, |
|
132 // so that we don't need duplicate buffer like in data reading. |
|
133 // It shouldn't have noticeable effect on performance in real use cases, |
|
134 // and reduces overhead, as condition listening is quite often stopped |
|
135 // inside ConditionMet method. |
|
136 iConditionObserver->ConditionMet(metSetId, *iWriteBuffer); |
|
137 |
|
138 CreateAndSendRequest(); |
|
139 } |
|
140 else |
|
141 { |
|
142 if( iErrorCount == 0 ) |
|
143 { |
|
144 iErrorCount++; |
|
145 CreateAndSendRequest(); |
|
146 |
|
147 // Send minor error |
|
148 iConditionObserver->ConditionError( ESensrvErrorSeverityMinor ); |
|
149 |
|
150 } |
|
151 else if( iErrorCount == 1 ) |
|
152 { |
|
153 iErrorCount++; |
|
154 |
|
155 // Reopen channel |
|
156 iConditionObserver->ReopenChannelForConditionListeningL(); |
|
157 |
|
158 TSensrvStartListeningParametersPckgBuf startListeningPckg( iListeningParameters ); |
|
159 |
|
160 TInt err = iSensrvClient.SendReceiveSync( ESensrvSrvReqStartConditionListening, |
|
161 TIpcArgs( iChannelInfo.iChannelId, &startListeningPckg ) ); |
|
162 if( err ) |
|
163 { |
|
164 // send fatal error |
|
165 iListening = EFalse; |
|
166 iConditionObserver->ConditionError( ESensrvErrorSeverityFatal ); |
|
167 } |
|
168 else |
|
169 { |
|
170 // Start listening |
|
171 CreateAndSendRequest(); |
|
172 } |
|
173 } |
|
174 |
|
175 else |
|
176 { |
|
177 // Send fatal error |
|
178 iListening = EFalse; |
|
179 iConditionObserver->ConditionError( ESensrvErrorSeverityFatal ); |
|
180 } |
|
181 } |
|
182 } |
|
183 |
|
184 // --------------------------------------------------------------------------- |
|
185 // CSensrvConditionHandler::RunError |
|
186 // --------------------------------------------------------------------------- |
|
187 // |
|
188 TInt CSensrvConditionHandler::RunError( TInt /*aError*/ ) |
|
189 { |
|
190 COMPONENT_TRACE( ( _L( "Sensrv Client - CSensrvConditionHandler::RunError" ) ) ); |
|
191 |
|
192 // Send fatal error |
|
193 iListening = EFalse; |
|
194 iConditionObserver->ConditionError( ESensrvErrorSeverityFatal ); |
|
195 return KErrNone; |
|
196 } |
|
197 |
|
198 |
|
199 // --------------------------------------------------------------------------- |
|
200 // CSensrvConditionHandler::StartListening |
|
201 // --------------------------------------------------------------------------- |
|
202 // |
|
203 void CSensrvConditionHandler::StartListening( MSensrvConditionHandlerObserver* aConditionObserver, |
|
204 TSensrvStartListeningParameters aListeningParameters) |
|
205 { |
|
206 COMPONENT_TRACE( ( _L( "Sensrv Client - CSensrvConditionHandler::StartListening - Start" ) ) ); |
|
207 |
|
208 if( !iListening ) |
|
209 { |
|
210 iConditionObserver = aConditionObserver; |
|
211 iListeningParameters = aListeningParameters; |
|
212 iListening = ETrue; |
|
213 |
|
214 iErrorCount = 0; |
|
215 |
|
216 CreateAndSendRequest(); |
|
217 } |
|
218 |
|
219 COMPONENT_TRACE( ( _L( "Sensrv Client - CSensrvConditionHandler::StartListening - Return" ) ) ); |
|
220 } |
|
221 |
|
222 // --------------------------------------------------------------------------- |
|
223 // CSensrvConditionHandler::CreateAndSendRequestL |
|
224 // --------------------------------------------------------------------------- |
|
225 // |
|
226 void CSensrvConditionHandler::CreateAndSendRequest() |
|
227 { |
|
228 COMPONENT_TRACE( ( _L( "Sensrv Client - CSensrvConditionHandler::CreateAndSendRequestL - Start" ) ) ); |
|
229 |
|
230 if (iListening && !IsActive() ) |
|
231 { |
|
232 iSensrvClient.SendReceiveAsync( ESensrvSrvReqAsyncConditionMet, |
|
233 TIpcArgs( iChannelInfo.iChannelId, iWriteBufferPtr, &iMetSetIdBuf ), iStatus ); |
|
234 |
|
235 SetActive(); |
|
236 } |
|
237 |
|
238 COMPONENT_TRACE( ( _L( "Sensrv Client - CSensrvConditionHandler::CreateAndSendRequestL - Return" ) ) ); |
|
239 } |
|
240 |