1/*
2 * Copyright (c) 2022 Chipsea Technologies (Shenzhen) Corp., 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 "medical_sensor.h"
17
18#include "medical_errors.h"
19#include "medical_log_domain.h"
20namespace OHOS {
21namespace Sensors {
22using namespace OHOS::HiviewDFX;
23
24namespace {
25constexpr HiLogLabel LABEL = { LOG_CORE, MedicalSensorLogDomain::MEDICAL_SENSOR_UTILS, "MedicalSensor" };
26}
27
28MedicalSensor::MedicalSensor()
29    : sensorId_(0),
30      version_(0),
31      maxRange_(0.0),
32      resolution_(0.0),
33      flags_(0),
34      fifoMaxEventCount_(0),
35      minSamplePeriodNs_(0),
36      maxSamplePeriodNs_(0)
37{}
38
39uint32_t MedicalSensor::GetSensorId() const
40{
41    return sensorId_;
42}
43
44void MedicalSensor::SetSensorId(uint32_t sensorId)
45{
46    sensorId_ = sensorId;
47}
48
49std::string MedicalSensor::GetName() const
50{
51    return name_;
52}
53
54void MedicalSensor::SetName(const std::string &name)
55{
56    name_ = name;
57}
58
59std::string MedicalSensor::GetVendor() const
60{
61    return vendor_;
62}
63
64void MedicalSensor::SetVendor(const std::string &vendor)
65{
66    vendor_ = vendor;
67}
68
69uint32_t MedicalSensor::GetVersion() const
70{
71    return version_;
72}
73
74void MedicalSensor::SetVersion(uint32_t version)
75{
76    version_ = version;
77}
78
79float MedicalSensor::GetMaxRange() const
80{
81    return maxRange_;
82}
83
84void MedicalSensor::SetMaxRange(float maxRange)
85{
86    maxRange_ = maxRange;
87}
88
89float MedicalSensor::GetResolution() const
90{
91    return resolution_;
92}
93
94void MedicalSensor::SetResolution(float resolution)
95{
96    resolution_ = resolution;
97}
98
99uint32_t MedicalSensor::GetFlags() const
100{
101    return flags_;
102}
103
104void MedicalSensor::SetFlags(uint32_t flags)
105{
106    flags_ = flags;
107}
108
109int32_t MedicalSensor::GetFifoMaxEventCount() const
110{
111    return fifoMaxEventCount_;
112}
113
114void MedicalSensor::SetFifoMaxEventCount(int32_t fifoMaxEventCount)
115{
116    fifoMaxEventCount_ = fifoMaxEventCount;
117}
118
119int64_t MedicalSensor::GetMinSamplePeriodNs() const
120{
121    return minSamplePeriodNs_;
122}
123
124void MedicalSensor::SetMinSamplePeriodNs(int64_t minSamplePeriodNs)
125{
126    minSamplePeriodNs_ = minSamplePeriodNs;
127}
128
129int64_t MedicalSensor::GetMaxSamplePeriodNs() const
130{
131    return maxSamplePeriodNs_;
132}
133
134void MedicalSensor::SetMaxSamplePeriodNs(int64_t maxSamplePeriodNs)
135{
136    maxSamplePeriodNs_ = maxSamplePeriodNs;
137}
138
139std::vector<uint32_t> MedicalSensor::GetReserved() const
140{
141    return reserved_;
142}
143
144void MedicalSensor::SetReserved(const std::vector<uint32_t> &reserved)
145{
146    auto reservedCount = reserved.size();
147    for (size_t i = 0; i < reservedCount; i++) {
148        reserved_[i] = reserved[i];
149    }
150}
151
152bool MedicalSensor::Marshalling(Parcel &parcel) const
153{
154    if (!parcel.WriteUint32(sensorId_)) {
155        HiLog::Error(LABEL, "%{public}s failed, write sensorId failed", __func__);
156        return false;
157    }
158    if (!parcel.WriteString(name_)) {
159        HiLog::Error(LABEL, "%{public}s failed, write name_ failed", __func__);
160        return false;
161    }
162    if (!parcel.WriteString(vendor_)) {
163        HiLog::Error(LABEL, "%{public}s failed, write vendor_ failed", __func__);
164        return false;
165    }
166    if (!parcel.WriteUint32(version_)) {
167        HiLog::Error(LABEL, "%{public}s failed, write version_ failed", __func__);
168        return false;
169    }
170    if (!parcel.WriteFloat(maxRange_)) {
171        HiLog::Error(LABEL, "%{public}s failed, write maxRange_ failed", __func__);
172        return false;
173    }
174    if (!parcel.WriteFloat(resolution_)) {
175        HiLog::Error(LABEL, "%{public}s failed, write resolution_ failed", __func__);
176        return false;
177    }
178    if (!parcel.WriteUint32(flags_)) {
179        HiLog::Error(LABEL, "%{public}s failed, write flags_ failed", __func__);
180        return false;
181    }
182    if (!parcel.WriteInt32(fifoMaxEventCount_)) {
183        HiLog::Error(LABEL, "%{public}s failed, write fifoMaxEventCount_ failed", __func__);
184        return false;
185    }
186    if (!parcel.WriteInt64(minSamplePeriodNs_)) {
187        HiLog::Error(LABEL, "%{public}s failed, write minSamplePeriodNs_ failed", __func__);
188        return false;
189    }
190    if (!parcel.WriteInt64(maxSamplePeriodNs_)) {
191        HiLog::Error(LABEL, "%{public}s failed, write maxSamplePeriodNs_ failed", __func__);
192        return false;
193    }
194    if (!parcel.WriteUInt32Vector(reserved_)) {
195        HiLog::Error(LABEL, "%{public}s failed, write reserved_ failed", __func__);
196        return false;
197    }
198
199    return true;
200}
201
202std::unique_ptr<MedicalSensor> MedicalSensor::Unmarshalling(Parcel &parcel)
203{
204    auto sensor = std::make_unique<MedicalSensor>();
205    if (sensor == nullptr) {
206        HiLog::Error(LABEL, "%{public}s sensor cannot be null", __func__);
207        return nullptr;
208    }
209
210    if (!sensor->ReadFromParcel(parcel)) {
211        HiLog::Error(LABEL, "%{public}s ReadFromParcel failed", __func__);
212        return nullptr;
213    }
214    return sensor;
215}
216
217bool MedicalSensor::ReadFromParcel(Parcel &parcel)
218{
219    sensorId_ = parcel.ReadUint32();
220    name_ = parcel.ReadString();
221    vendor_ = parcel.ReadString();
222    version_ = parcel.ReadUint32();
223    maxRange_ = parcel.ReadFloat();
224    resolution_ = parcel.ReadFloat();
225    flags_ = parcel.ReadUint32();
226    fifoMaxEventCount_ = parcel.ReadInt32();
227    minSamplePeriodNs_ = parcel.ReadInt64();
228    maxSamplePeriodNs_ = parcel.ReadInt64();
229    return parcel.ReadUInt32Vector(&reserved_);
230}
231}  // namespace Sensors
232}  // namespace OHOS
233