同步操作将从 OpenHarmony/docs 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
Driver 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.
The 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:
typedef enum {
/* The driver does not provide services. */
SERVICE_POLICY_NONE = 0,
/* The driver provides services for kernel-space applications. */
SERVICE_POLICY_PUBLIC = 1,
/* The driver provides services for both kernel- and user-space applications. */
SERVICE_POLICY_CAPACITY = 2,
/* The driver services are not released externally but can be subscribed to. */
SERVICE_POLICY_FRIENDLY = 3,
/* The driver services are private. They are neither released nor subscribed to. */
SERVICE_POLICY_PRIVATE = 4,
/* The service policy is incorrect. */
SERVICE_POLICY_INVALID
} ServicePolicy;
The driver service management capability can be used if the driver provides capabilities using APIs.
The table below describes the APIs used for driver service management.
Table 1 APIs for driver service management
The development of driver service management includes compiling, binding, obtaining, and subscribing to driver services. The details are as follows:
Release a driver service.
Define the driver service structure.
struct ISampleDriverService {
struct IDeviceIoService ioService; // The first member of the service structure must be a member of the IDeviceIoService type.
int32_t (*ServiceA)(void); // The first service interface of the driver.
int32_t (*ServiceB)(uint32_t inputCode); // The second service interface of the driver. The rest can be deduced by analogy.
};
Implementation of the driver service interface
int32_t SampleDriverServiceA(void)
{
// You need to implement the service logic.
return 0;
}
int32_t SampleDriverServiceB(uint32_t inputCode)
{
// You need to implement the service logic.
return 0;
}
Bind the driver service to the HDF and implement the Bind function in the HdfDriverEntry structure.
int32_t SampleDriverBind(struct HdfDeviceObject *deviceObject)
{
// 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.
if (deviceObject == NULL) {
HDF_LOGE("Sample device object is null!");
return -1;
}
static struct ISampleDriverService sampleDriverA = {
.ServiceA = SampleDriverServiceA,
.ServiceB = SampleDriverServiceB,
};
deviceObject->service = &sampleDriverA.ioService;
return 0;
}
Obtain the driver service.
You can either use the API or subscription mechanism provided by the HDF to obtain the driver service.
Using the API
After the driver is loaded, you can obtain the driver service using the API provided by the HDF, as shown in the following:
const struct ISampleDriverService *sampleService =
(const struct ISampleDriverService *)DevSvcManagerClntGetService("sample_driver");
if (sampleService == NULL) {
return -1;
}
sampleService->ServiceA();
sampleService->ServiceB(5);
Using the subscription mechanism
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:
// Subscription callback function. After the subscribed drivers are loaded, the HDF releases the driver services to you using this function.
// object indicates the pointer to the private data of the subscriber, and service indicates the pointer to the subscribed service.
int32_t TestDriverSubCallBack(struct HdfDeviceObject *deviceObject, const struct HdfObject *service)
{
const struct ISampleDriverService *sampleService =
(const struct ISampleDriverService *)service;
if (sampleService == NULL) {
return -1;
}
sampleService->ServiceA();
sampleService->ServiceB(5);
}
// Implement the subscription process.
int32_t TestDriverInit(struct HdfDeviceObject *deviceObject)
{
if (deviceObject == NULL) {
HDF_LOGE("Test driver init failed, deviceObject is null!");
return -1;
}
struct SubscriberCallback callBack;
callBack.deviceObject = deviceObject;
callBack.OnServiceConnected = TestDriverSubCallBack;
int32_t ret = HdfDeviceSubscribeService(deviceObject, "sample_driver", callBack);
if (ret != 0) {
HDF_LOGE("Test driver subscribe sample driver failed!");
}
return ret;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。