• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Driver Messaging Mechanism
2
3
4## When to Use
5
6The HDF messaging mechanism implements the interaction between the user-mode applications and kernel-mode drivers.
7
8
9## Available APIs
10
11The messaging mechanism allows:
12
131. A user-mode application to send a message to a driver.
14
152. A user-mode application to receive events reported by a driver.
16
17  **Table 1** APIs for the driver messaging mechanism
18
19| API| Description|
20| -------- | -------- |
21| struct HdfIoService \*HdfIoServiceBind(const char \*serviceName); | Obtains a driver service. After obtaining the driver service, the user-mode application uses **Dispatch()** in the driver service obtained to send messages to the driver.|
22| void HdfIoServiceRecycle(struct HdfIoService \*service); | Releases a driver service.|
23| int HdfDeviceRegisterEventListener(struct HdfIoService \*target, struct HdfDevEventlistener \*listener); | Registers the method for receiving events reported by the driver.|
24| int HdfDeviceSendEvent(struct HdfDeviceObject \*deviceObject, uint32_t id, struct HdfSBuf \*data); | Sends events. |
25
26
27## How to Develop
28
291. In the driver configuration file, set **policy** to **2**. For more details, see [Driver Service Management](../driver/driver-hdf-servicemanage.md).
30
31   ```
32   device_sample :: Device {
33       policy = 2;
34       ...
35   }
36   ```
37
382. Set permissions for the device node of the driver. By default, the **permission** field is set to **0666**. You can set it based on service requirements.
39
403. Implement the **Dispatch()** method of **IDeviceIoService**.
41
42   ```c
43   // Dispatch() is used to process messages sent from the user-mode application.
44   int32_t SampleDriverDispatch(struct HdfDeviceIoClient *device, int cmdCode, struct HdfSBuf *data, struct HdfSBuf *reply)
45   {
46       HDF_LOGI("sample driver lite A dispatch");
47       return HDF_SUCCESS;
48   }
49   int32_t SampleDriverBind(struct HdfDeviceObject *device)
50   {
51       HDF_LOGI("test for lite os sample driver A Open!");
52       if (device == NULL) {
53           HDF_LOGE("test for lite os sample driver A Open failed!");
54           return HDF_FAILURE;
55       }
56       static struct ISampleDriverService sampleDriverA = {
57           .ioService.Dispatch = SampleDriverDispatch,
58           .ServiceA = SampleDriverServiceA,
59           .ServiceB = SampleDriverServiceB,
60       };
61       device->service = (struct IDeviceIoService *)(&sampleDriverA);
62       return HDF_SUCCESS;
63   }
64   ```
65
664. Define the cmd type in the message processing function.
67
68   ```c
69   #define SAMPLE_WRITE_READ 1 // Read and write operation 1
70   ```
71
725. Enable the user-mode application to obtain a service and send a message to the driver.
73
74   ```c
75   int SendMsg(const char *testMsg)
76   {
77       if (testMsg == NULL) {
78           HDF_LOGE("test msg is null");
79           return HDF_FAILURE;
80       }
81       struct HdfIoService *serv = HdfIoServiceBind("sample_driver");
82       if (serv == NULL) {
83           HDF_LOGE("fail to get service");
84           return HDF_FAILURE;
85       }
86       struct HdfSBuf *data = HdfSbufObtainDefaultSize();
87       if (data == NULL) {
88           HDF_LOGE("fail to obtain sbuf data");
89           return HDF_FAILURE;
90       }
91       struct HdfSBuf *reply = HdfSbufObtainDefaultSize();
92       if (reply == NULL) {
93           HDF_LOGE("fail to obtain sbuf reply");
94           ret = HDF_DEV_ERR_NO_MEMORY;
95           goto out;
96       }
97       if (!HdfSbufWriteString(data, testMsg)) {
98           HDF_LOGE("fail to write sbuf");
99           ret = HDF_FAILURE;
100           goto out;
101       }
102       int ret = serv->dispatcher->Dispatch(&serv->object, SAMPLE_WRITE_READ, data, reply);
103       if (ret != HDF_SUCCESS) {
104           HDF_LOGE("fail to send service call");
105           goto out;
106       }
107   out:
108       HdfSbufRecycle(data);
109       HdfSbbufRecycle(reply);
110       HdfIoServiceRecycle(serv);
111       return ret;
112   }
113   ```
114
1156. Enable the user-mode application to receive messages from the driver.
116   1. Implement the method for the user-mode application to process the events reported by the driver.
117
118       ```c
119       static int OnDevEventReceived(void *priv,  uint32_t id, struct HdfSBuf *data)
120       {
121           OsalTimespec time;
122           OsalGetTime(&time);
123           HDF_LOGI("%{public}s received event at %{public}llu.%{public}llu", (char *)priv, time.sec, time.usec);
124
125           const char *string = HdfSbufReadString(data);
126           if (string == NULL) {
127               HDF_LOGE("fail to read string in event data");
128               return HDF_FAILURE;
129           }
130           HDF_LOGI("%{public}s: dev event received: %{public}d %{public}s",  (char *)priv, id, string);
131           return HDF_SUCCESS;
132       }
133       ```
134   2. Register the method for the user-mode application to receive messages from the driver.
135
136       ```c
137       int RegisterListen()
138       {
139           struct HdfIoService *serv = HdfIoServiceBind("sample_driver");
140           if (serv == NULL) {
141               HDF_LOGE("fail to get service");
142               return HDF_FAILURE;
143           }
144           static struct HdfDevEventlistener listener = {
145               .callBack = OnDevEventReceived,
146               .priv ="Service0"
147           };
148           if (HdfDeviceRegisterEventListener(serv, &listener) != 0) {
149               HDF_LOGE("fail to register event listener");
150               return HDF_FAILURE;
151           }
152           ......
153           HdfDeviceUnregisterEventListener(serv, &listener);
154           HdfIoServiceRecycle(serv);
155           return HDF_SUCCESS;
156       }
157       ```
158   3. Enable the driver to report events.
159
160       ```c
161       int32_t SampleDriverDispatch(HdfDeviceIoClient *client, int cmdCode, struct HdfSBuf *data, struct HdfSBuf *reply)
162       {
163           // Process the API call.
164           return HdfDeviceSendEvent(client->device, cmdCode, data);
165       }
166       ```
167