HDF框架实现了用户层与内核层进行通信的管理框架,关于其简易通信示例在以下两篇博文中有所介绍,
- 一个例子了解通过Openharmony的HDF框架实现简易驱动的流程
https://blog.csdn.net/procedurecode/article/details/128906246
- Openharmony的用户态应用通过HDF框架驱动消息机制实现的通信实例
https://blog.csdn.net/procedurecode/article/details/128921897
本文以常用的usb硬件为例,深入usb代码,剖析从usb用户层到驱动到内核层的整体流程,同时可以更深入的理解Openharmony的HDF框架。本文讲解从框架层开始,NAPI接口简单来说就是对框架层提供的接口进行的ets的封装,在此不做表述。好的,正好开始:
1.关于usb调用hdf驱动流程
OpenDevice接口为例
1.1 UsbSrvClient类
// 1.首先获取UsbSrvClient实例
UsbSrvClient &g_usbClient = UsbSrvClient::GetInstance();
// 2.OpenDevice
// base/usb/usb_manager/interfaces/kits/js/napi/src/usb_info.cpp
g_usbClient.OpenDevice(dev, pipe);
// 3.UsbSrvClient::OpenDevice
// base/usb/usb_manager/interfaces/innerkits/native/src/usb_srv_client.cpp
int32_t UsbSrvClient::OpenDevice(const UsbDevice &device, USBDevicePipe &pipe)int32_t ret = proxy_->OpenDevice(device.GetBusNum(), device.GetDevAddr());
在UsbSrvClient类中proxy_成员类型为如下所示:
// base/usb/usb_manager/interfaces/innerkits/native/include/usb_srv_client.h
sptr<IUsbSrv> proxy_ = nullptr;
proxy_在UsbSrvClient类的构造函数中调用Connect()方法实现初始化。
int32_t UsbSrvClient::Connect()
{std::lock_guard<std::mutex> lock(mutex_);if (proxy_ != nullptr) {return UEC_OK;}sptr<ISystemAbilityManager> sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();if (sm == nullptr) {USB_HILOGE(MODULE_USB_INNERKIT, "fail to get SystemAbilityManager");return UEC_INTERFACE_GET_SYSTEM_ABILITY_MANAGER_FAILED;}sptr<IRemoteObject> remoteObject = sm->CheckSystemAbility(USB_SYSTEM_ABILITY_ID);if (remoteObject == nullptr) {USB_HILOGE(MODULE_USB_INNERKIT, "GetSystemAbility failed.");return UEC_INTERFACE_GET_USB_SERVICE_FAILED;}proxy_ = iface_cast<IUsbSrv>(remoteObject);USB_HILOGI(MODULE_USB_INNERKIT, "Connect UsbService ok.");sptr<IRemot