From 85e8fe4fcb445cc2f9bc4e464f248902ecb8988a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E9=9C=B2?= Date: Wed, 23 Feb 2022 11:26:18 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E5=BC=BA=E8=BD=AC?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: bailu1992 --- .../native/sensor/src/sensor_agent_proxy.cpp | 4 +-- interfaces/native/src/geomagnetic_field.cpp | 2 +- interfaces/native/src/sensor_algorithm.cpp | 36 ++++++++++--------- interfaces/plugin/include/sensor_napi_utils.h | 2 +- interfaces/plugin/src/sensor_js.cpp | 2 +- interfaces/plugin/src/sensor_napi_utils.cpp | 3 +- services/sensor/src/sensor_data_processer.cpp | 34 +++++++++--------- 7 files changed, 43 insertions(+), 40 deletions(-) diff --git a/frameworks/native/sensor/src/sensor_agent_proxy.cpp b/frameworks/native/sensor/src/sensor_agent_proxy.cpp index ce29e2f1..54dca75b 100755 --- a/frameworks/native/sensor/src/sensor_agent_proxy.cpp +++ b/frameworks/native/sensor/src/sensor_agent_proxy.cpp @@ -349,8 +349,8 @@ int32_t SensorAgentProxy::GetAllSensors(SensorInfo **sensorInfo, int32_t *count) HiLog::Error(LABEL, "%{public}s strcpy hardwareVersion failed", __func__); return OHOS::Sensors::ERROR; } - (*sensorInfo + index)->sensorId = sensorList_[index].GetSensorId(); - (*sensorInfo + index)->sensorTypeId = sensorList_[index].GetSensorId(); + (*sensorInfo + index)->sensorId = static_cast(sensorList_[index].GetSensorId()); + (*sensorInfo + index)->sensorTypeId = static_cast(sensorList_[index].GetSensorId()); (*sensorInfo + index)->maxRange = sensorList_[index].GetMaxRange(); (*sensorInfo + index)->precision = sensorList_[index].GetResolution(); (*sensorInfo + index)->power = 0.0f; diff --git a/interfaces/native/src/geomagnetic_field.cpp b/interfaces/native/src/geomagnetic_field.cpp index 3de60330..05b2b834 100755 --- a/interfaces/native/src/geomagnetic_field.cpp +++ b/interfaces/native/src/geomagnetic_field.cpp @@ -195,7 +195,7 @@ void GeomagneticField::getRelativeRadiusPower() HiLog::Info(LABEL, "%{public}s begin", __func__); relativeRadiusPower[0] = 1.0f; relativeRadiusPower[1] = EARTH_REFERENCE_RADIUS / geocentricRadius; - for (int32_t index = 2; index < relativeRadiusPower.size(); ++index) { + for (int32_t index = 2; index < static_cast(relativeRadiusPower.size()); ++index) { relativeRadiusPower[index] = relativeRadiusPower[index - 1] * relativeRadiusPower[1]; } } diff --git a/interfaces/native/src/sensor_algorithm.cpp b/interfaces/native/src/sensor_algorithm.cpp index 34c55852..b38a51b4 100644 --- a/interfaces/native/src/sensor_algorithm.cpp +++ b/interfaces/native/src/sensor_algorithm.cpp @@ -26,15 +26,16 @@ static constexpr HiLogLabel LABEL = {LOG_CORE, OHOS::SensorsLogDomain::SENSORS_I int32_t SensorAlgorithm::createQuaternion(std::vector rotationVector, std::vector &quaternion) { - if (rotationVector.size() < ROTATION_VECTOR_LENGTH || rotationVector.size() > QUATERNION_LENGTH) { + if (static_cast(rotationVector.size()) < ROTATION_VECTOR_LENGTH + || static_cast(rotationVector.size()) > QUATERNION_LENGTH) { HiLog::Error(LABEL, "%{public}s Invalid input rotationVector parameter", __func__); return OHOS::Sensors::ERROR; } - if (quaternion.size() < 4) { + if (static_cast(quaternion.size()) < 4) { HiLog::Error(LABEL, "%{public}s Invalid input quaternion parameter", __func__); return OHOS::Sensors::ERROR; } - if (rotationVector.size() == ROTATION_VECTOR_LENGTH) { + if (static_cast(rotationVector.size()) == ROTATION_VECTOR_LENGTH) { quaternion[0] = 1 - static_cast((pow(rotationVector[0], 2) + pow(rotationVector[1], 2) + pow(rotationVector[2], 2))); quaternion[0] = (quaternion[0] > 0) ? static_cast(std::sqrt(quaternion[0])) : 0; @@ -66,7 +67,7 @@ int32_t SensorAlgorithm::transformCoordinateSystemImpl(std::vector inRota if (((x ^ ((z + 1) % 3)) | ( y ^ ((z + 2) % 3))) != 0) { axisZ ^= 0x80; } - int32_t inRotationMatrixLength = inRotationMatrix.size(); + int32_t inRotationMatrixLength = static_cast(inRotationMatrix.size()); int32_t matrixDimension = ((inRotationMatrixLength == FOUR_DIMENSIONAL_MATRIX_LENGTH) ? QUATERNION_LENGTH : ROTATION_VECTOR_LENGTH); for (int32_t j = 0; j < ROTATION_VECTOR_LENGTH; j++) { @@ -96,9 +97,9 @@ int32_t SensorAlgorithm::transformCoordinateSystem(std::vector inRotation std::vector &outRotationMatrix) { HiLog::Info(LABEL, "%{public}s inRotationMatrix start", __func__); - int32_t inRotationMatrixLength = inRotationMatrix.size(); + int32_t inRotationMatrixLength = static_cast(inRotationMatrix.size()); if (((inRotationMatrixLength != THREE_DIMENSIONAL_MATRIX_LENGTH) && (inRotationMatrixLength != FOUR_DIMENSIONAL_MATRIX_LENGTH)) - || (inRotationMatrixLength != outRotationMatrix.size())) { + || (inRotationMatrixLength != static_cast(outRotationMatrix.size()))) { HiLog::Error(LABEL, "%{public}s Invalid input parameter", __func__); return OHOS::Sensors::ERROR; } @@ -135,7 +136,7 @@ int32_t SensorAlgorithm::getGeomagneticDip(std::vector inclinationMatrix, HiLog::Error(LABEL, "%{public}s invalid parameter", __func__); return OHOS::Sensors::ERROR; } - int32_t matrixLength = inclinationMatrix.size(); + int32_t matrixLength = static_cast(inclinationMatrix.size()); if (matrixLength != THREE_DIMENSIONAL_MATRIX_LENGTH && matrixLength != FOUR_DIMENSIONAL_MATRIX_LENGTH) { HiLog::Error(LABEL, "%{public}s Invalid input parameter", __func__); return OHOS::Sensors::ERROR; @@ -151,12 +152,12 @@ int32_t SensorAlgorithm::getGeomagneticDip(std::vector inclinationMatrix, int32_t SensorAlgorithm::getAngleModify(std::vector curRotationMatrix, std::vector preRotationMatrix, std::vector &angleChange) { - if (angleChange.size() < ROTATION_VECTOR_LENGTH) { + if (static_cast(angleChange.size()) < ROTATION_VECTOR_LENGTH) { HiLog::Error(LABEL, "%{public}s invalid parameter", __func__); return OHOS::Sensors::ERROR; } - int32_t curRotationMatrixLength = curRotationMatrix.size(); - int32_t preRotationMatrixLength = preRotationMatrix.size(); + int32_t curRotationMatrixLength = static_cast(curRotationMatrix.size()); + int32_t preRotationMatrixLength = static_cast(preRotationMatrix.size()); if ((curRotationMatrixLength != FOUR_DIMENSIONAL_MATRIX_LENGTH) && (curRotationMatrixLength != THREE_DIMENSIONAL_MATRIX_LENGTH)) { HiLog::Error(LABEL, "%{public}s Invalid input curRotationMatrix parameter", __func__); @@ -195,11 +196,11 @@ int32_t SensorAlgorithm::getAngleModify(std::vector curRotationMatrix, st int32_t SensorAlgorithm::getDirection(std::vector rotationMatrix, std::vector &rotationAngle) { - if (rotationAngle.size() < ROTATION_VECTOR_LENGTH) { + if (static_cast(rotationAngle.size()) < ROTATION_VECTOR_LENGTH) { HiLog::Error(LABEL, "%{public}s invalid parameter", __func__); return OHOS::Sensors::ERROR; } - int32_t rotationMatrixLength = rotationMatrix.size(); + int32_t rotationMatrixLength = static_cast(rotationMatrix.size()); if ((rotationMatrixLength != FOUR_DIMENSIONAL_MATRIX_LENGTH) && (rotationMatrixLength != THREE_DIMENSIONAL_MATRIX_LENGTH)) { HiLog::Error(LABEL, "%{public}s Invalid input rotationMatrix parameter", __func__); @@ -218,8 +219,8 @@ int32_t SensorAlgorithm::getDirection(std::vector rotationMatrix, std::ve int32_t SensorAlgorithm::createRotationMatrix(std::vector rotationVector, std::vector &rotationMatrix) { - int32_t rotationMatrixLength = rotationMatrix.size(); - if ((rotationVector.size() < ROTATION_VECTOR_LENGTH) || ((rotationMatrixLength != FOUR_DIMENSIONAL_MATRIX_LENGTH) + int32_t rotationMatrixLength = static_cast(rotationMatrix.size()); + if ((static_cast(rotationVector.size()) < ROTATION_VECTOR_LENGTH) || ((rotationMatrixLength != FOUR_DIMENSIONAL_MATRIX_LENGTH) && (rotationMatrixLength != THREE_DIMENSIONAL_MATRIX_LENGTH))) { HiLog::Error(LABEL, "%{public}s Invalid input rotationMatrix parameter", __func__); return OHOS::Sensors::ERROR; @@ -267,7 +268,8 @@ int32_t SensorAlgorithm::createRotationMatrix(std::vector rotationVector, int32_t SensorAlgorithm::createRotationAndInclination(std::vector gravity, std::vector geomagnetic, std::vector &rotationMatrix, std::vector &inclinationMatrix) { - if (gravity.size() < ROTATION_VECTOR_LENGTH || geomagnetic.size() < ROTATION_VECTOR_LENGTH) { + if (static_cast(gravity.size()) < ROTATION_VECTOR_LENGTH + || static_cast(geomagnetic.size()) < ROTATION_VECTOR_LENGTH) { HiLog::Error(LABEL, "%{public}s Invalid input parameter", __func__); return OHOS::Sensors::ERROR; } @@ -302,8 +304,8 @@ int32_t SensorAlgorithm::createRotationAndInclination(std::vector gravity measuredValue[1] = gravity[2] * componentH[0] - gravity[0] * componentH[2]; measuredValue[2] = gravity[0] * componentH[1] - gravity[1] * componentH[0]; - int32_t rotationMatrixLength = rotationMatrix.size(); - int32_t inclinationMatrixLength = inclinationMatrix.size(); + int32_t rotationMatrixLength = static_cast(rotationMatrix.size()); + int32_t inclinationMatrixLength = static_cast(inclinationMatrix.size()); if ((rotationMatrixLength != 9 && rotationMatrixLength != 16) || (inclinationMatrixLength != 9 && inclinationMatrixLength != 16)) { HiLog::Error(LABEL, "%{public}s Invalid input parameter", __func__); diff --git a/interfaces/plugin/include/sensor_napi_utils.h b/interfaces/plugin/include/sensor_napi_utils.h index 46edf63a..47175c7a 100644 --- a/interfaces/plugin/include/sensor_napi_utils.h +++ b/interfaces/plugin/include/sensor_napi_utils.h @@ -45,7 +45,7 @@ struct RationMatrixData { struct SensorData { int32_t sensorTypeId; - int32_t dataLength; + uint32_t dataLength; float data[16]; int64_t timestamp; }; diff --git a/interfaces/plugin/src/sensor_js.cpp b/interfaces/plugin/src/sensor_js.cpp index 6f8ed84d..35a0c951 100644 --- a/interfaces/plugin/src/sensor_js.cpp +++ b/interfaces/plugin/src/sensor_js.cpp @@ -341,7 +341,7 @@ static napi_value TransformCoordinateSystem(napi_env env, napi_callback_info inf .deferred = nullptr, .type = TRANSFORM_COORDINATE_SYSTEM, }; - int32_t inRotationVectorLength = inRotationVector.size(); + int32_t inRotationVectorLength = static_cast(inRotationVector.size()); std::vector outRotationVector(inRotationVectorLength); std::unique_ptr sensorAlgorithm = std::make_unique(); int32_t ret = sensorAlgorithm->transformCoordinateSystem(inRotationVector, axisX, axisY, outRotationVector); diff --git a/interfaces/plugin/src/sensor_napi_utils.cpp b/interfaces/plugin/src/sensor_napi_utils.cpp index bc60458e..2a706d70 100644 --- a/interfaces/plugin/src/sensor_napi_utils.cpp +++ b/interfaces/plugin/src/sensor_napi_utils.cpp @@ -272,7 +272,8 @@ void ConvertToSensorData(napi_env env, AsyncCallbackInfo *asyncCallbackInfo, nap { napi_get_undefined(env, &result[0]); int32_t sensorTypeId = asyncCallbackInfo->data.sensorData.sensorTypeId; - if ((g_sensorAttributeList.count(sensorTypeId)) == 0 || (g_sensorAttributeList[sensorTypeId].size() + if ((g_sensorAttributeList.count(sensorTypeId)) == 0 + || (static_cast(g_sensorAttributeList[sensorTypeId].size()) > (asyncCallbackInfo->data.sensorData.dataLength / sizeof(float)))) { HiLog::Error(LABEL, "%{public}s count of sensorTypeId is zero", __func__); return; diff --git a/services/sensor/src/sensor_data_processer.cpp b/services/sensor/src/sensor_data_processer.cpp index a6288cef..29cbf982 100644 --- a/services/sensor/src/sensor_data_processer.cpp +++ b/services/sensor/src/sensor_data_processer.cpp @@ -66,9 +66,9 @@ void SensorDataProcesser::SendNoneFifoCacheData(std::unordered_map sendEvents; std::lock_guard dataCountLock(dataCountMutex_); sendEvents.push_back(event); - uint32_t sensorId = event.sensorTypeId; + uint32_t sensorId = static_cast(event.sensorTypeId); if (sensorId == FLUSH_COMPLETE_ID) { - sensorId = event.sensorTypeId; + sensorId = static_cast(event.sensorTypeId); } auto dataCountIt = dataCountMap_.find(sensorId); if (dataCountIt == dataCountMap_.end()) { @@ -116,9 +116,9 @@ void SensorDataProcesser::SendFifoCacheData(std::unordered_map &channel, struct SensorEvent &event, uint64_t periodCount, uint64_t fifoCount) { - uint32_t sensorId = event.sensorTypeId; + uint32_t sensorId = static_cast(event.sensorTypeId); if (sensorId == FLUSH_COMPLETE_ID) { - sensorId = event.sensorTypeId; + sensorId = static_cast(event.sensorTypeId); } std::lock_guard dataCountLock(dataCountMutex_); auto dataCountIt = dataCountMap_.find(sensorId); @@ -177,9 +177,9 @@ void SensorDataProcesser::ReportData(sptr &channel, stru HiLog::Error(LABEL, "%{public}s channel cannot be null", __func__); return; } - uint32_t sensorId = event.sensorTypeId; + uint32_t sensorId = static_cast(event.sensorTypeId); if (sensorId == FLUSH_COMPLETE_ID) { - sensorId = event.sensorTypeId; + sensorId = static_cast(event.sensorTypeId); } auto &cacheBuf = const_cast &>(channel->GetDataCacheBuf()); if (ReportNotContinuousData(cacheBuf, channel, event)) { @@ -200,9 +200,9 @@ void SensorDataProcesser::ReportData(sptr &channel, stru bool SensorDataProcesser::ReportNotContinuousData(std::unordered_map &cacheBuf, sptr &channel, struct SensorEvent &event) { - uint32_t sensorId = event.sensorTypeId; + uint32_t sensorId = static_cast(event.sensorTypeId); if (sensorId == FLUSH_COMPLETE_ID) { - sensorId = event.sensorTypeId; + sensorId = static_cast(event.sensorTypeId); } std::lock_guard sensorLock(sensorMutex_); auto sensor = sensorMap_.find(sensorId); @@ -240,9 +240,9 @@ void SensorDataProcesser::SendRawData(std::unordered_map(event.size()); std::vector transferEvents; - for (int32_t i = 0; i < (int32_t)eventSize; i++) { + for (int32_t i = 0; i < eventSize; i++) { struct TransferSensorEvents transferEvent = { .sensorTypeId = event[i].sensorTypeId, .version = event[i].version, @@ -260,9 +260,9 @@ void SensorDataProcesser::SendRawData(std::unordered_mapSendData(transferEvents.data(), eventSize * sizeof(struct TransferSensorEvents)); if (ret != ERR_OK) { HiLog::Error(LABEL, "%{public}s send data failed, ret : %{public}d", __func__, ret); - uint32_t sensorId = event[eventSize - 1].sensorTypeId; + uint32_t sensorId = static_cast(event[eventSize - 1].sensorTypeId); if (sensorId == FLUSH_COMPLETE_ID) { - sensorId = event[eventSize - 1].sensorTypeId; + sensorId = static_cast(event[eventSize - 1].sensorTypeId); } cacheBuf[sensorId] = event[eventSize - 1]; } @@ -274,11 +274,11 @@ int32_t SensorDataProcesser::CacheSensorEvent(const struct SensorEvent &event, s HiLog::Error(LABEL, "%{public}s channel cannot be null", __func__); return INVALID_POINTER; } - uint32_t ret = ERR_OK; + int32_t ret = ERR_OK; auto &cacheBuf = const_cast &>(channel->GetDataCacheBuf()); - uint32_t sensorId = event.sensorTypeId; + uint32_t sensorId = static_cast(event.sensorTypeId); if (sensorId == FLUSH_COMPLETE_ID) { - sensorId = event.sensorTypeId; + sensorId = static_cast(event.sensorTypeId); } auto cacheEvent = cacheBuf.find(sensorId); if (cacheEvent != cacheBuf.end()) { @@ -308,10 +308,10 @@ void SensorDataProcesser::EventFilter(struct CircularEventBuf &eventsBuf) { HiLog::Debug(LABEL, "%{public}s begin", __func__); uint32_t realSensorId = 0; - uint32_t sensorId = eventsBuf.circularBuf[eventsBuf.readPosition].sensorTypeId; + uint32_t sensorId = static_cast(eventsBuf.circularBuf[eventsBuf.readPosition].sensorTypeId); std::vector> channelList; if (sensorId == FLUSH_COMPLETE_ID) { - realSensorId = eventsBuf.circularBuf[eventsBuf.readPosition].sensorTypeId; + realSensorId = static_cast(eventsBuf.circularBuf[eventsBuf.readPosition].sensorTypeId); channelList = clientInfo_.GetSensorChannel(realSensorId); } else { channelList = clientInfo_.GetSensorChannel(sensorId); -- Gitee From c8be3cab2e989c802a87168ec79f89a89752f216 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E9=9C=B2?= Date: Wed, 23 Feb 2022 11:52:12 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=BC=BA=E8=BD=AC=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: bailu1992 --- interfaces/native/src/sensor_algorithm.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/interfaces/native/src/sensor_algorithm.cpp b/interfaces/native/src/sensor_algorithm.cpp index b38a51b4..6742b4d4 100644 --- a/interfaces/native/src/sensor_algorithm.cpp +++ b/interfaces/native/src/sensor_algorithm.cpp @@ -31,7 +31,7 @@ int32_t SensorAlgorithm::createQuaternion(std::vector rotationVector, std HiLog::Error(LABEL, "%{public}s Invalid input rotationVector parameter", __func__); return OHOS::Sensors::ERROR; } - if (static_cast(quaternion.size()) < 4) { + if (static_cast(quaternion.size()) < QUATERNION_LENGTH) { HiLog::Error(LABEL, "%{public}s Invalid input quaternion parameter", __func__); return OHOS::Sensors::ERROR; } @@ -220,7 +220,8 @@ int32_t SensorAlgorithm::getDirection(std::vector rotationMatrix, std::ve int32_t SensorAlgorithm::createRotationMatrix(std::vector rotationVector, std::vector &rotationMatrix) { int32_t rotationMatrixLength = static_cast(rotationMatrix.size()); - if ((static_cast(rotationVector.size()) < ROTATION_VECTOR_LENGTH) || ((rotationMatrixLength != FOUR_DIMENSIONAL_MATRIX_LENGTH) + if ((static_cast(rotationVector.size()) < ROTATION_VECTOR_LENGTH) + || ((rotationMatrixLength != FOUR_DIMENSIONAL_MATRIX_LENGTH) && (rotationMatrixLength != THREE_DIMENSIONAL_MATRIX_LENGTH))) { HiLog::Error(LABEL, "%{public}s Invalid input rotationMatrix parameter", __func__); return OHOS::Sensors::ERROR; -- Gitee