1 /*
2 * Copyright (c) 2021-2024 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 "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 int32_t 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 int32_t 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 int32_t 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, const void *data, uint32_t len, SessionPktType type)
74 {
75 if (data == NULL) {
76 TRANS_LOGE(TRANS_SDK, "param invalid");
77 return SOFTBUS_INVALID_PARAM;
78 }
79 int32_t ret = g_sessionCb.OnDataReceived(channelId, CHANNEL_TYPE_AUTH, data, len, type);
80 if (ret != SOFTBUS_OK) {
81 TRANS_LOGE(TRANS_SDK, "notify data recv err, ret=%{public}d, channelId=%{public}d", ret, channelId);
82 return ret;
83 }
84 return SOFTBUS_OK;
85 }
86
ClientTransAuthCloseChannel(int32_t channelId,ShutdownReason reason)87 void ClientTransAuthCloseChannel(int32_t channelId, ShutdownReason reason)
88 {
89 TRANS_LOGI(TRANS_SDK, "TransCloseAuthChannel, channelId=%{public}d", channelId);
90 if (ServerIpcCloseChannel(NULL, channelId, CHANNEL_TYPE_AUTH) != SOFTBUS_OK) {
91 TRANS_LOGE(TRANS_SDK, "server ipc close err. channelId=%{public}d", channelId);
92 }
93 if (ClientTransAuthOnChannelClosed(channelId, reason) != SOFTBUS_OK) {
94 TRANS_LOGE(TRANS_SDK, "server auth close err. channelId=%{public}d", channelId);
95 }
96 }
97
TransAuthChannelSendBytes(int32_t channelId,const void * data,uint32_t len)98 int32_t TransAuthChannelSendBytes(int32_t channelId, const void *data, uint32_t len)
99 {
100 int32_t ret = ServerIpcSendMessage(channelId, CHANNEL_TYPE_AUTH, data, len, TRANS_SESSION_BYTES);
101 TRANS_LOGI(TRANS_BYTES, "send bytes: channelId=%{public}d, ret=%{public}d", channelId, ret);
102 return ret;
103 }
104
TransAuthChannelSendMessage(int32_t channelId,const void * data,uint32_t len)105 int32_t TransAuthChannelSendMessage(int32_t channelId, const void *data, uint32_t len)
106 {
107 int32_t ret = ServerIpcSendMessage(channelId, CHANNEL_TYPE_AUTH, data, len, TRANS_SESSION_BYTES);
108 TRANS_LOGI(TRANS_MSG, "send msg: channelId=%{public}d, ret=%{public}d", channelId, ret);
109 return ret;
110 }