积分64 / 贡献0

提问1答案被采纳0文章6

[经验分享] OpenHarmony系统之Service代码一键生成工具介绍(二)

深开鸿_苟晶晶 显示全部楼层 发表于 2024-6-20 11:45:39
## 前言

在上一篇博客我们简单介绍了service生成工具如何安装和使用工具生成代码,以及如何集成到OpenHarmony 3.2 release上;由于OpenHarmony 4.1 release发生了很大变化,本篇将介绍如何如何将service工具生成的代码集成到OpenHarmony 4.1 release上。

## 使用说明

如何安装并使用service生成工具,请参考上一篇文章:

[OpenHarmony系统之Service代码一键生成工具介绍](https://blog.51cto.com/harmonyos/6438032)

## 集成

将service生成工具生成的代码集成到OpenHarmony 4.1 release

### 修改编译文件

1. 修改testservice/BUILD.gn文件,将utils/native 改为 commonlibrary/c_utils,将samgr_standard改为samgr,− 将hiviewdfx_hilog_native改为hilog,在ohos_shared_library("testservice")中include_dirs内新增"//base/startup/init/interfaces/innerkits/include/syspara",将ohos_shared_library("testservice")中的deps删除,并删除external_deps中的"startup_l2:syspara",同时在external_deps中新增"c_utils:utils", 将ohos_executable("testclient")中的deps删除,同时在external_deps中新增"c_utils:utils"。修改后的BUILD.gn文件内容如下所示:

   ```
   import("//build/ohos.gni")

   ohos_shared_library("testservice") {
     sources = [
       "//testservice/src/i_test_service.cpp",
       "//testservice/src/test_service_stub.cpp",
       "//testservice/src/test_service.cpp"
     ]
     include_dirs = [
       "//testservice/include",
       "//testservice/interface",
       "//commonlibrary/c_utils/base/include",
       "//base/startup/init/interfaces/innerkits/include/syspara",
     ]

     external_deps = [
       "hilog:libhilog",
       "ipc:ipc_core",
       "safwk:system_ability_fwk",
       "samgr:samgr_proxy",
       "c_utils:utils",
     ]

     part_name = "testservice_part"
     subsystem_name = "testservice"
   }

   ohos_executable("testclient") {
       sources = [
       "//testservice/src/i_test_service.cpp",
       "//testservice/src/test_service_proxy.cpp",
       "//testservice/src/test_client.cpp"
     ]

     include_dirs = [
       "//testservice/include",
       "//testservice/interface",
       "//commonlibrary/c_utils/base/include",
     ]

     external_deps = [
       "hilog:libhilog",
       "ipc:ipc_core",
       "samgr:samgr_proxy",
       "c_utils:utils",
     ]

     part_name = "testservice_part"
     subsystem_name = "testservice"
   }
   ```
2. 修改testservice/bundle.json文件,将"name": "@ohos/testservice"修改为 "name": "@ohos/testservice_part";将"samgr_standard"改为"samgr","utils_base"修改为"c_utils";将"hiviewdfx_hilog_native"修改为"hilog";− 将"deps":"components"下的"starup_l2"修改为"syspara_lite"。修改后的bundle.json文件内容如下所示:

   ```
   {
       "name": "@ohos/testservice_part",
       "description": "system ability framework test",
       "homePage": "https://gitee.com/",
       "version": "4.1",
       "license": "Apache License 2.0",
       "repository": "",
       "publishAs": "code-segment",
       "segment": {
           "destPath": "testservice"
       },
       "dirs": {},
       "scripts": {},
       "component": {
           "name": "testservice_part",
           "subsystem": "testservice",
           "adapted_system_type": [
               "standard"
           ],
           "rom": "2048KB",
           "ram": "~4096KB",
           "deps": {
               "components": [
                   "hilog",
                   "ipc",
                   "samgr",
                   "c_utils",
                   "safwk",
                   "syspara_lite"
               ],
               "third_party": [ "libxml2" ]
           },
           "build": {
               "sub_component": [
                   "//testservice:testservice",
                   "//testservice/sa_profile:testservice_sa_profile",
                   "//testservice:testclient",
                   "//testservice/etc:test_service_init"
               ],
               "inner_kits": [
               ],
               "test": [
               ]
           }
       }
   }
   ```
3. 步骤 1 修改testservice/sa_profile下的文件以及testservice/etc/test_service.cfg文件, 将testservice/sa_profile/9016.xml文件重命名为9016.json,并将内容修改为json格式,修改后的9016.json文件如下所示:

   ```
   {
     "process":"testservice_sa",
         "systemability":[
             {
                 "name":9016,
                 "libpath":"libtestservice.z.so",
                 "run-on-create":false,
                 "auto-restart":true,
                 "distributed":false,
                 "dump-level":1
             }
         ]
   }
   ```

   修改testservice/sa_profile/BUILD.gn文件:将sources = [ "9016.xml" ]修改为sources = [ "9016.json" ]

   修改testservice/etc/test_service.cfg文件:将"path"内的testservice_sa.xml修改为testservice_sa.json

### 修改系统公共文件

#### 基础配置

1. 服务配置

   在foundation/systemabilitymgr/samgr/interfaces/innerkits/samgr_proxy/include/

   system_ability_definition.h增加以下一行:

   ```
   TEST_SERVICE_ID                 = 9016,
   ```

   在base/hiviewdfx/hidumper/frameworks/native/dump_utils.cpp文件内的saNameMap中新增以下一行:

   ```
   { TEST_SERVICE_ID, "testservice" },
   ```

   其中,TEST_SERVICE_ID宏值与用户定义的serviceID一致。
2. 子系统配置

   在build/subsystem_config.json中增加以下内容。

   ```
   "testservice": {
     "path":"testservice",
     "name": "testservice"
   }
   ```
3. 产品配置,如rk3568

   在vendor/kaihong/rk3568/config.json中增加以下内容:

   ```
   {  
    "subsystem": "testservice",
    "components": [
     {
      "component": "testservice_part",
      "features": []
     }
    ]
   }
   ```
4. 权限配置

   在相应的产品目录的vendor/kaihong/rk3568/security_config/high_privilege_process_list.json中增加以下内容:

   ```
   {
     "name": "testservice",
     "uid": "system",
     "gid": ["root", "system"]
   }
   ```

#### selinux权限配置

上述基础配置时关闭了selinux 权限配置,用户新增服务时需根据自身需求配置selinux 权限 。

若要配置selinux权限,首先应将vendor/hihope/rk3568/config.json中"build_selinux"属性改为true,然后修改以下文件:

1. testservice/etc/sample_service.cfg

   ```
   "secon" : "u:r:testservice:s0"
   ```
2. base/security/selinux_adapter/sepolicy/base/public/service_contexts

   ```
   9016                 u:object_r:sa_testservice:s0
   ```
3. base/security/selinux_adapter/sepolicy/base/public/service.te

   ```
   type sa_testservice, sa_service_attr;
   ```
4. base/security/selinux_adapter/sepolicy/ohos_policy/startup/init/system/init.te

   ```
   allow init testservice:process { getattr rlimitinh siginh transition };
   ```
5. base/security/selinux/sepolicy/base/public/type.te

   ```
   type testservice, sadomain, domain;
   ```
6. /base/security/selinux/sepolicy/base/te目录下增加新service的te文件,新增文件名即为服务名,例如:testservice.te

   ```
   allow testservice init_param:file { map open read };
   allow testservice sa_testservice:samgr_class { add get };
   ```

## 运行

用户根据需求增加服务端客户端代码,编译镜像并烧录到开发板,查看服务是否正常启动,并运行客户端查看运行结果。具体如何增加示例代码以及查看最终结果,可查看上一篇文章:

[OpenHarmony系统之Service代码一键生成工具介绍](https://blog.51cto.com/harmonyos/6438032)
无用

©著作权归作者所有,转载或内容合作请联系作者

您尚未登录,无法参与评论,登录后可以:
参与开源共建问题交流
认同或收藏高质量问答
获取积分成为开源共建先驱

Copyright   ©2023  OpenHarmony开发者论坛  京ICP备2020036654号-3 |技术支持 Discuz!

返回顶部