1 /*
2 * Copyright (c) 2023 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 "active_info.h"
17
18 #include "sensor_errors.h"
19
20 #undef LOG_TAG
21 #define LOG_TAG "ActiveInfo"
22
23 namespace OHOS {
24 namespace Sensors {
25 using namespace OHOS::HiviewDFX;
26
ActiveInfo(int32_t pid,int32_t sensorId,int64_t samplingPeriodNs,int64_t maxReportDelayNs)27 ActiveInfo::ActiveInfo(int32_t pid, int32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs)
28 :pid_(pid), sensorId_(sensorId), samplingPeriodNs_(samplingPeriodNs), maxReportDelayNs_(maxReportDelayNs)
29 {}
30
GetPid() const31 int32_t ActiveInfo::GetPid() const
32 {
33 return pid_;
34 }
35
SetPid(int32_t pid)36 void ActiveInfo::SetPid(int32_t pid)
37 {
38 pid_ = pid;
39 }
40
GetSensorId() const41 int32_t ActiveInfo::GetSensorId() const
42 {
43 return sensorId_;
44 }
45
SetSensorId(int32_t sensorId)46 void ActiveInfo::SetSensorId(int32_t sensorId)
47 {
48 sensorId_ = sensorId;
49 }
50
GetSamplingPeriodNs() const51 int64_t ActiveInfo::GetSamplingPeriodNs() const
52 {
53 return samplingPeriodNs_;
54 }
55
SetSamplingPeriodNs(int64_t samplingPeriodNs)56 void ActiveInfo::SetSamplingPeriodNs(int64_t samplingPeriodNs)
57 {
58 samplingPeriodNs_ = samplingPeriodNs;
59 }
60
GetMaxReportDelayNs() const61 int64_t ActiveInfo::GetMaxReportDelayNs() const
62 {
63 return maxReportDelayNs_;
64 }
65
SetMaxReportDelayNs(int64_t maxReportDelayNs)66 void ActiveInfo::SetMaxReportDelayNs(int64_t maxReportDelayNs)
67 {
68 maxReportDelayNs_ = maxReportDelayNs;
69 }
70
Marshalling(Parcel & parcel) const71 bool ActiveInfo::Marshalling(Parcel &parcel) const
72 {
73 if (!parcel.WriteInt32(pid_)) {
74 SEN_HILOGE("Write pid failed");
75 return false;
76 }
77 if (!parcel.WriteInt32(sensorId_)) {
78 SEN_HILOGE("Write sensorId failed");
79 return false;
80 }
81 if (!parcel.WriteInt64(samplingPeriodNs_)) {
82 SEN_HILOGE("Write samplingPeriodNs failed");
83 return false;
84 }
85 if (!parcel.WriteInt64(maxReportDelayNs_)) {
86 SEN_HILOGE("Write maxReportDelayNs failed");
87 return false;
88 }
89 return true;
90 }
91
Unmarshalling(Parcel & parcel)92 std::unique_ptr<ActiveInfo> ActiveInfo::Unmarshalling(Parcel &parcel)
93 {
94 int32_t pid = -1;
95 int32_t sensorId = -1;
96 int64_t samplingPeriodNs = -1;
97 int64_t maxReportDelayNs = -1;
98 if (!(parcel.ReadInt32(pid) && parcel.ReadInt32(sensorId) &&
99 parcel.ReadInt64(samplingPeriodNs) && parcel.ReadInt64(maxReportDelayNs))) {
100 SEN_HILOGE("Read from parcel is failed");
101 return nullptr;
102 }
103 auto activeInfo = std::make_unique<ActiveInfo>();
104 activeInfo->SetPid(pid);
105 activeInfo->SetSensorId(sensorId);
106 activeInfo->SetSamplingPeriodNs(samplingPeriodNs);
107 activeInfo->SetMaxReportDelayNs(maxReportDelayNs);
108 return activeInfo;
109 }
110 } // namespace Sensors
111 } // namespace OHOS