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