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_session_service.h"
17
18 #ifndef _GNU_SOURCE
19 #define _GNU_SOURCE
20 #endif
21
22 #include <unistd.h>
23
24 #include "client_qos_manager.h"
25 #include "client_trans_channel_manager.h"
26 #include "client_trans_file_listener.h"
27 #include "client_trans_session_manager.h"
28 #include "dfs_session.h"
29 #include "inner_session.h"
30 #include "securec.h"
31 #include "softbus_client_frame_manager.h"
32 #include "softbus_def.h"
33 #include "softbus_errcode.h"
34 #include "softbus_json_utils.h"
35 #include "softbus_log.h"
36 #include "softbus_trans_def.h"
37 #include "softbus_utils.h"
38 #include "trans_server_proxy.h"
39
IsValidSessionId(int sessionId)40 static bool IsValidSessionId(int sessionId)
41 {
42 if (sessionId <= 0) {
43 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "invalid sessionId [%d]", sessionId);
44 return false;
45 }
46 return true;
47 }
48
IsValidListener(const ISessionListener * listener)49 static bool IsValidListener(const ISessionListener *listener)
50 {
51 if ((listener != NULL) &&
52 (listener->OnSessionOpened != NULL) &&
53 (listener->OnSessionClosed != NULL)) {
54 return true;
55 }
56 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "invalid ISessionListener");
57 return false;
58 }
59
OpenSessionWithExistSession(int32_t sessionId,bool isEnabled)60 static int32_t OpenSessionWithExistSession(int32_t sessionId, bool isEnabled)
61 {
62 if (!isEnabled) {
63 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "the channel is opening");
64 return sessionId;
65 }
66
67 ISessionListener listener = {0};
68 if (ClientGetSessionCallbackById(sessionId, &listener) != SOFTBUS_OK) {
69 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "get session listener failed");
70 return sessionId;
71 }
72
73 if (listener.OnSessionOpened(sessionId, SOFTBUS_OK) != 0) {
74 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "session callback OnSessionOpened failed");
75 CloseSession(sessionId);
76 return INVALID_SESSION_ID;
77 }
78 return sessionId;
79 }
80
CreateSessionServer(const char * pkgName,const char * sessionName,const ISessionListener * listener)81 int CreateSessionServer(const char *pkgName, const char *sessionName, const ISessionListener *listener)
82 {
83 if (!IsValidString(pkgName, PKG_NAME_SIZE_MAX) || !IsValidString(sessionName, SESSION_NAME_SIZE_MAX) ||
84 !IsValidListener(listener)) {
85 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "CreateSessionServer invalid param");
86 return SOFTBUS_INVALID_PARAM;
87 }
88 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "CreateSessionServer: pkgName=%s, sessionName=%s",
89 pkgName, sessionName);
90
91 if (InitSoftBus(pkgName) != SOFTBUS_OK) {
92 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "init softbus err");
93 return SOFTBUS_ERR;
94 }
95
96 if (CheckPackageName(pkgName) != SOFTBUS_OK) {
97 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "invalid pkg name");
98 return SOFTBUS_INVALID_PARAM;
99 }
100
101 int ret = ClientAddSessionServer(SEC_TYPE_CIPHERTEXT, pkgName, sessionName, listener);
102 if (ret == SOFTBUS_SERVER_NAME_REPEATED) {
103 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "SessionServer is already created in client");
104 ret = SOFTBUS_OK;
105 } else if (ret != SOFTBUS_OK) {
106 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "add session server err");
107 return ret;
108 }
109
110 ret = ServerIpcCreateSessionServer(pkgName, sessionName);
111 if (ret == SOFTBUS_SERVER_NAME_REPEATED) {
112 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "SessionServer is already created in server");
113 ret = SOFTBUS_OK;
114 } else if (ret != SOFTBUS_OK) {
115 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "Server createSessionServer failed");
116 (void)ClientDeleteSessionServer(SEC_TYPE_CIPHERTEXT, sessionName);
117 }
118 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "CreateSessionServer ok: ret=%d", ret);
119 return ret;
120 }
121
RemoveSessionServer(const char * pkgName,const char * sessionName)122 int RemoveSessionServer(const char *pkgName, const char *sessionName)
123 {
124 if (!IsValidString(pkgName, PKG_NAME_SIZE_MAX) || !IsValidString(sessionName, SESSION_NAME_SIZE_MAX)) {
125 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "RemoveSessionServer invalid param");
126 return SOFTBUS_INVALID_PARAM;
127 }
128 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "RemoveSessionServer: pkgName=%s, sessionName=%s",
129 pkgName, sessionName);
130
131 int32_t ret = ServerIpcRemoveSessionServer(pkgName, sessionName);
132 if (ret != SOFTBUS_OK) {
133 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "remove in server failed");
134 return ret;
135 }
136
137 ret = ClientDeleteSessionServer(SEC_TYPE_CIPHERTEXT, sessionName);
138 if (ret != SOFTBUS_OK) {
139 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "delete session server [%s] failed", sessionName);
140 }
141 DeleteFileListener(sessionName);
142 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "RemoveSessionServer ok: ret=%d", ret);
143 return ret;
144 }
145
CheckParamIsValid(const char * mySessionName,const char * peerSessionName,const char * peerDeviceId,const char * groupId,const SessionAttribute * attr)146 static int32_t CheckParamIsValid(const char *mySessionName, const char *peerSessionName,
147 const char *peerDeviceId, const char *groupId, const SessionAttribute *attr)
148 {
149 if (!IsValidString(mySessionName, SESSION_NAME_SIZE_MAX) ||
150 !IsValidString(peerSessionName, SESSION_NAME_SIZE_MAX) ||
151 !IsValidString(peerDeviceId, DEVICE_ID_SIZE_MAX) ||
152 (attr == NULL) ||
153 (attr->dataType >= TYPE_BUTT)) {
154 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "invalid param");
155 return SOFTBUS_INVALID_PARAM;
156 }
157
158 if (groupId == NULL || strlen(groupId) >= GROUP_ID_SIZE_MAX) {
159 return SOFTBUS_INVALID_PARAM;
160 }
161
162 return SOFTBUS_OK;
163 }
164
OpenSession(const char * mySessionName,const char * peerSessionName,const char * peerDeviceId,const char * groupId,const SessionAttribute * attr)165 int OpenSession(const char *mySessionName, const char *peerSessionName, const char *peerDeviceId,
166 const char *groupId, const SessionAttribute *attr)
167 {
168 int ret = CheckParamIsValid(mySessionName, peerSessionName, peerDeviceId, groupId, attr);
169 if (ret != SOFTBUS_OK) {
170 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "OpenSession invalid param");
171 return INVALID_SESSION_ID;
172 }
173 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "OpenSession: mySessionName=%s, peerSessionName=%s",
174 mySessionName, peerSessionName);
175
176 TransInfo transInfo;
177 SessionParam param = {
178 .sessionName = mySessionName,
179 .peerSessionName = peerSessionName,
180 .peerDeviceId = peerDeviceId,
181 .groupId = groupId,
182 .attr = attr,
183 };
184
185 int32_t sessionId = INVALID_SESSION_ID;
186 bool isEnabled = false;
187
188 ret = ClientAddSession(¶m, &sessionId, &isEnabled);
189 if (ret != SOFTBUS_OK) {
190 if (ret == SOFTBUS_TRANS_SESSION_REPEATED) {
191 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "session already opened");
192 return OpenSessionWithExistSession(sessionId, isEnabled);
193 }
194 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "add session err: ret=%d", ret);
195 return ret;
196 }
197
198 ret = ServerIpcOpenSession(¶m, &transInfo);
199 if (ret != SOFTBUS_OK) {
200 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "open session ipc err: ret=%d", ret);
201 (void)ClientDeleteSession(sessionId);
202 return ret;
203 }
204
205 ret = ClientSetChannelBySessionId(sessionId, &transInfo);
206 if (ret != SOFTBUS_OK) {
207 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "open session failed");
208 (void)ClientDeleteSession(sessionId);
209 return INVALID_SESSION_ID;
210 }
211 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "OpenSession ok: sessionId=%d, channelId=%d, channelType = %d",
212 sessionId, transInfo.channelId, transInfo.channelType);
213 return sessionId;
214 }
215
ConvertAddrStr(const char * addrStr,ConnectionAddr * addrInfo)216 static int32_t ConvertAddrStr(const char *addrStr, ConnectionAddr *addrInfo)
217 {
218 if (addrStr == NULL || addrInfo == NULL) {
219 return SOFTBUS_INVALID_PARAM;
220 }
221
222 cJSON *obj = cJSON_Parse(addrStr);
223 if (obj == NULL) {
224 return SOFTBUS_PARSE_JSON_ERR;
225 }
226 if (memset_s(addrInfo, sizeof(ConnectionAddr), 0x0, sizeof(ConnectionAddr)) != EOK) {
227 cJSON_Delete(obj);
228 return SOFTBUS_MEM_ERR;
229 }
230 int port;
231 if (GetJsonObjectStringItem(obj, "ETH_IP", addrInfo->info.ip.ip, IP_STR_MAX_LEN) &&
232 GetJsonObjectNumberItem(obj, "ETH_PORT", &port)) {
233 addrInfo->info.ip.port = (uint16_t)port;
234 if (IsValidString(addrInfo->info.ip.ip, IP_STR_MAX_LEN) && addrInfo->info.ip.port > 0) {
235 cJSON_Delete(obj);
236 addrInfo->type = CONNECTION_ADDR_ETH;
237 return SOFTBUS_OK;
238 }
239 }
240 if (GetJsonObjectStringItem(obj, "WIFI_IP", addrInfo->info.ip.ip, IP_STR_MAX_LEN) &&
241 GetJsonObjectNumberItem(obj, "WIFI_PORT", &port)) {
242 addrInfo->info.ip.port = (uint16_t)port;
243 if (IsValidString(addrInfo->info.ip.ip, IP_STR_MAX_LEN) && addrInfo->info.ip.port > 0) {
244 cJSON_Delete(obj);
245 addrInfo->type = CONNECTION_ADDR_WLAN;
246 return SOFTBUS_OK;
247 }
248 }
249 if (GetJsonObjectStringItem(obj, "BR_MAC", addrInfo->info.br.brMac, BT_MAC_LEN)) {
250 cJSON_Delete(obj);
251 addrInfo->type = CONNECTION_ADDR_BR;
252 return SOFTBUS_OK;
253 }
254 if (GetJsonObjectStringItem(obj, "BLE_MAC", addrInfo->info.ble.bleMac, BT_MAC_LEN)) {
255 cJSON_Delete(obj);
256 addrInfo->type = CONNECTION_ADDR_BLE;
257 return SOFTBUS_OK;
258 }
259 cJSON_Delete(obj);
260 return SOFTBUS_ERR;
261 }
262
IsValidAddrInfoArr(const ConnectionAddr * addrInfo,int num)263 static int IsValidAddrInfoArr(const ConnectionAddr *addrInfo, int num)
264 {
265 int32_t addrIndex = -1;
266 if (addrInfo == NULL || num <= 0) {
267 return addrIndex;
268 }
269 int32_t wifiIndex = -1;
270 int32_t brIndex = -1;
271 int32_t bleIndex = -1;
272 for (int32_t index = 0; index < num; index++) {
273 if ((addrInfo[index].type == CONNECTION_ADDR_ETH || addrInfo[index].type == CONNECTION_ADDR_WLAN) &&
274 wifiIndex < 0) {
275 wifiIndex = index;
276 }
277 if (addrInfo[index].type == CONNECTION_ADDR_BR && brIndex < 0) {
278 brIndex = index;
279 }
280 if (addrInfo[index].type == CONNECTION_ADDR_BLE && bleIndex < 0) {
281 bleIndex = index;
282 }
283 }
284 addrIndex = (wifiIndex >= 0) ? wifiIndex : addrIndex;
285 addrIndex = (addrIndex < 0) ? brIndex : addrIndex;
286 addrIndex = (addrIndex < 0) ? bleIndex : addrIndex;
287 return addrIndex;
288 }
289
OpenAuthSession(const char * sessionName,const ConnectionAddr * addrInfo,int num,const char * mixAddr)290 int OpenAuthSession(const char *sessionName, const ConnectionAddr *addrInfo, int num, const char *mixAddr)
291 {
292 if (!IsValidString(sessionName, SESSION_NAME_SIZE_MAX)) {
293 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "invalid param");
294 return SOFTBUS_INVALID_PARAM;
295 }
296 TransInfo transInfo;
297 int32_t addrIndex = IsValidAddrInfoArr(addrInfo, num);
298 ConnectionAddr *addr = NULL;
299 ConnectionAddr mix;
300 if (addrIndex < 0) {
301 if (ConvertAddrStr(mixAddr, &mix) != SOFTBUS_OK) {
302 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "invalid addrInfo param");
303 return SOFTBUS_INVALID_PARAM;
304 }
305 addr = &mix;
306 } else {
307 addr = (ConnectionAddr *)&addrInfo[addrIndex];
308 }
309 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "OpenAuthSession: mySessionName=%s", sessionName);
310
311 int32_t sessionId;
312 int32_t ret = ClientAddAuthSession(sessionName, &sessionId);
313 if (ret != SOFTBUS_OK) {
314 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "add non encrypt session err: ret=%d", ret);
315 return ret;
316 }
317
318 transInfo.channelId = ServerIpcOpenAuthSession(sessionName, addr);
319 if (addr->type == CONNECTION_ADDR_BR || addr->type == CONNECTION_ADDR_BLE) {
320 transInfo.channelType = CHANNEL_TYPE_PROXY;
321 } else {
322 transInfo.channelType = CHANNEL_TYPE_AUTH;
323 }
324 ret = ClientSetChannelBySessionId(sessionId, &transInfo);
325 if (ret != SOFTBUS_OK) {
326 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "OpenAuthSession failed");
327 (void)ClientDeleteSession(sessionId);
328 return INVALID_SESSION_ID;
329 }
330 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "OpenAuthSession ok: sessionId=%d, channelId=%d, channelType = %d",
331 sessionId, transInfo.channelId, transInfo.channelType);
332 return sessionId;
333 }
334
NotifyAuthSuccess(int sessionId)335 void NotifyAuthSuccess(int sessionId)
336 {
337 int32_t channelId;
338 int32_t type;
339 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "NotifyAuthSuccess sessionId:%d", sessionId);
340 int32_t ret = ClientGetChannelBySessionId(sessionId, &channelId, &type, NULL);
341 if (ret != SOFTBUS_OK) {
342 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "get channel err");
343 return;
344 }
345
346 int isServer = 0;
347 if (ClientGetSessionIntegerDataById(sessionId, &isServer, KEY_IS_SERVER) != SOFTBUS_OK) {
348 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "get isServer failed");
349 return;
350 }
351 if (isServer) {
352 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "device is service side, no notification");
353 return;
354 }
355 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "device is client side");
356
357 if (ServerIpcNotifyAuthSuccess(channelId) != SOFTBUS_OK) {
358 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "ServerIpcNotifyAuthSuccess err");
359 return;
360 }
361 }
362
CheckSessionIsOpened(int32_t sessionId)363 static int32_t CheckSessionIsOpened(int32_t sessionId)
364 {
365 #define SESSION_STATUS_CHECK_MAX_NUM 100
366 #define SESSION_CHECK_PERIOD 50000
367 int32_t i = 0;
368 bool isEnable = false;
369
370 while (i < SESSION_STATUS_CHECK_MAX_NUM) {
371 if (ClientGetChannelBySessionId(sessionId, NULL, NULL, &isEnable) != SOFTBUS_OK) {
372 return SOFTBUS_NOT_FIND;
373 }
374 if (isEnable == true) {
375 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "CheckSessionIsOpened session is enable");
376 return SOFTBUS_OK;
377 }
378 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "CheckSessionIsOpened session is opening, i=%d", i);
379 usleep(SESSION_CHECK_PERIOD);
380 i++;
381 }
382
383 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "CheckSessionIsOpened session open timeout");
384 return SOFTBUS_ERR;
385 }
386
OpenSessionSync(const char * mySessionName,const char * peerSessionName,const char * peerDeviceId,const char * groupId,const SessionAttribute * attr)387 int OpenSessionSync(const char *mySessionName, const char *peerSessionName, const char *peerDeviceId,
388 const char *groupId, const SessionAttribute *attr)
389 {
390 int ret = CheckParamIsValid(mySessionName, peerSessionName, peerDeviceId, groupId, attr);
391 if (ret != SOFTBUS_OK) {
392 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "OpenSessionSync invalid param");
393 return INVALID_SESSION_ID;
394 }
395 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "OpenSessionSync: mySessionName=%s, peerSessionName=%s",
396 mySessionName, peerSessionName);
397
398 TransInfo transInfo;
399 SessionParam param = {
400 .sessionName = mySessionName,
401 .peerSessionName = peerSessionName,
402 .peerDeviceId = peerDeviceId,
403 .groupId = groupId,
404 .attr = attr,
405 };
406
407 int32_t sessionId = INVALID_SESSION_ID;
408 bool isEnabled = false;
409
410 ret = ClientAddSession(¶m, &sessionId, &isEnabled);
411 if (ret != SOFTBUS_OK) {
412 if (ret == SOFTBUS_TRANS_SESSION_REPEATED) {
413 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "session already opened");
414 CheckSessionIsOpened(sessionId);
415 return OpenSessionWithExistSession(sessionId, isEnabled);
416 }
417 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "add session err: ret=%d", ret);
418 return ret;
419 }
420
421 ret = ServerIpcOpenSession(¶m, &transInfo);
422 if (ret != SOFTBUS_OK) {
423 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "open session ipc err: ret=%d", ret);
424 (void)ClientDeleteSession(sessionId);
425 return ret;
426 }
427 ret = ClientSetChannelBySessionId(sessionId, &transInfo);
428 if (ret != SOFTBUS_OK) {
429 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "server open session err: ret=%d", ret);
430 (void)ClientDeleteSession(sessionId);
431 return INVALID_SESSION_ID;
432 }
433
434 ret = CheckSessionIsOpened(sessionId);
435 if (ret != SOFTBUS_OK) {
436 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "CheckSessionIsOpened err: ret=%d", ret);
437 (void)ClientDeleteSession(sessionId);
438 return INVALID_SESSION_ID;
439 }
440 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "OpenSessionSync ok: sessionId=%d, channelId=%d",
441 sessionId, transInfo.channelId);
442 return sessionId;
443 }
444
CloseSession(int sessionId)445 void CloseSession(int sessionId)
446 {
447 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "CloseSession: sessionId=%d", sessionId);
448 int32_t channelId = INVALID_CHANNEL_ID;
449 int32_t type = CHANNEL_TYPE_BUTT;
450 int32_t ret;
451
452 if (!IsValidSessionId(sessionId)) {
453 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "invalid param");
454 return;
455 }
456 ret = ClientGetChannelBySessionId(sessionId, &channelId, &type, NULL);
457 if (ret != SOFTBUS_OK) {
458 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "get channel err");
459 return;
460 }
461 ret = ClientTransCloseChannel(channelId, type);
462 if (ret != SOFTBUS_OK) {
463 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "close channel err: ret=%d, channelId=%d, channeType=%d",
464 ret, channelId, type);
465 }
466
467 ret = ClientDeleteSession(sessionId);
468 if (ret != SOFTBUS_OK) {
469 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "CloseSession delete session err");
470 }
471 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "CloseSession ok");
472 return;
473 }
474
GetMySessionName(int sessionId,char * sessionName,unsigned int len)475 int GetMySessionName(int sessionId, char *sessionName, unsigned int len)
476 {
477 if (!IsValidSessionId(sessionId) || (sessionName == NULL) || (len > SESSION_NAME_SIZE_MAX)) {
478 return SOFTBUS_INVALID_PARAM;
479 }
480
481 return ClientGetSessionDataById(sessionId, sessionName, len, KEY_SESSION_NAME);
482 }
483
GetPeerSessionName(int sessionId,char * sessionName,unsigned int len)484 int GetPeerSessionName(int sessionId, char *sessionName, unsigned int len)
485 {
486 if (!IsValidSessionId(sessionId) || (sessionName == NULL) || (len > SESSION_NAME_SIZE_MAX)) {
487 return SOFTBUS_INVALID_PARAM;
488 }
489
490 return ClientGetSessionDataById(sessionId, sessionName, len, KEY_PEER_SESSION_NAME);
491 }
492
GetPeerDeviceId(int sessionId,char * devId,unsigned int len)493 int GetPeerDeviceId(int sessionId, char *devId, unsigned int len)
494 {
495 if (!IsValidSessionId(sessionId) || (devId == NULL) || (len > SESSION_NAME_SIZE_MAX)) {
496 return SOFTBUS_INVALID_PARAM;
497 }
498
499 return ClientGetSessionDataById(sessionId, devId, len, KEY_PEER_DEVICE_ID);
500 }
501
GetSessionSide(int sessionId)502 int GetSessionSide(int sessionId)
503 {
504 return ClientGetSessionSide(sessionId);
505 }
506
SetFileReceiveListener(const char * pkgName,const char * sessionName,const IFileReceiveListener * recvListener,const char * rootDir)507 int SetFileReceiveListener(const char *pkgName, const char *sessionName,
508 const IFileReceiveListener *recvListener, const char *rootDir)
509 {
510 if (!IsValidString(pkgName, PKG_NAME_SIZE_MAX) || !IsValidString(sessionName, SESSION_NAME_SIZE_MAX) ||
511 !IsValidString(rootDir, FILE_RECV_ROOT_DIR_SIZE_MAX) || (recvListener == NULL)) {
512 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "set file receive listener invalid param");
513 return SOFTBUS_INVALID_PARAM;
514 }
515 if (InitSoftBus(pkgName) != SOFTBUS_OK) {
516 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "set file receive listener init softbus client error");
517 return SOFTBUS_ERR;
518 }
519 return TransSetFileReceiveListener(sessionName, recvListener, rootDir);
520 }
521
SetFileSendListener(const char * pkgName,const char * sessionName,const IFileSendListener * sendListener)522 int SetFileSendListener(const char *pkgName, const char *sessionName, const IFileSendListener *sendListener)
523 {
524 if (!IsValidString(pkgName, PKG_NAME_SIZE_MAX) || !IsValidString(sessionName, SESSION_NAME_SIZE_MAX) ||
525 sendListener == NULL) {
526 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "set file send listener invalid param");
527 return SOFTBUS_INVALID_PARAM;
528 }
529 if (InitSoftBus(pkgName) != SOFTBUS_OK) {
530 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "set file send listener init softbus client error");
531 return SOFTBUS_ERR;
532 }
533 return TransSetFileSendListener(sessionName, sendListener);
534 }
535
536 static const char *g_busName = "DistributedFileService";
537
IsValidDFSSession(int32_t sessionId,int32_t * channelId)538 static int32_t IsValidDFSSession(int32_t sessionId, int32_t *channelId)
539 {
540 char sessionName[SESSION_NAME_SIZE_MAX] = {0};
541 int32_t type;
542 if (GetMySessionName(sessionId, sessionName, SESSION_NAME_SIZE_MAX) != SOFTBUS_OK) {
543 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "get dfs session name failed");
544 return SOFTBUS_ERR;
545 }
546 if (strncmp(sessionName, g_busName, strlen(g_busName)) != 0) {
547 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "invalid dfs session name");
548 return SOFTBUS_TRANS_FUNC_NOT_SUPPORT;
549 }
550
551 if (ClientGetChannelBySessionId(sessionId, channelId, &type, NULL) != SOFTBUS_OK) {
552 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "get channel failed");
553 return SOFTBUS_ERR;
554 }
555 if (type != CHANNEL_TYPE_TCP_DIRECT) {
556 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "invalid channel type");
557 return SOFTBUS_TRANS_FUNC_NOT_SUPPORT;
558 }
559 return SOFTBUS_OK;
560 }
561
GetSessionKey(int32_t sessionId,char * key,unsigned int len)562 int32_t GetSessionKey(int32_t sessionId, char *key, unsigned int len)
563 {
564 int32_t channelId;
565 if (!IsValidSessionId(sessionId) || key == NULL || len < SESSION_KEY_LEN) {
566 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "invalid param");
567 return SOFTBUS_INVALID_PARAM;
568 }
569 if (IsValidDFSSession(sessionId, &channelId) != SOFTBUS_OK) {
570 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "invalid dfs session");
571 return SOFTBUS_TRANS_FUNC_NOT_SUPPORT;
572 }
573 return ClientGetSessionKey(channelId, key, len);
574 }
575
GetSessionHandle(int32_t sessionId,int * handle)576 int32_t GetSessionHandle(int32_t sessionId, int *handle)
577 {
578 int32_t channelId;
579 if (!IsValidSessionId(sessionId) || handle == NULL) {
580 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "invalid param");
581 return SOFTBUS_INVALID_PARAM;
582 }
583 if (IsValidDFSSession(sessionId, &channelId) != SOFTBUS_OK) {
584 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "invalid dfs session");
585 return SOFTBUS_TRANS_FUNC_NOT_SUPPORT;
586 }
587 return ClientGetHandle(channelId, handle);
588 }
589
DisableSessionListener(int32_t sessionId)590 int32_t DisableSessionListener(int32_t sessionId)
591 {
592 int32_t channelId;
593 if (!IsValidSessionId(sessionId)) {
594 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "invalid param");
595 return SOFTBUS_INVALID_PARAM;
596 }
597 if (IsValidDFSSession(sessionId, &channelId) != SOFTBUS_OK) {
598 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "invalid dfs session");
599 return SOFTBUS_TRANS_FUNC_NOT_SUPPORT;
600 }
601 return ClientDisableSessionListener(channelId);
602 }
603
QosReport(int32_t sessionId,int32_t appType,int32_t quality)604 int32_t QosReport(int32_t sessionId, int32_t appType, int32_t quality)
605 {
606 if (quality != QOS_IMPROVE) {
607 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "qos report invalid param");
608 return SOFTBUS_INVALID_PARAM;
609 }
610
611 int32_t channelId = INVALID_CHANNEL_ID;
612 int32_t type = CHANNEL_TYPE_BUTT;
613 int32_t ret = ClientGetChannelBySessionId(sessionId, &channelId, &type, NULL);
614 if (ret != SOFTBUS_OK) {
615 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "get channel err");
616 return SOFTBUS_ERR;
617 }
618 if (ClientGetSessionSide(sessionId) != IS_CLIENT) {
619 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR,
620 "qos report sessionId[%d] not exist or not client side", sessionId);
621 return SOFTBUS_ERR;
622 }
623 if ((ret = ClientQosReport(channelId, type, appType, quality)) != SOFTBUS_OK) {
624 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "qos report sessionId[%d] failed", sessionId);
625 }
626 return ret;
627 }
628