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_auth_manager.h"
17 #include "softbus_def.h"
18 #include "softbus_errcode.h"
19 #include "trans_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 TRANS_LOGE(TRANS_SDK, "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 TRANS_LOGE(TRANS_SDK, "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 TRANS_LOGE(TRANS_SDK, "notify session open fail, ret=%{public}d", ret);
44 return ret;
45 }
46
47 return SOFTBUS_OK;
48 }
49
ClientTransAuthOnChannelClosed(int32_t channelId,ShutdownReason reason)50 int32_t ClientTransAuthOnChannelClosed(int32_t channelId, ShutdownReason reason)
51 {
52 int ret = g_sessionCb.OnSessionClosed(channelId, CHANNEL_TYPE_AUTH, reason);
53 if (ret != SOFTBUS_OK) {
54 TRANS_LOGE(TRANS_SDK, "notify session open fail. ret=%{public}d, channelId=%{public}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 TRANS_LOGE(TRANS_SDK,
65 "notify session open fail. ret=%{public}d, errCode=%{public}d, channelId=%{public}d",
66 ret, errCode, channelId);
67 return ret;
68 }
69
70 return SOFTBUS_OK;
71 }
72
ClientTransAuthOnDataReceived(int32_t channelId,const void * data,uint32_t len,SessionPktType type)73 int32_t ClientTransAuthOnDataReceived(int32_t channelId,
74 const void *data, uint32_t len, SessionPktType type)
75 {
76 if (data == NULL) {
77 TRANS_LOGE(TRANS_SDK, "param invalid");
78 return SOFTBUS_INVALID_PARAM;
79 }
80 int ret = g_sessionCb.OnDataReceived(channelId, CHANNEL_TYPE_AUTH, data, len, type);
81 if (ret != SOFTBUS_OK) {
82 TRANS_LOGE(TRANS_SDK, "notify data recv err, ret=%{public}d, channelId=%{public}d", ret, channelId);
83 return ret;
84 }
85 return SOFTBUS_OK;
86 }
87
ClientTransAuthCloseChannel(int32_t channelId,ShutdownReason reason)88 void ClientTransAuthCloseChannel(int32_t channelId, ShutdownReason reason)
89 {
90 TRANS_LOGI(TRANS_SDK, "TransCloseAuthChannel, channelId=%{public}d", channelId);
91 if (ServerIpcCloseChannel(channelId, CHANNEL_TYPE_AUTH) != SOFTBUS_OK) {
92 TRANS_LOGE(TRANS_SDK, "server ipc close err. channelId=%{public}d", channelId);
93 }
94 if (ClientTransAuthOnChannelClosed(channelId, reason) != SOFTBUS_OK) {
95 TRANS_LOGE(TRANS_SDK, "server auth close err. channelId=%{public}d", channelId);
96 }
97 }
98
TransAuthChannelSendBytes(int32_t channelId,const void * data,uint32_t len)99 int32_t TransAuthChannelSendBytes(int32_t channelId, const void *data, uint32_t len)
100 {
101 int ret = ServerIpcSendMessage(channelId, CHANNEL_TYPE_AUTH, data, len, TRANS_SESSION_BYTES);
102 TRANS_LOGI(TRANS_BYTES, "send bytes: channelId=%{public}d, ret=%{public}d", channelId, ret);
103 return ret;
104 }
105
TransAuthChannelSendMessage(int32_t channelId,const void * data,uint32_t len)106 int32_t TransAuthChannelSendMessage(int32_t channelId, const void *data, uint32_t len)
107 {
108 int ret = ServerIpcSendMessage(channelId, CHANNEL_TYPE_AUTH, data, len, TRANS_SESSION_BYTES);
109 TRANS_LOGI(TRANS_MSG, "send msg: channelId=%{public}d, ret=%{public}d", channelId, ret);
110 return ret;
111 }