OpenHarmony系统之Service代码一键生成工具介绍(二)

系统 OpenHarmony
本篇将介绍如何如何将service工具生成的代码集成到OpenHarmony 4.1 release上。

想了解更多关于开源的内容,请访问:

51CTO 鸿蒙开发者社区

https://ost.51cto.com

前言

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

使用说明

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

OpenHarmony系统之Service代码一键生成工具介绍

集成

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

修改编译文件

修改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"
}

修改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": [
            ]
        }
    }
}

步骤 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。

修改系统公共文件

基础配置

服务配置:
在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一致。

子系统配置:
在build/subsystem_config.json中增加以下内容。

"testservice": {
  "path":"testservice",
  "name": "testservice" 
}

产品配置,如rk3568:
在vendor/kaihong/rk3568/config.json中增加以下内容:

{  
 "subsystem": "testservice", 
 "components": [
  {
   "component": "testservice_part",
   "features": []
  }
 ]
}

权限配置:
在相应的产品目录的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,然后修改以下文件:

testservice/etc/sample_service.cfg

"secon" : "u:r:testservice:s0"

base/security/selinux_adapter/sepolicy/base/public/service_contexts

9016                 u:object_r:sa_testservice:s0

base/security/selinux_adapter/sepolicy/base/public/service.te

type sa_testservice, sa_service_attr;

base/security/selinux_adapter/sepolicy/ohos_policy/startup/init/system/init.te

allow init testservice:process { getattr rlimitinh siginh transition };

base/security/selinux/sepolicy/base/public/type.te

type testservice, sadomain, domain;

/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代码一键生成工具介绍

想了解更多关于开源的内容,请访问:

51CTO 鸿蒙开发者社区

https://ost.51cto.com

责任编辑:jianghua 来源: 51CTO 鸿蒙开发者社区
相关推荐

2023-06-01 15:30:30

Service代码生成工具

2024-06-21 10:27:21

Service生成工具鸿蒙

2024-01-03 15:41:49

2020-10-10 14:57:16

代码开发工具

2020-06-30 15:10:58

命令行应用开发工具

2024-03-26 15:25:55

应用接口文件转换工具鸿蒙

2021-11-30 08:09:52

PostgreSQLPGTune工具数据库

2022-08-02 14:27:01

HDF驱动框架驱动开发

2024-03-15 14:34:12

Oracle数据库一键巡检

2019-10-11 11:00:53

Nginx神器前端

2015-02-09 15:25:52

换肤

2023-12-19 15:45:07

Linux工具

2020-05-11 09:40:47

IDEA代码神器

2023-03-05 23:11:07

Go语言服务

2022-01-09 23:09:08

Windows 10Windows微软

2023-03-31 09:30:21

ChatGPT论文

2020-12-24 10:20:43

文档工具语言

2011-09-15 19:05:49

windows 7一键关机

2024-04-11 10:02:54

蚂蚁集团CodeFuse

2011-09-29 15:23:18

点赞
收藏

51CTO技术栈公众号