HDI接口中如何实现驱动入口

hdi接口概述
hdf 驱动框架的一个重要功能是为系统提供稳定的统一的硬件接口,这样才能保证系统服务可以运行在不同硬件上而不需要额外的适配工作,而hdi(hardware device interfaces)正是为了实现该目的而设计。
hdi 是对硬件功能的较高层次抽象接口,各类外设完成 hdi 接口定义后便只会在 hdi 的兼容性规则下进行变更,从而保证接口的稳定性。具体的驱动实现不需要再重复定义 hdi 接口,只需要按需实现即可接入系统功能。
在不同量级的 openharmony 系统上,hdi 存在两种部署形态,ipc 模式和直通模式。
在轻量级 openharmony 系统上,出于减小系统性能负载考虑,hdi 实现为用户态共享库,由系统服务直接加载 hdi 实现到自己进程中函数调用使用。hdi 实现封装具体的用户态-内核态交互过程,当需要访问驱动程序时使用 io service 请求将消息通过 system call 方式调用到内核驱动实现。
在 openharmony 系统上,hdi 以独立服务进程方式部署,系统服务只加载 hdi 客户端实现到自己进程中,实际业务运行在独立进程中,客户端通过 ipc 与服务端交互,便于架构解耦、权限管理。
hdi接口实现
直通模式为函数实现方式,无论调用还是实现都不需要其他组件支持即可实现,这里将重点分析 ipc 模式的实现。
hdi发布
hdi ipc 模式基于 openharmony 系统通信框架的通用模型,但是因为驱动很多时候涉及到底层操作和多系统迁移的场景而使用c语言编写,所以驱动框架还提供了 hdi 服务的 c 语言实现的基础组件,c++实现则主要使用系统通信框架组件。
hdi 服务发布基于 uhdf(用户态 hdf 驱动框架)实现,通用的服务发布实现如下。
1.实现驱动入口
int sampledriverbind(struct hdfdeviceobject *deviceobject){ hdf_loge(“sampledriverbind enter!”); static struct ideviceioservice testservice = { .dispatch = sampleservicedispatch, // 服务回调接口 }; deviceobject-》service = &testservice; return hdf_success;} int sampledriverinit(struct hdfdeviceobject *deviceobject){ hdf_loge(“sampledriverinit enter”); return hdf_success;} void sampledriverrelease(struct hdfdeviceobject *deviceobject){ hdf_loge(“sampledriverrelease enter”); return;} struct hdfdriverentry g_sampledriverentry = { .moduleversion = 1, .modulename = “sample_driver”, .bind = sampledriverbind, .init = sampledriverinit, .release = sampledriverrelease,};
hdf_init(g_sampledriverentry);
首先要添加一个 uhdf 驱动用于发布 ioservice 服务,ioservice 设备服务即为 hdi 服务实体。实现方式与 khdf 驱动一致。
2.实现服务响应接口
int32_t sampleserviceonremoterequest(struct hdfdeviceioclient *client, int cmdid, struct hdfsbuf *data, struct hdfsbuf *reply){ switch (cmdid) { case sample_service_ping: return sampleservicestubping(client, data, reply); … … default: hdf_loge(“sampleservicedispatch: not support cmd %d”, cmdid); return hdf_err_invalid_param; }}static int32_t sampleservicedispatch(struct hdfdeviceioclient *client, int cmdid, struct hdfsbuf *data, struct hdfsbuf *reply){ return sampleserviceonremoterequest(client, cmdid, data, reply);}
当收到 hdi 调用时,服务响应接口“sampleservicedispatch”将会被调用。
client 调用者对象,在用户态驱动中暂时未支持
cmdid 调用命令字,用于区分调用的 api
data 调用入参序列化对象,在 ipc 调用场景为 parcel 对象的 c 语言封装,入参需要使用序列化接口从 data 对象中获取后再使用
reply 调用出参对象,需要返回给调用的信息写入该序列化对象
如果 c++实现客户端可以使用下面接口将 sbuf 对象转换为 parcel 对象后操作:
int32_t sbuftoparcel(struct hdfsbuf *sbuf, ohos::messageparcel **parcel);
3.uhdf 驱动配置
platform :: host { hostname = “sample_host”; priority = 50; sample_device :: device { device0 :: devicenode { policy = 2; priority = 100; modulename = “libsample_driver.z.so”; servicename = “sample_driver_service”; } }}
参数说明:
host 一个 host 节点即为一个独立进程,如果需要独立进程,新增属于自己的 host 节点
policy 服务发布策略,hdi 服务设置为 2
modulename 驱动实现库名
servicename 服务名称,请保持全局唯一性
因为 hdi 服务 c 和 c++实现使用的 ipc 组件不一样,面向对象实现也不一致,所以在具体实现上存在一些差异。
hdi基础组件 uhdf 框架为了支持 hdi 实现,提供了以下基础组件(仅用于 c 语言 hdi 实现):
sbuf
sbuf 是同时支持 khdf 和 uhdf 驱动 ioservice 消息序列化的工具对象。在 uhdf ipc 通信场景中,sbuf 可以与系统 ipc 框架序列化对象 messageparcel 对象(仅支持 c++)相互转换,从而实现 c 和 c++实现的 ipc 互通。
常用 api 如下:
struct hdfsbuf;struct hdfsbufimpl;struct hdfremoteservice;
/** * @brief hdfsbuf类型定义。 * * @since 1.0 */enum hdfsbuftype { sbuf_raw = 0, /* 用于用户态内核态通信的sbuf类型 */ sbuf_ipc, /* 用于跨进程通信的sbuf类型 */ sbuf_ipc_hw, /* 用于扩展的预留类型 */ sbuf_type_max, /* sbuf类型最大值 */};
上述接口均有对应的写入接口,不再一一列举,可查阅官网api参考文档。
remoteservice
remoteservice 对象和系统 ipc 框架中的 iremoteobject 对象(仅支持 c++)对应并可以相互转换,表示一个 ipc 对象。相关 api 说明:
// 消息分发器,用于服务端响应调用或者在客户端发起调用struct hdfremotedispatcher { int (*dispatch)(struct hdfremoteservice *, int, struct hdfsbuf *, struct hdfsbuf *);};
// remoteservice 死亡回调对象struct hdfdeathrecipient { void (*onremotedied)(struct hdfdeathrecipient *, struct hdfremoteservice *);};
struct hdfremoteservice { struct hdfobject object_; struct hdfobject *target; struct hdfremotedispatcher *dispatcher; bool ishw;};// 以自定义的消息分发器实例化一个remoteservicestruct hdfremoteservice *hdfremoteserviceobtain( struct hdfobject *object, struct hdfremotedispatcher *dispatcher);
// 回收remoteservice对象void hdfremoteservicerecycle(struct hdfremoteservice *service);
// 添加remoteservice的死亡通知,如果对应remoteservice的进程异常退出,hdfdeathrecipient的回调接口将被调用void hdfremoteserviceadddeathrecipient(struct hdfremoteservice *service, struct hdfdeathrecipient *recipient);
基于 remoteservice 实现一个服务端的示例:
int sampleservicestubdispatch( struct hdfremoteservice* service, int code, struct hdfsbuf *data, struct hdfsbuf *reply){ // ipc 调用响应接口 int ret = hdf_failure; switch (code) { case sample_if_0: { // do something break; } default: { ret = hdf_err_invalid_param; } } return ret;}bool samplestubconstruct(){ // 构造消息分发器,实现消息处理回调 static struct hdfremotedispatcher dispatcher = { .dispatch = sampleservicestubdispatch};// 实例化remoteservice inst-》remote = hdfremoteserviceobtain((struct hdfobject *)inst, &dispatcher); if (inst-》remote == null) { hdf_loge(“device service manager failed to obtain remote service”); return false;}… …
直接基于 remoteservice 实现服务端只适用于需要实现匿名 ipc 服务的情况,基于 uhdf 发布 hdi 服务只需要实现 driver 绑定的 ioservice 即可。
remoteservice 客户端对象只能从 sbuf hdfsbufreadremoteservice 接口获取。
hdi实现
driver 为 hdi 服务的驱动入口实现
ioservice 为 hdi 服务的服务入口实现,ioservice 的 dispatch 方法中调用 servicestub 中的真正服务响应接口(onremoterequest)
servicestub 为服务端实现对象,主要处理与 ipc 相关的业务逻辑,在这里完成参数反序列化后调用真正的 service 实现接口,即 serviceimpl 接口
serviceimpl 为 hdi 接口的真正实现,这里不关注 ipc 过程,只实现函数接口。
驱动框架提供了实现的样例代码,可参考 gitee driver 代码仓。
hdi接口调用
hdi驱动框架hdi接口
hdi 服务管理功能由驱动框架 devicemanager 实现,所以驱动框架提供了 hdi 服务管理相关 hdi 接口。
c++实现:
namespace ohos {namespace hdi {namespace servicemanager {namespace v1_0 {
struct iservicemanager : public iremotebroker {public: declare_interface_descriptor(u“hdi.iservicemanager.v1_0”); // get()静态方法用于获取iservicemanager对象实例 static ::sptr《iservicemanager》 get(); // getservice()接口是真正提供的hdi接口,用于查询并获取其他hdi服务的客户端对象 virtual ::sptr《iremoteobject》 getservice(const char* servicename) = 0;};} // namespace v1_0} // namespace servicemanager} // namespace hdi} // namespace ohos
c 实现:
#ifdef __cplusplusextern “c” {#endif /* __cplusplus */
struct hdiservicemanager { struct hdfremoteservice *remote;
struct hdfremoteservice *(*getservice)(struct hdiservicemanager *self, const char* servicename);};
struct hdiservicemanager *hdiservicemanagerget(void);void hdiservicemanagerrelease(struct hdiservicemanager *servmgr);
#ifdef __cplusplus}#endif /* __cplusplus */
c 语言因为缺少原生的面向对象支持,这里我们采用 ooc 的实现,函数方法 hdiservicemanagerget/release 用于 hdiservicemanager 对象的实例化和释放,hdi 接口关联在接口对象内部成员中,与 c++实现类似。
hdi客户端实现
hdi 客户端同时支持 c 和 c++实现,实现方法较为简单,只需 realize hdi 接口类即可。提供 c++实现基于系统 ipc 子系统的统一模型,c 语言基于 remoteservice 和 sbuf 组件实现,但是有一些公共的约定:
1.客户端提供接口对象,接口与对象绑定且必须与 hdi 一致
2.提供服务接口对象的实例化和释放接口。
3.客户端实现 ipc 过程,只为调用者暴露函数化接口。
hdi接口调用
hdi 客户端接口已经提供了服务获取接口,调用者调用服务获取接口后再调用服务对象方法即可完成 hdi 调用。
这里以服务管理 hdi 接口为例:
c++接口调用:
#include 《iservmgr_hdi.h》
void gettestservice(){ auto servmgr = iservicemanager::get(); if (servmgr == nullptr) { hdf_loge(“failed to get iservicemanager”); return; }
auto sampleservice = servmgr-》getservice(test_service_name); if (sampleservice == nullptr) { hdf_loge(“failed to get test_service”); return; } // do something}
c 接口调用:
#include 《servmgr_hdi.h》
void gettestservice(){ struct hdiservicemanager *servmgr = hdiservicemanagerget(); if (servmgr == nullptr) { hdf_loge(“failed to get iservicemanager”); return; }
struct hdfremoteservice *sampleservice = servmgr-》getservice(servmgr, test_service_name); if (sampleservice == nullptr) { hdf_loge(“failed to get test_service”); return; } // do something}
总结
本文介绍了 hdi 的总体方案,重点介绍了 hdi 的 ipc 模式具体实现方法和驱动框架能力,相信对读者理解和使用 hdi 有所帮助。


MHL移动高清连接技术发送端静噪处理
三大点看懂如何选择激光位移传感器
用静电悬浮技术设计耐久的MEMS开关
品牌冲牙器推荐,这几款产品备受消费者喜爱
全球SiC和GaN功率半导体销售收入,预计到2029年将超过50亿美元
HDI接口中如何实现驱动入口
爱立信将通过化繁为简助力运营商开启5G商用之路
东方创科研发的天启主板开源电子创作套盒实现自主可控且国产化
如何利用近红外光谱技术提高流感疫苗生产效率?
IT8200系列回馈式交/直流电子负载的操作模式
DALI照明控制:面向未来绿色建筑的可持续解决方案
入驻宜春高铁站!“欢乐消2”助力人们安全出行
隔离开关和断路器区别
手机功率放大器设计中的降低VREF和VCC方案分析
数字视频技术的国际标准及视频压缩方式
西部数据蓝盘SN500固态硬盘评测 同类PCI-EX2NVMe产品中的佼佼者
航天科工203所研发出了远距离虹膜识别测温一体机
石墨烯的产业意义及应用领域
2019全球电动汽车发展
基于舌簧继电器和电信号实现激光光波控制系统的设计