自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

OpenHarmony系統(tǒng)之Service代碼一鍵生成工具介紹

系統(tǒng) OpenHarmony
Service代碼生成工具能夠幫助使用者生成框架代碼,提升開(kāi)發(fā)效率。用戶(hù)只需提供一個(gè)定義遠(yuǎn)程方法的.h頭文件,工具會(huì)自動(dòng)生成整個(gè)Service框架的代碼,包含Ability注冊(cè)、proxy/stub類(lèi)實(shí)現(xiàn)、MessageParcel數(shù)據(jù)包構(gòu)造、Service子系統(tǒng)編譯及開(kāi)機(jī)自啟動(dòng)相關(guān)配置文件。

想了解更多關(guān)于開(kāi)源的內(nèi)容,請(qǐng)?jiān)L問(wèn):

51CTO 開(kāi)源基礎(chǔ)軟件社區(qū)

https://ost.51cto.com

前言

當(dāng)開(kāi)發(fā)者為OpenHarmony系統(tǒng)框架開(kāi)發(fā)某些功能時(shí),有時(shí)需要將這個(gè)功能包裝成一個(gè)獨(dú)立的服務(wù)進(jìn)程運(yùn)行在系統(tǒng)中,為了其它應(yīng)用進(jìn)程能夠調(diào)用此服務(wù),開(kāi)發(fā)人員需要基于系統(tǒng)IPC通信框架編寫(xiě)一套遠(yuǎn)程接口調(diào)用實(shí)現(xiàn)。實(shí)現(xiàn)Service遠(yuǎn)程調(diào)用接口需要開(kāi)發(fā)人員熟悉IPC通信框架,了解proxy/stub的繼承與實(shí)現(xiàn)方式,掌握C++類(lèi)型轉(zhuǎn)為MessageParcel數(shù)據(jù)包的各種API方法,有一定的學(xué)習(xí)成本。而Service代碼生成工具能夠幫助使用者生成框架代碼,提升開(kāi)發(fā)效率。用戶(hù)只需提供一個(gè)定義遠(yuǎn)程方法的.h頭文件,工具會(huì)自動(dòng)生成整個(gè)Service框架的代碼,包含Ability注冊(cè)、proxy/stub類(lèi)實(shí)現(xiàn)、MessageParcel數(shù)據(jù)包構(gòu)造、Service子系統(tǒng)編譯及開(kāi)機(jī)自啟動(dòng)相關(guān)配置文件。

1、工具原理

Service框架代碼生成工具包含工具入口、工具框架、公共模塊、運(yùn)行環(huán)境、系統(tǒng)平臺(tái)。其中,工具入口描述調(diào)用Service框架代碼生成工具的入口方式,支持命令行調(diào)用、VS Code插件(即VS插件)調(diào)用,從而可以根據(jù)開(kāi)發(fā)環(huán)境的不同,采用相對(duì)應(yīng)的調(diào)用Service框架代碼生成工具的入口方式,實(shí)現(xiàn)Service框架代碼生成工具的入口多樣性,便于調(diào)用Service框架代碼生成工具。工具框架包含C語(yǔ)法解析器、代碼生成器兩部分,C語(yǔ)法解析器支持包括但不限于對(duì)class、function、properties、parameter等內(nèi)容的解析,代碼生成器支持包括但不限于對(duì)proxy、stub、service、interface等服務(wù)框架代碼的生成。公共模塊描述通用的、在不同部分均會(huì)使用的公共接口與模塊,可以包括通用的正則校驗(yàn)、類(lèi)型映射、代碼模板、文件操作等模塊,運(yùn)行環(huán)境描述Service框架代碼生成工具運(yùn)行的環(huán)境,包括Nodejs與Python,由于Nodejs本身具有跨平臺(tái)性特點(diǎn),故Service框架代碼生成工具可以在Windows、linux、mac、OpenHarmony等不同系統(tǒng)平臺(tái)靈活使用,Service框架代碼生成工具的運(yùn)行環(huán)境另一部分是python,針對(duì)不同平臺(tái)做python適配,Service框架代碼生成工具即可實(shí)現(xiàn)跨平臺(tái)使用。

架構(gòu)圖

OpenHarmony系統(tǒng)之Service代碼一鍵生成工具介紹-開(kāi)源基礎(chǔ)軟件社區(qū)

2、使用說(shuō)明

環(huán)境

visual studio code 版本需1.62.0及以上。

步驟

1、 打開(kāi)VS Code,在左側(cè)邊欄中選擇插件安裝。

OpenHarmony系統(tǒng)之Service代碼一鍵生成工具介紹-開(kāi)源基礎(chǔ)軟件社區(qū)

2、 在應(yīng)用商店搜索service-gen插件,再單擊安裝。

OpenHarmony系統(tǒng)之Service代碼一鍵生成工具介紹-開(kāi)源基礎(chǔ)軟件社區(qū)

3、 安裝完成后就會(huì)在VS Code的插件管理器中能看到service-gen這個(gè)插件了。

OpenHarmony系統(tǒng)之Service代碼一鍵生成工具介紹-開(kāi)源基礎(chǔ)軟件社區(qū)

4、 在VS Code中找到需要轉(zhuǎn)換的.h文件,待轉(zhuǎn)換的.h文件內(nèi)容如下所示:

#ifndef TEST_H
#define TEST_H

namespace OHOS {
    namespace Example {
    /**
     * @brief service服務(wù),提供IPC調(diào)用接口
     * @ServiceClass
     */
        class test {
        public:
            int testFunc(int v1, int v2, bool v3);
        };
    }  // namespace Example
}  // namespace OHOS
#endif  // TEST_H

5、 右鍵單擊.h文件,選擇“ Service Generate Frame”選項(xiàng)。

OpenHarmony系統(tǒng)之Service代碼一鍵生成工具介紹-開(kāi)源基礎(chǔ)軟件社區(qū)

6、 工具打開(kāi) Service Generate Frame窗口,.h文件選擇框默認(rèn)填寫(xiě)被操作的.h文件的絕對(duì)路徑;輸出路徑選擇框默認(rèn)填寫(xiě).h文件所在文件夾路徑,可修改為任意路徑;serviceID范圍是1-16777215之間的整數(shù),超出范圍會(huì)提示錯(cuò)誤,填入正確的serviceID,然后點(diǎn)擊ok。

OpenHarmony系統(tǒng)之Service代碼一鍵生成工具介紹-開(kāi)源基礎(chǔ)軟件社區(qū)

7、 轉(zhuǎn)換成功后,在輸出路徑下生成service框架代碼文件。

輸出文件說(shuō)明

service工具生成文件說(shuō)明如下圖所示:

OpenHarmony系統(tǒng)之Service代碼一鍵生成工具介紹-開(kāi)源基礎(chǔ)軟件社區(qū)

其中消息調(diào)用流程為:

  1. 服務(wù)端實(shí)現(xiàn)SystemAbility接口OnStart(),將自己的serviceId注冊(cè)到SystemAbility Manager管理類(lèi)。
  2. 客戶(hù)端根據(jù)serviceId向SystemAbility Manager管理類(lèi)獲取該service的proxy對(duì)象。
  3. 客戶(hù)端使用proxy對(duì)象調(diào)用服務(wù)端的遠(yuǎn)程接口。
  4. proxy將客戶(hù)端傳入的c++參數(shù)打包成消息數(shù)據(jù),通過(guò)系統(tǒng)提供的dbinder進(jìn)程間通信能力發(fā)送到服務(wù)端進(jìn)程。
  5. 服務(wù)端OnRemoteRequest()接收到遠(yuǎn)程調(diào)用消息,根據(jù)消息id分發(fā)給不同的innerFunction()處理。
  6. 服務(wù)端innerFunction()將遠(yuǎn)程消息數(shù)據(jù)包還原成C/C++參數(shù),傳入業(yè)務(wù)入口方法供業(yè)務(wù)開(kāi)發(fā)人員處理。

3.集成說(shuō)明

本集成說(shuō)明針對(duì)的是OpenHarmony 3.2release系統(tǒng),其他系統(tǒng)可能存在差別,開(kāi)發(fā)者可自行調(diào)試修改。

修改編譯文件

  1. 修改testservice/BUILD.gn文件,將utils/native 改為 commonlibrary/c_utils,將samgr_standard改為samgr。修改后的BUILD.gn文件內(nèi)容如下所示:
import("http://build/ohos.gni")

ohos_shared_library("testservice") {
  sources = [
    "http://testservice/src/i_test_service.cpp",
    "http://testservice/src/test_service_stub.cpp",
    "http://testservice/src/test_service.cpp"
  ]
  include_dirs = [
    "http://testservice/include",
    "http://testservice/interface",
    "http://commonlibrary/c_utils/base/include"
  ]

  deps = [
    "http://base/startup/syspara_lite/interfaces/innerkits/native/syspara:syspara",
    "http://commonlibrary/c_utils/base:utils",
  ]

  external_deps = [
    "hiviewdfx_hilog_native:libhilog",
    "ipc:ipc_core",
    "safwk:system_ability_fwk",
    "samgr:samgr_proxy",
    "startup_l2:syspara",
  ]

  part_name = "testservice_part"
  subsystem_name = "testservice"
}

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

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

  deps = [
    "http://commonlibrary/c_utils/base:utils",
  ]

  external_deps = [
    "hiviewdfx_hilog_native:libhilog",
    "ipc:ipc_core",
    "samgr:samgr_proxy",
  ]

  part_name = "testservice_part"
  subsystem_name = "testservice"
}
  1. 修改testservice/bundle.json文件,將"name": “@ohos/testservice"修改為 “name”: “@ohos/testservice_part”;將"samgr_standard"改為"samgr”,“utils_base"修改為"c_utils”;修改的bundle.json文件內(nèi)容如下所示:
{
    "name": "@ohos/testservice_part",
    "description": "system ability framework test",
    "homePage": "https://gitee.com/",
    "version": "3.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": [
                "hiviewdfx_hilog_native",
                "ipc",
                "samgr",
                "c_utils",
                "safwk",
                "startup_l2"
            ],
            "third_party": [ "libxml2" ]
        },
        "build": {
            "sub_component": [
                "http://testservice:testservice",
                "http://testservice/sa_profile:testservice_sa_profile",
                "http://testservice:testclient",
                "http://testservice/etc:test_service_init"
            ],
            "inner_kits": [
            ],
            "test": [
            ]
        }
    }
}

修改系統(tǒng)公共文件

基本配置

  1. 服務(wù)配置foundation/systemabilitymgr/samgr/interfaces/innerkits/samgr_proxy/include/system_ability_definition.h增加以下兩行(ID說(shuō)明: TEST_SERVICE_ID值與用戶(hù)指定的ID一致;TEST_SERVICE_ID宏值定義必須為這個(gè),因?yàn)榇a中使用的就是這個(gè))。
TEST_SERVICE_ID                                = 9016,
{TEST_SERVICE_ID, "testservice" },
  1. 子系統(tǒng)配置build/subsystem_config.json增加以下內(nèi)容:
"testservice": {
"path":"testservice",
"name": "testservice"
 }
  1. 產(chǎn)品配置,如rk3568vendor/hihope/rk3568/config.json增加以下內(nèi)容:
{
  "subsystem": "testservice",
  "components": [
    {
      "component": "testservice_part",
      "features": []
    }
  ]
}

注意:若用戶(hù)需要配置selinux相關(guān)配置,則將開(kāi)關(guān)改為true,再根據(jù)自身需求進(jìn)行相關(guān)配置

  1. 權(quán)限配置
    在相應(yīng)產(chǎn)品目錄下vendor/hihope/rk3568/security_config/high_privilege_process_list.json增加以下內(nèi)容:
{
    "name": "testservice",
    "uid": "system",
    "gid": ["root", "system"]
}

selinux安全配置

testservice/etc/sample_service.cfg。

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

base/security/selinux/sepolicy/base/public/service_contexts。

9016                 u:object_r:sa_testservice:s0

base/security/selinux/sepolicy/base/public/service.te。

type sa_testservice, sa_service_attr;

base/security/selinux/sepolicy/base/te/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文件,新增文件名即為服務(wù)名,例如:testservice。

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

4、示例演示

服務(wù)端修改

test_service.cpp在testservice/src/test_service.cpp注釋“// TODO: Invoke the business implementation”處添加各個(gè)接口的服務(wù)端實(shí)現(xiàn)代碼。本例實(shí)現(xiàn)一個(gè)簡(jiǎn)單的加減法,服務(wù)端代碼如下所示:

int testService::testFunc(int v1, int v2, bool v3)
{
    // TODO: Invoke the business implementation
    int ret = 0;
    printf("service test begin \r\n");
    if (v3) {
        printf("service test v3 = true\r\n");
        ret = v1 + v2;
    } else {
        printf("service test v3 = false \r\n");
        ret = v1 - v2;
    }
    printf("service test end \r\n");
    return ret;
}

遠(yuǎn)程方法的參數(shù)包裝已在生成代碼test_service_stub.cpp中統(tǒng)一處理,開(kāi)發(fā)人員無(wú)需關(guān)注。

客戶(hù)端修改

test_client.cpp 為自動(dòng)生成的客戶(hù)端樣例代碼。編譯燒錄后,會(huì)在/system/bin/目錄下生成可執(zhí)行程序test_client
在testservice/src/test_client.cpp的main函數(shù)中使用proxy對(duì)象進(jìn)行遠(yuǎn)程方法調(diào)用,參考注釋示例。本例實(shí)現(xiàn)一個(gè)簡(jiǎn)單的加減法,客戶(hù)端代碼如下所示:

int main(int argc, char *argv[])
{
    printf("---functest begin---\r\n");
    auto proxy = getRemoteProxy();
    uint32_t result = 0;
    // TODO: Invoke remote method by proxy
    result = proxy->testFunc(8, 5, false);
    printf("result is : %u\r\n", result);
    printf("---functest end---\r\n");

    IPCSkeleton::JoinWorkThread();
    return 0;
}

遠(yuǎn)程方法的參數(shù)包裝已在生成代碼test_service_proxy.cpp中統(tǒng)一處理,開(kāi)發(fā)人員無(wú)需關(guān)注。

編碼完成后,執(zhí)行鏡像編譯命令。

./build.sh --product-name 產(chǎn)品名

若編譯rk3568開(kāi)發(fā)板,則執(zhí)行。

./build.sh --product-name rk3568

運(yùn)行

將編譯好的鏡像燒錄到開(kāi)發(fā)板后,使用hdc_std shell登錄開(kāi)發(fā)板。
查看服務(wù)端進(jìn)程是否已正常啟動(dòng)。

ps -ef | grep testservice
system         682     1 0 08:00:08 ?     00:00:00 testservice_sa  --- 服務(wù)進(jìn)程已正常運(yùn)行

如下圖所示:

OpenHarmony系統(tǒng)之Service代碼一鍵生成工具介紹-開(kāi)源基礎(chǔ)軟件社區(qū)

運(yùn)行客戶(hù)端。

/system/bin/testclient

運(yùn)行結(jié)果如下所示:

---functest begin---
result is : 3
---functest end---

(客戶(hù)端具體執(zhí)行哪些遠(yuǎn)程調(diào)用方法請(qǐng)?jiān)趖est_client.cpp的main方法中實(shí)現(xiàn))。

總結(jié)

service生成工具是一個(gè)開(kāi)源項(xiàng)目,我們歡迎有興趣的開(kāi)發(fā)者試用該工具,并提出寶貴的改進(jìn)意見(jiàn),我們將繼續(xù)不斷優(yōu)化和完善該工具軟件。我們相信,該工具會(huì)成為OpenHarmony生態(tài)圈中一個(gè)有用的補(bǔ)充。

想了解更多關(guān)于開(kāi)源的內(nèi)容,請(qǐng)?jiān)L問(wèn):

51CTO 開(kāi)源基礎(chǔ)軟件社區(qū)

https://ost.51cto.com

責(zé)任編輯:jianghua 來(lái)源: 51CTO 開(kāi)源基礎(chǔ)軟件社區(qū)
相關(guān)推薦

2024-06-21 10:27:21

Service生成工具鴻蒙

2024-06-21 10:07:06

鴻蒙service生成工具

2024-01-03 15:41:49

2020-10-10 14:57:16

代碼開(kāi)發(fā)工具

2020-06-30 15:10:58

命令行應(yīng)用開(kāi)發(fā)工具

2024-03-26 15:25:55

應(yīng)用接口文件轉(zhuǎn)換工具鴻蒙

2021-11-30 08:09:52

PostgreSQLPGTune工具數(shù)據(jù)庫(kù)

2019-10-11 11:00:53

Nginx神器前端

2022-08-02 14:27:01

HDF驅(qū)動(dòng)框架驅(qū)動(dòng)開(kāi)發(fā)

2024-03-15 14:34:12

Oracle數(shù)據(jù)庫(kù)一鍵巡檢

2015-02-09 15:25:52

換膚

2020-05-11 09:40:47

IDEA代碼神器

2023-12-19 15:45:07

Linux工具

2025-03-03 03:35:00

DeepSeekXmind思維導(dǎo)圖

2023-03-05 23:11:07

Go語(yǔ)言服務(wù)

2020-12-24 10:20:43

文檔工具語(yǔ)言

2022-01-09 23:09:08

Windows 10Windows微軟

2011-09-15 19:05:49

windows 7一鍵關(guān)機(jī)

2023-03-31 09:30:21

ChatGPT論文

2023-06-20 15:45:15

服務(wù)卡片鴻蒙
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)