1 /*
2 * Copyright (c) 2021 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_auth_manager.h"
17
18 #include "softbus_errcode.h"
19 #include "softbus_log.h"
20 #include "trans_server_proxy.h"
21
22 static IClientSessionCallBack g_sessionCb;
23
ClientTransAuthInit(const IClientSessionCallBack * cb)24 int32_t ClientTransAuthInit(const IClientSessionCallBack *cb)
25 {
26 if (cb == NULL) {
27 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "ClientTransAuthInit cb is null.");
28 return SOFTBUS_INVALID_PARAM;
29 }
30 g_sessionCb = *cb;
31 return SOFTBUS_OK;
32 }
33
ClientTransAuthOnChannelOpened(const char * sessionName,const ChannelInfo * channel)34 int32_t ClientTransAuthOnChannelOpened(const char *sessionName, const ChannelInfo *channel)
35 {
36 if (sessionName == NULL || channel == NULL) {
37 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "ClientTransAuthOnChannelOpened param invalid.");
38 return SOFTBUS_INVALID_PARAM;
39 }
40
41 int ret = g_sessionCb.OnSessionOpened(sessionName, channel, TYPE_MESSAGE);
42 if (ret != SOFTBUS_OK) {
43 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "notify session open fail, ret=%d.", ret);
44 return ret;
45 }
46
47 return SOFTBUS_OK;
48 }
49
ClientTransAuthOnChannelClosed(int32_t channelId)50 int32_t ClientTransAuthOnChannelClosed(int32_t channelId)
51 {
52 int ret = g_sessionCb.OnSessionClosed(channelId, CHANNEL_TYPE_AUTH);
53 if (ret != SOFTBUS_OK) {
54 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "notify session openfail err[%d]. cid[%d].", ret, channelId);
55 return ret;
56 }
57 return SOFTBUS_OK;
58 }
59
ClientTransAuthOnChannelOpenFailed(int32_t channelId,int32_t errCode)60 int32_t ClientTransAuthOnChannelOpenFailed(int32_t channelId, int32_t errCode)
61 {
62 int ret = g_sessionCb.OnSessionOpenFailed(channelId, CHANNEL_TYPE_AUTH, errCode);
63 if (ret != SOFTBUS_OK) {
64 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR,
65 "notify session openfail ret[%d] err[%d], cid[%d].", ret, errCode, channelId);
66 return ret;
67 }
68
69 return SOFTBUS_OK;
70 }
71
ClientTransAuthOnDataReceived(int32_t channelId,const void * data,uint32_t len,SessionPktType type)72 int32_t ClientTransAuthOnDataReceived(int32_t channelId,
73 const void *data, uint32_t len, SessionPktType type)
74 {
75 if (data == NULL) {
76 return SOFTBUS_INVALID_PARAM;
77 }
78 int ret = g_sessionCb.OnDataReceived(channelId, CHANNEL_TYPE_AUTH, data, len, type);
79 if (ret != SOFTBUS_OK) {
80 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "notify data recv err, ret[%d], cId[%d].", ret, channelId);
81 return ret;
82 }
83 return SOFTBUS_OK;
84 }
85
ClientTransAuthCloseChannel(int32_t channelId)86 void ClientTransAuthCloseChannel(int32_t channelId)
87 {
88 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "TransCloseAuthChannel, channelId [%d]", channelId);
89 if (ServerIpcCloseChannel(channelId, CHANNEL_TYPE_AUTH) != SOFTBUS_OK) {
90 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "server ipc close channel[%d] err.", channelId);
91 }
92 if (ClientTransAuthOnChannelClosed(channelId) != SOFTBUS_OK) {
93 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "server auth close channel[%d] err.", channelId);
94 }
95 }
96
TransAuthChannelSendBytes(int32_t channelId,const void * data,uint32_t len)97 int32_t TransAuthChannelSendBytes(int32_t channelId, const void *data, uint32_t len)
98 {
99 int ret = ServerIpcSendMessage(channelId, CHANNEL_TYPE_AUTH, data, len, TRANS_SESSION_BYTES);
100 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "send bytes: channelId=%d, ret=%d", channelId, ret);
101 return ret;
102 }
103
TransAuthChannelSendMessage(int32_t channelId,const void * data,uint32_t len)104 int32_t TransAuthChannelSendMessage(int32_t channelId, const void *data, uint32_t len)
105 {
106 int ret = ServerIpcSendMessage(channelId, CHANNEL_TYPE_AUTH, data, len, TRANS_SESSION_BYTES);
107 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "send msg: channelId=%d, ret=%d", channelId, ret);
108 return ret;
109 }