1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "hdi_light_connection.h"
17
18 #include <thread>
19
20 #include "sensors_errors.h"
21
22 #undef LOG_TAG
23 #define LOG_TAG "HdiLightConnection"
24
25 namespace OHOS {
26 namespace Sensors {
27 namespace {
28 constexpr int32_t GET_HDI_SERVICE_COUNT = 10;
29 constexpr uint32_t WAIT_MS = 100;
30 } // namespace
31
ConnectHdi()32 int32_t HdiLightConnection::ConnectHdi()
33 {
34 CALL_LOG_ENTER;
35 int32_t retry = 0;
36 while (retry < GET_HDI_SERVICE_COUNT) {
37 lightInterface_ = ILightInterface::Get();
38 if (lightInterface_ != nullptr) {
39 RegisterHdiDeathRecipient();
40 return ERR_OK;
41 }
42 retry++;
43 MISC_HILOGW("Connect hdi service failed, retry:%{public}d", retry);
44 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_MS));
45 }
46 return ERR_INVALID_VALUE;
47 }
48
GetLightList(std::vector<LightInfoIPC> & lightList)49 int32_t HdiLightConnection::GetLightList(std::vector<LightInfoIPC> &lightList)
50 {
51 CALL_LOG_ENTER;
52 std::vector<HDI::Light::V1_0::HdfLightInfo> lightInfos;
53 CHKPR(lightInterface_, ERROR);
54 int32_t ret = lightInterface_->GetLightInfo(lightInfos);
55 if (ret != 0) {
56 MISC_HILOGE("Get light info failed");
57 return ret;
58 }
59 for (size_t i = 0; i < lightInfos.size(); ++i) {
60 LightInfoIPC light;
61 light.SetLightName(lightInfos[i].lightName);
62 light.SetLightId(lightInfos[i].lightId);
63 light.SetLightNumber(lightInfos[i].lightNumber);
64 light.SetLightType(lightInfos[i].lightType);
65 lightList.push_back(light);
66 }
67 return ERR_OK;
68 }
69
TurnOn(int32_t lightId,const LightColor & color,const LightAnimationIPC & animation)70 int32_t HdiLightConnection::TurnOn(int32_t lightId, const LightColor &color, const LightAnimationIPC &animation)
71 {
72 CALL_LOG_ENTER;
73 HDI::Light::V1_0::HdfLightColor lightColor;
74 lightColor.colorValue.singleColor = color.singleColor;
75
76 HDI::Light::V1_0::HdfLightFlashEffect flashEffect;
77 flashEffect.flashMode = animation.GetMode();
78 flashEffect.onTime = animation.GetOnTime();
79 flashEffect.offTime = animation.GetOffTime();
80
81 HDI::Light::V1_0::HdfLightEffect effect;
82 effect.lightColor = lightColor;
83 effect.flashEffect = flashEffect;
84 CHKPR(lightInterface_, ERROR);
85 int32_t ret = lightInterface_->TurnOnLight(lightId, effect);
86 if (ret < 0) {
87 MISC_HILOGE("TurnOn failed");
88 return ret;
89 }
90 return ERR_OK;
91 }
92
TurnOff(int32_t lightId)93 int32_t HdiLightConnection::TurnOff(int32_t lightId)
94 {
95 CALL_LOG_ENTER;
96 CHKPR(lightInterface_, ERROR);
97 int32_t ret = lightInterface_->TurnOffLight(lightId);
98 if (ret < 0) {
99 MISC_HILOGE("TurnOff failed");
100 return ret;
101 }
102 return ERR_OK;
103 }
104
DestroyHdiConnection()105 int32_t HdiLightConnection::DestroyHdiConnection()
106 {
107 UnregisterHdiDeathRecipient();
108 return ERR_OK;
109 }
110
RegisterHdiDeathRecipient()111 void HdiLightConnection::RegisterHdiDeathRecipient()
112 {
113 CALL_LOG_ENTER;
114 if (lightInterface_ == nullptr) {
115 MISC_HILOGE("Connect v1_0 hdi failed");
116 return;
117 }
118 if (hdiDeathObserver_ == nullptr) {
119 hdiDeathObserver_ = new (std::nothrow) DeathRecipientTemplate(*const_cast<HdiLightConnection *>(this));
120 CHKPV(hdiDeathObserver_);
121 }
122 OHOS::HDI::hdi_objcast<ILightInterface>(lightInterface_)->AddDeathRecipient(hdiDeathObserver_);
123 }
124
UnregisterHdiDeathRecipient()125 void HdiLightConnection::UnregisterHdiDeathRecipient()
126 {
127 CALL_LOG_ENTER;
128 if (lightInterface_ == nullptr || hdiDeathObserver_ == nullptr) {
129 MISC_HILOGE("lightInterface_ or hdiDeathObserver_ is nullptr");
130 return;
131 }
132 OHOS::HDI::hdi_objcast<ILightInterface>(lightInterface_)->RemoveDeathRecipient(hdiDeathObserver_);
133 }
134
ProcessDeathObserver(const wptr<IRemoteObject> & object)135 void HdiLightConnection::ProcessDeathObserver(const wptr<IRemoteObject> &object)
136 {
137 CALL_LOG_ENTER;
138 sptr<IRemoteObject> hdiService = object.promote();
139 if (hdiService == nullptr || hdiDeathObserver_ == nullptr) {
140 MISC_HILOGE("Invalid remote object or hdiDeathObserver_ is null");
141 return;
142 }
143 hdiService->RemoveDeathRecipient(hdiDeathObserver_);
144 Reconnect();
145 }
146
Reconnect()147 void HdiLightConnection::Reconnect()
148 {
149 if (ConnectHdi() != ERR_OK) {
150 MISC_HILOGE("Connect hdi failed");
151 }
152 }
153 } // namespace Sensors
154 } // namespace OHOS
155