|
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: List of SensorConnections |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 // INTERNAL INCLUDES |
|
20 #include "sensorconnection.h" |
|
21 #include "sensor.h" |
|
22 #include "sensornativeconstants.h" |
|
23 #include "logger.h" |
|
24 |
|
25 const int KMax_buffersize = 256; |
|
26 SensorConnection::SensorConnection(Sensor* aSensor) : |
|
27 iSensor(aSensor), |
|
28 iParams(0), |
|
29 iBufferSize(-1) |
|
30 { |
|
31 JELOG2(ESensor); |
|
32 } |
|
33 |
|
34 SensorConnection::~SensorConnection() |
|
35 { |
|
36 JELOG2(ESensor); |
|
37 if (iParams) |
|
38 { |
|
39 ClearData(iParams->iDatas, iParams->iDataCount); |
|
40 } |
|
41 delete iParams; |
|
42 delete iSensor; |
|
43 } |
|
44 |
|
45 void SensorConnection::DataReceived(SensorData** aData, bool aIsDataLost) |
|
46 { |
|
47 JELOG2(ESensor); |
|
48 DataFillerParams* params = iParams; |
|
49 if (params) |
|
50 { |
|
51 params->iJniEnv = iJniEnv; |
|
52 params->iDatas = aData; |
|
53 params->iIsDataLost = aIsDataLost; |
|
54 iDataReceived(params); |
|
55 } |
|
56 } |
|
57 |
|
58 void SensorConnection::ConditionMet(void* aHandle, int aChannelId, |
|
59 double aValue, long aTimeStamp) |
|
60 { |
|
61 JELOG2(ESensor); |
|
62 iConditionMet(iJniEnv, iJavaPeer, |
|
63 aHandle, aChannelId, aValue, aTimeStamp); |
|
64 } |
|
65 |
|
66 Sensor* SensorConnection::GetSensor() |
|
67 { |
|
68 JELOG2(ESensor); |
|
69 return iSensor; |
|
70 } |
|
71 |
|
72 int SensorConnection::PrepareDataListening(void* aNewDataObjects, |
|
73 int aDataCount, |
|
74 void*& aOldDataObject, |
|
75 int aDataType) |
|
76 { |
|
77 JELOG2(ESensor); |
|
78 if (!iParams) |
|
79 { |
|
80 iParams = new DataFillerParams(); |
|
81 if (!iParams) |
|
82 { |
|
83 return ERROR_NOMEMORY; |
|
84 } |
|
85 iParams->iDataCount = aDataCount; |
|
86 iParams->iJavaPeer = iJavaPeer; |
|
87 iParams->iDatas = AllocateData(KMax_buffersize, |
|
88 aDataType); |
|
89 if (!iParams->iDatas) |
|
90 { |
|
91 delete iParams; |
|
92 iParams = 0; |
|
93 return ERROR_NOMEMORY; |
|
94 } |
|
95 } |
|
96 aOldDataObject = iParams->iDataObjects; |
|
97 iParams->iDataObjects = aNewDataObjects; |
|
98 return ERROR_NONE; |
|
99 } |
|
100 |
|
101 int SensorConnection::StartDataListening(int aBufferSize, |
|
102 long aBufferingPeriod, |
|
103 bool aTimestampsIncluded, |
|
104 bool aValiditiesIncluded, |
|
105 bool aIsOneShot) |
|
106 { |
|
107 JELOG2(ESensor); |
|
108 for (int i = 0; i < iParams->iDataCount; i++) |
|
109 { |
|
110 SensorData* data = iParams->iDatas[ i ]; |
|
111 |
|
112 data->iBufferSize = aBufferSize; |
|
113 data->iTimeStampsIncluded = aTimestampsIncluded; |
|
114 data->iValiditiesIncluded = aValiditiesIncluded; |
|
115 } |
|
116 iSensor->StartDataListening(iParams->iDatas, aBufferSize, aBufferingPeriod, |
|
117 aTimestampsIncluded, aValiditiesIncluded, |
|
118 aIsOneShot); |
|
119 return ERROR_NONE; |
|
120 } |
|
121 |
|
122 void* SensorConnection::DataObjects() |
|
123 { |
|
124 JELOG2(ESensor); |
|
125 void* dataObjects = 0; |
|
126 if (iParams) |
|
127 { |
|
128 dataObjects = iParams->iDataObjects; |
|
129 } |
|
130 return dataObjects; |
|
131 } |
|
132 |
|
133 SensorData** SensorConnection::AllocateData(int aBufferSize, |
|
134 int aDataType) |
|
135 { |
|
136 JELOG2(ESensor); |
|
137 SensorData** senData = new SensorData*[ iParams->iDataCount ]; |
|
138 if (!senData) |
|
139 { |
|
140 return 0; |
|
141 } |
|
142 // Null all datas before continuing |
|
143 for (int i = 0; i < iParams->iDataCount; i++) |
|
144 { |
|
145 senData[ i ] = 0; |
|
146 } |
|
147 // create data containers |
|
148 for (int i = 0; i < iParams->iDataCount; i++) |
|
149 { |
|
150 senData[ i ] = new SensorData(); |
|
151 if (!senData[ i ]) |
|
152 { |
|
153 ClearData(senData, iParams->iDataCount); |
|
154 return 0; |
|
155 } |
|
156 // Data allocation |
|
157 if (aDataType == 2) // Type 2 for INT |
|
158 { |
|
159 senData[ i ]->iIntValues = new int[ aBufferSize ]; |
|
160 if (!senData[ i ]->iIntValues) |
|
161 { |
|
162 ClearData(senData, iParams->iDataCount); |
|
163 return 0; |
|
164 } |
|
165 } |
|
166 else |
|
167 { |
|
168 senData[ i ]->iDoubleValues = new double[ aBufferSize ]; |
|
169 if (!senData[ i ]->iDoubleValues) |
|
170 { |
|
171 ClearData(senData, iParams->iDataCount); |
|
172 return 0; |
|
173 } |
|
174 } |
|
175 // validities |
|
176 senData[ i ]->iValidities = new bool[ aBufferSize ]; |
|
177 if (!senData[ i ]->iValidities) |
|
178 { |
|
179 ClearData(senData, iParams->iDataCount); |
|
180 return 0; |
|
181 } |
|
182 // fill with trues by defaults |
|
183 for (int j = 0; j < aBufferSize; j++) |
|
184 { |
|
185 senData[ i ]->iValidities[ j ] = true; |
|
186 } |
|
187 |
|
188 // Time stamps |
|
189 senData[ i ]->iTimeStamps = new long long[ aBufferSize ]; |
|
190 if (!senData[ i ]->iTimeStamps) |
|
191 { |
|
192 ClearData(senData, iParams->iDataCount); |
|
193 return 0; |
|
194 } |
|
195 } |
|
196 return senData; |
|
197 } |
|
198 |
|
199 void SensorConnection::ClearData(SensorData** aDatas, int aCount) |
|
200 { |
|
201 JELOG2(ESensor); |
|
202 if (!aDatas) |
|
203 { |
|
204 return; |
|
205 } |
|
206 |
|
207 for (int i = 0; i < aCount; i++) |
|
208 { |
|
209 delete aDatas[ i ]; |
|
210 } |
|
211 delete [] aDatas; |
|
212 } |
|
213 // End of file |