1# Driver Service Management<a name="EN-US_TOPIC_0000001052777057"></a> 2 3- [When to Use](#section14244270117) 4- [Available APIs](#section1432412561722) 5- [How to Develop](#section393515164416) 6 7Driver services are objects of open capabilities provided by the HDF and are managed by the HDF in a unified manner. Using driver service management, you can release and obtain driver services. 8 9The HDF uses the **policy** field in the configuration file to define policies for drivers to release services externally. The values and meanings of this field are as follows: 10 11``` 12typedef enum { 13 /* The driver does not provide services. */ 14 SERVICE_POLICY_NONE = 0, 15 /* The driver provides services for kernel-space applications. */ 16 SERVICE_POLICY_PUBLIC = 1, 17 /* The driver provides services for both kernel- and user-space applications. */ 18 SERVICE_POLICY_CAPACITY = 2, 19 /* The driver services are not released externally but can be subscribed to. */ 20 SERVICE_POLICY_FRIENDLY = 3, 21 /* The driver services are private. They are neither released nor subscribed to. */ 22 SERVICE_POLICY_PRIVATE = 4, 23 /* The service policy is incorrect. */ 24 SERVICE_POLICY_INVALID 25} ServicePolicy; 26``` 27 28## When to Use<a name="section14244270117"></a> 29 30The driver service management capability can be used if the driver provides capabilities using APIs. 31 32## Available APIs<a name="section1432412561722"></a> 33 34The table below describes the APIs used for driver service management. 35 36**Table 1** APIs for driver service management 37 38<a name="table8431122013592"></a> 39<table><thead align="left"><tr id="row13431820135919"><th class="cellrowborder" valign="top" width="50%" id="mcps1.2.3.1.1"><p id="p1670132714592"><a name="p1670132714592"></a><a name="p1670132714592"></a><strong id="b12478202911367"><a name="b12478202911367"></a><a name="b12478202911367"></a>Function</strong></p> 40</th> 41<th class="cellrowborder" valign="top" width="50%" id="mcps1.2.3.1.2"><p id="p770172785910"><a name="p770172785910"></a><a name="p770172785910"></a><strong id="b134538148443950"><a name="b134538148443950"></a><a name="b134538148443950"></a>Description</strong></p> 42</th> 43</tr> 44</thead> 45<tbody><tr id="row1743112017594"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p18601333135911"><a name="p18601333135911"></a><a name="p18601333135911"></a>int32_t (*Bind)(struct HdfDeviceObject *deviceObject);</p> 46</td> 47<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p46015332591"><a name="p46015332591"></a><a name="p46015332591"></a>Binds a service interface to the HDF. You need to implement the <strong id="b876584084419"><a name="b876584084419"></a><a name="b876584084419"></a>Bind</strong> function.</p> 48</td> 49</tr> 50<tr id="row1543212045914"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p19601163314590"><a name="p19601163314590"></a><a name="p19601163314590"></a>const struct HdfObject *DevSvcManagerClntGetService(const char *svcName);</p> 51</td> 52<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p1601123318598"><a name="p1601123318598"></a><a name="p1601123318598"></a>Obtains a specified driver service.</p> 53</td> 54</tr> 55<tr id="row20432162019594"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p960173310590"><a name="p960173310590"></a><a name="p960173310590"></a>int HdfDeviceSubscribeService(</p> 56<p id="p126021533165915"><a name="p126021533165915"></a><a name="p126021533165915"></a>struct HdfDeviceObject *deviceObject, const char *serviceName, struct SubscriberCallback callback);</p> 57</td> 58<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p06029334597"><a name="p06029334597"></a><a name="p06029334597"></a>Subscribes to a specified driver service.</p> 59</td> 60</tr> 61</tbody> 62</table> 63 64## How to Develop<a name="section393515164416"></a> 65 66The development of driver service management includes compiling, binding, obtaining, and subscribing to driver services. The details are as follows: 67 681. Release a driver service. 69 70 ``` 71 Define the driver service structure. 72 struct ISampleDriverService { 73 struct IDeviceIoService ioService; // The first member of the service structure must be a member of the IDeviceIoService type. 74 int32_t (*ServiceA)(void); // The first service interface of the driver. 75 int32_t (*ServiceB)(uint32_t inputCode); // The second service interface of the driver. The rest can be deduced by analogy. 76 }; 77 78 Implementation of the driver service interface 79 int32_t SampleDriverServiceA(void) 80 { 81 // You need to implement the service logic. 82 return 0; 83 } 84 85 int32_t SampleDriverServiceB(uint32_t inputCode) 86 { 87 // You need to implement the service logic. 88 return 0; 89 } 90 ``` 91 922. Bind the driver service to the HDF and implement the **Bind** function in the **HdfDriverEntry** structure. 93 94 ``` 95 int32_t SampleDriverBind(struct HdfDeviceObject *deviceObject) 96 { 97 // deviceObject indicates the pointer to the device object created by the HDF for each driver. It is used to store device-related private data and service interfaces. 98 if (deviceObject == NULL) { 99 HDF_LOGE("Sample device object is null!"); 100 return -1; 101 } 102 static struct ISampleDriverService sampleDriverA = { 103 .ServiceA = SampleDriverServiceA, 104 .ServiceB = SampleDriverServiceB, 105 }; 106 deviceObject->service = &sampleDriverA.ioService; 107 return 0; 108 } 109 ``` 110 1113. Obtain the driver service. 112 113 You can either use the API or subscription mechanism provided by the HDF to obtain the driver service. 114 115 - Using the API 116 117 After the driver is loaded, you can obtain the driver service using the API provided by the HDF, as shown in the following: 118 119 ``` 120 const struct ISampleDriverService *sampleService = 121 (const struct ISampleDriverService *)DevSvcManagerClntGetService("sample_driver"); 122 if (sampleService == NULL) { 123 return -1; 124 } 125 sampleService->ServiceA(); 126 sampleService->ServiceB(5); 127 ``` 128 129 - Using the subscription mechanism 130 131 If the kernel sapce unaware of the time for loading drivers \(on the same host\), use the subscription mechanism provided by the HDF to subscribe to the drivers. After the drivers are loaded, the HDF releases the driver services to you. The implementation is as follows: 132 133 ``` 134 // Subscription callback function. After the subscribed drivers are loaded, the HDF releases the driver services to you using this function. 135 // object indicates the pointer to the private data of the subscriber, and service indicates the pointer to the subscribed service. 136 int32_t TestDriverSubCallBack(struct HdfDeviceObject *deviceObject, const struct HdfObject *service) 137 { 138 const struct ISampleDriverService *sampleService = 139 (const struct ISampleDriverService *)service; 140 if (sampleService == NULL) { 141 return -1; 142 } 143 sampleService->ServiceA(); 144 sampleService->ServiceB(5); 145 } 146 // Implement the subscription process. 147 int32_t TestDriverInit(struct HdfDeviceObject *deviceObject) 148 { 149 if (deviceObject == NULL) { 150 HDF_LOGE("Test driver init failed, deviceObject is null!"); 151 return -1; 152 } 153 struct SubscriberCallback callBack; 154 callBack.deviceObject = deviceObject; 155 callBack.OnServiceConnected = TestDriverSubCallBack; 156 int32_t ret = HdfDeviceSubscribeService(deviceObject, "sample_driver", callBack); 157 if (ret != 0) { 158 HDF_LOGE("Test driver subscribe sample driver failed!"); 159 } 160 return ret; 161 } 162 ``` 163 164 165 166