diff --git a/interfaces/plugin/include/sensor_js.h b/interfaces/plugin/include/sensor_js.h index bb38db195807fbbdbf7550c08865ca4246a0fc5f..00d01db9892937b5db2c372609e232eac7f93c73 100755 --- a/interfaces/plugin/include/sensor_js.h +++ b/interfaces/plugin/include/sensor_js.h @@ -16,6 +16,7 @@ #define SENSOR_JS_H #include "sensor_agent.h" +#include "errors.h" static int32_t UnsubscribeSensor(int32_t sensorTypeId); static void DataCallbackImpl(SensorEvent *event); diff --git a/interfaces/plugin/src/sensor_js.cpp b/interfaces/plugin/src/sensor_js.cpp index 02c4c67fdb21764891f6894f1bcd2dd471ec9873..d70df4a5d225650e053a0c88d7f7be22a3a90779 100644 --- a/interfaces/plugin/src/sensor_js.cpp +++ b/interfaces/plugin/src/sensor_js.cpp @@ -39,6 +39,8 @@ using namespace OHOS::HiviewDFX; static constexpr HiLogLabel LABEL = {LOG_CORE, 0xD002708, "SensorJsAPI"}; +const int32_t REPORTING_INTERVAL = 200000000; + static std::map g_onceCallbackInfos; static std::map> g_onCallbackInfos; @@ -98,12 +100,12 @@ static int32_t UnsubscribeSensor(int32_t sensorTypeId) { HiLog::Info(LABEL, "%{public}s in", __func__); int32_t ret = DeactivateSensor(sensorTypeId, &user); - if (ret < 0) { + if (ret != OHOS::ERR_OK) { HiLog::Error(LABEL, "%{public}s DeactivateSensor failed", __func__); return ret; } ret = UnsubscribeSensor(sensorTypeId, &user); - if (ret < 0) { + if (ret != OHOS::ERR_OK) { HiLog::Error(LABEL, "%{public}s UnsubscribeSensor failed", __func__); return ret; } @@ -115,17 +117,17 @@ static int32_t SubscribeSensor(int32_t sensorTypeId, int64_t interval, RecordSen { HiLog::Info(LABEL, "%{public}s in, sensorTypeId: %{public}d", __func__, sensorTypeId); int32_t ret = SubscribeSensor(sensorTypeId, &user); - if (ret < 0) { + if (ret != OHOS::ERR_OK) { HiLog::Error(LABEL, "%{public}s subscribeSensor failed", __func__); return ret; } ret = SetBatch(sensorTypeId, &user, interval, 0); - if (ret < 0) { + if (ret != OHOS::ERR_OK) { HiLog::Error(LABEL, "%{public}s set batch failed", __func__); return ret; } ret = ActivateSensor(sensorTypeId, &user); - if (ret < 0) { + if (ret != OHOS::ERR_OK) { HiLog::Error(LABEL, "%{public}s activateSensor failed", __func__); return ret; } @@ -159,7 +161,7 @@ static napi_value Once(napi_env env, napi_callback_info info) if (g_onCallbackInfos.find(sensorTypeId) == g_onCallbackInfos.end()) { HiLog::Debug(LABEL, "%{public}s no subscription to change sensor data, registration is required", __func__); int32_t ret = SubscribeSensor(sensorTypeId, 200000000, DataCallbackImpl); - if (ret < 0) { + if (ret != OHOS::ERR_OK) { HiLog::Error(LABEL, "%{public}s subscribe Sensor failed", __func__); g_onceCallbackInfos.erase(sensorTypeId); return nullptr; @@ -171,10 +173,11 @@ static napi_value Once(napi_env env, napi_callback_info info) static bool IsSubscribed(napi_env env, int32_t sensorTypeId, napi_value callback) { + HiLog::Info(LABEL, "%{public}s in, sensorTypeId: %{public}d", __func__, sensorTypeId); if (g_onCallbackInfos.find(sensorTypeId) == g_onCallbackInfos.end()) { return false; } - std::vector callbackInfos = g_onCallbackInfos[sensorTypeId]; + std::vector callbackInfos = g_onCallbackInfos[sensorTypeId]; for (auto callbackInfo : callbackInfos) { napi_value sensorCallback = nullptr; napi_get_reference_value(env, callbackInfo->callback[0], &sensorCallback); @@ -219,7 +222,7 @@ static napi_value On(napi_env env, napi_callback_info info) return nullptr; } int32_t sensorTypeId = GetCppInt32(args[0], env); - int64_t interval = 200000000; + int64_t interval = REPORTING_INTERVAL; if (argc == 3) { napi_value value = NapiGetNamedProperty(args[2], "interval", env); if (!IsMatchType(env, value, napi_number)) { @@ -230,7 +233,7 @@ static napi_value On(napi_env env, napi_callback_info info) HiLog::Debug(LABEL, "%{public}s interval is %{public}lld", __func__, interval); } int32_t ret = SubscribeSensor(sensorTypeId, interval, DataCallbackImpl); - if (ret < 0) { + if (ret != OHOS::ERR_OK) { HiLog::Error(LABEL, "%{public}s subscribeSensor failed", __func__); return nullptr; } @@ -299,7 +302,7 @@ static napi_value Off(napi_env env, napi_callback_info info) RemoveAllCallback(env, sensorTypeId); } int32_t ret = UnsubscribeSensor(sensorTypeId); - if (ret < 0) { + if (ret != OHOS::ERR_OK) { HiLog::Error(LABEL, "%{public}s UnsubscribeSensor failed", __func__); } return nullptr; @@ -396,7 +399,7 @@ static napi_value TransformCoordinateSystem(napi_env env, napi_callback_info inf std::vector outRotationVector(inRotationVectorLength); std::unique_ptr sensorAlgorithm = std::make_unique(); int32_t ret = sensorAlgorithm->transformCoordinateSystem(inRotationVector, axisX, axisY, outRotationVector); - if (ret < 0) { + if (ret != OHOS::ERR_OK) { HiLog::Error(LABEL, "%{public}s EmitPromiseWork failed", __func__); asyncCallbackInfo->type = FAIL; asyncCallbackInfo->error.code = ret; @@ -446,7 +449,7 @@ static napi_value GetAngleModify(napi_env env, napi_callback_info info) std::vector angleChange(ROTATION_VECTOR_LENGTH); std::unique_ptr sensorAlgorithm = std::make_unique(); int32_t ret = sensorAlgorithm->getAngleModify(curRotationVector, preRotationVector, angleChange); - if (ret < 0) { + if (ret != OHOS::ERR_OK) { HiLog::Error(LABEL, "%{public}s failed", __func__); asyncCallbackInfo->type = FAIL; asyncCallbackInfo->error.code = ret; @@ -495,7 +498,7 @@ static napi_value GetDirection(napi_env env, napi_callback_info info) std::vector rotationAngle(ROTATION_VECTOR_LENGTH); std::unique_ptr sensorAlgorithm = std::make_unique(); int32_t ret = sensorAlgorithm->getDirection(rotationMatrix, rotationAngle); - if (ret < 0) { + if (ret != OHOS::ERR_OK) { HiLog::Error(LABEL, "%{public}s failed", __func__); asyncCallbackInfo->type = FAIL; asyncCallbackInfo->error.code = ret; @@ -546,7 +549,7 @@ static napi_value CreateQuaternion(napi_env env, napi_callback_info info) std::vector quaternion(QUATERNION_LENGTH); std::unique_ptr sensorAlgorithm = std::make_unique(); int32_t ret = sensorAlgorithm->createQuaternion(rotationVector, quaternion); - if (ret < 0) { + if (ret != OHOS::ERR_OK) { HiLog::Error(LABEL, "%{public}s failed", __func__); asyncCallbackInfo->type = FAIL; asyncCallbackInfo->error.code = ret; @@ -597,7 +600,7 @@ static napi_value GetAltitude(napi_env env, napi_callback_info info) float altitude = 0; std::unique_ptr sensorAlgorithm = std::make_unique(); int32_t ret = sensorAlgorithm->getAltitude(seaPressure, currentPressure, &altitude); - if (ret < 0) { + if (ret != OHOS::ERR_OK) { HiLog::Error(LABEL, "%{public}s failed", __func__); asyncCallbackInfo->type = FAIL; asyncCallbackInfo->error.code = ret; @@ -643,7 +646,7 @@ static napi_value GetGeomagneticDip(napi_env env, napi_callback_info info) float geomagneticDip = 0; std::unique_ptr sensorAlgorithm = std::make_unique(); int32_t ret = sensorAlgorithm->getGeomagneticDip(inclinationMatrix, &geomagneticDip); - if (ret < 0) { + if (ret != OHOS::ERR_OK) { HiLog::Error(LABEL, "%{public}s failed", __func__); asyncCallbackInfo->type = FAIL; asyncCallbackInfo->error.code = ret; @@ -690,7 +693,7 @@ static napi_value CreateRotationMatrix(napi_env env, napi_callback_info info) std::vector rotationVector = GetCppArrayFloat(env, args[0]); std::vector rotationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH); int32_t ret = sensorAlgorithm->createRotationMatrix(rotationVector, rotationMatrix); - if (ret < 0) { + if (ret != OHOS::ERR_OK) { HiLog::Error(LABEL, "%{public}s failed", __func__); asyncCallbackInfo->type = FAIL; asyncCallbackInfo->error.code = ret; @@ -725,7 +728,7 @@ static napi_value CreateRotationMatrix(napi_env env, napi_callback_info info) std::vector rotation(THREE_DIMENSIONAL_MATRIX_LENGTH); std::vector inclination(THREE_DIMENSIONAL_MATRIX_LENGTH); int32_t ret = sensorAlgorithm->createRotationAndInclination(gravity, geomagnetic, rotation, inclination); - if (ret < 0) { + if (ret != OHOS::ERR_OK) { HiLog::Error(LABEL, "%{public}s failed", __func__); asyncCallbackInfo->type = FAIL; asyncCallbackInfo->error.code = ret; @@ -770,7 +773,7 @@ static napi_value GetSensorList(napi_env env, napi_callback_info info) .type = GET_SENSOR_LIST, }; int32_t ret = GetAllSensors(&sensorInfos, &count); - if (ret < 0) { + if (ret != OHOS::ERR_OK) { HiLog::Error(LABEL, "%{public}s get sensor list failed", __func__); asyncCallbackInfo->type = FAIL; asyncCallbackInfo->error.code = ret; @@ -779,7 +782,6 @@ static napi_value GetSensorList(napi_env env, napi_callback_info info) asyncCallbackInfo->sensorInfos.push_back(*(sensorInfos + i)); } } - if (argc == 0) { napi_deferred deferred = nullptr; napi_value promise = nullptr; @@ -819,7 +821,7 @@ static napi_value GetSingleSensor(napi_env env, napi_callback_info info) SensorInfo *sensorInfos = nullptr; int32_t count = 0; int32_t ret = GetAllSensors(&sensorInfos, &count); - if (ret < 0) { + if (ret != OHOS::ERR_OK) { HiLog::Error(LABEL, "%{public}s get sensorlist failed", __func__); asyncCallbackInfo->type = FAIL; asyncCallbackInfo->error.code = ret;