• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "sample_driver.h"
17 #include <linux/ioctl.h>
18 #include "hdf_log.h"
19 #include "hdf_base.h"
20 #include "hdf_device_desc.h"
21 
22 #define HDF_LOG_TAG sample_driver
23 
24 #ifndef INT32_MAX
25 #define INT32_MAX 0x7fffffff
26 #endif
27 
HdfSampleDriverRelease(struct HdfDeviceObject * deviceObject)28 void HdfSampleDriverRelease(struct HdfDeviceObject *deviceObject)
29 {
30     (void)deviceObject;
31     return;
32 }
33 
SampleDriverRegisteDevice(struct HdfSBuf * data)34 int32_t SampleDriverRegisteDevice(struct HdfSBuf *data)
35 {
36     if (data == NULL) {
37         return HDF_FAILURE;
38     }
39 
40     const char *moduleName = HdfSbufReadString(data);
41     if (moduleName == NULL) {
42         return HDF_FAILURE;
43     }
44     const char *serviceName = HdfSbufReadString(data);
45     if (serviceName == NULL) {
46         return HDF_FAILURE;
47     }
48 
49     struct HdfDeviceObject *devObj = HdfRegisteDevice(moduleName, serviceName);
50     if (devObj == NULL) {
51         return HDF_FAILURE;
52     }
53     return HDF_SUCCESS;
54 }
55 
SampleDriverUnregisteDevice(struct HdfSBuf * data)56 int32_t SampleDriverUnregisteDevice(struct HdfSBuf *data)
57 {
58     if (data == NULL) {
59         return HDF_FAILURE;
60     }
61 
62     const char *moduleName = HdfSbufReadString(data);
63     if (moduleName == NULL) {
64         return HDF_FAILURE;
65     }
66     const char *serviceName = HdfSbufReadString(data);
67     if (serviceName == NULL) {
68         return HDF_FAILURE;
69     }
70     HdfUnregisteDevice(moduleName, serviceName);
71     return HDF_SUCCESS;
72 }
73 
SampleDriverSendEvent(struct HdfDeviceIoClient * client,int id,struct HdfSBuf * data,bool broadcast)74 int32_t SampleDriverSendEvent(struct HdfDeviceIoClient *client, int id, struct HdfSBuf *data, bool broadcast)
75 {
76     int32_t ret = broadcast ? HdfDeviceSendEvent(client->device, id, data) :
77         HdfDeviceSendEventToClient(client, id, data);
78     HDF_LOGI("%{public}s:report event done, broadcast = %{public}d", __func__, broadcast);
79     return ret;
80 }
81 
SampleDriverDispatch(struct HdfDeviceIoClient * client,int cmdId,struct HdfSBuf * data,struct HdfSBuf * reply)82 int32_t SampleDriverDispatch(struct HdfDeviceIoClient *client, int cmdId, struct HdfSBuf *data, struct HdfSBuf *reply)
83 {
84     int32_t ret = HDF_SUCCESS;
85     if (reply == NULL || client == NULL) {
86         return HDF_FAILURE;
87     }
88     HDF_LOGI("%{public}s:cmdId = %{public}d", __func__, cmdId);
89     switch (cmdId) {
90         case SAMPLE_DRIVER_REGISTE_DEVICE: {
91             ret = SampleDriverRegisteDevice(data);
92             HdfSbufWriteInt32(reply, ret);
93             break;
94         }
95         case SAMPLE_DRIVER_UNREGISTE_DEVICE:
96             ret = SampleDriverUnregisteDevice(data);
97             HdfSbufWriteInt32(reply, ret);
98             break;
99         case SAMPLE_DRIVER_SENDEVENT_SINGLE_DEVICE:
100             ret =  SampleDriverSendEvent(client, cmdId, data, false);
101             HdfSbufWriteInt32(reply, INT32_MAX);
102             break;
103         case SAMPLE_DRIVER_SENDEVENT_BROADCAST_DEVICE:
104             ret = SampleDriverSendEvent(client, cmdId, data, true);
105             HdfSbufWriteInt32(reply, INT32_MAX);
106             break;
107         default:
108             break;
109     }
110 
111     return ret;
112 }
113 
HdfSampleDriverBind(struct HdfDeviceObject * deviceObject)114 int HdfSampleDriverBind(struct HdfDeviceObject *deviceObject)
115 {
116     HDF_LOGD("%{public}s::enter!, deviceObject=%{public}p", __func__, deviceObject);
117     if (deviceObject == NULL) {
118         return HDF_FAILURE;
119     }
120     static struct IDeviceIoService testService = {
121         .Dispatch = SampleDriverDispatch,
122         .Open = NULL,
123         .Release = NULL,
124     };
125     deviceObject->service = &testService;
126     return HDF_SUCCESS;
127 }
128 
HdfSampleDriverInit(struct HdfDeviceObject * deviceObject)129 int HdfSampleDriverInit(struct HdfDeviceObject *deviceObject)
130 {
131     HDF_LOGD("%{public}s::enter!, deviceObject=%{public}p", __func__, deviceObject);
132     if (deviceObject == NULL) {
133         HDF_LOGE("%{public}s::ptr is null!", __func__);
134         return HDF_FAILURE;
135     }
136     HDF_LOGD("%{public}s:Init success", __func__);
137     return HDF_SUCCESS;
138 }
139 
140 
141 struct HdfDriverEntry g_sampleDriverEntry = {
142     .moduleVersion = 1,
143     .moduleName = "sample_driver",
144     .Bind = HdfSampleDriverBind,
145     .Init = HdfSampleDriverInit,
146     .Release = HdfSampleDriverRelease,
147 };
148 
149 HDF_INIT(g_sampleDriverEntry);
150 
151