|
1 /* |
|
2 * Copyright (c) 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: Network field intensity sensor implementation |
|
15 * |
|
16 */ |
|
17 |
|
18 #include <etel3rdparty.h> |
|
19 #include <e32math.h> |
|
20 #include "cnetworkfieldintensitysensor.h" |
|
21 #include "sensorlistener.h" |
|
22 #include "csensorproperties.h" |
|
23 #include "logger.h" |
|
24 |
|
25 const TInt KNISensorChannel = 0; |
|
26 const TInt KNISensorInvalidValue = 0; |
|
27 const TInt KNIMaxBars = 7; |
|
28 |
|
29 _LIT(KNetworkFieldIntensitySensorDescriptionPart1, |
|
30 "network_field_intensity#" |
|
31 L"device#" |
|
32 L"device#" |
|
33 L"Nokia#" |
|
34 L"description="); |
|
35 _LIT(KNetworkFieldIntensitySensorDescriptionPart2, |
|
36 "#model=Nokia#" |
|
37 L"connectiontype=1#" |
|
38 L"1#" // isAvailable |
|
39 L"1#" // channel count |
|
40 L"network_field_intensity#" // channel name. for single channel sensors should be same as model |
|
41 L"0#" // accuracy, 100 steps (?) |
|
42 L"2#" // data type. here TYPE_INT |
|
43 L"0#" // scale. here zero (means that scaling is not used) |
|
44 L"%#" // unit. percent (?) |
|
45 L"1#" // measurement range count |
|
46 L"0#" // smallest measurable value |
|
47 L"100#" // largest measurable value |
|
48 L"1#" // measurement resolution |
|
49 L"@ch_end#"); // channel terminator constant |
|
50 |
|
51 CNetworkFieldIntensitySensor* CNetworkFieldIntensitySensor::NewL() |
|
52 { |
|
53 JELOG2(ESensor); |
|
54 CNetworkFieldIntensitySensor* self = new(ELeave)CNetworkFieldIntensitySensor(); |
|
55 CleanupStack::PushL(self); |
|
56 self->ConstructL(); |
|
57 CleanupStack::Pop(); // self |
|
58 return self; |
|
59 } |
|
60 |
|
61 CNetworkFieldIntensitySensor::CNetworkFieldIntensitySensor() |
|
62 { |
|
63 JELOG2(ESensor); |
|
64 } |
|
65 |
|
66 void CNetworkFieldIntensitySensor::ConstructL() |
|
67 { |
|
68 JELOG2(ESensor); |
|
69 CSensorBase::ConstructL(); |
|
70 } |
|
71 |
|
72 CNetworkFieldIntensitySensor::~CNetworkFieldIntensitySensor() |
|
73 { |
|
74 JELOG2(ESensor); |
|
75 delete iNotifier; |
|
76 } |
|
77 |
|
78 int CNetworkFieldIntensitySensor::OpenChannel(SensorListener* aListener) |
|
79 { |
|
80 JELOG2(ESensor); |
|
81 iSensorListener = aListener; |
|
82 TRAPD(err, iNotifier = CSignalAsyncHandler::NewL(*this)); |
|
83 return err; // KErrNone if succesful |
|
84 } |
|
85 |
|
86 void CNetworkFieldIntensitySensor::StartDataListeningL() |
|
87 { |
|
88 JELOG2(ESensor); |
|
89 iNotifier->Cancel(); |
|
90 ResetAccumulationData(); |
|
91 iNotifier->SetDataListeningMode(ETrue); |
|
92 } |
|
93 |
|
94 void CNetworkFieldIntensitySensor::CancelDataListeningL() |
|
95 { |
|
96 JELOG2(ESensor); |
|
97 iNotifier->Cancel(); |
|
98 iNotifier->SetDataListeningMode(EFalse); |
|
99 } |
|
100 |
|
101 void CNetworkFieldIntensitySensor::CloseChannelL() |
|
102 { |
|
103 JELOG2(ESensor); |
|
104 iSensorListener = NULL; |
|
105 delete iNotifier; |
|
106 iNotifier = NULL; |
|
107 } |
|
108 |
|
109 void CNetworkFieldIntensitySensor::StartConditionListeningL() |
|
110 { |
|
111 JELOG2(ESensor); |
|
112 iNotifier->Cancel(); |
|
113 iNotifier->SetConditionListeningMode(ETrue); |
|
114 } |
|
115 |
|
116 void CNetworkFieldIntensitySensor::StopConditionListeningL() |
|
117 { |
|
118 JELOG2(ESensor); |
|
119 iNotifier->Cancel(); |
|
120 iNotifier->SetConditionListeningMode(EFalse); |
|
121 } |
|
122 |
|
123 void CNetworkFieldIntensitySensor::ResetAccumulationData() |
|
124 { |
|
125 JELOG2(ESensor); |
|
126 iData[ KNISensorChannel ]->iNumOfValues = 0; |
|
127 iStartTime = KErrNotFound; |
|
128 } |
|
129 |
|
130 void CNetworkFieldIntensitySensor::SendData() |
|
131 { |
|
132 JELOG2(ESensor); |
|
133 if (iIsOneShot) |
|
134 { |
|
135 iNotifier->Cancel(); |
|
136 iNotifier->SetDataListeningMode(EFalse); |
|
137 } |
|
138 iSensorListener->DataReceived(iData, EFalse); |
|
139 ResetAccumulationData(); |
|
140 } |
|
141 |
|
142 void CNetworkFieldIntensitySensor::SignalLevelReceivedL(TInt aLevel) |
|
143 { |
|
144 JELOG2(ESensor); |
|
145 __ASSERT_DEBUG(iData != NULL, User::Invariant()); |
|
146 SensorData* data = iData[ KNISensorChannel ]; |
|
147 |
|
148 TTime currentTime; |
|
149 if (data->iTimeStamps || (iBufferingPeriod > 0)) |
|
150 { |
|
151 currentTime.UniversalTime(); |
|
152 if (iStartTime == KErrNotFound) |
|
153 { |
|
154 iStartTime = currentTime; |
|
155 } |
|
156 } |
|
157 |
|
158 // P&S sensors are assumed to have one channel only |
|
159 TInt interpretedValue = InterpretValue(aLevel); |
|
160 |
|
161 // Negative values are error codes and therefore marked |
|
162 // as invalid. Returned value for invalid values is zero. |
|
163 if (interpretedValue < 0) |
|
164 { |
|
165 data->iIntValues[ data->iNumOfValues ] = KNISensorInvalidValue; |
|
166 if (data->iValiditiesIncluded) |
|
167 { |
|
168 data->iValidities[ data->iNumOfValues ] = EFalse; |
|
169 } |
|
170 } |
|
171 else |
|
172 { |
|
173 data->iIntValues[ data->iNumOfValues ] = interpretedValue; |
|
174 if (data->iValiditiesIncluded) |
|
175 { |
|
176 data->iValidities[ data->iNumOfValues ] = ETrue; |
|
177 } |
|
178 } |
|
179 |
|
180 if (data->iTimeStampsIncluded) |
|
181 { |
|
182 data->iTimeStamps[ data->iNumOfValues ] = TimeStamp(currentTime); |
|
183 } |
|
184 data->iNumOfValues++; |
|
185 |
|
186 TInt64 intervalFromStart = -1; |
|
187 if (iBufferingPeriod > 0) |
|
188 { |
|
189 intervalFromStart = currentTime.MicroSecondsFrom(iStartTime).Int64(); |
|
190 } |
|
191 |
|
192 if ((data->iNumOfValues == iBufferSize) || |
|
193 (iBufferingPeriod > 0 && intervalFromStart > iBufferingPeriod)) |
|
194 { |
|
195 SendData(); |
|
196 } |
|
197 } |
|
198 |
|
199 void CNetworkFieldIntensitySensor::SignalLevelChangedL(TInt aLevel) |
|
200 { |
|
201 JELOG2(ESensor); |
|
202 // Values lower than zero are error code and do not qualify for |
|
203 // signal change notification |
|
204 if (aLevel >= 0) |
|
205 { |
|
206 EvaluateConditions(aLevel, KNISensorChannel); |
|
207 } |
|
208 } |
|
209 |
|
210 HBufC* CNetworkFieldIntensitySensor::CreateDescriptionLC() |
|
211 { |
|
212 JELOG2(ESensor); |
|
213 HBufC* desc = HBufC::NewLC(KMaxSensorDescriptionLength); |
|
214 TPtr desPtr = desc->Des(); |
|
215 desPtr.Append(KNetworkFieldIntensitySensorDescriptionPart1); |
|
216 desPtr.Append(SensorProperties::GetPropertyString(KSensorDescription, |
|
217 ENetworkFieldIntensitySensor)); |
|
218 desPtr.Append(KNetworkFieldIntensitySensorDescriptionPart2); |
|
219 return desc; |
|
220 } |
|
221 |
|
222 CSensorBase* CNetworkFieldIntensitySensor::DuplicateL() |
|
223 { |
|
224 JELOG2(ESensor); |
|
225 return NewL(); |
|
226 } |
|
227 |
|
228 TReal CNetworkFieldIntensitySensor::InterpretValue(TReal aValue) |
|
229 { |
|
230 JELOG2(ESensor); |
|
231 // Network intensity is between 0 and 100. Values below zero |
|
232 // are error codes and are returned as they are. |
|
233 return aValue > 0 ? aValue / KNIMaxBars * 100 : aValue; |
|
234 } |
|
235 |
|
236 // ************************************************************************** |
|
237 // CSignalAsyncHandler |
|
238 // ************************************************************************** |
|
239 |
|
240 CNetworkFieldIntensitySensor::CSignalAsyncHandler* |
|
241 CNetworkFieldIntensitySensor::CSignalAsyncHandler::NewL( |
|
242 CNetworkFieldIntensitySensor& aSensor) |
|
243 { |
|
244 JELOG2(ESensor); |
|
245 CSignalAsyncHandler* self = |
|
246 new(ELeave)CSignalAsyncHandler(aSensor); |
|
247 CleanupStack::PushL(self); |
|
248 self->ConstructL(); |
|
249 CleanupStack::Pop(); // self |
|
250 return self; |
|
251 } |
|
252 |
|
253 CNetworkFieldIntensitySensor::CSignalAsyncHandler::CSignalAsyncHandler( |
|
254 CNetworkFieldIntensitySensor& aSensor) |
|
255 : CActive(EPriorityStandard), iSensor(aSensor), |
|
256 iSigStrengthV1Pckg(iSigStrengthV1), iCurrentOperation(EOperationNone), |
|
257 iPrevSignalStrength(KErrNotFound) |
|
258 { |
|
259 JELOG2(ESensor); |
|
260 } |
|
261 |
|
262 CNetworkFieldIntensitySensor::CSignalAsyncHandler::~CSignalAsyncHandler() |
|
263 { |
|
264 JELOG2(ESensor); |
|
265 Cancel(); |
|
266 delete iTelephony; |
|
267 } |
|
268 |
|
269 void CNetworkFieldIntensitySensor::CSignalAsyncHandler::ConstructL() |
|
270 { |
|
271 JELOG2(ESensor); |
|
272 iTelephony = CTelephony::NewL(); |
|
273 CActiveScheduler::Add(this); |
|
274 } |
|
275 |
|
276 void CNetworkFieldIntensitySensor::CSignalAsyncHandler::RunL() |
|
277 { |
|
278 JELOG2(ESensor); |
|
279 #ifdef __WINS__ |
|
280 #pragma message("CNetworkFieldIntensitySensor: Test value used for WINS.") |
|
281 TInt signalStrength = 1; |
|
282 #else |
|
283 TInt signalStrength = (iStatus == KErrNone) ? |
|
284 iSigStrengthV1.iBar : KErrNotFound; |
|
285 #endif |
|
286 |
|
287 if (iDataListening) |
|
288 { |
|
289 iCurrentOperation = EGetSignalIntensity; |
|
290 iTelephony->GetSignalStrength(iStatus, iSigStrengthV1Pckg); |
|
291 SetActive(); |
|
292 iSensor.SignalLevelReceivedL(signalStrength); |
|
293 if (iConditionListening && (signalStrength != iPrevSignalStrength)) |
|
294 { |
|
295 iSensor.SignalLevelChangedL(signalStrength); |
|
296 } |
|
297 } |
|
298 else if (iConditionListening) |
|
299 { |
|
300 iCurrentOperation = ENotifySignalIntensityChange; |
|
301 iTelephony->NotifyChange(iStatus, |
|
302 CTelephony::ESignalStrengthChange, iSigStrengthV1Pckg); |
|
303 SetActive(); |
|
304 iSensor.SignalLevelChangedL(signalStrength); |
|
305 } |
|
306 iPrevSignalStrength = signalStrength; |
|
307 } |
|
308 |
|
309 void CNetworkFieldIntensitySensor::CSignalAsyncHandler::DoCancel() |
|
310 { |
|
311 JELOG2(ESensor); |
|
312 if (iCurrentOperation == EGetSignalIntensity) |
|
313 { |
|
314 iTelephony->CancelAsync(CTelephony::EGetSignalStrengthCancel); |
|
315 } |
|
316 else if (iCurrentOperation == ENotifySignalIntensityChange) |
|
317 { |
|
318 iTelephony->CancelAsync(CTelephony::ESignalStrengthChangeCancel); |
|
319 } |
|
320 iCurrentOperation = EOperationNone; |
|
321 } |
|
322 |
|
323 void CNetworkFieldIntensitySensor::CSignalAsyncHandler:: |
|
324 SetDataListeningMode(TBool aIsDataListening) |
|
325 { |
|
326 JELOG2(ESensor); |
|
327 __ASSERT_DEBUG(!IsActive(), User::Invariant()); |
|
328 iDataListening = aIsDataListening; |
|
329 Start(); |
|
330 } |
|
331 |
|
332 void CNetworkFieldIntensitySensor::CSignalAsyncHandler:: |
|
333 SetConditionListeningMode(TBool aIsConditionListening) |
|
334 { |
|
335 JELOG2(ESensor); |
|
336 __ASSERT_DEBUG(!IsActive(), User::Invariant()); |
|
337 iConditionListening = aIsConditionListening; |
|
338 Start(); |
|
339 } |
|
340 void CNetworkFieldIntensitySensor::CSignalAsyncHandler::Start() |
|
341 { |
|
342 JELOG2(ESensor); |
|
343 if (iDataListening || iConditionListening) |
|
344 { |
|
345 iPrevSignalStrength = KErrNotFound; |
|
346 iCurrentOperation = EGetSignalIntensity; |
|
347 iTelephony->GetSignalStrength(iStatus, iSigStrengthV1Pckg); |
|
348 SetActive(); |
|
349 } |
|
350 } |