diff --git a/frameworks/js/napi/include/sensor_napi_error.h b/frameworks/js/napi/include/sensor_napi_error.h index 7c2d507a2d68e63756b4c08a7acaadf347efc7fc..92bbeda36f63b3cff77e85c7df6e6df0a89cbe66 100755 --- a/frameworks/js/napi/include/sensor_napi_error.h +++ b/frameworks/js/napi/include/sensor_napi_error.h @@ -28,6 +28,7 @@ const std::map ERROR_MESSAGES = { {SERVICE_EXCEPTION, "Service exception."}, {PERMISSION_DENIED, "Permission denied."}, {PARAMETER_ERROR, "The parameter invalid."}, + {DEVICE_NOT_EXIST, "Device not exist"}, {NON_SYSTEM_API, "Non-system api."}, }; diff --git a/frameworks/js/napi/include/sensor_napi_utils.h b/frameworks/js/napi/include/sensor_napi_utils.h index ca4549ea1caf10c8353047b56cb35fd5f7f1f7af..099f44325ea20f02c6c6f9d5e277c4a5456acd03 100644 --- a/frameworks/js/napi/include/sensor_napi_utils.h +++ b/frameworks/js/napi/include/sensor_napi_utils.h @@ -51,6 +51,7 @@ bool ConvertToRotationMatrix(const napi_env &env, sptr asyncC bool ConvertToSensorData(const napi_env &env, sptr asyncCallbackInfo, napi_value result[2]); bool CreateNapiArray(const napi_env &env, float *data, int32_t dataLength, napi_value &result); bool ConvertToSensorInfos(const napi_env &env, sptr asyncCallbackInfo, napi_value result[2]); +bool ConvertToSensorInfo(const napi_env &env, SensorInfo sensorInfo, napi_value &result); bool ConvertToSingleSensor(const napi_env &env, sptr asyncCallbackInfo, napi_value result[2]); bool ConvertToBodyData(const napi_env &env, sptr asyncCallbackInfo, napi_value result[2]); bool CreateFailMessage(CallbackDataType type, int32_t code, string message, @@ -68,6 +69,14 @@ void ReleaseCallback(sptr asyncCallbackInfo); } \ } while (0) +#define CHKNCP(env, cond, message) \ + do { \ + if (!(cond)) { \ + SEN_HILOGE("(%{public}s)", #message); \ + return nullptr; \ + } \ + } while (0) + #define CHKNRP(env, state, message) \ do { \ if ((state) != napi_ok) { \ diff --git a/frameworks/js/napi/src/sensor_js.cpp b/frameworks/js/napi/src/sensor_js.cpp index 5a7a64ff2ace7f847554e3d61578a2d06e7d0a6c..48936bf3b34d4426c89a5530b386483eedab727e 100644 --- a/frameworks/js/napi/src/sensor_js.cpp +++ b/frameworks/js/napi/src/sensor_js.cpp @@ -987,6 +987,34 @@ static napi_value GetSensorList(napi_env env, napi_callback_info info) return EmitAsyncWork(nullptr, asyncCallbackInfo); } +static napi_value GetSensorListSync(napi_env env, napi_callback_info info) +{ + CALL_LOG_ENTER; + SensorInfo *sensorInfos = nullptr; + int32_t count = 0; + int32_t ret = GetAllSensors(&sensorInfos, &count); + if (ret != ERR_OK) { + ThrowErr(env, ret, "Failed to get sensor list synchronously"); + return nullptr; + } + napi_value resultArray = nullptr ; + napi_status status = napi_create_array(env, &resultArray); + if (status != napi_ok) { + SEN_HILOGE("Failed to create result array"); + return nullptr; + } + for (int32_t j = 0; j < count; ++j) { + if (sensorInfos[j].sensorTypeId == SENSOR_TYPE_ID_AMBIENT_LIGHT1) { + SEN_HILOGD("This sensor is secondary ambient light"); + continue; + } + napi_value value = nullptr; + CHKNCP(env, ConvertToSensorInfo(env, sensorInfos[j], value), "Convert sensor info fail"); + CHKNCP(env, (napi_set_element(env, resultArray, j, value) == napi_ok), "napi_set_element fail"); + } + return resultArray; +} + static napi_value GetSingleSensor(napi_env env, napi_callback_info info) { CALL_LOG_ENTER; @@ -1035,6 +1063,42 @@ static napi_value GetSingleSensor(napi_env env, napi_callback_info info) return EmitAsyncWork(nullptr, asyncCallbackInfo); } +napi_value GetSingleSensorSync(napi_env env, napi_callback_info info) +{ + CALL_LOG_ENTER; + size_t argc = 1; + napi_value args[1] = { 0 }; + napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); + if (status != napi_ok) { + ThrowErr(env, PARAMETER_ERROR, "napi_get_cb_info fail or number of parameter invalid"); + return nullptr; + } + int32_t sensorTypeId = INVALID_SENSOR_ID; + if (!GetNativeInt32(env, args[0], sensorTypeId)) { + ThrowErr(env, PARAMETER_ERROR, "Wrong argument type, get number fail"); + return nullptr; + } + SensorInfo *sensorInfos = nullptr; + int32_t count = 0; + int32_t ret = GetAllSensors(&sensorInfos, &count); + if (ret != OHOS::ERR_OK) { + ThrowErr(env, PARAMETER_ERROR, "Get sensor list fail"); + return nullptr; + } + for (int32_t i = 0; i < count; ++i) { + if (sensorInfos[i].sensorTypeId == sensorTypeId) { + napi_value result = nullptr; + if (!ConvertToSensorInfo(env, sensorInfos[i], result)) { + SEN_HILOGE("ConvertToSensorInfo fail"); + return nullptr; + } + return result; + } + } + ThrowErr(env, DEVICE_NOT_EXIST,"Sensor type not found"); + return nullptr; +} + napi_value Subscribe(napi_env env, napi_callback_info info, int32_t sensorTypeId, CallbackDataType type) { CALL_LOG_ENTER; @@ -1286,7 +1350,9 @@ static napi_value Init(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("createRotationMatrix", CreateRotationMatrix), DECLARE_NAPI_FUNCTION("getRotationMatrix", CreateRotationMatrix), DECLARE_NAPI_FUNCTION("getSensorList", GetSensorList), + DECLARE_NAPI_FUNCTION("getSensorListSync", GetSensorListSync), DECLARE_NAPI_FUNCTION("getSingleSensor", GetSingleSensor), + DECLARE_NAPI_FUNCTION("getSingleSensorSync", GetSingleSensorSync), DECLARE_NAPI_FUNCTION("subscribeAccelerometer", SubscribeAccelerometer), DECLARE_NAPI_FUNCTION("unsubscribeAccelerometer", UnsubscribeAccelerometer), DECLARE_NAPI_FUNCTION("subscribeCompass", SubscribeCompass), diff --git a/utils/common/include/sensor_errors.h b/utils/common/include/sensor_errors.h index 2676c93e5de708a2976a0a3a2617a6dd9f634649..23604fe125da82a0f07ff5850bd9734f0c12f072 100644 --- a/utils/common/include/sensor_errors.h +++ b/utils/common/include/sensor_errors.h @@ -29,6 +29,7 @@ enum ErrorCode : int32_t { PERMISSION_DENIED = 201, // Use this error code when permission is denied. PARAMETER_ERROR = 401, // Use this error code when the input parameter type or range does not match. SERVICE_EXCEPTION = 14500101, // Use this error code when the service is exception. + DEVICE_NOT_EXIST = 14500102, //Use this error code when the device not exist. NON_SYSTEM_API = 202 // Permission check failed. A non-system application uses the system API. };