diff --git a/frameworks/native/miscdevice/BUILD.gn b/frameworks/native/miscdevice/BUILD.gn new file mode 100755 index 0000000000000000000000000000000000000000..cf8bbe7ceb4e21fb1a4131f3321e3b4122a8e2dd --- /dev/null +++ b/frameworks/native/miscdevice/BUILD.gn @@ -0,0 +1,86 @@ +# Copyright (c) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") + +SUBSYSTEM_DIR = "//base/sensors" + +############################################## +ohos_shared_library("liblight_native") { + sources = [ + "src/light_service_client.cpp", + "src/miscdevice_service_proxy.cpp", + ] + + include_dirs = [ + "include", + "//utils/native/base/include", + "//utils/system/safwk/native/include", + "$SUBSYSTEM_DIR/miscdevice/utils/include", + "//utils/jni/jnikit/include", + ] + cflags = [ "-fstack-protector-all" ] + deps = [ + "$SUBSYSTEM_DIR/miscdevice/services/miscdevice_service:libmiscdevice_service", + "$SUBSYSTEM_DIR/miscdevice/utils:libmiscdevice_utils", + "//utils/native/base:utils", + ] + + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr_L2:samgr_proxy", + ] + part_name = "sensors_miscdevice" + subsystem_name = "sensors" +} + +############################################## +ohos_shared_library("libvibrator_native") { + sources = [ + "src/miscdevice_service_proxy.cpp", + "src/vibrator_service_client.cpp", + ] + + include_dirs = [ + "include", + "//utils/native/base/include", + "//utils/system/safwk/native/include", + "$SUBSYSTEM_DIR/miscdevice/utils/include", + "//utils/jni/jnikit/include", + "//foundation/communication/ipc/interfaces/innerkits/ipc_core/include", + ] + deps = [ + "$SUBSYSTEM_DIR/miscdevice/utils:libmiscdevice_utils", + "//foundation/communication/ipc/interfaces/innerkits/ipc_core:ipc_core", + "//utils/native/base:utils", + ] + + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr_L2:samgr_proxy", + ] + part_name = "sensors_miscdevice" + subsystem_name = "sensors" +} + +############################################## +group("miscdevice_native_target") { + deps = [ + ":liblight_native", + ":libvibrator_native", + ] +} diff --git a/frameworks/native/miscdevice/include/i_miscdevice_service.h b/frameworks/native/miscdevice/include/i_miscdevice_service.h new file mode 100755 index 0000000000000000000000000000000000000000..765d01d235bcfd58bf158448d0436265a9f76301 --- /dev/null +++ b/frameworks/native/miscdevice/include/i_miscdevice_service.h @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef I_MISCDEVICE_SERVICE_H +#define I_MISCDEVICE_SERVICE_H + +#include +#include + +#include "iremote_broker.h" +#include "miscdevice_common.h" + +namespace OHOS { +namespace Sensors { +class IMiscdeviceService : public IRemoteBroker { +public: + IMiscdeviceService() = default; + + virtual ~IMiscdeviceService() = default; + + DECLARE_INTERFACE_DESCRIPTOR(u"IMiscdeviceService"); + + virtual bool IsAbilityAvailable(MiscdeviceDeviceId groupID) = 0; + + virtual bool IsVibratorEffectAvailable(int32_t vibratorId, const std::string &effectType) = 0; + + virtual std::vector GetVibratorIdList() = 0; + + virtual int32_t Vibrate(int32_t vibratorId, uint32_t timeOut) = 0; + + virtual int32_t CancelVibrator(int32_t vibratorId) = 0; + + virtual int32_t PlayVibratorEffect(int32_t vibratorId, const std::string &effect, bool isLooping) = 0; + + virtual int32_t PlayCustomVibratorEffect(int32_t vibratorId, const std::vector &timing, + const std::vector &intensity, int32_t periodCount) = 0; + + virtual int32_t StopVibratorEffect(int32_t vibratorId, const std::string &effect) = 0; + + virtual int32_t SetVibratorParameter(int32_t vibratorId, const std::string &cmd) = 0; + + virtual std::string GetVibratorParameter(int32_t vibratorId, const std::string &cmd) = 0; + + virtual std::vector GetLightSupportId() = 0; + + virtual bool IsLightEffectSupport(int32_t lightId, const std::string &effectId) = 0; + + virtual int32_t Light(int32_t lightId, uint64_t brightness, uint32_t timeOn, uint32_t timeOff) = 0; + + virtual int32_t PlayLightEffect(int32_t lightId, const std::string &type) = 0; + + virtual int32_t StopLightEffect(int32_t lightId) = 0; + + enum { + IS_ABILITY_AVAILABLE = 0, + GET_VIBRATOR_ID_LIST, + IS_VIBRATOR_EFFECT_AVAILABLE, + VIBRATE, + CANCEL_VIBRATOR, + PLAY_VIBRATOR_EFFECT, + PLAY_CUSTOM_VIBRATOR_EFFECT, + STOP_VIBRATOR_EFFECT, + SET_VIBRATOR_PARA, + GET_VIBRATOR_PARA, + GET_LIGHT_SUPPORT_ID, + IS_LIGHT_EFFECT_SUPPORT, + LIGHT, + PLAY_LIGHT_EFFECT, + STOP_LIGHT_EFFECT, + }; +}; +} // namespace Sensors +} // namespace OHOS +#endif // I_MISCDEVICE_SERVICE_H diff --git a/frameworks/native/miscdevice/include/light_service_client.h b/frameworks/native/miscdevice/include/light_service_client.h new file mode 100755 index 0000000000000000000000000000000000000000..15eb964df56887407b231c5a60f35faf22cc3592 --- /dev/null +++ b/frameworks/native/miscdevice/include/light_service_client.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef LIGHT_SERVICE_CLIENT_H +#define LIGHT_SERVICE_CLIENT_H + +#include +#include "iremote_object.h" +#include "miscdevice_service_proxy.h" +#include "singleton.h" + +namespace OHOS { +namespace Sensors { +class LightServiceClient : public Singleton { +public: + std::vector GetLightIdList(); + bool IsLightEffectSupport(int32_t lightId, const std::string &effectId); + int32_t Light(int32_t lightId, uint64_t brightness, uint32_t timeOn, uint32_t timeOff); + int32_t PlayLightEffect(int32_t lightId, const std::string &type); + int32_t StopLightEffect(int32_t lightId); + void ProcessDeathObserver(const wptr &object); + +private: + int32_t InitServiceClient(); + sptr serviceDeathObserver_; + sptr miscdeviceProxy_; + std::mutex clientMutex_; +}; +} // namespace Sensors +} // namespace OHOS +#endif // LIGHT_SERVICE_CLIENT_H diff --git a/frameworks/native/miscdevice/include/miscdevice_service_proxy.h b/frameworks/native/miscdevice/include/miscdevice_service_proxy.h new file mode 100755 index 0000000000000000000000000000000000000000..01d812823635eb8880742dd1688e7bf55016e97d --- /dev/null +++ b/frameworks/native/miscdevice/include/miscdevice_service_proxy.h @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef MISCDEVICE_SERVICE_PROXY_H +#define MISCDEVICE_SERVICE_PROXY_H + +#include "i_miscdevice_service.h" +#include "iremote_proxy.h" +#include "nocopyable.h" + +namespace OHOS { +namespace Sensors { +class MiscdeviceServiceProxy : public IRemoteProxy { +public: + explicit MiscdeviceServiceProxy(const sptr &impl); + + ~MiscdeviceServiceProxy() = default; + + virtual bool IsAbilityAvailable(MiscdeviceDeviceId groupID) override; + + virtual bool IsVibratorEffectAvailable(int32_t vibratorId, const std::string &effectType) override; + + virtual std::vector GetVibratorIdList() override; + + virtual int32_t Vibrate(int32_t vibratorId, uint32_t timeOut) override; + + virtual int32_t CancelVibrator(int32_t vibratorId) override; + + virtual int32_t PlayVibratorEffect(int32_t vibratorId, const std::string &effect, bool isLooping) override; + + virtual int32_t PlayCustomVibratorEffect(int32_t vibratorId, const std::vector &timing, + const std::vector &intensity, int32_t periodCount) override; + + virtual int32_t StopVibratorEffect(int32_t vibratorId, const std::string &effect) override; + + virtual int32_t SetVibratorParameter(int32_t vibratorId, const std::string &cmd) override; + + virtual std::string GetVibratorParameter(int32_t vibratorId, const std::string &cmd) override; + + virtual std::vector GetLightSupportId() override; + + virtual bool IsLightEffectSupport(int32_t lightId, const std::string &effectId) override; + + virtual int32_t Light(int32_t lightId, uint64_t brightness, uint32_t timeOn, uint32_t timeOff) override; + + virtual int32_t PlayLightEffect(int32_t lightId, const std::string &type) override; + + virtual int32_t StopLightEffect(int32_t lightId) override; + +private: + DISALLOW_COPY_AND_MOVE(MiscdeviceServiceProxy); + static inline BrokerDelegator delegator_; +}; +} // namespace Sensors +} // namespace OHOS +#endif // MISCDEVICE_SERVICE_PROXY_H diff --git a/frameworks/native/miscdevice/include/vibrator_service_client.h b/frameworks/native/miscdevice/include/vibrator_service_client.h new file mode 100755 index 0000000000000000000000000000000000000000..207b59bf80dc41d366849b4eb42b6fe6796d659d --- /dev/null +++ b/frameworks/native/miscdevice/include/vibrator_service_client.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef VIBRATOR_SERVICE_CLIENT_H +#define VIBRATOR_SERVICE_CLIENT_H + +#include +#include "iremote_object.h" +#include "miscdevice_service_proxy.h" +#include "singleton.h" + +namespace OHOS { +namespace Sensors { +class VibratorServiceClient : public Singleton { +public: + std::vector GetVibratorIdList(); + bool IsVibratorEffectSupport(int32_t vibratorId, const std::string &effect); + int32_t Vibrate(int32_t vibratorId, uint32_t timeOut); + int32_t Vibrate(int32_t vibratorId, const std::string &effect, bool isLooping); + int32_t Vibrate(int32_t vibratorId, std::vector timing, std::vector intensity, + int32_t periodCount); + int32_t Stop(int32_t vibratorId, const std::string &type); + int32_t SetVibratorParameter(int32_t vibratorId, const std::string &cmd); + std::string GetVibratorParameter(int32_t vibratorId, const std::string &command); + void ProcessDeathObserver(const wptr &object); + +private: + int32_t InitServiceClient(); + sptr serviceDeathObserver_; + sptr miscdeviceProxy_; + std::mutex clientMutex_; +}; +} // namespace Sensors +} // namespace OHOS +#endif // VIBRATOR_SERVICE_CLIENT_H diff --git a/frameworks/native/miscdevice/src/light_service_client.cpp b/frameworks/native/miscdevice/src/light_service_client.cpp new file mode 100755 index 0000000000000000000000000000000000000000..6690169a10c2657d6b35b595612b001905bc26f5 --- /dev/null +++ b/frameworks/native/miscdevice/src/light_service_client.cpp @@ -0,0 +1,136 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "light_service_client.h" + +#include +#include "death_recipient_template.h" +#include "dmd_report.h" +#include "iservice_registry.h" +#include "sensors_errors.h" +#include "sensors_log_domain.h" +#include "system_ability_definition.h" + +namespace OHOS { +namespace Sensors { +using namespace OHOS::HiviewDFX; + +namespace { +constexpr HiLogLabel LABEL = { LOG_CORE, SensorsLogDomain::SENSOR_NATIVE, "LightServiceClient" }; +constexpr int32_t GET_SERVICE_MAX_COUNT = 30; +constexpr uint32_t WAIT_MS = 200; +} // namespace + +int32_t LightServiceClient::InitServiceClient() +{ + HiLog::Debug(LABEL, "%{public}s begin", __func__); + std::lock_guard clientLock(clientMutex_); + if (miscdeviceProxy_ != nullptr) { + HiLog::Debug(LABEL, "%{public}s already init", __func__); + return ERR_OK; + } + auto sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (sm == nullptr) { + HiLog::Error(LABEL, "%{public}s sm cannot be null", __func__); + return MISC_NATIVE_SAM_ERR; + } + int32_t retry = 0; + while (retry < GET_SERVICE_MAX_COUNT) { + miscdeviceProxy_ = iface_cast(sm->GetSystemAbility(MISCDEVICE_SERVICE_ABILITY_ID)); + if (miscdeviceProxy_ != nullptr) { + HiLog::Debug(LABEL, "%{public}s get service success, retry : %{public}d", __func__, retry); + serviceDeathObserver_ = new (std::nothrow) DeathRecipientTemplate(*const_cast(this)); + if (serviceDeathObserver_ != nullptr) { + miscdeviceProxy_->AsObject()->AddDeathRecipient(serviceDeathObserver_); + } + return ERR_OK; + } + HiLog::Warn(LABEL, "%{public}s get service failed, retry : %{public}d", __func__, retry); + std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_MS)); + retry++; + } + DmdReport::ReportException(MISC_SERVICE_EXCEPTION, "InitServiceClient", MISC_NATIVE_GET_SERVICE_ERR); + HiLog::Error(LABEL, "%{public}s get service failed", __func__); + return MISC_NATIVE_GET_SERVICE_ERR; +} + +std::vector LightServiceClient::GetLightIdList() +{ + HiLog::Debug(LABEL, "%{public}s begin", __func__); + int32_t ret = InitServiceClient(); + if (ret != ERR_OK) { + HiLog::Error(LABEL, "%{public}s InitServiceClient failed, ret : %{public}d", __func__, ret); + return {}; + } + return miscdeviceProxy_->GetLightSupportId(); +} // namespace Sensors + +bool LightServiceClient::IsLightEffectSupport(int32_t lightId, const std::string &effectId) +{ + HiLog::Debug(LABEL, "%{public}s begin", __func__); + int32_t ret = InitServiceClient(); + if (ret != ERR_OK) { + HiLog::Error(LABEL, "%{public}s InitServiceClient failed, ret : %{public}d", __func__, ret); + return false; + } + return miscdeviceProxy_->IsLightEffectSupport(lightId, effectId); +} + +int32_t LightServiceClient::Light(int32_t lightId, uint64_t brightness, uint32_t timeOn, uint32_t timeOff) +{ + HiLog::Debug(LABEL, "%{public}s begin", __func__); + int32_t ret = InitServiceClient(); + if (ret != ERR_OK) { + HiLog::Error(LABEL, "%{public}s InitServiceClient failed, ret : %{public}d", __func__, ret); + return SENSOR_NATIVE_GET_SERVICE_ERR; + } + return miscdeviceProxy_->Light(lightId, brightness, timeOn, timeOff); +} + +int32_t LightServiceClient::PlayLightEffect(int32_t lightId, const std::string &type) +{ + HiLog::Debug(LABEL, "%{public}s begin", __func__); + int32_t ret = InitServiceClient(); + if (ret != ERR_OK) { + HiLog::Error(LABEL, "%{public}s InitServiceClient failed, ret : %{public}d", __func__, ret); + return SENSOR_NATIVE_GET_SERVICE_ERR; + } + return miscdeviceProxy_->PlayLightEffect(lightId, type); +} + +int32_t LightServiceClient::StopLightEffect(int32_t lightId) +{ + HiLog::Debug(LABEL, "%{public}s begin", __func__); + int32_t ret = InitServiceClient(); + if (ret != ERR_OK) { + HiLog::Error(LABEL, "%{public}s InitServiceClient failed, ret : %{public}d", __func__, ret); + return SENSOR_NATIVE_GET_SERVICE_ERR; + } + return miscdeviceProxy_->StopLightEffect(lightId); +} + +void LightServiceClient::ProcessDeathObserver(const wptr &object) +{ + HiLog::Debug(LABEL, "%{public}s begin", __func__); + (void)object; + miscdeviceProxy_ = nullptr; + auto ret = InitServiceClient(); + if (ret != ERR_OK) { + HiLog::Error(LABEL, "%{public}s InitServiceClient failed, ret : %{public}d", __func__, ret); + return; + } +} +} // namespace Sensors +} // namespace OHOS diff --git a/frameworks/native/miscdevice/src/miscdevice_service_proxy.cpp b/frameworks/native/miscdevice/src/miscdevice_service_proxy.cpp new file mode 100755 index 0000000000000000000000000000000000000000..23567b743504ed139eae15fc1fed106999041c11 --- /dev/null +++ b/frameworks/native/miscdevice/src/miscdevice_service_proxy.cpp @@ -0,0 +1,416 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "miscdevice_service_proxy.h" + +#include "dmd_report.h" +#include "sensors_errors.h" +#include "sensors_log_domain.h" + +namespace OHOS { +namespace Sensors { +using namespace OHOS::HiviewDFX; + +namespace { +constexpr HiLogLabel LABEL = { LOG_CORE, SensorsLogDomain::MISCDEVICE_SERVICE, "MiscdeviceServiceProxy" }; +} + +MiscdeviceServiceProxy::MiscdeviceServiceProxy(const sptr &impl) : IRemoteProxy(impl) +{} + +bool MiscdeviceServiceProxy::IsAbilityAvailable(MiscdeviceDeviceId groupID) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(MiscdeviceServiceProxy::GetDescriptor())) { + HiLog::Error(LABEL, "%{public}s write descriptor failed", __func__); + return false; + } + if (!data.WriteUint32(static_cast(groupID))) { + HiLog::Error(LABEL, "%{public}s write groupID failed", __func__); + return false; + } + int32_t ret = Remote()->SendRequest(IS_ABILITY_AVAILABLE, data, reply, option); + if (ret != NO_ERROR) { + DmdReport::ReportException(MISC_SERVICE_IPC_EXCEPTION, "IsAbilityAvailable", ret); + HiLog::Error(LABEL, "%{public}s failed, ret : %{public}d", __func__, ret); + return false; + } + return reply.ReadBool(); +} + +bool MiscdeviceServiceProxy::IsVibratorEffectAvailable(int32_t vibratorId, const std::string &effectType) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(MiscdeviceServiceProxy::GetDescriptor())) { + HiLog::Error(LABEL, "%{public}s write descriptor failed", __func__); + return false; + } + if (!data.WriteInt32(vibratorId)) { + HiLog::Error(LABEL, "%{public}s WriteInt32 failed", __func__); + return false; + } + if (!data.WriteString(effectType)) { + HiLog::Error(LABEL, "%{public}s WriteString failed", __func__); + return false; + } + int32_t ret = Remote()->SendRequest(IS_VIBRATOR_EFFECT_AVAILABLE, data, reply, option); + if (ret != NO_ERROR) { + DmdReport::ReportException(MISC_SERVICE_IPC_EXCEPTION, "IsVibratorEffectAvailable", ret); + HiLog::Error(LABEL, "%{public}s failed, ret : %{public}d", __func__, ret); + return false; + } + return reply.ReadBool(); +} + +std::vector MiscdeviceServiceProxy::GetVibratorIdList() +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + std::vector idVec; + if (!data.WriteInterfaceToken(MiscdeviceServiceProxy::GetDescriptor())) { + HiLog::Error(LABEL, "%{public}s write descriptor failed", __func__); + return idVec; + } + int32_t ret = Remote()->SendRequest(GET_VIBRATOR_ID_LIST, data, reply, option); + if (ret != NO_ERROR) { + DmdReport::ReportException(MISC_SERVICE_IPC_EXCEPTION, "GetVibratorIdList", ret); + HiLog::Error(LABEL, "%{public}s failed, ret : %{public}d", __func__, ret); + return idVec; + } + uint32_t setCount = reply.ReadUint32(); + idVec.resize(setCount); + reply.ReadInt32Vector(&idVec); + return idVec; +} + +int32_t MiscdeviceServiceProxy::Vibrate(int32_t vibratorId, uint32_t timeOut) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(MiscdeviceServiceProxy::GetDescriptor())) { + HiLog::Error(LABEL, "%{public}s write descriptor failed", __func__); + return WRITE_MSG_ERR; + } + if (!data.WriteInt32(vibratorId)) { + HiLog::Error(LABEL, "%{public}s WriteInt32 vibratorId failed", __func__); + return WRITE_MSG_ERR; + } + if (!data.WriteUint32(timeOut)) { + HiLog::Error(LABEL, "%{public}s WriteUint32 timeOut failed", __func__); + return WRITE_MSG_ERR; + } + int32_t ret = Remote()->SendRequest(VIBRATE, data, reply, option); + if (ret != NO_ERROR) { + DmdReport::ReportException(MISC_SERVICE_IPC_EXCEPTION, "Vibrate", ret); + HiLog::Error(LABEL, "%{public}s ret : %{public}d", __func__, ret); + } + return ret; +} + +int32_t MiscdeviceServiceProxy::CancelVibrator(int32_t vibratorId) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(MiscdeviceServiceProxy::GetDescriptor())) { + HiLog::Error(LABEL, "%{public}s write descriptor failed", __func__); + return WRITE_MSG_ERR; + } + if (!data.WriteInt32(vibratorId)) { + HiLog::Error(LABEL, "%{public}s WriteInt32 failed", __func__); + return WRITE_MSG_ERR; + } + int32_t ret = Remote()->SendRequest(CANCEL_VIBRATOR, data, reply, option); + if (ret != NO_ERROR) { + DmdReport::ReportException(MISC_SERVICE_IPC_EXCEPTION, "CancelVibrator", ret); + HiLog::Error(LABEL, "%{public}s ret : %{public}d", __func__, ret); + } + return ret; +} + +int32_t MiscdeviceServiceProxy::PlayVibratorEffect(int32_t vibratorId, const std::string &effect, bool isLooping) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(MiscdeviceServiceProxy::GetDescriptor())) { + HiLog::Error(LABEL, "%{public}s write descriptor failed", __func__); + return WRITE_MSG_ERR; + } + if (!data.WriteInt32(vibratorId)) { + HiLog::Error(LABEL, "%{public}s WriteInt32 vibratorId failed", __func__); + return WRITE_MSG_ERR; + } + if (!data.WriteString(effect)) { + HiLog::Error(LABEL, "%{public}s WriteString effect failed", __func__); + return WRITE_MSG_ERR; + } + if (!data.WriteBool(isLooping)) { + HiLog::Error(LABEL, "%{public}s WriteBool effect failed", __func__); + return WRITE_MSG_ERR; + } + int32_t ret = Remote()->SendRequest(PLAY_VIBRATOR_EFFECT, data, reply, option); + if (ret != NO_ERROR) { + DmdReport::ReportException(MISC_SERVICE_IPC_EXCEPTION, "PlayVibratorEffect", ret); + HiLog::Error(LABEL, "%{public}s ret : %{public}d", __func__, ret); + } + return ret; +} + +int32_t MiscdeviceServiceProxy::PlayCustomVibratorEffect(int32_t vibratorId, const std::vector &timing, + const std::vector &intensity, int32_t periodCount) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(MiscdeviceServiceProxy::GetDescriptor())) { + HiLog::Error(LABEL, "%{public}s write descriptor failed", __func__); + return WRITE_MSG_ERR; + } + if (!data.WriteInt32(vibratorId)) { + HiLog::Error(LABEL, "%{public}s WriteInt32 vibratorId failed", __func__); + return WRITE_MSG_ERR; + } + if (!data.WriteInt32Vector(timing)) { + HiLog::Error(LABEL, "%{public}s WriteInt32Vector timing failed", __func__); + return WRITE_MSG_ERR; + } + if (!data.WriteInt32Vector(intensity)) { + HiLog::Error(LABEL, "%{public}s WriteInt32Vector intensity failed", __func__); + return WRITE_MSG_ERR; + } + if (!data.WriteInt32(periodCount)) { + HiLog::Error(LABEL, "%{public}s WriteInt32 periodCount failed", __func__); + return WRITE_MSG_ERR; + } + int32_t ret = Remote()->SendRequest(PLAY_CUSTOM_VIBRATOR_EFFECT, data, reply, option); + if (ret != NO_ERROR) { + DmdReport::ReportException(MISC_SERVICE_IPC_EXCEPTION, "PlayCustomVibratorEffect", ret); + HiLog::Error(LABEL, "%{public}s ret : %{public}d", __func__, ret); + } + return ret; +} + +int32_t MiscdeviceServiceProxy::StopVibratorEffect(int32_t vibratorId, const std::string &effect) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(MiscdeviceServiceProxy::GetDescriptor())) { + HiLog::Error(LABEL, "%{public}s write descriptor failed", __func__); + return WRITE_MSG_ERR; + } + if (!data.WriteInt32(vibratorId)) { + HiLog::Error(LABEL, "%{public}s WriteInt32 vibratorId failed", __func__); + return WRITE_MSG_ERR; + } + if (!data.WriteString(effect)) { + HiLog::Error(LABEL, "%{public}s WriteString effect failed", __func__); + return WRITE_MSG_ERR; + } + int32_t ret = Remote()->SendRequest(STOP_VIBRATOR_EFFECT, data, reply, option); + if (ret != NO_ERROR) { + DmdReport::ReportException(MISC_SERVICE_IPC_EXCEPTION, "StopVibratorEffect", ret); + HiLog::Error(LABEL, "%{public}s ret : %{public}d", __func__, ret); + } + return ret; +} + +int32_t MiscdeviceServiceProxy::SetVibratorParameter(int32_t vibratorId, const std::string &cmd) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(MiscdeviceServiceProxy::GetDescriptor())) { + HiLog::Error(LABEL, "%{public}s write descriptor failed", __func__); + return WRITE_MSG_ERR; + } + if (!data.WriteInt32(vibratorId)) { + HiLog::Error(LABEL, "%{public}s WriteInt32 vibratorId failed", __func__); + return WRITE_MSG_ERR; + } + if (!data.WriteString(cmd)) { + HiLog::Error(LABEL, "%{public}s WriteString cmd failed", __func__); + return WRITE_MSG_ERR; + } + int32_t ret = Remote()->SendRequest(SET_VIBRATOR_PARA, data, reply, option); + if (ret != NO_ERROR) { + DmdReport::ReportException(MISC_SERVICE_IPC_EXCEPTION, "SetVibratorParameter", ret); + HiLog::Error(LABEL, "%{public}s ret : %{public}d", __func__, ret); + } + return ret; +} + +std::string MiscdeviceServiceProxy::GetVibratorParameter(int32_t vibratorId, const std::string &cmd) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(MiscdeviceServiceProxy::GetDescriptor())) { + HiLog::Error(LABEL, "%{public}s write descriptor failed", __func__); + return ""; + } + if (!data.WriteInt32(vibratorId)) { + HiLog::Error(LABEL, "%{public}s WriteInt32 vibratorId failed", __func__); + return ""; + } + if (!data.WriteString(cmd)) { + HiLog::Error(LABEL, "%{public}s WriteString cmd failed", __func__); + return ""; + } + int32_t ret = Remote()->SendRequest(GET_VIBRATOR_PARA, data, reply, option); + if (ret != NO_ERROR) { + DmdReport::ReportException(MISC_SERVICE_IPC_EXCEPTION, "SetVibratorParameter", ret); + HiLog::Error(LABEL, "%{public}s ret : %{public}d", __func__, ret); + return ""; + } + return reply.ReadString(); +} + +std::vector MiscdeviceServiceProxy::GetLightSupportId() +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + std::vector idVec; + if (!data.WriteInterfaceToken(MiscdeviceServiceProxy::GetDescriptor())) { + HiLog::Error(LABEL, "%{public}s write descriptor failed", __func__); + return idVec; + } + int32_t ret = Remote()->SendRequest(GET_LIGHT_SUPPORT_ID, data, reply, option); + if (ret != NO_ERROR) { + DmdReport::ReportException(MISC_SERVICE_IPC_EXCEPTION, "GetLightSupportId", ret); + HiLog::Error(LABEL, "%{public}s failed, ret : %{public}d", __func__, ret); + } + int32_t setCount = reply.ReadInt32(); + idVec.resize(setCount); + reply.ReadInt32Vector(&idVec); + return idVec; +} + +bool MiscdeviceServiceProxy::IsLightEffectSupport(int32_t lightId, const std::string &effectId) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(MiscdeviceServiceProxy::GetDescriptor())) { + HiLog::Error(LABEL, "%{public}s write descriptor failed", __func__); + return false; + } + if (!data.WriteInt32(lightId)) { + HiLog::Error(LABEL, "%{public}s WriteInt32 lightId failed", __func__); + return false; + } + if (!data.WriteString(effectId)) { + HiLog::Error(LABEL, "%{public}s WriteString effectId failed", __func__); + return false; + } + + int32_t ret = Remote()->SendRequest(IS_LIGHT_EFFECT_SUPPORT, data, reply, option); + if (ret != NO_ERROR) { + DmdReport::ReportException(MISC_SERVICE_IPC_EXCEPTION, "IsLightEffectSupport", ret); + HiLog::Error(LABEL, "%{public}s failed, ret : %{public}d", __func__, ret); + return false; + } + return reply.ReadBool(); +} + +int32_t MiscdeviceServiceProxy::Light(int32_t id, uint64_t brightness, uint32_t timeOn, uint32_t timeOff) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(MiscdeviceServiceProxy::GetDescriptor())) { + HiLog::Error(LABEL, "%{public}s write descriptor failed", __func__); + return WRITE_MSG_ERR; + } + if (!data.WriteInt32(id)) { + HiLog::Error(LABEL, "%{public}s WriteInt32 id failed", __func__); + return WRITE_MSG_ERR; + } + if (!data.WriteUint64(brightness)) { + HiLog::Error(LABEL, "%{public}s WriteUint64 brightness failed", __func__); + return WRITE_MSG_ERR; + } + if (!data.WriteUint32(timeOn)) { + HiLog::Error(LABEL, "%{public}s WriteUint32 timeOn failed", __func__); + return WRITE_MSG_ERR; + } + if (!data.WriteUint32(timeOff)) { + HiLog::Error(LABEL, "%{public}s WriteUint32 timeOff failed", __func__); + return WRITE_MSG_ERR; + } + + int32_t ret = Remote()->SendRequest(LIGHT, data, reply, option); + if (ret != NO_ERROR) { + DmdReport::ReportException(MISC_SERVICE_IPC_EXCEPTION, "Light", ret); + HiLog::Error(LABEL, "%{public}s failed, ret : %{public}d", __func__, ret); + } + return ret; +} + +int32_t MiscdeviceServiceProxy::PlayLightEffect(int32_t id, const std::string &type) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(MiscdeviceServiceProxy::GetDescriptor())) { + HiLog::Error(LABEL, "%{public}s write descriptor failed", __func__); + return WRITE_MSG_ERR; + } + if (!data.WriteInt32(id)) { + HiLog::Error(LABEL, "%{public}s WriteInt32 id failed", __func__); + return WRITE_MSG_ERR; + } + if (!data.WriteString(type)) { + HiLog::Error(LABEL, "%{public}s WriteString type failed", __func__); + return WRITE_MSG_ERR; + } + int32_t ret = Remote()->SendRequest(PLAY_LIGHT_EFFECT, data, reply, option); + if (ret != NO_ERROR) { + DmdReport::ReportException(MISC_SERVICE_IPC_EXCEPTION, "PlayLightEffect", ret); + HiLog::Error(LABEL, "%{public}s failed, ret : %{public}d", __func__, ret); + } + return ret; +} + +int32_t MiscdeviceServiceProxy::StopLightEffect(int32_t id) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(MiscdeviceServiceProxy::GetDescriptor())) { + HiLog::Error(LABEL, "%{public}s write descriptor failed", __func__); + return WRITE_MSG_ERR; + } + if (!data.WriteInt32(id)) { + HiLog::Error(LABEL, "%{public}s WriteInt32 id failed", __func__); + return WRITE_MSG_ERR; + } + int32_t ret = Remote()->SendRequest(STOP_LIGHT_EFFECT, data, reply, option); + if (ret != NO_ERROR) { + DmdReport::ReportException(MISC_SERVICE_IPC_EXCEPTION, "StopLightEffect", ret); + HiLog::Error(LABEL, "%{public}s failed, ret : %{public}d", __func__, ret); + } + return ret; +} +} // namespace Sensors +} // namespace OHOS diff --git a/frameworks/native/miscdevice/src/vibrator_service_client.cpp b/frameworks/native/miscdevice/src/vibrator_service_client.cpp new file mode 100755 index 0000000000000000000000000000000000000000..adefcdd5856f6c517c853cfc3f7a838c4fdaf6f1 --- /dev/null +++ b/frameworks/native/miscdevice/src/vibrator_service_client.cpp @@ -0,0 +1,177 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "vibrator_service_client.h" + +#include +#include "death_recipient_template.h" +#include "dmd_report.h" +#include "iservice_registry.h" +#include "sensors_errors.h" +#include "sensors_log_domain.h" +#include "system_ability_definition.h" + +namespace OHOS { +namespace Sensors { +using namespace OHOS::HiviewDFX; + +namespace { +constexpr HiLogLabel LABEL = { LOG_CORE, SensorsLogDomain::SENSOR_NATIVE, "VibratorServiceClient" }; +constexpr int32_t GET_SERVICE_MAX_COUNT = 30; +constexpr uint32_t WAIT_MS = 200; +} // namespace + +int32_t VibratorServiceClient::InitServiceClient() +{ + HiLog::Debug(LABEL, "%{public}s begin", __func__); + std::lock_guard clientLock(clientMutex_); + if (miscdeviceProxy_ != nullptr) { + HiLog::Warn(LABEL, "%{public}s already init", __func__); + return ERR_OK; + } + auto sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (sm == nullptr) { + HiLog::Error(LABEL, "%{public}s sm cannot be null", __func__); + return MISC_NATIVE_SAM_ERR; + } + int32_t retry = 0; + while (retry < GET_SERVICE_MAX_COUNT) { + miscdeviceProxy_ = iface_cast(sm->GetSystemAbility(MISCDEVICE_SERVICE_ABILITY_ID)); + if (miscdeviceProxy_ != nullptr) { + HiLog::Debug(LABEL, "%{public}s get service success, retry : %{public}d", __func__, retry); + serviceDeathObserver_ = + new (std::nothrow) DeathRecipientTemplate(*const_cast(this)); + if (serviceDeathObserver_ != nullptr) { + miscdeviceProxy_->AsObject()->AddDeathRecipient(serviceDeathObserver_); + } + return ERR_OK; + } + HiLog::Warn(LABEL, "%{public}s get service failed, retry : %{public}d", __func__, retry); + std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_MS)); + retry++; + } + DmdReport::ReportException(MISC_SERVICE_EXCEPTION, "InitServiceClient", MISC_NATIVE_GET_SERVICE_ERR); + HiLog::Error(LABEL, "%{public}s get service failed", __func__); + return MISC_NATIVE_GET_SERVICE_ERR; +} + +std::vector VibratorServiceClient::GetVibratorIdList() +{ + HiLog::Debug(LABEL, "%{public}s begin", __func__); + int32_t ret = InitServiceClient(); + if (ret != ERR_OK) { + HiLog::Error(LABEL, "%{public}s InitServiceClient failed, ret : %{public}d", __func__, ret); + return {}; + } + return miscdeviceProxy_->GetVibratorIdList(); +} + +bool VibratorServiceClient::IsVibratorEffectSupport(int32_t vibratorId, const std::string &effect) +{ + HiLog::Debug(LABEL, "%{public}s begin", __func__); + int32_t ret = InitServiceClient(); + if (ret != ERR_OK) { + HiLog::Error(LABEL, "%{public}s InitServiceClient failed, ret : %{public}d", __func__, ret); + return false; + } + return miscdeviceProxy_->IsVibratorEffectAvailable(vibratorId, effect); +} + +int32_t VibratorServiceClient::Vibrate(int32_t vibratorId, uint32_t timeOut) +{ + HiLog::Debug(LABEL, "%{public}s begin, timeOut : %{public}u", __func__, timeOut); + int32_t ret = InitServiceClient(); + if (ret != ERR_OK) { + HiLog::Error(LABEL, "%{public}s InitServiceClient failed, ret : %{public}d", __func__, ret); + return MISC_NATIVE_GET_SERVICE_ERR; + } + return miscdeviceProxy_->Vibrate(vibratorId, timeOut); +} + +int32_t VibratorServiceClient::Vibrate(int32_t vibratorId, const std::string &effect, bool isLooping) +{ + HiLog::Debug(LABEL, "%{public}s begin, effect : %{public}s", __func__, effect.c_str()); + int32_t ret = InitServiceClient(); + if (ret != ERR_OK) { + HiLog::Error(LABEL, "%{public}s InitServiceClient failed, ret : %{public}d", __func__, ret); + return MISC_NATIVE_GET_SERVICE_ERR; + } + return miscdeviceProxy_->PlayVibratorEffect(vibratorId, effect, isLooping); +} + +int32_t VibratorServiceClient::Vibrate(int32_t vibratorId, std::vector timing, std::vector intensity, + int32_t periodCount) +{ + HiLog::Debug(LABEL, "%{public}s begin", __func__); + int32_t ret = InitServiceClient(); + if (ret != ERR_OK) { + HiLog::Error(LABEL, "%{public}s InitServiceClient failed, ret : %{public}d", __func__, ret); + return MISC_NATIVE_GET_SERVICE_ERR; + } + return miscdeviceProxy_->PlayCustomVibratorEffect(vibratorId, timing, intensity, periodCount); +} + +int32_t VibratorServiceClient::Stop(int32_t vibratorId, const std::string &type) +{ + HiLog::Debug(LABEL, "%{public}s begin, vibratorId : %{public}d, type : %{public}s", __func__, vibratorId, + type.c_str()); + int32_t ret = InitServiceClient(); + if (ret != ERR_OK) { + HiLog::Error(LABEL, "%{public}s InitServiceClient failed, ret : %{public}d", __func__, ret); + return MISC_NATIVE_GET_SERVICE_ERR; + } + if ((type == "customized") || (type == "time")) { + ret = miscdeviceProxy_->CancelVibrator(vibratorId); + } else { + ret = miscdeviceProxy_->StopVibratorEffect(vibratorId, type); + } + return ret; +} + +int32_t VibratorServiceClient::SetVibratorParameter(int32_t vibratorId, const std::string &cmd) +{ + HiLog::Debug(LABEL, "%{public}s begin", __func__); + int32_t ret = InitServiceClient(); + if (ret != ERR_OK) { + HiLog::Error(LABEL, "%{public}s InitServiceClient failed, ret : %{public}d", __func__, ret); + return MISC_NATIVE_GET_SERVICE_ERR; + } + return miscdeviceProxy_->SetVibratorParameter(vibratorId, cmd); +} + +std::string VibratorServiceClient::GetVibratorParameter(int32_t vibratorId, const std::string &command) +{ + HiLog::Debug(LABEL, "%{public}s begin", __func__); + int32_t ret = InitServiceClient(); + if (ret != ERR_OK) { + HiLog::Error(LABEL, "%{public}s InitServiceClient failed, ret : %{public}d", __func__, ret); + return {}; + } + return miscdeviceProxy_->GetVibratorParameter(vibratorId, command); +} + +void VibratorServiceClient::ProcessDeathObserver(const wptr &object) +{ + HiLog::Debug(LABEL, "%{public}s begin", __func__); + (void)object; + miscdeviceProxy_ = nullptr; + int32_t ret = InitServiceClient(); + if (ret != ERR_OK) { + HiLog::Error(LABEL, "%{public}s InitServiceClient failed, ret : %{public}d", __func__, ret); + return; + } +} +} // namespace Sensors +} // namespace OHOS diff --git a/frameworks/native/miscdevice/test/BUILD.gn b/frameworks/native/miscdevice/test/BUILD.gn new file mode 100755 index 0000000000000000000000000000000000000000..b0ee54618abb4b13b684a686c4475122f638c8b5 --- /dev/null +++ b/frameworks/native/miscdevice/test/BUILD.gn @@ -0,0 +1,25 @@ +# Copyright (c) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/test.gni") + +############################################################################### +group("unittest") { + testonly = true + if (is_phone_product || is_wearable_product) { + deps = [ "unittest/common:unittest" ] + } + if (is_phone_product) { + deps += [ "unittest/phone:unittest" ] + } +} diff --git a/frameworks/native/miscdevice/test/unittest/common/BUILD.gn b/frameworks/native/miscdevice/test/unittest/common/BUILD.gn new file mode 100755 index 0000000000000000000000000000000000000000..0829e15d9b058aa61cc00fcf818422c764771deb --- /dev/null +++ b/frameworks/native/miscdevice/test/unittest/common/BUILD.gn @@ -0,0 +1,82 @@ +# Copyright (c) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/test.gni") + +SUBSYSTEM_DIR = "//base/sensors" +module_output_path = "sensors/miscdevice/frameworks" + +####################################################### +ohos_unittest("LightNativeCommonTest") { + module_out_path = module_output_path + sources = [ "./light_native_test.cpp" ] + + include_dirs = [ + "../../../include", + "$SUBSYSTEM_DIR/miscdevice/utils/include", + "//utils/native/base/include", + ] + + deps = [ + "$SUBSYSTEM_DIR/miscdevice/frameworks/native/miscdevice:liblight_native", + "$SUBSYSTEM_DIR/miscdevice/utils:libmiscdevice_utils", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + "//utils/native/base:utils", + ] + + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr_L2:samgr_proxy", + ] +} + +####################################################### +ohos_unittest("VibratorNativeCommonTest") { + module_out_path = module_output_path + sources = [ "./vibrator_native_test.cpp" ] + + include_dirs = [ + "../../../include", + "$SUBSYSTEM_DIR/miscdevice/utils/include", + "//utils/native/base/include", + ] + + deps = [ + "$SUBSYSTEM_DIR/miscdevice/frameworks/native/miscdevice:libvibrator_native", + "$SUBSYSTEM_DIR/miscdevice/utils:libmiscdevice_utils", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + "//utils/native/base:utils", + ] + + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr_L2:samgr_proxy", + ] +} + +####################################################### +group("unittest") { + testonly = true + if (is_phone_product || is_wearable_product) { + deps = [ + ":LightNativeCommonTest", + ":VibratorNativeCommonTest", + ] + } +} diff --git a/frameworks/native/miscdevice/test/unittest/common/light_native_test.cpp b/frameworks/native/miscdevice/test/unittest/common/light_native_test.cpp new file mode 100755 index 0000000000000000000000000000000000000000..6f3f8b94c1c6cd24b171a389b2c12eac82a6b6a8 --- /dev/null +++ b/frameworks/native/miscdevice/test/unittest/common/light_native_test.cpp @@ -0,0 +1,132 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include "ipc_skeleton.h" +#include "iservice_registry.h" +#include "jni.h" + +#include "light_service_client.h" +#include "sensors_errors.h" +#include "sensors_log_domain.h" + +namespace OHOS { +namespace Sensors { +using namespace testing::ext; +using namespace OHOS::HiviewDFX; + +namespace { +constexpr HiLogLabel LABEL = { LOG_CORE, SensorsLogDomain::MISCDEVICE_LIGHT_NATIVE, "LightNativeTest" }; +} + +class LightNativeTest : public testing::Test { +public: + void SetUp(); + void TearDown(); + std::unique_ptr lightServiceClient_; +}; + +void LightNativeTest::SetUp() +{ + lightServiceClient_ = std::make_unique(); + ASSERT_NE(lightServiceClient_, nullptr); + HiLog::Info(LABEL, "%{public}s end", __func__); +} + +void LightNativeTest::TearDown() +{ + lightServiceClient_ = nullptr; +} + +/* + * @tc.name: GetLightIdList_001s + * @tc.desc: get light support id + * @tc.type: FUNC + */ +HWTEST_F(LightNativeTest, GetLightIdList_001, TestSize.Level1) +{ + std::vector setTarget; + setTarget.push_back(LightId::LIGHT_ID_LED); + std::map, int32_t> mapTarget; + mapTarget.insert(std::make_pair(setTarget, 1)); + std::vector setTest = lightServiceClient_->GetLightIdList(); + if (setTest.size() == 0) { + return; + } + bool flag = true; + auto it = mapTarget.find(setTest); + if (it == mapTarget.end()) { + flag = false; + } + ASSERT_TRUE(flag); +} + +/* + * @tc.name: PlayLightEffect_001 + * @tc.desc: perform light effect + * @tc.type: FUNC + */ +HWTEST_F(LightNativeTest, PlayLightEffect_001, TestSize.Level1) +{ + int32_t ret; + int32_t count = 0; + std::vector setTest = lightServiceClient_->GetLightIdList(); + if (setTest.size() == 0) { + return; + } + for (auto it = setTest.begin(); it != setTest.end(); it++) { + if (*it == static_cast(LightId::LIGHT_ID_BELT)) { + count++; + } + } + if (count == 1) { + ret = lightServiceClient_->PlayLightEffect(LightId::LIGHT_ID_BELT, "1"); + ASSERT_EQ(ret, ERR_OK); + } else { + uint32_t ret = lightServiceClient_->PlayLightEffect(LightId::LIGHT_ID_BELT, "1"); + ASSERT_EQ(ret, LIGHT_ID_NOT_SUPPORT); + } +} + +/* + * @tc.name: StopLightEffect_001 + * @tc.desc: perform light effect + * @tc.type: FUNC + */ +HWTEST_F(LightNativeTest, StopLightEffect_001, TestSize.Level1) +{ + int32_t ret; + int32_t count = 0; + std::vector setTest = lightServiceClient_->GetLightIdList(); + if (setTest.size() == 0) { + return; + } + for (auto it = setTest.begin(); it != setTest.end(); it++) { + if (*it == static_cast(LightId::LIGHT_ID_BELT)) { + count++; + } + } + if (count == 1) { + ret = lightServiceClient_->StopLightEffect(LightId::LIGHT_ID_BELT); + ASSERT_EQ(ret, ERR_OK); + } else { + uint32_t ret = lightServiceClient_->StopLightEffect(LightId::LIGHT_ID_BELT); + ASSERT_EQ(ret, LIGHT_ID_NOT_SUPPORT); + } +} +} // namespace Sensors +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/native/miscdevice/test/unittest/common/vibrator_native_test.cpp b/frameworks/native/miscdevice/test/unittest/common/vibrator_native_test.cpp new file mode 100755 index 0000000000000000000000000000000000000000..bbe50792ef66fa9f4d5933b7409a8b8ddea46d92 --- /dev/null +++ b/frameworks/native/miscdevice/test/unittest/common/vibrator_native_test.cpp @@ -0,0 +1,222 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include "ipc_skeleton.h" +#include "iservice_registry.h" +#include "jni.h" + +#include "sensors_errors.h" +#include "sensors_log_domain.h" +#include "vibrator_service_client.h" + +namespace OHOS { +namespace Sensors { +using namespace testing::ext; +using namespace OHOS::HiviewDFX; + +namespace { +constexpr HiLogLabel LABEL = { LOG_CORE, SensorsLogDomain::MISCDEVICE_VIBRATOR_NATIVE, "VibratorNativeTest" }; +} + +class VibratorNativeTest : public testing::Test { +public: + void SetUp(); + void TearDown(); + int32_t vibratorId_ = 123; + std::unique_ptr vibratorServiceClient_; +}; + +void VibratorNativeTest::SetUp() +{ + vibratorServiceClient_ = std::make_unique(); + ASSERT_NE(vibratorServiceClient_, nullptr); + HiLog::Info(LABEL, "%{public}s end", __func__); +} + +void VibratorNativeTest::TearDown() +{ + vibratorServiceClient_ = nullptr; +} + +/* + * @tc.name: IsVibratorEffectSupport_001 + * @tc.desc: IS support vibrator effect. + * @tc.type: FUNC + */ +HWTEST_F(VibratorNativeTest, IsVibratorEffectSupport_001, TestSize.Level1) +{ + std::string supportString("haptic.clock.timer"); + bool flag = vibratorServiceClient_->IsVibratorEffectSupport(vibratorId_, supportString); + if (!flag) { + return; + } + ASSERT_TRUE(flag); + std::string notsupportString("haptic"); + flag = vibratorServiceClient_->IsVibratorEffectSupport(vibratorId_, notsupportString); + ASSERT_TRUE(flag); +} + +/* + * @tc.name: VibratorOn_001 + * @tc.desc: vibrator on + * @tc.type: FUNC + */ +HWTEST_F(VibratorNativeTest, VibratorOn_001, TestSize.Level1) +{ + uint32_t time = 2000; + int32_t ret = vibratorServiceClient_->Vibrate(vibratorId_, time); + std::this_thread::sleep_for(std::chrono::milliseconds(2000)); + ASSERT_EQ(ret, NO_ERROR); +} + +/* + * @tc.name: VibratorOff_001 + * @tc.desc: vibrator off + * @tc.type: FUNC + */ +HWTEST_F(VibratorNativeTest, VibratorOff_001, TestSize.Level1) +{ + int32_t ret = vibratorServiceClient_->Stop(vibratorId_, "time"); + ASSERT_EQ(ret, NO_ERROR); +} + +/* + * @tc.name: GetVibratorIdList_001 + * @tc.desc: get vibrator list + * @tc.type: FUNC + */ +HWTEST_F(VibratorNativeTest, GetVibratorIdList_001, TestSize.Level1) +{ + std::vector vibratorIdList = vibratorServiceClient_->GetVibratorIdList(); + ASSERT_GT(vibratorIdList.size(), 0UL); +} + +/* + * @tc.name: GetVibratorParameter_001 + * @tc.desc: get vibrator parameter + * @tc.type: FUNC + */ +HWTEST_F(VibratorNativeTest, GetVibratorParameter_001, TestSize.Level1) +{ + std::string ret = vibratorServiceClient_->GetVibratorParameter(vibratorId_, "mmi_vibrator_calib_get_result"); + HiLog::Info(LABEL, "%{public}s ret : %{public}s", __func__, ret.c_str()); +} + +/* + * @tc.name: PlayVibratorEffect_001 + * @tc.desc: perform vibrator effect + * @tc.type: FUNC + */ +HWTEST_F(VibratorNativeTest, PlayVibratorEffect_001, TestSize.Level1) +{ + std::string supportString("haptic.clock.timer"); + int32_t ret; + if (vibratorServiceClient_->IsVibratorEffectSupport(vibratorId_, supportString)) { + ret = vibratorServiceClient_->Vibrate(vibratorId_, supportString, false); + std::this_thread::sleep_for(std::chrono::milliseconds(2000)); + if (ret == ERROR) { + ASSERT_EQ(ret, ERROR); + } + } +} + +/* + * @tc.name: PlayVibratorEffect_002 + * @tc.desc: perform vibrator effect repeat + * @tc.type: FUNC + * @tc.require: AR000F46AB + * @tc.author: guofuqiang + */ +HWTEST_F(VibratorNativeTest, PlayVibratorEffect_002, TestSize.Level1) +{ + std::string supportString("haptic.ringtone.Bounce"); + if (vibratorServiceClient_->IsVibratorEffectSupport(vibratorId_, supportString)) { + int32_t ret = vibratorServiceClient_->Vibrate(vibratorId_, supportString, true); + std::this_thread::sleep_for(std::chrono::milliseconds(5000)); + if (ret == NO_ERROR) { + ASSERT_EQ(ret, NO_ERROR); + } + ret = vibratorServiceClient_->Stop(vibratorId_, "preset"); + if (ret == NO_ERROR) { + ASSERT_EQ(ret, NO_ERROR); + } + } +} + +/* + * @tc.name: StopVibratorEffect_001 + * @tc.desc: stop vibrator effect + * @tc.type: FUNC + */ +HWTEST_F(VibratorNativeTest, StopVibratorEffect_001, TestSize.Level1) +{ + std::string supportString("preset"); + int32_t ret = vibratorServiceClient_->Stop(vibratorId_, supportString); + if (ret == NO_ERROR) { + ASSERT_EQ(ret, NO_ERROR); + } +} + +/* + * @tc.name: PlayCustomVibratorEffect_001 + * @tc.desc: perform custom vibrator effect + * @tc.type: FUNC + */ +HWTEST_F(VibratorNativeTest, PlayCustomVibratorEffect_001, TestSize.Level1) +{ + std::vector timing; + std::vector intensity = { 100, 120, 210, 50 }; + int32_t periodCount = 5; + int32_t ret; + + timing.push_back(500); // off time + timing.push_back(500); // on time + timing.push_back(500); // off time + timing.push_back(500); // on time + + ret = vibratorServiceClient_->Vibrate(vibratorId_, timing, intensity, periodCount); + ASSERT_EQ(ret, NO_ERROR); + + std::this_thread::sleep_for(std::chrono::milliseconds(4000)); + + timing.clear(); + timing.push_back(3000); // off time + timing.push_back(3000); // on time + intensity.clear(); + intensity.push_back(200); + intensity.push_back(100); + ret = vibratorServiceClient_->Vibrate(vibratorId_, timing, intensity, periodCount); + ASSERT_EQ(ret, NO_ERROR); + std::this_thread::sleep_for(std::chrono::milliseconds(10000)); + ret = vibratorServiceClient_->Stop(vibratorId_, "customized"); + ASSERT_EQ(ret, NO_ERROR); +} + +/* + * @tc.name: SetVibratorParameter_001 + * @tc.desc: set vibrator parameter + * @tc.type: FUNC + */ +HWTEST_F(VibratorNativeTest, SetVibratorParameter_001, TestSize.Level1) +{ + std::string s("mmi_vibrator_calib_on"); + int32_t ret = vibratorServiceClient_->SetVibratorParameter(vibratorId_, s); + ASSERT_EQ(ret, NO_ERROR); +} +} // namespace Sensors +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/native/miscdevice/test/unittest/phone/BUILD.gn b/frameworks/native/miscdevice/test/unittest/phone/BUILD.gn new file mode 100755 index 0000000000000000000000000000000000000000..4d0155a299273f5d21b36bb86c0e3433a5c0fa64 --- /dev/null +++ b/frameworks/native/miscdevice/test/unittest/phone/BUILD.gn @@ -0,0 +1,53 @@ +# Copyright (c) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/test.gni") + +SUBSYSTEM_DIR = "//base/sensors" +module_output_path = "sensors/miscdevice/frameworks" + +####################################################### +ohos_unittest("LightNativeTest") { + module_out_path = module_output_path + sources = [ "./light_native_test.cpp" ] + + include_dirs = [ + "$SUBSYSTEM_DIR/miscdevice/utils/include", + "$SUBSYSTEM_DIR/miscdevice/frameworks/native/miscdevice/include", + "$SUBSYSTEM_DIR/miscdevice/frameworks/native/miscdevice/include", + "$SUBSYSTEM_DIR/miscdevice/frameworks/native/common/include", + "//utils/native/base/include", + ] + + deps = [ + "$SUBSYSTEM_DIR/miscdevice/frameworks/native/miscdevice:liblight_native", + "$SUBSYSTEM_DIR/miscdevice/services/miscdevice_service:libmiscdevice_service", + "$SUBSYSTEM_DIR/miscdevice/utils:libmiscdevice_utils", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + "//utils/native/base:utils", + ] + + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr_L2:samgr_proxy", + ] +} + +####################################################### +group("unittest") { + testonly = true + deps = [ ":LightNativeTest" ] +} diff --git a/frameworks/native/miscdevice/test/unittest/phone/light_native_test.cpp b/frameworks/native/miscdevice/test/unittest/phone/light_native_test.cpp new file mode 100755 index 0000000000000000000000000000000000000000..ad255e672ba97f7ef5d56d3b231b47690636a20b --- /dev/null +++ b/frameworks/native/miscdevice/test/unittest/phone/light_native_test.cpp @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include "ipc_skeleton.h" +#include "iservice_registry.h" +#include "jni.h" + +#include "light_service_client.h" +#include "sensors_errors.h" +#include "sensors_log_domain.h" + +namespace OHOS { +namespace Sensors { +using namespace testing::ext; +using namespace OHOS::HiviewDFX; + +namespace { +constexpr HiLogLabel LABEL = { LOG_CORE, SensorsLogDomain::MISCDEVICE_LIGHT_NATIVE, "LightNativeTest" }; +} + +class LightNativeTest : public testing::Test { +public: + void SetUp(); + void TearDown(); + std::unique_ptr lightServiceClient_; +}; + +void LightNativeTest::SetUp() +{ + lightServiceClient_ = std::make_unique(); + ASSERT_NE(lightServiceClient_, nullptr); + HiLog::Info(LABEL, "%{public}s end", __func__); +} + +void LightNativeTest::TearDown() +{ + lightServiceClient_ = nullptr; +} + +/* + * @tc.name: Light_ON_001 + * @tc.desc: light on + * @tc.type: FUNC + */ +HWTEST_F(LightNativeTest, Light_ON_001, TestSize.Level1) +{ + std::vector setTest = lightServiceClient_->GetLightIdList(); + if (setTest.size() == 0) { + return; + } + uint32_t corlorBrightness = 0x00ffffff; + int32_t ret = lightServiceClient_->Light(LightId::LIGHT_ID_LED, corlorBrightness, 2000, 1000); + std::this_thread::sleep_for(std::chrono::milliseconds(3000)); + ASSERT_EQ(ret, NO_ERROR); +} + +/* + * @tc.name: Light_OFF_001 + * @tc.desc: light off + * @tc.type: FUNC + */ +HWTEST_F(LightNativeTest, Light_OFF_001, TestSize.Level1) +{ + std::vector setTest = lightServiceClient_->GetLightIdList(); + if (setTest.size() == 0) { + return; + } + uint32_t corlorBrightness = 0; + int32_t ret = lightServiceClient_->Light(LightId::LIGHT_ID_LED, corlorBrightness, 0, 0); + ASSERT_EQ(ret, NO_ERROR); +} +} // namespace Sensors +} // namespace OHOS \ No newline at end of file diff --git a/sa_profile/3602.xml b/sa_profile/3602.xml new file mode 100755 index 0000000000000000000000000000000000000000..9a5b946add8c007a726d8514c3d6d614be0f01f7 --- /dev/null +++ b/sa_profile/3602.xml @@ -0,0 +1,25 @@ + + + + hsensors + + 3602 + libmiscdevice_service.z.so + true + false + 1 + + \ No newline at end of file diff --git a/sa_profile/BUILD.gn b/sa_profile/BUILD.gn new file mode 100755 index 0000000000000000000000000000000000000000..eaf26639ae0e10a21bdf058aa0bcee686e0eda81 --- /dev/null +++ b/sa_profile/BUILD.gn @@ -0,0 +1,23 @@ +# Copyright (c) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos/sa_profile/sa_profile.gni") + +ohos_sa_profile("sensors_sa_profiles") { + sources = [ "3602.xml" ] + part_name = "sensors_miscdevice" +} + +group("sensors_profiles") { + deps = [ ":sensors_sa_profiles" ] +} diff --git a/services/miscdevice_service/BUILD.gn b/services/miscdevice_service/BUILD.gn new file mode 100755 index 0000000000000000000000000000000000000000..f070a2be624dbd6915670d99a70335e21fa1c449 --- /dev/null +++ b/services/miscdevice_service/BUILD.gn @@ -0,0 +1,62 @@ +# Copyright (c) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") + +SUBSYSTEM_DIR = "//base/sensors" +ohos_shared_library("libmiscdevice_service") { + sources = [ + "src/miscdevice_service.cpp", + "src/miscdevice_service_impl.cpp", + "src/miscdevice_service_stub.cpp", + ] + + include_dirs = [ + "include", + "//utils/native/base/include", + "//utils/system/safwk/native/include", + "$SUBSYSTEM_DIR/miscdevice/frameworks/native/miscdevice/include", + "$SUBSYSTEM_DIR/miscdevice/utils/include", + "//drivers/peripheral/misc/vibrator/interfaces/include", + ] + + cflags = [ "-Wno-error=inconsistent-missing-override" ] + deps = [ + "$SUBSYSTEM_DIR/miscdevice/utils:libmiscdevice_utils", + "//drivers/peripheral/misc/vibrator/hal:hdi_vibrator", + "//utils/native/base:utils", + ] + + external_deps = [ + #"appexecfwk:appexecfwk_base", + #"appexecfwk:appexecfwk_core", + "hiviewdfx_hilog_native:libhilog", + + #"intent:base", + #"intent:intent", + "ipc:ipc_core", + + #"communication_L2:ipc_core", + #"permission:permission_sdk_cxx", + "permission_standard:libpermissionsdk_standard", + "safwk:system_ability_fwk", + "samgr_L2:samgr_proxy", + ] + + part_name = "sensors_miscdevice" + subsystem_name = "sensors" +} + +group("miscdevice_service_target") { + deps = [ ":libmiscdevice_service" ] +} diff --git a/services/miscdevice_service/include/miscdevice_service.h b/services/miscdevice_service/include/miscdevice_service.h new file mode 100755 index 0000000000000000000000000000000000000000..f55542c2e8fec04452ec8b0f89ee44f7126dbb56 --- /dev/null +++ b/services/miscdevice_service/include/miscdevice_service.h @@ -0,0 +1,122 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef MISCDEVICE_SERVICE_H +#define MISCDEVICE_SERVICE_H + +#include +#include +#include +#include +#include +#include +#include + +#include "system_ability.h" +#include "thread_ex.h" + +#include "miscdevice_common.h" +#include "miscdevice_service_stub.h" +#include "nocopyable.h" +#include "vibrator_if.h" +#include "vibrator_type.h" +#include "miscdevice_service_impl.h" + +namespace OHOS { +namespace Sensors { +enum class MiscdeviceServiceState { + STATE_STOPPED, + STATE_RUNNING, +}; + +class MiscdeviceService : public SystemAbility, public MiscdeviceServiceStub { + DECLARE_SYSTEM_ABILITY(MiscdeviceService) +public: + explicit MiscdeviceService(int32_t systemAbilityId, bool runOnCreate = false); + ~MiscdeviceService(); + void OnDump() override; + void OnStart() override; + void OnStop() override; + int32_t Dump(int32_t fd, const std::vector &args) override; + virtual bool IsAbilityAvailable(MiscdeviceDeviceId groupID) override; + virtual bool IsVibratorEffectAvailable(int32_t vibratorId, const std::string &effectType) override; + virtual std::vector GetVibratorIdList() override; + virtual int32_t Vibrate(int32_t vibratorId, uint32_t timeOut) override; + virtual int32_t CancelVibrator(int32_t vibratorId) override; + virtual int32_t PlayVibratorEffect(int32_t vibratorId, const std::string &effect, bool isLooping) override; + virtual int32_t PlayCustomVibratorEffect(int32_t vibratorId, const std::vector &timing, + const std::vector &intensity, int32_t periodCount) override; + virtual int32_t StopVibratorEffect(int32_t vibratorId, const std::string &effect) override; + virtual int32_t SetVibratorParameter(int32_t vibratorId, const std::string &cmd) override; + virtual std::string GetVibratorParameter(int32_t vibratorId, const std::string &cmd) override; + virtual std::vector GetLightSupportId() override; + virtual bool IsLightEffectSupport(int32_t lightId, const std::string &effectId) override; + virtual int32_t Light(int32_t lightId, uint64_t brightness, uint32_t timeOn, uint32_t timeOff) override; + virtual int32_t PlayLightEffect(int32_t lightId, const std::string &type) override; + virtual int32_t StopLightEffect(int32_t lightId) override; + +private: + DISALLOW_COPY_AND_MOVE(MiscdeviceService); + bool InitInterface(); + MiscdeviceServiceImpl &vibratorServiceImpl_ = MiscdeviceServiceImpl::GetInstance(); + class VibratorThread : public Thread { + public: + explicit VibratorThread(const MiscdeviceService &service); + ~VibratorThread() = default; + void UpdateVibratorData(const std::vector &timing, const std::vector &intensity, + int32_t &periodCount); + void NotifyExit(); + + protected: + virtual bool Run(); + + private: + std::timed_mutex mtx_; + std::vector vecTimingMs_; + std::vector intensitys_; + int32_t periodCount_; + }; + class VibratorEffectThread : public Thread { + public: + explicit VibratorEffectThread(const MiscdeviceService &service); + ~VibratorEffectThread() = default; + void UpdateVibratorEffectData(const std::string effect, int32_t delayTiming); + + protected: + virtual bool Run(); + + private: + std::string hapticEffect_; + int32_t delayTimingMs_; + }; + bool lightExist_; + bool vibratorExist_; + std::set lightSupportId_; + std::map miscDdeviceIdMap_; + MiscdeviceServiceState state_; + VibratorThread *vibratorThread_ = nullptr; + std::unique_ptr vibratorEffectThread_; + std::mutex vibratorThreadMutex_; + std::mutex vibratorEffectMutex_; + std::mutex vibratorEffectThreadMutex_; + std::map vibratorEffectMap_; + static bool ready_; + static std::mutex conditionVarMutex_; + static std::condition_variable conditionVar_; + static std::unordered_map hapticRingMap_; +}; +} // namespace Sensors +} // namespace OHOS +#endif // MISCDEVICE_SERVICE_H diff --git a/services/miscdevice_service/include/miscdevice_service_impl.h b/services/miscdevice_service/include/miscdevice_service_impl.h new file mode 100755 index 0000000000000000000000000000000000000000..92de46a694d6ce050bbd531f9a03a8423a09d2fb --- /dev/null +++ b/services/miscdevice_service/include/miscdevice_service_impl.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef MISCDEVICE_INTERFACE_H +#define MISCDEVICE_INTERFACE_H + +#include "errors.h" +#include "refbase.h" +#include "singleton.h" +#include "vibrator_if.h" +#include "vibrator_type.h" +#include "nocopyable.h" + +namespace OHOS { +namespace Sensors { +class MiscdeviceServiceImpl : public Singleton { +public: + MiscdeviceServiceImpl() = default; + + virtual ~MiscdeviceServiceImpl() = default; + bool IsVibratorEffectAvailable(int32_t vibratorId, const std::string &effectType); + ErrCode InitVibratorServiceImpl(); + ErrCode StartVibrator(const char *effectType) const; + ErrCode StartOnceVibrator(uint32_t duration) const; + ErrCode StopVibratorImpl(enum VibratorMode mode) const; + +private: + DISALLOW_COPY_AND_MOVE(MiscdeviceServiceImpl); + + const struct VibratorInterface *vibratorInterface = nullptr; +}; +} // namespace Sensors +} // namespace OHOS +#endif // SENSOR_INTERFACE_H diff --git a/services/miscdevice_service/include/miscdevice_service_stub.h b/services/miscdevice_service/include/miscdevice_service_stub.h new file mode 100755 index 0000000000000000000000000000000000000000..22dd5dfb17a4c7a6bf46d7a2949779d8109d642b --- /dev/null +++ b/services/miscdevice_service/include/miscdevice_service_stub.h @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef MISCDEVICE_SERVICE_STUB_H +#define MISCDEVICE_SERVICE_STUB_H + +#include + +#include "iremote_stub.h" +#include "message_parcel.h" +#include "nocopyable.h" + +#include "i_miscdevice_service.h" + +namespace OHOS { +namespace Sensors { +using ServicePb = std::function; + +class MiscdeviceServiceStub : public IRemoteStub { +public: + MiscdeviceServiceStub(); + + virtual ~MiscdeviceServiceStub(); + + virtual int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, + MessageOption &option) override; + +private: + DISALLOW_COPY_AND_MOVE(MiscdeviceServiceStub); + using MiscBaseFunc = int32_t (MiscdeviceServiceStub::*)(MessageParcel &data, MessageParcel &reply); + + int32_t IsAbilityAvailablePb(MessageParcel &data, MessageParcel &reply); + int32_t IsVibratorEffectAvailablePb(MessageParcel &data, MessageParcel &reply); + int32_t GetVibratorIdListPb(MessageParcel &data, MessageParcel &reply); + int32_t VibratePb(MessageParcel &data, MessageParcel &reply); + int32_t CancelVibratorPb(MessageParcel &data, MessageParcel &reply); + int32_t PlayVibratorEffectPb(MessageParcel &data, MessageParcel &reply); + int32_t PlayCustomVibratorEffectPb(MessageParcel &data, MessageParcel &reply); + int32_t StopVibratorEffectPb(MessageParcel &data, MessageParcel &reply); + int32_t SetVibratorParameterPb(MessageParcel &data, MessageParcel &reply); + int32_t GetVibratorParameterPb(MessageParcel &data, MessageParcel &reply); + int32_t GetLightSupportIdPb(MessageParcel &data, MessageParcel &reply); + int32_t IsLightEffectSupportPb(MessageParcel &data, MessageParcel &reply); + int32_t LightPb(MessageParcel &data, MessageParcel &reply); + int32_t PlayLightEffectPb(MessageParcel &data, MessageParcel &reply); + int32_t StopLightEffectPb(MessageParcel &data, MessageParcel &reply); + + bool CheckVibratePermission(); + + std::map baseFuncs_; +}; +} // namespace Sensors +} // namespace OHOS +#endif // MISCDEVICE_SERVICE_STUB_H diff --git a/services/miscdevice_service/src/miscdevice_service.cpp b/services/miscdevice_service/src/miscdevice_service.cpp new file mode 100755 index 0000000000000000000000000000000000000000..8f63c91d92f4daf16ce2a55e754cdff4f051f850 --- /dev/null +++ b/services/miscdevice_service/src/miscdevice_service.cpp @@ -0,0 +1,415 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "miscdevice_service.h" + +#include "sensors_errors.h" +#include "sensors_log_domain.h" +#include "system_ability_definition.h" + +#include "miscdevice_service_impl.h" + + +namespace OHOS { +namespace Sensors { +using namespace OHOS::HiviewDFX; + +namespace { +constexpr HiLogLabel LABEL = { LOG_CORE, SensorsLogDomain::MISCDEVICE_SERVICE, "MiscdeviceService" }; +constexpr int32_t MIN_VIBRATOR_TIME = 0; +constexpr int32_t MAX_VIBRATOR_TIME = 1800000; +constexpr int32_t DEFAULT_VIBRATOR_ID = 123; +} // namespace + +bool MiscdeviceService::ready_ = false; +std::mutex MiscdeviceService::conditionVarMutex_; +std::condition_variable MiscdeviceService::conditionVar_; +std::unordered_map MiscdeviceService::hapticRingMap_ = { + {"haptic.ringtone.Bounce", 6762}, + {"haptic.ringtone.Cartoon", 2118}, + {"haptic.ringtone.Chilled", 11947}, + {"haptic.ringtone.Classic_Bell", 5166}, + {"haptic.ringtone.Concentrate", 15707}, + {"haptic.ringtone.Day_lily", 5695}, + {"haptic.ringtone.Digital_Ringtone", 3756}, + {"haptic.ringtone.Dream", 6044}, + {"haptic.ringtone.Dream_It_Possible", 39710}, + {"haptic.ringtone.Dynamo", 15148}, + {"haptic.ringtone.Flipped", 20477}, + {"haptic.ringtone.Forest_Day", 6112}, + {"haptic.ringtone.Free", 9917}, + {"haptic.ringtone.Halo", 16042}, + {"haptic.ringtone.Harp", 10030}, + {"haptic.ringtone.Hello_Ya", 35051}, + {"haptic.ringtone.Menuet", 8261}, + {"haptic.ringtone.Neon", 23925}, + {"haptic.ringtone.Notes", 9051}, + {"haptic.ringtone.Pulse", 27550}, + {"haptic.ringtone.Sailing", 19188}, + {"haptic.ringtone.Sax", 4780}, + {"haptic.ringtone.Spin", 6000}, + {"haptic.ringtone.Tune_Clean", 13342}, + {"haptic.ringtone.Tune_Living", 17249}, + {"haptic.ringtone.Tune_Orchestral", 15815}, + {"haptic.ringtone.Westlake", 11654}, + {"haptic.ringtone.Whistle", 20276}, + {"haptic.ringtone.Amusement_Park", 9441}, + {"haptic.ringtone.Breathe_Freely", 30887}, + {"haptic.ringtone.Summer_Afternoon", 31468}, + {"haptic.ringtone.Surging_Power", 17125}, + {"haptic.ringtone.Sunlit_Garden", 38330}, + {"haptic.ringtone.Fantasy_World", 15301} +}; + +REGISTER_SYSTEM_ABILITY_BY_ID(MiscdeviceService, MISCDEVICE_SERVICE_ABILITY_ID, true); + +MiscdeviceService::MiscdeviceService(int32_t systemAbilityId, bool runOnCreate) + : SystemAbility(systemAbilityId, runOnCreate), + lightExist_(false), + vibratorExist_(false), + state_(MiscdeviceServiceState::STATE_STOPPED), + vibratorThread_(nullptr) +{} + +MiscdeviceService::~MiscdeviceService() +{ + if (vibratorThread_ != nullptr) { + while (vibratorThread_->IsRunning()) { + vibratorThread_->NotifyExit(); + vibratorThread_->NotifyExitSync(); + } + delete vibratorThread_; + } +} + +void MiscdeviceService::OnDump() +{ + HiLog::Info(LABEL, "%{public}s is invoked", __func__); +} + +void MiscdeviceService::OnStart() +{ + HiLog::Info(LABEL, "%{public}s begin", __func__); + if (state_ == MiscdeviceServiceState::STATE_RUNNING) { + HiLog::Warn(LABEL, "%{public}s already started", __func__); + return; + } + if (!InitInterface()) { + HiLog::Error(LABEL, "%{public}s Init interface error", __func__); + return; + } + if (!SystemAbility::Publish(this)) { + HiLog::Error(LABEL, "%{public}s publish MiscdeviceService failed", __func__); + return; + } + auto ret = miscDdeviceIdMap_.insert(std::make_pair(MiscdeviceDeviceId::LED, lightExist_)); + if (!ret.second) { + HiLog::Info(LABEL, "%{public}s light exist in miscDdeviceIdMap_", __func__); + ret.first->second = lightExist_; + } + ret = miscDdeviceIdMap_.insert(std::make_pair(MiscdeviceDeviceId::VIBRATOR, vibratorExist_)); + if (!ret.second) { + HiLog::Info(LABEL, "%{public}s vibrator exist in miscDdeviceIdMap_", __func__); + ret.first->second = vibratorExist_; + } + state_ = MiscdeviceServiceState::STATE_RUNNING; + HiLog::Info(LABEL, "%{public}s end", __func__); +} + +bool MiscdeviceService::InitInterface() +{ + auto ret = vibratorServiceImpl_.InitVibratorServiceImpl(); + if (ret != ERR_OK) { + HiLog::Error(LABEL, "%{public}s InitVibratorServiceImpl failed", __func__); + return false; + } + return true; +} + +void MiscdeviceService::OnStop() +{ + HiLog::Info(LABEL, "%{public}s begin", __func__); + if (state_ == MiscdeviceServiceState::STATE_STOPPED) { + HiLog::Warn(LABEL, "%{public}s MiscdeviceService stopped already", __func__); + return; + } + state_ = MiscdeviceServiceState::STATE_STOPPED; + HiLog::Info(LABEL, "%{public}s end", __func__); +} + +bool MiscdeviceService::IsAbilityAvailable(MiscdeviceDeviceId groupID) +{ + auto it = miscDdeviceIdMap_.find(groupID); + if (it == miscDdeviceIdMap_.end()) { + HiLog::Error(LABEL, "%{public}s cannot find groupID : %{public}d", __func__, groupID); + return false; + } + return it->second; +} + +bool MiscdeviceService::IsVibratorEffectAvailable(int32_t vibratorId, const std::string &effectType) +{ + return true; +} + +std::vector MiscdeviceService::GetVibratorIdList() +{ + std::vector vibratorIds = { DEFAULT_VIBRATOR_ID }; + return vibratorIds; +} + +int32_t MiscdeviceService::Vibrate(int32_t vibratorId, uint32_t timeOut) +{ + if ((timeOut < MIN_VIBRATOR_TIME) || (timeOut > MAX_VIBRATOR_TIME)) { + HiLog::Error(LABEL, "%{public}s timeOut is invalid, timeOut : %{public}u", __func__, timeOut); + return ERR_INVALID_VALUE; + } + std::lock_guard vibratorEffectLock(vibratorEffectMutex_); + auto it = vibratorEffectMap_.find(vibratorId); + if (it != vibratorEffectMap_.end()) { + if (it->second == "time") { + vibratorServiceImpl_.StopVibratorImpl(VIBRATOR_MODE_ONCE); + } else { + vibratorServiceImpl_.StopVibratorImpl(VIBRATOR_MODE_PRESET); + } + } + vibratorEffectMap_[vibratorId] = "time"; + return vibratorServiceImpl_.StartOnceVibrator((timeOut < MIN_VIBRATOR_TIME) ? MIN_VIBRATOR_TIME : timeOut); +} + +int32_t MiscdeviceService::CancelVibrator(int32_t vibratorId) +{ + std::lock_guard vibratorEffectLock(vibratorEffectMutex_); + auto it = vibratorEffectMap_.find(vibratorId); + if (it != vibratorEffectMap_.end() && it->second == "time") { + HiLog::Info(LABEL, "%{public}s stop mode is %{public}s", __func__, it->second.c_str()); + vibratorEffectMap_.clear(); + } else { + HiLog::Error(LABEL, "%{public}s is failed", __func__); + return ERROR; + } + if (vibratorThread_ != nullptr) { + while (vibratorThread_->IsRunning()) { + HiLog::Info(LABEL, "%{public}s stop previous vibratorThread, vibratorId : %{public}d", __func__, + vibratorId); + vibratorThread_->NotifyExit(); + vibratorThread_->NotifyExitSync(); + } + } + return vibratorServiceImpl_.StopVibratorImpl(VIBRATOR_MODE_ONCE); +} + +int32_t MiscdeviceService::PlayVibratorEffect(int32_t vibratorId, const std::string &effect, bool isLooping) +{ + std::lock_guard vibratorEffectLock(vibratorEffectMutex_); + auto it = vibratorEffectMap_.find(vibratorId); + if (it != vibratorEffectMap_.end()) { + if (it->second == "time") { + if ((vibratorThread_ != nullptr) && (vibratorThread_->IsRunning())) { + HiLog::Info(LABEL, "%{public}s stop previous vibratorThread", __func__); + vibratorThread_->NotifyExit(); + vibratorThread_->NotifyExitSync(); + } + vibratorServiceImpl_.StopVibratorImpl(VIBRATOR_MODE_ONCE); + } else { + vibratorServiceImpl_.StopVibratorImpl(VIBRATOR_MODE_PRESET); + } + } + if (!isLooping) { + vibratorEffectMap_[vibratorId] = effect; + const char *effectType = (char *)effect.data(); + return vibratorServiceImpl_.StartVibrator(effectType); + } + if (hapticRingMap_.empty()) { + HiLog::Error(LABEL, "%{public}s hapticRingMap_ cannot be empty", __func__); + return ERROR; + } + std::unordered_map::iterator iter = hapticRingMap_.find(effect); + if (iter == hapticRingMap_.end()) { + HiLog::Error(LABEL, "%{public}s is not exist", __func__); + return ERROR; + } + vibratorEffectMap_[vibratorId] = effect; + int32_t delayTiming = iter->second; + if (vibratorEffectThread_ == nullptr) { + std::lock_guard lock(vibratorEffectThreadMutex_); + if (vibratorEffectThread_ == nullptr) { + vibratorEffectThread_ = std::make_unique(*this); + if (vibratorEffectThread_ == nullptr) { + HiLog::Error(LABEL, "%{public}s vibratorEffectThread_ cannot be null", __func__); + return ERROR; + } + } + } + while (vibratorEffectThread_->IsRunning()) { + HiLog::Debug(LABEL, "%{public}s notify the vibratorEffectThread", __func__); + ready_ = true; + conditionVar_.notify_one(); + ready_ = false; + } + HiLog::Debug(LABEL, "%{public}s update vibrator effect data and start", __func__); + vibratorEffectThread_->UpdateVibratorEffectData(effect, delayTiming); + vibratorEffectThread_->Start("VibratorEffectThread"); + return NO_ERROR; +} + +int32_t MiscdeviceService::PlayCustomVibratorEffect(int32_t vibratorId, const std::vector &timing, + const std::vector &intensity, int32_t periodCount) +{ + if (!MiscdeviceCommon::CheckCustomVibratorEffect(timing, intensity, periodCount)) { + HiLog::Error(LABEL, "%{public}s params are invalid", __func__); + return ERR_INVALID_VALUE; + } + std::lock_guard vibratorEffectLock(vibratorEffectMutex_); + auto it = vibratorEffectMap_.find(vibratorId); + if (it != vibratorEffectMap_.end()) { + if (it->second == "time") { + vibratorServiceImpl_.StopVibratorImpl(VIBRATOR_MODE_ONCE); + } else { + vibratorServiceImpl_.StopVibratorImpl(VIBRATOR_MODE_PRESET); + } + } + vibratorEffectMap_[vibratorId] = "time"; + if (vibratorThread_ == nullptr) { + std::lock_guard lock(vibratorThreadMutex_); + if (vibratorThread_ == nullptr) { + vibratorThread_ = new (std::nothrow) VibratorThread(*this); + if (vibratorThread_ == nullptr) { + HiLog::Error(LABEL, "%{public}s vibratorThread_ cannot be null", __func__); + return ERROR; + } + } + } + // check current vibrator execution sequences and abort + while (vibratorThread_->IsRunning()) { + HiLog::Info(LABEL, "%{public}s stop previous vibratorThread, vibratorId : %{public}d", __func__, vibratorId); + vibratorThread_->NotifyExit(); + vibratorThread_->NotifyExitSync(); + } + HiLog::Info(LABEL, "%{public}s update vibrator data and start, vibratorId : %{public}d", __func__, vibratorId); + vibratorThread_->UpdateVibratorData(timing, intensity, periodCount); + vibratorThread_->Start("VibratorThread"); + return NO_ERROR; +} + +int32_t MiscdeviceService::StopVibratorEffect(int32_t vibratorId, const std::string &effect) +{ + std::string curEffect = effect; + std::lock_guard vibratorEffectLock(vibratorEffectMutex_); + auto it = vibratorEffectMap_.find(vibratorId); + if ((it != vibratorEffectMap_.end()) && (it->second != "time")) { + HiLog::Info(LABEL, "%{public}s vibrator effect is %{public}s", __func__, it->second.c_str()); + curEffect = it->second; + vibratorEffectMap_.clear(); + } else { + HiLog::Error(LABEL, "%{public}s is failed", __func__); + return ERROR; + } + HiLog::Info(LABEL, "%{public}s curEffect : %{public}s", __func__, curEffect.c_str()); + if (vibratorEffectThread_ != nullptr) { + while (vibratorEffectThread_->IsRunning()) { + HiLog::Debug(LABEL, "%{public}s notify the vibratorEffectThread, vibratorId : %{public}d", + __func__, vibratorId); + ready_ = true; + conditionVar_.notify_one(); + ready_ = false; + } + } + int32_t ret = vibratorServiceImpl_.StopVibratorImpl(VIBRATOR_MODE_PRESET); + return ret; +} + +int32_t MiscdeviceService::SetVibratorParameter(int32_t vibratorId, const std::string &cmd) +{ + return 0; +} + +std::string MiscdeviceService::GetVibratorParameter(int32_t vibratorId, const std::string &cmd) +{ + return nullptr; +} + +std::vector MiscdeviceService::GetLightSupportId() +{ + std::vector list; + return list; +} + +bool MiscdeviceService::IsLightEffectSupport(int32_t lightId, const std::string &effectId) +{ + return false; +} + +int32_t MiscdeviceService::Light(int32_t lightId, uint64_t brightness, uint32_t timeOn, uint32_t timeOff) +{ + return 0; +} + +int32_t MiscdeviceService::PlayLightEffect(int32_t lightId, const std::string &type) +{ + return 0; +} + +int32_t MiscdeviceService::StopLightEffect(int32_t lightId) +{ + return 0; +} + +MiscdeviceService::VibratorThread::VibratorThread(const MiscdeviceService &service) + : periodCount_(0) +{ + mtx_.lock(); +} + +void MiscdeviceService::VibratorThread::NotifyExit() +{ + mtx_.unlock(); +} + +bool MiscdeviceService::VibratorThread::Run() +{ + return false; +} + +void MiscdeviceService::VibratorThread::UpdateVibratorData(const std::vector &timing, + const std::vector &intensity, int32_t &periodCount) +{ + vecTimingMs_ = timing; + intensitys_ = intensity; + periodCount_ = periodCount; +} + +MiscdeviceService::VibratorEffectThread::VibratorEffectThread(const MiscdeviceService &service) + : delayTimingMs_(0) +{ +} + +bool MiscdeviceService::VibratorEffectThread::Run() +{ + return false; +} + +void MiscdeviceService::VibratorEffectThread::UpdateVibratorEffectData(const std::string effect, int32_t delayTiming) +{ + hapticEffect_ = effect; + delayTimingMs_ = delayTiming; +} + +int32_t MiscdeviceService::Dump(int32_t fd, const std::vector &args) +{ + return 0; +} +} // namespace Sensors +} // namespace OHOS diff --git a/services/miscdevice_service/src/miscdevice_service_impl.cpp b/services/miscdevice_service/src/miscdevice_service_impl.cpp new file mode 100755 index 0000000000000000000000000000000000000000..63824532f491869433abbf41a3eb68ee033f875e --- /dev/null +++ b/services/miscdevice_service/src/miscdevice_service_impl.cpp @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "miscdevice_service_impl.h" + +#include + +#include "sensors_errors.h" +#include "sensors_log_domain.h" +#include "system_ability_definition.h" + + +namespace OHOS { +namespace Sensors { +using namespace OHOS::HiviewDFX; + +namespace { +constexpr HiLogLabel LABEL = { LOG_CORE, SensorsLogDomain::MISCDEVICE_SERVICE, "MiscdeviceServiceImpl" }; +} + +bool MiscdeviceServiceImpl::IsVibratorEffectAvailable(int32_t vibratorId, const std::string &effectType) +{ + return true; +} + +ErrCode MiscdeviceServiceImpl::InitVibratorServiceImpl() +{ + HiLog::Info(LABEL, "%{public}s begin", __func__); + vibratorInterface = NewVibratorInterfaceInstance(); + if (vibratorInterface == nullptr) { + HiLog::Error(LABEL, "%{public}s failed, vibratorInterface cannot be null", __func__); + return ERR_INVALID_VALUE; + } + HiLog::Info(LABEL, "%{public}s end", __func__); + return ERR_OK; +} + +ErrCode MiscdeviceServiceImpl::StartVibrator(const char *effectType) const +{ + HiLog::Info(LABEL, "%{public}s begin", __func__); + if (effectType == nullptr) { + HiLog::Error(LABEL, "%{public}s failed, effectType cannot be null", __func__); + return ERR_INVALID_VALUE; + } + int32_t ret = vibratorInterface->Start(effectType); + HiLog::Info(LABEL, "%{public}s end", __func__); + return ret; +} + +ErrCode MiscdeviceServiceImpl::StartOnceVibrator(uint32_t duration) const +{ + HiLog::Info(LABEL, "%{public}s begin", __func__); + if (duration < 0) { + HiLog::Error(LABEL, "%{public}s duration is invalid", __func__); + return ERR_INVALID_VALUE; + } + int32_t ret = vibratorInterface->StartOnce(duration); + HiLog::Info(LABEL, "%{public}s end", __func__); + return ret; +} + +ErrCode MiscdeviceServiceImpl::StopVibratorImpl(enum VibratorMode mode) const +{ + HiLog::Info(LABEL, "%{public}s begin", __func__); + if (mode < VIBRATOR_MODE_ONCE) { + HiLog::Error(LABEL, "%{public}s mode is invalid", __func__); + return ERR_INVALID_VALUE; + } + int32_t ret = vibratorInterface->Stop(mode); + HiLog::Info(LABEL, "%{public}s end", __func__); + return ret; +} +} // namespace Sensors +} // namespace OHOS diff --git a/services/miscdevice_service/src/miscdevice_service_stub.cpp b/services/miscdevice_service/src/miscdevice_service_stub.cpp new file mode 100755 index 0000000000000000000000000000000000000000..a3d6026f1b6c2fb59c1c2625c3bedcd2041e27dc --- /dev/null +++ b/services/miscdevice_service/src/miscdevice_service_stub.cpp @@ -0,0 +1,198 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "miscdevice_service_stub.h" + +#include +#include + +#include "ipc_skeleton.h" +#include "message_parcel.h" + +#include "sensors_errors.h" +#include "sensors_log_domain.h" + +namespace OHOS { +namespace Sensors { +using namespace OHOS::HiviewDFX; + +namespace { +constexpr HiLogLabel LABEL = { LOG_CORE, SensorsLogDomain::MISCDEVICE_SERVICE, "MiscdeviceServiceStub" }; +const std::string VIBRATE_PERMISSION = "ohos.permission.VIBRATE"; +} // namespace + +MiscdeviceServiceStub::MiscdeviceServiceStub() +{ + HiLog::Info(LABEL, "%{public}s begin, %{public}p", __func__, this); + baseFuncs_[IS_ABILITY_AVAILABLE] = &MiscdeviceServiceStub::IsAbilityAvailablePb; + baseFuncs_[IS_VIBRATOR_EFFECT_AVAILABLE] = &MiscdeviceServiceStub::IsVibratorEffectAvailablePb; + baseFuncs_[GET_VIBRATOR_ID_LIST] = &MiscdeviceServiceStub::GetVibratorIdListPb; + baseFuncs_[VIBRATE] = &MiscdeviceServiceStub::VibratePb; + baseFuncs_[CANCEL_VIBRATOR] = &MiscdeviceServiceStub::CancelVibratorPb; + baseFuncs_[PLAY_VIBRATOR_EFFECT] = &MiscdeviceServiceStub::PlayVibratorEffectPb; + baseFuncs_[PLAY_CUSTOM_VIBRATOR_EFFECT] = &MiscdeviceServiceStub::PlayCustomVibratorEffectPb; + baseFuncs_[STOP_VIBRATOR_EFFECT] = &MiscdeviceServiceStub::StopVibratorEffectPb; + baseFuncs_[SET_VIBRATOR_PARA] = &MiscdeviceServiceStub::SetVibratorParameterPb; + baseFuncs_[GET_VIBRATOR_PARA] = &MiscdeviceServiceStub::GetVibratorParameterPb; + baseFuncs_[GET_LIGHT_SUPPORT_ID] = &MiscdeviceServiceStub::GetLightSupportIdPb; + baseFuncs_[IS_LIGHT_EFFECT_SUPPORT] = &MiscdeviceServiceStub::IsLightEffectSupportPb; + baseFuncs_[LIGHT] = &MiscdeviceServiceStub::LightPb; + baseFuncs_[PLAY_LIGHT_EFFECT] = &MiscdeviceServiceStub::PlayLightEffectPb; + baseFuncs_[STOP_LIGHT_EFFECT] = &MiscdeviceServiceStub::StopLightEffectPb; +} + +MiscdeviceServiceStub::~MiscdeviceServiceStub() +{ + HiLog::Info(LABEL, "%{public}s begin, xigou %{public}p", __func__, this); + baseFuncs_.clear(); +} + +int32_t MiscdeviceServiceStub::IsAbilityAvailablePb(MessageParcel &data, MessageParcel &reply) +{ + MiscdeviceDeviceId groupId = static_cast(data.ReadUint32()); + reply.WriteBool(IsAbilityAvailable(groupId)); + return NO_ERROR; +} + +int32_t MiscdeviceServiceStub::IsVibratorEffectAvailablePb(MessageParcel &data, MessageParcel &reply) +{ + int32_t vibratorId = data.ReadInt32(); + std::string effectType = data.ReadString(); + reply.WriteBool(IsVibratorEffectAvailable(vibratorId, effectType)); + return NO_ERROR; +} + +int32_t MiscdeviceServiceStub::VibratePb(MessageParcel &data, MessageParcel &reply) +{ + return Vibrate(data.ReadInt32(), data.ReadUint32()); +} + +int32_t MiscdeviceServiceStub::GetVibratorIdListPb(MessageParcel &data, MessageParcel &reply) +{ + std::vector idSet = GetVibratorIdList(); + reply.WriteUint32(static_cast(idSet.size())); + reply.WriteInt32Vector(idSet); + return NO_ERROR; +} + +int32_t MiscdeviceServiceStub::CancelVibratorPb(MessageParcel &data, MessageParcel &reply) +{ + int32_t vibratorId = data.ReadInt32(); + return CancelVibrator(vibratorId); +} + +int32_t MiscdeviceServiceStub::PlayVibratorEffectPb(MessageParcel &data, MessageParcel &reply) +{ + int32_t vibratorId = data.ReadInt32(); + std::string effectType = data.ReadString(); + bool isLooping = data.ReadBool(); + return PlayVibratorEffect(vibratorId, effectType, isLooping); +} + +int32_t MiscdeviceServiceStub::PlayCustomVibratorEffectPb(MessageParcel &data, MessageParcel &reply) +{ + int32_t vibratorId = data.ReadInt32(); + std::vector timing; + data.ReadInt32Vector(&timing); + std::vector intensity; + data.ReadInt32Vector(&intensity); + int32_t periodCount = data.ReadInt32(); + return PlayCustomVibratorEffect(vibratorId, timing, intensity, periodCount); +} + +int32_t MiscdeviceServiceStub::StopVibratorEffectPb(MessageParcel &data, MessageParcel &reply) +{ + int32_t vibratorId = data.ReadInt32(); + std::string effectType = data.ReadString(); + return StopVibratorEffect(vibratorId, effectType); +} + +int32_t MiscdeviceServiceStub::SetVibratorParameterPb(MessageParcel &data, MessageParcel &reply) +{ + int32_t vibratorId = data.ReadInt32(); + std::string cmd = data.ReadString(); + return SetVibratorParameter(vibratorId, cmd); +} + +int32_t MiscdeviceServiceStub::GetVibratorParameterPb(MessageParcel &data, MessageParcel &reply) +{ + int32_t vibratorId = data.ReadInt32(); + std::string cmd = data.ReadString(); + std::string ret = GetVibratorParameter(vibratorId, cmd); + reply.WriteString(ret); + return NO_ERROR; +} + +int32_t MiscdeviceServiceStub::GetLightSupportIdPb(MessageParcel &data, MessageParcel &reply) +{ + std::vector idSet = GetLightSupportId(); + int32_t setCount = int32_t { idSet.size() }; + reply.WriteInt32(setCount); + reply.WriteInt32Vector(idSet); + return NO_ERROR; +} + +int32_t MiscdeviceServiceStub::IsLightEffectSupportPb(MessageParcel &data, MessageParcel &reply) +{ + int32_t id = data.ReadInt32(); + std::string effect = data.ReadString(); + reply.WriteBool(IsLightEffectSupport(id, effect)); + return NO_ERROR; +} + +int32_t MiscdeviceServiceStub::LightPb(MessageParcel &data, MessageParcel &reply) +{ + int32_t id = data.ReadInt32(); + uint32_t brightness = data.ReadUint64(); + uint32_t timeOn = data.ReadUint32(); + uint32_t timeOff = data.ReadUint32(); + return Light(id, brightness, timeOn, timeOff); +} + +int32_t MiscdeviceServiceStub::PlayLightEffectPb(MessageParcel &data, MessageParcel &reply) +{ + int32_t id = data.ReadInt32(); + std::string type = data.ReadString(); + return PlayLightEffect(id, type); +} + +int32_t MiscdeviceServiceStub::StopLightEffectPb(MessageParcel &data, MessageParcel &reply) +{ + int32_t id = data.ReadInt32(); + return StopLightEffect(id); +} + +int32_t MiscdeviceServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, + MessageOption &option) +{ + HiLog::Debug(LABEL, "%{public}s begin, cmd : %{public}u", __func__, code); + std::u16string descriptor = MiscdeviceServiceStub::GetDescriptor(); + std::u16string remoteDescriptor = data.ReadInterfaceToken(); + if (descriptor != remoteDescriptor) { + HiLog::Error(LABEL, "%{public}s client and service descriptors are inconsistent", __func__); + return OBJECT_NULL; + } + auto itFunc = baseFuncs_.find(code); + if (itFunc != baseFuncs_.end()) { + auto memberFunc = itFunc->second; + if (memberFunc != nullptr) { + return (this->*memberFunc)(data, reply); + } + } + HiLog::Debug(LABEL, "%{public}s no member function default process", __func__); + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); +} +} // namespace Sensors +} // namespace OHOS diff --git a/services/miscdevice_service/test/BUILD.gn b/services/miscdevice_service/test/BUILD.gn new file mode 100755 index 0000000000000000000000000000000000000000..5b6f24cf7f2e416a015b88763f3519846ed2cddc --- /dev/null +++ b/services/miscdevice_service/test/BUILD.gn @@ -0,0 +1,23 @@ +# Copyright (c) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/test.gni") + +############################################################################### +group("unittest") { + testonly = true + deps = [ "unittest/common:unittest" ] + if (is_phone_product) { + deps += [ "unittest/phone:unittest" ] + } +} diff --git a/services/miscdevice_service/test/unittest/common/BUILD.gn b/services/miscdevice_service/test/unittest/common/BUILD.gn new file mode 100755 index 0000000000000000000000000000000000000000..824763e082f928ac10685593e4c46fc517805f81 --- /dev/null +++ b/services/miscdevice_service/test/unittest/common/BUILD.gn @@ -0,0 +1,57 @@ +# Copyright (c) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/test.gni") + +SUBSYSTEM_DIR = "//base/sensors" +module_output_path = "sensors/miscdevice/services" + +ohos_unittest("MiscdeviceServiceCommonTest") { + module_out_path = module_output_path + if (is_phone_product || is_wearable_product) { + sources = [ "./miscdevice_service_test.cpp" ] + } + + include_dirs = [ + "//utils/native/base/include", + "//utils/system/safwk/native/include", + "$SUBSYSTEM_DIR/miscdevice/frameworks/native/miscdevice/include", + "$SUBSYSTEM_DIR/miscdevice/utils/include", + "//drivers/peripheral/misc/vibrator/interfaces/include", + "$SUBSYSTEM_DIR/miscdevice/services/miscdevice_service/include", + ] + + deps = [ + "$SUBSYSTEM_DIR/miscdevice/frameworks/native/miscdevice:libvibrator_native", + "$SUBSYSTEM_DIR/miscdevice/services/miscdevice_service:libmiscdevice_service", + "$SUBSYSTEM_DIR/miscdevice/utils:libmiscdevice_utils", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + "//utils/native/base:utils", + ] + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr_L2:samgr_proxy", + ] +} + +####################################################### + +group("unittest") { + testonly = true + if (is_phone_product || is_wearable_product) { + deps = [ ":MiscdeviceServiceCommonTest" ] + } +} diff --git a/services/miscdevice_service/test/unittest/common/miscdevice_service_test.cpp b/services/miscdevice_service/test/unittest/common/miscdevice_service_test.cpp new file mode 100755 index 0000000000000000000000000000000000000000..510478cfd5fd5cca12da3f130ae45058ab1eecc1 --- /dev/null +++ b/services/miscdevice_service/test/unittest/common/miscdevice_service_test.cpp @@ -0,0 +1,131 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include + +#include "ipc_skeleton.h" +#include "iservice_registry.h" +#include "string_ex.h" +#include "system_ability_definition.h" + +#include "miscdevice_service_proxy.h" +#include "sensors_errors.h" +#include "sensors_log_domain.h" + +#include "miscdevice_service.h" +#include "miscdevice_service_impl.h" +#include "singleton.h" +#include "vibrator_if.h" +#include "vibrator_type.h" + +namespace OHOS { +namespace Sensors { +using namespace testing::ext; +using namespace OHOS::HiviewDFX; + +namespace { +constexpr HiLogLabel LABEL = { LOG_CORE, SensorsLogDomain::COMMON, "MiscdeviceServiceTest" }; +const std::string CMD_LINE = "ps -ef | grep 'hardware.light' | grep -v grep | awk '{print $2}'"; +constexpr int32_t BUFFER_SIZE = 8; +constexpr pid_t INVALID_PID = -1; +} // namespace + +class MiscdeviceServiceTest : public testing::Test { +public: + void SetUp(); + void TearDown(); + pid_t GetSensorServicePid(); + MiscdeviceServiceImpl &vibratorServiceImpl_ = MiscdeviceServiceImpl::GetInstance(); + int32_t vibratorId_ = 123; +}; + +void MiscdeviceServiceTest::SetUp() +{ + auto sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + ASSERT_NE(sm, nullptr); + int32_t ret = vibratorServiceImpl_.InitVibratorServiceImpl(); + ASSERT_EQ(ret, ERR_OK); +} + +void MiscdeviceServiceTest::TearDown() +{} + +pid_t MiscdeviceServiceTest::GetSensorServicePid() +{ + pid_t pid = INVALID_PID; + char buf[BUFFER_SIZE] = { 0 }; + FILE *fp = popen(CMD_LINE.c_str(), "r"); + if (fp == nullptr) { + HiLog::Error(LABEL, "get error when getting sensor service process id"); + return pid; + } + + fgets(buf, sizeof(buf) - 1, fp); + pclose(fp); + fp = nullptr; + HiLog::Info(LABEL, "process is : %{public}s", buf); + + std::string pidStr(buf); + pidStr = TrimStr(pidStr, '\n'); + HiLog::Info(LABEL, "pidStr is : %{public}s", pidStr.c_str()); + if (pidStr.empty()) { + return pid; + } + + if (IsNumericStr(pidStr)) { + pid = std::stoi(pidStr); + } + return pid; +} +/* + * @tc.name: StopVibrator_001 + * @tc.desc: Test StopVibrator API functionality. + * @tc.type: FUNC + * @tc.require: + * @tc.author: wangcaiyi + */ +HWTEST_F(MiscdeviceServiceTest, StopVibrator_001, TestSize.Level1) +{ + uint32_t time = 5695; + int32_t ret = vibratorServiceImpl_.StartOnceVibrator(time); + std::this_thread::sleep_for(std::chrono::milliseconds(3000)); + ASSERT_EQ(ret, NO_ERROR); + ret = vibratorServiceImpl_.StopVibratorImpl(VIBRATOR_MODE_ONCE); + std::this_thread::sleep_for(std::chrono::milliseconds(3000)); + ASSERT_EQ(ret, NO_ERROR); +} + +/* + * @tc.name: StartVibrator_001 + * @tc.desc: Test StartVibrator API functionality. + * @tc.type: FUNC + * @tc.require: + * @tc.author: wangcaiyi + */ +HWTEST_F(MiscdeviceServiceTest, StartVibrator_001, TestSize.Level1) +{ + const char *builtIn = "vibrator.haptic.default.effect"; + int32_t ret = vibratorServiceImpl_.StartVibrator(builtIn); + std::this_thread::sleep_for(std::chrono::milliseconds(3000)); + ASSERT_EQ(ret, NO_ERROR); + ret = vibratorServiceImpl_.StopVibratorImpl(VIBRATOR_MODE_PRESET); + std::this_thread::sleep_for(std::chrono::milliseconds(3000)); + ASSERT_EQ(ret, NO_ERROR); +} +} // namespace Sensors +} // namespace OHOS diff --git a/services/miscdevice_service/test/unittest/phone/BUILD.gn b/services/miscdevice_service/test/unittest/phone/BUILD.gn new file mode 100755 index 0000000000000000000000000000000000000000..a8dd29093104d73f923bdce4475c6f8434465770 --- /dev/null +++ b/services/miscdevice_service/test/unittest/phone/BUILD.gn @@ -0,0 +1,50 @@ +# Copyright (c) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/test.gni") + +SUBSYSTEM_DIR = "//base/sensors" +module_output_path = "sensors/miscdevice/services" + +ohos_unittest("MiscdeviceServiceTest") { + module_out_path = module_output_path + sources = [ "./miscdevice_service_test.cpp" ] + + include_dirs = [ + "//utils/native/base/include", + "//utils/system/safwk/native/include", + "$SUBSYSTEM_DIR/miscdevice/frameworks/native/miscdevice/include", + "$SUBSYSTEM_DIR/miscdevice/utils/include", + "//drivers/peripheral/misc/vibrator/interfaces/include", + ] + + deps = [ + "$SUBSYSTEM_DIR/miscdevice/frameworks/native/miscdevice:libvibrator_native", + "$SUBSYSTEM_DIR/miscdevice/services/miscdevice_service:libmiscdevice_service", + "$SUBSYSTEM_DIR/miscdevice/utils:libmiscdevice_utils", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + "//utils/native/base:utils", + ] + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr_L2:samgr_proxy", + ] +} + +group("unittest") { + testonly = true + deps = [ ":MiscdeviceServiceTest" ] +} diff --git a/services/miscdevice_service/test/unittest/phone/miscdevice_service_test.cpp b/services/miscdevice_service/test/unittest/phone/miscdevice_service_test.cpp new file mode 100755 index 0000000000000000000000000000000000000000..c8686fcdcb30b0b7c854e384a9caf48ad7149a71 --- /dev/null +++ b/services/miscdevice_service/test/unittest/phone/miscdevice_service_test.cpp @@ -0,0 +1,128 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include + +#include "ipc_skeleton.h" +#include "iservice_registry.h" +#include "string_ex.h" +#include "system_ability_definition.h" + +#include "miscdevice_service_proxy.h" +#include "sensors_errors.h" +#include "sensors_log_domain.h" + +#include "vibrator_if.h" +#include "vibrator_type.h" + +namespace OHOS { +namespace Sensors { +using namespace testing::ext; +using namespace OHOS::HiviewDFX; + +namespace { +constexpr HiLogLabel LABEL = { LOG_CORE, SensorsLogDomain::COMMON, "MiscdeviceServiceTest" }; +const std::string CMD_LINE = "ps -ef | grep 'hardware.light' | grep -v grep | awk '{print $2}'"; +constexpr int32_t BUFFER_SIZE = 8; +constexpr pid_t INVALID_PID = -1; +} // namespace + +class MiscdeviceServiceTest : public testing::Test { +public: + void SetUp(); + void TearDown(); + pid_t GetSensorServicePid(); + sptr miscDeviceProxy_; +}; + +void MiscdeviceServiceTest::SetUp() +{ + auto sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + ASSERT_NE(sm, nullptr); + miscDeviceProxy_ = iface_cast(sm->GetSystemAbility(MISCDEVICE_SERVICE_ABILITY_ID)); + HiLog::Info(LABEL, "%{public}s end", __func__); +} + +void MiscdeviceServiceTest::TearDown() +{ + miscDeviceProxy_ = nullptr; +} + +pid_t MiscdeviceServiceTest::GetSensorServicePid() +{ + pid_t pid = INVALID_PID; + char buf[BUFFER_SIZE] = { 0 }; + FILE *fp = popen(CMD_LINE.c_str(), "r"); + if (fp == nullptr) { + HiLog::Error(LABEL, "get error when getting sensor service process id"); + return pid; + } + + fgets(buf, sizeof(buf) - 1, fp); + pclose(fp); + fp = nullptr; + HiLog::Info(LABEL, "process is : %{public}s", buf); + + std::string pidStr(buf); + pidStr = TrimStr(pidStr, '\n'); + HiLog::Info(LABEL, "pidStr is : %{public}s", pidStr.c_str()); + if (pidStr.empty()) { + return pid; + } + + if (IsNumericStr(pidStr)) { + pid = std::stoi(pidStr); + } + return pid; +} + +/* + * @tc.name: Light_ON_001 + * @tc.desc: light on + * @tc.type: FUNC + */ +HWTEST_F(MiscdeviceServiceTest, Light_ON_001, TestSize.Level1) +{ + ASSERT_NE(miscDeviceProxy_, nullptr); + std::vector setTest = miscDeviceProxy_->GetLightSupportId(); + if (setTest.size() == 0) { + return; + } + uint32_t corlorBrightness = 0x00ffffff; + int32_t ret = miscDeviceProxy_->Light(LightId::LIGHT_ID_LED, corlorBrightness, 0, 0); + ASSERT_EQ(ret, NO_ERROR); +} + +/* + * @tc.name: Light_OFF_001 + * @tc.desc: light off + * @tc.type: FUNC + */ +HWTEST_F(MiscdeviceServiceTest, Light_OFF_001, TestSize.Level1) +{ + ASSERT_NE(miscDeviceProxy_, nullptr); + std::vector setTest = miscDeviceProxy_->GetLightSupportId(); + if (setTest.size() == 0) { + return; + } + uint32_t corlorBrightness = 0; + int32_t ret = miscDeviceProxy_->Light(LightId::LIGHT_ID_LED, corlorBrightness, 0, 0); + ASSERT_EQ(ret, NO_ERROR); +} +} // namespace Sensors +} // namespace OHOS diff --git a/utils/BUILD.gn b/utils/BUILD.gn new file mode 100755 index 0000000000000000000000000000000000000000..bf09b683c788cb30a07fe0273e5df328e7eff575 --- /dev/null +++ b/utils/BUILD.gn @@ -0,0 +1,41 @@ +# Copyright (c) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") +ohos_shared_library("libmiscdevice_utils") { + sources = [ + "src/dmd_report.cpp", + "src/miscdevice_common.cpp", + ] + + include_dirs = [ + "include", + "//utils/native/base/include", + "//utils/system/safwk/native/include", + ] + deps = [ "//utils/native/base:utils" ] + + external_deps = [ + "hisysevent_native:libhisysevent", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr_L2:samgr_proxy", + ] + part_name = "sensors_miscdevice" + subsystem_name = "sensors" +} + +group("miscdevice_utils_target") { + deps = [ ":libmiscdevice_utils" ] +} diff --git a/utils/include/app_thread_info.h b/utils/include/app_thread_info.h new file mode 100755 index 0000000000000000000000000000000000000000..0a765d135214d1c2c8aa7570230703615e07c22d --- /dev/null +++ b/utils/include/app_thread_info.h @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef APP_THREAD_INFO_H +#define APP_THREAD_INFO_H + +namespace OHOS { +namespace Sensors { +struct AppThreadInfo { + int32_t pid; + int32_t uid; + AppThreadInfo() : pid(0), uid(0) {}; +}; +} // namespace Sensors +} // namespace OHOS +#endif // APP_THREAD_INFO_H diff --git a/utils/include/death_recipient_template.h b/utils/include/death_recipient_template.h new file mode 100755 index 0000000000000000000000000000000000000000..d3a13a7a36a0e2a7f5564cf96b5e4786cadb476b --- /dev/null +++ b/utils/include/death_recipient_template.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef DEATH_RECIPIENT_TEMPLATE_H +#define DEATH_RECIPIENT_TEMPLATE_H + +#include "iremote_object.h" + +namespace OHOS { +namespace Sensors { +template +class DeathRecipientTemplate : public IRemoteObject::DeathRecipient { +public: + explicit DeathRecipientTemplate(T &privateData) : privateData_(privateData){}; + virtual ~DeathRecipientTemplate() = default; + virtual void OnRemoteDied(const wptr &object) + { + privateData_.ProcessDeathObserver(object); + }; + +private: + T &privateData_; +}; +} // namespace Sensors +} // namespace OHOS +#endif // DEATH_RECIPIENT_TEMPLATE_H diff --git a/utils/include/dmd_report.h b/utils/include/dmd_report.h new file mode 100755 index 0000000000000000000000000000000000000000..a47975c489bceb684aa9a9fa6f335dd08c89340d --- /dev/null +++ b/utils/include/dmd_report.h @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef DMD_REPORT_H +#define DMD_REPORT_H + +#include +#include +#include + +namespace OHOS { +namespace Sensors { +enum SensorEventId { + JNI_ENV_VAR_EXCEPTION = 951000106, + CLASS_NOT_FOUND = 951000107, + NATIVE_METHOD_REGISTER_EXCEPTION = 951000108, + JAVA_VM_THREAD_ATTACH_EXCEPTION = 951000109, + SENSOR_SERVICE_EXCEPTION = 951000110, + MISC_SERVICE_EXCEPTION = 951000111, + SENSOR_SERVICE_IPC_EXCEPTION = 951000112, + MISC_SERVICE_IPC_EXCEPTION = 951000113, + SENSOR_HIDL_SERVICE_EXCEPTION = 951000114, + LIGHT_HIDL_SERVICE_EXCEPTION = 951000115, + VIBRATOR_HIDL_SERVICE_EXCEPTION = 951000116, + SENSOR_DATA_CHANNEL_EXCEPTION = 951000117, +}; + +class DmdReport { +public: + DmdReport() = default; + virtual ~DmdReport() = default; + static void ReportException(int32_t eventId, const std::string &interfaceName, int32_t error); + +private: + static std::mutex eventMutex_; + static std::map eventMap_; +}; +} // namespace Sensors +} // namespace OHOS +#endif // DMD_REPORT_H diff --git a/utils/include/miscdevice_common.h b/utils/include/miscdevice_common.h new file mode 100755 index 0000000000000000000000000000000000000000..9932d560ab22cd7ab571f70b3e648905835637f4 --- /dev/null +++ b/utils/include/miscdevice_common.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef MISCDEVICE_COMMON_H +#define MISCDEVICE_COMMON_H + +#include +#include + +namespace OHOS { +namespace Sensors { +// These LightId correspand to logical lights +enum LightId { + LIGHT_ID_LED = 0, + LIGHT_ID_KEYBOARD, + LIGHT_ID_BUTTONS, + LIGHT_ID_BELT, + + UNKNOWN, +}; + +// MISC device support id +enum class MiscdeviceDeviceId { LED = 0, VIBRATOR, UNKNOWN }; + +class MiscdeviceCommon { +public: + MiscdeviceCommon() = default; + ~MiscdeviceCommon() = default; + static bool CheckCustomVibratorEffect(const std::vector &timing, const std::vector &intensity, + int32_t periodCount); +}; +} // namespace Sensors +} // namespace OHOS +#endif // MISCDEVICE_COMMON_H diff --git a/utils/include/sensors_errors.h b/utils/include/sensors_errors.h new file mode 100755 index 0000000000000000000000000000000000000000..a05258d222f0564c75f9db7789f6d3be786dc873 --- /dev/null +++ b/utils/include/sensors_errors.h @@ -0,0 +1,127 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef SENSORS_ERRORS_H +#define SENSORS_ERRORS_H + +#include + +namespace OHOS { +namespace Sensors { +enum { + MODULE_COMMON = 0x00, + MODULE_SENSORS_ADAPTER = 0x01, + MODULE_SENSOR_SERVICE = 0x02, + MODULE_SENSORS_UTILS = 0x03, + MODULE_MISCDEVICE_SERVICE = 0x04, + MODULE_SENSORS_NATIVE = 0X05, +}; + +// Error code for common +constexpr ErrCode COMMON_ERR_OFFSET = ErrCodeOffset(SUBSYS_SENSORS, MODULE_COMMON); + +enum { + ERROR = -1, + SUCCESS = 0, + COMMON_ERR = COMMON_ERR_OFFSET, +}; + +// Error code for adapter +constexpr ErrCode ADAPTER_ERR_OFFSET = ErrCodeOffset(SUBSYS_SENSORS, MODULE_SENSORS_ADAPTER); + +enum { + ADAPTER_ERR = ADAPTER_ERR_OFFSET, + ADAPTER_ENABLE_SENSOR_ERR = ADAPTER_ERR_OFFSET + 1, + ADAPTER_DISABLE_SENSOR_ERR = ADAPTER_ENABLE_SENSOR_ERR + 1, + ADAPTER_RUN_COMMAND_ERR = ADAPTER_DISABLE_SENSOR_ERR + 1, + ADAPTER_SET_SENSOR_CONFIG_ERR = ADAPTER_RUN_COMMAND_ERR + 1, + ADAPTER_NOT_SUPPORT_CMD_ERR = ADAPTER_SET_SENSOR_CONFIG_ERR + 1, +}; + +// Error code for sensor service +constexpr ErrCode SENSOR_SERVICE_ERR_OFFSET = ErrCodeOffset(SUBSYS_SENSORS, MODULE_SENSOR_SERVICE); + +enum { + SENSOR_SERVICE_ERR = SENSOR_SERVICE_ERR_OFFSET, + CMD_TYPE_ERR = SENSOR_SERVICE_ERR + 1, + SENSOR_DEVICE_INIT_ERR = CMD_TYPE_ERR + 1, + CONNECT_SENSOR_HIDL_ERR = SENSOR_DEVICE_INIT_ERR + 1, + SET_SENSOR_CONFIG_ERR = CONNECT_SENSOR_HIDL_ERR + 1, + ENABLE_SENSOR_ERR = SET_SENSOR_CONFIG_ERR + 1, + DISABLE_SENSOR_ERR = ENABLE_SENSOR_ERR + 1, + RUN_COMMAND_ERR = DISABLE_SENSOR_ERR + 1, + GET_SENSOR_LIST_ERR = RUN_COMMAND_ERR + 1, + SENSOR_ENABLED_ERR = GET_SENSOR_LIST_ERR + 1, + UPDATE_SENSOR_CHANNEL_ERR = SENSOR_ENABLED_ERR + 1, + CLEAR_SENSOR_CHANNEL_ERR = UPDATE_SENSOR_CHANNEL_ERR + 1, + CLEAR_SENSOR_INFO_ERR = CLEAR_SENSOR_CHANNEL_ERR + 1, + UPDATE_SENSOR_INFO_ERR = CLEAR_SENSOR_INFO_ERR + 1, + CLIENT_PID_INVALID_ERR = UPDATE_SENSOR_INFO_ERR + 1, + DESTROY_SENSOR_CHANNEL_ERR = CLIENT_PID_INVALID_ERR + 1, + UPDATE_UID_ERR = DESTROY_SENSOR_CHANNEL_ERR + 1, + INVALID_POINTER = UPDATE_UID_ERR + 1, + NO_EVENT = INVALID_POINTER + 1, + COPY_ERR = NO_EVENT + 1, + REGIST_PERMISSION_CHANGED_ERR = COPY_ERR + 1, + DUMP_PARAM_ERR = REGIST_PERMISSION_CHANGED_ERR + 1, + WRITE_MSG_ERR = DUMP_PARAM_ERR + 1, +}; + +// Error code for miscdevice service +constexpr ErrCode MISCDEVICE_SERVICE_ERR_OFFSET = ErrCodeOffset(SUBSYS_SENSORS, MODULE_MISCDEVICE_SERVICE); + +enum { + LIGHT_HIDL_CONNECT_ERR = MISCDEVICE_SERVICE_ERR_OFFSET, + LIGHT_ID_NOT_SUPPORT = LIGHT_HIDL_CONNECT_ERR + 1, + LIGHT_ERR = LIGHT_ID_NOT_SUPPORT + 1, + LIGHT_PLAY_EFFECT_ERROR = LIGHT_ERR + 1, + LIGHT_STOP_EFFECT_ERROR = LIGHT_PLAY_EFFECT_ERROR + 1, + LIGHT_END_ERROR = LIGHT_STOP_EFFECT_ERROR, + VIBRATOR_HIDL_CONNECT_ERR = LIGHT_END_ERROR + 1, + VIBRATOR_ON_ERR = VIBRATOR_HIDL_CONNECT_ERR + 1, + VIBRATOR_OFF_ERR = VIBRATOR_ON_ERR + 1, + VIBRATOR_PLAY_EFFECT_ERR = VIBRATOR_OFF_ERR + 1, + VIBRATOR_STOP_EFFECT_ERR = VIBRATOR_PLAY_EFFECT_ERR + 1, + VIBRATOR_SET_PARA_ERR = VIBRATOR_STOP_EFFECT_ERR + 1, +}; +// Error code for Sensor uitls +constexpr ErrCode SENSOR_UTILS_ERR_OFFSET = ErrCodeOffset(SUBSYS_SENSORS, MODULE_SENSORS_UTILS); +enum { + SENSOR_CHANNEL_SOCKET_CREATE_ERR = SENSOR_UTILS_ERR_OFFSET, + SENSOR_CHANNEL_SENDFD_ERR = SENSOR_CHANNEL_SOCKET_CREATE_ERR + 1, + SENSOR_CHANNEL_WRITE_DESCRIPTOR_ERR = SENSOR_CHANNEL_SENDFD_ERR + 1, + SENSOR_CHANNEL_DUP_ERR = SENSOR_CHANNEL_WRITE_DESCRIPTOR_ERR + 1, + SENSOR_CHANNEL_BASIC_CHANNEL_NOT_INIT = SENSOR_CHANNEL_DUP_ERR + 1, + SENSOR_CHANNEL_SEND_ADDR_ERR = SENSOR_CHANNEL_BASIC_CHANNEL_NOT_INIT + 1, + SENSOR_CHANNEL_SEND_DATA_ERR = SENSOR_CHANNEL_SEND_ADDR_ERR + 1, + SENSOR_CHANNEL_RECEIVE_DATA_ERR = SENSOR_CHANNEL_SEND_DATA_ERR + 1, + SENSOR_CHANNEL_RECEIVE_ADDR_ERR = SENSOR_CHANNEL_RECEIVE_DATA_ERR + 1, + SENSOR_CHANNEL_RESTORE_CB_ERR = SENSOR_CHANNEL_RECEIVE_ADDR_ERR + 1, + SENSOR_CHANNEL_RESTORE_FD_ERR = SENSOR_CHANNEL_RESTORE_CB_ERR + 1, + SENSOR_CHANNEL_RESTORE_THREAD_ERR = SENSOR_CHANNEL_RESTORE_FD_ERR + 1, +}; +// Error code for Sensor native +constexpr ErrCode SENSOR_NATIVE_ERR_OFFSET = ErrCodeOffset(SUBSYS_SENSORS, MODULE_SENSORS_NATIVE); + +enum { + SENSOR_NATIVE_SAM_ERR = SENSOR_NATIVE_ERR_OFFSET, + SENSOR_NATIVE_GET_SERVICE_ERR = SENSOR_NATIVE_SAM_ERR + 1, + SENSOR_NATIVE_REGSITER_CB_ERR = SENSOR_NATIVE_GET_SERVICE_ERR + 1, + MISC_NATIVE_GET_SERVICE_ERR = SENSOR_NATIVE_REGSITER_CB_ERR + 1, + MISC_NATIVE_SAM_ERR = MISC_NATIVE_GET_SERVICE_ERR + 1, +}; +} // namespace Sensors +} // namespace OHOS +#endif // SENSORS_ERRORS_H diff --git a/utils/include/sensors_log_domain.h b/utils/include/sensors_log_domain.h new file mode 100755 index 0000000000000000000000000000000000000000..4de1c0321e477b4d5797ba34090a1959986e276b --- /dev/null +++ b/utils/include/sensors_log_domain.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef SENSORS_LOG_DOMAIN_H +#define SENSORS_LOG_DOMAIN_H + +#include "hilog/log.h" + +namespace OHOS { +namespace SensorsLogDomain { +constexpr uint32_t COMMON = 0xD002700; +constexpr uint32_t SENSOR_ADAPTER = 0xD002701; +constexpr uint32_t SENSOR_SERVICE = 0xD002702; +constexpr uint32_t SENSOR_UTILS = 0xD002703; +constexpr uint32_t SENSOR_TEST = 0xD002704; +constexpr uint32_t SENSOR_NATIVE = 0xD002705; +constexpr uint32_t SENSOR_JNI = 0xD002706; +constexpr uint32_t SENSORS_IMPLEMENT = 0xD002707; +constexpr uint32_t SENSORS_INTERFACE = 0xD002708; +constexpr uint32_t MISCDEVICE_SERVICE = 0xD002751; +constexpr uint32_t MISCDEVICE_FWK = 0xD002752; +constexpr uint32_t MISCDEVICE_LIGHT_JNI = 0xD002753; +constexpr uint32_t MISCDEVICE_VIBRATOR_JNI = 0xD002754; +constexpr uint32_t MISCDEVICE_LIGHT_NATIVE = 0xD002755; +constexpr uint32_t MISCDEVICE_VIBRATOR_NATIVE = 0xD002756; +constexpr uint32_t MISCDEVICE_VIBRATOR_INTERFACE = 0xD002757; +} // namespace SensorsLogDomain +} // namespace OHOS +#endif // SENSORS_LOG_DOMAIN_H diff --git a/utils/src/dmd_report.cpp b/utils/src/dmd_report.cpp new file mode 100755 index 0000000000000000000000000000000000000000..ed1ed73d6b7c36d0223dc1e5d43697f80cdf0cc7 --- /dev/null +++ b/utils/src/dmd_report.cpp @@ -0,0 +1,102 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "dmd_report.h" + +#include "datetime_ex.h" +#include "hisysevent.h" + +#include "sensors_errors.h" +#include "sensors_log_domain.h" + +namespace OHOS { +namespace Sensors { +using namespace OHOS::HiviewDFX; + +namespace { +constexpr HiLogLabel LABEL = { LOG_CORE, SensorsLogDomain::SENSOR_UTILS, "DmdReport" }; +constexpr int32_t SECONDS_HALF_HOUR = 1800; +} // namespace + +std::map DmdReport::eventMap_ = { + { JNI_ENV_VAR_EXCEPTION, 0 }, + { CLASS_NOT_FOUND, 0 }, + { NATIVE_METHOD_REGISTER_EXCEPTION, 0 }, + { JAVA_VM_THREAD_ATTACH_EXCEPTION, 0 }, + { SENSOR_SERVICE_EXCEPTION, 0 }, + { MISC_SERVICE_EXCEPTION, 0 }, + { SENSOR_SERVICE_IPC_EXCEPTION, 0 }, + { MISC_SERVICE_IPC_EXCEPTION, 0 }, + { SENSOR_HIDL_SERVICE_EXCEPTION, 0 }, + { LIGHT_HIDL_SERVICE_EXCEPTION, 0 }, + { VIBRATOR_HIDL_SERVICE_EXCEPTION, 0 }, + { SENSOR_DATA_CHANNEL_EXCEPTION, 0 }, +}; + +std::mutex DmdReport::eventMutex_; + +static std::string GetEventName(int32_t eventId) +{ + switch (eventId) { + case JNI_ENV_VAR_EXCEPTION: + return "JniEnvVarException"; + case CLASS_NOT_FOUND: + return "ClassNotFound"; + case NATIVE_METHOD_REGISTER_EXCEPTION: + return "NativeMethodRegisterException"; + case JAVA_VM_THREAD_ATTACH_EXCEPTION: + return "JavaVmThreadAttachException"; + case SENSOR_SERVICE_EXCEPTION: + return "SensorServiceException"; + case MISC_SERVICE_EXCEPTION: + return "MiscServiceException"; + case SENSOR_SERVICE_IPC_EXCEPTION: + return "SensorServiceIpcException"; + case MISC_SERVICE_IPC_EXCEPTION: + return "MiscServiceIpcException"; + case SENSOR_HIDL_SERVICE_EXCEPTION: + return "SensorHidlServiceException"; + case LIGHT_HIDL_SERVICE_EXCEPTION: + return "LightHidlServiceException"; + case VIBRATOR_HIDL_SERVICE_EXCEPTION: + return "VibratorHidlServiceException"; + case SENSOR_DATA_CHANNEL_EXCEPTION: + return "SensorDataChannelException"; + default: + return ""; + } +} + +void DmdReport::ReportException(int32_t eventId, const std::string &interfaceName, int32_t error) +{ + HiLog::Debug(LABEL, "%{public}s begin", __func__); + std::lock_guard eventLock(eventMutex_); + auto eventIt = eventMap_.find(eventId); + if (eventIt == eventMap_.end()) { + HiLog::Error(LABEL, "%{public}s eventId : %{public}d doesn't support", __func__, eventId); + return; + } + int64_t curTime = GetSecondsSince1970ToNow(); + if ((curTime - eventIt->second) > SECONDS_HALF_HOUR) { + HiviewDFX::HiSysEvent::Write(HiviewDFX::HiSysEvent::Domain::SENSORS, GetEventName(eventId), + HiviewDFX::HiSysEvent::EventType::FAULT, interfaceName, error); + eventMap_[eventId] = curTime; + HiLog::Debug(LABEL, "%{public}s end", __func__); + return; + } + HiLog::Warn(LABEL, "%{public}s every eventId report one time half an hour", __func__); +} +} // namespace Sensors +} // namespace OHOS diff --git a/utils/src/miscdevice_common.cpp b/utils/src/miscdevice_common.cpp new file mode 100755 index 0000000000000000000000000000000000000000..5520dee94930a8b71b4e36ff1d3b59d6f623971c --- /dev/null +++ b/utils/src/miscdevice_common.cpp @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "miscdevice_common.h" +#include "sensors_log_domain.h" + +namespace OHOS { +namespace Sensors { +using namespace OHOS::HiviewDFX; + +namespace { +constexpr HiLogLabel LABEL = { LOG_CORE, SensorsLogDomain::SENSOR_UTILS, "MiscdeviceCommon" }; +constexpr int32_t MIN_VIBRATOR_COUNT = 0; +constexpr int32_t MAX_VIBRATOR_COUNT = 100; +constexpr int32_t MIN_VIBRATOR_INTENSITY = 0; +constexpr int32_t MAX_VIBRATOR_INTENSITY = 255; +constexpr int32_t HALF_AN_HOUR = 1800000; // ms +} // namespace + +bool MiscdeviceCommon::CheckCustomVibratorEffect(const std::vector &timing, + const std::vector &intensity, int32_t periodCount) +{ + if ((periodCount < MIN_VIBRATOR_COUNT) || (periodCount > MAX_VIBRATOR_COUNT)) { + HiLog::Error(LABEL, "%{public}s failed, input param invalid", __func__); + return false; + } + if (timing.size() != intensity.size()) { + HiLog::Error(LABEL, "%{public}s failed, timing size invalid", __func__); + return false; + } + int32_t totalTime = 0; + for (uint32_t i = 0; i < timing.size(); i = i + 2) { + totalTime += timing[i]; + } + if (totalTime > HALF_AN_HOUR) { + HiLog::Error(LABEL, "%{public}s failed, totalTime invalid", __func__); + return false; + } + for (uint32_t i = 0; i < intensity.size(); i++) { + if ((intensity[i] < MIN_VIBRATOR_INTENSITY) || (intensity[i] > MAX_VIBRATOR_INTENSITY)) { + return false; + } + } + return true; +} +} // namespace Sensors +} // namespace OHOS