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 namespace OHOS {
20 namespace Sensors {
21 using namespace OHOS::HiviewDFX;
22
23 namespace {
24 constexpr HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "Sensor" };
25 }
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 {}
42
GetSensorId() const43 int32_t Sensor::GetSensorId() const
44 {
45 return sensorId_;
46 }
47
SetSensorId(int32_t sensorId)48 void Sensor::SetSensorId(int32_t sensorId)
49 {
50 sensorId_ = sensorId;
51 }
52
GetSensorTypeId() const53 int32_t Sensor::GetSensorTypeId() const
54 {
55 return sensorTypeId_;
56 }
57
SetSensorTypeId(int32_t sensorTypeId)58 void Sensor::SetSensorTypeId(int32_t sensorTypeId)
59 {
60 sensorTypeId_ = sensorTypeId;
61 }
62
GetSensorName() const63 std::string Sensor::GetSensorName() const
64 {
65 return sensorName_;
66 }
67
SetSensorName(const std::string & sensorName)68 void Sensor::SetSensorName(const std::string &sensorName)
69 {
70 sensorName_ = sensorName;
71 }
72
GetVendorName() const73 std::string Sensor::GetVendorName() const
74 {
75 return vendorName_;
76 }
77
SetVendorName(const std::string & vendorName)78 void Sensor::SetVendorName(const std::string &vendorName)
79 {
80 vendorName_ = vendorName;
81 }
82
GetHardwareVersion() const83 std::string Sensor::GetHardwareVersion() const
84 {
85 return hardwareVersion_;
86 }
87
SetHardwareVersion(const std::string & hardwareVersion)88 void Sensor::SetHardwareVersion(const std::string &hardwareVersion)
89 {
90 hardwareVersion_ = hardwareVersion;
91 }
92
GetFirmwareVersion() const93 std::string Sensor::GetFirmwareVersion() const
94 {
95 return firmwareVersion_;
96 }
97
SetFirmwareVersion(const std::string & firmwareVersion)98 void Sensor::SetFirmwareVersion(const std::string &firmwareVersion)
99 {
100 firmwareVersion_ = firmwareVersion;
101 }
102
GetMaxRange() const103 float Sensor::GetMaxRange() const
104 {
105 return maxRange_;
106 }
107
SetMaxRange(float maxRange)108 void Sensor::SetMaxRange(float maxRange)
109 {
110 maxRange_ = maxRange;
111 }
112
GetResolution() const113 float Sensor::GetResolution() const
114 {
115 return resolution_;
116 }
117
SetResolution(float resolution)118 void Sensor::SetResolution(float resolution)
119 {
120 resolution_ = resolution;
121 }
122
GetPower() const123 float Sensor::GetPower() const
124 {
125 return power_;
126 }
127
SetPower(float power)128 void Sensor::SetPower(float power)
129 {
130 power_ = power;
131 }
132
GetFlags() const133 uint32_t Sensor::Sensor::GetFlags() const
134 {
135 return flags_;
136 }
137
SetFlags(uint32_t flags)138 void Sensor::SetFlags(uint32_t flags)
139 {
140 flags_ = flags;
141 }
142
GetFifoMaxEventCount() const143 int32_t Sensor::GetFifoMaxEventCount() const
144 {
145 return fifoMaxEventCount_;
146 }
147
SetFifoMaxEventCount(int32_t fifoMaxEventCount)148 void Sensor::SetFifoMaxEventCount(int32_t fifoMaxEventCount)
149 {
150 fifoMaxEventCount_ = fifoMaxEventCount;
151 }
152
GetMinSamplePeriodNs() const153 int64_t Sensor::GetMinSamplePeriodNs() const
154 {
155 return minSamplePeriodNs_;
156 }
157
SetMinSamplePeriodNs(int64_t minSamplePeriodNs)158 void Sensor::SetMinSamplePeriodNs(int64_t minSamplePeriodNs)
159 {
160 minSamplePeriodNs_ = minSamplePeriodNs;
161 }
162
GetMaxSamplePeriodNs() const163 int64_t Sensor::GetMaxSamplePeriodNs() const
164 {
165 return maxSamplePeriodNs_;
166 }
167
SetMaxSamplePeriodNs(int64_t maxSamplePeriodNs)168 void Sensor::SetMaxSamplePeriodNs(int64_t maxSamplePeriodNs)
169 {
170 maxSamplePeriodNs_ = maxSamplePeriodNs;
171 }
172
Marshalling(Parcel & parcel) const173 bool Sensor::Marshalling(Parcel &parcel) const
174 {
175 if (!parcel.WriteInt32(sensorId_)) {
176 SEN_HILOGE("Failed, write sensorId failed");
177 return false;
178 }
179 if (!parcel.WriteInt32(sensorTypeId_)) {
180 SEN_HILOGE("Failed, write sensorTypeId failed");
181 return false;
182 }
183 if (!parcel.WriteString(sensorName_)) {
184 SEN_HILOGE("Failed, write sensorName failed");
185 return false;
186 }
187 if (!parcel.WriteString(vendorName_)) {
188 SEN_HILOGE("Failed, write vendorName failed");
189 return false;
190 }
191 if (!parcel.WriteString(firmwareVersion_)) {
192 SEN_HILOGE("Failed, write firmwareVersion failed");
193 return false;
194 }
195 if (!parcel.WriteString(hardwareVersion_)) {
196 SEN_HILOGE("Failed, write hardwareVersion failed");
197 return false;
198 }
199 if (!parcel.WriteFloat(maxRange_)) {
200 SEN_HILOGE("Failed, write maxRange failed");
201 return false;
202 }
203 if (!parcel.WriteFloat(resolution_)) {
204 SEN_HILOGE("Failed, write resolution failed");
205 return false;
206 }
207 if (!parcel.WriteFloat(power_)) {
208 SEN_HILOGE("Failed, write power failed");
209 return false;
210 }
211 if (!parcel.WriteUint32(flags_)) {
212 SEN_HILOGE("Failed, write flags failed");
213 return false;
214 }
215 if (!parcel.WriteInt32(fifoMaxEventCount_)) {
216 SEN_HILOGE("Failed, write fifoMaxEventCount failed");
217 return false;
218 }
219 if (!parcel.WriteInt64(minSamplePeriodNs_)) {
220 SEN_HILOGE("Failed, write minSamplePeriodNs failed");
221 return false;
222 }
223 if (!parcel.WriteInt64(maxSamplePeriodNs_)) {
224 SEN_HILOGE("Failed, write maxSamplePeriodNs failed");
225 return false;
226 }
227 return true;
228 }
229
Unmarshalling(Parcel & parcel)230 std::unique_ptr<Sensor> Sensor::Unmarshalling(Parcel &parcel)
231 {
232 auto sensor = std::make_unique<Sensor>();
233 if (!sensor->ReadFromParcel(parcel)) {
234 SEN_HILOGE("ReadFromParcel is failed");
235 return nullptr;
236 }
237 return sensor;
238 }
239
ReadFromParcel(Parcel & parcel)240 bool Sensor::ReadFromParcel(Parcel &parcel)
241 {
242 if ((!parcel.ReadInt32(sensorId_)) ||
243 (!parcel.ReadInt32(sensorTypeId_)) ||
244 (!parcel.ReadString(sensorName_)) ||
245 (!parcel.ReadString(vendorName_)) ||
246 (!parcel.ReadString(firmwareVersion_)) ||
247 (!parcel.ReadString(hardwareVersion_)) ||
248 (!parcel.ReadFloat(power_)) ||
249 (!parcel.ReadFloat(maxRange_)) ||
250 (!parcel.ReadFloat(resolution_)) ||
251 (!parcel.ReadUint32(flags_)) ||
252 (!parcel.ReadInt32(fifoMaxEventCount_)) ||
253 (!parcel.ReadInt64(minSamplePeriodNs_)) ||
254 (!parcel.ReadInt64(maxSamplePeriodNs_))) {
255 return false;
256 }
257 return true;
258 }
259 } // namespace Sensors
260 } // namespace OHOS
261