• 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[using]
23
24namespace OHOS {
25namespace [serviceName] {
26[dependClasses]
27class I[className]Service : public OHOS::IRemoteBroker {
28public:
29    enum {
30        [funcEnum]
31    };
32
33    DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.[serviceName].I[className]Service");
34public:
35    [functions]
36};
37[marshallFunctions]
38} // namespace [serviceName]
39} // namespace OHOS
40#endif // I_[marcoName]_SERVICE_H
41`;
42
43let proxyHTemplate = `#ifndef [marcoName]_PROXY_H
44#define [marcoName]_PROXY_H
45#include "message_parcel.h"
46#include "parcel.h"
47#include "iremote_broker.h"
48#include "iremote_proxy.h"
49#include "[iServiceHInclude]"
50
51namespace OHOS {
52namespace [serviceName] {
53class [className]Proxy : public IRemoteProxy<I[className]Service> {
54public:
55    explicit [className]Proxy(const sptr<IRemoteObject> &impl);
56    ~[className]Proxy() = default;
57    [functions]
58private:
59    static inline BrokerDelegator<[className]Proxy> delegator_;
60};
61
62class [className]DeathRecipient : public IRemoteObject::DeathRecipient {
63public:
64    virtual void OnRemoteDied(const wptr<IRemoteObject> &remote) override;
65    [className]DeathRecipient();
66    virtual ~[className]DeathRecipient();
67};
68} // namespace [serviceName]
69} // namespace OHOS
70#endif // [marcoName]_PROXY_H
71`;
72
73let stubHTemplate = `#ifndef [marcoName]_STUB_H
74#define [marcoName]_STUB_H
75#include "iremote_stub.h"
76#include "message_parcel.h"
77#include "parcel.h"
78#include "[iServiceHInclude]"
79
80namespace OHOS {
81namespace [serviceName] {
82class [className]Stub : public IRemoteStub<I[className]Service> {
83public:
84    [className]Stub();
85    virtual ~[className]Stub();
86    int OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply,
87        MessageOption &option) override;
88private:
89    using [className]InnerFunc = ErrCode ([className]Stub::*)(MessageParcel &data, MessageParcel &reply);
90    [innerFuncDef]
91    std::unordered_map<uint32_t, [className]InnerFunc> innerFuncs_;
92};
93} // namespace [serviceName]
94} // namespace OHOS
95#endif // [marcoName]_STUB_H`;
96
97let serviceHTemplate = `#ifndef [marcoName]_SERVICE_H
98#define [marcoName]_SERVICE_H
99#include "ipc_skeleton.h"
100#include "system_ability.h"
101#include "[stubHInclude]"
102
103namespace OHOS {
104namespace [serviceName] {
105// Business implementation
106class [className]Service : public SystemAbility, public [className]Stub {
107public:
108    DECLARE_SYSTEM_ABILITY([className]Service);
109    DISALLOW_COPY_AND_MOVE([className]Service);
110    explicit [className]Service(int32_t systemAbilityId, bool runOnCreate = true);
111    ~[className]Service() override;
112
113    // Business implementation
114    [functions]
115
116    // implement SystemAbility methods
117    void OnStart() override;
118    void OnStop() override;
119};
120}
121}
122#endif // [marcoName]_SERVICE_H`;
123
124let proxyCppTemplate = `#include "[proxyHInclude]"
125using namespace std;
126
127namespace OHOS {
128namespace [serviceName] {
129[className]Proxy::[className]Proxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<I[className]Service>(impl){}
130
131[remoteFuncImpl]
132/**
133 * @brief Notify that a remote object died.
134 * It's called when the linked remote object died.
135 *
136 * @param remote The died IRemoteObject handler of the remote object
137 */
138void [className]DeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
139{
140
141}
142
143[className]DeathRecipient::[className]DeathRecipient()
144{
145}
146
147[className]DeathRecipient::~[className]DeathRecipient()
148{
149}
150
151} // namespace [serviceName]
152} // namespace OHOS
153`;
154
155let proxyFuncTemplate = `[retType] [className]Proxy::[funcName]([params])
156{
157    int retCode;
158    MessageParcel data, reply;
159    MessageOption option;
160    data.WriteInterfaceToken(GetDescriptor());
161    [writeData]
162    retCode = Remote()->SendRequest([funcEnum], data, reply, option);
163    retCode = reply.ReadInt32();
164    if (retCode != ERR_OK) {
165
166    }
167
168    [readReply]
169}\n\n`;
170
171let stubCppTemplate = `#include "[stubHInclude]"
172using namespace std;
173
174namespace OHOS {
175namespace [serviceName] {
176
177[className]Stub::[className]Stub()
178{
179    [innerFuncMap]
180}
181
182[className]Stub::~[className]Stub()
183{
184    innerFuncs_.clear();
185}
186
187int [className]Stub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply,
188    MessageOption &option)
189{
190    std::u16string descriptor = [className]Stub::GetDescriptor();
191    std::u16string remoteDescriptor = data.ReadInterfaceToken();
192    if (descriptor != remoteDescriptor) {
193        return OBJECT_NULL;
194    }
195    auto itFunc = innerFuncs_.find(code);
196    if (itFunc != innerFuncs_.end()) {
197        auto memberFunc = itFunc->second;
198        if (memberFunc != nullptr) {
199            return (this->*memberFunc)(data, reply);
200        }
201    }
202    return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
203}
204
205[innerFuncImpl]
206
207} // namespace [serviceName]
208} // namespace OHOS
209`;
210
211let stubInnerFuncTemplate = `ErrCode [className]Stub::[funcName]Inner(MessageParcel &data, MessageParcel &reply)
212{
213    int retCode = ERR_OK;
214    [readData]
215    [writeReply]
216    return retCode;
217}
218`;
219
220let serviceCppTemplate = `#include "[serviceHInclude]"
221#include "system_ability_definition.h"
222using namespace std;
223
224namespace OHOS {
225namespace [serviceName] {
226// [marcoName]_SERVICE_ID should be defined in system_ability_definition.h
227REGISTER_SYSTEM_ABILITY_BY_ID([className]Service, [marcoName]_SERVICE_ID, true)
228
229[className]Service::[className]Service(int32_t systemAbilityId, bool runOnCreate)
230    :SystemAbility(systemAbilityId, runOnCreate)
231{
232
233}
234
235[className]Service::~[className]Service(){
236
237}
238
239void [className]Service::OnStart()
240{
241    // Publish(): Register service by method ISystemAbilityManager->AddSystemAbility()
242    bool isPublished = Publish(this);
243    if (!isPublished) {
244        // Log: Failed to publish the service
245    }
246    return;
247}
248
249void [className]Service::OnStop()
250{
251
252}
253
254[serviceFuncImpl]
255}
256}`;
257
258let serviceFuncImplTemplate = `[retType] [className]Service::[funcName]([params])
259{
260    [retType] ret = [initRetvalue];
261    // TODO: Invoke the business implementation
262    ret = [paramsName];
263    printf("service [paramsName]= %i", ret);
264    return ret;
265}
266`;
267
268let clientCppTemplate = `#include "[proxyHInclude]"
269#include "ipc_skeleton.h"
270#include "system_ability_definition.h"
271#include "iservice_registry.h"
272
273using namespace std;
274using namespace OHOS;
275using namespace OHOS::[serviceName];
276
277sptr<I[className]Service> getRemoteProxy()
278{
279    auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
280    if (saMgr == nullptr) {
281        return nullptr;
282    }
283
284    // [marcoName]_SERVICE_ID should be defined in system_ability_definition.h
285    sptr<IRemoteObject> object = saMgr->GetSystemAbility([marcoName]_SERVICE_ID);
286    sptr<I[className]Service> proxy = nullptr;
287    if (object != nullptr) {
288        sptr<IRemoteObject::DeathRecipient> death(new [className]DeathRecipient());
289        object->AddDeathRecipient(death.GetRefPtr());
290        proxy = iface_cast<I[className]Service>(object);
291    }
292
293    if (proxy == nullptr) {
294        return nullptr;
295    }
296
297    return proxy;
298}
299
300int main(int argc, char *argv[])
301{
302    auto proxy = getRemoteProxy();
303    // TODO: Invoke remote method by proxy
304[clientFuncParaMessage]
305    int res = 99;
306    [clientFuncInvoke]
307    printf("get res = %i", res);
308[clientFuncParaLogMessage]
309    IPCSkeleton::JoinWorkThread();
310    return 0;
311}`;
312
313let buildGnTemplate = `import("//build/ohos.gni")
314
315ohos_shared_library("[lowServiceName]service") {
316  sources = [
317    "//[lowServiceName]service/src/[iServiceCppFile]",
318    "//[lowServiceName]service/src/[stubCppFile]",
319    "//[lowServiceName]service/src/[serviceCppFile]"
320  ]
321  include_dirs = [
322    "//[lowServiceName]service/include",
323    "//[lowServiceName]service/interface",
324    "//utils/native/base/include"
325  ]
326
327  deps = [
328    "//base/startup/syspara_lite/interfaces/innerkits/native/syspara:syspara",
329    "//utils/native/base:utils",
330  ]
331
332  external_deps = [
333    "hiviewdfx_hilog_native:libhilog",
334    "ipc:ipc_core",
335    "safwk:system_ability_fwk",
336    "samgr_standard:samgr_proxy",
337    "startup_l2:syspara",
338  ]
339
340  part_name = "[lowServiceName]service_part"
341  subsystem_name = "[lowServiceName]service"
342}
343
344ohos_executable("[lowServiceName]client") {
345  sources = [
346    "//[lowServiceName]service/src/[iServiceCppFile]",
347    "//[lowServiceName]service/src/[proxyCppFile]",
348    "//[lowServiceName]service/src/[clientCppFile]"
349  ]
350
351  include_dirs = [
352    "//[lowServiceName]service/include",
353    "//[lowServiceName]service/interface",
354    "//utils/native/base/include"
355  ]
356
357  deps = [
358    "//utils/native/base:utils",
359  ]
360
361  external_deps = [
362    "hiviewdfx_hilog_native:libhilog",
363    "ipc:ipc_core",
364    "samgr_standard:samgr_proxy",
365  ]
366
367  part_name = "[lowServiceName]service_part"
368  subsystem_name = "[lowServiceName]service"
369}
370`;
371
372let buildGnTemplate41 = `import("//build/ohos.gni")
373
374ohos_shared_library("[lowServiceName]service") {
375  sources = [
376    "//[lowServiceName]service/src/[iServiceCppFile]",
377    "//[lowServiceName]service/src/[stubCppFile]",
378    "//[lowServiceName]service/src/[serviceCppFile]"
379  ]
380  include_dirs = [
381    "//[lowServiceName]service/include",
382    "//[lowServiceName]service/interface",
383    "//commonlibrary/c_utils/base/include",
384    "//base/startup/init/interfaces/innerkits/include/syspara"
385  ]
386
387  external_deps = [
388    "hilog:libhilog",
389    "ipc:ipc_core",
390    "safwk:system_ability_fwk",
391    "samgr:samgr_proxy",
392    "c_utils:utils",
393  ]
394
395  part_name = "[lowServiceName]service_part"
396  subsystem_name = "[lowServiceName]service"
397}
398
399ohos_executable("[lowServiceName]client") {
400  sources = [
401    "//[lowServiceName]service/src/[iServiceCppFile]",
402    "//[lowServiceName]service/src/[proxyCppFile]",
403    "//[lowServiceName]service/src/[clientCppFile]"
404  ]
405
406  include_dirs = [
407    "//[lowServiceName]service/include",
408    "//[lowServiceName]service/interface",
409    "//commonlibrary/c_utils/base/include"
410  ]
411
412  external_deps = [
413    "hilog:libhilog",
414    "ipc:ipc_core",
415    "samgr:samgr_proxy",
416    "c_utils:utils",
417  ]
418
419  part_name = "[lowServiceName]service_part"
420  subsystem_name = "[lowServiceName]service"
421}
422`;
423
424let bundleJsonTemplate = `{
425    "name": "@ohos/[lowServiceName]service",
426    "description": "system ability framework test",
427    "homePage": "https://gitee.com/",
428    "version": "3.1",
429    "license": "Apache License 2.0",
430    "repository": "",
431    "publishAs": "code-segment",
432    "segment": {
433        "destPath": "[lowServiceName]service"
434    },
435    "dirs": {},
436    "scripts": {},
437    "component": {
438        "name": "[lowServiceName]service_part",
439        "subsystem": "[lowServiceName]service",
440        "adapted_system_type": [
441            "standard"
442        ],
443        "rom": "2048KB",
444        "ram": "~4096KB",
445        "deps": {
446            "components": [
447                "hiviewdfx_hilog_native",
448                "ipc",
449                "samgr_standard",
450                "utils_base",
451                "safwk",
452                "startup_l2"
453            ],
454            "third_party": [ "libxml2" ]
455        },
456        "build": {
457            "sub_component": [
458                "//[lowServiceName]service:[lowServiceName]service",
459                "//[lowServiceName]service/sa_profile:[lowServiceName]service_sa_profile",
460                "//[lowServiceName]service:[lowServiceName]client",
461                "//[lowServiceName]service/etc:[lowServiceName]_service_init"
462            ],
463            "inner_kits": [
464            ],
465            "test": [
466            ]
467        }
468    }
469}`;
470
471let bundleJsonTemplate41 = `{
472    "name": "@ohos/[lowServiceName]service_part",
473    "description": "system ability framework test",
474    "homePage": "https://gitee.com/",
475    "version": "4.1",
476    "license": "Apache License 2.0",
477    "repository": "",
478    "publishAs": "code-segment",
479    "segment": {
480        "destPath": "[lowServiceName]service"
481    },
482    "dirs": {},
483    "scripts": {},
484    "component": {
485        "name": "[lowServiceName]service_part",
486        "subsystem": "[lowServiceName]service",
487        "adapted_system_type": [
488            "standard"
489        ],
490        "rom": "2048KB",
491        "ram": "~4096KB",
492        "deps": {
493            "components": [
494                "hilog",
495                "ipc",
496                "samgr",
497                "c_utils",
498                "safwk"
499            ],
500            "third_party": [ "libxml2" ]
501        },
502        "build": {
503            "sub_component": [
504                "//[lowServiceName]service:[lowServiceName]service",
505                "//[lowServiceName]service/sa_profile:[lowServiceName]service_sa_profile",
506                "//[lowServiceName]service:[lowServiceName]client",
507                "//[lowServiceName]service/etc:[lowServiceName]_service_init"
508            ],
509            "inner_kits": [
510            ],
511            "test": [
512            ]
513        }
514    }
515}`;
516
517let profileGnTemplate = `import("//build/ohos.gni")
518import("//build/ohos/sa_profile/sa_profile.gni")
519
520ohos_sa_profile("[lowServiceName]service_sa_profile") {
521  sources = [ "[serviceId].xml" ]
522
523  part_name = "[lowServiceName]service_part"
524}
525`;
526
527let profileGnTemplate41 = `import("//build/ohos.gni")
528import("//build/ohos/sa_profile/sa_profile.gni")
529
530ohos_sa_profile("[lowServiceName]service_sa_profile") {
531  sources = [ "[serviceId].json" ]
532
533  part_name = "[lowServiceName]service_part"
534}
535`;
536
537let profileXmlTemplate = `<?xml version="1.0" encoding="utf-8"?>
538<info>
539    <process>[lowServiceName]service_sa</process>
540    <systemability>
541        <name>[serviceId]</name>
542        <libpath>lib[lowServiceName]service.z.so</libpath>
543        <run-on-create>true</run-on-create>
544        <distributed>false</distributed>
545        <dump-level>1</dump-level>
546    </systemability>
547</info>
548`;
549
550let profileJsonTemplate = `{
551  "process":"[lowServiceName]service_sa",
552      "systemability":[
553          {
554              "name":[serviceId],
555              "libpath":"lib[lowServiceName]service.z.so",
556              "run-on-create":false,
557              "auto-restart":true,
558              "distributed":false,
559              "dump-level":1
560          }
561      ]
562}`;
563
564let serviceCfgTemplate = `{
565    "services" : [{
566            "name" : "[lowServiceName]service",
567            "path" : ["/system/bin/sa_main", "/system/profile/[lowServiceName]service_sa.xml"],
568            "uid" : "system",
569            "gid" : ["system", "shell"]
570        }
571    ]
572}
573`;
574
575let serviceCfgTemplate41 = `{
576    "services" : [{
577            "name" : "[lowServiceName]service",
578            "path" : ["/system/bin/sa_main", "/system/profile/[lowServiceName]service_sa.json"],
579            "uid" : "system",
580            "gid" : ["system", "shell"]
581        }
582    ]
583}
584`;
585
586let serviceCfgGnTemplate = `import("//build/ohos.gni")
587
588ohos_prebuilt_etc("[lowServiceName]_service_init") {
589  source = "[lowServiceName]_service.cfg"
590  relative_install_dir = "init"
591  part_name = "[lowServiceName]service_part"
592  subsystem_name = "[lowServiceName]service"
593}
594`;
595
596let iServiceCppTemplate = `#include "[iServiceHInclude]"
597`;
598
599module.exports = {
600    iServiceHTemplate,
601    proxyHTemplate,
602    stubHTemplate,
603    serviceHTemplate,
604    proxyCppTemplate,
605    proxyFuncTemplate,
606    stubCppTemplate,
607    stubInnerFuncTemplate,
608    serviceCppTemplate,
609    serviceFuncImplTemplate,
610    clientCppTemplate,
611    buildGnTemplate,
612    buildGnTemplate41,
613    bundleJsonTemplate,
614    bundleJsonTemplate41,
615    profileGnTemplate,
616    profileGnTemplate41,
617    profileXmlTemplate,
618    profileJsonTemplate,
619    serviceCfgTemplate,
620    serviceCfgTemplate41,
621    serviceCfgGnTemplate,
622    iServiceCppTemplate
623};