From 9c9eca0415c91a6f53541c199ddcf96a920f64ab Mon Sep 17 00:00:00 2001 From: hui1975 Date: Sat, 25 Mar 2023 07:45:46 +0000 Subject: [PATCH 1/7] =?UTF-8?q?sensor=E7=9C=81=E5=8A=9F=E8=80=97=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E4=BB=A3=E7=A0=811?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hui1975 Change-Id: I390d026cb73ac39a73a095af3db4048cfd94bc9c --- services/sensor/BUILD.gn | 7 +- services/sensor/include/client_info.h | 7 + services/sensor/include/sensor_power_policy.h | 55 +++++ services/sensor/include/stream_server.h | 49 ++++ services/sensor/src/client_info.cpp | 46 ++++ services/sensor/src/sensor_power_policy.cpp | 209 ++++++++++++++++++ services/sensor/src/sensor_suspend_policy.cpp | 178 --------------- 7 files changed, 372 insertions(+), 179 deletions(-) create mode 100644 services/sensor/include/sensor_power_policy.h create mode 100644 services/sensor/include/stream_server.h create mode 100644 services/sensor/src/sensor_power_policy.cpp delete mode 100644 services/sensor/src/sensor_suspend_policy.cpp diff --git a/services/sensor/BUILD.gn b/services/sensor/BUILD.gn index eb574276..6356df68 100644 --- a/services/sensor/BUILD.gn +++ b/services/sensor/BUILD.gn @@ -27,6 +27,7 @@ ohos_shared_library("libsensor_service") { "src/sensor_data_processer.cpp", "src/sensor_dump.cpp", "src/sensor_manager.cpp", + "src/sensor_power_policy.cpp", "src/sensor_service.cpp", "src/sensor_service_stub.cpp", ] @@ -38,6 +39,7 @@ ohos_shared_library("libsensor_service") { "//commonlibrary/c_utils/base/include", "//utils/system/safwk/native/include", "$SUBSYSTEM_DIR/sensor/utils/common/include", + "$SUBSYSTEM_DIR/sensor/utils/ipc/include", "$SUBSYSTEM_DIR/sensor/services/sensor/include", "//drivers/peripheral/sensor/interfaces/include", "hdi_connection/interface/include", @@ -45,7 +47,10 @@ ohos_shared_library("libsensor_service") { "hdi_connection/hardware/include", ] - deps = [ "$SUBSYSTEM_DIR/sensor/utils/common:libsensor_utils" ] + deps = [ + "$SUBSYSTEM_DIR/sensor/utils/common:libsensor_utils", + "$SUBSYSTEM_DIR/sensor/utils/ipc:libsensor_ipc", + ] external_deps = [ "access_token:libaccesstoken_sdk", diff --git a/services/sensor/include/client_info.h b/services/sensor/include/client_info.h index 7356e6e3..514ba48a 100644 --- a/services/sensor/include/client_info.h +++ b/services/sensor/include/client_info.h @@ -20,6 +20,7 @@ #include #include #include +#include #include #include "refbase.h" @@ -75,6 +76,10 @@ public: void ClearDataQueue(int32_t sensorId); int32_t GetUidByPid(int32_t pid); AccessTokenID GetTokenIdByPid(int32_t pid); + int32_t AddActiveInfoCBPid(int32_t pid); + int32_t DelActiveInfoCBPid(int32_t pid); + std::unordered_set GetActiveInfoCBPid(); + bool IsUnregisterClientDeathRecipient(int32_t pid); int32_t GetPidByTokenId(AccessTokenID tokenId); void UpdatePermState(int32_t pid, int32_t sensorId, bool state); void ChangeSensorPerm(AccessTokenID tokenId, const std::string &permName, bool state); @@ -96,6 +101,8 @@ private: std::map, int32_t> clientPidMap_; std::unordered_map>> cmdMap_; std::unordered_map> dumpQueue_; + std::mutex activeInfoCBPidMutex_; + std::unordered_set activeInfoCBPidSet_; static std::unordered_map> userGrantPermMap_; }; } // namespace Sensors diff --git a/services/sensor/include/sensor_power_policy.h b/services/sensor/include/sensor_power_policy.h new file mode 100644 index 00000000..7433ecee --- /dev/null +++ b/services/sensor/include/sensor_power_policy.h @@ -0,0 +1,55 @@ +/* + * 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_POWER_POLICY_H +#define SENSOR_POWER_POLICY_H + +#include +#include +#include + +#include "nocopyable.h" + +#include "active_info.h" +#include "client_info.h" +#include "sensor_hdi_connection.h" +#include "sensor_manager.h" +#include "sensors_errors.h" +#include "stream_session.h" + +namespace OHOS { +namespace Sensors { +class SensorPowerPolicy : public Singleton{ +public: + ErrCode SuspendSensors(int32_t pid); + ErrCode ResumeSensors(int32_t pid); + void GetActiveInfoList(int32_t pid, std::vector &activeInfoList); + void ReportActiveInfo(ActiveInfo activeInfo, const std::vector &sessionList); + +private: + bool CheckFreezingSensor(int32_t sensorId); + bool Suspend(std::unordered_map &SensorInfoMap, + std::vector &sensorIdList, int32_t pid); + bool Resume(int32_t pid, int32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs); + ErrCode RestoreSensorInfo(int32_t pid, int32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs); + SensorHdiConnection &sensorHdiConnection_ = SensorHdiConnection::GetInstance(); + ClientInfo &clientInfo_ = ClientInfo::GetInstance(); + SensorManager &sensorManager_ = SensorManager::GetInstance(); + std::mutex pidSensorInfoMutex_; + std::unordered_map> pidSensorInfoMap_; +}; +} // namespace Sensors +} // namespace OHOS +#endif // SENSOR_POWER_POLICY_H \ No newline at end of file diff --git a/services/sensor/include/stream_server.h b/services/sensor/include/stream_server.h new file mode 100644 index 00000000..03e1ba7d --- /dev/null +++ b/services/sensor/include/stream_server.h @@ -0,0 +1,49 @@ +/* + * 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 STREAM_SERVER_H +#define STREAM_SERVER_H + +#include +#include + +#include "stream_session.h" +#include "stream_socket.h" + +namespace OHOS { +namespace Sensors { +class StreamServer : public StreamSocket { +public: + StreamServer() = default; + virtual ~StreamServer(); + bool SendMsg(int32_t fd, NetPacket& pkt); + void Multicast(const std::vector& fdList, NetPacket& pkt); + int32_t GetClientFd(int32_t pid); + int32_t GetClientPid(int32_t fd); + SessionPtr GetSession(int32_t fd); + SessionPtr GetSessionByPid(int32_t pid); + int32_t AddSocketPairInfo(int32_t uid, int32_t pid, int32_t tokenType, int32_t &serverFd, int32_t &clientFd); + +protected: + bool AddSession(SessionPtr ses); + void DelSession(int32_t pid); + std::mutex idxPidMutex_; + std::map idxPidMap_; + std::mutex sessionMutex_; + std::map sessionsMap_; +}; +} // namespace Sensors +} // namespace OHOS +#endif // STREAM_SERVER_H diff --git a/services/sensor/src/client_info.cpp b/services/sensor/src/client_info.cpp index 33a840a4..754d4efc 100644 --- a/services/sensor/src/client_info.cpp +++ b/services/sensor/src/client_info.cpp @@ -693,6 +693,52 @@ void ClientInfo::ClearDataQueue(int32_t sensorId) } } +int32_t ClientInfo::AddActiveInfoCBPid(int32_t pid) +{ + std::lock_guard activeInfoCBPidLock(activeInfoCBPidMutex_); + auto pairRet = activeInfoCBPidSet_.insert(pid); + if (!pairRet.second) { + SEN_HILOGE("ActiveInfoCBPidSet insert pid fail"); + return ERROR; + } + return ERR_OK; +} + +int32_t ClientInfo::DelActiveInfoCBPid(int32_t pid) +{ + std::lock_guard activeInfoCBPidLock(activeInfoCBPidMutex_); + auto it = activeInfoCBPidSet_.find(pid); + if (it == activeInfoCBPidSet_.end()) { + SEN_HILOGE("ActiveInfoCBPidSet not find pid"); + return ERROR; + } + activeInfoCBPidSet_.erase(it); + return ERR_OK; +} + +std::unordered_set ClientInfo::GetActiveInfoCBPid() +{ + return activeInfoCBPidSet_; +} + +bool ClientInfo::IsUnregisterClientDeathRecipient(int32_t pid) +{ + std::lock_guard channelLock(channelMutex_); + auto channelIt = channelMap_.find(pid); + if (channelIt != channelMap_.end()) { + SEN_HILOGD("Pid exist in channelMap"); + return false; + } + std::lock_guard activeInfoCBPidLock(activeInfoCBPidMutex_); + auto pidIt = activeInfoCBPidSet_.find(pid); + if (pidIt != activeInfoCBPidSet_.end()) { + SEN_HILOGD("Pid exist in activeInfoCBPidSet"); + return false; + } + return true; +} + + int32_t ClientInfo::GetPidByTokenId(AccessTokenID tokenId) { std::lock_guard uidLock(uidMutex_); diff --git a/services/sensor/src/sensor_power_policy.cpp b/services/sensor/src/sensor_power_policy.cpp new file mode 100644 index 00000000..9899efce --- /dev/null +++ b/services/sensor/src/sensor_power_policy.cpp @@ -0,0 +1,209 @@ +/* + * 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 + * + * 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_power_policy.h" + +#include "sensor.h" +#include "sensor_agent_type.h" + +namespace OHOS { +namespace Sensors { +using namespace OHOS::HiviewDFX; + +namespace { +constexpr HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "SensorPowerPolicy" }; +constexpr int32_t INVALID_SENSOR_ID = -1; +constexpr int64_t MAX_EVENT_COUNT = 1000; +} // namespace + +bool SensorPowerPolicy::CheckFreezingSensor(int32_t sensorId) +{ + return ((sensorId == SENSOR_TYPE_ID_PEDOMETER_DETECTION) || (sensorId == SENSOR_TYPE_ID_PEDOMETER)); +} + +ErrCode SensorPowerPolicy::SuspendSensors(int32_t pid) +{ + CALL_LOG_ENTER; + std::vector sensorIdList = clientInfo_.GetSensorIdByPid(pid); + std::lock_guard pidSensorInfoLock(pidSensorInfoMutex_); + auto pidSensorInfoIt = pidSensorInfoMap_.find(pid); + if (pidSensorInfoIt != pidSensorInfoMap_.end()) { + if (sensorIdList.empty()) { + SEN_HILOGD("Pid already suspend, all sensors suspend success, not need suspend again"); + return ERR_OK; + } else { + SEN_HILOGD("Pid already suspend, some sensors suspend failed, suspend these sensors again"); + std::unordered_map SensorInfoMap = pidSensorInfoIt->second; + if (!Suspend(SensorInfoMap, sensorIdList, pid)) { + SEN_HILOGE("Some sensor suspend failed"); + return DISABLE_SENSOR_ERR; + } + return ERR_OK; + } + } + if (sensorIdList.empty()) { + SEN_HILOGE("Pid sensorId list is empty"); + return ERROR; + } + std::unordered_map SensorInfoMap; + auto isAllSuspend = Suspend(SensorInfoMap, sensorIdList, pid); + pidSensorInfoMap_.insert(std::make_pair(pid, SensorInfoMap)); + if (!isAllSuspend) { + SEN_HILOGE("Some sensor suspend failed"); + return DISABLE_SENSOR_ERR; + } + return ERR_OK; +} + +bool SensorPowerPolicy::Suspend(std::unordered_map &SensorInfoMap, + std::vector &sensorIdList, int32_t pid) +{ + CALL_LOG_ENTER; + bool isAllSuspend = true; + for (auto &sensorId : sensorIdList) { + if (CheckFreezingSensor(sensorId)) { + SEN_HILOGD("Current sensor is pedometer detectio or pedometer, can not suspend"); + continue; + } + auto sensorInfo = clientInfo_.GetCurPidSensorInfo(sensorId, pid); + if (sensorManager_.IsOtherClientUsingSensor(sensorId, pid)) { + SEN_HILOGD("Other client is using this sensor now, cannot suspend"); + SensorInfoMap.insert(std::make_pair(sensorId, sensorInfo)); + continue; + } + if (sensorHdiConnection_.DisableSensor(sensorId) != ERR_OK) { + SEN_HILOGE("Disable sensor failed, sensorId:%{public}d", sensorId); + isAllSuspend = false; + continue; + } + SensorInfoMap.insert(std::make_pair(sensorId, sensorInfo)); + sensorManager_.AfterDisableSensor(sensorId); + } + return isAllSuspend; +} + +ErrCode SensorPowerPolicy::ResumeSensors(int32_t pid) +{ + CALL_LOG_ENTER; + std::lock_guard pidSensorInfoLock(pidSensorInfoMutex_); + auto pidSensorInfoIt = pidSensorInfoMap_.find(pid); + if (pidSensorInfoIt == pidSensorInfoMap_.end()) { + SEN_HILOGE("Pid not have suspend sensors"); + return ERROR; + } + bool isAllResume = true; + std::unordered_map SensorInfoMap = pidSensorInfoIt->second; + for (auto sensorIt = SensorInfoMap.begin(); sensorIt != SensorInfoMap.end();) { + int32_t sensorId = sensorIt->first; + int64_t samplingPeriodNs = sensorIt->second.GetSamplingPeriodNs(); + int64_t maxReportDelayNs = sensorIt->second.GetMaxReportDelayNs(); + if (!Resume(pid, sensorId, samplingPeriodNs, maxReportDelayNs)) { + SEN_HILOGE("Resume sensor failed. sensorId:%{public}d", sensorId); + isAllResume = false; + ++sensorIt; + } else { + sensorIt = SensorInfoMap.erase(sensorIt); + } + } + if (!isAllResume) { + SEN_HILOGE("Some sensor resume failed"); + return ENABLE_SENSOR_ERR; + } + pidSensorInfoMap_.erase(pidSensorInfoIt); + return ERR_OK; +} + +bool SensorPowerPolicy::Resume(int32_t pid, int32_t sensorId, int64_t samplingPeriodNs, + int64_t maxReportDelayNs) +{ + CALL_LOG_ENTER; + if ((sensorId == INVALID_SENSOR_ID) || (samplingPeriodNs == 0) || + ((samplingPeriodNs != 0L) && (maxReportDelayNs / samplingPeriodNs > MAX_EVENT_COUNT))) { + SEN_HILOGE("SensorId is invalid or maxReportDelayNs exceed the maximum value"); + return false; + } + if (clientInfo_.GetSensorState(sensorId)) { + SEN_HILOGD("Sensor has been resume already, sensorId:%{public}d", sensorId); + auto ret = RestoreSensorInfo(pid, sensorId, samplingPeriodNs, maxReportDelayNs); + if (ret != ERR_OK) { + SEN_HILOGE("Restore sensor info failed, ret:%{public}d", ret); + return false; + } + return true; + } + auto ret = RestoreSensorInfo(pid, sensorId, samplingPeriodNs, maxReportDelayNs); + if (ret != ERR_OK) { + SEN_HILOGE("Restore sensor info failed, ret:%{public}d", ret); + return false; + } + ret = sensorHdiConnection_.EnableSensor(sensorId); + if (ret != ERR_OK) { + SEN_HILOGE("Resume sensor failed, ret:%{public}d", ret); + clientInfo_.RemoveSubscriber(sensorId, pid); + return false; + } + return true; +} + +ErrCode SensorPowerPolicy::RestoreSensorInfo(int32_t pid, int32_t sensorId, int64_t samplingPeriodNs, + int64_t maxReportDelayNs) +{ + CALL_LOG_ENTER; + auto ret = sensorManager_.SaveSubscriber(sensorId, pid, samplingPeriodNs, maxReportDelayNs); + if (ret != ERR_OK) { + SEN_HILOGE("SaveSubscriber failed, ret:%{public}d", ret); + return ret; + } + sensorManager_.StartDataReportThread(); + if (!sensorManager_.SetBestSensorParams(sensorId, samplingPeriodNs, maxReportDelayNs)) { + SEN_HILOGE("SetBestSensorParams failed"); + clientInfo_.RemoveSubscriber(sensorId, pid); + return ENABLE_SENSOR_ERR; + } + return ERR_OK; +} + +void SensorPowerPolicy::GetActiveInfoList(int32_t pid, std::vector &activeInfoList) +{ + CALL_LOG_ENTER; + std::vector sensorIdList = clientInfo_.GetSensorIdByPid(pid); + for (auto &sensorId : sensorIdList) { + auto sensorInfo = clientInfo_.GetCurPidSensorInfo(sensorId, pid); + ActiveInfo activeInfo(pid, sensorId, sensorInfo.GetSamplingPeriodNs(), + sensorInfo.GetMaxReportDelayNs()); + activeInfoList.push_back(activeInfo); + } +} + +void SensorPowerPolicy::ReportActiveInfo(ActiveInfo activeInfo, + const std::vector &sessionList) +{ + CALL_LOG_ENTER; + NetPacket pkt(MessageId::ACTIVE_INFO); + pkt << activeInfo.GetPid() << activeInfo.GetSensorId() << + activeInfo.GetSamplingPeriodNs() << activeInfo.GetMaxReportDelayNs(); + if (pkt.ChkRWError()) { + SEN_HILOGE("Packet write data failed"); + return; + } + for (auto sess : sessionList) { + if (!sess->SendMsg(pkt)) { + SEN_HILOGE("Packet send failed"); + continue; + } + } +} +} // namespace Sensors +} // namespace OHOS \ No newline at end of file diff --git a/services/sensor/src/sensor_suspend_policy.cpp b/services/sensor/src/sensor_suspend_policy.cpp deleted file mode 100644 index 82ac9832..00000000 --- a/services/sensor/src/sensor_suspend_policy.cpp +++ /dev/null @@ -1,178 +0,0 @@ -/* - * Copyright (c) 2021 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_suspend_policy.h" - -#include "sensor.h" -#include "sensor_agent_type.h" -#include "sensor_service.h" -#include "system_ability_definition.h" - -namespace OHOS { -namespace Sensors { -using namespace OHOS::HiviewDFX; - -namespace { -constexpr HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "SensorSuspendPolicy" }; -constexpr int32_t INVALID_SENSOR_ID = -1; -constexpr int64_t MAX_EVENT_COUNT = 1000; -constexpr int64_t DEFAULT_SAMPLING_RATE = 200000000; -constexpr int64_t DEFAULT_REPORT_DELAY = 0; -} // namespace - -SensorSuspendPolicy::~SensorSuspendPolicy() -{} - -bool SensorSuspendPolicy::CheckFreezingSensor(int32_t sensorId) -{ - return ((sensorId == SENSOR_TYPE_ID_PEDOMETER_DETECTION) || (sensorId == SENSOR_TYPE_ID_PEDOMETER)); -} - -ErrCode SensorSuspendPolicy::DisableSensor(int32_t sensorId, int32_t pid) -{ - CALL_LOG_ENTER; - if (sensorId == INVALID_SENSOR_ID) { - SEN_HILOGE("sensorId is invalid"); - return ERR_NO_INIT; - } - if (sensorManager_.IsOtherClientUsingSensor(sensorId, pid)) { - SEN_HILOGW("other client is using this sensor now, cannot disable"); - return ERR_OK; - } - if (interface_.DisableSensor(sensorId) != ERR_OK) { - SEN_HILOGE("DisableSensor is failed"); - return DISABLE_SENSOR_ERR; - } - return sensorManager_.AfterDisableSensor(sensorId); -} - -void SensorSuspendPolicy::DoSuspend(const std::shared_ptr &info) -{ - CALL_LOG_ENTER; - std::lock_guard suspendLock(suspendMutex_); - auto &list = info->GetAppIPCInfoList(); - for (const auto &appInfo : list) { - std::vector sensorIdList = clientInfo_.GetSensorIdByPid(appInfo.pid); - if (sensorIdList.empty()) { - continue; - } - pidSensorIdMap_.emplace(std::make_pair(appInfo.pid, sensorIdList)); - for (auto &sensorId : sensorIdList) { - if (CheckFreezingSensor(sensorId)) { - continue; - } - auto sensorInfo = clientInfo_.GetCurPidSensorInfo(sensorId, appInfo.pid); - sensorIdInfoMap_.emplace(std::make_pair(sensorId, sensorInfo)); - DisableSensor(sensorId, appInfo.pid); - } - } -} - -ErrCode SensorSuspendPolicy::SaveSubscriber(int32_t sensorId, int64_t samplingPeriodNs, - int64_t maxReportDelayNs, int32_t pid) -{ - auto ret = sensorManager_.SaveSubscriber(sensorId, pid, samplingPeriodNs, maxReportDelayNs); - if (ret != ERR_OK) { - SEN_HILOGE("SaveSubscriber is failed"); - return ret; - } - sensorManager_.StartDataReportThread(); - if (!sensorManager_.SetBestSensorParams(sensorId, samplingPeriodNs, maxReportDelayNs)) { - SEN_HILOGE("SetBestSensorParams is failed"); - clientInfo_.RemoveSubscriber(sensorId, pid); - return ENABLE_SENSOR_ERR; - } - return ret; -} - -ErrCode SensorSuspendPolicy::EnableSensor(int32_t sensorId, int32_t pid, int64_t samplingPeriodNs, - int64_t maxReportDelayNs) -{ - CALL_LOG_ENTER; - if ((sensorId == INVALID_SENSOR_ID) || (samplingPeriodNs == 0) || - ((samplingPeriodNs != 0L) && (maxReportDelayNs / samplingPeriodNs > MAX_EVENT_COUNT))) { - SEN_HILOGE("sensorId is 0 or maxReportDelayNs exceed the maximum value"); - return ERR_NO_INIT; - } - if (clientInfo_.GetSensorState(sensorId) == SENSOR_ENABLED) { - SEN_HILOGW("sensor has been enabled already"); - auto ret = SaveSubscriber(sensorId, samplingPeriodNs, maxReportDelayNs, pid); - if (ret != ERR_OK) { - SEN_HILOGE("SaveSubscriber is failed"); - return ret; - } - uint32_t flag = sensorManager_.GetSensorFlag(sensorId); - ret = flushInfo_.FlushProcess(sensorId, flag, pid, true); - if (ret != ERR_OK) { - SEN_HILOGW("ret:%{public}d", ret); - } - return ERR_OK; - } - auto ret = SaveSubscriber(sensorId, samplingPeriodNs, maxReportDelayNs, pid); - if (ret != ERR_OK) { - SEN_HILOGE("SaveSubscriber is failed"); - return ret; - } - ret = interface_.EnableSensor(sensorId); - if (ret != ERR_OK) { - SEN_HILOGE("EnableSensor is failed"); - clientInfo_.RemoveSubscriber(sensorId, pid); - return ENABLE_SENSOR_ERR; - } - return ret; -} - -std::vector SensorSuspendPolicy::GetSensorIdByPid(int32_t pid) -{ - SEN_HILOGD("pid:%{public}d", pid); - auto it = pidSensorIdMap_.find(pid); - if (it != pidSensorIdMap_.end()) { - SEN_HILOGD("pid:%{public}d found", pid); - return it->second; - } - SEN_HILOGD("pid:%{public}d not found", pid); - return {}; -} - -void SensorSuspendPolicy::DoActive(const std::shared_ptr &info) -{ - CALL_LOG_ENTER; - int64_t samplePeriod = DEFAULT_SAMPLING_RATE; - int64_t maxReportDelay = DEFAULT_REPORT_DELAY; - std::vector sensorIdList; - std::lock_guard suspendLock(suspendMutex_); - auto &list = info->GetAppIPCInfoList(); - for (const auto &appInfo : list) { - sensorIdList = GetSensorIdByPid(appInfo.pid); - for (auto &sensorId : sensorIdList) { - if (CheckFreezingSensor(sensorId)) { - continue; - } - auto it = sensorIdInfoMap_.find(sensorId); - if (it != sensorIdInfoMap_.end()) { - samplePeriod = it->second.GetSamplingPeriodNs(); - maxReportDelay = it->second.GetMaxReportDelayNs(); - } - auto ret = EnableSensor(sensorId, appInfo.pid, samplePeriod, maxReportDelay); - if (ret != ERR_OK) { - SEN_HILOGE("sensorId:%{public}u,pid:%{public}d,ret:%{public}d", sensorId, appInfo.pid, ret); - } - } - } - pidSensorIdMap_.clear(); - sensorIdInfoMap_.clear(); -} -} // namespace Sensors -} // namespace OHOS -- Gitee From 1afe0104dd6f38313cbe35af44fbb22fabb50e08 Mon Sep 17 00:00:00 2001 From: hui1975 Date: Sat, 25 Mar 2023 07:50:33 +0000 Subject: [PATCH 2/7] update Signed-off-by: hui1975 Change-Id: I48e39b6a19ef2b25c9b019d36278616041aec7fe --- services/sensor/include/sensor_service.h | 16 ++++++++++++++-- services/sensor/include/sensor_service_stub.h | 9 ++++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/services/sensor/include/sensor_service.h b/services/sensor/include/sensor_service.h index 3f1a3a9f..f2eaedd1 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 @@ -28,7 +28,9 @@ #include "sensor_data_event.h" #include "sensor_hdi_connection.h" #include "sensor_manager.h" +#include "sensor_power_policy.h" #include "sensor_service_stub.h" +#include "stream_server.h" namespace OHOS { namespace Sensors { @@ -37,7 +39,7 @@ enum class SensorServiceState { STATE_RUNNING, }; -class SensorService : public SystemAbility, public SensorServiceStub { +class SensorService : public SystemAbility, public StreamServer, public SensorServiceStub { DECLARE_SYSTEM_ABILITY(SensorService) public: @@ -54,6 +56,13 @@ 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 GetActiveInfoList(int32_t pid, std::vector &activeInfoList) override; + ErrCode CreateSocketChannel(int32_t &clientFd, const sptr &sensorClient) override; + ErrCode DestroySocketChannel(const sptr &sensorClient) override; + ErrCode EnableActiveInfoCB() override; + ErrCode DisableActiveInfoCB() override; private: DISALLOW_COPY_AND_MOVE(SensorService); @@ -88,6 +97,7 @@ private: SensorHdiConnection &sensorHdiConnection_ = SensorHdiConnection::GetInstance(); ClientInfo &clientInfo_ = ClientInfo::GetInstance(); SensorManager &sensorManager_ = SensorManager::GetInstance(); + SensorPowerPolicy &sensorPowerPolicy_ = SensorPowerPolicy::GetInstance(); sptr sensorDataProcesser_ = nullptr; sptr reportDataCallback_ = nullptr; std::mutex uidLock_; @@ -95,6 +105,8 @@ private: sptr clientDeathObserver_ = nullptr; std::shared_ptr permStateChangeCb_; ErrCode SaveSubscriber(int32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs); + std::atomic_bool isReportActiveInfo_ = false; + void ReportActiveInfo(int32_t sensorId, int32_t pid); }; } // namespace Sensors } // namespace OHOS diff --git a/services/sensor/include/sensor_service_stub.h b/services/sensor/include/sensor_service_stub.h index 9259e793..60b4ba49 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,13 @@ 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 GetActiveInfoListInner(MessageParcel &data, MessageParcel &reply); + ErrCode CreateSocketChannelInner(MessageParcel &data, MessageParcel &reply); + ErrCode DestroySocketChannelInner(MessageParcel &data, MessageParcel &reply); + ErrCode EnableActiveInfoCBInner(MessageParcel &data, MessageParcel &reply); + ErrCode DisableActiveInfoCBInner(MessageParcel &data, MessageParcel &reply); std::unordered_map baseFuncs_; }; } // namespace Sensors -- Gitee From aae7a627af3d5173f07ccb22a7fbfb480e473f31 Mon Sep 17 00:00:00 2001 From: hui1975 Date: Sat, 25 Mar 2023 08:31:47 +0000 Subject: [PATCH 3/7] update Signed-off-by: hui1975 Change-Id: I85a7b2822cd27f607901c40d698f9da558fa6ae2 --- services/sensor/include/sensor_service.h | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/services/sensor/include/sensor_service.h b/services/sensor/include/sensor_service.h index f2eaedd1..3f1a3a9f 100644 --- a/services/sensor/include/sensor_service.h +++ b/services/sensor/include/sensor_service.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2023 Huawei Device Co., Ltd. + * Copyright (c) 2021 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 @@ -28,9 +28,7 @@ #include "sensor_data_event.h" #include "sensor_hdi_connection.h" #include "sensor_manager.h" -#include "sensor_power_policy.h" #include "sensor_service_stub.h" -#include "stream_server.h" namespace OHOS { namespace Sensors { @@ -39,7 +37,7 @@ enum class SensorServiceState { STATE_RUNNING, }; -class SensorService : public SystemAbility, public StreamServer, public SensorServiceStub { +class SensorService : public SystemAbility, public SensorServiceStub { DECLARE_SYSTEM_ABILITY(SensorService) public: @@ -56,13 +54,6 @@ 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 GetActiveInfoList(int32_t pid, std::vector &activeInfoList) override; - ErrCode CreateSocketChannel(int32_t &clientFd, const sptr &sensorClient) override; - ErrCode DestroySocketChannel(const sptr &sensorClient) override; - ErrCode EnableActiveInfoCB() override; - ErrCode DisableActiveInfoCB() override; private: DISALLOW_COPY_AND_MOVE(SensorService); @@ -97,7 +88,6 @@ private: SensorHdiConnection &sensorHdiConnection_ = SensorHdiConnection::GetInstance(); ClientInfo &clientInfo_ = ClientInfo::GetInstance(); SensorManager &sensorManager_ = SensorManager::GetInstance(); - SensorPowerPolicy &sensorPowerPolicy_ = SensorPowerPolicy::GetInstance(); sptr sensorDataProcesser_ = nullptr; sptr reportDataCallback_ = nullptr; std::mutex uidLock_; @@ -105,8 +95,6 @@ private: sptr clientDeathObserver_ = nullptr; std::shared_ptr permStateChangeCb_; ErrCode SaveSubscriber(int32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs); - std::atomic_bool isReportActiveInfo_ = false; - void ReportActiveInfo(int32_t sensorId, int32_t pid); }; } // namespace Sensors } // namespace OHOS -- Gitee From 2482afc0b44ab4de056fff23451dc5443cb9373a Mon Sep 17 00:00:00 2001 From: hui1975 Date: Mon, 27 Mar 2023 12:23:56 +0000 Subject: [PATCH 4/7] update Signed-off-by: hui1975 Change-Id: If5721e45a873db8411926813eaba6d86caea133c --- services/sensor/include/sensor_power_policy.h | 4 +- services/sensor/src/sensor_power_policy.cpp | 57 +++++++++---------- utils/ipc/src/stream_socket.cpp | 2 +- 3 files changed, 29 insertions(+), 34 deletions(-) diff --git a/services/sensor/include/sensor_power_policy.h b/services/sensor/include/sensor_power_policy.h index 7433ecee..cba4d092 100644 --- a/services/sensor/include/sensor_power_policy.h +++ b/services/sensor/include/sensor_power_policy.h @@ -40,8 +40,8 @@ public: private: bool CheckFreezingSensor(int32_t sensorId); - bool Suspend(std::unordered_map &SensorInfoMap, - std::vector &sensorIdList, int32_t pid); + bool Suspend(int32_t pid, std::vector &sensorIdList, + std::unordered_map &SensorInfoMap); bool Resume(int32_t pid, int32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs); ErrCode RestoreSensorInfo(int32_t pid, int32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs); SensorHdiConnection &sensorHdiConnection_ = SensorHdiConnection::GetInstance(); diff --git a/services/sensor/src/sensor_power_policy.cpp b/services/sensor/src/sensor_power_policy.cpp index 9899efce..cfe41444 100644 --- a/services/sensor/src/sensor_power_policy.cpp +++ b/services/sensor/src/sensor_power_policy.cpp @@ -37,29 +37,24 @@ ErrCode SensorPowerPolicy::SuspendSensors(int32_t pid) { CALL_LOG_ENTER; std::vector sensorIdList = clientInfo_.GetSensorIdByPid(pid); + if (sensorIdList.empty()) { + SEN_HILOGW("SensorIdList is empty, pid not enable sensor or all sensor have bean suspend"); + return ERR_OK; + } std::lock_guard pidSensorInfoLock(pidSensorInfoMutex_); auto pidSensorInfoIt = pidSensorInfoMap_.find(pid); if (pidSensorInfoIt != pidSensorInfoMap_.end()) { - if (sensorIdList.empty()) { - SEN_HILOGD("Pid already suspend, all sensors suspend success, not need suspend again"); - return ERR_OK; - } else { - SEN_HILOGD("Pid already suspend, some sensors suspend failed, suspend these sensors again"); - std::unordered_map SensorInfoMap = pidSensorInfoIt->second; - if (!Suspend(SensorInfoMap, sensorIdList, pid)) { - SEN_HILOGE("Some sensor suspend failed"); - return DISABLE_SENSOR_ERR; - } - return ERR_OK; + SEN_HILOGI("Pid already call suspend, but some sensors suspend failed, suspend these sensors again"); + std::unordered_map sensorInfoMap = pidSensorInfoIt->second; + if (!Suspend(pid, sensorIdList, sensorInfoMap)) { + SEN_HILOGE("Some sensor suspend failed"); + return DISABLE_SENSOR_ERR; } + return ERR_OK; } - if (sensorIdList.empty()) { - SEN_HILOGE("Pid sensorId list is empty"); - return ERROR; - } - std::unordered_map SensorInfoMap; - auto isAllSuspend = Suspend(SensorInfoMap, sensorIdList, pid); - pidSensorInfoMap_.insert(std::make_pair(pid, SensorInfoMap)); + std::unordered_map sensorInfoMap; + auto isAllSuspend = Suspend(pid, sensorIdList, sensorInfoMap); + pidSensorInfoMap_.insert(std::make_pair(pid, sensorInfoMap)); if (!isAllSuspend) { SEN_HILOGE("Some sensor suspend failed"); return DISABLE_SENSOR_ERR; @@ -67,8 +62,8 @@ ErrCode SensorPowerPolicy::SuspendSensors(int32_t pid) return ERR_OK; } -bool SensorPowerPolicy::Suspend(std::unordered_map &SensorInfoMap, - std::vector &sensorIdList, int32_t pid) +bool SensorPowerPolicy::Suspend(int32_t pid, std::vector &sensorIdList, + std::unordered_map &sensorInfoMap) { CALL_LOG_ENTER; bool isAllSuspend = true; @@ -80,7 +75,7 @@ bool SensorPowerPolicy::Suspend(std::unordered_map &Se auto sensorInfo = clientInfo_.GetCurPidSensorInfo(sensorId, pid); if (sensorManager_.IsOtherClientUsingSensor(sensorId, pid)) { SEN_HILOGD("Other client is using this sensor now, cannot suspend"); - SensorInfoMap.insert(std::make_pair(sensorId, sensorInfo)); + sensorInfoMap.insert(std::make_pair(sensorId, sensorInfo)); continue; } if (sensorHdiConnection_.DisableSensor(sensorId) != ERR_OK) { @@ -88,7 +83,7 @@ bool SensorPowerPolicy::Suspend(std::unordered_map &Se isAllSuspend = false; continue; } - SensorInfoMap.insert(std::make_pair(sensorId, sensorInfo)); + sensorInfoMap.insert(std::make_pair(sensorId, sensorInfo)); sensorManager_.AfterDisableSensor(sensorId); } return isAllSuspend; @@ -104,8 +99,8 @@ ErrCode SensorPowerPolicy::ResumeSensors(int32_t pid) return ERROR; } bool isAllResume = true; - std::unordered_map SensorInfoMap = pidSensorInfoIt->second; - for (auto sensorIt = SensorInfoMap.begin(); sensorIt != SensorInfoMap.end();) { + std::unordered_map sensorInfoMap = pidSensorInfoIt->second; + for (auto sensorIt = sensorInfoMap.begin(); sensorIt != sensorInfoMap.end();) { int32_t sensorId = sensorIt->first; int64_t samplingPeriodNs = sensorIt->second.GetSamplingPeriodNs(); int64_t maxReportDelayNs = sensorIt->second.GetMaxReportDelayNs(); @@ -114,7 +109,7 @@ ErrCode SensorPowerPolicy::ResumeSensors(int32_t pid) isAllResume = false; ++sensorIt; } else { - sensorIt = SensorInfoMap.erase(sensorIt); + sensorIt = sensorInfoMap.erase(sensorIt); } } if (!isAllResume) { @@ -126,16 +121,16 @@ ErrCode SensorPowerPolicy::ResumeSensors(int32_t pid) } bool SensorPowerPolicy::Resume(int32_t pid, int32_t sensorId, int64_t samplingPeriodNs, - int64_t maxReportDelayNs) + int64_t maxReportDelayNs) { CALL_LOG_ENTER; - if ((sensorId == INVALID_SENSOR_ID) || (samplingPeriodNs == 0) || + if ((sensorId == INVALID_SENSOR_ID) || (samplingPeriodNs <= 0) || ((samplingPeriodNs != 0L) && (maxReportDelayNs / samplingPeriodNs > MAX_EVENT_COUNT))) { SEN_HILOGE("SensorId is invalid or maxReportDelayNs exceed the maximum value"); return false; } if (clientInfo_.GetSensorState(sensorId)) { - SEN_HILOGD("Sensor has been resume already, sensorId:%{public}d", sensorId); + SEN_HILOGD("Sensor is enable, sensorId:%{public}d", sensorId); auto ret = RestoreSensorInfo(pid, sensorId, samplingPeriodNs, maxReportDelayNs); if (ret != ERR_OK) { SEN_HILOGE("Restore sensor info failed, ret:%{public}d", ret); @@ -158,7 +153,7 @@ bool SensorPowerPolicy::Resume(int32_t pid, int32_t sensorId, int64_t samplingPe } ErrCode SensorPowerPolicy::RestoreSensorInfo(int32_t pid, int32_t sensorId, int64_t samplingPeriodNs, - int64_t maxReportDelayNs) + int64_t maxReportDelayNs) { CALL_LOG_ENTER; auto ret = sensorManager_.SaveSubscriber(sensorId, pid, samplingPeriodNs, maxReportDelayNs); @@ -182,13 +177,13 @@ void SensorPowerPolicy::GetActiveInfoList(int32_t pid, std::vector & for (auto &sensorId : sensorIdList) { auto sensorInfo = clientInfo_.GetCurPidSensorInfo(sensorId, pid); ActiveInfo activeInfo(pid, sensorId, sensorInfo.GetSamplingPeriodNs(), - sensorInfo.GetMaxReportDelayNs()); + sensorInfo.GetMaxReportDelayNs()); activeInfoList.push_back(activeInfo); } } void SensorPowerPolicy::ReportActiveInfo(ActiveInfo activeInfo, - const std::vector &sessionList) + const std::vector &sessionList) { CALL_LOG_ENTER; NetPacket pkt(MessageId::ACTIVE_INFO); diff --git a/utils/ipc/src/stream_socket.cpp b/utils/ipc/src/stream_socket.cpp index d2ca0c9b..9dd64d54 100644 --- a/utils/ipc/src/stream_socket.cpp +++ b/utils/ipc/src/stream_socket.cpp @@ -140,7 +140,7 @@ void StreamSocket::Close() { if (fd_ >= 0) { auto rf = close(fd_); - if (rf > 0) { + if (rf != 0) { SEN_HILOGE("Socket close failed, rf:%{public}d", rf); } } -- Gitee From a24abb59c07b9671c5fd8c039b0e0ed0f545f5ad Mon Sep 17 00:00:00 2001 From: hui1975 Date: Mon, 27 Mar 2023 12:38:43 +0000 Subject: [PATCH 5/7] update Signed-off-by: hui1975 Change-Id: I233bc2cbcce4ab3da55fbfa40a9df7d8bca8db6b --- services/sensor/include/sensor_power_policy.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/sensor/include/sensor_power_policy.h b/services/sensor/include/sensor_power_policy.h index cba4d092..daf960d1 100644 --- a/services/sensor/include/sensor_power_policy.h +++ b/services/sensor/include/sensor_power_policy.h @@ -31,7 +31,7 @@ namespace OHOS { namespace Sensors { -class SensorPowerPolicy : public Singleton{ +class SensorPowerPolicy : public Singleton { public: ErrCode SuspendSensors(int32_t pid); ErrCode ResumeSensors(int32_t pid); -- Gitee From a167ef5c32942950e7a4117586c4e2dfaf4c0fdc Mon Sep 17 00:00:00 2001 From: hui1975 Date: Wed, 29 Mar 2023 03:46:43 +0000 Subject: [PATCH 6/7] update Signed-off-by: hui1975 Change-Id: Ifa047f878290935ca1872c6c2612c0887a8efcf6 --- services/sensor/include/sensor_power_policy.h | 3 --- services/sensor/src/client_info.cpp | 4 ++-- services/sensor/src/sensor_power_policy.cpp | 9 ++++++--- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/services/sensor/include/sensor_power_policy.h b/services/sensor/include/sensor_power_policy.h index daf960d1..10eb4fab 100644 --- a/services/sensor/include/sensor_power_policy.h +++ b/services/sensor/include/sensor_power_policy.h @@ -44,9 +44,6 @@ private: std::unordered_map &SensorInfoMap); bool Resume(int32_t pid, int32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs); ErrCode RestoreSensorInfo(int32_t pid, int32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs); - SensorHdiConnection &sensorHdiConnection_ = SensorHdiConnection::GetInstance(); - ClientInfo &clientInfo_ = ClientInfo::GetInstance(); - SensorManager &sensorManager_ = SensorManager::GetInstance(); std::mutex pidSensorInfoMutex_; std::unordered_map> pidSensorInfoMap_; }; diff --git a/services/sensor/src/client_info.cpp b/services/sensor/src/client_info.cpp index 754d4efc..f37b2960 100644 --- a/services/sensor/src/client_info.cpp +++ b/services/sensor/src/client_info.cpp @@ -698,7 +698,7 @@ int32_t ClientInfo::AddActiveInfoCBPid(int32_t pid) std::lock_guard activeInfoCBPidLock(activeInfoCBPidMutex_); auto pairRet = activeInfoCBPidSet_.insert(pid); if (!pairRet.second) { - SEN_HILOGE("ActiveInfoCBPidSet insert pid fail"); + SEN_HILOGE("Pid is duplicated"); return ERROR; } return ERR_OK; @@ -709,7 +709,7 @@ int32_t ClientInfo::DelActiveInfoCBPid(int32_t pid) std::lock_guard activeInfoCBPidLock(activeInfoCBPidMutex_); auto it = activeInfoCBPidSet_.find(pid); if (it == activeInfoCBPidSet_.end()) { - SEN_HILOGE("ActiveInfoCBPidSet not find pid"); + SEN_HILOGE("Pid is not exists"); return ERROR; } activeInfoCBPidSet_.erase(it); diff --git a/services/sensor/src/sensor_power_policy.cpp b/services/sensor/src/sensor_power_policy.cpp index cfe41444..ebdb6901 100644 --- a/services/sensor/src/sensor_power_policy.cpp +++ b/services/sensor/src/sensor_power_policy.cpp @@ -26,6 +26,9 @@ namespace { constexpr HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "SensorPowerPolicy" }; constexpr int32_t INVALID_SENSOR_ID = -1; constexpr int64_t MAX_EVENT_COUNT = 1000; +ClientInfo &clientInfo_ = ClientInfo::GetInstance(); +SensorManager &sensorManager_ = SensorManager::GetInstance(); +SensorHdiConnection &sensorHdiConnection_ = SensorHdiConnection::GetInstance(); } // namespace bool SensorPowerPolicy::CheckFreezingSensor(int32_t sensorId) @@ -47,7 +50,7 @@ ErrCode SensorPowerPolicy::SuspendSensors(int32_t pid) SEN_HILOGI("Pid already call suspend, but some sensors suspend failed, suspend these sensors again"); std::unordered_map sensorInfoMap = pidSensorInfoIt->second; if (!Suspend(pid, sensorIdList, sensorInfoMap)) { - SEN_HILOGE("Some sensor suspend failed"); + SEN_HILOGE("Suspend last failed sensors, but some failed"); return DISABLE_SENSOR_ERR; } return ERR_OK; @@ -56,7 +59,7 @@ ErrCode SensorPowerPolicy::SuspendSensors(int32_t pid) auto isAllSuspend = Suspend(pid, sensorIdList, sensorInfoMap); pidSensorInfoMap_.insert(std::make_pair(pid, sensorInfoMap)); if (!isAllSuspend) { - SEN_HILOGE("Some sensor suspend failed"); + SEN_HILOGE("Suspend all sensors, but some failed"); return DISABLE_SENSOR_ERR; } return ERR_OK; @@ -69,7 +72,7 @@ bool SensorPowerPolicy::Suspend(int32_t pid, std::vector &sensorIdList, bool isAllSuspend = true; for (auto &sensorId : sensorIdList) { if (CheckFreezingSensor(sensorId)) { - SEN_HILOGD("Current sensor is pedometer detectio or pedometer, can not suspend"); + SEN_HILOGD("Current sensor is pedometer detection or pedometer, can not suspend"); continue; } auto sensorInfo = clientInfo_.GetCurPidSensorInfo(sensorId, pid); -- Gitee From b819f776290172e5f2f2b3c2a07e617a66abbbb7 Mon Sep 17 00:00:00 2001 From: hui1975 Date: Tue, 11 Apr 2023 11:20:08 +0000 Subject: [PATCH 7/7] update Signed-off-by: hui1975 Change-Id: Idcef16db0569042776f7fb2e5f047c07b80555bc --- services/sensor/include/client_info.h | 2 +- services/sensor/include/sensor_power_policy.h | 4 ++-- services/sensor/include/stream_server.h | 4 ++-- services/sensor/src/client_info.cpp | 12 ++++++++---- services/sensor/src/sensor_power_policy.cpp | 6 ++++-- utils/ipc/include/stream_session.h | 2 +- utils/ipc/src/stream_session.cpp | 2 +- 7 files changed, 19 insertions(+), 13 deletions(-) diff --git a/services/sensor/include/client_info.h b/services/sensor/include/client_info.h index 514ba48a..bf89eaaf 100644 --- a/services/sensor/include/client_info.h +++ b/services/sensor/include/client_info.h @@ -78,7 +78,7 @@ public: AccessTokenID GetTokenIdByPid(int32_t pid); int32_t AddActiveInfoCBPid(int32_t pid); int32_t DelActiveInfoCBPid(int32_t pid); - std::unordered_set GetActiveInfoCBPid(); + std::vector GetActiveInfoCBPid(); bool IsUnregisterClientDeathRecipient(int32_t pid); int32_t GetPidByTokenId(AccessTokenID tokenId); void UpdatePermState(int32_t pid, int32_t sensorId, bool state); diff --git a/services/sensor/include/sensor_power_policy.h b/services/sensor/include/sensor_power_policy.h index 10eb4fab..7416d3b8 100644 --- a/services/sensor/include/sensor_power_policy.h +++ b/services/sensor/include/sensor_power_policy.h @@ -35,8 +35,8 @@ class SensorPowerPolicy : public Singleton { public: ErrCode SuspendSensors(int32_t pid); ErrCode ResumeSensors(int32_t pid); - void GetActiveInfoList(int32_t pid, std::vector &activeInfoList); - void ReportActiveInfo(ActiveInfo activeInfo, const std::vector &sessionList); + std::vector GetActiveInfoList(int32_t pid); + void ReportActiveInfo(const ActiveInfo &activeInfo, const std::vector &sessionList); private: bool CheckFreezingSensor(int32_t sensorId); diff --git a/services/sensor/include/stream_server.h b/services/sensor/include/stream_server.h index 03e1ba7d..ee32c2da 100644 --- a/services/sensor/include/stream_server.h +++ b/services/sensor/include/stream_server.h @@ -28,8 +28,8 @@ class StreamServer : public StreamSocket { public: StreamServer() = default; virtual ~StreamServer(); - bool SendMsg(int32_t fd, NetPacket& pkt); - void Multicast(const std::vector& fdList, NetPacket& pkt); + bool SendMsg(int32_t fd, const NetPacket& pkt); + void Multicast(const std::vector& fdList, const NetPacket& pkt); int32_t GetClientFd(int32_t pid); int32_t GetClientPid(int32_t fd); SessionPtr GetSession(int32_t fd); diff --git a/services/sensor/src/client_info.cpp b/services/sensor/src/client_info.cpp index 604d80f6..495848bc 100644 --- a/services/sensor/src/client_info.cpp +++ b/services/sensor/src/client_info.cpp @@ -716,9 +716,13 @@ int32_t ClientInfo::DelActiveInfoCBPid(int32_t pid) return ERR_OK; } -std::unordered_set ClientInfo::GetActiveInfoCBPid() +std::vector ClientInfo::GetActiveInfoCBPid() { - return activeInfoCBPidSet_; + std::vector activeInfoCBPids; + for (auto it = activeInfoCBPidSet_.begin(); it != activeInfoCBPidSet_.end(); ++it) { + activeInfoCBPids.push_back(*it); + } + return activeInfoCBPids; } bool ClientInfo::IsUnregisterClientDeathRecipient(int32_t pid) @@ -726,15 +730,15 @@ bool ClientInfo::IsUnregisterClientDeathRecipient(int32_t pid) std::lock_guard channelLock(channelMutex_); auto channelIt = channelMap_.find(pid); if (channelIt != channelMap_.end()) { - SEN_HILOGD("Pid exist in channelMap"); return false; } + SEN_HILOGD("Pid is not exists in channelMap"); std::lock_guard activeInfoCBPidLock(activeInfoCBPidMutex_); auto pidIt = activeInfoCBPidSet_.find(pid); if (pidIt != activeInfoCBPidSet_.end()) { - SEN_HILOGD("Pid exist in activeInfoCBPidSet"); return false; } + SEN_HILOGD("Pid is not exists in activeInfoCBPidSet"); return true; } diff --git a/services/sensor/src/sensor_power_policy.cpp b/services/sensor/src/sensor_power_policy.cpp index ebdb6901..b466037f 100644 --- a/services/sensor/src/sensor_power_policy.cpp +++ b/services/sensor/src/sensor_power_policy.cpp @@ -173,9 +173,10 @@ ErrCode SensorPowerPolicy::RestoreSensorInfo(int32_t pid, int32_t sensorId, int6 return ERR_OK; } -void SensorPowerPolicy::GetActiveInfoList(int32_t pid, std::vector &activeInfoList) +std::vector SensorPowerPolicy::GetActiveInfoList(int32_t pid) { CALL_LOG_ENTER; + std::vector activeInfoList; std::vector sensorIdList = clientInfo_.GetSensorIdByPid(pid); for (auto &sensorId : sensorIdList) { auto sensorInfo = clientInfo_.GetCurPidSensorInfo(sensorId, pid); @@ -183,9 +184,10 @@ void SensorPowerPolicy::GetActiveInfoList(int32_t pid, std::vector & sensorInfo.GetMaxReportDelayNs()); activeInfoList.push_back(activeInfo); } + return activeInfoList; } -void SensorPowerPolicy::ReportActiveInfo(ActiveInfo activeInfo, +void SensorPowerPolicy::ReportActiveInfo(const ActiveInfo &activeInfo, const std::vector &sessionList) { CALL_LOG_ENTER; diff --git a/utils/ipc/include/stream_session.h b/utils/ipc/include/stream_session.h index 46f5d430..55a991e9 100644 --- a/utils/ipc/include/stream_session.h +++ b/utils/ipc/include/stream_session.h @@ -39,7 +39,7 @@ public: StreamSession(const std::string &programName, const int32_t fd, const int32_t uid, const int32_t pid); ~StreamSession() = default; bool SendMsg(const char *buf, size_t size) const; - bool SendMsg(NetPacket &pkt) const; + bool SendMsg(const NetPacket &pkt) const; void Close(); int32_t GetUid() const; int32_t GetPid() const; diff --git a/utils/ipc/src/stream_session.cpp b/utils/ipc/src/stream_session.cpp index 10abaa09..a53fd9cf 100644 --- a/utils/ipc/src/stream_session.cpp +++ b/utils/ipc/src/stream_session.cpp @@ -104,7 +104,7 @@ void StreamSession::UpdateDescript() descript_ = oss.str().c_str(); } -bool StreamSession::SendMsg(NetPacket &pkt) const +bool StreamSession::SendMsg(const NetPacket &pkt) const { if (pkt.ChkRWError()) { SEN_HILOGE("Read and write status failed"); -- Gitee