1# 驱动服务管理<a name="ZH-CN_TOPIC_0000001052777057"></a> 2 3- [使用场景](#section14244270117) 4- [接口说明](#section1432412561722) 5- [开发步骤](#section393515164416) 6 7驱动服务是HDF驱动设备对外提供能力的对象,由HDF框架统一管理。驱动服务管理主要包含驱动服务的发布和获取。 8 9HDF框架定义了驱动对外发布服务的策略,是由配置文件中的policy字段来控制,policy字段的取值范围以及含义如下: 10 11``` 12typedef enum { 13 /* 驱动不提供服务 */ 14 SERVICE_POLICY_NONE = 0, 15 /* 驱动对内核态发布服务 */ 16 SERVICE_POLICY_PUBLIC = 1, 17 /* 驱动对内核态和用户态都发布服务 */ 18 SERVICE_POLICY_CAPACITY = 2, 19 /* 驱动服务不对外发布服务,但可以被订阅 */ 20 SERVICE_POLICY_FRIENDLY = 3, 21 /* 驱动私有服务不对外发布服务,也不能被订阅 */ 22 SERVICE_POLICY_PRIVATE = 4, 23 /* 错误的服务策略 */ 24 SERVICE_POLICY_INVALID 25} ServicePolicy; 26``` 27 28## 使用场景<a name="section14244270117"></a> 29 30当驱动以接口的形式对外提供能力时,可以使用HDF框架的驱动服务管理能力。 31 32## 接口说明<a name="section1432412561722"></a> 33 34针对驱动服务管理功能,HDF框架开放了以下接口供开发者调用,如下表所示: 35 36**表 1** 服务管理接口 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>方法</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>描述</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>需要驱动开发者实现Bind函数,将自己的服务接口绑定到HDF框架中。</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>获取驱动的服务。</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>订阅驱动的服务。</p> 59</td> 60</tr> 61</tbody> 62</table> 63 64## 开发步骤<a name="section393515164416"></a> 65 66驱动服务管理的开发包括驱动服务的编写、绑定、获取或者订阅,详细步骤如下。 67 681. 驱动服务发布。 69 70 ``` 71 驱动服务结构的定义 72 struct ISampleDriverService { 73 struct IDeviceIoService ioService; // 服务结构的首个成员必须是IDeviceIoService类型的成员 74 int32_t (*ServiceA)(void); // 驱动的第一个服务接口 75 int32_t (*ServiceB)(uint32_t inputCode); // 驱动的第二个服务接口,有多个可以依次往下累加 76 }; 77 78 驱动服务接口的实现 79 int32_t SampleDriverServiceA(void) 80 { 81 // 驱动开发者实现业务逻辑 82 return 0; 83 } 84 85 int32_t SampleDriverServiceB(uint32_t inputCode) 86 { 87 // 驱动开发者实现业务逻辑 88 return 0; 89 } 90 ``` 91 922. 驱动服务绑定到HDF框架中,实现HdfDriverEntry中的Bind指针函数。 93 94 ``` 95 int32_t SampleDriverBind(struct HdfDeviceObject *deviceObject) 96 { 97 // deviceObject为HDF框架给每一个驱动创建的设备对象,用来保存设备相关的私有数据和服务接口 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. 驱动服务获取。 112 113 驱动服务的获取有两种方式,HDF框架提供接口直接获取和HDF框架提供订阅机制获取。 114 115 - 通过HDF接口直接获取 116 117 当明确驱动已经加载完成时,获取该驱动的服务可以通过HDF框架提供的能力接口直接获取,如下所示: 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 - 通过HDF提供的订阅机制获取 130 131 当内核态对驱动(同一个host)加载的时机不感知时,可以通过HDF框架提供的订阅机制来订阅该驱动,当该驱动加载完成时,HDF框架会将被订阅的驱动服务发布给订阅者,实现方式如下所示: 132 133 ``` 134 // 订阅回调函数的编写,当被订阅的驱动加载完成后,HDF框架会将被订阅驱动的服务发布给订阅者,通过这个回调函数给订阅者使用 135 // object为订阅者的私有数据,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 // 订阅过程的实现 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