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 "light_controller.h"
17 #include <fcntl.h>
18 #include <stdio.h>
19 #include "hdf_base.h"
20 #include "hdf_dlist.h"
21 #include "hdf_io_service_if.h"
22 #include "hdf_log.h"
23 #include "light_if.h"
24 #include "light_type.h"
25 #include "osal_mem.h"
26
27 #define HDF_LOG_TAG hdf_light_dal
28 #define LIGHT_SERVICE_NAME "hdf_light"
29
GetLightDevicePriv(void)30 static struct LightDevice *GetLightDevicePriv(void)
31 {
32 static struct LightDevice lightDeviceData = {
33 .initState = false,
34 .lightNum = 0,
35 .ioService = NULL,
36 .lightInfoEntry = NULL,
37 };
38
39 return &lightDeviceData;
40 }
41
SendLightMsg(uint32_t cmd,struct HdfSBuf * msg,struct HdfSBuf * reply)42 static int32_t SendLightMsg(uint32_t cmd, struct HdfSBuf *msg, struct HdfSBuf *reply)
43 {
44 struct LightDevice *priv = GetLightDevicePriv();
45
46 if (priv->ioService == NULL || priv->ioService->dispatcher == NULL ||
47 priv->ioService->dispatcher->Dispatch == NULL) {
48 HDF_LOGE("%s: para invalid", __func__);
49 return HDF_FAILURE;
50 }
51
52 int32_t ret = priv->ioService->dispatcher->Dispatch(&priv->ioService->object, cmd, msg, reply);
53 if (ret != HDF_SUCCESS) {
54 HDF_LOGE("%{public}s: Light dispatch failed", __func__);
55 return ret;
56 }
57
58 return HDF_SUCCESS;
59 }
60
ReadLightInfo(struct HdfSBuf * reply,struct LightDevice * priv)61 static int32_t ReadLightInfo(struct HdfSBuf *reply, struct LightDevice *priv)
62 {
63 uint32_t len;
64 struct LightInfo *pos = NULL;
65 struct LightInfo *buf = NULL;
66
67 if (!HdfSbufReadUint32(reply, &priv->lightNum)) {
68 HDF_LOGE("%s: sbuf read lightNum failed", __func__);
69 return HDF_FAILURE;
70 }
71
72 if (priv->lightInfoEntry != NULL) {
73 OsalMemFree(priv->lightInfoEntry);
74 priv->lightInfoEntry = NULL;
75 }
76
77 priv->lightInfoEntry = (struct LightInfo *)OsalMemCalloc(sizeof(*priv->lightInfoEntry) * priv->lightNum);
78 if (priv->lightInfoEntry == NULL) {
79 HDF_LOGE("%s: malloc fail", __func__);
80 return HDF_FAILURE;
81 }
82
83 pos = priv->lightInfoEntry;
84
85 for (uint32_t i = 0; i < priv->lightNum; ++i) {
86 if (!HdfSbufReadBuffer(reply, (const void **)&buf, &len)) {
87 return HDF_FAILURE;
88 }
89
90 pos->lightId = buf->lightId;
91 pos->reserved = buf->reserved;
92 pos++;
93 }
94
95 return HDF_SUCCESS;
96 }
97
GetLightInfo(struct LightInfo ** lightInfo,uint32_t * count)98 static int32_t GetLightInfo(struct LightInfo **lightInfo, uint32_t *count)
99 {
100 if ((lightInfo == NULL) || (count == NULL)) {
101 HDF_LOGE("%s:line:%{public}d pointer is null and return ret", __func__, __LINE__);
102 return HDF_FAILURE;
103 }
104
105 struct LightDevice *priv = GetLightDevicePriv();
106
107 if (priv->lightNum > 0) {
108 *count = priv->lightNum;
109 *lightInfo = priv->lightInfoEntry;
110 return HDF_SUCCESS;
111 }
112
113 (void)OsalMutexLock(&priv->mutex);
114 struct HdfSBuf *reply = HdfSbufObtainDefaultSize();
115 if (reply == NULL) {
116 HDF_LOGE("%s: get sbuf failed", __func__);
117 (void)OsalMutexUnlock(&priv->mutex);
118 return HDF_FAILURE;
119 }
120
121 int32_t ret = SendLightMsg(LIGHT_IO_CMD_GET_INFO_LIST, NULL, reply);
122 if (ret != HDF_SUCCESS) {
123 HDF_LOGE("%{public}s: Light send cmd failed, ret[%{public}d]", __func__, ret);
124 HdfSbufRecycle(reply);
125 (void)OsalMutexUnlock(&priv->mutex);
126 return ret;
127 }
128
129 if (ReadLightInfo(reply, priv) != HDF_SUCCESS) {
130 HdfSbufRecycle(reply);
131 (void)OsalMutexUnlock(&priv->mutex);
132 return HDF_FAILURE;
133 }
134
135 HdfSbufRecycle(reply);
136 (void)OsalMutexUnlock(&priv->mutex);
137
138 *count = priv->lightNum;
139 *lightInfo = priv->lightInfoEntry;
140
141 return HDF_SUCCESS;
142 }
143
ValidityJudgment(uint32_t lightId,struct LightEffect * effect)144 static int32_t ValidityJudgment(uint32_t lightId, struct LightEffect *effect)
145 {
146 if (lightId >= LIGHT_ID_BUTT) {
147 HDF_LOGE("%{public}s: id not supported", __func__);
148 return LIGHT_NOT_SUPPORT;
149 }
150
151 if (effect->flashEffect.flashMode > LIGHT_FLASH_TIMED) {
152 HDF_LOGE("%{public}s: flashMode not supported", __func__);
153 return LIGHT_NOT_FLASH;
154 }
155
156 if ((effect->flashEffect.flashMode == LIGHT_FLASH_TIMED) && (effect->flashEffect.onTime == 0 ||
157 effect->flashEffect.offTime == 0)) {
158 HDF_LOGE("%{public}s: flashMode not supported", __func__);
159 return LIGHT_NOT_FLASH;
160 }
161
162 return LIGHT_SUCCESS;
163 }
164
OnLight(uint32_t lightId,struct LightEffect * effect)165 static int32_t OnLight(uint32_t lightId, struct LightEffect *effect)
166 {
167 int32_t ret;
168
169 if (effect == NULL) {
170 HDF_LOGE("%{public}s: effect is NULL", __func__);
171 return HDF_FAILURE;
172 }
173
174 ret = ValidityJudgment(lightId, effect);
175 if (ret != HDF_SUCCESS) {
176 HDF_LOGE("%{public}s: effect is false", __func__);
177 return ret;
178 }
179
180 struct LightDevice *priv = GetLightDevicePriv();
181 (void)OsalMutexLock(&priv->mutex);
182
183 struct HdfSBuf *msg = HdfSbufObtainDefaultSize();
184 if (msg == NULL) {
185 HDF_LOGE("%{public}s: Failed to obtain sBuf size", __func__);
186 (void)OsalMutexUnlock(&priv->mutex);
187 return HDF_FAILURE;
188 }
189
190 if (!HdfSbufWriteInt32(msg, lightId)) {
191 HDF_LOGE("%{public}s: Light write id failed", __func__);
192 HdfSbufRecycle(msg);
193 (void)OsalMutexUnlock(&priv->mutex);
194 return HDF_FAILURE;
195 }
196
197 if (!HdfSbufWriteInt32(msg, LIGHT_OPS_IO_CMD_ENABLE)) {
198 HDF_LOGE("%{public}s: Light write enable failed", __func__);
199 HdfSbufRecycle(msg);
200 (void)OsalMutexUnlock(&priv->mutex);
201 return HDF_FAILURE;
202 }
203
204 if (!HdfSbufWriteBuffer(msg, effect, sizeof(*effect))) {
205 HDF_LOGE("%{public}s: Light write enable failed", __func__);
206 HdfSbufRecycle(msg);
207 (void)OsalMutexUnlock(&priv->mutex);
208 return HDF_FAILURE;
209 }
210
211 ret = SendLightMsg(LIGHT_IO_CMD_OPS, msg, NULL);
212 if (ret != HDF_SUCCESS) {
213 HDF_LOGE("%{public}s: Light enable failed, ret[%{public}d]", __func__, ret);
214 }
215 HdfSbufRecycle(msg);
216 (void)OsalMutexUnlock(&priv->mutex);
217
218 return ret;
219 }
220
OffLight(uint32_t lightId)221 static int32_t OffLight(uint32_t lightId)
222 {
223 if (lightId >= LIGHT_ID_BUTT) {
224 HDF_LOGE("%{public}s: id not supported", __func__);
225 return HDF_FAILURE;
226 }
227
228 struct LightDevice *priv = GetLightDevicePriv();
229 (void)OsalMutexLock(&priv->mutex);
230
231 struct HdfSBuf *msg = HdfSbufObtainDefaultSize();
232 if (msg == NULL) {
233 HDF_LOGE("%{public}s: Failed to obtain sBuf", __func__);
234 (void)OsalMutexUnlock(&priv->mutex);
235 return HDF_FAILURE;
236 }
237
238 if (!HdfSbufWriteInt32(msg, lightId)) {
239 HDF_LOGE("%{public}s: Light write id failed", __func__);
240 HdfSbufRecycle(msg);
241 (void)OsalMutexUnlock(&priv->mutex);
242 return HDF_FAILURE;
243 }
244
245 if (!HdfSbufWriteInt32(msg, LIGHT_OPS_IO_CMD_DISABLE)) {
246 HDF_LOGE("%{public}s: Light write disable failed", __func__);
247 HdfSbufRecycle(msg);
248 (void)OsalMutexUnlock(&priv->mutex);
249 return HDF_FAILURE;
250 }
251
252 int32_t ret = SendLightMsg(LIGHT_IO_CMD_OPS, msg, NULL);
253 if (ret != HDF_SUCCESS) {
254 HDF_LOGE("%{public}s: Light disable failed, ret[%{public}d]", __func__, ret);
255 }
256 HdfSbufRecycle(msg);
257 (void)OsalMutexUnlock(&priv->mutex);
258
259 return ret;
260 }
261
NewLightInterfaceInstance(void)262 const struct LightInterface *NewLightInterfaceInstance(void)
263 {
264 static struct LightInterface lightDevInstance;
265 struct LightDevice *priv = GetLightDevicePriv();
266
267 if (priv->initState) {
268 return &lightDevInstance;
269 }
270
271 OsalMutexInit(&priv->mutex);
272 lightDevInstance.GetLightInfo = GetLightInfo;
273 lightDevInstance.TurnOnLight = OnLight;
274 lightDevInstance.TurnOffLight = OffLight;
275
276 priv->ioService = HdfIoServiceBind(LIGHT_SERVICE_NAME);
277 if (priv->ioService == NULL) {
278 HDF_LOGE("%s: get light ioService failed", __func__);
279 OsalMutexDestroy(&priv->mutex);
280 return NULL;
281 }
282
283 priv->initState = true;
284 HDF_LOGI("get light devInstance success");
285
286 return &lightDevInstance;
287 }
288
FreeLightInterfaceInstance(void)289 int32_t FreeLightInterfaceInstance(void)
290 {
291 struct LightDevice *priv = GetLightDevicePriv();
292
293 if (!priv->initState) {
294 HDF_LOGI("%s: light instance had released", __func__);
295 return HDF_SUCCESS;
296 }
297
298 priv->lightNum = 0;
299
300 if (priv->ioService != NULL) {
301 HdfIoServiceRecycle(priv->ioService);
302 }
303
304 if (priv->lightInfoEntry != NULL) {
305 OsalMemFree(priv->lightInfoEntry);
306 priv->lightInfoEntry = NULL;
307 }
308
309 OsalMutexDestroy(&priv->mutex);
310
311 return HDF_SUCCESS;
312 }