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_interface_impl.h"
17 #include <hdf_base.h>
18 #include "light_uhdf_log.h"
19 #include "devhost_dump_reg.h"
20 #include "light_dump.h"
21 #include "light_if.h"
22 
23 #define HDF_LOG_TAG     uhdf_light_service
24 
25 namespace OHOS {
26 namespace HDI {
27 namespace Light {
28 namespace V1_0 {
29 
Init()30 int32_t LightInterfaceImpl::Init()
31 {
32     const struct LightInterface *lightInterface = NewLightInterfaceInstance();
33     if (lightInterface == nullptr || lightInterface->GetLightInfo == nullptr) {
34         HDF_LOGE("%{public}s: get light Module instance failed", __func__);
35         return HDF_FAILURE;
36     }
37 
38     DevHostRegisterDumpHost(GetLightDump);
39 
40     return HDF_SUCCESS;
41 }
42 
GetLightInfo(std::vector<HdfLightInfoVdi> & info)43 int32_t LightInterfaceImpl::GetLightInfo(std::vector<HdfLightInfoVdi>& info)
44 {
45     const struct LightInterface *lightInterface = NewLightInterfaceInstance();
46     if (lightInterface == nullptr || lightInterface->GetLightInfo == nullptr) {
47         HDF_LOGE("%{public}s: get light Module instance failed", __func__);
48         return HDF_FAILURE;
49     }
50 
51     struct LightInfo *lightInfo = nullptr;
52     uint32_t count = 0;
53     int32_t ret = lightInterface->GetLightInfo(&lightInfo, &count);
54     if (ret != HDF_SUCCESS) {
55         HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
56         return ret;
57     }
58 
59     while (count--) {
60         HdfLightInfoVdi hdfLightInfo;
61         hdfLightInfo.lightId = static_cast<int32_t>(lightInfo->lightId);
62         hdfLightInfo.lightType = lightInfo->lightType;
63         if (strcpy_s(hdfLightInfo.lightName, LIGHT_NAME_MAX_LEN, lightInfo->lightName) != EOK) {
64             HDF_LOGE("%{public}s: Light name cpy faild", __func__);
65             return HDF_FAILURE;
66         }
67         hdfLightInfo.lightNumber = static_cast<int32_t>(lightInfo->lightNumber);
68         info.push_back(hdfLightInfo);
69         lightInfo++;
70     }
71 
72     return HDF_SUCCESS;
73 }
74 
TurnOnLight(int32_t lightId,const HdfLightEffectVdi & effect)75 int32_t LightInterfaceImpl::TurnOnLight(int32_t lightId, const HdfLightEffectVdi& effect)
76 {
77     HDF_LOGI("%{public}s: Enter the TurnOnLight function, lightId is %{public}d", __func__, lightId);
78     const struct LightInterface *lightInterface = NewLightInterfaceInstance();
79     if (lightInterface == nullptr || lightInterface->TurnOnLight == nullptr) {
80         HDF_LOGE("%{public}s: get light Module instance failed", __func__);
81         return HDF_FAILURE;
82     }
83 
84     LightEffect lightEffect;
85     lightEffect.lightColor.colorValue.rgbColor.b = effect.lightColor.colorValue.rgbColor.b;
86     lightEffect.lightColor.colorValue.rgbColor.g = effect.lightColor.colorValue.rgbColor.g;
87     lightEffect.lightColor.colorValue.rgbColor.r = effect.lightColor.colorValue.rgbColor.r;
88     lightEffect.lightColor.colorValue.wrgbColor.b = effect.lightColor.colorValue.wrgbColor.b;
89     lightEffect.lightColor.colorValue.wrgbColor.g = effect.lightColor.colorValue.wrgbColor.g;
90     lightEffect.lightColor.colorValue.wrgbColor.r = effect.lightColor.colorValue.wrgbColor.r;
91     lightEffect.lightColor.colorValue.wrgbColor.w = effect.lightColor.colorValue.wrgbColor.w;
92     lightEffect.flashEffect.flashMode = effect.flashEffect.flashMode;
93     lightEffect.flashEffect.onTime = effect.flashEffect.onTime;
94     lightEffect.flashEffect.offTime = effect.flashEffect.offTime;
95     int32_t ret = lightInterface->TurnOnLight(lightId, &lightEffect);
96     if (ret != HDF_SUCCESS) {
97         HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
98     }
99 
100     return ret;
101 }
102 
TurnOnMultiLights(int32_t lightId,const std::vector<HdfLightColorVdi> & colors)103 int32_t LightInterfaceImpl::TurnOnMultiLights(int32_t lightId, const std::vector<HdfLightColorVdi>& colors)
104 {
105     HDF_LOGI("%{public}s: Enter the TurnOnMultiLights function, lightId is %{public}d", __func__, lightId);
106     const struct LightInterface *lightInterface = NewLightInterfaceInstance();
107     if (lightInterface == nullptr || lightInterface->TurnOnMultiLights == nullptr) {
108         HDF_LOGE("%{public}s: get light module instance failed", __func__);
109         return HDF_FAILURE;
110     }
111 
112     uint32_t num = colors.size();
113     LightColor lightColor[num];
114     int32_t i = 0;
115     for (auto iter : colors) {
116         lightColor[i].colorValue.rgbColor.b = iter.colorValue.rgbColor.b;
117         lightColor[i].colorValue.rgbColor.g = iter.colorValue.rgbColor.g;
118         lightColor[i].colorValue.rgbColor.r = iter.colorValue.rgbColor.r;
119         lightColor[i].colorValue.wrgbColor.b = iter.colorValue.wrgbColor.b;
120         lightColor[i].colorValue.wrgbColor.g = iter.colorValue.wrgbColor.g;
121         lightColor[i].colorValue.wrgbColor.r = iter.colorValue.wrgbColor.r;
122         lightColor[i++].colorValue.wrgbColor.w = iter.colorValue.wrgbColor.w;
123     }
124 
125     int32_t ret = lightInterface->TurnOnMultiLights(lightId, lightColor, num);
126     if (ret != HDF_SUCCESS) {
127         HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
128     }
129 
130     return ret;
131 }
132 
TurnOffLight(int32_t lightId)133 int32_t LightInterfaceImpl::TurnOffLight(int32_t lightId)
134 {
135     HDF_LOGI("%{public}s: Enter the TurnOffLight function, lightId is %{public}d", __func__, lightId);
136     const struct LightInterface *lightInterface = NewLightInterfaceInstance();
137     if (lightInterface == nullptr || lightInterface->TurnOffLight == nullptr) {
138         HDF_LOGE("%{public}s: get light Module instance failed", __func__);
139         return HDF_FAILURE;
140     }
141     int32_t ret = lightInterface->TurnOffLight(lightId);
142     if (ret != HDF_SUCCESS) {
143         HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
144     }
145 
146     return ret;
147 }
148 
CreateLightVdiInstance(struct HdfVdiBase * vdiBase)149 static int32_t CreateLightVdiInstance(struct HdfVdiBase *vdiBase)
150 {
151     HDF_LOGI("%{public}s: Enter the CreateLightVdiInstance function", __func__);
152     if (vdiBase == nullptr) {
153         HDF_LOGE("%{public}s parameter vdiBase is NULL", __func__);
154         return HDF_FAILURE;
155     }
156 
157     struct VdiWrapperLight *lightVdi = reinterpret_cast<VdiWrapperLight *>(vdiBase);
158     lightVdi->lightModule = new LightInterfaceImpl();
159     if (lightVdi->lightModule == nullptr) {
160         HDF_LOGI("%{public}s: new lightModule failed!", __func__);
161         return HDF_FAILURE;
162     }
163     return HDF_SUCCESS;
164 }
165 
DestoryLightVdiInstance(struct HdfVdiBase * vdiBase)166 static int32_t DestoryLightVdiInstance(struct HdfVdiBase *vdiBase)
167 {
168     HDF_LOGI("%{public}s: Enter the DestoryLightVdiInstance function", __func__);
169     if (vdiBase == nullptr) {
170         HDF_LOGE("%{public}s parameter vdiBase is NULL", __func__);
171         return HDF_FAILURE;
172     }
173 
174     struct VdiWrapperLight *lightVdi = reinterpret_cast<VdiWrapperLight *>(vdiBase);
175     LightInterfaceImpl *lightImpl = reinterpret_cast<LightInterfaceImpl *>(lightVdi->lightModule);
176     if (lightImpl != nullptr) {
177         delete lightImpl;
178         lightVdi->lightModule = nullptr;
179     }
180     return HDF_SUCCESS;
181 }
182 
183 static struct VdiWrapperLight g_lightVdi = {
184     .base = {
185         .moduleVersion = 1,
186         .moduleName = "light_service",
187         .CreateVdiInstance = CreateLightVdiInstance,
188         .DestoryVdiInstance = DestoryLightVdiInstance,
189     },
190     .lightModule = nullptr,
191 };
192 
193 extern "C" HDF_VDI_INIT(g_lightVdi);
194 } // V1_0
195 } // Light
196 } // HDI
197 } // OHOS
198