• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 系统服务框架部件<a name="ZH-CN_TOPIC_0000001115588558"></a>
2## 简介<a name="section11660541593"></a>
3
4在系统服务管理子系统中safwk组件定义OpenHarmony中SystemAbility的实现方法,并提供启动、注册等接口实现。
5
6## 系统架构<a name="section342962219551"></a>
7
8**图 1**  系统服务框架图
9
10
11![](figures/zh-cn_image_0000001115820566.png)
12
13## 目录<a name="section161941989596"></a>
14
15```
16/foundation/systemabilitymgr
17│── safwk               # 组件目录
18│  ├── bundle.json      # 组件描述及编译脚本
19│  ├── etc              # 配置文件
20│  ├── interfaces       # 对外接口目录
21│  ├── services         # 组件服务实现
22│  ├── test             # 测试用例
23```
24
25## 说明<a name="section1312121216216"></a>
26
27### 接口说明<a name="section1551164914237"></a>
28
29<a name="table775715438253"></a>
30<table><thead align="left"><tr id="row12757154342519"><th class="cellrowborder" valign="top" width="43.19%" id="mcps1.1.3.1.1"><p id="p1075794372512"><a name="p1075794372512"></a><a name="p1075794372512"></a>接口名</p>
31</th>
32<th class="cellrowborder" valign="top" width="56.81%" id="mcps1.1.3.1.2"><p id="p375844342518"><a name="p375844342518"></a><a name="p375844342518"></a>接口描述</p>
33</th>
34</tr>
35</thead>
36<tbody><tr id="row1975804332517"><td class="cellrowborder" valign="top" width="43.19%" headers="mcps1.1.3.1.1 "><p id="p5758174313255"><a name="p5758174313255"></a><a name="p5758174313255"></a>sptr&lt;IRemoteObject&gt; GetSystemAbility(int32_t systemAbilityId);</p>
37</td>
38<td class="cellrowborder" valign="top" width="56.81%" headers="mcps1.1.3.1.2 "><p id="p14758743192519"><a name="p14758743192519"></a><a name="p14758743192519"></a>获取指定系统服务的RPC对象。</p>
39</td>
40</tr>
41<tr id="row2758943102514"><td class="cellrowborder" valign="top" width="43.19%" headers="mcps1.1.3.1.1 "><p id="p107581438250"><a name="p107581438250"></a><a name="p107581438250"></a>bool Publish(sptr&lt;IRemoteObject&gt; systemAbility);</p>
42</td>
43<td class="cellrowborder" valign="top" width="56.81%" headers="mcps1.1.3.1.2 "><p id="p8758743202512"><a name="p8758743202512"></a><a name="p8758743202512"></a>发布系统服务。</p>
44</td>
45</tr>
46<tr id="row09311240175710"><td class="cellrowborder" valign="top" width="43.19%" headers="mcps1.1.3.1.1 "><p id="p159328405571"><a name="p159328405571"></a><a name="p159328405571"></a>virtual void DoStartSAProcess(const std::string&amp; profilePath) = 0;</p>
47</td>
48<td class="cellrowborder" valign="top" width="56.81%" headers="mcps1.1.3.1.2 "><p id="p493294018574"><a name="p493294018574"></a><a name="p493294018574"></a>根据SA profile配置启动System Ability。</p>
49</td>
50</tr>
51</tbody>
52</table>
53
54### 使用说明<a name="section129654513264"></a>
55
56SystemAbility实现一般采用XXX.cfg + profile.xml + libXXX.z.so的方式由init进程执行对应的XXX.cfg文件拉起相关SystemAbility进程。
57
58**C++实现SystemAbility**
59
60示例代码如下:
61
62-   **1. 定义IPC对外接口IXXX**
63
64定义该服务对外提供的能力集合函数,统一继承IPC接口类IRemoteBroker;同时实现该IPC对外接口唯一标识符DECLARE\_INTERFACE\_DESCRIPTOR\(XXX\);该标识符用于IPC通信的校验等目的。
65
66```
67namespace OHOS {
68class IListenAbility : public IRemoteBroker {
69public:
70    virtual int AddVolume(int volume) = 0;
71
72public:
73    enum {
74        ADD_VOLUME = 1,
75    };
76public:
77    DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.test.IListenAbility");
78};
79}
80```
81
82-   **2. 定义客户端通信代码XXXProxy**
83
84```
85namespace OHOS {
86class ListenAbilityProxy : public IRemoteProxy<IListenAbility> {
87public:
88    int AddVolume(int volume);
89
90    explicit ListenAbilityProxy(const sptr<IRemoteObject>& impl)
91        : IRemoteProxy<IListenAbility>(impl)
92    {
93    }
94
95private:
96    static inline BrokerDelegator<ListenAbilityProxy> delegator_;
97};
98} // namespace OHOS
99```
100
101-   **3. 定义服务端通信代码XXXStub**
102
103```
104namespace OHOS {
105int32_t ListenAbilityStub::OnRemoteRequest(uint32_t code,
106    MessageParcel& data, MessageParcel &reply, MessageOption &option)
107{
108    switch (code) {
109        case ADD_VOLUME: {
110            return reply.WriteInt32(AddVolume(data.ReadInt32()));
111        }
112
113        default:
114            return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
115    }
116}
117}
118```
119
120-   **4. SystemAbility的实现类**
121
122```
123namespace {
124constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, 0xD001800, "SA_TST"};
125}
126
127REGISTER_SYSTEM_ABILITY_BY_ID(ListenAbility, DISTRIBUTED_SCHED_TEST_LISTEN_ID, true);
128
129ListenAbility::ListenAbility(int32_t saId, bool runOnCreate) : SystemAbility(saId, runOnCreate)
130{
131    HiLog::Info(LABEL, ":%s called", __func__);
132    HiLog::Info(LABEL, "ListenAbility()");
133}
134
135ListenAbility::~ListenAbility()
136{
137    HiLog::Info(LABEL, "~ListenAbility()");
138}
139
140int ListenAbility::AddVolume(int volume)
141{
142    pid_t current = getpid();
143    HiLog::Info(LABEL, "ListenAbility::AddVolume volume = %d, pid = %d.", volume, current);
144    return (volume + 1);
145}
146
147void ListenAbility::OnDump()
148{
149}
150
151void ListenAbility::OnStart()
152{
153    HiLog::Info(LABEL, "ListenAbility::OnStart()");
154    HiLog::Info(LABEL, "ListenAbility:%s called:-----Publish------", __func__);
155    bool res = Publish(this);
156    if (res) {
157        HiLog::Error(LABEL, "ListenAbility: res == false");
158    }
159    HiLog::Info(LABEL, "ListenAbility:%s called:AddAbilityListener_OS_TST----beg-----", __func__);
160    AddSystemAbilityListener(DISTRIBUTED_SCHED_TEST_OS_ID);
161    HiLog::Info(LABEL, "ListenAbility:%s called:AddAbilityListener_OS_TST----end-----", __func__);
162
163    HiLog::Info(LABEL, "ListenAbility:%s called:StopAbility_OS_TST----beg-----", __func__);
164    StopAbility(DISTRIBUTED_SCHED_TEST_OS_ID);
165    HiLog::Info(LABEL, "ListenAbility:%s called:StopAbility_OS_TST----end-----", __func__);
166    return;
167}
168
169void ListenAbility::OnStop()
170{
171}
172```
173
174-   **5. SystemAbility配置**
175
176以c++实现的SA必须配置相关System Ability的profile配置文件才会完成SA的加载注册逻辑,否则没有编写profile配置的System Ability不会完成注册。配置方法如下:
177
178在子系统的根目录新建一个以sa\_profile为名的文件夹,然后在此文件夹中新建两个文件:一个以serviceId为前缀的xml文件,另外一个为BUILD.gn文件。
179
180serviceid.xml181
182```
183<?xml version="1.0" encoding="UTF-8"?>
184<info>
185    <process>listen_test</process>
186    <systemability>
187    <name>serviceid</name>
188    <libpath>/system/lib64/liblistentest.z.so</libpath>
189    <run-on-create>true</run-on-create>
190    <distributed>false</distributed>
191    <dump-level>1</dump-level>
192</systemability>
193</info>
194```
195
196BUILD.gn197
198```
199import("//build/ohos/sa_profile/sa_profile.gni")
200ohos_sa_profile("xxx_sa_profile") {
201    sources = [
202        "serviceid.xml"
203    ]
204    subsystem_name = "systemabilitymgr"
205}
206```
207
208>**说明:**
209>1.  进程名字即该SystemAbility要运行的进程空间,此字段是必填选项。
210>2.  一个SystemAbility配置文件只能配置一个SystemAbility节点,配置多个会导致编译失败。
211>3.  SystemAbility的name为对应的serviceId必须与代码中注册的serviceId保持一致,必配项。
212>4.  libpath为SystemAbility的加载路径,必配项。
213>5.  run-on-create:true表示进程启动后即向samgr组件注册该SystemAbility;false表示按需启动,即在其他模块访问到该SystemAbility时启动,必配项。
214>6.  distributed:true表示该SystemAbility为分布式SystemAbility,支持跨设备访问;false表示只有本地跨IPC访问。
215>7.  bootphase:可不设置;可以设置的值有三种:BootStartPhase、CoreStartPhase、OtherStartPhase(默认类型),三种优先级依次降低,当同一个进程中,会优先拉起注册配置BootStartPhase的SystemAbility,然后是配置了CoreStartPhase的SystemAbility,最后是OtherStartPhase;当高优先级的SystemAbility全部启动注册完毕才会启动下一级的SystemAbility的注册启动。
216>8.  dump-level:表示systemdumper支持的level等级,默认配置1。
217>9. BUILD.gn中subsystem\_name为相应部件名称;sources表示当前子系统需要配置的SystemAbility列表,可支持配置多个SystemAbility。
218
219以上步骤完成后,全量编译代码后会在out路径向生成一个以进程名为前缀的xml文件listen\_test.xml;路径为:out\\...\\system\\profile\\listen\_test.xml220
221-   **6. cfg配置文件**
222
223cfg配置文件为linux提供的native进程拉起策略,开机启动阶段由init进程解析配置的cfg文件进行拉起。
224
225```
226{
227    "jobs" : [{
228            "name" : "post-fs-data",
229            "cmds" : [
230                "start listen_test"
231            ]
232        }
233    ],
234	"services" : [{
235            "name" : "listen_test",
236            "path" : ["/system/bin/sa_main", "/system/profile/listen_test.xml"],
237            "uid" : "system",
238            "gid" : ["system", "shell"]
239        }
240    ]
241}
242```
243
244>**说明:**
245>listen\_ability的实现可以参考:test/services/safwk/unittest/common/listen\_ability。
246
247## 相关仓<a name="section1371113476307"></a>
248
249系统服务管理子系统
250
251[**systemabilitymgr\_safwk**](https://gitee.com/openharmony/systemabilitymgr_safwk)
252
253[systemabilitymgr\_samgr](https://gitee.com/openharmony/systemabilitymgr_samgr)
254
255[systemabilitymgr\_safwk\_lite](https://gitee.com/openharmony/systemabilitymgr_safwk_lite)
256
257[systemabilitymgr\_samgr\_lite](https://gitee.com/openharmony/systemabilitymgr_samgr_lite)