• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <hdf_base.h>
17 #include <hdf_device_desc.h>
18 #include <hdf_log.h>
19 #include <hdf_sbuf_ipc.h>
20 #include <osal_mem.h>
21 #include "light_interface_impl.h"
22 
23 #define HDF_LOG_TAG           hdf_light_if_driver
24 
25 using namespace OHOS::HDI::Light::V1_0;
26 
27 struct HdfLightInterfaceHost {
28     struct IDeviceIoService ioservice;
29     LightInterfaceImpl *service;
30 };
31 
LightInterfaceDriverDispatch(struct HdfDeviceIoClient * client,int cmdId,struct HdfSBuf * data,struct HdfSBuf * reply)32 static int32_t LightInterfaceDriverDispatch(struct HdfDeviceIoClient *client, int cmdId, struct HdfSBuf *data,
33     struct HdfSBuf *reply)
34 {
35     struct HdfLightInterfaceHost *hdfLightInterfaceHost = CONTAINER_OF(
36         client->device->service, struct HdfLightInterfaceHost, ioservice);
37 
38     OHOS::MessageParcel *dataParcel = nullptr;
39     OHOS::MessageParcel *replyParcel = nullptr;
40     OHOS::MessageOption option;
41 
42     (void)SbufToParcel(reply, &replyParcel);
43     if (SbufToParcel(data, &dataParcel) != HDF_SUCCESS) {
44         HDF_LOGE("%{public}s:invalid data sbuf object to dispatch", __func__);
45         return HDF_ERR_INVALID_PARAM;
46     }
47 
48     return hdfLightInterfaceHost->service->OnRemoteRequest(cmdId, *dataParcel, *replyParcel, option);
49 }
50 
HdfLightInterfaceDriverInit(struct HdfDeviceObject * deviceObject)51 static int HdfLightInterfaceDriverInit(struct HdfDeviceObject *deviceObject)
52 {
53     HDF_LOGI("HdfLightInterfaceDriverInit enter");
54     return HDF_SUCCESS;
55 }
56 
HdfLightInterfaceDriverBind(struct HdfDeviceObject * deviceObject)57 static int HdfLightInterfaceDriverBind(struct HdfDeviceObject *deviceObject)
58 {
59     struct HdfLightInterfaceHost *hdfLightInterfaceHost = (struct HdfLightInterfaceHost *)OsalMemAlloc(
60         sizeof(struct HdfLightInterfaceHost));
61     if (hdfLightInterfaceHost == nullptr) {
62         HDF_LOGE("HdfLightInterfaceDriverBind OsalMemAlloc HdfLightInterfaceHost failed!");
63         return HDF_FAILURE;
64     }
65 
66     hdfLightInterfaceHost->ioservice.Dispatch = LightInterfaceDriverDispatch;
67     hdfLightInterfaceHost->ioservice.Open = nullptr;
68     hdfLightInterfaceHost->ioservice.Release = nullptr;
69     hdfLightInterfaceHost->service = new LightInterfaceImpl();
70 
71     deviceObject->service = &hdfLightInterfaceHost->ioservice;
72     HDF_LOGI("HdfLightInterfaceDriverBind success");
73     return HDF_SUCCESS;
74 }
75 
HdfLightInterfaceDriverRelease(struct HdfDeviceObject * deviceObject)76 static void HdfLightInterfaceDriverRelease(struct HdfDeviceObject *deviceObject)
77 {
78     HDF_LOGI("HdfLightInterfaceDriverRelease enter");
79 
80     struct HdfLightInterfaceHost *hdfLightInterfaceHost = CONTAINER_OF(
81         deviceObject->service, struct HdfLightInterfaceHost, ioservice);
82     delete hdfLightInterfaceHost->service;
83     OsalMemFree(hdfLightInterfaceHost);
84 }
85 
86 struct HdfDriverEntry g_lightinterfaceDriverEntry = {
87     .moduleVersion = 1,
88     .moduleName = "light_service",
89     .Bind = HdfLightInterfaceDriverBind,
90     .Init = HdfLightInterfaceDriverInit,
91     .Release = HdfLightInterfaceDriverRelease,
92 };
93 
94 #ifndef __cplusplus
95 extern "C" {
96 #endif
97 HDF_INIT(g_lightinterfaceDriverEntry);
98 #ifndef __cplusplus
99 }
100 #endif
101