diff --git a/bundle.json b/bundle.json index cad1f51727c9061e516753b365a3793a92aa74ee..b8db11934c12d7c539aedb123641b1245a7c71ab 100644 --- a/bundle.json +++ b/bundle.json @@ -38,6 +38,7 @@ "fwk_group": [ "//base/sensors/miscdevice/interfaces/plugin:vibrator_js_target", "//base/sensors/miscdevice/interfaces/native/vibrator:vibrator_target", + "//base/sensors/miscdevice/interfaces/native/light:light_target", "//base/sensors/miscdevice/frameworks/native/miscdevice:miscdevice_native_target", "//base/sensors/miscdevice/utils:miscdevice_utils_target" ], diff --git a/interfaces/native/light/include/light_agent.h b/interfaces/native/light/include/light_agent.h index 778c7c376ea7b29faa29519c9cdb1dc0f5afb25a..01e87e1aebd73c3b3359bf3450c8f54c30b8dcc2 100755 --- a/interfaces/native/light/include/light_agent.h +++ b/interfaces/native/light/include/light_agent.h @@ -19,9 +19,12 @@ * @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 diff --git a/interfaces/native/light/include/light_agent_type.h b/interfaces/native/light/include/light_agent_type.h index f1b0fe574435034666725b1208f5380bdcf2263c..4b366bd408f0f8191fdcb86b2e2e42508024eb89 100755 --- a/interfaces/native/light/include/light_agent_type.h +++ b/interfaces/native/light/include/light_agent_type.h @@ -15,7 +15,9 @@ #ifndef LIGHT_AGENT_TYPE_H #define LIGHT_AGENT_TYPE_H + #include + #ifdef __cplusplus extern "C" { #endif @@ -104,6 +106,7 @@ struct WRGBColor { * @since 10 */ union LightColor { + LightColor() {}; int32_t singleColor; /** Single color mode. */ RGBColor rgbColor; /** RGB mode, see {@link RGBColor}. */ WRGBColor wrgbColor; /** WRGB mode, see {@link WRGBColor}. */ diff --git a/services/miscdevice_service/BUILD.gn b/services/miscdevice_service/BUILD.gn index d4a3496bfc67b00c9daec058da8a36995a388d1a..3fdb7567106f3113f4b6ef4757d1db937254de4f 100644 --- a/services/miscdevice_service/BUILD.gn +++ b/services/miscdevice_service/BUILD.gn @@ -17,7 +17,10 @@ SUBSYSTEM_DIR = "//base/sensors" ohos_shared_library("libmiscdevice_service") { sources = [ "hdi_connection/adapter/src/compatible_connection.cpp", + "hdi_connection/adapter/src/compatible_light_connection.cpp", "hdi_connection/adapter/src/hdi_connection.cpp", + "hdi_connection/adapter/src/hdi_light_connection.cpp", + "hdi_connection/interface/src/light_hdi_connection.cpp", "hdi_connection/interface/src/vibrator_hdi_connection.cpp", "src/miscdevice_dump.cpp", "src/miscdevice_service.cpp", @@ -44,6 +47,7 @@ ohos_shared_library("libmiscdevice_service") { external_deps = [ "access_token:libaccesstoken_sdk", "c_utils:utils", + "drivers_interface_light:liblight_proxy_1.0", "drivers_interface_vibrator:libvibrator_proxy_1.1", "hisysevent_native:libhisysevent", "hitrace_native:hitrace_meter", diff --git a/services/miscdevice_service/hdi_connection/adapter/include/compatible_light_connection.h b/services/miscdevice_service/hdi_connection/adapter/include/compatible_light_connection.h new file mode 100755 index 0000000000000000000000000000000000000000..52287938ee1707290a44184e7654619616794124 --- /dev/null +++ b/services/miscdevice_service/hdi_connection/adapter/include/compatible_light_connection.h @@ -0,0 +1,43 @@ +/* + * 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 COMPATIBLE_LIGHT_CONNECTION_H +#define COMPATIBLE_LIGHT_CONNECTION_H + +#include +#include +#include + +#include "i_light_hdi_connection.h" +namespace OHOS { +namespace Sensors { +class CompatibleLightConnection : public ILightHdiConnection { +public: + CompatibleLightConnection() = default; + virtual ~CompatibleLightConnection() = default; + int32_t ConnectHdi() override; + int32_t GetLightList(std::vector& lightList) const override; + int32_t TurnOn(int32_t lightId, const LightColor &color, const LightAnimation &animation) override; + int32_t TurnOff(int32_t lightId) override; + int32_t DestroyHdiConnection() override; + +private: + std::vector turnOnLights_; + static std::atomic_bool isStop_; + DISALLOW_COPY_AND_MOVE(CompatibleLightConnection); +}; +} // namespace Sensors +} // namespace OHOS +#endif // COMPATIBLE_LIGHT_CONNECTION_H \ No newline at end of file diff --git a/services/miscdevice_service/hdi_connection/adapter/include/hdi_light_connection.h b/services/miscdevice_service/hdi_connection/adapter/include/hdi_light_connection.h new file mode 100755 index 0000000000000000000000000000000000000000..de53ccea9554b80d6aaceb0de459eb45a89c8ff6 --- /dev/null +++ b/services/miscdevice_service/hdi_connection/adapter/include/hdi_light_connection.h @@ -0,0 +1,46 @@ +/* + * 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 HDI_LIGHT_CONNECTION_H +#define HDI_LIGHT_CONNECTION_H + +#include "death_recipient_template.h" +#include "i_light_hdi_connection.h" +#include "v1_0/light_interface_proxy.h" +namespace OHOS { +namespace Sensors { +using OHOS::HDI::Light::V1_0::ILightInterface; +class HdiLightConnection : public ILightHdiConnection { +public: + HdiLightConnection() = default; + virtual ~HdiLightConnection() {}; + int32_t ConnectHdi() override; + int32_t GetLightList(std::vector& lightList) const override; + int32_t TurnOn(int32_t lightId, const LightColor &color, const LightAnimation &animation) override; + int32_t TurnOff(int32_t lightId) override; + int32_t DestroyHdiConnection() override; + void ProcessDeathObserver(const wptr &object); + +private: + DISALLOW_COPY_AND_MOVE(HdiLightConnection); + sptr hdiDeathObserver_; + sptr lightInterface_ = nullptr; + void RegisterHdiDeathRecipient(); + void UnregisterHdiDeathRecipient(); + void Reconnect(); +}; +} // namespace Sensors +} // namespace OHOS +#endif // HDI_LIGHT_CONNECTION_H \ No newline at end of file diff --git a/services/miscdevice_service/hdi_connection/adapter/src/compatible_light_connection.cpp b/services/miscdevice_service/hdi_connection/adapter/src/compatible_light_connection.cpp new file mode 100755 index 0000000000000000000000000000000000000000..871e5dd3c50fd19ee27545b3382ad8b580c009fb --- /dev/null +++ b/services/miscdevice_service/hdi_connection/adapter/src/compatible_light_connection.cpp @@ -0,0 +1,83 @@ +/* + * 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 "compatible_light_connection.h" + +#include +#include +#include + +#include "sensors_errors.h" + +namespace OHOS { +namespace Sensors { +namespace { +constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MISC_LOG_DOMAIN, "CompatibleLightConnection" }; +} +std::vector lightInfo = { + {"light_test", 1, 3, 1} +}; +std::vector supportLights = { 1 }; + +int32_t CompatibleLightConnection::ConnectHdi() +{ + CALL_LOG_ENTER; + return ERR_OK; +} + +int32_t CompatibleLightConnection::GetLightList(std::vector& lightList) const +{ + CALL_LOG_ENTER; + lightList.assign(lightInfo.begin(), lightInfo.end()); + return ERR_OK; +} + +int32_t CompatibleLightConnection::TurnOn(int32_t lightId, const LightColor &color, const LightAnimation &animation) +{ + CALL_LOG_ENTER; + if (std::find(supportLights.begin(), supportLights.end(), lightId) == supportLights.end()) { + MISC_HILOGE("not support TurnOn lightId:%{public}d", lightId); + return LIGHT_ID_NOT_SUPPORT; + } + if (std::find(turnOnLights_.begin(), turnOnLights_.end(), lightId) != turnOnLights_.end()) { + MISC_HILOGI("lightId:%{public}d has been turnOn", lightId); + return ERR_OK; + } + turnOnLights_.push_back(lightId); + return ERR_OK; +} + +int32_t CompatibleLightConnection::TurnOff(int32_t lightId) +{ + CALL_LOG_ENTER; + if (std::find(supportLights.begin(), supportLights.end(), lightId) == supportLights.end()) { + MISC_HILOGE("not support TurnOff lightId:%{public}d", lightId); + return LIGHT_ID_NOT_SUPPORT; + } + if (std::find(turnOnLights_.begin(), turnOnLights_.end(), lightId) == turnOnLights_.end()) { + MISC_HILOGE("lightId:%{public}d should not be turn off", lightId); + return LIGHT_END_ERROR; + } + std::vector::iterator iter = std::find(turnOnLights_.begin(), turnOnLights_.end(), lightId); + turnOnLights_.erase(iter); + return ERR_OK; +} + +int32_t CompatibleLightConnection::DestroyHdiConnection() +{ + CALL_LOG_ENTER; + return ERR_OK; +} +} // namespace Sensors +} // namespace OHOS diff --git a/services/miscdevice_service/hdi_connection/adapter/src/hdi_light_connection.cpp b/services/miscdevice_service/hdi_connection/adapter/src/hdi_light_connection.cpp new file mode 100755 index 0000000000000000000000000000000000000000..9a4340185ae417ae8ff6fa0790c0a2cf46020581 --- /dev/null +++ b/services/miscdevice_service/hdi_connection/adapter/src/hdi_light_connection.cpp @@ -0,0 +1,164 @@ +/* + * 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 "hdi_light_connection.h" + +#include +#include +#include +#include + +#include "hisysevent.h" +#include "sensors_errors.h" +#include "v1_0/light_interface_proxy.h" + +namespace OHOS { +namespace Sensors { +namespace { +constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MISC_LOG_DOMAIN, "HdiLightConnection" }; +constexpr int32_t GET_HDI_SERVICE_COUNT = 10; +constexpr uint32_t WAIT_MS = 100; +} + +int32_t HdiLightConnection::ConnectHdi() +{ + CALL_LOG_ENTER; + int32_t retry = 0; + while (retry < GET_HDI_SERVICE_COUNT) { + lightInterface_ = ILightInterface::Get(); + if (lightInterface_ != nullptr) { + RegisterHdiDeathRecipient(); + return ERR_OK; + } + retry++; + MISC_HILOGW("connect hdi service failed, retry:%{public}d", retry); + std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_MS)); + } + return ERR_INVALID_VALUE; +} + +int32_t HdiLightConnection::GetLightList(std::vector &lightList) const +{ + CALL_LOG_ENTER; + std::vector lightInfos; + CHKPR(lightInterface_, ERROR); + int32_t ret = lightInterface_->GetLightInfo(lightInfos); + if (ret != 0) { + MISC_HILOGE("get light info failed"); + return ret; + } + for (size_t i = 0; i < lightInfos.size(); ++i) { + LightInfo light; + light.lightId = lightInfos[i].lightId; + light.lightNumber = lightInfos[i].lightNumber; + auto ret = memcpy_s(light.lightName, NAME_MAX_LEN, lightInfos[i].lightName.c_str(), + lightInfos[i].lightName.length()); + if (ret != EOK) { + MISC_HILOGE("memcpy_s failed, error number: %{public}d", errno); + return ret; + } + light.lightType = lightInfos[i].lightType; + lightList.push_back(light); + } + return ERR_OK; +} + +int32_t HdiLightConnection::TurnOn(int32_t lightId, const LightColor &color, const LightAnimation &animation) +{ + CALL_LOG_ENTER; + HDI::Light::V1_0::HdfLightColor lightColor = { + .colorValue = color.singleColor + }; + HDI::Light::V1_0::HdfLightFlashEffect flashEffect = { + .flashMode = animation.mode, + .onTime = animation.onTime, + .offTime = animation.offTime + }; + HDI::Light::V1_0::HdfLightEffect effect = { + .lightColor = lightColor, + .flashEffect = flashEffect + }; + CHKPR(lightInterface_, ERROR); + int32_t ret = lightInterface_->TurnOnLight(lightId, effect); + if (ret < 0) { + MISC_HILOGE("TurnOn failed"); + return ret; + } + return ERR_OK; +} + +int32_t HdiLightConnection::TurnOff(int32_t lightId) +{ + CALL_LOG_ENTER; + CHKPR(lightInterface_, ERROR); + int32_t ret = lightInterface_->TurnOffLight(lightId); + if (ret < 0) { + MISC_HILOGE("TurnOff failed"); + return ret; + } + return ERR_OK; +} + +int32_t HdiLightConnection::DestroyHdiConnection() +{ + UnregisterHdiDeathRecipient(); + return ERR_OK; +} + +void HdiLightConnection::RegisterHdiDeathRecipient() +{ + CALL_LOG_ENTER; + if (lightInterface_ == nullptr) { + MISC_HILOGE("connect v1_0 hdi failed"); + return; + } + hdiDeathObserver_ = new (std::nothrow) DeathRecipientTemplate(*const_cast(this)); + if (hdiDeathObserver_ == nullptr) { + MISC_HILOGE("hdiDeathObserver_ is nullptr"); + return; + } + OHOS::HDI::hdi_objcast(lightInterface_)->AddDeathRecipient(hdiDeathObserver_); +} + +void HdiLightConnection::UnregisterHdiDeathRecipient() +{ + CALL_LOG_ENTER; + if (lightInterface_ == nullptr || hdiDeathObserver_ == nullptr) { + MISC_HILOGE("lightInterface_ or hdiDeathObserver_ is nullptr"); + return; + } + OHOS::HDI::hdi_objcast(lightInterface_)->RemoveDeathRecipient(hdiDeathObserver_); +} + +void HdiLightConnection::ProcessDeathObserver(const wptr &object) +{ + CALL_LOG_ENTER; + sptr hdiService = object.promote(); + if (hdiService == nullptr || hdiDeathObserver_ == nullptr) { + MISC_HILOGE("invalid remote object or hdiDeathObserver_ is null"); + return; + } + hdiService->RemoveDeathRecipient(hdiDeathObserver_); + Reconnect(); +} + +void HdiLightConnection::Reconnect() +{ + if (ConnectHdi() != ERR_OK) { + MISC_HILOGE("connect hdi failed"); + } +} +} // namespace Sensors +} // namespace OHOS diff --git a/services/miscdevice_service/hdi_connection/interface/include/i_light_hdi_connection.h b/services/miscdevice_service/hdi_connection/interface/include/i_light_hdi_connection.h new file mode 100755 index 0000000000000000000000000000000000000000..4dd299f86a391793edc810c40bf5f43a1dd3826e --- /dev/null +++ b/services/miscdevice_service/hdi_connection/interface/include/i_light_hdi_connection.h @@ -0,0 +1,42 @@ +/* + * 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 I_LIGHT_HDI_CONNECTION_H +#define I_LIGHT_HDI_CONNECTION_H + +#include +#include +#include + +#include "light_agent_type.h" + +namespace OHOS { +namespace Sensors { +class ILightHdiConnection { +public: + ILightHdiConnection() = default; + virtual ~ILightHdiConnection() = default; + virtual int32_t ConnectHdi() = 0; + virtual int32_t GetLightList(std::vector& lightList) const = 0; + virtual int32_t TurnOn(int32_t lightId, const LightColor &color, const LightAnimation &animation) = 0; + virtual int32_t TurnOff(int32_t lightId) = 0; + virtual int32_t DestroyHdiConnection() = 0; + +private: + DISALLOW_COPY_AND_MOVE(ILightHdiConnection); +}; +} // namespace Sensors +} // namespace OHOS +#endif // I_LIGHT_HDI_CONNECTION_H diff --git a/services/miscdevice_service/hdi_connection/interface/include/light_hdi_connection.h b/services/miscdevice_service/hdi_connection/interface/include/light_hdi_connection.h new file mode 100755 index 0000000000000000000000000000000000000000..e98223f6b208c79420cc8b5f5fc609ce85215d62 --- /dev/null +++ b/services/miscdevice_service/hdi_connection/interface/include/light_hdi_connection.h @@ -0,0 +1,42 @@ +/* + * 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_HDI_CONNECTION_H +#define LIGHT_HDI_CONNECTION_H + +#include + +#include "i_light_hdi_connection.h" +#include "singleton.h" + +namespace OHOS { +namespace Sensors { +class LightHdiConnection : public ILightHdiConnection, public Singleton { +public: + LightHdiConnection() = default; + virtual ~LightHdiConnection() {} + int32_t ConnectHdi() override; + int32_t GetLightList(std::vector &lightList) const override; + int32_t TurnOn(int32_t lightId, const LightColor &color, const LightAnimation &animation) override; + int32_t TurnOff(int32_t lightId) override; + int32_t DestroyHdiConnection() override; +private: + DISALLOW_COPY_AND_MOVE(LightHdiConnection); + std::unique_ptr iLightHdiConnection_; + std::vector lightInfoList_; +}; +} // namespace Sensors +} // namespace OHOS +#endif // LIGHT_HDI_CONNECTION_H diff --git a/services/miscdevice_service/hdi_connection/interface/src/light_hdi_connection.cpp b/services/miscdevice_service/hdi_connection/interface/src/light_hdi_connection.cpp new file mode 100755 index 0000000000000000000000000000000000000000..86a7e1994af9588336c8bcbc4aa464bd3214fea8 --- /dev/null +++ b/services/miscdevice_service/hdi_connection/interface/src/light_hdi_connection.cpp @@ -0,0 +1,93 @@ +/* + * 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_hdi_connection.h" + +#include + +#include "compatible_light_connection.h" +#include "hdi_light_connection.h" +#include "hitrace_meter.h" +#include "sensors_errors.h" + +namespace OHOS { +namespace Sensors { +using namespace OHOS::HiviewDFX; + +namespace { +constexpr HiLogLabel LABEL = { LOG_CORE, MISC_LOG_DOMAIN, "LightHdiConnection" }; +} + +int32_t LightHdiConnection::ConnectHdi() +{ + iLightHdiConnection_ = std::make_unique(); + int32_t ret = iLightHdiConnection_->ConnectHdi(); + if (ret != 0) { + MISC_HILOGE("hdi direct failed"); + iLightHdiConnection_ = std::make_unique(); + CHKPR(iLightHdiConnection_, ERROR); + ret = iLightHdiConnection_->ConnectHdi(); + } + if (ret != 0) { + MISC_HILOGE("hdi connection failed"); + return LIGHT_HDF_CONNECT_ERR; + } + ret = iLightHdiConnection_->GetLightList(lightInfoList_); + if (ret != 0) { + MISC_HILOGE("GetLightList failed"); + return LIGHT_ERR; + } + return ERR_OK; +} + +int32_t LightHdiConnection::GetLightList(std::vector& lightList) const +{ + lightList.assign(lightInfoList_.begin(), lightInfoList_.end()); + return ERR_OK; +} + +int32_t LightHdiConnection::TurnOn(int32_t lightId, const LightColor &color, const LightAnimation &animation) +{ + CHKPR(iLightHdiConnection_, ERROR); + int32_t ret = iLightHdiConnection_->TurnOn(lightId, color, animation); + if (ret != 0) { + MISC_HILOGE("TurnOn failed"); + return LIGHT_ID_NOT_SUPPORT; + } + return ERR_OK; +} + +int32_t LightHdiConnection::TurnOff(int32_t lightId) +{ + CHKPR(iLightHdiConnection_, ERROR); + int32_t ret = iLightHdiConnection_->TurnOff(lightId); + if (ret != 0) { + MISC_HILOGE("TurnOff failed"); + return LIGHT_ERR; + } + return ERR_OK; +} + +int32_t LightHdiConnection::DestroyHdiConnection() +{ + CHKPR(iLightHdiConnection_, ERROR); + int32_t ret = iLightHdiConnection_->DestroyHdiConnection(); + if (ret != 0) { + MISC_HILOGE("DestroyHdiConnection failed"); + return LIGHT_HDF_CONNECT_ERR; + } + return ERR_OK; +} +} // namespace Sensors +} // namespace OHOS diff --git a/services/miscdevice_service/include/miscdevice_service.h b/services/miscdevice_service/include/miscdevice_service.h index f035e96221ff5c6717cc1e8d93a86ed12c20d20b..62183b792e6e610fefeae9d03854fec05b4fca28 100644 --- a/services/miscdevice_service/include/miscdevice_service.h +++ b/services/miscdevice_service/include/miscdevice_service.h @@ -27,6 +27,7 @@ #include "system_ability.h" #include "thread_ex.h" +#include "light_hdi_connection.h" #include "miscdevice_common.h" #include "miscdevice_dump.h" #include "miscdevice_service_stub.h" @@ -50,6 +51,7 @@ public: void OnDump() override; void OnStart() override; void OnStop() override; + bool IsValid(int32_t lightId); int32_t Dump(int32_t fd, const std::vector &args) override; virtual int32_t Vibrate(int32_t vibratorId, int32_t timeOut, int32_t usage) override; virtual int32_t CancelVibrator(int32_t vibratorId) override; @@ -63,12 +65,16 @@ public: private: DISALLOW_COPY_AND_MOVE(MiscdeviceService); bool InitInterface(); + bool InitLightInterface(); std::string GetPackageName(AccessTokenID tokenId); void StartVibrateThread(VibrateInfo info); bool ShouldIgnoreVibrate(const VibrateInfo &info); + bool InitLightList(); VibratorHdiConnection &vibratorHdiConnection_ = VibratorHdiConnection::GetInstance(); + LightHdiConnection &lightHdiConnection_ = LightHdiConnection::GetInstance(); bool lightExist_; bool vibratorExist_; + std::vector lightInfos_; std::map miscDeviceIdMap_; MiscdeviceServiceState state_; std::shared_ptr vibratorThread_; diff --git a/services/miscdevice_service/include/miscdevice_service_stub.h b/services/miscdevice_service/include/miscdevice_service_stub.h index 22dd5dfb17a4c7a6bf46d7a2949779d8109d642b..f76885036dfc24305a535464b1030e24c1ffd688 100755 --- a/services/miscdevice_service/include/miscdevice_service_stub.h +++ b/services/miscdevice_service/include/miscdevice_service_stub.h @@ -51,12 +51,9 @@ private: 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); - + int32_t GetLightListPb(MessageParcel &data, MessageParcel &reply); + int32_t TurnOnPb(MessageParcel &data, MessageParcel &reply); + int32_t TurnOffPb(MessageParcel &data, MessageParcel &reply); bool CheckVibratePermission(); std::map baseFuncs_; diff --git a/services/miscdevice_service/src/miscdevice_service.cpp b/services/miscdevice_service/src/miscdevice_service.cpp index 26bed44eb3b06a817508948494333cce20883aff..fbba8a459db749ad27d7ceddf58ff65f084045c8 100644 --- a/services/miscdevice_service/src/miscdevice_service.cpp +++ b/services/miscdevice_service/src/miscdevice_service.cpp @@ -20,6 +20,7 @@ #include "sensors_errors.h" #include "system_ability_definition.h" #include "vibration_priority_manager.h" +#include "v1_0/light_interface_proxy.h" namespace OHOS { namespace Sensors { @@ -66,6 +67,10 @@ void MiscdeviceService::OnStart() MISC_HILOGE("Init interface error"); return; } + if (!InitLightInterface()) { + MISC_HILOGE("InitLightInterface failed"); + return; + } if (!SystemAbility::Publish(this)) { MISC_HILOGE("publish MiscdeviceService failed"); return; @@ -93,6 +98,27 @@ bool MiscdeviceService::InitInterface() return true; } +bool MiscdeviceService::InitLightInterface() +{ + auto ret = lightHdiConnection_.ConnectHdi(); + if (ret != ERR_OK) { + MISC_HILOGE("ConnectHdi failed"); + return false; + } + return true; +} + +bool MiscdeviceService::IsValid(int32_t lightId) +{ + CALL_LOG_ENTER; + for (const auto &item : lightInfos_) { + if (lightId == item.lightId) { + return true; + } + } + return false; +} + void MiscdeviceService::OnStop() { CALL_LOG_ENTER; @@ -210,21 +236,6 @@ 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; @@ -257,6 +268,55 @@ std::string MiscdeviceService::GetPackageName(AccessTokenID tokenId) return packageName; } +std::vector MiscdeviceService::GetLightList() +{ + if (!InitLightList()) { + MISC_HILOGE("InitLightList init failed"); + return lightInfos_; + } + return lightInfos_; +} + +bool MiscdeviceService::InitLightList() +{ + int32_t ret = lightHdiConnection_.GetLightList(lightInfos_); + if (ret != ERR_OK) { + MISC_HILOGE("InitLightList failed, ret:%{public}d", ret); + return false; + } + return true; +} + +int32_t MiscdeviceService::TurnOn(int32_t lightId, const LightColor &color, const LightAnimation &animation) +{ + CALL_LOG_ENTER; + if (!IsValid(lightId)) { + MISC_HILOGE("lightId is invalid, lightId:%{pubilc}d", lightId); + return MISCDEVICE_NATIVE_SAM_ERR; + } + int32_t ret = lightHdiConnection_.TurnOn(lightId, color, animation); + if (ret != ERR_OK) { + MISC_HILOGE("TurnOn failed, error:%{public}d", ret); + return ERROR; + } + return ret; +} + +int32_t MiscdeviceService::TurnOff(int32_t lightId) +{ + CALL_LOG_ENTER; + if (!IsValid(lightId)) { + MISC_HILOGE("lightId is invalid, lightId:%{pubilc}d", lightId); + return MISCDEVICE_NATIVE_SAM_ERR; + } + int32_t ret = lightHdiConnection_.TurnOff(lightId); + if (ret != ERR_OK) { + MISC_HILOGE("TurnOff failed, error:%{public}d", ret); + return ERROR; + } + return ret; +} + int32_t MiscdeviceService::Dump(int32_t fd, const std::vector &args) { CALL_LOG_ENTER; diff --git a/services/miscdevice_service/src/miscdevice_service_stub.cpp b/services/miscdevice_service/src/miscdevice_service_stub.cpp index 1657aa60ddf973ff8d875204f60aa624b23ac4c7..b958b259b06ed7c35fdd071c0f81640e2fbb69b3 100644 --- a/services/miscdevice_service/src/miscdevice_service_stub.cpp +++ b/services/miscdevice_service/src/miscdevice_service_stub.cpp @@ -21,6 +21,7 @@ #include "hisysevent.h" #include "ipc_skeleton.h" #include "message_parcel.h" +#include "securec.h" #include "permission_util.h" #include "sensors_errors.h" @@ -41,6 +42,9 @@ MiscdeviceServiceStub::MiscdeviceServiceStub() baseFuncs_[CANCEL_VIBRATOR] = &MiscdeviceServiceStub::CancelVibratorPb; baseFuncs_[PLAY_VIBRATOR_EFFECT] = &MiscdeviceServiceStub::PlayVibratorEffectPb; baseFuncs_[STOP_VIBRATOR_EFFECT] = &MiscdeviceServiceStub::StopVibratorEffectPb; + baseFuncs_[GET_LIGHT_LIST] = &MiscdeviceServiceStub::GetLightListPb; + baseFuncs_[TURN_ON] = &MiscdeviceServiceStub::TurnOnPb; + baseFuncs_[TURN_OFF] = &MiscdeviceServiceStub::TurnOffPb; } MiscdeviceServiceStub::~MiscdeviceServiceStub() @@ -128,6 +132,52 @@ int32_t MiscdeviceServiceStub::StopVibratorEffectPb(MessageParcel &data, Message return StopVibratorEffect(vibratorId, effectType); } +int32_t MiscdeviceServiceStub::GetLightListPb(MessageParcel &data, MessageParcel &reply) +{ + (void)data; + std::vector lightInfos(GetLightList()); + size_t lightCount = lightInfos.size(); + MISC_HILOGE("lightCount:%{public}d", lightCount); + if (!reply.WriteUint32(lightCount)) { + MISC_HILOGE("Parcel write failed"); + return WRITE_MSG_ERR; + } + for (int32_t i = 0; i < lightCount; ++i) { + if (!reply.WriteBuffer(&lightInfos[i], sizeof(LightInfo))) { + MISC_HILOGE("WriteBuffer failed"); + return WRITE_MSG_ERR; + } + } + return NO_ERROR; +} + +int32_t MiscdeviceServiceStub::TurnOnPb(MessageParcel &data, MessageParcel &reply) +{ + int32_t lightId = data.ReadInt32(); + const unsigned char *info = data.ReadBuffer(sizeof(LightColor)); + CHKPR(info, ERROR); + LightColor lightColor; + if (memcpy_s(&lightColor, sizeof(LightColor), info, sizeof(LightColor)) != EOK) { + MISC_HILOGE("memcpy_s lightColor failed"); + return ERROR; + } + + const unsigned char *buf = data.ReadBuffer(sizeof(LightAnimation)); + CHKPR(buf, ERROR); + LightAnimation lightAnimation; + if (memcpy_s(&lightAnimation, sizeof(LightAnimation), buf, sizeof(LightAnimation)) != EOK) { + MISC_HILOGE("memcpy_s lightAnimation failed"); + return ERROR; + } + return TurnOn(lightId, lightColor, lightAnimation); +} + +int32_t MiscdeviceServiceStub::TurnOffPb(MessageParcel &data, MessageParcel &reply) +{ + int32_t lightId = data.ReadInt32(); + return TurnOff(lightId); +} + int32_t MiscdeviceServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {