• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2* Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development Co., Ltd.
3* Licensed under the Apache License, Version 2.0 (the "License");
4* you may not use this file except in compliance with the License.
5* You may obtain a copy of the License at
6*
7* http://www.apache.org/licenses/LICENSE-2.0
8*
9* Unless required by applicable law or agreed to in writing, software
10* distributed under the License is distributed on an "AS IS" BASIS,
11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12* See the License for the specific language governing permissions and
13* limitations under the License.
14*/
15
16let iServiceHTemplate = `#ifndef I_[marcoName]_SERVICE_H
17#define I_[marcoName]_SERVICE_H
18
19[includes]
20#include "iremote_broker.h"
21#include "iremote_proxy.h"
22
23namespace OHOS {
24namespace [serviceName] {
25class I[className]Service : public OHOS::IRemoteBroker {
26public:
27    enum {
28        [funcEnum]
29    };
30
31    DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.[serviceName].I[className]Service");
32public:
33    [functions]
34};
35} // namespace [serviceName]
36} // namespace OHOS
37#endif // I_[marcoName]_SERVICE_H
38`;
39let proxyHTemplate = `#ifndef [marcoName]_PROXY_H
40#define [marcoName]_PROXY_H
41#include "message_parcel.h"
42#include "parcel.h"
43#include "iremote_broker.h"
44#include "iremote_proxy.h"
45#include "[iServiceHInclude]"
46
47namespace OHOS {
48namespace [serviceName] {
49class [className]Proxy : public IRemoteProxy<I[className]Service> {
50public:
51    explicit [className]Proxy(const sptr<IRemoteObject> &impl);
52    ~[className]Proxy() = default;
53    [functions]
54private:
55    static inline BrokerDelegator<[className]Proxy> delegator_;
56};
57
58class [className]DeathRecipient : public IRemoteObject::DeathRecipient {
59public:
60    virtual void OnRemoteDied(const wptr<IRemoteObject> &remote) override;
61    [className]DeathRecipient();
62    virtual ~[className]DeathRecipient();
63};
64} // namespace [serviceName]
65} // namespace OHOS
66#endif // [marcoName]_PROXY_H
67`;
68
69let stubHTemplate = `#ifndef [marcoName]_STUB_H
70#define [marcoName]_STUB_H
71#include "iremote_stub.h"
72#include "message_parcel.h"
73#include "parcel.h"
74#include "[iServiceHInclude]"
75
76namespace OHOS {
77namespace [serviceName] {
78class [className]Stub : public IRemoteStub<I[className]Service> {
79public:
80    [className]Stub();
81    virtual ~[className]Stub();
82    int OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply,
83        MessageOption &option) override;
84private:
85    using [className]InnerFunc = ErrCode ([className]Stub::*)(MessageParcel &data, MessageParcel &reply);
86    [innerFuncDef]
87    std::unordered_map<uint32_t, [className]InnerFunc> innerFuncs_;
88};
89} // namespace [serviceName]
90} // namespace OHOS
91#endif // [marcoName]_STUB_H`;
92
93let serviceHTemplate = `#ifndef [marcoName]_SERVICE_H
94#define [marcoName]_SERVICE_H
95#include "ipc_skeleton.h"
96#include "system_ability.h"
97#include "[stubHInclude]"
98
99namespace OHOS {
100namespace [serviceName] {
101// Business implementation
102class [className]Service : public SystemAbility, public [className]Stub {
103public:
104    DECLARE_SYSTEM_ABILITY([className]Service);
105    DISALLOW_COPY_AND_MOVE([className]Service);
106    explicit [className]Service(int32_t systemAbilityId, bool runOnCreate = true);
107    ~[className]Service() override;
108
109    // Business implementation
110    [functions]
111
112    // implement SystemAbility methods
113    void OnStart() override;
114    void OnStop() override;
115};
116}
117}
118#endif // [marcoName]_SERVICE_H`;
119
120let proxyCppTemplate = `#include "[proxyHInclude]"
121using namespace std;
122
123namespace OHOS {
124namespace [serviceName] {
125[className]Proxy::[className]Proxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<I[className]Service>(impl){}
126
127[remoteFuncImpl]
128/**
129 * @brief Notify that a remote object died.
130 * It's called when the linked remote object died.
131 *
132 * @param remote The died IRemoteObject handler of the remote object
133 */
134void [className]DeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
135{
136
137}
138
139[className]DeathRecipient::[className]DeathRecipient()
140{
141}
142
143[className]DeathRecipient::~[className]DeathRecipient()
144{
145}
146
147} // namespace [serviceName]
148} // namespace OHOS
149`;
150let proxyFuncTemplate = `[retType] [className]Proxy::[funcName]([params])
151{
152    int retCode;
153    MessageParcel data, reply;
154    MessageOption option;
155    data.WriteInterfaceToken(GetDescriptor());
156    [writeData]
157    retCode = Remote()->SendRequest([funcEnum], data, reply, option);
158    retCode = reply.ReadInt32();
159    if (retCode != ERR_OK) {
160
161    }
162
163    [readReply]
164}\n\n`;
165
166let stubCppTemplate = `#include "[stubHInclude]"
167using namespace std;
168
169namespace OHOS {
170namespace [serviceName] {
171
172[className]Stub::[className]Stub()
173{
174    [innerFuncMap]
175}
176
177[className]Stub::~[className]Stub()
178{
179    innerFuncs_.clear();
180}
181
182int [className]Stub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply,
183    MessageOption &option)
184{
185    std::u16string descriptor = [className]Stub::GetDescriptor();
186    std::u16string remoteDescriptor = data.ReadInterfaceToken();
187    if (descriptor != remoteDescriptor) {
188        return OBJECT_NULL;
189    }
190    auto itFunc = innerFuncs_.find(code);
191    if (itFunc != innerFuncs_.end()) {
192        auto memberFunc = itFunc->second;
193        if (memberFunc != nullptr) {
194            return (this->*memberFunc)(data, reply);
195        }
196    }
197    return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
198}
199
200[innerFuncImpl]
201
202} // namespace [serviceName]
203} // namespace OHOS
204`;
205
206let stubInnerFuncTemplate = `ErrCode [className]Stub::[funcName]Inner(MessageParcel &data, MessageParcel &reply)
207{
208    int retCode = ERR_OK;
209    [readData]
210    [writeReply]
211    return retCode;
212}
213`;
214
215let serviceCppTemplate = `#include "[serviceHInclude]"
216#include "system_ability_definition.h"
217using namespace std;
218
219namespace OHOS {
220namespace [serviceName] {
221// [marcoName]_SERVICE_ID should be defined in system_ability_definition.h
222REGISTER_SYSTEM_ABILITY_BY_ID([className]Service, [marcoName]_SERVICE_ID, true)
223
224[className]Service::[className]Service(int32_t systemAbilityId, bool runOnCreate)
225    :SystemAbility(systemAbilityId, runOnCreate)
226{
227
228}
229
230[className]Service::~[className]Service(){
231
232}
233
234void [className]Service::OnStart()
235{
236    // Publish(): Register service by method ISystemAbilityManager->AddSystemAbility()
237    bool isPublished = Publish(this);
238    if (!isPublished) {
239        // Log: Failed to publish the service
240    }
241    return;
242}
243
244void [className]Service::OnStop()
245{
246
247}
248
249[serviceFuncImpl]
250}
251}`;
252
253let serviceFuncImplTemplate = `[retType] [className]Service::[funcName]([params])
254{
255    [retType] ret;
256    // TODO: Invoke the business implementation
257    return ret;
258}
259`;
260let clientCppTemplate = `#include "[proxyHInclude]"
261#include "ipc_skeleton.h"
262#include "system_ability_definition.h"
263#include "iservice_registry.h"
264
265using namespace std;
266using namespace OHOS;
267using namespace OHOS::[serviceName];
268
269sptr<I[className]Service> getRemoteProxy()
270{
271    auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
272    if (saMgr == nullptr) {
273        return nullptr;
274    }
275
276    // [marcoName]_SERVICE_ID should be defined in system_ability_definition.h
277    sptr<IRemoteObject> object = saMgr->GetSystemAbility([marcoName]_SERVICE_ID);
278    sptr<I[className]Service> proxy = nullptr;
279    if (object != nullptr) {
280        sptr<IRemoteObject::DeathRecipient> death(new [className]DeathRecipient());
281        object->AddDeathRecipient(death.GetRefPtr());
282        proxy = iface_cast<I[className]Service>(object);
283    }
284
285    if (proxy == nullptr) {
286        return nullptr;
287    }
288
289    return proxy;
290}
291
292int main(int argc, char *argv[])
293{
294    auto proxy = getRemoteProxy();
295    // TODO: Invoke remote method by proxy
296    [clientFuncInvoke]
297
298    IPCSkeleton::JoinWorkThread();
299    return 0;
300}`;
301
302let buildGnTemplate = `import("//build/ohos.gni")
303
304ohos_shared_library("[lowServiceName]service") {
305  sources = [
306    "//[lowServiceName]service/src/[stubCppFile]",
307    "//[lowServiceName]service/src/[serviceCppFile]"
308  ]
309  include_dirs = [
310    "//[lowServiceName]service/include",
311    "//[lowServiceName]service/interface",
312    "//utils/native/base/include"
313  ]
314
315  deps = [
316    "//base/startup/syspara_lite/interfaces/innerkits/native/syspara:syspara",
317    "//utils/native/base:utils",
318  ]
319
320  external_deps = [
321    "hiviewdfx_hilog_native:libhilog",
322    "ipc:ipc_core",
323    "safwk:system_ability_fwk",
324    "samgr_standard:samgr_proxy",
325    "startup_l2:syspara",
326  ]
327
328  part_name = "[lowServiceName]service_part"
329  subsystem_name = "[lowServiceName]service"
330}
331
332ohos_executable("[lowServiceName]client") {
333    sources = [
334    "//[lowServiceName]service/src/[proxyCppFile]",
335    "//[lowServiceName]service/src/[clientCppFile]"
336  ]
337
338  include_dirs = [
339    "//[lowServiceName]service/include",
340    "//[lowServiceName]service/interface",
341    "//utils/native/base/include"
342  ]
343
344  deps = [
345    "//utils/native/base:utils",
346  ]
347
348  external_deps = [
349    "hiviewdfx_hilog_native:libhilog",
350    "ipc:ipc_core",
351    "samgr_standard:samgr_proxy",
352  ]
353
354  part_name = "[lowServiceName]service_part"
355  subsystem_name = "[lowServiceName]service"
356}
357`;
358
359let bundleJsonTemplate = `{
360    "name": "@ohos/[lowServiceName]service",
361    "description": "system ability framework test",
362    "homePage": "https://gitee.com/",
363    "version": "3.1",
364    "license": "Apache License 2.0",
365    "repository": "",
366    "publishAs": "code-segment",
367    "segment": {
368        "destPath": "[lowServiceName]service"
369    },
370    "dirs": {},
371    "scripts": {},
372    "component": {
373        "name": "[lowServiceName]service_part",
374        "subsystem": "[lowServiceName]service",
375        "adapted_system_type": [
376            "standard"
377        ],
378        "rom": "2048KB",
379        "ram": "~4096KB",
380        "deps": {
381            "components": [
382                "hiviewdfx_hilog_native",
383                "ipc",
384                "samgr_standard",
385                "utils_base",
386                "safwk",
387                "startup_l2"
388            ],
389            "third_party": [ "libxml2" ]
390        },
391        "build": {
392            "sub_component": [
393                "//[lowServiceName]service:[lowServiceName]service",
394                "//[lowServiceName]service/sa_profile:[lowServiceName]service_sa_profile",
395                "//[lowServiceName]service:[lowServiceName]client",
396                "//[lowServiceName]service/etc:[lowServiceName]_service_init"
397            ],
398            "inner_kits": [
399            ],
400            "test": [
401            ]
402        }
403    }
404}`;
405
406let profileGnTemplate = `import("//build/ohos.gni")
407import("//build/ohos/sa_profile/sa_profile.gni")
408
409ohos_sa_profile("[lowServiceName]service_sa_profile") {
410  sources = [ "[serviceId].xml" ]
411
412  part_name = "[lowServiceName]service_part"
413}
414`;
415
416let profileXmlTemplate = `<?xml version="1.0" encoding="utf-8"?>
417<info>
418    <process>[lowServiceName]service_sa</process>
419    <systemability>
420        <name>[serviceId]</name>
421        <libpath>lib[lowServiceName]service.z.so</libpath>
422        <run-on-create>true</run-on-create>
423        <distributed>false</distributed>
424        <dump-level>1</dump-level>
425    </systemability>
426</info>
427`;
428
429let serviceCfgTemplate = `{
430    "services" : [{
431            "name" : "[lowServiceName]service",
432            "path" : ["/system/bin/sa_main", "/system/profile/[lowServiceName]service_sa.xml"],
433            "uid" : "system",
434            "gid" : ["system", "shell"]
435        }
436    ]
437}
438`;
439
440let serviceCfgGnTemplate = `import("//build/ohos.gni")
441
442ohos_prebuilt_etc("[lowServiceName]_service_init") {
443  source = "[lowServiceName]_service.cfg"
444  relative_install_dir = "init"
445  part_name = "[lowServiceName]service_part"
446  subsystem_name = "[lowServiceName]service"
447}
448`;
449
450module.exports = {
451    iServiceHTemplate,
452    proxyHTemplate,
453    stubHTemplate,
454    serviceHTemplate,
455    proxyCppTemplate,
456    proxyFuncTemplate,
457    stubCppTemplate,
458    stubInnerFuncTemplate,
459    serviceCppTemplate,
460    serviceFuncImplTemplate,
461    clientCppTemplate,
462    buildGnTemplate,
463    bundleJsonTemplate,
464    profileGnTemplate,
465    profileXmlTemplate,
466    serviceCfgTemplate,
467    serviceCfgGnTemplate
468}