1 /*
2  * Copyright (c) 2021-2023 Huawei Device 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 
16 #include "client_trans_tcp_direct_callback.h"
17 
18 #include <stddef.h>
19 #include <securec.h>
20 
21 #include "softbus_def.h"
22 #include "softbus_errcode.h"
23 #include "client_trans_tcp_direct_manager.h"
24 #include "client_trans_tcp_direct_message.h"
25 #include "trans_log.h"
26 #include "client_trans_tcp_direct_listener.h"
27 
28 static IClientSessionCallBack g_sessionCb;
29 
ClientTransTdcSetCallBack(const IClientSessionCallBack * cb)30 int32_t ClientTransTdcSetCallBack(const IClientSessionCallBack *cb)
31 {
32     if (cb == NULL) {
33         TRANS_LOGE(TRANS_SDK, "cb null.");
34         return SOFTBUS_INVALID_PARAM;
35     }
36     g_sessionCb = *cb;
37     return SOFTBUS_OK;
38 }
39 
ClientTransTdcOnSessionOpened(const char * sessionName,const ChannelInfo * channel)40 int32_t ClientTransTdcOnSessionOpened(const char *sessionName, const ChannelInfo *channel)
41 {
42     return g_sessionCb.OnSessionOpened(sessionName, channel, TYPE_BYTES);
43 }
44 
ClientTransTdcOnSessionClosed(int32_t channelId,ShutdownReason reason)45 int32_t ClientTransTdcOnSessionClosed(int32_t channelId, ShutdownReason reason)
46 {
47     (void)TransDelDataBufNode(channelId);
48     (void)TransTdcCloseChannel(channelId);
49     return g_sessionCb.OnSessionClosed(channelId, CHANNEL_TYPE_TCP_DIRECT, reason);
50 }
51 
ClientTransTdcOnSessionOpenFailed(int32_t channelId,int32_t errCode)52 int32_t ClientTransTdcOnSessionOpenFailed(int32_t channelId, int32_t errCode)
53 {
54     return g_sessionCb.OnSessionOpenFailed(channelId, CHANNEL_TYPE_TCP_DIRECT, errCode);
55 }
56 
ClientTransTdcOnDataReceived(int32_t channelId,const void * data,uint32_t len,SessionPktType type)57 int32_t ClientTransTdcOnDataReceived(int32_t channelId,
58     const void *data, uint32_t len, SessionPktType type)
59 {
60     return g_sessionCb.OnDataReceived(channelId, CHANNEL_TYPE_TCP_DIRECT, data, len, type);
61 }
62 
ClientTransTdcOnChannelBind(int32_t channelId,int32_t channelType)63 int32_t ClientTransTdcOnChannelBind(int32_t channelId, int32_t channelType)
64 {
65     if (g_sessionCb.OnChannelBind == NULL) {
66         TRANS_LOGW(TRANS_SDK, "OnChannelBind is null channelId=%{public}d", channelId);
67         return SOFTBUS_INVALID_PARAM;
68     }
69 
70     int32_t ret = g_sessionCb.OnChannelBind(channelId, channelType);
71     if (ret == SOFTBUS_NOT_NEED_UPDATE) {
72         return SOFTBUS_OK;
73     }
74     if (ret != SOFTBUS_OK) {
75         return ret;
76     }
77 
78     TcpDirectChannelInfo info;
79     (void)memset_s(&info, sizeof(TcpDirectChannelInfo), 0, sizeof(TcpDirectChannelInfo));
80     TcpDirectChannelInfo *res = TransTdcGetInfoById(channelId, &info);
81     if (res == NULL) {
82         TRANS_LOGE(TRANS_SDK, "TransTdcGetInfoById failed, channelId=%{public}d", channelId);
83         return SOFTBUS_NOT_FIND;
84     }
85 
86     if (info.detail.needStopListener) {
87         TRANS_LOGI(TRANS_SDK, "info.detail.needStopListener is true, channelId=%{public}d", channelId);
88         return SOFTBUS_OK;
89     }
90 
91     ret = TransTdcCreateListener(info.detail.fd);
92     if (ret != SOFTBUS_OK) {
93         TRANS_LOGE(TRANS_SDK, "TransTdcCreateListener failed, channelId=%{public}d", channelId);
94         g_sessionCb.OnSessionClosed(channelId, CHANNEL_TYPE_TCP_DIRECT, SHUTDOWN_REASON_LOCAL);
95         return ret;
96     }
97 
98     return SOFTBUS_OK;
99 }
100 
ClientTransTdcIfChannelForSocket(const char * sessionName,bool * isSocket)101 int32_t ClientTransTdcIfChannelForSocket(const char *sessionName, bool *isSocket)
102 {
103     return g_sessionCb.IfChannelForSocket(sessionName, isSocket);
104 }
105