diff --git a/interfaces/plugin/include/sensor_napi_utils.h b/interfaces/plugin/include/sensor_napi_utils.h index e078b8d0461e43a67388580abebeabc20babd864..d654973e451a5fc8b2b0771539ed98df0a9bc10e 100755 --- a/interfaces/plugin/include/sensor_napi_utils.h +++ b/interfaces/plugin/include/sensor_napi_utils.h @@ -26,7 +26,8 @@ using std::string; const int32_t THREE_DIMENSIONAL_MATRIX_LENGTH = 9; const int32_t FOUR_DIMENSIONAL_MATRIX_LENGTH = 16; - +const int32_t QUATERNION_LENGTH = 4; +const int32_t ROTATION_VECTOR_LENGTH = 3; struct GeomagneticData { float x; float y; @@ -49,12 +50,16 @@ struct SensorData { int64_t timestamp; }; +struct ReserveData { + float reserve[16]; + int32_t length; +}; + union CallbackData { SensorData sensorData; GeomagneticData geomagneticData; RationMatrixData rationMatrixData; - float reserve[16]; - int32_t dataLength; + ReserveData reserveData; }; struct BusinessError { diff --git a/interfaces/plugin/src/sensor_js.cpp b/interfaces/plugin/src/sensor_js.cpp index 0c34ee61ea71be77299924735695f7dada53cadf..0a9f568262bc09a526c01dbccaa870bb4acc33a9 100755 --- a/interfaces/plugin/src/sensor_js.cpp +++ b/interfaces/plugin/src/sensor_js.cpp @@ -351,9 +351,9 @@ static napi_value TransformCoordinateSystem(napi_env env, napi_callback_info inf asyncCallbackInfo->error.code = ret; } else { for (int32_t i = 0; i < inRotationVectorLength; i++) { - asyncCallbackInfo->data.reserve[i] = outRotationVector[i]; + asyncCallbackInfo->data.reserveData.reserve[i] = outRotationVector[i]; } - asyncCallbackInfo->data.dataLength = inRotationVectorLength; + asyncCallbackInfo->data.reserveData.length = inRotationVectorLength; } if (argc == 2) { napi_deferred deferred = nullptr; @@ -392,7 +392,7 @@ static napi_value GetAngleModify(napi_env env, napi_callback_info info) }; std::vector curRotationVector = GetCppArrayFloat(env, args[0]); std::vector preRotationVector = GetCppArrayFloat(env, args[1]); - std::vector angleChange(3); + std::vector angleChange(ROTATION_VECTOR_LENGTH); std::unique_ptr sensorAlgorithm = std::make_unique(); int32_t ret = sensorAlgorithm->getAngleModify(curRotationVector, preRotationVector, angleChange); if (ret < 0) { @@ -400,9 +400,9 @@ static napi_value GetAngleModify(napi_env env, napi_callback_info info) asyncCallbackInfo->type = FAIL; asyncCallbackInfo->error.code = ret; } else { - asyncCallbackInfo->data.dataLength = angleChange.size(); - for (int32_t i = 0; i < asyncCallbackInfo->data.dataLength; i++) { - asyncCallbackInfo->data.reserve[i] = angleChange[i]; + asyncCallbackInfo->data.reserveData.length = ROTATION_VECTOR_LENGTH; + for (int32_t i = 0; i < ROTATION_VECTOR_LENGTH; i++) { + asyncCallbackInfo->data.reserveData.reserve[i] = angleChange[i]; } } if (argc == 2) { @@ -441,7 +441,7 @@ static napi_value GetDirection(napi_env env, napi_callback_info info) .type = GET_DIRECTION, }; std::vector rotationMatrix = GetCppArrayFloat(env, args[0]); - std::vector rotationAngle(3); + std::vector rotationAngle(ROTATION_VECTOR_LENGTH); std::unique_ptr sensorAlgorithm = std::make_unique(); int32_t ret = sensorAlgorithm->getDirection(rotationMatrix, rotationAngle); if (ret < 0) { @@ -449,9 +449,9 @@ static napi_value GetDirection(napi_env env, napi_callback_info info) asyncCallbackInfo->type = FAIL; asyncCallbackInfo->error.code = ret; } else { - asyncCallbackInfo->data.dataLength = rotationAngle.size(); - for (int32_t i = 0; i < asyncCallbackInfo->data.dataLength; i++) { - asyncCallbackInfo->data.reserve[i] = rotationAngle[i]; + asyncCallbackInfo->data.reserveData.length = ROTATION_VECTOR_LENGTH; + for (int32_t i = 0; i < ROTATION_VECTOR_LENGTH; i++) { + asyncCallbackInfo->data.reserveData.reserve[i] = rotationAngle[i]; } } if (argc == 1) { @@ -490,7 +490,7 @@ static napi_value CreateQuaternion(napi_env env, napi_callback_info info) .type = CREATE_QUATERNION, }; std::vector rotationVector = GetCppArrayFloat(env, args[0]); - std::vector quaternion(4); + std::vector quaternion(QUATERNION_LENGTH); std::unique_ptr sensorAlgorithm = std::make_unique(); int32_t ret = sensorAlgorithm->createQuaternion(rotationVector, quaternion); if (ret < 0) { @@ -498,9 +498,9 @@ static napi_value CreateQuaternion(napi_env env, napi_callback_info info) asyncCallbackInfo->type = FAIL; asyncCallbackInfo->error.code = ret; } else { - asyncCallbackInfo->data.dataLength = quaternion.size(); - for (int32_t i = 0; i < asyncCallbackInfo->data.dataLength; i++) { - asyncCallbackInfo->data.reserve[i] = quaternion[i]; + asyncCallbackInfo->data.reserveData.length = QUATERNION_LENGTH; + for (int32_t i = 0; i < QUATERNION_LENGTH; i++) { + asyncCallbackInfo->data.reserveData.reserve[i] = quaternion[i]; } } if (argc == 1) { @@ -549,7 +549,7 @@ static napi_value GetAltitude(napi_env env, napi_callback_info info) asyncCallbackInfo->type = FAIL; asyncCallbackInfo->error.code = ret; } else { - asyncCallbackInfo->data.reserve[0] = altitude; + asyncCallbackInfo->data.reserveData.reserve[0] = altitude; } if (argc == 2) { napi_deferred deferred = nullptr; @@ -595,7 +595,7 @@ static napi_value GetGeomagneticDip(napi_env env, napi_callback_info info) asyncCallbackInfo->type = FAIL; asyncCallbackInfo->error.code = ret; } else { - asyncCallbackInfo->data.reserve[0] = geomagneticDip; + asyncCallbackInfo->data.reserveData.reserve[0] = geomagneticDip; } if (argc == 1) { napi_deferred deferred = nullptr; @@ -642,9 +642,9 @@ static napi_value CreateRotationMatrix(napi_env env, napi_callback_info info) asyncCallbackInfo->type = FAIL; asyncCallbackInfo->error.code = ret; } else { - asyncCallbackInfo->data.dataLength = rotationMatrix.size(); - for (int32_t i = 0; i < asyncCallbackInfo->data.dataLength; i++) { - asyncCallbackInfo->data.reserve[i] = rotationMatrix[i]; + asyncCallbackInfo->data.reserveData.length = THREE_DIMENSIONAL_MATRIX_LENGTH; + for (int32_t i = 0; i < THREE_DIMENSIONAL_MATRIX_LENGTH; i++) { + asyncCallbackInfo->data.reserveData.reserve[i] = rotationMatrix[i]; } } if (argc == 1) { diff --git a/interfaces/plugin/src/sensor_napi_utils.cpp b/interfaces/plugin/src/sensor_napi_utils.cpp index 383426aac10eab6207cbba614ffd3bd5c72c7af2..e0870ce31f513bf64d1206042a2e763444a95a05 100755 --- a/interfaces/plugin/src/sensor_napi_utils.cpp +++ b/interfaces/plugin/src/sensor_napi_utils.cpp @@ -249,13 +249,13 @@ void ConvertToGeomagneticData(napi_env env, AsyncCallbackInfo *asyncCallbackInfo void ConvertToNumber(napi_env env, AsyncCallbackInfo *asyncCallbackInfo, napi_value result[2]) { napi_get_undefined(env, &result[0]); - napi_create_double(env, static_cast(asyncCallbackInfo->data.reserve[0]), &result[1]); + napi_create_double(env, static_cast(asyncCallbackInfo->data.reserveData.reserve[0]), &result[1]); } void ConvertToArray(napi_env env, AsyncCallbackInfo *asyncCallbackInfo, napi_value result[2]) { napi_get_undefined(env, &result[0]); - CreateNapiArray(env, asyncCallbackInfo->data.reserve, asyncCallbackInfo->data.dataLength, result[1]); + CreateNapiArray(env, asyncCallbackInfo->data.reserveData.reserve, asyncCallbackInfo->data.reserveData.length, result[1]); } void ConvertToRotationMatrix(napi_env env, AsyncCallbackInfo *asyncCallbackInfo, napi_value result[2])