From 7435f47232b2b993202439c1211cf28bfcf062ed Mon Sep 17 00:00:00 2001 From: hui1975 Date: Mon, 6 Feb 2023 13:19:55 +0000 Subject: [PATCH] fix review comments Signed-off-by: hui1975 Change-Id: I6d2ec667b1515bfcd91268de9b24984daf40bc23 --- services/sensor/BUILD.gn | 4 +- services/sensor/include/sensor_service.h | 11 ++- services/sensor/include/sensor_service_stub.h | 6 +- .../include/sensor_status_callback_proxy.h | 38 ++++++++ services/sensor/src/sensor_service.cpp | 79 +++++++++++++++-- services/sensor/src/sensor_service_stub.cpp | 86 ++++++++++++++++++- .../src/sensor_status_callback_proxy.cpp | 70 +++++++++++++++ 7 files changed, 282 insertions(+), 12 deletions(-) create mode 100644 services/sensor/include/sensor_status_callback_proxy.h create mode 100644 services/sensor/src/sensor_status_callback_proxy.cpp diff --git a/services/sensor/BUILD.gn b/services/sensor/BUILD.gn index 3f15192b..20a66a76 100644 --- a/services/sensor/BUILD.gn +++ b/services/sensor/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021 Huawei Device Co., Ltd. +# Copyright (c) 2021-2023 Huawei Device Co., Ltd. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at @@ -29,6 +29,8 @@ ohos_shared_library("libsensor_service") { "src/sensor_manager.cpp", "src/sensor_service.cpp", "src/sensor_service_stub.cpp", + "src/sensor_status_callback_proxy.cpp", + "src/sensor_suspend_policy.cpp", ] include_dirs = [ diff --git a/services/sensor/include/sensor_service.h b/services/sensor/include/sensor_service.h index e77d05ed..60750289 100644 --- a/services/sensor/include/sensor_service.h +++ b/services/sensor/include/sensor_service.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -23,12 +23,14 @@ #include "nocopyable.h" #include "system_ability.h" +#include "i_sensor_status_callback.h" #include "client_info.h" #include "death_recipient_template.h" #include "sensor_data_event.h" #include "sensor_hdi_connection.h" #include "sensor_manager.h" #include "sensor_service_stub.h" +#include "sensor_suspend_policy.h" namespace OHOS { namespace Sensors { @@ -54,6 +56,10 @@ public: const sptr &sensorClient) override; ErrCode DestroySensorChannel(sptr sensorClient) override; void ProcessDeathObserver(const wptr &object); + ErrCode SuspendSensors(int32_t pid) override; + ErrCode ResumeSensors(int32_t pid) override; + ErrCode GetAppSensorList(int32_t pid, std::vector &appSensorList) override; + ErrCode RegisterCallback(sptr callback) override; private: DISALLOW_COPY_AND_MOVE(SensorService); @@ -62,7 +68,7 @@ private: bool InitInterface(); bool InitDataCallback(); bool InitSensorList(); - bool InitSensorPolicy(); + bool InitSensorSuspendPolicy(); void ReportOnChangeData(int32_t sensorId); void ReportSensorSysEvent(int32_t sensorId, bool enable, int32_t pid); ErrCode DisableSensor(int32_t sensorId, int32_t pid); @@ -76,6 +82,7 @@ private: ClientInfo &clientInfo_ = ClientInfo::GetInstance(); SensorManager &sensorManager_ = SensorManager::GetInstance(); FlushInfoRecord &flushInfo_ = FlushInfoRecord::GetInstance(); + SensorSuspendPolicy &suspendPolicy_ = SensorSuspendPolicy::GetInstance(); sptr sensorDataProcesser_ = nullptr; sptr reportDataCallback_ = nullptr; std::mutex uidLock_; diff --git a/services/sensor/include/sensor_service_stub.h b/services/sensor/include/sensor_service_stub.h index 9259e793..ed31edb4 100644 --- a/services/sensor/include/sensor_service_stub.h +++ b/services/sensor/include/sensor_service_stub.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -39,6 +39,10 @@ private: ErrCode GetAllSensorsInner(MessageParcel &data, MessageParcel &reply); ErrCode CreateDataChannelInner(MessageParcel &data, MessageParcel &reply); ErrCode DestroyDataChannelInner(MessageParcel &data, MessageParcel &reply); + ErrCode SuspendSensorsInner(MessageParcel &data, MessageParcel &reply); + ErrCode ResumeSensorsInner(MessageParcel &data, MessageParcel &reply); + ErrCode GetAppSensorListInner(MessageParcel &data, MessageParcel &reply); + ErrCode RegisterCallbackInner(MessageParcel &data, MessageParcel &reply); std::unordered_map baseFuncs_; }; } // namespace Sensors diff --git a/services/sensor/include/sensor_status_callback_proxy.h b/services/sensor/include/sensor_status_callback_proxy.h new file mode 100644 index 00000000..4d3a606f --- /dev/null +++ b/services/sensor/include/sensor_status_callback_proxy.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef SENSOR_STATUS_CALLBACK_PROXY_H +#define SENSOR_STATUS_CALLBACK_PROXY_H + +#include "iremote_proxy.h" +#include "nocopyable.h" + +#include "i_sensor_status_callback.h" + +namespace OHOS { +namespace Sensors { +class SensorStatusCallbackProxy : public IRemoteProxy { +public: + explicit SensorStatusCallbackProxy(const sptr &impl) : IRemoteProxy(impl) {} + ~SensorStatusCallbackProxy() = default; + DISALLOW_COPY_AND_MOVE(SensorStatusCallbackProxy); + void OnSensorChanged(const AppSensorInfo &appSensorInfo) override; + +private: + static inline BrokerDelegator delegator_; +}; +} // namespace Sensors +} // namespace OHOS +#endif // SENSOR_STATUS_CALLBACK_PROXY_H diff --git a/services/sensor/src/sensor_service.cpp b/services/sensor/src/sensor_service.cpp index 2c7563b7..27089efb 100644 --- a/services/sensor/src/sensor_service.cpp +++ b/services/sensor/src/sensor_service.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Copyright (c) 2021-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -77,10 +77,10 @@ void SensorService::OnStart() } sensorDataProcesser_ = new (std::nothrow) SensorDataProcesser(sensorMap_); CHKPV(sensorDataProcesser_); - if (!InitSensorPolicy()) { - SEN_HILOGE("Init sensor policy error"); + if (!InitSensorSuspendPolicy()) { + SEN_HILOGE("Init sensor suspend policy error"); + return; } - if (!SystemAbility::Publish(this)) { SEN_HILOGE("publish SensorService error"); return; @@ -131,8 +131,12 @@ bool SensorService::InitSensorList() return true; } -bool SensorService::InitSensorPolicy() +bool SensorService::InitSensorSuspendPolicy() { + if(!suspendPolicy_.InitDeathRecipient()) { + SEN_HILOGE("SuspendPolicy init death recipient failed"); + return false; + } return true; } @@ -233,6 +237,7 @@ ErrCode SensorService::EnableSensor(int32_t sensorId, int64_t samplingPeriodNs, SEN_HILOGE("ret : %{public}d", ret); } ReportOnChangeData(sensorId); + suspendPolicy_.ExecuteCallbackAsync(pid, sensorId, true, samplingPeriodNs, maxReportDelayNs); return ERR_OK; } auto ret = SaveSubscriber(sensorId, samplingPeriodNs, maxReportDelayNs); @@ -248,6 +253,7 @@ ErrCode SensorService::EnableSensor(int32_t sensorId, int64_t samplingPeriodNs, return ENABLE_SENSOR_ERR; } ReportSensorSysEvent(sensorId, true, pid); + suspendPolicy_.ExecuteCallbackAsync(pid, sensorId, true, samplingPeriodNs, maxReportDelayNs); return ret; } @@ -262,10 +268,14 @@ ErrCode SensorService::DisableSensor(int32_t sensorId, int32_t pid) SEN_HILOGE("pid is invalid, pid:%{public}d", pid); return CLIENT_PID_INVALID_ERR; } + SensorBasicInfo sensorInfo = clientInfo_.GetCurPidSensorInfo(sensorId, pid); + int64_t samplingPeriodNs = sensorInfo.GetSamplingPeriodNs(); + int64_t maxReportDelayNs = sensorInfo.GetMaxReportDelayNs(); ReportSensorSysEvent(sensorId, false, pid); std::lock_guard serviceLock(serviceLock_); if (sensorManager_.IsOtherClientUsingSensor(sensorId, pid)) { SEN_HILOGW("other client is using this sensor now, cannot disable"); + suspendPolicy_.ExecuteCallbackAsync(pid, sensorId, false, samplingPeriodNs, maxReportDelayNs); return ERR_OK; } if (sensorHdiConnection_.DisableSensor(sensorId) != ERR_OK) { @@ -275,6 +285,7 @@ ErrCode SensorService::DisableSensor(int32_t sensorId, int32_t pid) int32_t uid = clientInfo_.GetUidByPid(pid); clientInfo_.DestroyCmd(uid); clientInfo_.ClearDataQueue(sensorId); + suspendPolicy_.ExecuteCallbackAsync(pid, sensorId, false, samplingPeriodNs, maxReportDelayNs); return sensorManager_.AfterDisableSensor(sensorId); } @@ -325,7 +336,6 @@ ErrCode SensorService::DestroySensorChannel(sptr sensorClient) const int32_t clientPid = GetCallingPid(); if (clientPid < 0) { SEN_HILOGE("clientPid is invalid, clientPid:%{public}d", clientPid); - return CLIENT_PID_INVALID_ERR; } std::lock_guard serviceLock(serviceLock_); @@ -404,5 +414,62 @@ int32_t SensorService::Dump(int32_t fd, const std::vector &args) sensorDump.ParseCommand(fd, argList, sensors_, clientInfo_); return ERR_OK; } + +ErrCode SensorService::SuspendSensors(int32_t pid) +{ + CALL_LOG_ENTER; + if (pid < 0) { + SEN_HILOGE("Pid is invalid, pid:%{public}d", pid); + return CLIENT_PID_INVALID_ERR; + } + int32_t ret = suspendPolicy_.DoSuspend(pid); + if (ret != ERR_OK) { + SEN_HILOGE("Suspend pid sensors failed, pid:%{public}d", pid); + return ERROR; + } + return ERR_OK; +} + +ErrCode SensorService::ResumeSensors(int32_t pid) +{ + CALL_LOG_ENTER; + if (pid < 0) { + SEN_HILOGE("Pid is invalid, pid:%{public}d", pid); + return CLIENT_PID_INVALID_ERR; + } + int32_t ret = suspendPolicy_.DoResume(pid); + if (ret != ERR_OK) { + SEN_HILOGE("Resume pid sensors failed, pid:%{public}d", pid); + return ERROR; + } + return ERR_OK; +} + +ErrCode SensorService::GetAppSensorList(int32_t pid, std::vector &appSensorList) +{ + CALL_LOG_ENTER; + if (pid < 0) { + SEN_HILOGE("Pid is invalid, pid:%{public}d", pid); + return CLIENT_PID_INVALID_ERR; + } + int32_t ret = suspendPolicy_.GetAppSensorList(pid, appSensorList); + if (ret != ERR_OK) { + SEN_HILOGE("Get pid sensor list failed, pid:%{public}d", pid); + return ERROR; + } + return ERR_OK; +} + +ErrCode SensorService::RegisterCallback(sptr callback) +{ + CALL_LOG_ENTER; + CHKPR(callback, ERROR); + int32_t ret = suspendPolicy_.AddCallback(callback); + if (ret != ERR_OK) { + SEN_HILOGE("Register callback failed"); + return ERROR; + } + return ERR_OK; +} } // namespace Sensors } // namespace OHOS diff --git a/services/sensor/src/sensor_service_stub.cpp b/services/sensor/src/sensor_service_stub.cpp index 94c2a8af..ebb39b45 100755 --- a/services/sensor/src/sensor_service_stub.cpp +++ b/services/sensor/src/sensor_service_stub.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -30,7 +30,6 @@ namespace OHOS { namespace Sensors { using namespace OHOS::HiviewDFX; - namespace { constexpr HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "SensorServiceStub" }; } // namespace @@ -43,6 +42,10 @@ SensorServiceStub::SensorServiceStub() baseFuncs_[GET_SENSOR_LIST] = &SensorServiceStub::GetAllSensorsInner; baseFuncs_[TRANSFER_DATA_CHANNEL] = &SensorServiceStub::CreateDataChannelInner; baseFuncs_[DESTROY_SENSOR_CHANNEL] = &SensorServiceStub::DestroyDataChannelInner; + baseFuncs_[SUSPEND_SENSORS] = &SensorServiceStub::SuspendSensorsInner; + baseFuncs_[RESUME_SENSORS] = &SensorServiceStub::ResumeSensorsInner; + baseFuncs_[GET_APP_SENSOR_LIST] = &SensorServiceStub::GetAppSensorListInner; + baseFuncs_[REGISTER_CALLBACK] = &SensorServiceStub::RegisterCallbackInner; } SensorServiceStub::~SensorServiceStub() @@ -153,5 +156,84 @@ ErrCode SensorServiceStub::DestroyDataChannelInner(MessageParcel &data, MessageP CHKPR(sensorClient, OBJECT_NULL); return DestroySensorChannel(sensorClient); } + +ErrCode SensorServiceStub::SuspendSensorsInner(MessageParcel &data, MessageParcel &reply) +{ + PermissionUtil &permissionUtil = PermissionUtil::GetInstance(); + if(!permissionUtil.IsNativeToken(GetCallingTokenID())) { + SEN_HILOGE("TokenType is not TOKEN_NATIVE"); + return PERMISSION_DENIED; + } + (void)reply; + int32_t pid; + if (!data.ReadInt32(pid)) { + SEN_HILOGE("Parcel read failed"); + return ERROR; + } + return SuspendSensors(pid); +} + +ErrCode SensorServiceStub::ResumeSensorsInner(MessageParcel &data, MessageParcel &reply) +{ + PermissionUtil &permissionUtil = PermissionUtil::GetInstance(); + if(!permissionUtil.IsNativeToken(GetCallingTokenID())) { + SEN_HILOGE("TokenType is not TOKEN_NATIVE"); + return PERMISSION_DENIED; + } + (void)reply; + int32_t pid; + if (!data.ReadInt32(pid)) { + SEN_HILOGE("Parcel read failed"); + return ERROR; + } + return ResumeSensors(pid); +} + +ErrCode SensorServiceStub::GetAppSensorListInner(MessageParcel &data, MessageParcel &reply) +{ + PermissionUtil &permissionUtil = PermissionUtil::GetInstance(); + if(!permissionUtil.IsNativeToken(GetCallingTokenID())) { + SEN_HILOGE("TokenType is not TOKEN_NATIVE"); + return PERMISSION_DENIED; + } + int32_t pid; + if (!data.ReadInt32(pid)) { + SEN_HILOGE("Parcel read failed"); + return ERROR; + } + std::vector appSensors; + GetAppSensorList(pid, appSensors); + int32_t appSensorCount = int32_t { appSensors.size() }; + reply.WriteInt32(appSensorCount); + for (int32_t i = 0; i < appSensorCount; i++) { + bool flag = appSensors[i].Marshalling(reply); + if (!flag) { + SEN_HILOGE("AppSensor %{public}d failed", i); + return ERROR; + } + } + return NO_ERROR; +} + +ErrCode SensorServiceStub::RegisterCallbackInner(MessageParcel &data, MessageParcel &reply) +{ + PermissionUtil &permissionUtil = PermissionUtil::GetInstance(); + if(!permissionUtil.IsNativeToken(GetCallingTokenID())) { + SEN_HILOGE("TokenType is not TOKEN_NATIVE"); + return PERMISSION_DENIED; + } + (void)reply; + sptr obj = data.ReadRemoteObject(); + if (obj == nullptr) { + SEN_HILOGE("Data ReadRemoteObject() is nullptr"); + return ERROR; + } + sptr callback = iface_cast(obj); + if (callback == nullptr) { + SEN_HILOGE("Obj iface_cast ISensorStatusCallback failed"); + return ERROR; + } + return RegisterCallback(callback); +} } // namespace Sensors } // namespace OHOS diff --git a/services/sensor/src/sensor_status_callback_proxy.cpp b/services/sensor/src/sensor_status_callback_proxy.cpp new file mode 100644 index 00000000..8a99ee7c --- /dev/null +++ b/services/sensor/src/sensor_status_callback_proxy.cpp @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "sensor_status_callback_proxy.h" + +#include "message_parcel.h" +#include "hisysevent.h" + +#include "sensors_errors.h" + +namespace OHOS { +namespace Sensors { +namespace { +constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "SensorStatusCallbackProxy" }; +} // namespace + + +void SensorStatusCallbackProxy::OnSensorChanged(const AppSensorInfo &appSensorInfo) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(SensorStatusCallbackProxy::GetDescriptor())) { + SEN_HILOGE("Write descriptor failed"); + return; + } + if (!data.WriteInt32(appSensorInfo.pid)) { + SEN_HILOGE("Write pid failed"); + return; + } + if (!data.WriteInt32(appSensorInfo.sensorId)) { + SEN_HILOGE("Write sensorId failed"); + return; + } + if (!data.WriteBool(appSensorInfo.isActive)) { + SEN_HILOGE("Write isActive failed"); + return; + } + if (!data.WriteInt64(appSensorInfo.samplingPeriodNs)) { + SEN_HILOGE("Write samplingPeriodNs failed"); + return; + } + if (!data.WriteInt64(appSensorInfo.maxReportDelayNs)) { + SEN_HILOGE("Write maxReportDelayNs failed"); + return; + } + sptr remote = Remote(); + CHKPV(remote); + int32_t ret = remote->SendRequest(ISensorStatusCallback::SENSOR_CHANGE, data, reply, option); + if (ret != NO_ERROR) { + HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SENSOR, "SENSOR_CALLBACK_IPC_EXCEPTION", + HiviewDFX::HiSysEvent::EventType::FAULT, "PKG_NAME", "OnSensorChanged", "ERROR_CODE", ret); + SEN_HILOGE("Failed, ret:%{public}d", ret); + return; + } +} +} // namespace Sensors +} // namespace OHOS -- Gitee