diff --git a/frameworks/native/sensor/include/sensor_service_client.h b/frameworks/native/sensor/include/sensor_service_client.h index 2a2ac2901daa1474f1df6fc0a5d6b37d0edafe27..304f66373cc845549c8b85535a9b949a2911fb36 100755 --- a/frameworks/native/sensor/include/sensor_service_client.h +++ b/frameworks/native/sensor/include/sensor_service_client.h @@ -44,6 +44,7 @@ private: int32_t InitServiceClient(); void UpdateSensorInfoMap(uint32_t sensorId, int64_t samplingPeroid, int64_t maxReportDelay); void DeleteSensorInfoItem(uint32_t sensorId); + bool IsValidSensorId(uint32_t sensorId); std::mutex clientMutex_; sptr serviceDeathObserver_; sptr sensorServer_; diff --git a/frameworks/native/sensor/src/sensor_agent_proxy.cpp b/frameworks/native/sensor/src/sensor_agent_proxy.cpp index 5bfb89dc715973a1425939458c1e841836443644..09adc437d9171827425a0b5ba689f242cac5bbb3 100755 --- a/frameworks/native/sensor/src/sensor_agent_proxy.cpp +++ b/frameworks/native/sensor/src/sensor_agent_proxy.cpp @@ -203,22 +203,18 @@ int32_t SensorAgentProxy::DeactivateSensor(int32_t sensorId, const SensorUser *u HiLog::Error(LABEL, "%{public}s user is null or sensorId is invalid", __func__); return OHOS::Sensors::ERROR; } - if (g_subscribeMap.find(sensorId) != g_subscribeMap.end()) { - HiLog::Error(LABEL, "%{public}s unsubscribe sensorId first", __func__); - return OHOS::Sensors::ERROR; - } - - if (g_unsubscribeMap.find(sensorId) == g_unsubscribeMap.end() || g_unsubscribeMap[sensorId] != user) { - HiLog::Error(LABEL, "%{public}s unsubscribe sensorId first", __func__); + if ((g_subscribeMap.find(sensorId) == g_subscribeMap.end()) || (g_subscribeMap[sensorId] != user)) { + HiLog::Error(LABEL, "%{public}s subscribe sensorId first", __func__); return OHOS::Sensors::ERROR; } + g_subscribeMap.erase(sensorId); + g_unsubscribeMap[sensorId] = user; SensorServiceClient &client = SensorServiceClient::GetInstance(); int32_t ret = client.DisableSensor(sensorId); if (ret != 0) { HiLog::Error(LABEL, "%{public}s disable sensor failed, ret: %{public}d", __func__, ret); return OHOS::Sensors::ERROR; } - g_unsubscribeMap.erase(sensorId); return OHOS::Sensors::SUCCESS; } @@ -265,12 +261,13 @@ int32_t SensorAgentProxy::UnsubscribeSensor(int32_t sensorId, const SensorUser * HiLog::Error(LABEL, "%{public}s user is null or sensorId is invalid", __func__); return OHOS::Sensors::ERROR; } - if ((g_subscribeMap.find(sensorId) == g_subscribeMap.end()) || (g_subscribeMap.at(sensorId) != user)) { - HiLog::Error(LABEL, "%{public}s subscribe sensorId first", __func__); + + if (g_unsubscribeMap.find(sensorId) == g_unsubscribeMap.end() || g_unsubscribeMap[sensorId] != user) { + HiLog::Error(LABEL, "%{public}s deactivate sensorId first", __func__); return OHOS::Sensors::ERROR; } - g_subscribeMap.erase(sensorId); - g_unsubscribeMap[sensorId] = user; + + g_unsubscribeMap.erase(sensorId); if (g_subscribeMap.empty()) { DestroySensorDataChannel(); g_isChannelCreated = false; diff --git a/frameworks/native/sensor/src/sensor_data_channel.cpp b/frameworks/native/sensor/src/sensor_data_channel.cpp index 38287de6567cfa0364edcc10b6d7186be72f7922..ca97239207bf35daf8f727454b1d25d7212939b6 100755 --- a/frameworks/native/sensor/src/sensor_data_channel.cpp +++ b/frameworks/native/sensor/src/sensor_data_channel.cpp @@ -142,7 +142,11 @@ int32_t SensorDataChannel::DestroySensorDataChannel() } int32_t fd = GetReceiveDataFd(); eventHandler_->RemoveFileDescriptorListener(fd); - + eventHandler_ = nullptr; + if (eventRunner_ != nullptr) { + eventRunner_->Stop(); + eventRunner_ = nullptr; + } threadStop_ = true; if (sensorDataThread_.joinable()) { diff --git a/frameworks/native/sensor/src/sensor_service_client.cpp b/frameworks/native/sensor/src/sensor_service_client.cpp index 9460cba8caf24f3fdeb0196705d1514bc467c4ad..68293575fbad9a2ccac9a1bbccf62cda82ae5298 100755 --- a/frameworks/native/sensor/src/sensor_service_client.cpp +++ b/frameworks/native/sensor/src/sensor_service_client.cpp @@ -75,9 +75,27 @@ int32_t SensorServiceClient::InitServiceClient() return SENSOR_NATIVE_GET_SERVICE_ERR; } +bool SensorServiceClient::IsValidSensorId(uint32_t sensorId) +{ + if (sensorList_.empty()) { + HiLog::Error(LABEL, "%{public}s sensorList_ cannot be empty", __func__); + return false; + } + for (auto &sensor : sensorList_) { + if (sensor.GetSensorId() == sensorId) { + return true; + } + } + return false; +} + int32_t SensorServiceClient::EnableSensor(uint32_t sensorId, int64_t samplingPeriod, int64_t maxReportDelay) { HiLog::Debug(LABEL, "%{public}s begin", __func__); + if (!IsValidSensorId(sensorId)) { + HiLog::Error(LABEL, "%{public}s sensorId is invalid", __func__); + return SENSOR_NATIVE_SAM_ERR; + } int32_t ret = InitServiceClient(); if (ret != ERR_OK) { HiLog::Error(LABEL, "%{public}s InitServiceClient failed, ret : %{public}d", __func__, ret); @@ -93,6 +111,10 @@ int32_t SensorServiceClient::EnableSensor(uint32_t sensorId, int64_t samplingPer int32_t SensorServiceClient::DisableSensor(uint32_t sensorId) { HiLog::Debug(LABEL, "%{public}s begin", __func__); + if (!IsValidSensorId(sensorId)) { + HiLog::Error(LABEL, "%{public}s sensorId is invalid", __func__); + return SENSOR_NATIVE_SAM_ERR; + } int32_t ret = InitServiceClient(); if (ret != ERR_OK) { HiLog::Error(LABEL, "%{public}s InitServiceClient failed, ret : %{public}d", __func__, ret); @@ -108,6 +130,10 @@ int32_t SensorServiceClient::DisableSensor(uint32_t sensorId) int32_t SensorServiceClient::RunCommand(uint32_t sensorId, int32_t cmdType, int32_t params) { HiLog::Debug(LABEL, "%{public}s begin", __func__); + if (!IsValidSensorId(sensorId)) { + HiLog::Error(LABEL, "%{public}s sensorId is invalid", __func__); + return SENSOR_NATIVE_SAM_ERR; + } int32_t ret = InitServiceClient(); if (ret != ERR_OK) { HiLog::Error(LABEL, "%{public}s InitServiceClient failed, ret : %{public}d", __func__, ret); diff --git a/interfaces/plugin/src/sensor_js.cpp b/interfaces/plugin/src/sensor_js.cpp index ae12480cb94651b6a7e8b24ca15d9adb7f3dd88c..0e51f8aa24eba5c599aafbed7c15593c0b91c531 100755 --- a/interfaces/plugin/src/sensor_js.cpp +++ b/interfaces/plugin/src/sensor_js.cpp @@ -87,14 +87,14 @@ static const SensorUser user = { static int32_t UnsubscribeSensor(int32_t sensorTypeId) { HiLog::Info(LABEL, "%{public}s in", __func__); - int32_t ret = UnsubscribeSensor(sensorTypeId, &user); + int32_t ret = DeactivateSensor(sensorTypeId, &user); if (ret < 0) { - HiLog::Error(LABEL, "%{public}s UnsubscribeSensor failed", __func__); + HiLog::Error(LABEL, "%{public}s DeactivateSensor failed", __func__); return ret; } - ret = DeactivateSensor(sensorTypeId, &user); + ret = UnsubscribeSensor(sensorTypeId, &user); if (ret < 0) { - HiLog::Error(LABEL, "%{public}s DeactivateSensor failed", __func__); + HiLog::Error(LABEL, "%{public}s UnsubscribeSensor failed", __func__); return ret; } HiLog::Info(LABEL, "%{public}s left", __func__); diff --git a/services/sensor/src/sensor_service.cpp b/services/sensor/src/sensor_service.cpp index a715761c7f0d56447dc20e55e603e111ba5a47e1..f33cb447c5f1333b71e28e24904a7ddd40c57ec9 100755 --- a/services/sensor/src/sensor_service.cpp +++ b/services/sensor/src/sensor_service.cpp @@ -258,6 +258,7 @@ ErrCode SensorService::EnableSensor(uint32_t sensorId, int64_t samplingPeriodNs, auto ret = SaveSubscriber(sensorId, samplingPeriodNs, maxReportDelayNs); if (ret != ERR_OK) { HiLog::Error(LABEL, "%{public}s SaveSubscriber failed", __func__); + clientInfo_.RemoveSubscriber(sensorId, this->GetCallingPid()); return ret; } @@ -285,6 +286,10 @@ ErrCode SensorService::DisableSensor(uint32_t sensorId) HiLog::Error(LABEL, "%{public}s clientPid is invalid, clientPid : %{public}d", __func__, clientPid); return CLIENT_PID_INVALID_ERR; } + if (clientInfo_.GetSensorState(sensorId) != SENSOR_ENABLED) { + HiLog::Error(LABEL, "%{public}s sensor should be enabled first", __func__); + return DISABLE_SENSOR_ERR; + } if (sensorManager_.IsOtherClientUsingSensor(sensorId, clientPid)) { HiLog::Warn(LABEL, "%{public}s other client is using this sensor now, cannot disable", __func__); return ERR_OK;