1 /*
2  * Copyright (c) 2022-2023 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 "light_if_service.h"
17 #include <hdf_base.h>
18 #include "light_uhdf_log.h"
19 #include "hitrace_meter.h"
20 
21 #define HDF_LOG_TAG   "uhdf_light_service"
22 
23 namespace OHOS {
24 namespace HDI {
25 namespace Light {
26 namespace V1_0 {
LightIfService()27 LightIfService::LightIfService()
28 {
29     int32_t ret = GetLightVdiImpl();
30     if (ret != HDF_SUCCESS) {
31         HDF_LOGE("%{public}s: get light vdi instance failed", __func__);
32     }
33 }
34 
~LightIfService()35 LightIfService::~LightIfService()
36 {
37     if (vdi_ != nullptr) {
38         HdfCloseVdi(vdi_);
39     }
40 }
41 
GetLightVdiImpl()42 int32_t LightIfService::GetLightVdiImpl()
43 {
44     struct VdiWrapperLight *vdiWrapperLight = nullptr;
45     uint32_t version = 0;
46     vdi_ = HdfLoadVdi(HDI_LIGHT_VDI_LIBNAME);
47     if (vdi_ == nullptr || vdi_->vdiBase == nullptr) {
48         HDF_LOGE("%{public}s: load light vdi failed", __func__);
49         return HDF_FAILURE;
50     }
51 
52     version = HdfGetVdiVersion(vdi_);
53     if (version != 1) {
54         HDF_LOGE("%{public}s: get light vdi version failed", __func__);
55         return HDF_FAILURE;
56     }
57 
58     vdiWrapperLight = reinterpret_cast<struct VdiWrapperLight *>(vdi_->vdiBase);
59     lightVdiImpl_ = vdiWrapperLight->lightModule;
60     if (lightVdiImpl_ == nullptr) {
61         HDF_LOGE("%{public}s: get light impl failed", __func__);
62         return HDF_FAILURE;
63     }
64 
65     return HDF_SUCCESS;
66 }
67 
Init()68 int32_t LightIfService::Init()
69 {
70     if (lightVdiImpl_ == nullptr) {
71         HDF_LOGE("%{public}s: lightVdiImpl_ is nullptr", __func__);
72         return HDF_FAILURE;
73     }
74     int32_t ret = lightVdiImpl_->Init();
75     if (ret != HDF_SUCCESS) {
76         HDF_LOGE("%{public}s Init failed, error code is %{public}d", __func__, ret);
77     }
78 
79     return ret;
80 }
81 
GetLightInfo(std::vector<HdfLightInfo> & info)82 int32_t LightIfService::GetLightInfo(std::vector<HdfLightInfo>& info)
83 {
84     HDF_LOGD("%{public}s: Enter the GetLightInfo function.", __func__);
85     if (lightVdiImpl_ == nullptr) {
86         HDF_LOGE("%{public}s: lightVdiImpl_ is nullptr", __func__);
87         return HDF_FAILURE;
88     }
89 
90     std::vector<HdfLightInfoVdi> lightInfoVdi;
91     StartTrace(HITRACE_TAG_HDF, "GetLightInfo");
92     int32_t ret = lightVdiImpl_->GetLightInfo(lightInfoVdi);
93     if (ret != HDF_SUCCESS) {
94         HDF_LOGE("%{public}s: GetLightInfo failed, error code is %{public}d", __func__, ret);
95         return ret;
96     }
97     FinishTrace(HITRACE_TAG_HDF);
98 
99     if (lightInfoVdi.empty()) {
100         HDF_LOGE("%{public}s: no sensor info in list", __func__);
101         return HDF_FAILURE;
102     }
103 
104     for (const auto &iter : lightInfoVdi) {
105         HdfLightInfo hdfLightInfo;
106         hdfLightInfo.lightId = iter.lightId;
107         hdfLightInfo.lightType = iter.lightType;
108         hdfLightInfo.lightName = iter.lightName;
109         hdfLightInfo.lightNumber = iter.lightNumber;
110         info.push_back(std::move(hdfLightInfo));
111     }
112     return HDF_SUCCESS;
113 }
114 
TurnOnLight(int32_t lightId,const HdfLightEffect & effect)115 int32_t LightIfService::TurnOnLight(int32_t lightId, const HdfLightEffect& effect)
116 {
117     HDF_LOGD("%{public}s: Enter the TurnOnLight function, lightId is %{public}d", __func__, lightId);
118     if (lightVdiImpl_ == nullptr) {
119         HDF_LOGE("%{public}s: lightVdiImpl_ is nullptr", __func__);
120         return HDF_FAILURE;
121     }
122 
123     HdfLightEffectVdi lightEffectVdi;
124     lightEffectVdi.lightColor.colorValue.rgbColor.b = effect.lightColor.colorValue.rgbColor.b;
125     lightEffectVdi.lightColor.colorValue.rgbColor.g = effect.lightColor.colorValue.rgbColor.g;
126     lightEffectVdi.lightColor.colorValue.rgbColor.r = effect.lightColor.colorValue.rgbColor.r;
127     lightEffectVdi.lightColor.colorValue.wrgbColor.b = effect.lightColor.colorValue.wrgbColor.b;
128     lightEffectVdi.lightColor.colorValue.wrgbColor.g = effect.lightColor.colorValue.wrgbColor.g;
129     lightEffectVdi.lightColor.colorValue.wrgbColor.r = effect.lightColor.colorValue.wrgbColor.r;
130     lightEffectVdi.lightColor.colorValue.wrgbColor.w = effect.lightColor.colorValue.wrgbColor.w;
131     lightEffectVdi.flashEffect.flashMode = effect.flashEffect.flashMode;
132     lightEffectVdi.flashEffect.onTime = effect.flashEffect.onTime;
133     lightEffectVdi.flashEffect.offTime = effect.flashEffect.offTime;
134 
135     StartTrace(HITRACE_TAG_HDF, "TurnOnLight");
136     int32_t ret = lightVdiImpl_->TurnOnLight(lightId, lightEffectVdi);
137     if (ret != HDF_SUCCESS) {
138         HDF_LOGE("%{public}s TurnOnLight failed, error code is %{public}d", __func__, ret);
139     }
140     FinishTrace(HITRACE_TAG_HDF);
141 
142     return ret;
143 }
144 
TurnOnMultiLights(int32_t lightId,const std::vector<HdfLightColor> & colors)145 int32_t LightIfService::TurnOnMultiLights(int32_t lightId, const std::vector<HdfLightColor>& colors)
146 {
147     HDF_LOGD("%{public}s: Enter the TurnOnMultiLights function, lightId is %{public}d", __func__, lightId);
148     std::vector<HdfLightColorVdi> colorVdi;
149     if (lightVdiImpl_ == nullptr) {
150         HDF_LOGE("%{public}s: lightVdiImpl_ is nullptr", __func__);
151         return HDF_FAILURE;
152     }
153 
154     for (auto iter : colors) {
155         HdfLightColorVdi lightColorVdi;
156         lightColorVdi.colorValue.rgbColor.b = iter.colorValue.rgbColor.b;
157         lightColorVdi.colorValue.rgbColor.g = iter.colorValue.rgbColor.g;
158         lightColorVdi.colorValue.rgbColor.r = iter.colorValue.rgbColor.r;
159         lightColorVdi.colorValue.wrgbColor.b = iter.colorValue.wrgbColor.b;
160         lightColorVdi.colorValue.wrgbColor.g = iter.colorValue.wrgbColor.g;
161         lightColorVdi.colorValue.wrgbColor.r = iter.colorValue.wrgbColor.r;
162         lightColorVdi.colorValue.wrgbColor.w = iter.colorValue.wrgbColor.w;
163         colorVdi.push_back(std::move(lightColorVdi));
164     }
165 
166     StartTrace(HITRACE_TAG_HDF, "TurnOnMultiLights");
167     int32_t ret = lightVdiImpl_->TurnOnMultiLights(lightId, colorVdi);
168     if (ret != HDF_SUCCESS) {
169         HDF_LOGE("%{public}s TurnOnMultiLights failed, error code is %{public}d", __func__, ret);
170     }
171     FinishTrace(HITRACE_TAG_HDF);
172 
173     return ret;
174 }
175 
TurnOffLight(int32_t lightId)176 int32_t LightIfService::TurnOffLight(int32_t lightId)
177 {
178     HDF_LOGD("%{public}s: Enter the TurnOffLight function, lightId is %{public}d", __func__, lightId);
179     if (lightVdiImpl_ == nullptr) {
180         HDF_LOGE("%{public}s: lightVdiImpl_ is nullptr", __func__);
181         return HDF_FAILURE;
182     }
183     StartTrace(HITRACE_TAG_HDF, "TurnOffLight");
184     int32_t ret = lightVdiImpl_->TurnOffLight(lightId);
185     if (ret != HDF_SUCCESS) {
186         HDF_LOGE("%{public}s TurnOffLight failed, error code is %{public}d", __func__, ret);
187     }
188     FinishTrace(HITRACE_TAG_HDF);
189 
190     return ret;
191 }
192 
LightInterfaceImplGetInstance(void)193 extern "C" ILightInterface *LightInterfaceImplGetInstance(void)
194 {
195     LightIfService *impl = new (std::nothrow) LightIfService();
196     if (impl == nullptr) {
197         HDF_LOGE("%{public}s: impl nullptr", __func__);
198         return nullptr;
199     }
200 
201     int32_t ret = impl->Init();
202     if (ret != HDF_SUCCESS) {
203         HDF_LOGE("%{public}s: service init failed, error code is %{public}d", __func__, ret);
204         delete impl;
205         return nullptr;
206     }
207 
208     return impl;
209 }
210 } // V1_0
211 } // Light
212 } // HDI
213 } // OHOS
214