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 "sensors_errors.h"
19 #include "sensors_log_domain.h"
20 namespace OHOS {
21 namespace Sensors {
22 using namespace OHOS::HiviewDFX;
23
24 namespace {
25 constexpr HiLogLabel LABEL = { LOG_CORE, SensorsLogDomain::SENSOR_UTILS, "Sensor" };
26 }
27
Sensor()28 Sensor::Sensor()
29 : sensorId_(0),
30 sensorTypeId_(0),
31 sensorName_(""),
32 vendorName_(""),
33 firmwareVersion_(""),
34 hardwareVersion_(""),
35 maxRange_(0.0),
36 resolution_(0.0),
37 power_(0.0),
38 flags_(0),
39 fifoMaxEventCount_(0),
40 minSamplePeriodNs_(0),
41 maxSamplePeriodNs_(0)
42 {}
43
GetSensorId() const44 uint32_t Sensor::GetSensorId() const
45 {
46 return sensorId_;
47 }
48
SetSensorId(uint32_t sensorId)49 void Sensor::SetSensorId(uint32_t sensorId)
50 {
51 sensorId_ = sensorId;
52 }
53
GetSensorTypeId() const54 uint32_t Sensor::GetSensorTypeId() const
55 {
56 return sensorTypeId_;
57 }
58
SetSensorTypeId(uint32_t sensorTypeId)59 void Sensor::SetSensorTypeId(uint32_t sensorTypeId)
60 {
61 sensorTypeId_ = sensorTypeId;
62 }
63
GetSensorName() const64 std::string Sensor::GetSensorName() const
65 {
66 return sensorName_;
67 }
68
SetSensorName(const std::string & sensorName)69 void Sensor::SetSensorName(const std::string &sensorName)
70 {
71 sensorName_ = sensorName;
72 }
73
GetVendorName() const74 std::string Sensor::GetVendorName() const
75 {
76 return vendorName_;
77 }
78
SetVendorName(const std::string & vendorName)79 void Sensor::SetVendorName(const std::string &vendorName)
80 {
81 vendorName_ = vendorName;
82 }
83
GetHardwareVersion() const84 std::string Sensor::GetHardwareVersion() const
85 {
86 return hardwareVersion_;
87 }
88
SetHardwareVersion(const std::string & hardwareVersion)89 void Sensor::SetHardwareVersion(const std::string &hardwareVersion)
90 {
91 hardwareVersion_ = hardwareVersion;
92 }
93
GetFirmwareVersion() const94 std::string Sensor::GetFirmwareVersion() const
95 {
96 return firmwareVersion_;
97 }
98
SetFirmwareVersion(const std::string & firmwareVersion)99 void Sensor::SetFirmwareVersion(const std::string &firmwareVersion)
100 {
101 firmwareVersion_ = firmwareVersion;
102 }
103
GetMaxRange() const104 float Sensor::GetMaxRange() const
105 {
106 return maxRange_;
107 }
108
SetMaxRange(float maxRange)109 void Sensor::SetMaxRange(float maxRange)
110 {
111 maxRange_ = maxRange;
112 }
113
GetResolution() const114 float Sensor::GetResolution() const
115 {
116 return resolution_;
117 }
118
SetResolution(float resolution)119 void Sensor::SetResolution(float resolution)
120 {
121 resolution_ = resolution;
122 }
123
GetPower() const124 float Sensor::GetPower() const
125 {
126 return power_;
127 }
128
SetPower(float power)129 void Sensor::SetPower(float power)
130 {
131 power_ = power;
132 }
133
GetFlags() const134 uint32_t Sensor::Sensor::GetFlags() const
135 {
136 return flags_;
137 }
138
SetFlags(uint32_t flags)139 void Sensor::SetFlags(uint32_t flags)
140 {
141 flags_ = flags;
142 }
143
GetFifoMaxEventCount() const144 int32_t Sensor::GetFifoMaxEventCount() const
145 {
146 return fifoMaxEventCount_;
147 }
148
SetFifoMaxEventCount(int32_t fifoMaxEventCount)149 void Sensor::SetFifoMaxEventCount(int32_t fifoMaxEventCount)
150 {
151 fifoMaxEventCount_ = fifoMaxEventCount;
152 }
153
GetMinSamplePeriodNs() const154 int64_t Sensor::GetMinSamplePeriodNs() const
155 {
156 return minSamplePeriodNs_;
157 }
158
SetMinSamplePeriodNs(int64_t minSamplePeriodNs)159 void Sensor::SetMinSamplePeriodNs(int64_t minSamplePeriodNs)
160 {
161 minSamplePeriodNs_ = minSamplePeriodNs;
162 }
163
GetMaxSamplePeriodNs() const164 int64_t Sensor::GetMaxSamplePeriodNs() const
165 {
166 return maxSamplePeriodNs_;
167 }
168
SetMaxSamplePeriodNs(int64_t maxSamplePeriodNs)169 void Sensor::SetMaxSamplePeriodNs(int64_t maxSamplePeriodNs)
170 {
171 maxSamplePeriodNs_ = maxSamplePeriodNs;
172 }
173
Marshalling(Parcel & parcel) const174 bool Sensor::Marshalling(Parcel &parcel) const
175 {
176 if (!parcel.WriteUint32(sensorId_)) {
177 HiLog::Error(LABEL, "%{public}s failed, write sensorId failed", __func__);
178 return false;
179 }
180 if (!parcel.WriteUint32(sensorTypeId_)) {
181 HiLog::Error(LABEL, "%{public}s failed, write sensorTypeId failed", __func__);
182 return false;
183 }
184 if (!parcel.WriteString(sensorName_)) {
185 HiLog::Error(LABEL, "%{public}s failed, write sensorName failed", __func__);
186 return false;
187 }
188 if (!parcel.WriteString(vendorName_)) {
189 HiLog::Error(LABEL, "%{public}s failed, write vendorName failed", __func__);
190 return false;
191 }
192 if (!parcel.WriteString(firmwareVersion_)) {
193 HiLog::Error(LABEL, "%{public}s failed, write firmwareVersion failed", __func__);
194 return false;
195 }
196 if (!parcel.WriteString(hardwareVersion_)) {
197 HiLog::Error(LABEL, "%{public}s failed, write hardwareVersion failed", __func__);
198 return false;
199 }
200 if (!parcel.WriteFloat(maxRange_)) {
201 HiLog::Error(LABEL, "%{public}s failed, write maxRange failed", __func__);
202 return false;
203 }
204 if (!parcel.WriteFloat(resolution_)) {
205 HiLog::Error(LABEL, "%{public}s failed, write resolution failed", __func__);
206 return false;
207 }
208 if (!parcel.WriteFloat(power_)) {
209 HiLog::Error(LABEL, "%{public}s failed, write power failed", __func__);
210 return false;
211 }
212 if (!parcel.WriteUint32(flags_)) {
213 HiLog::Error(LABEL, "%{public}s failed, write flags failed", __func__);
214 return false;
215 }
216 if (!parcel.WriteInt32(fifoMaxEventCount_)) {
217 HiLog::Error(LABEL, "%{public}s failed, write fifoMaxEventCount failed", __func__);
218 return false;
219 }
220 if (!parcel.WriteInt64(minSamplePeriodNs_)) {
221 HiLog::Error(LABEL, "%{public}s failed, write minSamplePeriodNs failed", __func__);
222 return false;
223 }
224 if (!parcel.WriteInt64(maxSamplePeriodNs_)) {
225 HiLog::Error(LABEL, "%{public}s failed, write maxSamplePeriodNs failed", __func__);
226 return false;
227 }
228 return true;
229 }
230
Unmarshalling(Parcel & parcel)231 std::unique_ptr<Sensor> Sensor::Unmarshalling(Parcel &parcel)
232 {
233 auto sensor = std::make_unique<Sensor>();
234 if (sensor == nullptr) {
235 HiLog::Error(LABEL, "%{public}s sensor cannot be null", __func__);
236 return nullptr;
237 }
238
239 if (!sensor->ReadFromParcel(parcel)) {
240 HiLog::Error(LABEL, "%{public}s ReadFromParcel failed", __func__);
241 return nullptr;
242 }
243 return sensor;
244 }
245
ReadFromParcel(Parcel & parcel)246 bool Sensor::ReadFromParcel(Parcel &parcel)
247 {
248 sensorId_ = parcel.ReadUint32();
249 sensorTypeId_ = parcel.ReadUint32();
250 sensorName_ = parcel.ReadString();
251 vendorName_ = parcel.ReadString();
252 firmwareVersion_ = parcel.ReadString();
253 hardwareVersion_ = parcel.ReadString();
254 power_ = parcel.ReadFloat();
255 maxRange_ = parcel.ReadFloat();
256 resolution_ = parcel.ReadFloat();
257 flags_ = parcel.ReadUint32();
258 fifoMaxEventCount_ = parcel.ReadInt32();
259 minSamplePeriodNs_ = parcel.ReadInt64();
260 maxSamplePeriodNs_ = parcel.ReadInt64();
261 return true;
262 }
263 } // namespace Sensors
264 } // namespace OHOS
265