• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 业务侧进行工具集成
2
3## 导入IDL工具gni
4
5  必须引用下面这个idl.gni
6
7```gn
8import("//build/config/components/idl_tool/idl.gni")
9```
10
11## idl_gen_interface内部配置选项
12
13
14|        变量名         | 作用                                                         | 使用示例                                                     |
15| :-------------------: | :----------------------------------------------------------- | :----------------------------------------------------------- |
16|        src_idl        | 【必选项,与sources二选一】字符串类型。业务定义的idl文件,推荐和BUILD.gn在同一个路径;支持相对路径;<br/>配置后external_deps需要增加"**c_utils:utils**"、"**ipc:ipc_core**"依赖。不支持生成so。 | src_idl = "IThermalSrv.idl"                                  |
17|        sources        | 【必选项,与src_idl二选一】列表类型。业务定义的idl文件,支持相对路径,支持多个idl文件。功能同src_idl,可用sources代替src_idl。支持生成so。 | sources = [<br/>  "IThermalSrv.idl",<br/>]                   |
18|   sources_callback    | 【可选项】列表类型。业务定义的回调idl文件,为接口idl文件的相对路径。<br />当使用相对路径时,需要在对应include_dir加上<br />include_dirs = [<br />        "${target_gen_dir}",<br />        "${target_gen_dir}/ + 相对路径"<br />] | sources_callback = [<br/> "cb/IThermalActionCallback.idl", <br/>"cb/IThermalLevelCallback.idl",<br/>"cb/IThermalTempCallback.idl", <br/>] |
19|    sources_common     | 【可选项】列表类型。业务定义的复杂类型的idl文件,为接口idl文件的相对路径。<br />当使用相对路径时,需要在对应include_dir加上<br />include_dirs = [<br />        "${target_gen_dir}",<br />        "${target_gen_dir}/ + 相对路径"<br />] | source_common = [<br/> "type/IThermalSensorInfo.idl",<br/>]  |
20|       dst_file        | 【已作废】配置生成的文件名称;目前配置后不生效,相关的文件名称可自动生成; |                                                              |
21|        hitrace        | 【可选项】开启hitrace,值是hitrace_meter.h文件中定义的uint64_t类型标识,需要填入常量的变量名;<br/>配置后external_deps需要增加“**hitrace:hitrace_meter**”依赖 | HITRACE_TAG_ABILITY_MANAGER                                  |
22|     log_domainid      | 【可选项】开启hilog,Domain ID 使用16进制的整数;<br/>配置后external_deps需要增加“**hilog:libhilog**”依赖;<br/>**注:需要和log_tag成对配置** | log_domainid = "0xD001718"                                   |
23|        log_tag        | 【可选项】开启hilog,字符串类型tag名、一般为子系统名称;<br/>配置后external_deps需要增加“**hilog:libhilog**”依赖;<br/>**注:需要和log_tag成对配置** | log_tag = "idlTest"                                          |
24|     client_enable     | 【可选项】开启死亡监听,生成死亡监听对应的文件。             | client_enbale = true                                         |
25|      source_cpp       | 根据idl文件中对自定义对象的使用,编译为so时需要增加自定义对应使用的cpp的编译,默认为空 |                                                              |
26|        configs        | 编译so时增加configs配置                                      |                                                              |
27| sequenceable_pub_deps | 编译so时增加public_deps配置                                  |                                                              |
28| sequenceable_ext_deps | 编译so时增加external_desp配置                                |                                                              |
29|     innerapi_tags     | 编译so时增加innerapi_tags                                    |                                                              |
30|       sanitize        | 编译so时增加sanitize,<br/><br/>so使用方式的默认值为:<br/>  sanitize = {<br/>  cfi = true<br/>  cfi_cross_dso = true<br/>  debug = false<br/>} |                                                              |
31|        cflags         | 编译so时增加cflags配置                                       |                                                              |
32|       cflags_cc       | 编译so时增加cflags_cc配置                                    |                                                              |
33|    subsystem_name     | 【必选项】编译so时增加subsystem_name,与业务保持一致         | subsystem_name = "powermgr"                                  |
34|       part_name       | 【必选项】编译so时增加part_name,与业务保持一致              | part_name = "thermal_manager"                                |
35
36示例:
37
38```cpp
39idl_gen_interface("thermalsrv_interface") {
40    sources = [
41        "IThermalSrc.idl",
42    ]
43    sources_callback = [
44        "cb/IThermalActionCallback.idl",
45        "cb/IThermalLevelCallback.idl",
46        "cb/IThermalTempCallback.idl",
47    ]
48    sources_common = [
49        "type/IThermalSensorInfo.idl",
50    ]
51    sub_include = [ "native/include" ]
52    sequenceable_ext_deps = [ "bundle_framework:appexecfwk_base" ]
53    sources_cpp = [rebase_path("${thermal_manager_path}/frameworks/native/thermal_srv_sensor_info.cpp") ]
54    configs = [
55        "${utils_path}:utils_config",
56        ":thermalsrc_private_config",
57        "${utils_path}:coverage_flags",
58    ]
59    log_domainid = "0xD003900"
60    log_tag = "QuickFixManagerService"
61    subsystem_name = "powermgr"
62    part_name = "thermal_manager"
63}
64```
65
66## include_dir配置
67
68| 变量名               | 作用                    |
69|:-----------------:| --------------------- |
70| ${target_gen_dir} | 【必选项】该目录包含idl工具生成的头文件 |
71| 业务方自己定义的头文件路径     | 业务方自己定义的头文件路径         |
72
73示例:
74
75```gn
76config("thermalsrv_public_config") {
77    include_dirs = [
78        "${target_gen_dir}",
79        "native/include",
80    ]
81}
82
83
84// 若想在其他build.gn中依赖上面的config可以定义一个ohos_source_set,其中public_configs,接着在其他build.gn中依赖下面这个ohos_source_set
85
86
87ohos_source_set("example") {
88    public_configs = [ ":thermalsrv_public_config" ]
89}
90```
91
92
93
94## so使用方式
95
96只需在业务的 deps 中增加对生成的 so 的依赖, IDL工具就会生成对应 so。
97
98- proxy so名字为:"lib" + target_name + "_proxy"
99
100- stub so 名字为:"lib" + target_name + "_stub"
101
102示例:
103
104```gn
105ohos_shared_library("thermalsrv_client") {
106    sanitize = {
107        cfi = true
108        cfi_cross_dso = true
109        debug = false
110    }
111    sources = [ "${thermal_frameworks}/native/thermal_mgr_client.cpp" ]
112    configs = [
113        "${utils_path}:utils_config",
114        ":thermalsrv_private_config",
115        "${utils_path}:coverage_flags",
116    ]
117    public_configs = [ ":thermalsrv_public_config" ]
118    deps = [
119        ":libthermalsrv_interface_proxy",
120    ]
121    external_deps = [
122        "bundle_framework:appexecfwk_base",
123        "c_utils:utils",
124        "hilog:libhilog",
125        "ipc:ipc_core",
126        "samgr:smagr_proxy",
127    ]
128    subsystem_name = "powermgr"
129    part_name = "thermal_manager"
130}
131```
132
133## 源码使用方式
134
135使用 get_target_outputs 获取IDL工具返回的生成文件路径,将其**过滤后**加入到 sources 中进行编译,同时需要在 deps 中增加对模板调用的依赖。
136
137下例将生成的 stub 代码生成一个 source_set,业务方可依赖此 source_set 生成 so。
138
139示例:
140
141```gn
142ohos_source_set("thermalsrv_stub") {
143    configs = [
144        "${utils_path}:utils_config",
145        ":thermalsrv_private_config",
146        "${utils_path}:coverage_flags",
147    ]
148    output_values = get_target_outputs(":thermalsrv_interface")
149    sources = []
150    sources += filterinclude(output_values, [ "*srv_stub.cpp" ] )
151    sources += filterinclude(output_values, [ "*sensor_info.cpp" ] )
152    sources += filterinclude(output_values, [ "*callback_proxy.cpp" ] )
153    public_configs = [ ":thermalsrv_public_config" ]
154    deps = [
155        ":thermalsrv_interface",
156    ]
157    external_deps = [
158        "bundle_framework:appexecfwk_base",
159        "c_utils:utils",
160        "hilog:libhilog",
161        "ipc:ipc_core",
162        "samgr:smagr_proxy",
163    ]
164    subsystem_name = "powermgr"
165    part_name = "thermal_manager"
166}
167```
168
169| 步骤  | 配置说明                                                     |
170| :---: | ------------------------------------------------------------ |
171| 第①步 | 业务client端编译目标,内部编译的sources由业务实现,deps依赖了proxy的编译目标; |
172| 第②步 | proxy编译目标,内部编译的sources由IDL工具根据.idl文件生成,deps依赖了idl_gen_interface配置的编译目标; |
173| 第③步 | 工具生成代码的action执行目标,目标名称由业务自定义,idl_gen_interface模板由idl.gni(//build/config/components/idl_tool/idl.gni)提供; |
174| 第④步 | 调用到工具生成代码的action执行目标,可配置相关变量传递到idl.gni模板中; |
175| 第⑤步 | 调用get_target_outputs获取生成的代码文件的完整路径,添加到proxy编译目标的sources中; |
176
177
178
179# 自测试native用例
180
181## 获取ondemand工具
182
183        编译目标:ondemand;
184
185        源码路径://foundation/systemabilitymgr/samgr/services/samgr/native/test/unittest/src/ondemand_helper.cpp
186
187        使用方法:
188
189```batch
190# 测试SA的SAID为1494
191
192# 加载 SA
193./ondemand sa load 1494
194
195# 卸载 SA
196./ondemand sa unload 1494
197```
198
199        推工具脚本:**推送后需要给二进制增加执行权限**
200
201```batch
202hdc target mount
203hdc file send "%cd%\ondemand" /data/ondemand
204hdc shell chmod 777 /data/ondemand
205pause
206```
207
208
209
210## SA的配置文件
211
212### listen_test.cfg文件,推送到设备的 /system/etc/init 目录
213
214```json
215{
216    "service" :[{
217            "name" : "listen_test",
218            "path" : ["/system/bin/sa_main", "/system/profile/listen_test.json"],
219            "ondemand" : true,
220            "secon" : "u:r:listen_test:s0",
221            "permission" : ["ohos.permission.DISTRIBUTED_DATASYNC"],
222            "uid" : "system",
223            "gid" : ["system", "shell"]
224        }
225    ]
226}
227```
228
229
230
231### listen_test.json文件,推送到设备的 /system/profile 目录
232
233```json
234{
235    "process" : "listen_test",
236    "systemability" :[
237        {
238            "name" : "1494",
239            "libpath" : "libidl_sa_test.z.so",
240            "run-o-create" : false,
241            "distributed" : false,
242            "dump-level" : 1
243        }
244    ]
245}
246```
247
248
249
250### 推送配置文件的脚本
251
252```batch
253hdc target mount
254hdc file send "%cd%\listen_test.cfg" /system/etc/init
255hdc file send "%cd%\listen_test.json" /system/profile
256pause
257```
258
259
260
261## 编译"测试SA"服务端so
262
263    注:先在 code/foundation/ability/idl_tool/test/unittest/BUILD.gn 中增加 "../native/idl_sa_test:unittest" 依赖
264
265    编译目标:idl_sa_test
266
267    源码路径://foundation/ability/idl_tool/test/unittest/idl_sa_test/listen_ability/BUILD.gn
268
269    产物路径://out/generic_generic_arm_64only/general_all_phone_standard/ability/idl_tool/libidl_sa_test.z.so
270
271    推 so 脚本:
272
273```batch
274hdc target mount
275hdc file send "%cd%\libidl_sa_test.z.so" /system/lib64/libidl_sa_test.z.so
276pause
277```
278
279
280
281## 编译测试用例
282
283    注:先在 code/foundation/ability/idl_tool/test/unittest/BUILD.gn 中增加 "../native/idl_sa_test:unittest" 依赖
284
285    编译目标:idl_sa_client_test
286
287    源码路径://foundation/ability/idl_tool/test/unittest/idl_sa_test/BUILD.gn
288
289    产物路径://out/generic_generic_arm_64only/general_all_phone_standard/tests/unittest/idl_sa_client_test
290
291    推测试用例脚本:**推送后需要给二进制增加执行权限**
292
293```batch
294hdc target mount
295hdc file send "%cd%\idl_sa_client_test" /data/idl_sa_client_test
296pause
297```
298
299
300
301## 执行步骤
302
303### 验证同步func
304
305```batch
306# 执行后抓日志验证功能调用
307./idl_sa_client_test --gtest_filter="IdlSaUnitTest.IdlSaProxyTest001"
308```
309
310### 验证异步func
311
312```batch
313# 执行后抓日志验证功能调用
314./idl_sa_client_test --gtest_filter="IdlSaUnitTest.IdlSaProxyTest002"
315```
316
317### 验证死亡回调
318
319```batch
320# 执行后抓日志验证功能调用
321./idl_sa_client_test --gtest_filter="IdlSaUnitTest.IdlSaRemoveDeathTest001"
322
323# 执行后,再在另一个命令窗口执行 SA卸载
324./data/ondemand sa unload 1494
325```
326
327### 同步加载SA
328
329```batch
330# 执行后抓日志验证功能调用
331./idl_sa_client_test --gtest_filter="IdlSaUnitTest.IdlSaLoadTest001"
332```
333
334### 异步加载SA
335
336```batch
337# 执行后抓日志验证功能调用
338./idl_sa_client_test --gtest_filter="IdlSaUnitTest.IdlSaLoadTest002"
339```
340