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(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 int ret = TransOnChannelOpened(sessionName, &channel);
63 if (ret < 0) {
64 TRANS_LOGE(TRANS_CTRL, "TransOnChannelOpened fail, errcode=%{public}d.", ret);
65 }
66 return SOFTBUS_OK;
67 }
68
ClientOnChannelOpenfailed(IpcIo * data,IpcIo * reply)69 int32_t ClientOnChannelOpenfailed(IpcIo *data, IpcIo *reply)
70 {
71 if (data == NULL) {
72 TRANS_LOGE(TRANS_CTRL, "invalid param.");
73 return SOFTBUS_INVALID_PARAM;
74 }
75 int32_t channelId = 0;
76 int32_t channelType = 0;
77 int32_t errCode = SOFTBUS_OK;
78 ReadInt32(data, &channelId);
79 ReadInt32(data, &channelType);
80 ReadInt32(data, &errCode);
81 (void)TransOnChannelOpenFailed(channelId, channelType, errCode);
82 return SOFTBUS_OK;
83 }
84
ClientOnChannelClosed(IpcIo * data,IpcIo * reply)85 int32_t ClientOnChannelClosed(IpcIo *data, IpcIo *reply)
86 {
87 if (data == NULL) {
88 TRANS_LOGE(TRANS_CTRL, "invalid param.");
89 return SOFTBUS_INVALID_PARAM;
90 }
91 int32_t channelId = 0;
92 int32_t channelType = 0;
93 int32_t messageType = 0;
94 ReadInt32(data, &channelId);
95 ReadInt32(data, &channelType);
96 ReadInt32(data, &messageType);
97 (void)TransOnChannelClosed(channelId, channelType, messageType, SHUTDOWN_REASON_PEER);
98 return SOFTBUS_OK;
99 }
100
ClientOnChannelMsgreceived(IpcIo * data,IpcIo * reply)101 int32_t ClientOnChannelMsgreceived(IpcIo *data, IpcIo *reply)
102 {
103 if (data == NULL) {
104 TRANS_LOGE(TRANS_CTRL, "invalid param.");
105 return SOFTBUS_INVALID_PARAM;
106 }
107 int32_t channelId = 0;
108 int32_t channelType = 0;
109 int32_t type = 0;
110 ReadInt32(data, &channelId);
111 ReadInt32(data, &channelType);
112 ReadInt32(data, &type);
113 uint32_t dataLen = 0;
114 ReadUint32(data, &dataLen);
115 const uint8_t *data2 = ReadBuffer(data, dataLen);
116 (void)TransOnChannelMsgReceived(channelId, channelType, data2, dataLen, type);
117 return SOFTBUS_OK;
118 }
119
ClientSetChannelInfo(IpcIo * data,IpcIo * reply)120 int32_t ClientSetChannelInfo(IpcIo *data, IpcIo *reply)
121 {
122 if (data == NULL) {
123 TRANS_LOGE(TRANS_CTRL, "invalid param.");
124 return SOFTBUS_INVALID_PARAM;
125 }
126 (void)reply;
127 size_t size = 0;
128 const char *sessionName = (const char *)ReadString(data, &size);
129 int32_t sessionId = 0;
130 int32_t channelId = 0;
131 int32_t channelType = 0;
132 ReadInt32(data, &sessionId);
133 ReadInt32(data, &channelId);
134 ReadInt32(data, &channelType);
135 (void)TransSetChannelInfo(sessionName, sessionId, channelId, channelType);
136 return SOFTBUS_OK;
137 }
138
ClientOnChannelBind(IpcIo * data,IpcIo * reply)139 int32_t ClientOnChannelBind(IpcIo *data, IpcIo *reply)
140 {
141 if (data == NULL) {
142 TRANS_LOGE(TRANS_CTRL, "invalid param.");
143 return SOFTBUS_INVALID_PARAM;
144 }
145 (void)reply;
146 int32_t channelId = 0;
147 int32_t channelType = 0;
148 ReadInt32(data, &channelId);
149 ReadInt32(data, &channelType);
150 int32_t ret = TransOnChannelBind(channelId, channelType);
151 if (ret != SOFTBUS_OK) {
152 COMM_LOGE(COMM_SDK, "OnChannelBindInner failed! ret=%{public}d, channelId=%{public}d.", ret, channelId);
153 }
154 return ret;
155 }
156
ReadCollabInfo(IpcIo * data,CollabInfo * info)157 static int32_t ReadCollabInfo(IpcIo *data, CollabInfo *info)
158 {
159 size_t size = 0;
160 char *accountId = (char *)ReadString(data, &size);
161 if (accountId == NULL) {
162 COMM_LOGE(COMM_SDK, "read accountId failed");
163 } else {
164 if (strcpy_s(info->accountId, size, accountId) != EOK) {
165 COMM_LOGE(COMM_SDK, "strcpy_s failed to copy accountId");
166 }
167 }
168 size = 0;
169 ReadUint64(data, &info->tokenId);
170 ReadInt32(data, &info->userId);
171 ReadInt32(data, &info->pid);
172 char *deviceId = (char *)ReadString(data, &size);
173 COMM_CHECK_AND_RETURN_RET_LOGE(deviceId != NULL, SOFTBUS_IPC_ERR, COMM_SDK, "read deviceId failed.");
174 if (strcpy_s(info->deviceId, size, deviceId) != EOK) {
175 COMM_LOGI(COMM_SDK, "strcpy_s failed to copy deviceId.");
176 return SOFTBUS_STRCPY_ERR;
177 }
178 return SOFTBUS_OK;
179 }
180
ClientCheckCollabRelation(IpcIo * data,IpcIo * reply)181 int32_t ClientCheckCollabRelation(IpcIo *data, IpcIo *reply)
182 {
183 if (data == NULL) {
184 COMM_LOGI(COMM_SDK, "invalid param.");
185 return SOFTBUS_INVALID_PARAM;
186 }
187 (void)reply;
188 CollabInfo sourceInfo = { 0 };
189 CollabInfo sinkInfo = { 0 };
190 bool isSinkSide;
191 bool retReadBool = ReadBool(data, &isSinkSide);
192 COMM_CHECK_AND_RETURN_RET_LOGE(
193 retReadBool, SOFTBUS_TRANS_PROXY_READBOOL_FAILED, COMM_SDK, "read isSinkSide failed.");
194 int32_t ret = ReadCollabInfo(data, &sourceInfo);
195 COMM_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, COMM_SDK, "read sourceInfo failed.");
196 ret = ReadCollabInfo(data, &sinkInfo);
197 COMM_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, COMM_SDK, "read sinkInfo failed.");
198 int32_t channelId = -1;
199 int32_t channelType = -1;
200 if (isSinkSide) {
201 COMM_CHECK_AND_RETURN_RET_LOGE(
202 ReadInt32(data, &channelId), SOFTBUS_TRANS_PROXY_READINT_FAILED, COMM_SDK, "read channelId failed");
203 COMM_CHECK_AND_RETURN_RET_LOGE(
204 ReadInt32(data, &channelType), SOFTBUS_TRANS_PROXY_READINT_FAILED, COMM_SDK, "read channelType failed");
205 }
206 (void)TransOnCheckCollabRelation(&sourceInfo, isSinkSide, &sinkInfo, channelId, channelType);
207 return SOFTBUS_OK;
208 }
209