xref: /napi_generator/examples/napitest.cpp (revision 8c339a94)
1/*
2* Copyright (c) 2023 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#include "napitest.h"
16#include "napitest_middle.h"
17#include "hilog/log.h"
18static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, 0XD002E00, "NAPITESTNAPILayer"};
19#define NAPITEST_LOGI(fmt, ...) OHOS::HiviewDFX::HiLog::Info(LABEL, \
20    	"%{public}s:%{public}d " fmt, __func__, __LINE__, ##__VA_ARGS__)
21
22namespace napitest {
23namespace napitest_interface {
24NodeISayHelloListener NodeISayHello::listener_ = {};
25bool NodeISayHello::addSayHelloListener(NodeISayHelloListener& listener)
26{
27    NodeISayHello::listener_ = listener;
28    return true;
29}
30
31bool NodeISayHello::removeSayHelloListener(NodeISayHelloListener& listener)
32{
33    return true;
34}
35
36bool NodeISayHello::registerCallbackfunc()
37{
38    return true;
39}
40
41// 供业务调用的回调接口
42void NodeISayHello::CallbackfuncCallback(NUMBER_TYPE_2& wid)
43{
44    std::string eventName = "Callbackfunc";
45    NodeISayHello_middle *ptr = new NodeISayHello_middle();
46    ptr->CallbackfuncCallbackMiddle(eventName,  wid);
47    delete ptr;
48}
49
50bool NodeISayHello::unRegisterCallbackfunc()
51{
52    return true;
53}
54
55bool NodeISayHello::sayHello(std::string& from, std::string& to, NUMBER_TYPE_9& sayType)
56{
57    NAPITEST_LOGI("NAPITEST_LOGI sayHello from = %s\r\n", from.c_str());
58    NAPITEST_LOGI("NAPITEST_LOGI sayHello to = %s\r\n", to.c_str());
59    NAPITEST_LOGI("NAPITEST_LOGI sayHello sayType = %d\r\n", sayType);
60    SayInfo info1;
61    info1.from = "js1";
62    uint32_t a = 992;
63    info1.fromId.emplace(a);
64    uint32_t b = 1014;
65    info1.toId.emplace(b);
66    info1.to = "native1";
67    info1.content = "hello1";
68    info1.saidTime = "123456789";
69    info1.isEnd = false;
70    SayInfo info2;
71    info2.from = "native";
72    uint32_t c = 101;
73    info2.fromId.emplace(c);
74    uint32_t d = 99;
75    info2.toId.emplace(d);
76    info2.to = "js";
77    info2.content = "hello";
78    info2.saidTime = "987654321";
79    info2.isEnd = true;
80    // 业务代码调用 onSayHelloStart callback
81    listener_.NodeISayHelloListener_onSayHelloStartCallback(info1);
82    // 业务代码调用 onSayHelloEnd callback
83    listener_.NodeISayHelloListener_onSayHelloEndCallback(info2);
84    return true;
85}
86
87bool NodeISayHello::sayHi(std::string& from, std::string& to, NUMBER_TYPE_10& sayType)
88{
89    NAPITEST_LOGI("NAPITEST_LOGI sayHi from = %s\r\n", from.c_str());
90    NAPITEST_LOGI("NAPITEST_LOGI sayHi to = %s\r\n", to.c_str());
91    NAPITEST_LOGI("NAPITEST_LOGI sayHi sayType = %d\r\n", sayType);
92    NodeISayHello *ptr = new NodeISayHello();
93    uint32_t callbackNum = 50;
94    ptr->CallbackfuncCallback(callbackNum);
95    delete ptr;
96    return true;
97}
98
99bool NodeISayHello::sayHelloWithResponse(std::string& from, std::string& to, NUMBER_TYPE_11& sayType,
100    uint32_t& outErrCode, AUTO_INTERFACE_5& out)
101{
102    NAPITEST_LOGI("NAPITEST_LOGI sayHelloWithResponse from = %s\r\n", from.c_str());
103    NAPITEST_LOGI("NAPITEST_LOGI sayHelloWithResponse to = %s\r\n", to.c_str());
104    NAPITEST_LOGI("NAPITEST_LOGI sayHelloWithResponse sayType = %d\r\n", sayType);
105    out.errMsg = "";
106    out.response = "rec hello.";
107    out.result = 0;
108    return true;
109}
110
111AUTO_INTERFACE_5 NodeISayHello::auto_interface_5OutRes = {};
112void NodeISayHello::auto_interface_5SetCbValue(NUMBER_TYPE_6 result, std::string errMsg, std::string response)
113{
114    NodeISayHello::auto_interface_5OutRes.result = result;
115    NodeISayHello::auto_interface_5OutRes.errMsg = errMsg;
116    NodeISayHello::auto_interface_5OutRes.response = response;
117    return;
118}
119
120bool NodeISayHelloListener::onSayHelloStart()
121{
122    return true;
123}
124
125// 供业务调用的回调接口
126void NodeISayHelloListener::NodeISayHelloListener_onSayHelloStartCallback(SayInfo& info)
127{
128    std::string eventName = "NodeISayHelloListener_onSayHelloStart";
129    NodeISayHelloListener_middle *ptr = new NodeISayHelloListener_middle();
130    ptr->NodeISayHelloListener_onSayHelloStartCallbackMiddle(eventName,  info);
131    delete ptr;
132}
133
134bool NodeISayHelloListener::onSayHelloEnd()
135{
136    return true;
137}
138
139// 供业务调用的回调接口
140void NodeISayHelloListener::NodeISayHelloListener_onSayHelloEndCallback(SayInfo& info)
141{
142    std::string eventName = "NodeISayHelloListener_onSayHelloEnd";
143    NodeISayHelloListener_middle *ptr = new NodeISayHelloListener_middle();
144    ptr->NodeISayHelloListener_onSayHelloEndCallbackMiddle(eventName,  info);
145    delete ptr;
146}
147
148bool funcTest(bool& v, std::string& out)
149{
150    if (v) {
151        out = "ret is true";
152    } else {
153        out = "ret is false";
154    }
155    return true;
156}
157}
158}
159