From e5d80de29ff057f56151cf0062171868f6e2000b Mon Sep 17 00:00:00 2001 From: bailu1992 Date: Mon, 3 Jun 2024 16:40:15 +0800 Subject: [PATCH 1/3] Add a binary file type policy filter Signed-off-by: bailu1992 --- OAT.xml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/OAT.xml b/OAT.xml index c5ccef5..18565f5 100755 --- a/OAT.xml +++ b/OAT.xml @@ -50,5 +50,9 @@ " desc=""/> + + + + -- Gitee From f146a58f32b29f4118ff3cb6f9b05f1da7049b20 Mon Sep 17 00:00:00 2001 From: bailu1992 Date: Tue, 4 Jun 2024 15:47:10 +0800 Subject: [PATCH 2/3] fixed b961080 from https://gitee.com/bailu1992/sensors_miscdevice/pulls/367 Example Modify the binary file filtering policy Signed-off-by: bailu1992 --- OAT.xml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/OAT.xml b/OAT.xml index 18565f5..66da5bc 100755 --- a/OAT.xml +++ b/OAT.xml @@ -42,6 +42,10 @@ + + + + @@ -50,9 +54,5 @@ " desc=""/> - - - - -- Gitee From e74b9d88cd75ee88fdd2755bb0a5fda073647461 Mon Sep 17 00:00:00 2001 From: lixiangpeng5 Date: Fri, 7 Jun 2024 08:16:35 +0000 Subject: [PATCH 3/3] add he decoder and parse Signed-off-by: lixiangpeng5 Change-Id: Ia2aa54259cd2752b2d077e3bf25b2983b81c054f --- .../vibrator/include/vibrator_napi_error.h | 4 +- .../vibrator/include/vibrator_napi_utils.h | 4 +- .../js/napi/vibrator/src/vibrator_js.cpp | 38 +++++- .../napi/vibrator/src/vibrator_napi_error.cpp | 4 +- .../napi/vibrator/src/vibrator_napi_utils.cpp | 4 +- .../common/include/i_miscdevice_service.h | 8 +- .../common/include/miscdevice_service_proxy.h | 8 +- .../include/sensors_ipc_interface_code.h | 6 +- .../common/src/miscdevice_service_proxy.cpp | 10 +- frameworks/native/vibrator/BUILD.gn | 1 + .../include/vibrator_service_client.h | 3 +- .../vibrator/src/vibrator_service_client.cpp | 10 +- frameworks/native/vibrator/vibrator_agent.cpp | 3 +- .../adapter/include/compatible_connection.h | 2 +- .../adapter/include/hdi_connection.h | 2 +- .../adapter/src/compatible_connection.cpp | 4 +- .../adapter/src/hdi_connection.cpp | 4 +- .../include/i_vibrator_hdi_connection.h | 11 +- .../include/vibrator_hdi_connection.h | 2 +- .../interface/src/vibrator_hdi_connection.cpp | 2 +- .../include/miscdevice_service.h | 4 +- .../src/miscdevice_service.cpp | 117 ++++++++++-------- .../src/miscdevice_service_stub.cpp | 5 +- .../src/vibrator_thread.cpp | 8 +- utils/common/include/vibrator_infos.h | 1 + utils/common/src/file_utils.cpp | 6 - .../interface/vibrator_decoder_creator.cpp | 26 +++- .../interface/vibrator_decoder_creator.h | 2 + 28 files changed, 183 insertions(+), 116 deletions(-) diff --git a/frameworks/js/napi/vibrator/include/vibrator_napi_error.h b/frameworks/js/napi/vibrator/include/vibrator_napi_error.h index acf5e97..0e3f83e 100644 --- a/frameworks/js/napi/vibrator/include/vibrator_napi_error.h +++ b/frameworks/js/napi/vibrator/include/vibrator_napi_error.h @@ -34,6 +34,6 @@ const std::map ERROR_MESSAGES = { napi_value CreateBusinessError(const napi_env &env, const int32_t errCode, const std::string &errMessage); void ThrowErr(const napi_env &env, const int32_t errCode, const std::string &printMsg); std::optional GetNapiError(int32_t errorCode); -} // namespace Sensors -} // namespace OHOS +} // namespace Sensors +} // namespace OHOS #endif // VIBRATOR_NAPI_ERROR_H \ No newline at end of file diff --git a/frameworks/js/napi/vibrator/include/vibrator_napi_utils.h b/frameworks/js/napi/vibrator/include/vibrator_napi_utils.h index 8ccf665..0a86075 100644 --- a/frameworks/js/napi/vibrator/include/vibrator_napi_utils.h +++ b/frameworks/js/napi/vibrator/include/vibrator_napi_utils.h @@ -78,6 +78,6 @@ bool ConstructIsSupportEffectResult(const napi_env &env, sptr napi_value result[], int32_t length); void EmitAsyncCallbackWork(sptr async_callback_info); void EmitPromiseWork(sptr asyncCallbackInfo); -} // namespace Sensors -} // namespace OHOS +} // namespace Sensors +} // namespace OHOS #endif // VIBRATOR_NAPI_UTILS_H \ No newline at end of file diff --git a/frameworks/js/napi/vibrator/src/vibrator_js.cpp b/frameworks/js/napi/vibrator/src/vibrator_js.cpp index 4c931ac..dc0c49a 100644 --- a/frameworks/js/napi/vibrator/src/vibrator_js.cpp +++ b/frameworks/js/napi/vibrator/src/vibrator_js.cpp @@ -38,6 +38,7 @@ constexpr int32_t VIBRATE_SHORT_DURATION = 35; constexpr int32_t VIBRATE_LONG_DURATION = 1000; constexpr int32_t PARAMETER_TWO = 2; constexpr int32_t PARAMETER_THREE = 3; +constexpr int32_t INTENSITY_ADJUST_MAX = 100; } // namespace static std::map g_usageType = { @@ -61,6 +62,7 @@ struct VibrateInfo { int32_t fd = -1; int64_t offset = 0; int64_t length = -1; + int32_t intensity = 0; }; static napi_value EmitAsyncWork(napi_value param, sptr info) @@ -180,8 +182,13 @@ bool ParseParameter(napi_env env, napi_value args[], size_t argc, VibrateInfo &i if (info.type == "time") { CHKCF(GetPropertyInt32(env, args[0], "duration", info.duration), "Get vibrate duration fail"); } else if (info.type == "preset") { - CHKCF(GetPropertyInt32(env, args[0], "count", info.count), "Get vibrate count fail"); CHKCF(GetPropertyString(env, args[0], "effectId", info.effectId), "Get vibrate effectId fail"); + if (!GetPropertyInt32(env, args[0], "count", info.count)) { + info.count = 1; + } + if (!GetPropertyInt32(env, args[0], "intensity", info.intensity)) { + info.intensity = INTENSITY_ADJUST_MAX; + } } else if (info.type == "file") { napi_value hapticFd = nullptr; CHKCF(GetPropertyItem(env, args[0], "hapticFd", hapticFd), "Get vibrate hapticFd fail"); @@ -221,7 +228,7 @@ int32_t StartVibrate(const VibrateInfo &info) MISC_HILOGE("SetLoopCount fail"); return PARAMETER_ERROR; } - return StartVibrator(info.effectId.c_str()); + return PlayPrimitiveEffect(info.effectId.c_str(), info.intensity); } else if (info.type == "file") { return PlayVibratorCustom(info.fd, info.offset, info.length); } @@ -472,6 +479,27 @@ static napi_value CreateEnumEffectId(const napi_env env, const napi_value export return exports; } +static napi_value CreateEnumHapticFeedback(const napi_env env, napi_value exports) +{ + napi_value effectSoft = nullptr; + napi_value effectHard = nullptr; + napi_value effectSharp = nullptr; + NAPI_CALL(env, napi_create_string_utf8(env, "haptic.effect.soft", NAPI_AUTO_LENGTH, &effectSoft)); + NAPI_CALL(env, napi_create_string_utf8(env, "haptic.effect.hard", NAPI_AUTO_LENGTH, &effectHard)); + NAPI_CALL(env, napi_create_string_utf8(env, "haptic.effect.sharp", NAPI_AUTO_LENGTH, &effectSharp)); + + napi_property_descriptor desc[] = { + DECLARE_NAPI_STATIC_PROPERTY("EFFECT_SOFT", effectSoft), + DECLARE_NAPI_STATIC_PROPERTY("EFFECT_HARD", effectHard), + DECLARE_NAPI_STATIC_PROPERTY("EFFECT_SHARP", effectSharp), + }; + napi_value result = nullptr; + NAPI_CALL(env, napi_define_class(env, "HapticFeedback", NAPI_AUTO_LENGTH, EnumClassConstructor, nullptr, + sizeof(desc) / sizeof(*desc), desc, &result)); + NAPI_CALL(env, napi_set_named_property(env, exports, "HapticFeedback", result)); + return exports; +} + static napi_value Init(napi_env env, napi_value exports) { napi_property_descriptor desc[] = { @@ -487,6 +515,8 @@ static napi_value Init(napi_env env, napi_value exports) NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(napi_property_descriptor), desc)); NAPI_ASSERT_BASE(env, CreateEnumStopMode(env, exports) != nullptr, "Create enum stop mode fail", exports); NAPI_ASSERT_BASE(env, CreateEnumEffectId(env, exports) != nullptr, "Create enum effect id fail", exports); + NAPI_ASSERT_BASE(env, CreateEnumHapticFeedback(env, exports) != nullptr, "Create enum haptic feedback fail", + exports); return exports; } @@ -504,5 +534,5 @@ extern "C" __attribute__((constructor)) void RegisterModule(void) { napi_module_register(&_module); } -} // namespace Sensors -} // namespace OHOS +} // namespace Sensors +} // namespace OHOS diff --git a/frameworks/js/napi/vibrator/src/vibrator_napi_error.cpp b/frameworks/js/napi/vibrator/src/vibrator_napi_error.cpp index e58bcf5..cdc963d 100644 --- a/frameworks/js/napi/vibrator/src/vibrator_napi_error.cpp +++ b/frameworks/js/napi/vibrator/src/vibrator_napi_error.cpp @@ -57,5 +57,5 @@ void ThrowErr(const napi_env &env, const int32_t errCode, const std::string &pri napi_throw(env, error); napi_close_handle_scope(env, scope); } -} // namespace Sensors -} // namespace OHOS \ No newline at end of file +} // namespace Sensors +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/js/napi/vibrator/src/vibrator_napi_utils.cpp b/frameworks/js/napi/vibrator/src/vibrator_napi_utils.cpp index 27a16dd..738462b 100644 --- a/frameworks/js/napi/vibrator/src/vibrator_napi_utils.cpp +++ b/frameworks/js/napi/vibrator/src/vibrator_napi_utils.cpp @@ -351,5 +351,5 @@ void EmitPromiseWork(sptr asyncCallbackInfo) asyncCallbackInfo->DecStrongRef(nullptr); } } -} // namespace Sensors -} // namespace OHOS +} // namespace Sensors +} // namespace OHOS diff --git a/frameworks/native/common/include/i_miscdevice_service.h b/frameworks/native/common/include/i_miscdevice_service.h index 37160e9..2c69c91 100644 --- a/frameworks/native/common/include/i_miscdevice_service.h +++ b/frameworks/native/common/include/i_miscdevice_service.h @@ -53,9 +53,9 @@ public: virtual int32_t PlayPattern(const VibratePattern &pattern, int32_t usage, const VibrateParameter ¶meter) = 0; virtual int32_t TransferClientRemoteObject(const sptr &vibratorClient) = 0; virtual int32_t PlayPrimitiveEffect(int32_t vibratorId, const std::string &effect, int32_t intensity, - int32_t usage) = 0; + int32_t usage, int32_t count) = 0; virtual int32_t GetVibratorCapacity(VibratorCapacity &capacity) = 0; }; -} // namespace Sensors -} // namespace OHOS -#endif // I_MISCDEVICE_SERVICE_H +} // namespace Sensors +} // namespace OHOS +#endif // I_MISCDEVICE_SERVICE_H diff --git a/frameworks/native/common/include/miscdevice_service_proxy.h b/frameworks/native/common/include/miscdevice_service_proxy.h index 6533aa5..370e0a9 100644 --- a/frameworks/native/common/include/miscdevice_service_proxy.h +++ b/frameworks/native/common/include/miscdevice_service_proxy.h @@ -45,13 +45,13 @@ public: const VibrateParameter ¶meter) override; virtual int32_t TransferClientRemoteObject(const sptr &vibratorClient) override; virtual int32_t PlayPrimitiveEffect(int32_t vibratorId, const std::string &effect, int32_t intensity, - int32_t usage) override; + int32_t usage, int32_t count) override; virtual int32_t GetVibratorCapacity(VibratorCapacity &capacity) override; private: DISALLOW_COPY_AND_MOVE(MiscdeviceServiceProxy); static inline BrokerDelegator delegator_; }; -} // namespace Sensors -} // namespace OHOS -#endif // MISCDEVICE_SERVICE_PROXY_H +} // namespace Sensors +} // namespace OHOS +#endif // MISCDEVICE_SERVICE_PROXY_H diff --git a/frameworks/native/common/include/sensors_ipc_interface_code.h b/frameworks/native/common/include/sensors_ipc_interface_code.h index e25c66d..f6daaee 100644 --- a/frameworks/native/common/include/sensors_ipc_interface_code.h +++ b/frameworks/native/common/include/sensors_ipc_interface_code.h @@ -37,6 +37,6 @@ enum class MiscdeviceInterfaceCode { PLAY_PRIMITIVE_EFFECT, GET_VIBRATOR_CAPACITY, }; -} // namespace Sensors -} // namespace OHOS -#endif // SENSORS_IPC_INTERFACE_CODE_H +} // namespace Sensors +} // namespace OHOS +#endif // SENSORS_IPC_INTERFACE_CODE_H diff --git a/frameworks/native/common/src/miscdevice_service_proxy.cpp b/frameworks/native/common/src/miscdevice_service_proxy.cpp index 7caa4fe..7834bd0 100644 --- a/frameworks/native/common/src/miscdevice_service_proxy.cpp +++ b/frameworks/native/common/src/miscdevice_service_proxy.cpp @@ -418,7 +418,7 @@ int32_t MiscdeviceServiceProxy::TransferClientRemoteObject(const sptr remote = Remote(); CHKPR(remote, ERROR); MessageParcel reply; @@ -482,5 +486,5 @@ int32_t MiscdeviceServiceProxy::GetVibratorCapacity(VibratorCapacity &capacity) capacity = vibratorCapacity.value(); return ret; } -} // namespace Sensors -} // namespace OHOS +} // namespace Sensors +} // namespace OHOS diff --git a/frameworks/native/vibrator/BUILD.gn b/frameworks/native/vibrator/BUILD.gn index 7ba57c9..ed5f701 100644 --- a/frameworks/native/vibrator/BUILD.gn +++ b/frameworks/native/vibrator/BUILD.gn @@ -101,6 +101,7 @@ ohos_shared_library("libvibrator_native") { } external_deps = [ + "cJSON:cjson", "c_utils:utils", "hilog:libhilog", "hisysevent:libhisysevent", diff --git a/frameworks/native/vibrator/include/vibrator_service_client.h b/frameworks/native/vibrator/include/vibrator_service_client.h index 41755aa..d3ddf3f 100644 --- a/frameworks/native/vibrator/include/vibrator_service_client.h +++ b/frameworks/native/vibrator/include/vibrator_service_client.h @@ -69,7 +69,8 @@ public: int32_t GetDelayTime(int32_t &delayTime); int32_t PlayPattern(const VibratorPattern &pattern, int32_t usage, const VibratorParameter ¶meter); int32_t FreeVibratorPackage(VibratorPackage &package); - int32_t PlayPrimitiveEffect(int32_t vibratorId, const std::string &effect, int32_t intensity, int32_t usage); + int32_t PlayPrimitiveEffect(int32_t vibratorId, const std::string &effect, int32_t intensity, int32_t usage, + int32_t count); bool IsSupportVibratorCustom(); private: diff --git a/frameworks/native/vibrator/src/vibrator_service_client.cpp b/frameworks/native/vibrator/src/vibrator_service_client.cpp index a836656..d022d40 100644 --- a/frameworks/native/vibrator/src/vibrator_service_client.cpp +++ b/frameworks/native/vibrator/src/vibrator_service_client.cpp @@ -467,10 +467,10 @@ int32_t VibratorServiceClient::FreeVibratorPackage(VibratorPackage &package) } int32_t VibratorServiceClient::PlayPrimitiveEffect(int32_t vibratorId, const std::string &effect, int32_t intensity, - int32_t usage) + int32_t usage, int32_t count) { - MISC_HILOGD("Vibrate begin, effect:%{public}s, intensity:%{public}d, usage:%{public}d", - effect.c_str(), intensity, usage); + MISC_HILOGD("Vibrate begin, effect:%{public}s, intensity:%{public}d, usage:%{public}d, count:%{public}d", + effect.c_str(), intensity, usage, count); int32_t ret = InitServiceClient(); if (ret != ERR_OK) { MISC_HILOGE("InitServiceClient failed, ret:%{public}d", ret); @@ -478,11 +478,11 @@ int32_t VibratorServiceClient::PlayPrimitiveEffect(int32_t vibratorId, const std } CHKPR(miscdeviceProxy_, ERROR); StartTrace(HITRACE_TAG_SENSORS, "PlayPrimitiveEffect"); - ret = miscdeviceProxy_->PlayPrimitiveEffect(vibratorId, effect, intensity, usage); + ret = miscdeviceProxy_->PlayPrimitiveEffect(vibratorId, effect, intensity, usage, count); FinishTrace(HITRACE_TAG_SENSORS); if (ret != ERR_OK) { MISC_HILOGE("Play primitive effect failed, ret:%{public}d, effect:%{public}s, intensity:%{public}d," - "usage:%{public}d", ret, effect.c_str(), intensity, usage); + "usage:%{public}d, count:%{public}d", ret, effect.c_str(), intensity, usage, count); } return ret; } diff --git a/frameworks/native/vibrator/vibrator_agent.cpp b/frameworks/native/vibrator/vibrator_agent.cpp index 756df96..418c581 100644 --- a/frameworks/native/vibrator/vibrator_agent.cpp +++ b/frameworks/native/vibrator/vibrator_agent.cpp @@ -252,7 +252,8 @@ int32_t PlayPrimitiveEffect(const char *effectId, int32_t intensity) MISC_HILOGD("Time delay measurement:start time"); CHKPR(effectId, PARAMETER_ERROR); auto &client = VibratorServiceClient::GetInstance(); - int32_t ret = client.PlayPrimitiveEffect(DEFAULT_VIBRATOR_ID, effectId, intensity, g_usage); + int32_t ret = client.PlayPrimitiveEffect(DEFAULT_VIBRATOR_ID, effectId, intensity, g_usage, g_loopCount); + g_loopCount = 1; g_usage = USAGE_UNKNOWN; if (ret != ERR_OK) { MISC_HILOGE("Play primitive effect failed, ret:%{public}d", ret); diff --git a/services/miscdevice_service/hdi_connection/adapter/include/compatible_connection.h b/services/miscdevice_service/hdi_connection/adapter/include/compatible_connection.h index dae06a1..da6e5b5 100644 --- a/services/miscdevice_service/hdi_connection/adapter/include/compatible_connection.h +++ b/services/miscdevice_service/hdi_connection/adapter/include/compatible_connection.h @@ -35,7 +35,7 @@ public: bool IsVibratorRunning() override; #endif // OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM std::optional GetEffectInfo(const std::string &effect) override; - int32_t Stop(HdfVibratorMode mode) override; + int32_t Stop(HdfVibratorModeV1_2 mode) override; int32_t GetDelayTime(int32_t mode, int32_t &delayTime) override; int32_t GetVibratorCapacity(VibratorCapacity &capacity) override; int32_t PlayPattern(const VibratePattern &pattern) override; diff --git a/services/miscdevice_service/hdi_connection/adapter/include/hdi_connection.h b/services/miscdevice_service/hdi_connection/adapter/include/hdi_connection.h index 0241990..abdfc72 100644 --- a/services/miscdevice_service/hdi_connection/adapter/include/hdi_connection.h +++ b/services/miscdevice_service/hdi_connection/adapter/include/hdi_connection.h @@ -36,7 +36,7 @@ public: bool IsVibratorRunning() override; #endif // OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM std::optional GetEffectInfo(const std::string &effect) override; - int32_t Stop(HdfVibratorMode mode) override; + int32_t Stop(HdfVibratorModeV1_2 mode) override; int32_t GetDelayTime(int32_t mode, int32_t &delayTime) override; int32_t GetVibratorCapacity(VibratorCapacity &capacity) override; int32_t PlayPattern(const VibratePattern &pattern) override; diff --git a/services/miscdevice_service/hdi_connection/adapter/src/compatible_connection.cpp b/services/miscdevice_service/hdi_connection/adapter/src/compatible_connection.cpp index 676ff99..3ad529f 100644 --- a/services/miscdevice_service/hdi_connection/adapter/src/compatible_connection.cpp +++ b/services/miscdevice_service/hdi_connection/adapter/src/compatible_connection.cpp @@ -43,7 +43,7 @@ std::unordered_map g_vibratorEffect = { {"haptic.effect.soft", 30}, {"haptic.effect.sharp", 20} }; -HdfVibratorMode g_vibrateMode; +HdfVibratorModeV1_2 g_vibrateMode; constexpr int32_t VIBRATE_DELAY_TIME = 10; } // namespace int32_t CompatibleConnection::duration_ = 0; @@ -133,7 +133,7 @@ std::optional CompatibleConnection::GetEffectInfo(const std::stri return effectInfo; } -int32_t CompatibleConnection::Stop(HdfVibratorMode mode) +int32_t CompatibleConnection::Stop(HdfVibratorModeV1_2 mode) { CALL_LOG_ENTER; if (mode < 0 || mode >= HDF_VIBRATOR_MODE_BUTT) { diff --git a/services/miscdevice_service/hdi_connection/adapter/src/hdi_connection.cpp b/services/miscdevice_service/hdi_connection/adapter/src/hdi_connection.cpp index 20b48a5..6c5ee47 100644 --- a/services/miscdevice_service/hdi_connection/adapter/src/hdi_connection.cpp +++ b/services/miscdevice_service/hdi_connection/adapter/src/hdi_connection.cpp @@ -127,10 +127,10 @@ std::optional HdiConnection::GetEffectInfo(const std::string &eff return effectInfo; } -int32_t HdiConnection::Stop(HdfVibratorMode mode) +int32_t HdiConnection::Stop(HdfVibratorModeV1_2 mode) { CHKPR(vibratorInterface_, ERR_INVALID_VALUE); - int32_t ret = vibratorInterface_->Stop(mode); + int32_t ret = vibratorInterface_->StopV1_2(mode); if (ret < 0) { HiSysEventWrite(HiSysEvent::Domain::MISCDEVICE, "VIBRATOR_HDF_SERVICE_EXCEPTION", HiSysEvent::EventType::FAULT, "PKG_NAME", "Stop", "ERROR_CODE", ret); diff --git a/services/miscdevice_service/hdi_connection/interface/include/i_vibrator_hdi_connection.h b/services/miscdevice_service/hdi_connection/interface/include/i_vibrator_hdi_connection.h index a302d3f..09ad7a0 100644 --- a/services/miscdevice_service/hdi_connection/interface/include/i_vibrator_hdi_connection.h +++ b/services/miscdevice_service/hdi_connection/interface/include/i_vibrator_hdi_connection.h @@ -26,10 +26,11 @@ namespace OHOS { namespace Sensors { -using OHOS::HDI::Vibrator::V1_1::HdfVibratorMode; -using OHOS::HDI::Vibrator::V1_1::HDF_VIBRATOR_MODE_ONCE; -using OHOS::HDI::Vibrator::V1_1::HDF_VIBRATOR_MODE_PRESET; -using OHOS::HDI::Vibrator::V1_1::HDF_VIBRATOR_MODE_BUTT; +using OHOS::HDI::Vibrator::V1_2::HdfVibratorModeV1_2; +using OHOS::HDI::Vibrator::V1_2::HDF_VIBRATOR_MODE_ONCE; +using OHOS::HDI::Vibrator::V1_2::HDF_VIBRATOR_MODE_PRESET; +using OHOS::HDI::Vibrator::V1_2::HDF_VIBRATOR_MODE_HDHAPTIC; +using OHOS::HDI::Vibrator::V1_2::HDF_VIBRATOR_MODE_BUTT; using OHOS::HDI::Vibrator::V1_2::CurvePoint; using OHOS::HDI::Vibrator::V1_2::EVENT_TYPE; using OHOS::HDI::Vibrator::V1_2::HapticCapacity; @@ -58,7 +59,7 @@ public: virtual bool IsVibratorRunning() = 0; #endif // OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM virtual std::optional GetEffectInfo(const std::string &effect) = 0; - virtual int32_t Stop(HdfVibratorMode mode) = 0; + virtual int32_t Stop(HdfVibratorModeV1_2 mode) = 0; virtual int32_t DestroyHdiConnection() = 0; virtual int32_t GetDelayTime(int32_t mode, int32_t &delayTime) = 0; virtual int32_t GetVibratorCapacity(VibratorCapacity &capacity) = 0; diff --git a/services/miscdevice_service/hdi_connection/interface/include/vibrator_hdi_connection.h b/services/miscdevice_service/hdi_connection/interface/include/vibrator_hdi_connection.h index 655f109..938fb26 100644 --- a/services/miscdevice_service/hdi_connection/interface/include/vibrator_hdi_connection.h +++ b/services/miscdevice_service/hdi_connection/interface/include/vibrator_hdi_connection.h @@ -34,7 +34,7 @@ public: bool IsVibratorRunning() override; #endif // OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM std::optional GetEffectInfo(const std::string &effect) override; - int32_t Stop(HdfVibratorMode mode) override; + int32_t Stop(HdfVibratorModeV1_2 mode) override; int32_t DestroyHdiConnection() override; int32_t GetDelayTime(int32_t mode, int32_t &delayTime) override; int32_t GetVibratorCapacity(VibratorCapacity &capacity) override; diff --git a/services/miscdevice_service/hdi_connection/interface/src/vibrator_hdi_connection.cpp b/services/miscdevice_service/hdi_connection/interface/src/vibrator_hdi_connection.cpp index dd37250..015189e 100644 --- a/services/miscdevice_service/hdi_connection/interface/src/vibrator_hdi_connection.cpp +++ b/services/miscdevice_service/hdi_connection/interface/src/vibrator_hdi_connection.cpp @@ -105,7 +105,7 @@ std::optional VibratorHdiConnection::GetEffectInfo(const std::str return ret; } -int32_t VibratorHdiConnection::Stop(HdfVibratorMode mode) +int32_t VibratorHdiConnection::Stop(HdfVibratorModeV1_2 mode) { CHKPR(iVibratorHdiConnection_, VIBRATOR_HDF_CONNECT_ERR); StartTrace(HITRACE_TAG_SENSORS, "Stop"); diff --git a/services/miscdevice_service/include/miscdevice_service.h b/services/miscdevice_service/include/miscdevice_service.h index cfeb4f1..d1ef530 100644 --- a/services/miscdevice_service/include/miscdevice_service.h +++ b/services/miscdevice_service/include/miscdevice_service.h @@ -78,7 +78,7 @@ public: virtual int32_t GetDelayTime(int32_t &delayTime) override; virtual int32_t TransferClientRemoteObject(const sptr &vibratorServiceClient) override; virtual int32_t PlayPrimitiveEffect(int32_t vibratorId, const std::string &effect, int32_t intensity, - int32_t usage) override; + int32_t usage, int32_t count) override; virtual int32_t GetVibratorCapacity(VibratorCapacity &capacity) override; private: @@ -89,7 +89,7 @@ private: void StartVibrateThread(VibrateInfo info); void StopVibrateThread(); bool ShouldIgnoreVibrate(const VibrateInfo &info); - void VibrateCurrentTime(std::string &startTime); + std::string GetCurrentTime(); void MergeVibratorParmeters(const VibrateParameter ¶meter, VibratePackage &package); bool CheckVibratorParmeters(const VibrateParameter ¶meter); bool InitLightList(); diff --git a/services/miscdevice_service/src/miscdevice_service.cpp b/services/miscdevice_service/src/miscdevice_service.cpp index f976e12..c0090dd 100644 --- a/services/miscdevice_service/src/miscdevice_service.cpp +++ b/services/miscdevice_service/src/miscdevice_service.cpp @@ -33,6 +33,7 @@ #include "parameters.h" #include "default_vibrator_decoder.h" #include "default_vibrator_decoder_factory.h" +#include "vibrator_decoder_creator.h" #endif // OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM #undef LOG_TAG @@ -215,9 +216,6 @@ bool MiscdeviceService::ShouldIgnoreVibrate(const VibrateInfo &info) int32_t MiscdeviceService::Vibrate(int32_t vibratorId, int32_t timeOut, int32_t usage) { - std::string packageName = GetPackageName(GetCallingTokenID()); - MISC_HILOGD("Start vibrator time, time:%{public}d, usage:%{public}d, package:%{public}s", - timeOut, usage, packageName.c_str()); if ((timeOut <= MIN_VIBRATOR_TIME) || (timeOut > MAX_VIBRATOR_TIME) || (usage >= USAGE_MAX) || (usage < 0)) { MISC_HILOGE("Invalid parameter"); @@ -225,7 +223,7 @@ int32_t MiscdeviceService::Vibrate(int32_t vibratorId, int32_t timeOut, int32_t } VibrateInfo info = { .mode = VIBRATE_TIME, - .packageName = packageName, + .packageName = GetPackageName(GetCallingTokenID()), .pid = GetCallingPid(), .uid = GetCallingUid(), .usage = usage, @@ -236,19 +234,16 @@ int32_t MiscdeviceService::Vibrate(int32_t vibratorId, int32_t timeOut, int32_t MISC_HILOGE("Vibration is ignored and high priority is vibrating"); return ERROR; } - std::string curVibrateTime; StartVibrateThread(info); - VibrateCurrentTime(curVibrateTime); - MISC_HILOGI("Vibrate currentTime:%{public}s, pid:%{public}d, vibratorId:%{public}d," - "duration:%{public}d, package:%{public}s", curVibrateTime.c_str(), info.pid, vibratorId, - info.duration, packageName.c_str()); + std::string curVibrateTime = GetCurrentTime(); + MISC_HILOGI("Start vibrator, currentTime:%{public}s, package:%{public}s, pid:%{public}d, usage:%{public}d," + "vibratorId:%{public}d, duration:%{public}d", curVibrateTime.c_str(), info.packageName.c_str(), info.pid, + info.usage, vibratorId, info.duration); return NO_ERROR; } int32_t MiscdeviceService::StopVibrator(int32_t vibratorId) { - std::string packageName = GetPackageName(GetCallingTokenID()); - MISC_HILOGD("Stop vibrator, package:%{public}s", packageName.c_str()); std::lock_guard lock(vibratorThreadMutex_); #ifdef OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM if ((vibratorThread_ == nullptr) || (!vibratorThread_->IsRunning() && @@ -258,6 +253,7 @@ int32_t MiscdeviceService::StopVibrator(int32_t vibratorId) } if (vibratorHdiConnection_.IsVibratorRunning()) { vibratorHdiConnection_.Stop(HDF_VIBRATOR_MODE_PRESET); + vibratorHdiConnection_.Stop(HDF_VIBRATOR_MODE_HDHAPTIC); } #else if ((vibratorThread_ == nullptr) || (!vibratorThread_->IsRunning())) { @@ -266,15 +262,16 @@ int32_t MiscdeviceService::StopVibrator(int32_t vibratorId) } #endif // OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM StopVibrateThread(); + std::string packageName = GetPackageName(GetCallingTokenID()); + std::string curVibrateTime = GetCurrentTime(); + MISC_HILOGI("Stop vibrator, currentTime:%{public}s, package:%{public}s, pid:%{public}d, vibratorId:%{public}d", + curVibrateTime.c_str(), packageName.c_str(), GetCallingPid(), vibratorId); return NO_ERROR; } int32_t MiscdeviceService::PlayVibratorEffect(int32_t vibratorId, const std::string &effect, int32_t count, int32_t usage) { - std::string packageName = GetPackageName(GetCallingTokenID()); - MISC_HILOGD("Start vibrator effect, effect:%{public}s, count:%{public}d, usage:%{public}d, package:%{public}s", - effect.c_str(), count, usage, packageName.c_str()); if ((count < MIN_VIBRATOR_COUNT) || (count > MAX_VIBRATOR_COUNT) || (usage >= USAGE_MAX) || (usage < 0)) { MISC_HILOGE("Invalid parameter"); return PARAMETER_ERROR; @@ -290,24 +287,25 @@ int32_t MiscdeviceService::PlayVibratorEffect(int32_t vibratorId, const std::str } VibrateInfo info = { .mode = VIBRATE_PRESET, - .packageName = packageName, + .packageName = GetPackageName(GetCallingTokenID()), .pid = GetCallingPid(), .uid = GetCallingUid(), .usage = usage, .duration = effectInfo->duration, .effect = effect, - .count = count + .count = count, + .intensity = INTENSITY_ADJUST_MAX }; std::lock_guard lock(vibratorThreadMutex_); if (ShouldIgnoreVibrate(info)) { MISC_HILOGE("Vibration is ignored and high priority is vibrating"); return ERROR; } - std::string curEffectTime; - VibrateCurrentTime(curEffectTime); StartVibrateThread(info); - MISC_HILOGI("PlayVibratorEffect currentTime:%{public}s, pid:%{public}d, duration:%{public}d," - "package:%{public}s", curEffectTime.c_str(), info.pid, info.duration, packageName.c_str()); + std::string curVibrateTime = GetCurrentTime(); + MISC_HILOGI("Start vibrator, currentTime:%{public}s, package:%{public}s, pid:%{public}d, usage:%{public}d," + "vibratorId:%{public}d, duration:%{public}d, effect:%{public}s, count:%{public}d", curVibrateTime.c_str(), + info.packageName.c_str(), info.pid, info.usage, vibratorId, info.duration, info.effect.c_str(), info.count); return NO_ERROR; } @@ -320,6 +318,7 @@ void MiscdeviceService::StartVibrateThread(VibrateInfo info) #ifdef OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM if (vibratorHdiConnection_.IsVibratorRunning()) { vibratorHdiConnection_.Stop(HDF_VIBRATOR_MODE_PRESET); + vibratorHdiConnection_.Stop(HDF_VIBRATOR_MODE_HDHAPTIC); } #endif // OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM vibratorThread_->UpdateVibratorEffect(info); @@ -329,13 +328,7 @@ void MiscdeviceService::StartVibrateThread(VibrateInfo info) void MiscdeviceService::StopVibrateThread() { - std::string packageName = GetPackageName(GetCallingTokenID()); - MISC_HILOGD("Stop vibrator,package:%{public}s", packageName.c_str()); if ((vibratorThread_ != nullptr) && (vibratorThread_->IsRunning())) { - std::string curStopTime; - VibrateCurrentTime(curStopTime); - MISC_HILOGI("StopVibrateThread currentTime:%{public}s, pid:%{public}d, package:%{public}s", - curStopTime.c_str(), GetCallingPid(), packageName.c_str()); vibratorThread_->SetExitStatus(true); vibratorThread_->WakeUp(); vibratorThread_->NotifyExitSync(); @@ -345,8 +338,6 @@ void MiscdeviceService::StopVibrateThread() int32_t MiscdeviceService::StopVibrator(int32_t vibratorId, const std::string &mode) { - std::string packageName = GetPackageName(GetCallingTokenID()); - MISC_HILOGD("Stop vibrator, mode:%{public}s, package:%{public}s", mode.c_str(), packageName.c_str()); std::lock_guard lock(vibratorThreadMutex_); if ((vibratorThread_ == nullptr) || (!vibratorThread_->IsRunning())) { MISC_HILOGD("No vibration, no need to stop"); @@ -358,6 +349,10 @@ int32_t MiscdeviceService::StopVibrator(int32_t vibratorId, const std::string &m return ERROR; } StopVibrateThread(); + std::string packageName = GetPackageName(GetCallingTokenID()); + std::string curVibrateTime = GetCurrentTime(); + MISC_HILOGI("Stop vibrator, currentTime:%{public}s, package:%{public}s, pid:%{public}d, vibratorId:%{public}d," + "mode:%{public}s", curVibrateTime.c_str(), packageName.c_str(), GetCallingPid(), vibratorId, mode.c_str()); return NO_ERROR; } @@ -372,25 +367,28 @@ int32_t MiscdeviceService::IsSupportEffect(const std::string &effect, bool &stat return NO_ERROR; } -void MiscdeviceService::VibrateCurrentTime(std::string &startTime) +std::string MiscdeviceService::GetCurrentTime() { timespec curTime; clock_gettime(CLOCK_REALTIME, &curTime); struct tm *timeinfo = localtime(&(curTime.tv_sec)); - CHKPV(timeinfo); - startTime.append(std::to_string(timeinfo->tm_year + BASE_YEAR)).append("-") + std::string currentTime; + if (timeinfo == nullptr) { + MISC_HILOGE("timeinfo is null"); + return currentTime; + } + currentTime.append(std::to_string(timeinfo->tm_year + BASE_YEAR)).append("-") .append(std::to_string(timeinfo->tm_mon + BASE_MON)).append("-").append(std::to_string(timeinfo->tm_mday)) .append(" ").append(std::to_string(timeinfo->tm_hour)).append(":").append(std::to_string(timeinfo->tm_min)) .append(":").append(std::to_string(timeinfo->tm_sec)).append(".") .append(std::to_string(curTime.tv_nsec / (CONVERSION_RATE * CONVERSION_RATE))); + return currentTime; } #ifdef OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM int32_t MiscdeviceService::PlayVibratorCustom(int32_t vibratorId, const RawFileDescriptor &rawFd, int32_t usage, const VibrateParameter ¶meter) { - std::string packageName = GetPackageName(GetCallingTokenID()); - MISC_HILOGD("Start vibrator custom, usage:%{public}d, package:%{public}s", usage, packageName.c_str()); if (!(g_capacity.isSupportHdHaptic || g_capacity.isSupportPresetMapping || g_capacity.isSupportTimeDelay)) { MISC_HILOGE("The device does not support this operation"); return IS_NOT_SUPPORTED; @@ -399,8 +397,9 @@ int32_t MiscdeviceService::PlayVibratorCustom(int32_t vibratorId, const RawFileD MISC_HILOGE("Invalid parameter, usage:%{public}d", usage); return PARAMETER_ERROR; } - std::unique_ptr decoderFactory = std::make_unique(); - std::unique_ptr decoder(decoderFactory->CreateDecoder()); + VibratorDecoderCreator creator; + std::unique_ptr decoder(creator.CreateDecoder(rawFd)); + CHKPR(decoder, ERROR); VibratePackage package; int32_t ret = decoder->DecodeEffect(rawFd, package); if (ret != SUCCESS || package.patterns.empty()) { @@ -410,23 +409,29 @@ int32_t MiscdeviceService::PlayVibratorCustom(int32_t vibratorId, const RawFileD MergeVibratorParmeters(parameter, package); package.Dump(); VibrateInfo info = { - .mode = VIBRATE_CUSTOM_COMPOSITE_EFFECT, - .packageName = packageName, + .packageName = GetPackageName(GetCallingTokenID()), .pid = GetCallingPid(), .uid = GetCallingUid(), .usage = usage, .package = package, }; + if (g_capacity.isSupportHdHaptic) { + info.mode = VIBRATE_CUSTOM_HD; + } else if (g_capacity.isSupportPresetMapping) { + info.mode = VIBRATE_CUSTOM_COMPOSITE_EFFECT; + } else if (g_capacity.isSupportTimeDelay) { + info.mode = VIBRATE_CUSTOM_COMPOSITE_TIME; + } std::lock_guard lock(vibratorThreadMutex_); if (ShouldIgnoreVibrate(info)) { MISC_HILOGE("Vibration is ignored and high priority is vibrating"); return ERROR; } - std::string curCustomTime; - VibrateCurrentTime(curCustomTime); StartVibrateThread(info); - MISC_HILOGI("PlayVibratorCustom currentTime:%{public}s, pid:%{public}d, duration:%{public}d," - "package:%{public}s", curCustomTime.c_str(), info.pid, package.packageDuration, packageName.c_str()); + std::string curVibrateTime = GetCurrentTime(); + MISC_HILOGI("Start vibrator, currentTime:%{public}s, package:%{public}s, pid:%{public}d, usage:%{public}d," + "vibratorId:%{public}d, duration:%{public}d", curVibrateTime.c_str(), info.packageName.c_str(), info.pid, + info.usage, vibratorId, package.packageDuration); return NO_ERROR; } #endif // OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM @@ -545,8 +550,6 @@ int32_t MiscdeviceService::Dump(int32_t fd, const std::vector &a int32_t MiscdeviceService::PlayPattern(const VibratePattern &pattern, int32_t usage, const VibrateParameter ¶meter) { - std::string packageName = GetPackageName(GetCallingTokenID()); - MISC_HILOGD("Start vibrator pattern, usage:%{public}d, package:%{public}s", usage, packageName.c_str()); if ((usage >= USAGE_MAX) || (usage < 0) || (!CheckVibratorParmeters(parameter))) { MISC_HILOGE("Invalid parameter, usage:%{public}d", usage); return PARAMETER_ERROR; @@ -563,7 +566,7 @@ int32_t MiscdeviceService::PlayPattern(const VibratePattern &pattern, int32_t us package.Dump(); VibrateInfo info = { .mode = VIBRATE_BUTT, - .packageName = packageName, + .packageName = GetPackageName(GetCallingTokenID()), .pid = GetCallingPid(), .uid = GetCallingUid(), .usage = usage, @@ -587,11 +590,11 @@ int32_t MiscdeviceService::PlayPattern(const VibratePattern &pattern, int32_t us MISC_HILOGE("Vibration is ignored and high priority is vibrating"); return ERROR; } - std::string curPatternTime; - VibrateCurrentTime(curPatternTime); StartVibrateThread(info); - MISC_HILOGI("PlayVibratorCustom currentTime:%{public}s, pid:%{public}d, duration:%{public}d," - "package:%{public}s", curPatternTime.c_str(), info.pid, pattern.patternDuration, packageName.c_str()); + std::string curVibrateTime = GetCurrentTime(); + MISC_HILOGI("Start vibrator, currentTime:%{public}s, package:%{public}s, pid:%{public}d, usage:%{public}d," + "duration:%{public}d", curVibrateTime.c_str(), info.packageName.c_str(), info.pid, info.usage, + pattern.patternDuration); return ERR_OK; } @@ -747,11 +750,8 @@ void MiscdeviceService::DestroyClientPid(const sptr &vibratorServ } int32_t MiscdeviceService::PlayPrimitiveEffect(int32_t vibratorId, const std::string &effect, - int32_t intensity, int32_t usage) + int32_t intensity, int32_t usage, int32_t count) { - std::string packageName = GetPackageName(GetCallingTokenID()); - MISC_HILOGD("Start vibrator effect, effect:%{public}s, intensity:%{public}d, usage:%{public}d, package:%{public}s", - effect.c_str(), intensity, usage, packageName.c_str()); if ((intensity <= INTENSITY_MIN) || (intensity > INTENSITY_MAX) || (usage >= USAGE_MAX) || (usage < 0)) { MISC_HILOGE("Invalid parameter"); return PARAMETER_ERROR; @@ -766,12 +766,15 @@ int32_t MiscdeviceService::PlayPrimitiveEffect(int32_t vibratorId, const std::st return PARAMETER_ERROR; } VibrateInfo info = { - .mode = VIBRATE_BUTT, - .packageName = packageName, + .mode = VIBRATE_PRESET, + .packageName = GetPackageName(GetCallingTokenID()), .pid = GetCallingPid(), .uid = GetCallingUid(), .usage = usage, + .duration = effectInfo->duration, .effect = effect, + .count = count, + .intensity = intensity }; std::lock_guard lock(vibratorThreadMutex_); if (ShouldIgnoreVibrate(info)) { @@ -779,7 +782,11 @@ int32_t MiscdeviceService::PlayPrimitiveEffect(int32_t vibratorId, const std::st return ERROR; } StartVibrateThread(info); - return vibratorHdiConnection_.StartByIntensity(effect, intensity); + std::string curVibrateTime = GetCurrentTime(); + MISC_HILOGI("Start vibrator, currentTime:%{public}s, package:%{public}s, pid:%{public}d, usage:%{public}d," + "vibratorId:%{public}d, duration:%{public}d, effect:%{public}s", curVibrateTime.c_str(), + info.packageName.c_str(), info.pid, info.usage, vibratorId, info.duration, info.effect.c_str()); + return NO_ERROR; } int32_t MiscdeviceService::GetVibratorCapacity(VibratorCapacity &capacity) diff --git a/services/miscdevice_service/src/miscdevice_service_stub.cpp b/services/miscdevice_service/src/miscdevice_service_stub.cpp index e3dde86..2e4958e 100644 --- a/services/miscdevice_service/src/miscdevice_service_stub.cpp +++ b/services/miscdevice_service/src/miscdevice_service_stub.cpp @@ -370,12 +370,13 @@ int32_t MiscdeviceServiceStub::PlayPrimitiveEffectStub(MessageParcel &data, Mess std::string effect; int32_t intensity = 0; int32_t usage = 0; + int32_t count = 0; if ((!data.ReadInt32(vibratorId)) || (!data.ReadString(effect)) || - (!data.ReadInt32(intensity)) || (!data.ReadInt32(usage))) { + (!data.ReadInt32(intensity)) || (!data.ReadInt32(usage)) || (!data.ReadInt32(count))) { MISC_HILOGE("Parcel read failed"); return ERROR; } - return PlayPrimitiveEffect(vibratorId, effect, intensity, usage); + return PlayPrimitiveEffect(vibratorId, effect, intensity, usage, count); } int32_t MiscdeviceServiceStub::GetVibratorCapacityStub(MessageParcel &data, MessageParcel &reply) diff --git a/services/miscdevice_service/src/vibrator_thread.cpp b/services/miscdevice_service/src/vibrator_thread.cpp index aba3a27..3ea67a2 100644 --- a/services/miscdevice_service/src/vibrator_thread.cpp +++ b/services/miscdevice_service/src/vibrator_thread.cpp @@ -85,7 +85,7 @@ int32_t VibratorThread::PlayEffect(const VibrateInfo &info) std::unique_lock vibrateLck(vibrateMutex_); for (int32_t i = 0; i < info.count; ++i) { std::string effect = info.effect; - int32_t ret = VibratorDevice.Start(effect); + int32_t ret = VibratorDevice.StartByIntensity(effect, info.intensity); if (ret != SUCCESS) { MISC_HILOGE("Vibrate effect %{public}s failed, ", effect.c_str()); return ERROR; @@ -114,9 +114,15 @@ int32_t VibratorThread::PlayCustomByHdHptic(const VibrateInfo &info) } cv_.wait_for(vibrateLck, std::chrono::milliseconds(delayTime), [this] { return exitFlag_.load(); }); if (exitFlag_) { + VibratorDevice.Stop(HDF_VIBRATOR_MODE_HDHAPTIC); MISC_HILOGD("Stop hd haptic, package:%{public}s", info.packageName.c_str()); return SUCCESS; } + int32_t ret = VibratorDevice.PlayPattern(patterns[i]); + if (ret != SUCCESS) { + MISC_HILOGE("Vibrate hd haptic failed"); + return ERROR; + } } return SUCCESS; } diff --git a/utils/common/include/vibrator_infos.h b/utils/common/include/vibrator_infos.h index 6e7eb92..a0e576f 100644 --- a/utils/common/include/vibrator_infos.h +++ b/utils/common/include/vibrator_infos.h @@ -127,6 +127,7 @@ struct VibrateInfo { int32_t duration = 0; std::string effect; int32_t count = 0; + int32_t intensity = 0; VibratePackage package; }; diff --git a/utils/common/src/file_utils.cpp b/utils/common/src/file_utils.cpp index 282855b..e8c307d 100755 --- a/utils/common/src/file_utils.cpp +++ b/utils/common/src/file_utils.cpp @@ -198,9 +198,6 @@ std::string ReadFd(const RawFileDescriptor &rawFd) CHKPS(fp); if (fseek(fp, rawFd.offset, SEEK_SET) != 0) { MISC_HILOGE("fseek failed, errno:%{public}d", errno); - if (fclose(fp) != 0) { - MISC_HILOGW("Close file failed, errno:%{public}d", errno); - } return {}; } std::string dataStr; @@ -212,9 +209,6 @@ std::string ReadFd(const RawFileDescriptor &rawFd) dataStr += buf; alreadyRead = ftell(fp) - rawFd.offset; } - if (fclose(fp) != 0) { - MISC_HILOGW("Close file failed after read, errno:%{public}d", errno); - } return dataStr; } diff --git a/utils/haptic_decoder/interface/vibrator_decoder_creator.cpp b/utils/haptic_decoder/interface/vibrator_decoder_creator.cpp index e8db461..d711563 100644 --- a/utils/haptic_decoder/interface/vibrator_decoder_creator.cpp +++ b/utils/haptic_decoder/interface/vibrator_decoder_creator.cpp @@ -27,21 +27,33 @@ namespace OHOS { namespace Sensors { - +namespace { +const std::string JSON_TAG = "Channels"; +} // namespace IVibratorDecoder *VibratorDecoderCreator::CreateDecoder(const RawFileDescriptor &fd) { CALL_LOG_ENTER; DecoderType type = GetDecoderType(fd); if (type == DECODER_TYPE_HE) { - MISC_HILOGD("Get he decoder"); + MISC_HILOGD("Get he type"); HEVibratorDecoderFactory factory; return factory.CreateDecoder(); } else if (type == DECODER_TYPE_OH_JSON) { - MISC_HILOGD("Get oh_json decoder"); + MISC_HILOGD("Get oh_json type"); DefaultVibratorDecoderFactory factory; return factory.CreateDecoder(); } - MISC_HILOGE("Invalid decoder type"); + JsonParser parser(fd); + if (CheckJsonMetadata(parser)) { + MISC_HILOGD("Get oh_json tag"); + DefaultVibratorDecoderFactory factory; + return factory.CreateDecoder(); + } else { + MISC_HILOGD("Get he tag"); + HEVibratorDecoderFactory factory; + return factory.CreateDecoder(); + } + MISC_HILOGE("Create decoder fail"); return nullptr; } @@ -63,6 +75,12 @@ DecoderType VibratorDecoderCreator::GetDecoderType(const RawFileDescriptor &rawF } } +bool VibratorDecoderCreator::CheckJsonMetadata(const JsonParser &parser) +{ + cJSON *channelItem = parser.GetObjectItem(JSON_TAG); + return channelItem != nullptr; +} + extern "C" IVibratorDecoder *Create(const RawFileDescriptor &rawFd) { VibratorDecoderCreator creator; diff --git a/utils/haptic_decoder/interface/vibrator_decoder_creator.h b/utils/haptic_decoder/interface/vibrator_decoder_creator.h index cf6937e..708c01e 100644 --- a/utils/haptic_decoder/interface/vibrator_decoder_creator.h +++ b/utils/haptic_decoder/interface/vibrator_decoder_creator.h @@ -17,6 +17,7 @@ #define VIBRATOR_DECODER_MANAGER_H #include "i_vibrator_decoder.h" +#include "json_parser.h" #include "raw_file_descriptor.h" #include "vibrator_infos.h" @@ -35,6 +36,7 @@ public: IVibratorDecoder *CreateDecoder(const RawFileDescriptor &rawFd); private: + bool CheckJsonMetadata(const JsonParser &parser); DecoderType GetDecoderType(const RawFileDescriptor &rawFd); }; } // namespace Sensors -- Gitee