OpenHarmony系統(tǒng)之Service代碼一鍵生成工具介紹
前言
當(dāng)開發(fā)者為OpenHarmony系統(tǒng)框架開發(fā)某些功能時,有時需要將這個功能包裝成一個獨立的服務(wù)進程運行在系統(tǒng)中,為了其它應(yīng)用進程能夠調(diào)用此服務(wù),開發(fā)人員需要基于系統(tǒng)IPC通信框架編寫一套遠程接口調(diào)用實現(xiàn)。實現(xiàn)Service遠程調(diào)用接口需要開發(fā)人員熟悉IPC通信框架,了解proxy/stub的繼承與實現(xiàn)方式,掌握C++類型轉(zhuǎn)為MessageParcel數(shù)據(jù)包的各種API方法,有一定的學(xué)習(xí)成本。而Service代碼生成工具能夠幫助使用者生成框架代碼,提升開發(fā)效率。用戶只需提供一個定義遠程方法的.h頭文件,工具會自動生成整個Service框架的代碼,包含Ability注冊、proxy/stub類實現(xiàn)、MessageParcel數(shù)據(jù)包構(gòu)造、Service子系統(tǒng)編譯及開機自啟動相關(guān)配置文件。
1、工具原理
Service框架代碼生成工具包含工具入口、工具框架、公共模塊、運行環(huán)境、系統(tǒng)平臺。其中,工具入口描述調(diào)用Service框架代碼生成工具的入口方式,支持命令行調(diào)用、VS Code插件(即VS插件)調(diào)用,從而可以根據(jù)開發(fā)環(huán)境的不同,采用相對應(yīng)的調(diào)用Service框架代碼生成工具的入口方式,實現(xiàn)Service框架代碼生成工具的入口多樣性,便于調(diào)用Service框架代碼生成工具。工具框架包含C語法解析器、代碼生成器兩部分,C語法解析器支持包括但不限于對class、function、properties、parameter等內(nèi)容的解析,代碼生成器支持包括但不限于對proxy、stub、service、interface等服務(wù)框架代碼的生成。公共模塊描述通用的、在不同部分均會使用的公共接口與模塊,可以包括通用的正則校驗、類型映射、代碼模板、文件操作等模塊,運行環(huán)境描述Service框架代碼生成工具運行的環(huán)境,包括Nodejs與Python,由于Nodejs本身具有跨平臺性特點,故Service框架代碼生成工具可以在Windows、linux、mac、OpenHarmony等不同系統(tǒng)平臺靈活使用,Service框架代碼生成工具的運行環(huán)境另一部分是python,針對不同平臺做python適配,Service框架代碼生成工具即可實現(xiàn)跨平臺使用。
架構(gòu)圖
2、使用說明
環(huán)境
visual studio code 版本需1.62.0及以上。
步驟
1、 打開VS Code,在左側(cè)邊欄中選擇插件安裝。
2、 在應(yīng)用商店搜索service-gen插件,再單擊安裝。
3、 安裝完成后就會在VS Code的插件管理器中能看到service-gen這個插件了。
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”選項。
6、 工具打開 Service Generate Frame窗口,.h文件選擇框默認填寫被操作的.h文件的絕對路徑;輸出路徑選擇框默認填寫.h文件所在文件夾路徑,可修改為任意路徑;serviceID范圍是1-16777215之間的整數(shù),超出范圍會提示錯誤,填入正確的serviceID,然后點擊ok。
7、 轉(zhuǎn)換成功后,在輸出路徑下生成service框架代碼文件。
輸出文件說明
service工具生成文件說明如下圖所示:
其中消息調(diào)用流程為:
- 服務(wù)端實現(xiàn)SystemAbility接口OnStart(),將自己的serviceId注冊到SystemAbility Manager管理類。
- 客戶端根據(jù)serviceId向SystemAbility Manager管理類獲取該service的proxy對象。
- 客戶端使用proxy對象調(diào)用服務(wù)端的遠程接口。
- proxy將客戶端傳入的c++參數(shù)打包成消息數(shù)據(jù),通過系統(tǒng)提供的dbinder進程間通信能力發(fā)送到服務(wù)端進程。
- 服務(wù)端OnRemoteRequest()接收到遠程調(diào)用消息,根據(jù)消息id分發(fā)給不同的innerFunction()處理。
- 服務(wù)端innerFunction()將遠程消息數(shù)據(jù)包還原成C/C++參數(shù),傳入業(yè)務(wù)入口方法供業(yè)務(wù)開發(fā)人員處理。
3.集成說明
本集成說明針對的是OpenHarmony 3.2release系統(tǒng),其他系統(tǒng)可能存在差別,開發(fā)者可自行調(diào)試修改。
修改編譯文件
- 修改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"
}
- 修改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)公共文件
基本配置
- 服務(wù)配置foundation/systemabilitymgr/samgr/interfaces/innerkits/samgr_proxy/include/system_ability_definition.h增加以下兩行(ID說明: TEST_SERVICE_ID值與用戶指定的ID一致;TEST_SERVICE_ID宏值定義必須為這個,因為代碼中使用的就是這個)。
TEST_SERVICE_ID = 9016,
{TEST_SERVICE_ID, "testservice" },
- 子系統(tǒng)配置build/subsystem_config.json增加以下內(nèi)容:
"testservice": {
"path":"testservice",
"name": "testservice"
}
- 產(chǎn)品配置,如rk3568vendor/hihope/rk3568/config.json增加以下內(nèi)容:
{
"subsystem": "testservice",
"components": [
{
"component": "testservice_part",
"features": []
}
]
}
注意:若用戶需要配置selinux相關(guān)配置,則將開關(guān)改為true,再根據(jù)自身需求進行相關(guān)配置
- 權(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”處添加各個接口的服務(wù)端實現(xiàn)代碼。本例實現(xià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;
}
遠程方法的參數(shù)包裝已在生成代碼test_service_stub.cpp中統(tǒng)一處理,開發(fā)人員無需關(guān)注。
客戶端修改
test_client.cpp 為自動生成的客戶端樣例代碼。編譯燒錄后,會在/system/bin/目錄下生成可執(zhí)行程序test_client
在testservice/src/test_client.cpp的main函數(shù)中使用proxy對象進行遠程方法調(diào)用,參考注釋示例。本例實現(xiàn)一個簡單的加減法,客戶端代碼如下所示:
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;
}
遠程方法的參數(shù)包裝已在生成代碼test_service_proxy.cpp中統(tǒng)一處理,開發(fā)人員無需關(guān)注。
編碼完成后,執(zhí)行鏡像編譯命令。
./build.sh --product-name 產(chǎn)品名
若編譯rk3568開發(fā)板,則執(zhí)行。
./build.sh --product-name rk3568
運行
將編譯好的鏡像燒錄到開發(fā)板后,使用hdc_std shell登錄開發(fā)板。
查看服務(wù)端進程是否已正常啟動。
ps -ef | grep testservice
system 682 1 0 08:00:08 ? 00:00:00 testservice_sa --- 服務(wù)進程已正常運行
如下圖所示:
運行客戶端。
/system/bin/testclient
運行結(jié)果如下所示:
---functest begin---
result is : 3
---functest end---
(客戶端具體執(zhí)行哪些遠程調(diào)用方法請在test_client.cpp的main方法中實現(xiàn))。
總結(jié)
service生成工具是一個開源項目,我們歡迎有興趣的開發(fā)者試用該工具,并提出寶貴的改進意見,我們將繼續(xù)不斷優(yōu)化和完善該工具軟件。我們相信,該工具會成為OpenHarmony生態(tài)圈中一個有用的補充。