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 23namespace OHOS { 24namespace Sensors { 25using namespace OHOS::HiviewDFX; 26 27Sensor::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 43int32_t Sensor::GetSensorId() const 44{ 45 return sensorId_; 46} 47 48void Sensor::SetSensorId(int32_t sensorId) 49{ 50 sensorId_ = sensorId; 51} 52 53int32_t Sensor::GetSensorTypeId() const 54{ 55 return sensorTypeId_; 56} 57 58void Sensor::SetSensorTypeId(int32_t sensorTypeId) 59{ 60 sensorTypeId_ = sensorTypeId; 61} 62 63std::string Sensor::GetSensorName() const 64{ 65 return sensorName_; 66} 67 68void Sensor::SetSensorName(const std::string &sensorName) 69{ 70 sensorName_ = sensorName; 71} 72 73std::string Sensor::GetVendorName() const 74{ 75 return vendorName_; 76} 77 78void Sensor::SetVendorName(const std::string &vendorName) 79{ 80 vendorName_ = vendorName; 81} 82 83std::string Sensor::GetHardwareVersion() const 84{ 85 return hardwareVersion_; 86} 87 88void Sensor::SetHardwareVersion(const std::string &hardwareVersion) 89{ 90 hardwareVersion_ = hardwareVersion; 91} 92 93std::string Sensor::GetFirmwareVersion() const 94{ 95 return firmwareVersion_; 96} 97 98void Sensor::SetFirmwareVersion(const std::string &firmwareVersion) 99{ 100 firmwareVersion_ = firmwareVersion; 101} 102 103float Sensor::GetMaxRange() const 104{ 105 return maxRange_; 106} 107 108void Sensor::SetMaxRange(float maxRange) 109{ 110 maxRange_ = maxRange; 111} 112 113float Sensor::GetResolution() const 114{ 115 return resolution_; 116} 117 118void Sensor::SetResolution(float resolution) 119{ 120 resolution_ = resolution; 121} 122 123float Sensor::GetPower() const 124{ 125 return power_; 126} 127 128void Sensor::SetPower(float power) 129{ 130 power_ = power; 131} 132 133uint32_t Sensor::Sensor::GetFlags() const 134{ 135 return flags_; 136} 137 138void Sensor::SetFlags(uint32_t flags) 139{ 140 flags_ = flags; 141} 142 143int32_t Sensor::GetFifoMaxEventCount() const 144{ 145 return fifoMaxEventCount_; 146} 147 148void Sensor::SetFifoMaxEventCount(int32_t fifoMaxEventCount) 149{ 150 fifoMaxEventCount_ = fifoMaxEventCount; 151} 152 153int64_t Sensor::GetMinSamplePeriodNs() const 154{ 155 return minSamplePeriodNs_; 156} 157 158void Sensor::SetMinSamplePeriodNs(int64_t minSamplePeriodNs) 159{ 160 minSamplePeriodNs_ = minSamplePeriodNs; 161} 162 163int64_t Sensor::GetMaxSamplePeriodNs() const 164{ 165 return maxSamplePeriodNs_; 166} 167 168void Sensor::SetMaxSamplePeriodNs(int64_t maxSamplePeriodNs) 169{ 170 maxSamplePeriodNs_ = maxSamplePeriodNs; 171} 172 173bool 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 230std::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 240bool 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