1.IDL
安卓上我们熟知的aidl能够帮我们快速生成binder的代码,OpenHarmony 上也提供了此功能,用法与aidl相似.与安卓不同的地方以及和OpenHarmony 3.1不同的是:
-
不再需要在代码中分别引入proxy和sub,只需要在deps中是加入不同的包
-
idl文件也与服务端解耦,需要单独编写bundle.json.
1. IDL 声明和引入.
1.IDL的bundle.json: drivers\interface\location\gnss\bundle.json
"inner_kits": [
{
"name": "//drivers/interface/location/gnss/v1_0:liblocation_gnss_proxy_1.0",
"header": {
"header_files": [
],
"header_base": "//drivers/interface/location/gnss"
}
},
{
"name": "//drivers/interface/location/gnss/v1_0:location_gnss_idl_headers",
"header": {
"header_files": [
],
"header_base": "//drivers/interface/location/gnss"
}
}
]
2.proxy端引入:base\location\services\location_gnss\gnss\BUILD.gn
external_deps = [
...
"drivers_interface_location_agnss:liblocation_agnss_proxy_1.0",
"drivers_interface_location_gnss:liblocation_gnss_proxy_1.0",
...
]
3.service端引入:drivers\peripheral\location\gnss\hdi_service\BUILD.gn
external_deps = [
"drivers_interface_location_agnss:location_gnss_idl_headers",
...
]
2.IDL 文件编写
先看readme.md,可以简单了解一下idl文件规则 OpenHarmony IDL工具规格及使用说明书.目前OpenHarmony文档内容有点少,但是hdf中已经在使用,可能还没给上层开放.这里用的是HarmonyOS的介绍:
IDL文件编写规范
一个 HarmonyOS IDL 文件只能定义一个接口类,接口定义的形式使用 BNF 范式描述,示例如下:
<*interface_attributes_declaration*> ? interface <*interface_name_with_namespace*> { <*method_declaration*> ? }
<interface_attributes_declaration>是接口属性声明,当前支持 oneway、callback、full、lite 属性:(OpenHarmony的文档中只有oneway)
- oneway:表示该接口中的方法都是单向方法,即调用方法后不用等待该方法执行即可返回,该属性为可选项,如果无声明,则默认为同步调用方法
- callback:表示该接口为callback接口,即从下层实现中回调到上层服务中;
- full、lite: