Modify the OH and OS differences

Signed-off-by: li-yaoyao777 <liyaoyao7@huawei.com>
This commit is contained in:
li-yaoyao777 2024-11-13 16:00:31 +08:00
parent 4056d00d49
commit e122155777
7 changed files with 38 additions and 23 deletions

View File

@ -118,11 +118,13 @@ public:
if (asyncWork != nullptr) {
SEN_HILOGD("Delete async work");
napi_delete_async_work(env, asyncWork);
asyncWork = nullptr;
}
for (int32_t i = 0; i < CALLBACK_NUM; ++i) {
if (callback[i] != nullptr) {
SEN_HILOGD("Delete reference, i:%{public}d", i);
napi_delete_reference(env, callback[i]);
callback[i] = nullptr;
}
}
}

View File

@ -31,6 +31,7 @@ namespace Sensors {
namespace {
constexpr int32_t STRING_LENGTH_MAX = 64;
} // namespace
static std::mutex g_sensorAttrListMutex;
bool IsSameValue(const napi_env &env, const napi_value &lhs, const napi_value &rhs)
{
CALL_LOG_ENTER;
@ -321,6 +322,7 @@ bool ConvertToSensorData(const napi_env &env, sptr<AsyncCallbackInfo> asyncCallb
{
CHKPF(asyncCallbackInfo);
int32_t sensorTypeId = asyncCallbackInfo->data.sensorData.sensorTypeId;
std::lock_guard<std::mutex> sensorAttrListMutex(g_sensorAttrListMutex);
CHKNCF(env, (g_sensorAttributeList.find(sensorTypeId) != g_sensorAttributeList.end()), "Invalid sensor type");
if (sensorTypeId == SENSOR_TYPE_ID_WEAR_DETECTION && asyncCallbackInfo->type == SUBSCRIBE_CALLBACK) {
return ConvertToBodyData(env, asyncCallbackInfo, result);
@ -639,5 +641,5 @@ bool GetSelfTargetVersion(uint32_t &targetVersion)
targetVersion = bundleInfo.targetVersion;
return true;
}
} // namespace Sensors
} // namespace OHOS
} // namespace Sensors
} // namespace OHOS

View File

@ -58,6 +58,7 @@ private:
int32_t ConvertSensorInfos() const;
void ClearSensorInfos() const;
std::set<RecordSensorCallback> GetSubscribeUserCallback(int32_t sensorId);
bool IsSubscribeMapEmpty() const;
static std::recursive_mutex subscribeMutex_;
static std::mutex chanelMutex_;
OHOS::sptr<OHOS::Sensors::SensorDataChannel> dataChannel_ = nullptr;
@ -71,4 +72,4 @@ private:
#define SENSOR_AGENT_IMPL OHOS::DelayedSingleton<SensorAgentProxy>::GetInstance()
} // namespace Sensors
} // namespace OHOS
#endif // endif SENSOR_PROXY_H
#endif // endif SENSOR_PROXY_H

View File

@ -263,6 +263,12 @@ int32_t SensorAgentProxy::SubscribeSensor(int32_t sensorId, const SensorUser *us
return OHOS::Sensors::SUCCESS;
}
bool SensorAgentProxy::IsSubscribeMapEmpty() const
{
std::lock_guard<std::recursive_mutex> subscribeLock(subscribeMutex_);
return subscribeMap_.empty();
}
int32_t SensorAgentProxy::UnsubscribeSensor(int32_t sensorId, const SensorUser *user)
{
SEN_HILOGI("In, sensorId:%{public}d", sensorId);
@ -272,27 +278,29 @@ int32_t SensorAgentProxy::UnsubscribeSensor(int32_t sensorId, const SensorUser *
SEN_HILOGE("sensorId is invalid, %{public}d", sensorId);
return PARAMETER_ERROR;
}
std::lock_guard<std::recursive_mutex> subscribeLock(subscribeMutex_);
if (unsubscribeMap_.find(sensorId) == unsubscribeMap_.end()) {
SEN_HILOGE("Deactivate sensorId first");
return OHOS::Sensors::ERROR;
{
std::lock_guard<std::recursive_mutex> subscribeLock(subscribeMutex_);
if (unsubscribeMap_.find(sensorId) == unsubscribeMap_.end()) {
SEN_HILOGE("Deactivate sensorId first");
return OHOS::Sensors::ERROR;
}
auto& unsubscribeSet = unsubscribeMap_[sensorId];
if (unsubscribeSet.find(user) == unsubscribeSet.end()) {
SEN_HILOGE("Deactivate user first");
return OHOS::Sensors::ERROR;
}
unsubscribeSet.erase(user);
if (unsubscribeSet.empty()) {
unsubscribeMap_.erase(sensorId);
}
}
auto& unsubscribeSet = unsubscribeMap_[sensorId];
if (unsubscribeSet.find(user) == unsubscribeSet.end()) {
SEN_HILOGE("Deactivate user first");
return OHOS::Sensors::ERROR;
}
if (subscribeMap_.empty()) {
if (IsSubscribeMapEmpty()) {
int32_t ret = DestroySensorDataChannel();
if (ret != ERR_OK) {
SEN_HILOGE("Destroy data channel fail, ret:%{public}d", ret);
return ret;
}
}
unsubscribeSet.erase(user);
if (unsubscribeSet.empty()) {
unsubscribeMap_.erase(sensorId);
}
if (PrintSensorData::GetInstance().IsContinuousType(sensorId)) {
PrintSensorData::GetInstance().RemovePrintUserInfo(user->callback);
}

View File

@ -223,6 +223,7 @@ void SensorServiceClient::ReenableSensor()
CALL_LOG_ENTER;
std::lock_guard<std::mutex> mapLock(mapMutex_);
for (const auto &it : sensorInfoMap_) {
std::lock_guard<std::mutex> clientLock_(clientMutex_);
if (sensorServer_ != nullptr) {
sensorServer_->EnableSensor(it.first, it.second.GetSamplingPeriodNs(), it.second.GetMaxReportDelayNs());
}
@ -264,6 +265,7 @@ void SensorServiceClient::ProcessDeathObserver(const wptr<IRemoteObject> &object
SENSOR_AGENT_IMPL->SetIsChannelCreated(false);
return;
}
std::lock_guard<std::mutex> clientLock_(clientMutex_);
if (sensorServer_ != nullptr && sensorClientStub_ != nullptr) {
auto remoteObject = sensorClientStub_->AsObject();
if (remoteObject != nullptr) {

View File

@ -35,7 +35,7 @@ SensorManager &sensorManager_ = SensorManager::GetInstance();
#ifdef HDF_DRIVERS_INTERFACE_SENSOR
SensorHdiConnection &sensorHdiConnection_ = SensorHdiConnection::GetInstance();
#endif // HDF_DRIVERS_INTERFACE_SENSOR
} // namespace
} // namespace
bool SensorPowerPolicy::CheckFreezingSensor(int32_t sensorId)
{
@ -47,7 +47,7 @@ ErrCode SensorPowerPolicy::SuspendSensors(int32_t pid)
CALL_LOG_ENTER;
std::vector<int32_t> sensorIdList = clientInfo_.GetSensorIdByPid(pid);
if (sensorIdList.empty()) {
SEN_HILOGI("Suspend sensors failed, sensorIdList is empty, pid:%{public}d", pid);
SEN_HILOGD("Suspend sensors failed, sensorIdList is empty, pid:%{public}d", pid);
return SUSPEND_ERR;
}
std::lock_guard<std::mutex> pidSensorInfoLock(pidSensorInfoMutex_);
@ -107,7 +107,7 @@ ErrCode SensorPowerPolicy::ResumeSensors(int32_t pid)
std::lock_guard<std::mutex> pidSensorInfoLock(pidSensorInfoMutex_);
auto pidSensorInfoIt = pidSensorInfoMap_.find(pid);
if (pidSensorInfoIt == pidSensorInfoMap_.end()) {
SEN_HILOGI("Resume sensors failed, please suspend sensors first, pid:%{public}d", pid);
SEN_HILOGD("Resume sensors failed, please suspend sensors first, pid:%{public}d", pid);
return RESUME_ERR;
}
bool isAllResume = true;
@ -261,5 +261,5 @@ void SensorPowerPolicy::ReportActiveInfo(const ActiveInfo &activeInfo,
}
}
}
} // namespace Sensors
} // namespace OHOS
} // namespace Sensors
} // namespace OHOS

View File

@ -104,4 +104,4 @@ public:
};
} // namespace Sensors
} // namespace OHOS
#endif
#endif // AUDIO_UTILS_H