• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Service框架生成代码集成到OpenHarmony的方法
2
3## 场景说明
4
5为了实现工具生成的接口被其它子系统或者应用调用,需将生成的代码经系统框架开发者二次开发后编译集成到OpenHarmony系统中,使其生成动态库,供OpenHarmony应用层调用。本文介绍如何将工具生成的源码利用OpenHarmony编译系统生成动态库供应用层调用。
6
7## 编译
8
9将生成的整个testservice目录复制到OpenHarmony源码根目录下(与base、foundation目录平级)
10
11### OpenHarmony 3.1 release
12
13#### 修改系统公共文件
14
151. 服务配置
16   foundation/distributedschedule/samgr/interfaces/innerkits/samgr_proxy/include/system_ability_definition.h增加以下两行(ID说明: TEST_SERVICE_ID值与用户指定的ID一致;TEST_SERVICE_ID宏值定义必须为这个,因为代码中使用的就是这个)
17
18   ```
19   TEST_SERVICE_ID                                = 9016,
20   {TEST_SERVICE_ID, "testservice" },
21   ```
22
232. 子系统配置
24   build/subsystem_config.json
25   增加以下内容
26
27   ```
28   "testservice": {
29   "path":"testservice",
30   "name": "testservice"
31    }
32   ```
33
343. 产品配置,如Hi3516DV300
35   productdefine/common/products/Hi3516DV300.json
36
37```
38 "testservice:testservice_part":{}
39```
40
41### OpenHarmony 3.2 release
42
43#### 修改编译文件
44
451. 修改testservice/BUILD.gn文件,将utils/native 改为 commonlibrary/c_utils,将samgr_standard改为samgr。修改后的BUILD.gn文件内容如下所示:
46
47   ```
48   import("//build/ohos.gni")
49
50   ohos_shared_library("testservice") {
51     sources = [
52       "//testservice/src/i_test_service.cpp",
53       "//testservice/src/test_service_stub.cpp",
54       "//testservice/src/test_service.cpp"
55     ]
56     include_dirs = [
57       "//testservice/include",
58       "//testservice/interface",
59       "//commonlibrary/c_utils/base/include"
60     ]
61
62     deps = [
63       "//base/startup/syspara_lite/interfaces/innerkits/native/syspara:syspara",
64       "//commonlibrary/c_utils/base:utils",
65     ]
66
67     external_deps = [
68       "hiviewdfx_hilog_native:libhilog",
69       "ipc:ipc_core",
70       "safwk:system_ability_fwk",
71       "samgr:samgr_proxy",
72       "startup_l2:syspara",
73     ]
74
75     part_name = "testservice_part"
76     subsystem_name = "testservice"
77   }
78
79   ohos_executable("testclient") {
80       sources = [
81       "//testservice/src/i_test_service.cpp",
82       "//testservice/src/test_service_proxy.cpp",
83       "//testservice/src/test_client.cpp"
84     ]
85
86     include_dirs = [
87       "//testservice/include",
88       "//testservice/interface",
89       "//commonlibrary/c_utils/base/include"
90     ]
91
92     deps = [
93       "//commonlibrary/c_utils/base:utils",
94     ]
95
96     external_deps = [
97       "hiviewdfx_hilog_native:libhilog",
98       "ipc:ipc_core",
99       "samgr:samgr_proxy",
100     ]
101
102     part_name = "testservice_part"
103     subsystem_name = "testservice"
104   }
105
106   ```
107
1082. 修改testservice/bundle.json文件,将"name": "@ohos/testservice"修改为 "name": "@ohos/testservice_part";将"samgr_standard"改为"samgr","utils_base"修改为"c_utils";修改后的bundle.json文件内容如下所示:
109
110   ```
111   {
112       "name": "@ohos/testservice_part",
113       "description": "system ability framework test",
114       "homePage": "https://gitee.com/",
115       "version": "3.1",
116       "license": "Apache License 2.0",
117       "repository": "",
118       "publishAs": "code-segment",
119       "segment": {
120           "destPath": "testservice"
121       },
122       "dirs": {},
123       "scripts": {},
124       "component": {
125           "name": "testservice_part",
126           "subsystem": "testservice",
127           "adapted_system_type": [
128               "standard"
129           ],
130           "rom": "2048KB",
131           "ram": "~4096KB",
132           "deps": {
133               "components": [
134                   "hiviewdfx_hilog_native",
135                   "ipc",
136                   "samgr",
137                   "c_utils",
138                   "safwk",
139                   "startup_l2"
140               ],
141               "third_party": [ "libxml2" ]
142           },
143           "build": {
144               "sub_component": [
145                   "//testservice:testservice",
146                   "//testservice/sa_profile:testservice_sa_profile",
147                   "//testservice:testclient",
148                   "//testservice/etc:test_service_init"
149               ],
150               "inner_kits": [
151               ],
152               "test": [
153               ]
154           }
155       }
156   }
157   ```
158
159#### 修改系统公共文件
160
161##### 基础配置
162
1631. 服务配置
164
165   foundation/systemabilitymgr/samgr/interfaces/innerkits/samgr_proxy/include/system_ability_definition.h增加以下两行(ID说明: TEST_SERVICE_ID值与用户指定的ID一致;TEST_SERVICE_ID宏值定义必须为这个,因为代码中使用的就是这个)
166
167   ```
168   TEST_SERVICE_ID                                = 9016,
169   {TEST_SERVICE_ID, "testservice" },
170   ```
171
1722. 子系统配置
173
174   build/subsystem_config.json
175
176   增加以下内容
177
178   ```
179   "testservice": {
180   "path":"testservice",
181   "name": "testservice"
182    }
183   ```
184
1853. 产品配置,如rk3568
186
187   vendor/hihope/rk3568/config.json
188
189   若用户不需要配置selinux,则将"build_selinux"属性改为false
190
191   ```
192   "build_selinux": false,
193   ```
194
195   增加以下内容
196
197   ```
198   {
199     "subsystem": "testservice",
200     "components": [
201       {
202         "component": "testservice_part",
203         "features": []
204       }
205     ]
206   }
207   ```
208
209   注意:若用户需要配置selinux相关配置,则将开关改为true,再根据自身需求进行相关配置
210
2114. 权限配置
212
213   在相应产品目录下
214
215   vendor/hihope/rk3568/security_config/high_privilege_process_list.json
216
217   增加以下内容
218
219   ```
220   {
221       "name": "testservice",
222       "uid": "system",
223       "gid": ["root", "system"]
224   }
225   ```
226
227##### selinux权限配置
228
229若要配置selinux权限,首先应将vendor/hihope/rk3568/config.json中"build_selinux"属性改为true,然后修改以下文件:
230
2311. testservice/etc/sample_service.cfg
232
233   ```
234   "secon" : "u:r:testservice:s0"
235   ```
236
2372. base/security/selinux/sepolicy/base/public/service_contexts
238
239   ```
240   9016                 u:object_r:sa_testservice:s0
241   ```
242
2433. base/security/selinux/sepolicy/base/public/service.te
244
245   ```
246   type sa_testservice, sa_service_attr;
247   ```
248
2494. base/security/selinux/sepolicy/base/te/init.te
250
251   ```
252   allow init testservice:process { getattr rlimitinh siginh transition };
253   ```
254
2555. base/security/selinux/sepolicy/base/public/type.te
256
257   ```
258   type testservice, sadomain, domain;
259   ```
260
2616. /base/security/selinux/sepolicy/base/te目录下增加新service的te文件,新增文件名即为服务名,例如:testservice
262
263   ```
264   allow testservice init_param:file { map open read };
265   allow testservice sa_testservice:samgr_class { add get };
266   ```
267
268### 补充 服务端/客户端 业务逻辑实现
269
270**服务端**
271test_service.cpp
272testservice/src/test_service.cpp注释“// TODO: Invoke the business implementation”处添加各个接口的服务端实现代码,当前版本生成服务端代码需要用户先初始化,给int ret值赋初值0,如下所示:
273
274![](./../figures/service_init_example.png)
275
276远程方法的参数包装已在生成代码test_service_stub.cpp中统一处理,开发人员无需关注
277
278**客户端**
279test_client.cpp 为自动生成的客户端样例代码。编译烧录后,会在/system/bin/目录下生成可执行程序test_client
280testservice/src/test_client.cpp的main函数中使用proxy对象进行远程方法调用,参考注释示例。如下图:
281
282![](./../figures/service_client_proxy_example.png)
283
284远程方法的参数包装已在生成代码test_service_proxy.cpp中统一处理,开发人员无需关注
285
286编码完成后,执行镜像编译命令
287
288```
289./build.sh --product-name 产品名
290```
291
292如:若编译Hi3516DV300开发板,则执行
293
294```
295./build.sh --product-name Hi3516DV300
296```
297
298若编译rk3568开发板,则执行
299
300```
301./build.sh --product-name rk3568
302```
303
304## 运行
305
306将编译好的镜像烧录到开发板后,使用hdc_std shell登录开发板。
307查看服务端进程是否已正常启动
308
309```
310ps -ef | grep testservice
311system         288     1 0 00:02:13 ?     00:00:00 testservice_sa  --- 服务进程已正常运行
312```
313
314如下图所示:
315
316![](./../figures/service_init_success.png)
317
318运行客户端
319
320```
321/system/bin/testclient
322```
323
324
325 (客户端具体执行哪些远程调用方法请在test_client.cpp的main方法中实现)
326
327