diff --git a/services/sensor/BUILD.gn b/services/sensor/BUILD.gn index eb5742762f200b7aac577ae69afe0c563730464c..6356df681f1eb7dabecb32703497d347a0552420 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 7356e6e32a2903471656634f46130a5f9076db6b..bf89eaaf6c3029eb01701bfb5579a4e957fde069 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::vector 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 0000000000000000000000000000000000000000..7416d3b88c8f442a88bd9967d0a7e7414b4fecd7 --- /dev/null +++ b/services/sensor/include/sensor_power_policy.h @@ -0,0 +1,52 @@ +/* + * 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); + std::vector GetActiveInfoList(int32_t pid); + void ReportActiveInfo(const ActiveInfo &activeInfo, const std::vector &sessionList); + +private: + bool CheckFreezingSensor(int32_t sensorId); + 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); + 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/sensor_service_stub.h b/services/sensor/include/sensor_service_stub.h index 9259e793f11101593e9d19465052a4a08266169e..60b4ba49f772ac7a28f8d6d7d213c0a25c023282 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 diff --git a/services/sensor/include/stream_server.h b/services/sensor/include/stream_server.h new file mode 100644 index 0000000000000000000000000000000000000000..ee32c2da26bacdeea586e935573d8b4fcc63a881 --- /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, 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); + 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 dcb9577ea0eade99ea94f02e873397b696237303..495848bc1ccd28d86ddb52e337899dbaabc51c9c 100644 --- a/services/sensor/src/client_info.cpp +++ b/services/sensor/src/client_info.cpp @@ -693,6 +693,56 @@ 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("Pid is duplicated"); + 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("Pid is not exists"); + return ERROR; + } + activeInfoCBPidSet_.erase(it); + return ERR_OK; +} + +std::vector ClientInfo::GetActiveInfoCBPid() +{ + std::vector activeInfoCBPids; + for (auto it = activeInfoCBPidSet_.begin(); it != activeInfoCBPidSet_.end(); ++it) { + activeInfoCBPids.push_back(*it); + } + return activeInfoCBPids; +} + +bool ClientInfo::IsUnregisterClientDeathRecipient(int32_t pid) +{ + std::lock_guard channelLock(channelMutex_); + auto channelIt = channelMap_.find(pid); + if (channelIt != channelMap_.end()) { + return false; + } + SEN_HILOGD("Pid is not exists in channelMap"); + std::lock_guard activeInfoCBPidLock(activeInfoCBPidMutex_); + auto pidIt = activeInfoCBPidSet_.find(pid); + if (pidIt != activeInfoCBPidSet_.end()) { + return false; + } + SEN_HILOGD("Pid is not exists in activeInfoCBPidSet"); + 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 0000000000000000000000000000000000000000..b466037fb81e9bc44b2fc2c0c50b87f7d44570bd --- /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; +ClientInfo &clientInfo_ = ClientInfo::GetInstance(); +SensorManager &sensorManager_ = SensorManager::GetInstance(); +SensorHdiConnection &sensorHdiConnection_ = SensorHdiConnection::GetInstance(); +} // 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); + 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()) { + 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("Suspend last failed sensors, but some failed"); + return DISABLE_SENSOR_ERR; + } + return ERR_OK; + } + std::unordered_map sensorInfoMap; + auto isAllSuspend = Suspend(pid, sensorIdList, sensorInfoMap); + pidSensorInfoMap_.insert(std::make_pair(pid, sensorInfoMap)); + if (!isAllSuspend) { + SEN_HILOGE("Suspend all sensors, but some failed"); + return DISABLE_SENSOR_ERR; + } + return ERR_OK; +} + +bool SensorPowerPolicy::Suspend(int32_t pid, std::vector &sensorIdList, + std::unordered_map &sensorInfoMap) +{ + CALL_LOG_ENTER; + bool isAllSuspend = true; + for (auto &sensorId : sensorIdList) { + if (CheckFreezingSensor(sensorId)) { + SEN_HILOGD("Current sensor is pedometer detection 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 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); + 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; +} + +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); + ActiveInfo activeInfo(pid, sensorId, sensorInfo.GetSamplingPeriodNs(), + sensorInfo.GetMaxReportDelayNs()); + activeInfoList.push_back(activeInfo); + } + return activeInfoList; +} + +void SensorPowerPolicy::ReportActiveInfo(const 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 82ac98324343a998174c455212e19b7ea2382bbf..0000000000000000000000000000000000000000 --- 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 diff --git a/utils/ipc/include/stream_session.h b/utils/ipc/include/stream_session.h index 46f5d4300ab4dc1b8f2220854400c9696abffc35..55a991e9fe86923c4366fb743bf863020add45fb 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 10abaa09fb2f848be35c717da7f0236ab36261f0..a53fd9cf79a561d9fbe4530d2edde37227477f3f 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"); diff --git a/utils/ipc/src/stream_socket.cpp b/utils/ipc/src/stream_socket.cpp index d2ca0c9b86a022290706dc8ce8a51a178d4e362d..9dd64d540a5f11252d2f7119001b5f464d3f4c5d 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); } }