From 7019f1f2bf194756dcc0bbfc187dcd60da7a0479 Mon Sep 17 00:00:00 2001 From: lixiangpeng5 Date: Thu, 5 Jun 2025 21:13:44 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=9A=E4=BC=A0=E6=84=9F=E5=99=A8=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lixiangpeng5 --- frameworks/js/napi/src/sensor_js.cpp | 28 ++++++++++++------- frameworks/native/src/sensor_agent_proxy.cpp | 4 +-- .../native/src/sensor_service_client.cpp | 3 +- .../interface/include/sensor_hdi_connection.h | 1 + .../interface/src/sensor_hdi_connection.cpp | 23 +++++++++++---- services/src/sensor_dump.cpp | 22 ++++++++------- services/src/sensor_service.cpp | 8 ++++-- 7 files changed, 56 insertions(+), 33 deletions(-) diff --git a/frameworks/js/napi/src/sensor_js.cpp b/frameworks/js/napi/src/sensor_js.cpp index e54b7884..c3446676 100644 --- a/frameworks/js/napi/src/sensor_js.cpp +++ b/frameworks/js/napi/src/sensor_js.cpp @@ -1439,6 +1439,10 @@ static napi_value GetSingleSensor(napi_env env, napi_callback_info info) ThrowErr(env, PARAMETER_ERROR, "Wrong argument type, get number fail"); return nullptr; } + int32_t deviceId = DEFAULT_DEVICE_ID; + if (!GetLocationDeviceId(deviceId)) { + SEN_HILOGW("Cant fand local deviceId, default deviceId :%{public}d", deviceId); + } sptr asyncCallbackInfo = new (std::nothrow) AsyncCallbackInfo(env, GET_SINGLE_SENSOR); CHKPP(asyncCallbackInfo); SensorInfo *sensorInfos = nullptr; @@ -1456,7 +1460,7 @@ static napi_value GetSingleSensor(napi_env env, napi_callback_info info) SEN_HILOGD("This sensor is secondary ambient light"); continue; } - if (sensorInfos[i].sensorTypeId == sensorTypeId) { + if (sensorInfos[i].deviceId == deviceId && sensorInfos[i].sensorTypeId == sensorTypeId) { asyncCallbackInfo->sensorInfos.push_back(*(sensorInfos + i)); break; } @@ -1495,6 +1499,10 @@ static napi_value GetSingleSensorSync(napi_env env, napi_callback_info info) ThrowErr(env, PARAMETER_ERROR, "Wrong argument type, get number fail"); return result; } + int32_t deviceId = DEFAULT_DEVICE_ID; + if (!GetLocationDeviceId(deviceId)) { + SEN_HILOGW("Cant fand local deviceId, default deviceId :%{public}d", deviceId); + } SensorInfo *sensorInfos = nullptr; int32_t count = 0; int32_t ret = GetAllSensors(&sensorInfos, &count); @@ -1510,7 +1518,7 @@ static napi_value GetSingleSensorSync(napi_env env, napi_callback_info info) SEN_HILOGD("This sensor is secondary ambient light"); continue; } - if (sensorInfos[i].sensorTypeId == sensorTypeId) { + if (sensorInfos[i].deviceId == deviceId && sensorInfos[i].sensorTypeId == sensorTypeId) { sensorInfoVec.push_back(*(sensorInfos + i)); break; } @@ -1546,6 +1554,10 @@ static napi_value GetSensorListByDeviceSync(napi_env env, napi_callback_info inf napi_value args[1] = { 0 }; napi_value result = nullptr; napi_get_undefined(env, &result); + if (napi_create_array(env, &result) != napi_ok) { + SEN_HILOGE("napi_create_array fail"); + return result; + } napi_value thisVar = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr); if (status != napi_ok) { @@ -1568,10 +1580,6 @@ static napi_value GetSensorListByDeviceSync(napi_env env, napi_callback_info inf } vector sensorInfoVec; FilteringSensorList(sensorInfos, sensorInfoVec, count); - if (napi_create_array(env, &result) != napi_ok) { - SEN_HILOGE("napi_create_array fail"); - return result; - } for (uint32_t i = 0; i < sensorInfoVec.size(); ++i) { napi_value value = nullptr; if (!ConvertToSensorInfo(env, sensorInfoVec[i], value)) { @@ -1609,6 +1617,10 @@ static napi_value GetSingleSensorByDeviceSync(napi_env env, napi_callback_info i napi_value args[2] = { 0 }; napi_value result = nullptr; napi_get_undefined(env, &result); + if (napi_create_array(env, &result) != napi_ok) { + SEN_HILOGE("napi_create_array fail"); + return result; + } napi_value thisVar = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr); if (status != napi_ok || argc < 1) { @@ -1636,10 +1648,6 @@ static napi_value GetSingleSensorByDeviceSync(napi_env env, napi_callback_info i } vector sensorInfoVec; FilteringSingleSensorList(sensorInfos, sensorInfoVec, count, sensorTypeId); - if (napi_create_array(env, &result) != napi_ok) { - SEN_HILOGE("napi_create_array fail"); - return result; - } for (uint32_t i = 0; i < sensorInfoVec.size(); ++i) { napi_value value = nullptr; if (!ConvertToSensorInfo(env, sensorInfoVec[i], value)) { diff --git a/frameworks/native/src/sensor_agent_proxy.cpp b/frameworks/native/src/sensor_agent_proxy.cpp index 6ebf660a..e89c504d 100644 --- a/frameworks/native/src/sensor_agent_proxy.cpp +++ b/frameworks/native/src/sensor_agent_proxy.cpp @@ -445,8 +445,8 @@ int32_t SensorAgentProxy::ConvertSensorInfos() const sensorInfo->power = sensorList[i].GetPower(); sensorInfo->minSamplePeriod = sensorList[i].GetMinSamplePeriodNs(); sensorInfo->maxSamplePeriod = sensorList[i].GetMaxSamplePeriodNs(); - SEN_HILOGI("deviceId %{public}d: sensorTypeId %{public}d, sensorId %{public}d", - sensorInfo->deviceId, sensorInfo->sensorTypeId, sensorInfo->sensorId); + SEN_HILOGI("deviceId %{public}d: sensorTypeId %{public}d, sensorIndex %{public}d", + sensorInfo->deviceId, sensorInfo->sensorTypeId, sensorInfo->sensorIndex); } sensorInfoCount_ = static_cast(count); return SUCCESS; diff --git a/frameworks/native/src/sensor_service_client.cpp b/frameworks/native/src/sensor_service_client.cpp index 2e4c7611..bb4efab2 100644 --- a/frameworks/native/src/sensor_service_client.cpp +++ b/frameworks/native/src/sensor_service_client.cpp @@ -211,8 +211,7 @@ int32_t SensorServiceClient::GetSensorListByDevice(int32_t deviceId, std::vector if (singleDevSensors.empty()) { singleDevSensors = GetSensorListByDevice(deviceId); if (singleDevSensors.empty()) { - SEN_HILOGE("GetSensorListByDevice failed, singleDevSensors cannot be empty"); - return SERVICE_EXCEPTION; + SEN_HILOGW("singleDevSensors is empty"); } } return ERR_OK; diff --git a/services/hdi_connection/interface/include/sensor_hdi_connection.h b/services/hdi_connection/interface/include/sensor_hdi_connection.h index 881208df..b5b67968 100644 --- a/services/hdi_connection/interface/include/sensor_hdi_connection.h +++ b/services/hdi_connection/interface/include/sensor_hdi_connection.h @@ -56,6 +56,7 @@ private: Sensor GenerateSarSensor(); Sensor GenerateHeadPostureSensor(); Sensor GenerateProximitySensor(); + void UpdataSensorList(std::vector &singleDevSensors); std::atomic_bool hdiConnectionStatus_ = false; }; } // namespace Sensors diff --git a/services/hdi_connection/interface/src/sensor_hdi_connection.cpp b/services/hdi_connection/interface/src/sensor_hdi_connection.cpp index 6d98e466..b98ffb93 100644 --- a/services/hdi_connection/interface/src/sensor_hdi_connection.cpp +++ b/services/hdi_connection/interface/src/sensor_hdi_connection.cpp @@ -443,14 +443,9 @@ int32_t SensorHdiConnection::DestroyHdiConnection() return ret; } -int32_t SensorHdiConnection::GetSensorListByDevice(int32_t deviceId, std::vector &singleDevSensors) +void SensorHdiConnection::UpdataSensorList(std::vector &singleDevSensors) { CALL_LOG_ENTER; - CHKPR(iSensorHdiConnection_, GET_SENSOR_LIST_ERR); - std::lock_guard sensorLock(sensorMutex_); - if (iSensorHdiConnection_->GetSensorListByDevice(deviceId, singleDevSensors) != ERR_OK) { - SEN_HILOGW("Get sensor list by device failed"); - } for (const auto& newSensor : singleDevSensors) { bool found = false; for (const auto& oldSensor : sensorList_) { @@ -466,6 +461,22 @@ int32_t SensorHdiConnection::GetSensorListByDevice(int32_t deviceId, std::vector sensorList_.push_back(newSensor); } } + return; +} + +int32_t SensorHdiConnection::GetSensorListByDevice(int32_t deviceId, std::vector &singleDevSensors) +{ + CALL_LOG_ENTER; + CHKPR(iSensorHdiConnection_, GET_SENSOR_LIST_ERR); + std::lock_guard sensorLock(sensorMutex_); + if (iSensorHdiConnection_->GetSensorListByDevice(deviceId, singleDevSensors) != ERR_OK) { + SEN_HILOGW("Get sensor list by device failed"); + } + if (singleDevSensors.empty()) { + SEN_HILOGW("Get sensor list is empty"); + return ERR_OK; + } + UpdataSensorList(singleDevSensors); #ifdef BUILD_VARIANT_ENG if (singleDevSensors[0].GetLocation() == IS_LOCAL_DEVICE) { if (!hdiConnectionStatus_) { diff --git a/services/src/sensor_dump.cpp b/services/src/sensor_dump.cpp index ed978d2a..896c5482 100644 --- a/services/src/sensor_dump.cpp +++ b/services/src/sensor_dump.cpp @@ -205,9 +205,10 @@ bool SensorDump::DumpSensorList(int32_t fd, const std::vector &sensors) continue; } dprintf(fd, - "deviceId:%d | sensorType:%s |sensorId:%8u | sensorName:%s | vendorName:%s | maxRange:%f" - "| fifoMaxEventCount:%d | minSamplePeriodNs:%" PRId64 " | maxSamplePeriodNs:%" PRId64 "\n", - deviceId, sensorMap_[sensorTypeId].c_str(), sensorId, sensor.GetSensorName().c_str(), + "deviceId:%d | sensorType:%s |sensorId:%8u |sensorIndex:%d | sensorName:%s | vendorName:%s" + "| maxRange:%f| fifoMaxEventCount:%d | minSamplePeriodNs:%" PRId64 "" + "| maxSamplePeriodNs:%" PRId64 "\n", + deviceId, sensorMap_[sensorTypeId].c_str(), sensorTypeId, sensorId, sensor.GetSensorName().c_str(), sensor.GetVendorName().c_str(), sensor.GetMaxRange(), sensor.GetFifoMaxEventCount(), sensor.GetMinSamplePeriodNs(), sensor.GetMaxSamplePeriodNs()); } @@ -228,10 +229,10 @@ bool SensorDump::DumpSensorChannel(int32_t fd, ClientInfo &clientInfo) continue; } dprintf(fd, - "uid:%d | packageName:%s | deviceId:%d | sensorType:%s |sensorId:%8u | samplingPeriodNs:%" PRId64 "" - "| fifoCount:%u\n", - channel.GetUid(), channel.GetPackageName().c_str(), deviceId, sensorMap_[sensorType].c_str(), sensorId, - channel.GetSamplingPeriodNs(), channel.GetFifoCount()); + "uid:%d | packageName:%s | deviceId:%d | sensorType:%s |sensorId:%8u |sensorIndex:%d " + "| samplingPeriodNs:%" PRId64 "| fifoCount:%u\n", + channel.GetUid(), channel.GetPackageName().c_str(), deviceId, sensorMap_[sensorType].c_str(), + sensorType, sensorId, channel.GetSamplingPeriodNs(), channel.GetFifoCount()); } return true; } @@ -247,9 +248,10 @@ bool SensorDump::DumpOpeningSensor(int32_t fd, const std::vector &sensor if (sensorMap_.find(sensorTypeId) == sensorMap_.end()) { continue; } - if (clientInfo.GetSensorState({sensor.GetDeviceId(), sensorTypeId, sensor.GetSensorId()})) { - dprintf(fd, "deviceId:%d | sensorType:%s |sensorId:%8u | channelSize: %lu\n", - deviceId, sensorMap_[sensorTypeId].c_str(), sensorId, clientInfo.GetSensorChannel({ + if (clientInfo.GetSensorState({sensor.GetDeviceId(), sensorTypeId, sensor.GetSensorId(), + sensor.GetLocation()})) { + dprintf(fd, "deviceId:%d | sensorType:%s |sensorId:%8u |sensorIndex:%d | channelSize: %lu\n", + deviceId, sensorMap_[sensorTypeId].c_str(), sensorTypeId, sensorId, clientInfo.GetSensorChannel({ sensor.GetDeviceId(), sensorTypeId, sensor.GetSensorId(), sensor.GetLocation()}).size()); } } diff --git a/services/src/sensor_service.cpp b/services/src/sensor_service.cpp index 3cf5ad38..0c9ebd77 100644 --- a/services/src/sensor_service.cpp +++ b/services/src/sensor_service.cpp @@ -580,8 +580,8 @@ std::vector SensorService::GetSensorListByDevice(int32_t deviceId) #ifdef HDF_DRIVERS_INTERFACE_SENSOR std::vector singleDevSensors; int32_t ret = sensorHdiConnection_.GetSensorListByDevice(deviceId, singleDevSensors); - if (ret != 0) { - SEN_HILOGE("GetSensorListByDevice is failed"); + if (ret != 0 || singleDevSensors.empty()) { + SEN_HILOGW("GetSensorListByDevice is failed or empty"); return sensors_; } for (const auto& newSensor : singleDevSensors) { @@ -611,7 +611,9 @@ std::vector SensorService::GetSensorListByDevice(int32_t deviceId) } else { sensorMap_.insert(std::pair( {it.GetDeviceId(), it.GetSensorTypeId(), it.GetSensorId(), it.GetLocation()}, it)); - sensorDataProcesser_->UpdataSensorMap(sensorMap_); + if (sensorDataProcesser_ != nullptr) { + sensorDataProcesser_->UpdataSensorMap(sensorMap_); + } } } return singleDevSensors; -- Gitee