1/*
2* Copyright (c) 2024 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
19#undef LOG_DOMAIN
20#undef LOG_TAG
21#define LOG_DOMAIN 0x3200  // 全局domain宏,标识业务领域
22#define LOG_TAG "MY_TAG"   // 全局tag宏,标识模块日志tag
23#define OH_LOG_INFO(type, ...) ((void)OH_LOG_Print((type), LOG_INFO, LOG_DOMAIN, LOG_TAG, __VA_ARGS__))
24
25    namespace napitest {
26  // 1. 打印from, to, enum sayType的值
27  // 2. 调用注册的NodeISayHelloListenerSayHelloStart(info: SayInfo)方法
28  //    工具提供的业务接口(回调) void NodeISayHello::SayHelloListenerSayHelloStartCallback(SayInfo& info)
29  // 3. 调用注册的NodeISayHelloListenerSayHelloEnd(info: SayInfo)方法
30  //    工具提供的业务接口(回调) void NodeISayHello::SayHelloListenerSayHelloEndCallback(SayInfo& info)
31void NodeISayHello::sayHello(std::string& from, std::string& to, uint32_t& sayType)
32{
33    // 1.打印
34    OH_LOG_INFO(LOG_APP, "NAPITEST_LOGI sayHello from=%{public}s", from.c_str());
35    OH_LOG_INFO(LOG_APP, "NAPITEST_LOGI sayHello to=%{public}s", to.c_str());
36    OH_LOG_INFO(LOG_APP, "NAPITEST_LOGI sayHello sayType=%{public}d", sayType);
37
38    // 2.调用回调
39    napitest::napitest_interface::SayInfo info1;
40    info1.from = "js";
41    uint32_t a = 99;
42    info1.fromId.emplace(a);
43    uint32_t b = 101;
44    info1.toId.emplace(b);
45    info1.to = "native";
46    info1.content = "hello";
47    info1.saidTime = "123456789";
48    info1.isEnd = false;
49
50    napitest::napitest_interface::SayInfo info2;
51    info2.from = "native";
52    uint32_t c = 101;
53    info2.fromId.emplace(c);
54    uint32_t d = 99;
55    info2.toId.emplace(d);
56    info2.to = "js";
57    info2.content = "hi";
58    info2.saidTime = "987654321";
59    info2.isEnd = true;
60    // 业务代码调用 onSayHelloStart callback
61    OH_LOG_INFO(LOG_APP, "NAPITEST_LOGI NodeISayHelloListener_onSayHelloStartCallback begin");
62    napitest::napitest_interface::NodeISayHello::listener_.NodeISayHelloListener_onSayHelloStartCallback(info1);
63    OH_LOG_INFO(LOG_APP, "NAPITEST_LOGI NodeISayHelloListener_onSayHelloStartCallback end");
64    // 业务代码调用 onSayHelloEnd callback
65    OH_LOG_INFO(LOG_APP, "NAPITEST_LOGI NodeISayHelloListener_onSayHelloEndCallback begin");
66    napitest::napitest_interface::NodeISayHello::listener_.NodeISayHelloListener_onSayHelloEndCallback(info2);
67    OH_LOG_INFO(LOG_APP, "NAPITEST_LOGI NodeISayHelloListener_onSayHelloEndCallback end");
68    return;
69}
70
71// 调用register注册的回调
72void NodeISayHello::sayHi(std::string& from, std::string& to, uint32_t& sayType)
73{
74    // 1.打印
75    OH_LOG_INFO(LOG_APP, "NAPITEST_LOGI sayHi from=%{public}s", from.c_str());
76    OH_LOG_INFO(LOG_APP, "NAPITEST_LOGI sayHi to=%{public}s", to.c_str());
77    OH_LOG_INFO(LOG_APP, "NAPITEST_LOGI sayHi sayType=%{public}d", sayType);
78    // 2.调用回调
79    napitest::napitest_interface::NodeISayHello *ptr = new napitest::napitest_interface::NodeISayHello();
80    uint32_t callbackNum = 58;
81    ptr->CallbackfuncCallback(callbackNum);
82    delete ptr;
83    return;
84}
85
86// 普通函数调用,返回str
87std::string funcTest(bool& v)
88{
89    if (v) {
90        return "true";
91    } else {
92        return "false";
93    }
94}
95
96// 1.打印值:from, to 以及枚举enum SayType的值
97// 2. 将回调值(0, "", "recv hello.")的值传回Js层
98void NodeISayHello::sayHelloWithResponse(std::string& from, std::string& to, uint32_t& sayType)
99{
100    // 1.打印
101    OH_LOG_INFO(LOG_APP, "NAPITEST_LOGI sayHelloWithResponse from=%{public}s", from.c_str());
102    OH_LOG_INFO(LOG_APP, "NAPITEST_LOGI sayHelloWithResponse to=%{public}s", to.c_str());
103    OH_LOG_INFO(LOG_APP, "NAPITEST_LOGI sayHelloWithResponse sayType=%{public}d", sayType);
104    // 2.调用promise回调 (0, "", "recv hello.")
105    napitest::napitest_interface::NodeISayHello *p = new  napitest::napitest_interface::NodeISayHello();
106    // 调用工具接口将回调传回工具
107    p->auto_interface_5SetCbValue(0, "no err", "recv hello.");
108    delete p;
109    return;
110}
111}