# systemabilitymgr_safwk **Repository Path**: fqwert/systemabilitymgr_safwk ## Basic Information - **Project Name**: systemabilitymgr_safwk - **Description**: System ability framework | 系统服务框架定义 - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 271 - **Created**: 2023-11-08 - **Last Updated**: 2025-02-26 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 系统服务框架部件 ## 简介 在系统服务管理子系统中safwk组件定义OpenHarmony中SystemAbility的实现方法,并提供启动、注册等接口实现。 ## 系统架构 **图 1** 系统服务框架图 ![](figures/zh-cn_image_0000001115820566.png) ## 目录 ``` /foundation/systemabilitymgr │── safwk # 组件目录 │ ├── bundle.json # 组件描述及编译脚本 │ ├── etc # 配置文件 │ ├── interfaces # 对外接口目录 │ ├── services # 组件服务实现 │ ├── test # 测试用例 ``` ## 说明 ### 接口说明

接口名

接口描述

sptr<IRemoteObject> GetSystemAbility(int32_t systemAbilityId);

获取指定系统服务的RPC对象。

bool Publish(sptr<IRemoteObject> systemAbility);

发布系统服务。

virtual void DoStartSAProcess(const std::string& profilePath) = 0;

根据SA profile配置启动System Ability。

### 使用说明 SystemAbility实现一般采用XXX.cfg + profile.json + libXXX.z.so的方式由init进程执行对应的XXX.cfg文件拉起相关SystemAbility进程。 **C++实现SystemAbility** 示例代码如下: - **1. 定义IPC对外接口IXXX** 定义该服务对外提供的能力集合函数,统一继承IPC接口类IRemoteBroker;同时实现该IPC对外接口唯一标识符DECLARE\_INTERFACE\_DESCRIPTOR\(XXX\);该标识符用于IPC通信的校验等目的。 ``` namespace OHOS { class IListenAbility : public IRemoteBroker { public: virtual int AddVolume(int volume) = 0; public: enum { ADD_VOLUME = 1, }; public: DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.test.IListenAbility"); }; } ``` - **2. 定义客户端通信代码XXXProxy** ``` namespace OHOS { class ListenAbilityProxy : public IRemoteProxy { public: int AddVolume(int volume); explicit ListenAbilityProxy(const sptr& impl) : IRemoteProxy(impl) { } private: static inline BrokerDelegator delegator_; }; } // namespace OHOS ``` - **3. 定义服务端通信代码XXXStub** ``` namespace OHOS { int32_t ListenAbilityStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel &reply, MessageOption &option) { switch (code) { case ADD_VOLUME: { return reply.WriteInt32(AddVolume(data.ReadInt32())); } default: return IPCObjectStub::OnRemoteRequest(code, data, reply, option); } } } ``` - **4. SystemAbility的实现类** ``` namespace { constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, 0xD001800, "SA_TST"}; } REGISTER_SYSTEM_ABILITY_BY_ID(ListenAbility, DISTRIBUTED_SCHED_TEST_LISTEN_ID, true); ListenAbility::ListenAbility(int32_t saId, bool runOnCreate) : SystemAbility(saId, runOnCreate) { HiLog::Info(LABEL, ":%s called", __func__); HiLog::Info(LABEL, "ListenAbility()"); } ListenAbility::~ListenAbility() { HiLog::Info(LABEL, "~ListenAbility()"); } int ListenAbility::AddVolume(int volume) { pid_t current = getpid(); HiLog::Info(LABEL, "ListenAbility::AddVolume volume = %d, pid = %d.", volume, current); return (volume + 1); } void ListenAbility::OnDump() { } void ListenAbility::OnStart() { HiLog::Info(LABEL, "ListenAbility::OnStart()"); HiLog::Info(LABEL, "ListenAbility:%s called:-----Publish------", __func__); bool res = Publish(this); if (res) { HiLog::Error(LABEL, "ListenAbility: res == false"); } HiLog::Info(LABEL, "ListenAbility:%s called:AddAbilityListener_OS_TST----beg-----", __func__); AddSystemAbilityListener(DISTRIBUTED_SCHED_TEST_OS_ID); HiLog::Info(LABEL, "ListenAbility:%s called:AddAbilityListener_OS_TST----end-----", __func__); HiLog::Info(LABEL, "ListenAbility:%s called:StopAbility_OS_TST----beg-----", __func__); StopAbility(DISTRIBUTED_SCHED_TEST_OS_ID); HiLog::Info(LABEL, "ListenAbility:%s called:StopAbility_OS_TST----end-----", __func__); return; } void ListenAbility::OnStop() { } ``` - **5. SystemAbility配置** 以c++实现的SA必须配置相关System Ability的profile配置文件才会完成SA的加载注册逻辑,否则没有编写profile配置的System Ability不会完成注册。配置方法如下: 在子系统的根目录新建一个以sa\_profile为名的文件夹,然后在此文件夹中新建两个文件:一个以serviceId为前缀的json文件,另外一个为BUILD.gn文件。 serviceid.json: ``` { "process": "listen_test", "systemability": [ { "name": serviceid, "libpath": "liblisten_test.z.so", "run-on-create": true, "distributed": true, "dump_level": 1 } ] } ``` BUILD.gn: ``` import("//build/ohos/sa_profile/sa_profile.gni") ohos_sa_profile("xxx_sa_profile") { sources = [ "serviceid.json" ] subsystem_name = "systemabilitymgr" } ``` >**说明:** >1. 进程名字即该SystemAbility要运行的进程空间,此字段是必填选项。 >2. 一个SystemAbility配置文件只能配置一个SystemAbility节点,配置多个会导致编译失败。 >3. SystemAbility的name为对应的serviceId必须与代码中注册的serviceId保持一致,必配项。 >4. libpath为SystemAbility的加载路径,必配项。 >5. run-on-create:true表示进程启动后即向samgr组件注册该SystemAbility;false表示按需启动,即在其他模块访问到该SystemAbility时启动,必配项。 >6. distributed:true表示该SystemAbility为分布式SystemAbility,支持跨设备访问;false表示只有本地跨IPC访问。 >7. bootphase:可不设置;可以设置的值有三种:BootStartPhase、CoreStartPhase、OtherStartPhase(默认类型),三种优先级依次降低,当同一个进程中,会优先拉起注册配置BootStartPhase的SystemAbility,然后是配置了CoreStartPhase的SystemAbility,最后是OtherStartPhase;当高优先级的SystemAbility全部启动注册完毕才会启动下一级的SystemAbility的注册启动。 >8. dump-level:表示systemdumper支持的level等级,默认配置1。 >9. BUILD.gn中subsystem\_name为相应部件名称;sources表示当前子系统需要配置的SystemAbility列表,可支持配置多个SystemAbility。 以上步骤完成后,全量编译代码后会在out路径向生成一个以进程名为前缀的json文件listen\_test.json;路径为:out\\...\\system\\profile\\listen\_test.json。 - **6. cfg配置文件** cfg配置文件为linux提供的native进程拉起策略,开机启动阶段由init进程解析配置的cfg文件进行拉起。 ``` { "jobs" : [{ "name" : "post-fs-data", "cmds" : [ "start listen_test" ] } ], "services" : [{ "name" : "listen_test", "path" : ["/system/bin/sa_main", "/system/profile/listen_test.json"], "uid" : "system", "gid" : ["system", "shell"] } ] } ``` >**说明:** >listen\_ability的实现可以参考:test/services/safwk/unittest/common/listen\_ability。 ## 相关仓 系统服务管理子系统 [**systemabilitymgr\_safwk**](https://gitee.com/openharmony/systemabilitymgr_safwk) [systemabilitymgr\_samgr](https://gitee.com/openharmony/systemabilitymgr_samgr) [systemabilitymgr\_safwk\_lite](https://gitee.com/openharmony/systemabilitymgr_safwk_lite) [systemabilitymgr\_samgr\_lite](https://gitee.com/openharmony/systemabilitymgr_samgr_lite)