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 deviceId,int32_t sensorTypeId,int32_t sensorId,int64_t samplingPeriodNs,int64_t maxReportDelayNs)27 ActiveInfo::ActiveInfo(int32_t pid, int32_t deviceId, int32_t sensorTypeId, int32_t sensorId, int64_t samplingPeriodNs,
28 int64_t maxReportDelayNs)
29 :pid_(pid), deviceId_(deviceId), sensorTypeId_(sensorTypeId), sensorId_(sensorId),
30 samplingPeriodNs_(samplingPeriodNs), maxReportDelayNs_(maxReportDelayNs)
31 {}
32
GetPid() const33 int32_t ActiveInfo::GetPid() const
34 {
35 return pid_;
36 }
37
SetPid(int32_t pid)38 void ActiveInfo::SetPid(int32_t pid)
39 {
40 pid_ = pid;
41 }
42
GetDeviceId() const43 int32_t ActiveInfo::GetDeviceId() const
44 {
45 return deviceId_;
46 }
47
SetDeviceId(int32_t deviceId)48 void ActiveInfo::SetDeviceId(int32_t deviceId)
49 {
50 deviceId_ = deviceId;
51 }
52
GetSensorTypeId() const53 int32_t ActiveInfo::GetSensorTypeId() const
54 {
55 return sensorTypeId_;
56 }
57
SetSensorTypeId(int32_t sensorTypeId)58 void ActiveInfo::SetSensorTypeId(int32_t sensorTypeId)
59 {
60 sensorTypeId_ = sensorTypeId;
61 }
62
GetSensorId() const63 int32_t ActiveInfo::GetSensorId() const
64 {
65 return sensorId_;
66 }
67
SetSensorId(int32_t sensorId)68 void ActiveInfo::SetSensorId(int32_t sensorId)
69 {
70 sensorId_ = sensorId;
71 }
72
GetSamplingPeriodNs() const73 int64_t ActiveInfo::GetSamplingPeriodNs() const
74 {
75 return samplingPeriodNs_;
76 }
77
SetSamplingPeriodNs(int64_t samplingPeriodNs)78 void ActiveInfo::SetSamplingPeriodNs(int64_t samplingPeriodNs)
79 {
80 samplingPeriodNs_ = samplingPeriodNs;
81 }
82
GetMaxReportDelayNs() const83 int64_t ActiveInfo::GetMaxReportDelayNs() const
84 {
85 return maxReportDelayNs_;
86 }
87
SetMaxReportDelayNs(int64_t maxReportDelayNs)88 void ActiveInfo::SetMaxReportDelayNs(int64_t maxReportDelayNs)
89 {
90 maxReportDelayNs_ = maxReportDelayNs;
91 }
92
Marshalling(Parcel & parcel) const93 bool ActiveInfo::Marshalling(Parcel &parcel) const
94 {
95 if (!parcel.WriteInt32(pid_)) {
96 SEN_HILOGE("Write pid failed");
97 return false;
98 }
99 if (!parcel.WriteInt32(deviceId_)) {
100 SEN_HILOGE("Write deviceId failed");
101 return false;
102 }
103 if (!parcel.WriteInt32(sensorTypeId_)) {
104 SEN_HILOGE("Write sensorTypeId failed");
105 return false;
106 }
107 if (!parcel.WriteInt32(sensorId_)) {
108 SEN_HILOGE("Write sensorId failed");
109 return false;
110 }
111 if (!parcel.WriteInt64(samplingPeriodNs_)) {
112 SEN_HILOGE("Write samplingPeriodNs failed");
113 return false;
114 }
115 if (!parcel.WriteInt64(maxReportDelayNs_)) {
116 SEN_HILOGE("Write maxReportDelayNs failed");
117 return false;
118 }
119 return true;
120 }
121
Unmarshalling(Parcel & parcel)122 ActiveInfo* ActiveInfo::Unmarshalling(Parcel &parcel)
123 {
124 int32_t pid = -1;
125 int32_t sensorId = -1;
126 int64_t samplingPeriodNs = -1;
127 int64_t maxReportDelayNs = -1;
128 int32_t deviceId = -1;
129 int32_t sensorTypeId = -1;
130 auto activeInfo = new (std::nothrow) ActiveInfo();
131 if (activeInfo == nullptr || !(parcel.ReadInt32(pid) && parcel.ReadInt32(deviceId) &&
132 parcel.ReadInt32(sensorTypeId) && parcel.ReadInt32(sensorId) &&
133 parcel.ReadInt64(samplingPeriodNs) && parcel.ReadInt64(maxReportDelayNs))) {
134 SEN_HILOGE("Read from parcel is failed");
135 if (activeInfo != nullptr) {
136 delete activeInfo;
137 activeInfo = nullptr;
138 }
139 return activeInfo;
140 }
141 activeInfo->SetPid(pid);
142 activeInfo->SetDeviceId(deviceId);
143 activeInfo->SetSensorTypeId(sensorTypeId);
144 activeInfo->SetSensorId(sensorId);
145 activeInfo->SetSamplingPeriodNs(samplingPeriodNs);
146 activeInfo->SetMaxReportDelayNs(maxReportDelayNs);
147 return activeInfo;
148 }
149 } // namespace Sensors
150 } // namespace OHOS