diff --git a/frameworks/native/miscdevice/BUILD.gn b/frameworks/native/miscdevice/BUILD.gn index 99a430d1a0ce5de6c3cf593dc4272475360ddab1..89bc0ee3b76305afd8d491d3f0bd28a6c79e931e 100644 --- a/frameworks/native/miscdevice/BUILD.gn +++ b/frameworks/native/miscdevice/BUILD.gn @@ -15,6 +15,38 @@ import("//build/ohos.gni") SUBSYSTEM_DIR = "//base/sensors" +############################################## +ohos_shared_library("liblight_native") { + sources = [ + "src/light_client.cpp", + "src/miscdevice_service_proxy.cpp", + ] + + include_dirs = [ + "include", + "//commonlibrary/c_utils/base/include", + "//utils/system/safwk/native/include", + "$SUBSYSTEM_DIR/miscdevice/interfaces/native/light/include", + "$SUBSYSTEM_DIR/miscdevice/utils/include", + "//utils/jni/jnikit/include", + ] + cflags = [ "-fstack-protector-all" ] + deps = [ + "$SUBSYSTEM_DIR/miscdevice/interfaces/native/light:light_ndk_header", + "$SUBSYSTEM_DIR/miscdevice/utils:libmiscdevice_utils", + ] + + external_deps = [ + "c_utils:utils", + "hisysevent_native:libhisysevent", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "samgr:samgr_proxy", + ] + part_name = "miscdevice" + subsystem_name = "sensors" +} + ############################################## ohos_shared_library("libvibrator_native") { sources = [ @@ -26,6 +58,7 @@ ohos_shared_library("libvibrator_native") { "include", "//commonlibrary/c_utils/base/include", "//utils/system/safwk/native/include", + "$SUBSYSTEM_DIR/miscdevice/interfaces/native/light/include", "$SUBSYSTEM_DIR/miscdevice/utils/include", "//utils/jni/jnikit/include", "//foundation/communication/ipc/interfaces/innerkits/ipc_core/include", @@ -45,5 +78,8 @@ ohos_shared_library("libvibrator_native") { ############################################## group("miscdevice_native_target") { - deps = [ ":libvibrator_native" ] + 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 index 0c6936c166731b1fa8f8074c6dd8737f62cae129..2f2549e8dee5ae800efc960571d8b4a27eef448e 100755 --- a/frameworks/native/miscdevice/include/i_miscdevice_service.h +++ b/frameworks/native/miscdevice/include/i_miscdevice_service.h @@ -20,6 +20,7 @@ #include #include "iremote_broker.h" +#include "light_agent_type.h" #include "miscdevice_common.h" namespace OHOS { @@ -27,25 +28,25 @@ namespace Sensors { class IMiscdeviceService : public IRemoteBroker { public: IMiscdeviceService() = default; - virtual ~IMiscdeviceService() = default; - DECLARE_INTERFACE_DESCRIPTOR(u"IMiscdeviceService"); - virtual int32_t Vibrate(int32_t vibratorId, int32_t timeOut, int32_t usage) = 0; - virtual int32_t CancelVibrator(int32_t vibratorId) = 0; - virtual int32_t PlayVibratorEffect(int32_t vibratorId, const std::string &effect, int32_t loopCount, int32_t usage) = 0; - virtual int32_t StopVibratorEffect(int32_t vibratorId, const std::string &effect) = 0; + virtual std::vector GetLightList() = 0; + virtual int32_t TurnOn(int32_t lightId, const LightColor &color, const LightAnimation &animation) = 0; + virtual int32_t TurnOff(int32_t lightId) = 0; enum { VIBRATE, CANCEL_VIBRATOR, PLAY_VIBRATOR_EFFECT, - STOP_VIBRATOR_EFFECT + STOP_VIBRATOR_EFFECT, + GET_LIGHT_LIST, + TURN_ON, + TURN_OFF, }; }; } // namespace Sensors diff --git a/frameworks/native/miscdevice/include/light_client.h b/frameworks/native/miscdevice/include/light_client.h new file mode 100755 index 0000000000000000000000000000000000000000..a71baced024e41fe3323b8392e86d15bd45d5e7d --- /dev/null +++ b/frameworks/native/miscdevice/include/light_client.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2022 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_CLIENT_H +#define LIGHT_CLIENT_H + +#include + +#include "iremote_object.h" +#include "miscdevice_service_proxy.h" +#include "singleton.h" + +namespace OHOS { +namespace Sensors { +class LightClient : public Singleton { +public: + int32_t GetLightList(LightInfo **lightInfo, int32_t &count); + int32_t TurnOn(int32_t lightId, const LightColor &color, const LightAnimation &animation); + int32_t TurnOff(int32_t lightId); + void ProcessDeathObserver(wptr object); + +private: + bool IsValid(int32_t lightId); + void ClearLightInfos(); + int32_t ConvertLightInfos(); + int32_t InitLightClient(); + bool IsLightAnimationValid(const LightAnimation &animation); + bool IsLightIdValid(int32_t lightId); + std::mutex lightInfosMutex_; + LightInfo *lightInfos_ {nullptr}; + int32_t lightInfoCount_ {-1}; + sptr serviceDeathObserver_ = nullptr; + sptr miscdeviceProxy_ = nullptr; + std::vector lightInfoList_; + std::mutex clientMutex_; +}; +} // namespace Sensors +} // namespace OHOS +#endif // LIGHT_CLIENT_H diff --git a/frameworks/native/miscdevice/include/miscdevice_service_proxy.h b/frameworks/native/miscdevice/include/miscdevice_service_proxy.h index 3819b0a48c24f674b632184181b8c68eb1d2066a..3641dc28938147bb275df2e19b170d83d14033f1 100755 --- a/frameworks/native/miscdevice/include/miscdevice_service_proxy.h +++ b/frameworks/native/miscdevice/include/miscdevice_service_proxy.h @@ -25,17 +25,15 @@ namespace Sensors { class MiscdeviceServiceProxy : public IRemoteProxy { public: explicit MiscdeviceServiceProxy(const sptr &impl); - ~MiscdeviceServiceProxy() = default; - virtual int32_t Vibrate(int32_t vibratorId, int32_t timeOut, int32_t usage) override; - virtual int32_t CancelVibrator(int32_t vibratorId) override; - virtual int32_t PlayVibratorEffect(int32_t vibratorId, const std::string &effect, int32_t loopCount, int32_t usage) override; - virtual int32_t StopVibratorEffect(int32_t vibratorId, const std::string &effect) override; + virtual std::vector GetLightList() override; + virtual int32_t TurnOn(int32_t lightId, const LightColor &color, const LightAnimation &animation) override; + virtual int32_t TurnOff(int32_t lightId) override; private: DISALLOW_COPY_AND_MOVE(MiscdeviceServiceProxy); diff --git a/frameworks/native/miscdevice/src/light_client.cpp b/frameworks/native/miscdevice/src/light_client.cpp new file mode 100755 index 0000000000000000000000000000000000000000..b753b1948958a3e885c55045f2f0ee76100b98b5 --- /dev/null +++ b/frameworks/native/miscdevice/src/light_client.cpp @@ -0,0 +1,189 @@ +/* + * Copyright (c) 2022 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_client.h" + +#include +#include + +#include "death_recipient_template.h" +#include "hisysevent.h" +#include "iservice_registry.h" +#include "sensors_errors.h" +#include "system_ability_definition.h" + +namespace OHOS { +namespace Sensors { + +namespace { +constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MISC_LOG_DOMAIN, "LightClient" }; +constexpr int32_t GET_SERVICE_MAX_COUNT = 30; +constexpr uint32_t MAX_LIGHT_LIST_SIZE = 0X00ff; +constexpr uint32_t WAIT_MS = 200; +} // namespace + +int32_t LightClient::InitLightClient() +{ + CALL_LOG_ENTER; + std::lock_guard clientLock(clientMutex_); + if (miscdeviceProxy_ != nullptr) { + MISC_HILOGD("miscdeviceProxy_ already init"); + return ERR_OK; + } + auto systemManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + CHKPR(systemManager, MISC_NATIVE_SAM_ERR); + int32_t retry = 0; + while (retry < GET_SERVICE_MAX_COUNT) { + miscdeviceProxy_ = iface_cast(systemManager->GetSystemAbility( + MISCDEVICE_SERVICE_ABILITY_ID)); + if (miscdeviceProxy_ != nullptr) { + MISC_HILOGD("miscdeviceProxy_ get service success, retry:%{public}d", retry); + serviceDeathObserver_ = new (std::nothrow) DeathRecipientTemplate(*const_cast(this)); + if (serviceDeathObserver_ != nullptr) { + miscdeviceProxy_->AsObject()->AddDeathRecipient(serviceDeathObserver_); + } + lightInfoList_ = miscdeviceProxy_->GetLightList(); + return ERR_OK; + } + MISC_HILOGW("Get service failed, retry:%{public}d", retry); + std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_MS)); + retry++; + } + HiviewDFX::HiSysEvent::Write(HiviewDFX::HiSysEvent::Domain::MISCDEVICE, "MISC_SERVICE_EXCEPTION", + HiviewDFX::HiSysEvent::EventType::FAULT, "PKG_NAME", "InitLightClient", "ERROR_CODE", MISC_NATIVE_GET_SERVICE_ERR); + MISC_HILOGE("Get service failed"); + return MISC_NATIVE_GET_SERVICE_ERR; +} + +bool LightClient::IsLightIdValid(int32_t lightId) +{ + int32_t ret = InitLightClient(); + if (ret != ERR_OK) { + MISC_HILOGE("InitLightClient failed, ret:%{public}d", ret); + return false; + } + for (const auto &item : lightInfoList_) { + if (lightId == item.lightId) { + return true; + } + } + return false; +} + +int32_t LightClient::GetLightList(LightInfo **lightInfo, int32_t &count) +{ + CALL_LOG_ENTER; + CHKPR(lightInfo, ERROR); + int32_t ret = InitLightClient(); + if (ret != ERR_OK) { + MISC_HILOGE("InitLightClient failed"); + return ERROR; + } + std::lock_guard lightInfosLock(lightInfosMutex_); + if (lightInfos_ == nullptr) { + int32_t ret = ConvertLightInfos(); + if (ret != ERR_OK) { + MISC_HILOGE("Convert light lists failed"); + ClearLightInfos(); + return ERROR; + } + } + *lightInfo = lightInfos_; + count = lightInfoCount_; + return ERR_OK; +} + +bool LightClient::IsLightAnimationValid(const LightAnimation &animation) +{ + if ((animation.mode < 0) || (animation.mode > LIGHT_MODE_BUTT)) { + MISC_HILOGE("animation mode is invalid, mode:%{pubilc}d", animation.mode); + return false; + } + if ((animation.onTime < 0) || (animation.offTime < 0)) { + MISC_HILOGE("animation onTime or offTime is invalid, onTime:%{pubilc}d, offTime:%{pubilc}d", + animation.onTime, animation.offTime); + return false; + } + return true; +} + +int32_t LightClient::TurnOn(int32_t lightId, const LightColor &color, const LightAnimation &animation) +{ + CALL_LOG_ENTER; + if (!IsLightIdValid(lightId)) { + MISC_HILOGE("lightId is invalid, lightId:%{pubilc}d", lightId); + return PARAMETER_ERROR; + } + if (!IsLightAnimationValid(animation)) { + MISC_HILOGE("animation is invalid"); + return PARAMETER_ERROR; + } + CHKPR(miscdeviceProxy_, ERROR); + return miscdeviceProxy_->TurnOn(lightId, color, animation); +} + +int32_t LightClient::TurnOff(int32_t lightId) +{ + CALL_LOG_ENTER; + if (!IsLightIdValid(lightId)) { + MISC_HILOGE("lightId is invalid, lightId:%{pubilc}d", lightId); + return LIGHT_ID_NOT_SUPPORT; + } + CHKPR(miscdeviceProxy_, ERROR); + return miscdeviceProxy_->TurnOff(lightId); +} + +void LightClient::ProcessDeathObserver(wptr object) +{ + CALL_LOG_ENTER; + (void)object; + miscdeviceProxy_ = nullptr; + auto ret = InitLightClient(); + if (ret != ERR_OK) { + MISC_HILOGE("InitLightClient failed, ret:%{public}d", ret); + return; + } +} + +void LightClient::ClearLightInfos() +{ + CHKPV(lightInfos_); + free(lightInfos_); + lightInfos_ = nullptr; +} + +int32_t LightClient::ConvertLightInfos() +{ + CALL_LOG_ENTER; + if (lightInfoList_.empty()) { + MISC_HILOGE("get light lists failed"); + return ERROR; + } + size_t count = lightInfoList_.size(); + if (count > MAX_LIGHT_LIST_SIZE) { + MISC_HILOGE("The number of lights exceed the maximum value"); + return ERROR; + } + std::lock_guard lightInfosLock(lightInfosMutex_); + lightInfos_ = (LightInfo *)malloc(sizeof(LightInfo) * count); + CHKPR(lightInfos_, ERROR); + for (size_t i = 0; i < count; ++i) { + *(lightInfos_ + i) = lightInfoList_[i]; + } + lightInfoCount_ = static_cast(count); + return SUCCESS; +} +} // namespace Sensors +} // namespace OHOS diff --git a/frameworks/native/miscdevice/src/miscdevice_service_proxy.cpp b/frameworks/native/miscdevice/src/miscdevice_service_proxy.cpp index ffc5cad0016d3bf8bb489b8ee3d67ad4cb502a3c..b843902d487600546ff4d1025083bdccfe020141 100644 --- a/frameworks/native/miscdevice/src/miscdevice_service_proxy.cpp +++ b/frameworks/native/miscdevice/src/miscdevice_service_proxy.cpp @@ -16,6 +16,7 @@ #include "miscdevice_service_proxy.h" #include "hisysevent.h" +#include "securec.h" #include "sensors_errors.h" namespace OHOS { @@ -24,6 +25,7 @@ using namespace OHOS::HiviewDFX; namespace { constexpr HiLogLabel LABEL = { LOG_CORE, MISC_LOG_DOMAIN, "MiscdeviceServiceProxy" }; +constexpr int32_t MAX_LIGHT_COUNT = 0XFF; } MiscdeviceServiceProxy::MiscdeviceServiceProxy(const sptr &impl) : IRemoteProxy(impl) @@ -149,5 +151,104 @@ int32_t MiscdeviceServiceProxy::StopVibratorEffect(int32_t vibratorId, const std } return ret; } +std::vector MiscdeviceServiceProxy::GetLightList() +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + std::vector lightInfos; + if (!data.WriteInterfaceToken(MiscdeviceServiceProxy::GetDescriptor())) { + MISC_HILOGE("WriteInterfaceToken failed"); + return lightInfos; + } + sptr remote = Remote(); + if (remote == nullptr) { + MISC_HILOGE("remote is null"); + return lightInfos; + } + int32_t ret = remote->SendRequest(GET_LIGHT_LIST, data, reply, option); + if (ret != NO_ERROR) { + MISC_HILOGE("failed, ret:%{public}d", ret); + return lightInfos; + } + uint32_t lightCount = 0; + if (!reply.ReadUint32(lightCount)) { + MISC_HILOGE("Parcel read failed"); + return lightInfos; + } + if (lightCount > MAX_LIGHT_COUNT) { + lightCount = MAX_LIGHT_COUNT; + } + for (uint32_t i = 0; i < lightCount; ++i) { + const uint8_t *info = reply.ReadBuffer(sizeof(LightInfo)); + if (info == nullptr) { + MISC_HILOGE("ReadBuffer failed"); + return lightInfos; + } + LightInfo lightInfo; + if (memcpy_s(&lightInfo, sizeof(LightInfo), info, sizeof(LightInfo)) != EOK) { + MISC_HILOGE("memcpy_s failed"); + return lightInfos; + } + lightInfos.push_back(lightInfo); + } + return lightInfos; +} + +int32_t MiscdeviceServiceProxy::TurnOn(int32_t lightId, const LightColor &color, const LightAnimation &animation) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(MiscdeviceServiceProxy::GetDescriptor())) { + MISC_HILOGE("write descriptor failed"); + return WRITE_MSG_ERR; + } + if (!data.WriteInt32(lightId)) { + MISC_HILOGE("WriteUint32 lightId failed"); + return WRITE_MSG_ERR; + } + if (!data.WriteBuffer(&color, sizeof(LightColor))) { + MISC_HILOGE("WriteBuffer color failed"); + return WRITE_MSG_ERR; + } + if (!data.WriteBuffer(&animation, sizeof(LightAnimation))) { + MISC_HILOGE("WriteBuffer animation failed"); + return WRITE_MSG_ERR; + } + sptr remote = Remote(); + CHKPR(remote, ERROR); + int32_t ret = remote->SendRequest(TURN_ON, data, reply, option); + if (ret != NO_ERROR) { + HiSysEvent::Write(HiSysEvent::Domain::MISCDEVICE, "MISC_SERVICE_IPC_EXCEPTION", + HiSysEvent::EventType::FAULT, "PKG_NAME", "TurnOn", "ERROR_CODE", ret); + MISC_HILOGE("sendRequest failed, ret:%{public}d", ret); + } + return ret; +} + +int32_t MiscdeviceServiceProxy::TurnOff(int32_t lightId) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(MiscdeviceServiceProxy::GetDescriptor())) { + MISC_HILOGE("write descriptor failed"); + return WRITE_MSG_ERR; + } + if (!data.WriteInt32(lightId)) { + MISC_HILOGE("WriteInt32 lightId failed"); + return WRITE_MSG_ERR; + } + sptr remote = Remote(); + CHKPR(remote, ERROR); + int32_t ret = remote->SendRequest(TURN_OFF, data, reply, option); + if (ret != NO_ERROR) { + HiSysEvent::Write(HiSysEvent::Domain::MISCDEVICE, "MISC_SERVICE_IPC_EXCEPTION", + HiSysEvent::EventType::FAULT, "PKG_NAME", "TurnOff", "ERROR_CODE", ret); + MISC_HILOGE("sendRequest failed, ret:%{public}d", ret); + } + return ret; +} } // namespace Sensors } // namespace OHOS diff --git a/interfaces/native/light/BUILD.gn b/interfaces/native/light/BUILD.gn new file mode 100755 index 0000000000000000000000000000000000000000..985f8dba7c990b345876a6f8baaa757209434bf5 --- /dev/null +++ b/interfaces/native/light/BUILD.gn @@ -0,0 +1,68 @@ +# Copyright (c) 2022 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/miscdevice" +ohos_ndk_library("liblight_ndk") { + output_name = "light_agent" + ndk_description_file = "./liblight.json" + min_compact_version = "7" +} + +ohos_ndk_headers("light_ndk_header") { + dest_dir = "$ndk_headers_out_dir/sensors" + sources = [ + "./include/light_agent.h", + "./include/light_agent_type.h", + ] +} + +config("light_config") { + include_dirs = [ "include" ] +} + +ohos_shared_library("light_interface_native") { + output_name = "light_agent" + sources = [ "src/light_agent.cpp" ] + + include_dirs = [ + "include", + "//base/sensors/miscdevice/frameworks/native/miscdevice/include", + "//base/sensors/miscdevice/utils/include", + "//commonlibrary/c_utils/base/include", + "//foundation/communication/ipc/interfaces/innerkits/ipc_core/include", + ] + + cflags = [ "-Wno-error=inconsistent-missing-override" ] + + deps = [ + "$SUBSYSTEM_DIR/frameworks/native/miscdevice:liblight_native", + "$SUBSYSTEM_DIR/interfaces/native/light:liblight_ndk", + "$SUBSYSTEM_DIR/interfaces/native/light:light_ndk_header", + ] + + external_deps = [ + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + ] + + public_configs = [ ":light_config" ] + part_name = "miscdevice" + subsystem_name = "sensors" +} + +group("light_target") { + deps = [ ":light_interface_native" ] +} diff --git a/interfaces/native/light/include/light_agent.h b/interfaces/native/light/include/light_agent.h new file mode 100755 index 0000000000000000000000000000000000000000..778c7c376ea7b29faa29519c9cdb1dc0f5afb25a --- /dev/null +++ b/interfaces/native/light/include/light_agent.h @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2022 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. + */ + +/** + * @file light_agent.h + * + * @brief Declares the functions for starting or stopping a light. + * @since 10 + */ +#ifndef LIGHT_AGENT_H +#define LIGHT_AGENT_H +#include "light_agent_type.h" +#ifdef __cplusplus +extern "C" { +#endif + +namespace OHOS { +namespace Sensors { +/** + * @brief Obtains information about all the lights in the system. + * + * @param lightInfo Indicates the list of the light information. + * @param count Indicates the count of all the lights in the system. + * @return Returns 0 if the operation is successful. + * @return Returns a negative value if the operation fails. + * @since 10 + */ +int32_t GetLightList(LightInfo **lightInfo, int32_t &count); + +/** + * @brief Turns on available lights in the list based on the specified light id. + * + * @param lightId Indicates the light id. + * @param color:Color corresponding to the light. + * @param animation Indicates the blinking parameters. + * @return Returns 0 if the operation is successful. + * @return Returns a negative value if the operation fails. + * @since 10 + */ +int32_t TurnOn(int32_t lightId, const LightColor &color, const LightAnimation &animation); + +/** + * @brief Turns off available lights in the list based on the specified light id. + * + * @param lightId Indicates the light id. + * @return Returns 0 if the operation is successful. + * @return Returns a negative value if the operation fails. + * @since 10 + */ +int32_t TurnOff(int32_t lightId); +} // namespace Sensors +} // namespace OHOS +#ifdef __cplusplus +}; +#endif +/** @} */ +#endif // endif LIGHT_AGENT_H diff --git a/interfaces/native/light/include/light_agent_type.h b/interfaces/native/light/include/light_agent_type.h new file mode 100755 index 0000000000000000000000000000000000000000..f1b0fe574435034666725b1208f5380bdcf2263c --- /dev/null +++ b/interfaces/native/light/include/light_agent_type.h @@ -0,0 +1,116 @@ +/* + * Copyright (c) 2022 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_AGENT_TYPE_H +#define LIGHT_AGENT_TYPE_H +#include +#ifdef __cplusplus +extern "C" { +#endif + +/** Maximum length of the light name */ +#ifndef NAME_MAX_LEN +#define NAME_MAX_LEN 32 +#endif /* NAME_MAX_LEN */ +/** + * @brief Enumerates the light types. + * + * @since 10 + */ +enum LightType { + LIGHT_TYPE_SINGLE_COLOR = 1, + LIGHT_TYPE_RGB_COLOR = 2, + LIGHT_TYPE_WRGB_COLOR = 3, +}; + +/** + * @brief Defines the basic light information. + * + * @since 10 + */ +typedef struct { + char lightName[NAME_MAX_LEN]; /**< light name */ + int32_t lightId; /**< Light id */ + int32_t lightNumber; /**< Number of physical lights in the logic controller */ + int32_t lightType; /** Light type. For details, see {@link LightType}. */ +} LightInfo; + +/** + * @brief Enumerates the lighting modes. + * + * @since 10 + */ +typedef enum { + LIGHT_MODE_DEFAULT = 0, /**< No flicker mode. */ + LIGHT_MODE_BLINK = 1, /**< Timed flashing mode. */ + LIGHT_MODE_GRADIENT = 2, /**< Gradient mode. */ + LIGHT_MODE_BUTT = 3, /**< Invalid mode. */ +} FlashMode; + +/** + * @brief Defines the blinking parameters. + * + * @since 10 + */ +struct LightAnimation { + int32_t mode = LIGHT_MODE_DEFAULT; /**< Blinking mode. For details, see {@link FlashMode}. */ + int32_t onTime; /**< Duration (in ms) for which the light is on in a blinking period. */ + int32_t offTime; /**< Duration (in ms) for which the light is off in a blinking period. */ +}; + +/** + * @brief Defines the color and custom extended information of the light. + * + * The parameters include the These parameters include red, green, blue values and custom extended information. + * + * @since 10 + */ +struct RGBColor { + uint8_t r = 0; /** Red value range 0-255. */ + uint8_t g = 0; /** Green value range 0-255. */ + uint8_t b = 0; /** Blue value range 0-255. */ + uint8_t reserved = 0; /** Custom extended information */ +}; + +/** + * @brief Defines the color of the light. + * + * The parameters include the These parameters include red, green, blue and white values. + ght idNumber of physical lights in the logic controllerint reservedCustom extended information. + * @since 10 + */ +struct WRGBColor { + uint8_t w = 0; /** White value range 0-255. */ + uint8_t r = 0; /** Red value range 0-255. */ + uint8_t g = 0; /** Green value range 0-255. */ + uint8_t b = 0; /** Blue value range 0-255. */ +}; + +/** + * @brief Defines the color of the light。 + * + * @since 10 + */ +union LightColor { + int32_t singleColor; /** Single color mode. */ + RGBColor rgbColor; /** RGB mode, see {@link RGBColor}. */ + WRGBColor wrgbColor; /** WRGB mode, see {@link WRGBColor}. */ +}; + +/** @} */ +#ifdef __cplusplus +}; +#endif +#endif // endif LIGHT_AGENT_TYPE_H diff --git a/interfaces/native/light/liblight.json b/interfaces/native/light/liblight.json new file mode 100755 index 0000000000000000000000000000000000000000..969401ae51277b7e1f2ec9500fb80523d3f8986b --- /dev/null +++ b/interfaces/native/light/liblight.json @@ -0,0 +1,15 @@ +[ + { + "first_introduced": "7", + "name": "GetLightList" + }, + { + "name": "TurnOn" + }, + { + "name": "TurnOnMulti" + }, + { + "name": "TurnOff" + } + ] \ No newline at end of file diff --git a/interfaces/native/light/src/light_agent.cpp b/interfaces/native/light/src/light_agent.cpp new file mode 100755 index 0000000000000000000000000000000000000000..ca88a66626701a04f5eec3733c08e5f889bd4700 --- /dev/null +++ b/interfaces/native/light/src/light_agent.cpp @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2022 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_agent.h" + +#include "light_client.h" +#include "sensors_errors.h" + +namespace OHOS { +namespace Sensors { +using OHOS::HiviewDFX::HiLog; +using OHOS::HiviewDFX::HiLogLabel; + +namespace { +constexpr HiLogLabel LABEL = { LOG_CORE, MISC_LOG_DOMAIN, "LightNDK" }; +} // namespace + +int32_t GetLightList(LightInfo **lightInfo, int32_t &count) +{ + CHKPR(lightInfo, ERROR); + auto &client = LightClient::GetInstance(); + int32_t ret = client.GetLightList(lightInfo, count); + if (ret != ERR_OK) { + MISC_HILOGE("GetLightList failed"); + return ERROR; + } + return SUCCESS; +} + +int32_t TurnOn(int32_t lightId, const LightColor &color, const LightAnimation &animation) +{ + if (lightId < 0) { + MISC_HILOGE("lightId is invalid"); + return ERROR; + } + auto &client = LightClient::GetInstance(); + int32_t ret = client.TurnOn(lightId, color, animation); + if (ret != ERR_OK) { + MISC_HILOGE("TurnOn failed, lightId:%{public}d,ret:%{public}d ", lightId, ret); + return ERROR; + } + return SUCCESS; +} + +int32_t TurnOff(int32_t lightId) +{ + if (lightId < 0) { + MISC_HILOGE("lightId is error"); + return ERROR; + } + auto &client = LightClient::GetInstance(); + int32_t ret = client.TurnOff(lightId); + if (ret != ERR_OK) { + MISC_HILOGE("TurnOff failed, lightId:%{public}d,ret:%{public}d", lightId, ret); + return ERROR; + } + return SUCCESS; +} +} // namespace Sensors +} // namespace OHOS diff --git a/interfaces/native/vibrator/BUILD.gn b/interfaces/native/vibrator/BUILD.gn index 48349c12bd93bd6cd1647403ef7cfc796c73d88d..4a58c2e06b7b269123bfddab8244719c550ebc02 100644 --- a/interfaces/native/vibrator/BUILD.gn +++ b/interfaces/native/vibrator/BUILD.gn @@ -41,6 +41,7 @@ ohos_shared_library("vibrator_interface_native") { "//commonlibrary/c_utils/base/include", "//base/sensors/miscdevice/utils/include", "//base/sensors/miscdevice/frameworks/native/miscdevice/include", + "//base/sensors/miscdevice/interfaces/native/light/include", "//foundation/communication/ipc/interfaces/innerkits/ipc_core/include", ] @@ -48,6 +49,7 @@ ohos_shared_library("vibrator_interface_native") { deps = [ "$SUBSYSTEM_DIR/frameworks/native/miscdevice:libvibrator_native", + "$SUBSYSTEM_DIR/interfaces/native/light:light_ndk_header", "$SUBSYSTEM_DIR/interfaces/native/vibrator:libvibrator_ndk", ] diff --git a/services/miscdevice_service/BUILD.gn b/services/miscdevice_service/BUILD.gn index 465ab491d707385318cf2ae770616b254f2027bb..d4a3496bfc67b00c9daec058da8a36995a388d1a 100644 --- a/services/miscdevice_service/BUILD.gn +++ b/services/miscdevice_service/BUILD.gn @@ -31,6 +31,7 @@ ohos_shared_library("libmiscdevice_service") { "//commonlibrary/c_utils/base/include", "//utils/system/safwk/native/include", "$SUBSYSTEM_DIR/miscdevice/frameworks/native/miscdevice/include", + "$SUBSYSTEM_DIR/miscdevice/interfaces/native/light/include", "$SUBSYSTEM_DIR/miscdevice/utils/include", "hdi_connection/adapter/include", "hdi_connection/interface/include", diff --git a/services/miscdevice_service/include/miscdevice_service.h b/services/miscdevice_service/include/miscdevice_service.h index b76d9bac992b69c3e222a6f00392dbb14eecd055..f035e96221ff5c6717cc1e8d93a86ed12c20d20b 100644 --- a/services/miscdevice_service/include/miscdevice_service.h +++ b/services/miscdevice_service/include/miscdevice_service.h @@ -56,6 +56,9 @@ public: virtual int32_t PlayVibratorEffect(int32_t vibratorId, const std::string &effect, int32_t loopCount, int32_t usage) override; virtual int32_t StopVibratorEffect(int32_t vibratorId, const std::string &effect) override; + virtual std::vector GetLightList() override; + virtual int32_t TurnOn(int32_t lightId, const LightColor &color, const LightAnimation &animation) override; + virtual int32_t TurnOff(int32_t lightId) override; private: DISALLOW_COPY_AND_MOVE(MiscdeviceService); diff --git a/services/miscdevice_service/src/miscdevice_service.cpp b/services/miscdevice_service/src/miscdevice_service.cpp index 396527f066134d2e016db66da095c77338a9825e..26bed44eb3b06a817508948494333cce20883aff 100644 --- a/services/miscdevice_service/src/miscdevice_service.cpp +++ b/services/miscdevice_service/src/miscdevice_service.cpp @@ -210,6 +210,21 @@ int32_t MiscdeviceService::StopVibratorEffect(int32_t vibratorId, const std::str return NO_ERROR; } +std::vector MiscdeviceService::GetLightList() +{ + return {}; +} + +int32_t MiscdeviceService::TurnOn(int32_t lightId, const LightColor &color, const LightAnimation &animation) +{ + return NO_ERROR; +} + +int32_t MiscdeviceService::TurnOff(int32_t lightId) +{ + return NO_ERROR; +} + std::string MiscdeviceService::GetPackageName(AccessTokenID tokenId) { std::string packageName;