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 "NodeISayHello.h"
16 #include "../generatorCode/napitest.h"
17 #include "hilog/log.h"
18 static 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
22 namespace napitest {
23 // 1. 打印from, to, enum sayType的值
24 // 2. 调用注册的NodeISayHelloListenerSayHelloStart(info: SayInfo)方法
25 // 工具提供的业务接口(回调) void NodeISayHello::SayHelloListenerSayHelloStartCallback(SayInfo& info)
26 // 3. 调用注册的NodeISayHelloListenerSayHelloEnd(info: SayInfo)方法
27 // 工具提供的业务接口(回调) void NodeISayHello::SayHelloListenerSayHelloEndCallback(SayInfo& info)
sayHello(std::string & from,std::string & to,uint32_t & sayType)28 void NodeISayHello::sayHello(std::string& from, std::string& to, uint32_t& sayType)
29 {
30 // 1.打印
31 NAPITEST_LOGI("NAPITEST_LOGI sayHello from = %s\r\n", from.c_str());
32 NAPITEST_LOGI("NAPITEST_LOGI sayHello to = %s\r\n", to.c_str());
33 NAPITEST_LOGI("NAPITEST_LOGI sayHello sayType = %d\r\n", sayType);
34
35 // 2.调用回调
36 napitest::napitest_interface::SayInfo info1;
37 info1.from = "js1";
38 uint32_t a = 992;
39 info1.fromId.emplace(a);
40 uint32_t b = 1014;
41 info1.toId.emplace(b);
42 info1.to = "native1";
43 info1.content = "hello1";
44 info1.saidTime = "123456789";
45 info1.isEnd = false;
46
47 napitest::napitest_interface::SayInfo info2;
48 info2.from = "native";
49 uint32_t c = 101;
50 info2.fromId.emplace(c);
51 uint32_t d = 99;
52 info2.toId.emplace(d);
53 info2.to = "js";
54 info2.content = "hello";
55 info2.saidTime = "987654321";
56 info2.isEnd = true;
57 // 业务代码调用 onSayHelloStart callback
58 NAPITEST_LOGI("NAPITEST_LOGI NodeISayHelloListener_onSayHelloStartCallback begin\r\n");
59 napitest::napitest_interface::NodeISayHello::listener_.NodeISayHelloListener_onSayHelloStartCallback(info1);
60 NAPITEST_LOGI("NAPITEST_LOGI NodeISayHelloListener_onSayHelloStartCallback end\r\n");
61 // 业务代码调用 onSayHelloEnd callback
62 NAPITEST_LOGI("NAPITEST_LOGI NodeISayHelloListener_onSayHelloEndCallback begin\r\n");
63 napitest::napitest_interface::NodeISayHello::listener_.NodeISayHelloListener_onSayHelloEndCallback(info2);
64 NAPITEST_LOGI("NAPITEST_LOGI NodeISayHelloListener_onSayHelloEndCallback end\r\n");
65 return;
66 }
67
68 // 调用register注册的回调
sayHi(std::string & from,std::string & to,uint32_t & sayType)69 void NodeISayHello::sayHi(std::string& from, std::string& to, uint32_t& sayType)
70 {
71 // 1.打印
72 NAPITEST_LOGI("NAPITEST_LOGI sayHi from = %s\r\n", from.c_str());
73 NAPITEST_LOGI("NAPITEST_LOGI sayHi to = %s\r\n", to.c_str());
74 NAPITEST_LOGI("NAPITEST_LOGI sayHi sayType = %d\r\n", sayType);
75 // 2.调用回调
76 napitest::napitest_interface::NodeISayHello *ptr = new napitest::napitest_interface::NodeISayHello();
77 uint32_t callbackNum = 50;
78 ptr->CallbackfuncCallback(callbackNum);
79 delete ptr;
80 return;
81 }
82
83 // 普通函数调用,返回str
funcTest(bool & v)84 std::string funcTest(bool& v)
85 {
86 if (v) {
87 return "ret is true";
88 } else {
89 return "ret is false";
90 }
91 }
92
93 // 1.打印值:from, to 以及枚举enum SayType的值
94 // 2. 将回调值(0, "", "recv hello.")的值传回Js层
sayHelloWithResponse(std::string & from,std::string & to,uint32_t & sayType)95 void NodeISayHello::sayHelloWithResponse(std::string& from, std::string& to, uint32_t& sayType)
96 {
97 // 1.打印
98 NAPITEST_LOGI("NAPITEST_LOGI sayHelloWithResponse from = %s\r\n", from.c_str());
99 NAPITEST_LOGI("NAPITEST_LOGI sayHelloWithResponse to = %s\r\n", to.c_str());
100 NAPITEST_LOGI("NAPITEST_LOGI sayHelloWithResponse sayType = %d\r\n", sayType);
101 // 2.调用promise回调 (0, "", "recv hello.")
102 napitest::napitest_interface::NodeISayHello *p = new napitest::napitest_interface::NodeISayHello();
103 // 调用工具接口将回调传回工具
104 p->auto_interface_5SetCbValue(0, "", "recv hello.");
105 delete p;
106 return;
107 }
108 }
109