1 /*
2 * Copyright (c) 2021-2025 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 "trans_client_stub.h"
17
18 #include <securec.h>
19
20 #include "client_trans_channel_callback.h"
21 #include "ipc_skeleton.h"
22 #include "softbus_error_code.h"
23 #include "softbus_server_ipc_interface_code.h"
24 #include "trans_log.h"
25
ClientOnChannelOpened(IpcIo * data,IpcIo * reply)26 int32_t ClientOnChannelOpened(IpcIo *data, IpcIo *reply)
27 {
28 TRANS_CHECK_AND_RETURN_RET_LOGE(data != NULL && reply != NULL, SOFTBUS_INVALID_PARAM, TRANS_CTRL, "invalid param");
29 size_t size = 0;
30 ChannelInfo channel = {0};
31 const char *sessionName = (const char *)ReadString(data, &size);
32 ReadInt32(data, &(channel.channelId));
33 ReadInt32(data, &(channel.channelType));
34 ReadBool(data, &(channel.isServer));
35 ReadBool(data, &(channel.isEnabled));
36 ReadBool(data, &(channel.isEncrypt));
37 ReadInt32(data, &(channel.peerUid));
38 ReadInt32(data, &(channel.peerPid));
39 channel.groupId = (char *)ReadString(data, &size);
40 ReadUint32(data, &(channel.keyLen));
41 channel.sessionKey = (char *)ReadBuffer(data, channel.keyLen);
42 channel.peerSessionName = (char *)ReadString(data, &size);
43 channel.peerDeviceId = (char *)ReadString(data, &size);
44 if (channel.groupId == NULL || channel.sessionKey == NULL || channel.peerSessionName == NULL ||
45 channel.peerDeviceId == NULL) {
46 TRANS_LOGE(TRANS_CTRL, "pointer null error.");
47 return SOFTBUS_NO_INIT;
48 }
49 if (channel.channelType == CHANNEL_TYPE_TCP_DIRECT) {
50 channel.myIp = (char *)ReadString(data, &size);
51 TRANS_CHECK_AND_RETURN_RET_LOGE(channel.myIp != NULL, SOFTBUS_IPC_ERR, TRANS_CTRL, "pointer null error");
52 channel.fd = ReadFileDescriptor(data);
53 }
54 ReadInt32(data, &(channel.businessType));
55 if (channel.channelType == CHANNEL_TYPE_UDP) {
56 channel.myIp = (char *)ReadString(data, &size);
57 ReadInt32(data, &(channel.streamType));
58 ReadBool(data, &(channel.isUdpFile));
59 ReadInt32(data, &(channel.peerPort));
60 channel.peerIp = (char *)ReadString(data, &size);
61 }
62 ReadInt32(data, &(channel.routeType));
63 ReadBool(data, &(channel.isSupportTlv));
64 int ret = TransOnChannelOpened(sessionName, &channel);
65 if (ret < 0) {
66 TRANS_LOGE(TRANS_CTRL, "TransOnChannelOpened fail, errcode=%{public}d.", ret);
67 }
68 return SOFTBUS_OK;
69 }
70
ClientOnChannelOpenfailed(IpcIo * data,IpcIo * reply)71 int32_t ClientOnChannelOpenfailed(IpcIo *data, IpcIo *reply)
72 {
73 if (data == NULL) {
74 TRANS_LOGE(TRANS_CTRL, "invalid param.");
75 return SOFTBUS_INVALID_PARAM;
76 }
77 int32_t channelId = 0;
78 int32_t channelType = 0;
79 int32_t errCode = SOFTBUS_OK;
80 ReadInt32(data, &channelId);
81 ReadInt32(data, &channelType);
82 ReadInt32(data, &errCode);
83 (void)TransOnChannelOpenFailed(channelId, channelType, errCode);
84 return SOFTBUS_OK;
85 }
86
ClientOnChannelClosed(IpcIo * data,IpcIo * reply)87 int32_t ClientOnChannelClosed(IpcIo *data, IpcIo *reply)
88 {
89 if (data == NULL) {
90 TRANS_LOGE(TRANS_CTRL, "invalid param.");
91 return SOFTBUS_INVALID_PARAM;
92 }
93 int32_t channelId = 0;
94 int32_t channelType = 0;
95 int32_t messageType = 0;
96 ReadInt32(data, &channelId);
97 ReadInt32(data, &channelType);
98 ReadInt32(data, &messageType);
99 (void)TransOnChannelClosed(channelId, channelType, messageType, SHUTDOWN_REASON_PEER);
100 return SOFTBUS_OK;
101 }
102
ClientOnChannelMsgreceived(IpcIo * data,IpcIo * reply)103 int32_t ClientOnChannelMsgreceived(IpcIo *data, IpcIo *reply)
104 {
105 if (data == NULL) {
106 TRANS_LOGE(TRANS_CTRL, "invalid param.");
107 return SOFTBUS_INVALID_PARAM;
108 }
109 int32_t channelId = 0;
110 int32_t channelType = 0;
111 int32_t type = 0;
112 ReadInt32(data, &channelId);
113 ReadInt32(data, &channelType);
114 ReadInt32(data, &type);
115 uint32_t dataLen = 0;
116 ReadUint32(data, &dataLen);
117 const uint8_t *data2 = ReadBuffer(data, dataLen);
118 (void)TransOnChannelMsgReceived(channelId, channelType, data2, dataLen, type);
119 return SOFTBUS_OK;
120 }
121
ClientSetChannelInfo(IpcIo * data,IpcIo * reply)122 int32_t ClientSetChannelInfo(IpcIo *data, IpcIo *reply)
123 {
124 if (data == NULL) {
125 TRANS_LOGE(TRANS_CTRL, "invalid param.");
126 return SOFTBUS_INVALID_PARAM;
127 }
128 (void)reply;
129 size_t size = 0;
130 const char *sessionName = (const char *)ReadString(data, &size);
131 int32_t sessionId = 0;
132 int32_t channelId = 0;
133 int32_t channelType = 0;
134 ReadInt32(data, &sessionId);
135 ReadInt32(data, &channelId);
136 ReadInt32(data, &channelType);
137 (void)TransSetChannelInfo(sessionName, sessionId, channelId, channelType);
138 return SOFTBUS_OK;
139 }
140
ClientOnChannelBind(IpcIo * data,IpcIo * reply)141 int32_t ClientOnChannelBind(IpcIo *data, IpcIo *reply)
142 {
143 if (data == NULL) {
144 TRANS_LOGE(TRANS_CTRL, "invalid param.");
145 return SOFTBUS_INVALID_PARAM;
146 }
147 (void)reply;
148 int32_t channelId = 0;
149 int32_t channelType = 0;
150 ReadInt32(data, &channelId);
151 ReadInt32(data, &channelType);
152 int32_t ret = TransOnChannelBind(channelId, channelType);
153 if (ret != SOFTBUS_OK) {
154 COMM_LOGE(COMM_SDK, "OnChannelBindInner failed! ret=%{public}d, channelId=%{public}d.", ret, channelId);
155 }
156 return ret;
157 }
158
ReadCollabInfo(IpcIo * data,CollabInfo * info)159 static int32_t ReadCollabInfo(IpcIo *data, CollabInfo *info)
160 {
161 if (info == NULL) {
162 COMM_LOGE(COMM_SDK, "invalid param.");
163 return SOFTBUS_INVALID_PARAM;
164 }
165 size_t size = 0;
166 char *accountId = (char *)ReadString(data, &size);
167 if (accountId == NULL) {
168 COMM_LOGE(COMM_SDK, "read accountId failed");
169 } else {
170 if (strcpy_s(info->accountId, size, accountId) != EOK) {
171 COMM_LOGE(COMM_SDK, "strcpy_s failed to copy accountId");
172 }
173 }
174 size = 0;
175 ReadUint64(data, &info->tokenId);
176 ReadInt32(data, &info->userId);
177 ReadInt32(data, &info->pid);
178 char *deviceId = (char *)ReadString(data, &size);
179 COMM_CHECK_AND_RETURN_RET_LOGE(deviceId != NULL, SOFTBUS_IPC_ERR, COMM_SDK, "read deviceId failed.");
180 if (strcpy_s(info->deviceId, size, deviceId) != EOK) {
181 COMM_LOGI(COMM_SDK, "strcpy_s failed to copy deviceId.");
182 return SOFTBUS_STRCPY_ERR;
183 }
184 return SOFTBUS_OK;
185 }
186
ClientCheckCollabRelation(IpcIo * data,IpcIo * reply)187 int32_t ClientCheckCollabRelation(IpcIo *data, IpcIo *reply)
188 {
189 if (data == NULL) {
190 COMM_LOGI(COMM_SDK, "invalid param.");
191 return SOFTBUS_INVALID_PARAM;
192 }
193 (void)reply;
194 CollabInfo sourceInfo = { 0 };
195 CollabInfo sinkInfo = { 0 };
196 bool isSinkSide;
197 bool retReadBool = ReadBool(data, &isSinkSide);
198 COMM_CHECK_AND_RETURN_RET_LOGE(
199 retReadBool, SOFTBUS_TRANS_PROXY_READBOOL_FAILED, COMM_SDK, "read isSinkSide failed.");
200 int32_t ret = ReadCollabInfo(data, &sourceInfo);
201 COMM_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, COMM_SDK, "read sourceInfo failed.");
202 ret = ReadCollabInfo(data, &sinkInfo);
203 COMM_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, COMM_SDK, "read sinkInfo failed.");
204 int32_t channelId = -1;
205 int32_t channelType = -1;
206 if (isSinkSide) {
207 COMM_CHECK_AND_RETURN_RET_LOGE(
208 ReadInt32(data, &channelId), SOFTBUS_TRANS_PROXY_READINT_FAILED, COMM_SDK, "read channelId failed");
209 COMM_CHECK_AND_RETURN_RET_LOGE(
210 ReadInt32(data, &channelType), SOFTBUS_TRANS_PROXY_READINT_FAILED, COMM_SDK, "read channelType failed");
211 }
212 (void)TransOnCheckCollabRelation(&sourceInfo, isSinkSide, &sinkInfo, channelId, channelType);
213 return SOFTBUS_OK;
214 }
215