xref: /napi_generator/examples/napitest.cpp (revision 8c339a94)
18c339a94Sopenharmony_ci/*
28c339a94Sopenharmony_ci* Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development Co., Ltd.
38c339a94Sopenharmony_ci* Licensed under the Apache License, Version 2.0 (the "License");
48c339a94Sopenharmony_ci* you may not use this file except in compliance with the License.
58c339a94Sopenharmony_ci* You may obtain a copy of the License at
68c339a94Sopenharmony_ci*
78c339a94Sopenharmony_ci* http://www.apache.org/licenses/LICENSE-2.0
88c339a94Sopenharmony_ci*
98c339a94Sopenharmony_ci* Unless required by applicable law or agreed to in writing, software
108c339a94Sopenharmony_ci* distributed under the License is distributed on an "AS IS" BASIS,
118c339a94Sopenharmony_ci* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
128c339a94Sopenharmony_ci* See the License for the specific language governing permissions and
138c339a94Sopenharmony_ci* limitations under the License.
148c339a94Sopenharmony_ci*/
158c339a94Sopenharmony_ci#include "napitest.h"
168c339a94Sopenharmony_ci#include "napitest_middle.h"
178c339a94Sopenharmony_ci#include "hilog/log.h"
188c339a94Sopenharmony_cistatic constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, 0XD002E00, "NAPITESTNAPILayer"};
198c339a94Sopenharmony_ci#define NAPITEST_LOGI(fmt, ...) OHOS::HiviewDFX::HiLog::Info(LABEL, \
208c339a94Sopenharmony_ci    	"%{public}s:%{public}d " fmt, __func__, __LINE__, ##__VA_ARGS__)
218c339a94Sopenharmony_ci
228c339a94Sopenharmony_cinamespace napitest {
238c339a94Sopenharmony_cinamespace napitest_interface {
248c339a94Sopenharmony_ciNodeISayHelloListener NodeISayHello::listener_ = {};
258c339a94Sopenharmony_cibool NodeISayHello::addSayHelloListener(NodeISayHelloListener& listener)
268c339a94Sopenharmony_ci{
278c339a94Sopenharmony_ci    NodeISayHello::listener_ = listener;
288c339a94Sopenharmony_ci    return true;
298c339a94Sopenharmony_ci}
308c339a94Sopenharmony_ci
318c339a94Sopenharmony_cibool NodeISayHello::removeSayHelloListener(NodeISayHelloListener& listener)
328c339a94Sopenharmony_ci{
338c339a94Sopenharmony_ci    return true;
348c339a94Sopenharmony_ci}
358c339a94Sopenharmony_ci
368c339a94Sopenharmony_cibool NodeISayHello::registerCallbackfunc()
378c339a94Sopenharmony_ci{
388c339a94Sopenharmony_ci    return true;
398c339a94Sopenharmony_ci}
408c339a94Sopenharmony_ci
418c339a94Sopenharmony_ci// 供业务调用的回调接口
428c339a94Sopenharmony_civoid NodeISayHello::CallbackfuncCallback(NUMBER_TYPE_2& wid)
438c339a94Sopenharmony_ci{
448c339a94Sopenharmony_ci    std::string eventName = "Callbackfunc";
458c339a94Sopenharmony_ci    NodeISayHello_middle *ptr = new NodeISayHello_middle();
468c339a94Sopenharmony_ci    ptr->CallbackfuncCallbackMiddle(eventName,  wid);
478c339a94Sopenharmony_ci    delete ptr;
488c339a94Sopenharmony_ci}
498c339a94Sopenharmony_ci
508c339a94Sopenharmony_cibool NodeISayHello::unRegisterCallbackfunc()
518c339a94Sopenharmony_ci{
528c339a94Sopenharmony_ci    return true;
538c339a94Sopenharmony_ci}
548c339a94Sopenharmony_ci
558c339a94Sopenharmony_cibool NodeISayHello::sayHello(std::string& from, std::string& to, NUMBER_TYPE_9& sayType)
568c339a94Sopenharmony_ci{
578c339a94Sopenharmony_ci    NAPITEST_LOGI("NAPITEST_LOGI sayHello from = %s\r\n", from.c_str());
588c339a94Sopenharmony_ci    NAPITEST_LOGI("NAPITEST_LOGI sayHello to = %s\r\n", to.c_str());
598c339a94Sopenharmony_ci    NAPITEST_LOGI("NAPITEST_LOGI sayHello sayType = %d\r\n", sayType);
608c339a94Sopenharmony_ci    SayInfo info1;
618c339a94Sopenharmony_ci    info1.from = "js1";
628c339a94Sopenharmony_ci    uint32_t a = 992;
638c339a94Sopenharmony_ci    info1.fromId.emplace(a);
648c339a94Sopenharmony_ci    uint32_t b = 1014;
658c339a94Sopenharmony_ci    info1.toId.emplace(b);
668c339a94Sopenharmony_ci    info1.to = "native1";
678c339a94Sopenharmony_ci    info1.content = "hello1";
688c339a94Sopenharmony_ci    info1.saidTime = "123456789";
698c339a94Sopenharmony_ci    info1.isEnd = false;
708c339a94Sopenharmony_ci    SayInfo info2;
718c339a94Sopenharmony_ci    info2.from = "native";
728c339a94Sopenharmony_ci    uint32_t c = 101;
738c339a94Sopenharmony_ci    info2.fromId.emplace(c);
748c339a94Sopenharmony_ci    uint32_t d = 99;
758c339a94Sopenharmony_ci    info2.toId.emplace(d);
768c339a94Sopenharmony_ci    info2.to = "js";
778c339a94Sopenharmony_ci    info2.content = "hello";
788c339a94Sopenharmony_ci    info2.saidTime = "987654321";
798c339a94Sopenharmony_ci    info2.isEnd = true;
808c339a94Sopenharmony_ci    // 业务代码调用 onSayHelloStart callback
818c339a94Sopenharmony_ci    listener_.NodeISayHelloListener_onSayHelloStartCallback(info1);
828c339a94Sopenharmony_ci    // 业务代码调用 onSayHelloEnd callback
838c339a94Sopenharmony_ci    listener_.NodeISayHelloListener_onSayHelloEndCallback(info2);
848c339a94Sopenharmony_ci    return true;
858c339a94Sopenharmony_ci}
868c339a94Sopenharmony_ci
878c339a94Sopenharmony_cibool NodeISayHello::sayHi(std::string& from, std::string& to, NUMBER_TYPE_10& sayType)
888c339a94Sopenharmony_ci{
898c339a94Sopenharmony_ci    NAPITEST_LOGI("NAPITEST_LOGI sayHi from = %s\r\n", from.c_str());
908c339a94Sopenharmony_ci    NAPITEST_LOGI("NAPITEST_LOGI sayHi to = %s\r\n", to.c_str());
918c339a94Sopenharmony_ci    NAPITEST_LOGI("NAPITEST_LOGI sayHi sayType = %d\r\n", sayType);
928c339a94Sopenharmony_ci    NodeISayHello *ptr = new NodeISayHello();
938c339a94Sopenharmony_ci    uint32_t callbackNum = 50;
948c339a94Sopenharmony_ci    ptr->CallbackfuncCallback(callbackNum);
958c339a94Sopenharmony_ci    delete ptr;
968c339a94Sopenharmony_ci    return true;
978c339a94Sopenharmony_ci}
988c339a94Sopenharmony_ci
998c339a94Sopenharmony_cibool NodeISayHello::sayHelloWithResponse(std::string& from, std::string& to, NUMBER_TYPE_11& sayType,
1008c339a94Sopenharmony_ci    uint32_t& outErrCode, AUTO_INTERFACE_5& out)
1018c339a94Sopenharmony_ci{
1028c339a94Sopenharmony_ci    NAPITEST_LOGI("NAPITEST_LOGI sayHelloWithResponse from = %s\r\n", from.c_str());
1038c339a94Sopenharmony_ci    NAPITEST_LOGI("NAPITEST_LOGI sayHelloWithResponse to = %s\r\n", to.c_str());
1048c339a94Sopenharmony_ci    NAPITEST_LOGI("NAPITEST_LOGI sayHelloWithResponse sayType = %d\r\n", sayType);
1058c339a94Sopenharmony_ci    out.errMsg = "";
1068c339a94Sopenharmony_ci    out.response = "rec hello.";
1078c339a94Sopenharmony_ci    out.result = 0;
1088c339a94Sopenharmony_ci    return true;
1098c339a94Sopenharmony_ci}
1108c339a94Sopenharmony_ci
1118c339a94Sopenharmony_ciAUTO_INTERFACE_5 NodeISayHello::auto_interface_5OutRes = {};
1128c339a94Sopenharmony_civoid NodeISayHello::auto_interface_5SetCbValue(NUMBER_TYPE_6 result, std::string errMsg, std::string response)
1138c339a94Sopenharmony_ci{
1148c339a94Sopenharmony_ci    NodeISayHello::auto_interface_5OutRes.result = result;
1158c339a94Sopenharmony_ci    NodeISayHello::auto_interface_5OutRes.errMsg = errMsg;
1168c339a94Sopenharmony_ci    NodeISayHello::auto_interface_5OutRes.response = response;
1178c339a94Sopenharmony_ci    return;
1188c339a94Sopenharmony_ci}
1198c339a94Sopenharmony_ci
1208c339a94Sopenharmony_cibool NodeISayHelloListener::onSayHelloStart()
1218c339a94Sopenharmony_ci{
1228c339a94Sopenharmony_ci    return true;
1238c339a94Sopenharmony_ci}
1248c339a94Sopenharmony_ci
1258c339a94Sopenharmony_ci// 供业务调用的回调接口
1268c339a94Sopenharmony_civoid NodeISayHelloListener::NodeISayHelloListener_onSayHelloStartCallback(SayInfo& info)
1278c339a94Sopenharmony_ci{
1288c339a94Sopenharmony_ci    std::string eventName = "NodeISayHelloListener_onSayHelloStart";
1298c339a94Sopenharmony_ci    NodeISayHelloListener_middle *ptr = new NodeISayHelloListener_middle();
1308c339a94Sopenharmony_ci    ptr->NodeISayHelloListener_onSayHelloStartCallbackMiddle(eventName,  info);
1318c339a94Sopenharmony_ci    delete ptr;
1328c339a94Sopenharmony_ci}
1338c339a94Sopenharmony_ci
1348c339a94Sopenharmony_cibool NodeISayHelloListener::onSayHelloEnd()
1358c339a94Sopenharmony_ci{
1368c339a94Sopenharmony_ci    return true;
1378c339a94Sopenharmony_ci}
1388c339a94Sopenharmony_ci
1398c339a94Sopenharmony_ci// 供业务调用的回调接口
1408c339a94Sopenharmony_civoid NodeISayHelloListener::NodeISayHelloListener_onSayHelloEndCallback(SayInfo& info)
1418c339a94Sopenharmony_ci{
1428c339a94Sopenharmony_ci    std::string eventName = "NodeISayHelloListener_onSayHelloEnd";
1438c339a94Sopenharmony_ci    NodeISayHelloListener_middle *ptr = new NodeISayHelloListener_middle();
1448c339a94Sopenharmony_ci    ptr->NodeISayHelloListener_onSayHelloEndCallbackMiddle(eventName,  info);
1458c339a94Sopenharmony_ci    delete ptr;
1468c339a94Sopenharmony_ci}
1478c339a94Sopenharmony_ci
1488c339a94Sopenharmony_cibool funcTest(bool& v, std::string& out)
1498c339a94Sopenharmony_ci{
1508c339a94Sopenharmony_ci    if (v) {
1518c339a94Sopenharmony_ci        out = "ret is true";
1528c339a94Sopenharmony_ci    } else {
1538c339a94Sopenharmony_ci        out = "ret is false";
1548c339a94Sopenharmony_ci    }
1558c339a94Sopenharmony_ci    return true;
1568c339a94Sopenharmony_ci}
1578c339a94Sopenharmony_ci}
1588c339a94Sopenharmony_ci}
159