1bae4d13cSopenharmony_ci/* 2bae4d13cSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 3bae4d13cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4bae4d13cSopenharmony_ci * you may not use this file except in compliance with the License. 5bae4d13cSopenharmony_ci * You may obtain a copy of the License at 6bae4d13cSopenharmony_ci * 7bae4d13cSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8bae4d13cSopenharmony_ci * 9bae4d13cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10bae4d13cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11bae4d13cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12bae4d13cSopenharmony_ci * See the License for the specific language governing permissions and 13bae4d13cSopenharmony_ci * limitations under the License. 14bae4d13cSopenharmony_ci */ 15bae4d13cSopenharmony_ci 16bae4d13cSopenharmony_ci#include "active_info.h" 17bae4d13cSopenharmony_ci 18bae4d13cSopenharmony_ci#include "sensor_errors.h" 19bae4d13cSopenharmony_ci 20bae4d13cSopenharmony_ci#undef LOG_TAG 21bae4d13cSopenharmony_ci#define LOG_TAG "ActiveInfo" 22bae4d13cSopenharmony_ci 23bae4d13cSopenharmony_cinamespace OHOS { 24bae4d13cSopenharmony_cinamespace Sensors { 25bae4d13cSopenharmony_ciusing namespace OHOS::HiviewDFX; 26bae4d13cSopenharmony_ci 27bae4d13cSopenharmony_ciActiveInfo::ActiveInfo(int32_t pid, int32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs) 28bae4d13cSopenharmony_ci :pid_(pid), sensorId_(sensorId), samplingPeriodNs_(samplingPeriodNs), maxReportDelayNs_(maxReportDelayNs) 29bae4d13cSopenharmony_ci{} 30bae4d13cSopenharmony_ci 31bae4d13cSopenharmony_ciint32_t ActiveInfo::GetPid() const 32bae4d13cSopenharmony_ci{ 33bae4d13cSopenharmony_ci return pid_; 34bae4d13cSopenharmony_ci} 35bae4d13cSopenharmony_ci 36bae4d13cSopenharmony_civoid ActiveInfo::SetPid(int32_t pid) 37bae4d13cSopenharmony_ci{ 38bae4d13cSopenharmony_ci pid_ = pid; 39bae4d13cSopenharmony_ci} 40bae4d13cSopenharmony_ci 41bae4d13cSopenharmony_ciint32_t ActiveInfo::GetSensorId() const 42bae4d13cSopenharmony_ci{ 43bae4d13cSopenharmony_ci return sensorId_; 44bae4d13cSopenharmony_ci} 45bae4d13cSopenharmony_ci 46bae4d13cSopenharmony_civoid ActiveInfo::SetSensorId(int32_t sensorId) 47bae4d13cSopenharmony_ci{ 48bae4d13cSopenharmony_ci sensorId_ = sensorId; 49bae4d13cSopenharmony_ci} 50bae4d13cSopenharmony_ci 51bae4d13cSopenharmony_ciint64_t ActiveInfo::GetSamplingPeriodNs() const 52bae4d13cSopenharmony_ci{ 53bae4d13cSopenharmony_ci return samplingPeriodNs_; 54bae4d13cSopenharmony_ci} 55bae4d13cSopenharmony_ci 56bae4d13cSopenharmony_civoid ActiveInfo::SetSamplingPeriodNs(int64_t samplingPeriodNs) 57bae4d13cSopenharmony_ci{ 58bae4d13cSopenharmony_ci samplingPeriodNs_ = samplingPeriodNs; 59bae4d13cSopenharmony_ci} 60bae4d13cSopenharmony_ci 61bae4d13cSopenharmony_ciint64_t ActiveInfo::GetMaxReportDelayNs() const 62bae4d13cSopenharmony_ci{ 63bae4d13cSopenharmony_ci return maxReportDelayNs_; 64bae4d13cSopenharmony_ci} 65bae4d13cSopenharmony_ci 66bae4d13cSopenharmony_civoid ActiveInfo::SetMaxReportDelayNs(int64_t maxReportDelayNs) 67bae4d13cSopenharmony_ci{ 68bae4d13cSopenharmony_ci maxReportDelayNs_ = maxReportDelayNs; 69bae4d13cSopenharmony_ci} 70bae4d13cSopenharmony_ci 71bae4d13cSopenharmony_cibool ActiveInfo::Marshalling(Parcel &parcel) const 72bae4d13cSopenharmony_ci{ 73bae4d13cSopenharmony_ci if (!parcel.WriteInt32(pid_)) { 74bae4d13cSopenharmony_ci SEN_HILOGE("Write pid failed"); 75bae4d13cSopenharmony_ci return false; 76bae4d13cSopenharmony_ci } 77bae4d13cSopenharmony_ci if (!parcel.WriteInt32(sensorId_)) { 78bae4d13cSopenharmony_ci SEN_HILOGE("Write sensorId failed"); 79bae4d13cSopenharmony_ci return false; 80bae4d13cSopenharmony_ci } 81bae4d13cSopenharmony_ci if (!parcel.WriteInt64(samplingPeriodNs_)) { 82bae4d13cSopenharmony_ci SEN_HILOGE("Write samplingPeriodNs failed"); 83bae4d13cSopenharmony_ci return false; 84bae4d13cSopenharmony_ci } 85bae4d13cSopenharmony_ci if (!parcel.WriteInt64(maxReportDelayNs_)) { 86bae4d13cSopenharmony_ci SEN_HILOGE("Write maxReportDelayNs failed"); 87bae4d13cSopenharmony_ci return false; 88bae4d13cSopenharmony_ci } 89bae4d13cSopenharmony_ci return true; 90bae4d13cSopenharmony_ci} 91bae4d13cSopenharmony_ci 92bae4d13cSopenharmony_cistd::unique_ptr<ActiveInfo> ActiveInfo::Unmarshalling(Parcel &parcel) 93bae4d13cSopenharmony_ci{ 94bae4d13cSopenharmony_ci int32_t pid = -1; 95bae4d13cSopenharmony_ci int32_t sensorId = -1; 96bae4d13cSopenharmony_ci int64_t samplingPeriodNs = -1; 97bae4d13cSopenharmony_ci int64_t maxReportDelayNs = -1; 98bae4d13cSopenharmony_ci if (!(parcel.ReadInt32(pid) && parcel.ReadInt32(sensorId) && 99bae4d13cSopenharmony_ci parcel.ReadInt64(samplingPeriodNs) && parcel.ReadInt64(maxReportDelayNs))) { 100bae4d13cSopenharmony_ci SEN_HILOGE("Read from parcel is failed"); 101bae4d13cSopenharmony_ci return nullptr; 102bae4d13cSopenharmony_ci } 103bae4d13cSopenharmony_ci auto activeInfo = std::make_unique<ActiveInfo>(); 104bae4d13cSopenharmony_ci activeInfo->SetPid(pid); 105bae4d13cSopenharmony_ci activeInfo->SetSensorId(sensorId); 106bae4d13cSopenharmony_ci activeInfo->SetSamplingPeriodNs(samplingPeriodNs); 107bae4d13cSopenharmony_ci activeInfo->SetMaxReportDelayNs(maxReportDelayNs); 108bae4d13cSopenharmony_ci return activeInfo; 109bae4d13cSopenharmony_ci} 110bae4d13cSopenharmony_ci} // namespace Sensors 111bae4d13cSopenharmony_ci} // namespace OHOS