作者:茍晶晶
前言
當(dāng)開發(fā)者為OpenHarmony系統(tǒng)框架開發(fā)某些功能時,有時需要將這個功能包裝成一個獨(dú)立的服務(wù)進(jìn)程運(yùn)行在系統(tǒng)中,為了其它應(yīng)用進(jìn)程能夠調(diào)用此服務(wù),開發(fā)人員需要基于系統(tǒng)IPC通信框架編寫一套遠(yuǎn)程接口調(diào)用實(shí)現(xiàn)。實(shí)現(xiàn)Service遠(yuǎn)程調(diào)用接口需要開發(fā)人員熟悉IPC通信框架,了解proxy/stub的繼承與實(shí)現(xiàn)方式,掌握C++類型轉(zhuǎn)為MessageParcel數(shù)據(jù)包的各種API方法,有一定的學(xué)習(xí)成本。而Service代碼生成工具能夠幫助使用者生成框架代碼,提升開發(fā)效率。用戶只需提供一個定義遠(yuǎn)程方法的.h頭文件,工具會自動生成整個Service框架的代碼,包含Ability注冊、proxy/stub類實(shí)現(xiàn)、MessageParcel數(shù)據(jù)包構(gòu)造、Service子系統(tǒng)編譯及開機(jī)自啟動相關(guān)配置文件。
1.工具原理
Service框架代碼生成工具包含工具入口、工具框架、公共模塊、運(yùn)行環(huán)境、系統(tǒng)平臺。其中,工具入口描述調(diào)用Service框架代碼生成工具的入口方式,支持命令行調(diào)用、VS Code插件(即VS插件)調(diào)用,從而可以根據(jù)開發(fā)環(huán)境的不同,采用相對應(yīng)的調(diào)用Service框架代碼生成工具的入口方式,實(shí)現(xiàn)Service框架代碼生成工具的入口多樣性,便于調(diào)用Service框架代碼生成工具。工具框架包含C語法解析器、代碼生成器兩部分,C語法解析器支持包括但不限于對class、function、properties、parameter等內(nèi)容的解析,代碼生成器支持包括但不限于對proxy、stub、service、interface等服務(wù)框架代碼的生成。公共模塊描述通用的、在不同部分均會使用的公共接口與模塊,可以包括通用的正則校驗(yàn)、類型映射、代碼模板、文件操作等模塊,運(yùn)行環(huán)境描述Service框架代碼生成工具運(yùn)行的環(huán)境,包括Nodejs與Python/ target=_blank class=infotextkey>Python,由于Nodejs本身具有跨平臺性特點(diǎn),故Service框架代碼生成工具可以在windows、linux、mac、OpenHarmony等不同系統(tǒng)平臺靈活使用,Service框架代碼生成工具的運(yùn)行環(huán)境另一部分是python,針對不同平臺做python適配,Service框架代碼生成工具即可實(shí)現(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”選項(xiàng)。
6、 工具打開 Service Generate Frame窗口,.h文件選擇框默認(rèn)填寫被操作的.h文件的絕對路徑;輸出路徑選擇框默認(rèn)填寫.h文件所在文件夾路徑,可修改為任意路徑;serviceID范圍是1-16777215之間的整數(shù),超出范圍會提示錯誤,填入正確的serviceID,然后點(diǎn)擊ok。
7、 轉(zhuǎn)換成功后,在輸出路徑下生成service框架代碼文件。
輸出文件說明
service工具生成文件說明如下圖所示:
其中消息調(diào)用流程為:
- 服務(wù)端實(shí)現(xiàn)SystemAbility接口OnStart(),將自己的serviceId注冊到SystemAbility Manager管理類。
- 客戶端根據(jù)serviceId向SystemAbility Manager管理類獲取該service的proxy對象。
- 客戶端使用proxy對象調(diào)用服務(wù)端的遠(yuǎn)程接口。
- proxy將客戶端傳入的c++參數(shù)打包成消息數(shù)據(jù),通過系統(tǒng)提供的dbinder進(jìn)程間通信能力發(fā)送到服務(wù)端進(jìn)程。
- 服務(wù)端OnRemoteRequest()接收到遠(yuǎn)程調(diào)用消息,根據(jù)消息id分發(fā)給不同的innerFunction()處理。
- 服務(wù)端innerFunction()將遠(yuǎn)程消息數(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("//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" ] deps = [ "//base/startup/syspara_lite/interfaces/innerkits/native/syspara:syspara", "//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 = [ "//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" ] deps = [ "//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": [ "//testservice:testservice", "//testservice/sa_profile:testservice_sa_profile", "//testservice:testclient", "//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宏值定義必須為這個,因?yàn)榇a中使用的就是這個)
TEST_SERVICE_ID = 9016, {TEST_SERVICE_ID, "testservice" },
-
子系統(tǒng)配置
build/subsystem_config.json
增加以下內(nèi)容
"testservice": { "path":"testservice", "name": "testservice" }
-
產(chǎn)品配置,如rk3568
vendor/hihope/rk3568/config.json
增加以下內(nèi)容
{ "subsystem": "testservice", "components": [ { "component": "testservice_part", "features": [] } ] }
注意:若用戶需要配置selinux相關(guān)配置,則將開關(guān)改為true,再根據(jù)自身需求進(jìn)行相關(guān)配置
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;
4.示例演示
服務(wù)端修改
test_service.cpp
在testservice/src/test_service.cpp注釋“// TODO: Invoke the business implementation”處添加各個接口的服務(wù)端實(shí)現(xiàn)代碼。本例實(shí)現(xiàn)一個簡單的加減法,服務(wù)端代碼如下所示:
int testService::testFunc(int v1, int v2, bool v3)
{
// TODO: Invoke the business implementation
int ret = 0;
printf("service test begin rn");
if (v3) {
printf("service test v3 = truern");
ret = v1 + v2;
} else {
printf("service test v3 = false rn");
ret = v1 - v2;
}
printf("service test end rn");
return ret;
}
遠(yuǎn)程方法的參數(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對象進(jìn)行遠(yuǎn)程方法調(diào)用,參考注釋示例。本例實(shí)現(xiàn)一個簡單的加減法,客戶端代碼如下所示:
int main(int argc, char *argv[])
{
printf("---functest begin---rn");
auto proxy = getRemoteProxy();
uint32_t result = 0;
// TODO: Invoke remote method by proxy
result = proxy->testFunc(8, 5, false);
printf("result is : %urn", result);
printf("---functest end---rn");
IPCSkeleton::JoinWorkThread();
return 0;
}
遠(yuǎn)程方法的參數(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
運(yùn)行
將編譯好的鏡像燒錄到開發(fā)板后,使用hdc_std shell登錄開發(fā)板。
查看服務(wù)端進(jìn)程是否已正常啟動
ps -ef | grep testservice
system 682 1 0 08:00:08 ? 00:00:00 testservice_sa --- 服務(wù)進(jìn)程已正常運(yùn)行
如下圖所示:
運(yùn)行客戶端
/system/bin/testclient
運(yùn)行結(jié)果如下所示:
---functest begin---
result is : 3
---functest end---
(客戶端具體執(zhí)行哪些遠(yuǎn)程調(diào)用方法請?jiān)趖est_client.cpp的main方法中實(shí)現(xiàn))
總結(jié)
service生成工具是一個開源項(xiàng)目,我們歡迎有興趣的開發(fā)者試用該工具,并提出寶貴的改進(jìn)意見,我們將繼續(xù)不斷優(yōu)化和完善該工具軟件。我們相信,該工具會成為OpenHarmony生態(tài)圈中一個有用的補(bǔ)充。