diff --git a/frameworks/js/napi/include/sensor_napi_error.h b/frameworks/js/napi/include/sensor_napi_error.h index 7c2d507a2d68e63756b4c08a7acaadf347efc7fc..6b2e5b9f8e8244ce7a04ec271a8325ee2574f618 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."}, + {SENSOR_NO_SUPPORT, "The Sensor is not supported by the device."}, {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..a3b857a56094d130cbadff63c42ecb5ec8f5b9de 100644 --- a/frameworks/js/napi/include/sensor_napi_utils.h +++ b/frameworks/js/napi/include/sensor_napi_utils.h @@ -52,6 +52,7 @@ bool ConvertToSensorData(const napi_env &env, sptr asyncCallb 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 ConvertToSingleSensor(const napi_env &env, sptr asyncCallbackInfo, napi_value result[2]); +bool ConvertToSensorInfo(const napi_env &env, const SensorInfo &sensorInfo, napi_value &result); bool ConvertToBodyData(const napi_env &env, sptr asyncCallbackInfo, napi_value result[2]); bool CreateFailMessage(CallbackDataType type, int32_t code, string message, sptr &asyncCallbackInfo); diff --git a/frameworks/js/napi/src/sensor_js.cpp b/frameworks/js/napi/src/sensor_js.cpp index 5a7a64ff2ace7f847554e3d61578a2d06e7d0a6c..a5517f8c76fb01bdad4fcebc607386db6257ed65 100644 --- a/frameworks/js/napi/src/sensor_js.cpp +++ b/frameworks/js/napi/src/sensor_js.cpp @@ -987,6 +987,50 @@ 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; + size_t argc = 0; + napi_value result = nullptr; + napi_get_undefined(env, &result); + napi_value thisVar = nullptr; + napi_status status = napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr); + if (status != napi_ok) { + ThrowErr(env, PARAMETER_ERROR, "napi_get_cb_info fail"); + return result; + } + SensorInfo *sensorInfos = nullptr; + int32_t count = 0; + int32_t ret = GetAllSensors(&sensorInfos, &count); + if (ret != OHOS::ERR_OK) { + ThrowErr(env, ret, "Get sensor list fail"); + return result; + } + vector sensorInfoVec; + for (int32_t i = 0; i < count; ++i) { + if (sensorInfos[i].sensorTypeId == SENSOR_TYPE_ID_AMBIENT_LIGHT1) { + SEN_HILOGD("This sensor is secondary ambient light"); + continue; + } + sensorInfoVec.push_back(*(sensorInfos + i)); + } + if (napi_create_array(env, &result) != napi_ok) { + ThrowErr(env, PARAMETER_ERROR, "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)) { + ThrowErr(env, PARAMETER_ERROR, "Convert sensor info fail"); + return result; + } + if (napi_set_element(env, result, i, value) != napi_ok) { + ThrowErr(env, PARAMETER_ERROR, "napi_set_element fail"); + } + } + return result; +} + static napi_value GetSingleSensor(napi_env env, napi_callback_info info) { CALL_LOG_ENTER; @@ -1035,6 +1079,52 @@ static napi_value GetSingleSensor(napi_env env, napi_callback_info info) return EmitAsyncWork(nullptr, asyncCallbackInfo); } +static napi_value GetSingleSensorSync(napi_env env, napi_callback_info info) +{ + CALL_LOG_ENTER; + size_t argc = 1; + napi_value args[1] = { 0 }; + napi_value result = nullptr; + napi_get_undefined(env, &result); + napi_value thisVar = nullptr; + napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr); + if (status != napi_ok || argc == 0) { + ThrowErr(env, PARAMETER_ERROR, "napi_get_cb_info fail or number of parameter invalid"); + return result; + } + int32_t sensorTypeId = INVALID_SENSOR_ID; + if (!GetNativeInt32(env, args[0], sensorTypeId)) { + ThrowErr(env, PARAMETER_ERROR, "Wrong argument type, get number fail"); + return result; + } + SensorInfo *sensorInfos = nullptr; + int32_t count = 0; + int32_t ret = GetAllSensors(&sensorInfos, &count); + if (ret != OHOS::ERR_OK) { + ThrowErr(env, ret, "Get sensor list fail"); + return result; + } + vector sensorInfoVec; + for (int32_t i = 0; i < count; ++i) { + if (sensorInfos[i].sensorTypeId == SENSOR_TYPE_ID_AMBIENT_LIGHT1) { + SEN_HILOGD("This sensor is secondary ambient light"); + continue; + } + if (sensorInfos[i].sensorTypeId == sensorTypeId) { + sensorInfoVec.push_back(*(sensorInfos + i)); + break; + } + } + if (sensorInfoVec.empty()) { + ThrowErr(env, SENSOR_NO_SUPPORT, "Can't find the sensorId"); + return result; + } + if (!ConvertToSensorInfo(env, sensorInfoVec[0], result)) { + ThrowErr(env, PARAMETER_ERROR, "Convert sensor info fail"); + } + return result; +} + napi_value Subscribe(napi_env env, napi_callback_info info, int32_t sensorTypeId, CallbackDataType type) { CALL_LOG_ENTER; @@ -1286,7 +1376,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/frameworks/js/napi/src/sensor_napi_utils.cpp b/frameworks/js/napi/src/sensor_napi_utils.cpp index df2dd4d3fd0552c943a5ec670680db0a11d8b740..77efe702dc3b3e8ac972f6fcd6868856d739acf3 100644 --- a/frameworks/js/napi/src/sensor_napi_utils.cpp +++ b/frameworks/js/napi/src/sensor_napi_utils.cpp @@ -236,7 +236,7 @@ bool getJsonObject(const napi_env &env, sptr asyncCallbackInf return true; } -bool ConvertToSensorInfo(const napi_env &env, SensorInfo sensorInfo, napi_value &result) +bool ConvertToSensorInfo(const napi_env &env, const SensorInfo &sensorInfo, napi_value &result) { CALL_LOG_ENTER; CHKNRF(env, napi_create_object(env, &result), "napi_create_object"); diff --git a/utils/common/include/sensor_errors.h b/utils/common/include/sensor_errors.h index 2676c93e5de708a2976a0a3a2617a6dd9f634649..68e780a13933c69d9949075fa73739836c855612 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. + SENSOR_NO_SUPPORT = 14500102, // Use this error code when the sensor is not supported by the device. NON_SYSTEM_API = 202 // Permission check failed. A non-system application uses the system API. };