1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "sensor.h"
17
18 #include "sensor_errors.h"
19
20 #undef LOG_TAG
21 #define LOG_TAG "Sensor"
22
23 namespace OHOS {
24 namespace Sensors {
25 using namespace OHOS::HiviewDFX;
26
Sensor()27 Sensor::Sensor()
28 : sensorId_(0),
29 sensorTypeId_(0),
30 sensorName_(""),
31 vendorName_(""),
32 firmwareVersion_(""),
33 hardwareVersion_(""),
34 maxRange_(0.0),
35 resolution_(0.0),
36 power_(0.0),
37 flags_(0),
38 fifoMaxEventCount_(0),
39 minSamplePeriodNs_(0),
40 maxSamplePeriodNs_(0),
41 deviceId_(0),
42 location_(0)
43 {}
44
GetSensorId() const45 int32_t Sensor::GetSensorId() const
46 {
47 return sensorId_;
48 }
49
SetSensorId(int32_t sensorId)50 void Sensor::SetSensorId(int32_t sensorId)
51 {
52 sensorId_ = sensorId;
53 }
54
GetSensorTypeId() const55 int32_t Sensor::GetSensorTypeId() const
56 {
57 return sensorTypeId_;
58 }
59
SetSensorTypeId(int32_t sensorTypeId)60 void Sensor::SetSensorTypeId(int32_t sensorTypeId)
61 {
62 sensorTypeId_ = sensorTypeId;
63 }
64
GetSensorName() const65 std::string Sensor::GetSensorName() const
66 {
67 return sensorName_;
68 }
69
SetSensorName(const std::string & sensorName)70 void Sensor::SetSensorName(const std::string &sensorName)
71 {
72 sensorName_ = sensorName;
73 }
74
GetVendorName() const75 std::string Sensor::GetVendorName() const
76 {
77 return vendorName_;
78 }
79
SetVendorName(const std::string & vendorName)80 void Sensor::SetVendorName(const std::string &vendorName)
81 {
82 vendorName_ = vendorName;
83 }
84
GetHardwareVersion() const85 std::string Sensor::GetHardwareVersion() const
86 {
87 return hardwareVersion_;
88 }
89
SetHardwareVersion(const std::string & hardwareVersion)90 void Sensor::SetHardwareVersion(const std::string &hardwareVersion)
91 {
92 hardwareVersion_ = hardwareVersion;
93 }
94
GetFirmwareVersion() const95 std::string Sensor::GetFirmwareVersion() const
96 {
97 return firmwareVersion_;
98 }
99
SetFirmwareVersion(const std::string & firmwareVersion)100 void Sensor::SetFirmwareVersion(const std::string &firmwareVersion)
101 {
102 firmwareVersion_ = firmwareVersion;
103 }
104
GetMaxRange() const105 float Sensor::GetMaxRange() const
106 {
107 return maxRange_;
108 }
109
SetMaxRange(float maxRange)110 void Sensor::SetMaxRange(float maxRange)
111 {
112 maxRange_ = maxRange;
113 }
114
GetResolution() const115 float Sensor::GetResolution() const
116 {
117 return resolution_;
118 }
119
SetResolution(float resolution)120 void Sensor::SetResolution(float resolution)
121 {
122 resolution_ = resolution;
123 }
124
GetPower() const125 float Sensor::GetPower() const
126 {
127 return power_;
128 }
129
SetPower(float power)130 void Sensor::SetPower(float power)
131 {
132 power_ = power;
133 }
134
GetFlags() const135 uint32_t Sensor::Sensor::GetFlags() const
136 {
137 return flags_;
138 }
139
SetFlags(uint32_t flags)140 void Sensor::SetFlags(uint32_t flags)
141 {
142 flags_ = flags;
143 }
144
GetFifoMaxEventCount() const145 int32_t Sensor::GetFifoMaxEventCount() const
146 {
147 return fifoMaxEventCount_;
148 }
149
SetFifoMaxEventCount(int32_t fifoMaxEventCount)150 void Sensor::SetFifoMaxEventCount(int32_t fifoMaxEventCount)
151 {
152 fifoMaxEventCount_ = fifoMaxEventCount;
153 }
154
GetMinSamplePeriodNs() const155 int64_t Sensor::GetMinSamplePeriodNs() const
156 {
157 return minSamplePeriodNs_;
158 }
159
SetMinSamplePeriodNs(int64_t minSamplePeriodNs)160 void Sensor::SetMinSamplePeriodNs(int64_t minSamplePeriodNs)
161 {
162 minSamplePeriodNs_ = minSamplePeriodNs;
163 }
164
GetMaxSamplePeriodNs() const165 int64_t Sensor::GetMaxSamplePeriodNs() const
166 {
167 return maxSamplePeriodNs_;
168 }
169
SetMaxSamplePeriodNs(int64_t maxSamplePeriodNs)170 void Sensor::SetMaxSamplePeriodNs(int64_t maxSamplePeriodNs)
171 {
172 maxSamplePeriodNs_ = maxSamplePeriodNs;
173 }
174
GetDeviceId() const175 int32_t Sensor::GetDeviceId() const
176 {
177 return deviceId_;
178 }
179
SetDeviceId(int32_t deviceId)180 void Sensor::SetDeviceId(int32_t deviceId)
181 {
182 deviceId_ = deviceId;
183 }
184
GetLocation() const185 int32_t Sensor::GetLocation() const
186 {
187 return location_;
188 }
189
SetLocation(int32_t location)190 void Sensor::SetLocation(int32_t location)
191 {
192 location_ = location;
193 }
194
Marshalling(Parcel & parcel) const195 bool Sensor::Marshalling(Parcel &parcel) const
196 {
197 if (!parcel.WriteInt32(sensorId_)) {
198 SEN_HILOGE("Failed, write sensorId failed");
199 return false;
200 }
201 if (!parcel.WriteInt32(sensorTypeId_)) {
202 SEN_HILOGE("Failed, write sensorTypeId failed");
203 return false;
204 }
205 if (!parcel.WriteString(sensorName_)) {
206 SEN_HILOGE("Failed, write sensorName failed");
207 return false;
208 }
209 if (!parcel.WriteString(vendorName_)) {
210 SEN_HILOGE("Failed, write vendorName failed");
211 return false;
212 }
213 if (!parcel.WriteString(firmwareVersion_)) {
214 SEN_HILOGE("Failed, write firmwareVersion failed");
215 return false;
216 }
217 if (!parcel.WriteString(hardwareVersion_)) {
218 SEN_HILOGE("Failed, write hardwareVersion failed");
219 return false;
220 }
221 if (!parcel.WriteFloat(maxRange_)) {
222 SEN_HILOGE("Failed, write maxRange failed");
223 return false;
224 }
225 if (!parcel.WriteFloat(resolution_)) {
226 SEN_HILOGE("Failed, write resolution failed");
227 return false;
228 }
229 if (!parcel.WriteFloat(power_)) {
230 SEN_HILOGE("Failed, write power failed");
231 return false;
232 }
233 if (!parcel.WriteUint32(flags_)) {
234 SEN_HILOGE("Failed, write flags failed");
235 return false;
236 }
237 if (!parcel.WriteInt32(fifoMaxEventCount_)) {
238 SEN_HILOGE("Failed, write fifoMaxEventCount failed");
239 return false;
240 }
241 if (!parcel.WriteInt64(minSamplePeriodNs_)) {
242 SEN_HILOGE("Failed, write minSamplePeriodNs failed");
243 return false;
244 }
245 if (!parcel.WriteInt64(maxSamplePeriodNs_)) {
246 SEN_HILOGE("Failed, write maxSamplePeriodNs failed");
247 return false;
248 }
249 if (!parcel.WriteInt32(deviceId_)) {
250 SEN_HILOGE("Failed, write deviceId failed");
251 return false;
252 }
253 if (!parcel.WriteInt32(location_)) {
254 SEN_HILOGE("Failed, write location_ failed");
255 return false;
256 }
257 return true;
258 }
259
Unmarshalling(Parcel & parcel)260 Sensor* Sensor::Unmarshalling(Parcel &parcel)
261 {
262 auto sensor = new (std::nothrow) Sensor();
263 if (sensor != nullptr && !sensor->ReadFromParcel(parcel)) {
264 SEN_HILOGE("ReadFromParcel is failed");
265 delete sensor;
266 sensor = nullptr;
267 }
268 return sensor;
269 }
270
ReadFromParcel(Parcel & parcel)271 bool Sensor::ReadFromParcel(Parcel &parcel)
272 {
273 if ((!parcel.ReadInt32(sensorId_)) ||
274 (!parcel.ReadInt32(sensorTypeId_)) ||
275 (!parcel.ReadString(sensorName_)) ||
276 (!parcel.ReadString(vendorName_)) ||
277 (!parcel.ReadString(firmwareVersion_)) ||
278 (!parcel.ReadString(hardwareVersion_)) ||
279 (!parcel.ReadFloat(power_)) ||
280 (!parcel.ReadFloat(maxRange_)) ||
281 (!parcel.ReadFloat(resolution_)) ||
282 (!parcel.ReadUint32(flags_)) ||
283 (!parcel.ReadInt32(fifoMaxEventCount_)) ||
284 (!parcel.ReadInt64(minSamplePeriodNs_)) ||
285 (!parcel.ReadInt64(maxSamplePeriodNs_)) ||
286 (!parcel.ReadInt32(deviceId_)) ||
287 (!parcel.ReadInt32(location_))) {
288 return false;
289 }
290 return true;
291 }
292
SensorDescriptionIPC()293 SensorDescriptionIPC::SensorDescriptionIPC()
294 :deviceId(0), sensorType(0), sensorId(0), location(0)
295 {}
296
SensorDescriptionIPC(int32_t deviceId,int32_t sensorTypeId,int32_t sensorId,int32_t location)297 SensorDescriptionIPC::SensorDescriptionIPC(int32_t deviceId, int32_t sensorTypeId, int32_t sensorId, int32_t location)
298 :deviceId(deviceId), sensorType(sensorTypeId), sensorId(sensorId), location(location)
299 {}
300
Marshalling(Parcel & parcel) const301 bool SensorDescriptionIPC::Marshalling(Parcel &parcel) const
302 {
303 if (!parcel.WriteInt32(deviceId)) {
304 SEN_HILOGE("Failed, write deviceId failed");
305 return false;
306 }
307 if (!parcel.WriteInt32(sensorType)) {
308 SEN_HILOGE("Failed, write sensorTypeId failed");
309 return false;
310 }
311 if (!parcel.WriteInt32(sensorId)) {
312 SEN_HILOGE("Failed, write sensorId failed");
313 return false;
314 }
315 if (!parcel.WriteInt32(location)) {
316 SEN_HILOGE("Failed, write location_ failed");
317 return false;
318 }
319 return true;
320 }
321
Unmarshalling(Parcel & data)322 SensorDescriptionIPC* SensorDescriptionIPC::Unmarshalling(Parcel &data)
323 {
324 auto sensorDesc = new (std::nothrow) SensorDescriptionIPC();
325 if (sensorDesc == nullptr) {
326 SEN_HILOGE("Read init capacity failed");
327 return nullptr;
328 }
329 if (!(data.ReadInt32(sensorDesc->deviceId))) {
330 SEN_HILOGE("Read deviceId failed");
331 delete sensorDesc;
332 sensorDesc = nullptr;
333 return sensorDesc;
334 }
335 if (!(data.ReadInt32(sensorDesc->sensorType))) {
336 SEN_HILOGE("Read sensorTypeId failed");
337 delete sensorDesc;
338 sensorDesc = nullptr;
339 return sensorDesc;
340 }
341 if (!(data.ReadInt32(sensorDesc->sensorId))) {
342 SEN_HILOGE("Read sensorId failed");
343 delete sensorDesc;
344 sensorDesc = nullptr;
345 return sensorDesc;
346 }
347 if (!(data.ReadInt32(sensorDesc->location))) {
348 SEN_HILOGE("Read location_ failed");
349 delete sensorDesc;
350 sensorDesc = nullptr;
351 return sensorDesc;
352 }
353 return sensorDesc;
354 }
355 } // namespace Sensors
356 } // namespace OHOS
357