• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# HDF Development Example<a name="EN-US_TOPIC_0000001052451677"></a>
2
3-   [Adding Configurations](#section27261067111)
4-   [Compiling the Driver Code](#section177988005)
5-   [Compiling the Code for Interaction](#section6205173816412)
6
7The following example shows how to add configurations, implement the driver code, and compile the code for interaction between the user-level applications and the driver.
8
9## Adding Configurations<a name="section27261067111"></a>
10
11Add the driver configurations to the HDF configuration file \(for example,  **vendor/hisilicon/xxx/config/device\_info**\). Example:
12
13```
14root {
15    device_info {
16        match_attr = "hdf_manager";
17        template host {
18            hostName = "";
19            priority = 100;
20            template device {
21                template deviceNode {
22                    policy = 0;
23                    priority = 100;
24                    preload = 0;
25                    permission = 0664;
26                    moduleName = "";
27                    serviceName = "";
28                    deviceMatchAttr = "";
29                }
30            }
31        }
32        sample_host :: host {
33            hostName = "sample_host";
34            sample_device :: device {
35                device0 :: deviceNode {
36                    policy = 2;
37                    priority = 100;
38                    preload = 1;
39                    permission = 0664;
40                    moduleName = "sample_driver";
41                    serviceName = "sample_service";
42                }
43            }
44        }
45    }
46}
47```
48
49## Compiling the Driver Code<a name="section177988005"></a>
50
51A sample of driver code compiled based on the HDF is as follows:
52
53```
54#include <fcntl.h>
55#include <sys/stat.h>
56#include <sys/ioctl.h>
57#include "hdf_log.h"
58#include "hdf_base.h"
59#include "hdf_device_desc.h"
60
61#define HDF_LOG_TAG "sample_driver"
62
63#define SAMPLE_WRITE_READ 123
64
65int32_t HdfSampleDriverDispatch(
66    struct HdfDeviceObject *deviceObject, int id, struct HdfSBuf *data, struct HdfSBuf *reply)
67{
68    HDF_LOGE("%s: received cmd %d", __func__, id);
69    if (id == SAMPLE_WRITE_READ) {
70        const char *readData = HdfSbufReadString(data);
71        if (readData != NULL) {
72            HDF_LOGE("%s: read data is: %s", __func__, readData);
73        }
74        if (!HdfSbufWriteInt32(reply, INT32_MAX)) {
75            HDF_LOGE("%s: reply int32 fail", __func__);
76        }
77        return HdfDeviceSendEvent(deviceObject, id, data);
78    }
79    return HDF_FAILURE;
80}
81
82void HdfSampleDriverRelease(struct HdfDeviceObject *deviceObject)
83{
84    // Release resources here
85    return;
86}
87
88int HdfSampleDriverBind(struct HdfDeviceObject *deviceObject)
89{
90    if (deviceObject == NULL) {
91        return HDF_FAILURE;
92    }
93    static struct IDeviceIoService testService = {
94        .Dispatch = HdfSampleDriverDispatch,
95    };
96    deviceObject->service = &testService;
97    return HDF_SUCCESS;
98}
99
100int HdfSampleDriverInit(struct HdfDeviceObject *deviceObject)
101{
102    if (deviceObject == NULL) {
103        HDF_LOGE("%s::ptr is null!", __func__);
104        return HDF_FAILURE;
105    }
106    HDF_LOGE("Sample driver Init success");
107    return HDF_SUCCESS;
108}
109
110struct HdfDriverEntry g_sampleDriverEntry = {
111    .moduleVersion = 1,
112    .moduleName = "sample_driver",
113    .Bind = HdfSampleDriverBind,
114    .Init = HdfSampleDriverInit,
115    .Release = HdfSampleDriverRelease,
116};
117
118HDF_INIT(g_sampleDriverEntry);
119```
120
121## Compiling the Code for Interaction<a name="section6205173816412"></a>
122
123A sample code for interaction between the user-level application and driver compiled based on the HDF is as follows:
124
125```
126#include <fcntl.h>
127#include <sys/stat.h>
128#include <sys/ioctl.h>
129#include <unistd.h>
130#include "hdf_log.h"
131#include "hdf_sbuf.h"
132#include "hdf_io_service_if.h"
133
134#define HDF_LOG_TAG "sample_test"
135#define SAMPLE_SERVICE_NAME "sample_service"
136
137#define SAMPLE_WRITE_READ 123
138
139int g_replyFlag = 0;
140
141static int OnDevEventReceived(void *priv,  uint32_t id, struct HdfSBuf *data)
142{
143    const char *string = HdfSbufReadString(data);
144    if (string == NULL) {
145        HDF_LOGE("fail to read string in event data");
146        g_replyFlag = 1;
147        return HDF_FAILURE;
148    }
149    HDF_LOGE("%s: dev event received: %u %s",  (char *)priv, id, string);
150    g_replyFlag = 1;
151    return HDF_SUCCESS;
152}
153
154static int SendEvent(struct HdfIoService *serv, char *eventData)
155{
156    int ret = 0;
157    struct HdfSBuf *data = HdfSBufObtainDefaultSize();
158    if (data == NULL) {
159        HDF_LOGE("fail to obtain sbuf data");
160        return 1;
161    }
162
163    struct HdfSBuf *reply = HdfSBufObtainDefaultSize();
164    if (reply == NULL) {
165        HDF_LOGE("fail to obtain sbuf reply");
166        ret = HDF_DEV_ERR_NO_MEMORY;
167        goto out;
168    }
169
170    if (!HdfSbufWriteString(data, eventData)) {
171        HDF_LOGE("fail to write sbuf");
172        ret = HDF_FAILURE;
173        goto out;
174    }
175
176    ret = serv->dispatcher->Dispatch(&serv->object, SAMPLE_WRITE_READ, data, reply);
177    if (ret != HDF_SUCCESS) {
178        HDF_LOGE("fail to send service call");
179        goto out;
180    }
181
182    int replyData = 0;
183    if (!HdfSbufReadInt32(reply, &replyData)) {
184        HDF_LOGE("fail to get service call reply");
185        ret = HDF_ERR_INVALID_OBJECT;
186        goto out;
187    }
188    HDF_LOGE("Get reply is: %d", replyData);
189out:
190    HdfSBufRecycle(data);
191    HdfSBufRecycle(reply);
192    return ret;
193}
194
195int main()
196{
197    char *sendData = "default event info";
198    struct HdfIoService *serv = HdfIoServiceBind(SAMPLE_SERVICE_NAME);
199    if (serv == NULL) {
200        HDF_LOGE("fail to get service %s", SAMPLE_SERVICE_NAME);
201        return HDF_FAILURE;
202    }
203
204    static struct HdfDevEventlistener listener = {
205        .callBack = OnDevEventReceived,
206        .priv ="Service0"
207    };
208
209    if (HdfDeviceRegisterEventListener(serv, &listener) != HDF_SUCCESS) {
210        HDF_LOGE("fail to register event listener");
211        return HDF_FAILURE;
212    }
213    if (SendEvent(serv, sendData)) {
214        HDF_LOGE("fail to send event");
215        return HDF_FAILURE;
216    }
217
218    while (g_replyFlag == 0) {
219        sleep(1);
220    }
221
222    if (HdfDeviceUnregisterEventListener(serv, &listener)) {
223        HDF_LOGE("fail to  unregister listener");
224        return HDF_FAILURE;
225    }
226
227    HdfIoServiceRecycle(serv);
228    return HDF_SUCCESS;
229}
230```
231
232>![](../public_sys-resources/icon-note.gif) **NOTE:**
233>The code compilation of user-level applications depends on the dynamic libraries  **hdf\_core**  and  **osal**  provided by the HDF because user-level applications use the message sending interface of the HDF. In the GN compilation file, add the following dependency relationships:
234>deps = \[
235>"//drivers/adapter/lite/uhdf/manager:hdf\_core",
236>"//drivers/adapter/lite/uhdf/posix:hdf\_posix\_osal",
237>\]
238
239