diff --git a/frameworks/js/napi/src/sensor_js.cpp b/frameworks/js/napi/src/sensor_js.cpp index e54b78842c4738b48adf6e77bdf4521189344b3f..c34466769a915456f25a34284aea75c07bc0325d 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 6ebf660a65aefb78b1db0d1609ec16d67c08b206..e89c504df8771d6e407449f948fc27a6323706aa 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 2e4c761106557e4f949ee9eb2e0bc2a12c082fcc..bb4efab2d5e79096e0f3d6f50eecba89993138d0 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 881208df354e456153b97f77c63703c706af78fa..b5b6796886d920037a66df0a421c357c8f3c1705 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 6d98e4666df7ef2f825e1ea4fc28f333f1b6f281..b98ffb93ac0b2ffd8eb9a3f485e412b2528ec75d 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 ed978d2a9c8ba74e41c9a954bea00312cd036613..896c5482b0a80625f96785ff4287f64754991398 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 3cf5ad38aca451ba48c2ae006caf2611eea667ed..0c9ebd77186ac9e5fe222d84c8edcd11fb299ce0 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;