• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "net_trans_common.h"
17 
18 static int ONE_SECOND = 1;
19 static int32_t g_currentSessionId4Data = -1;
20 static int32_t g_currentSessionId4Ctl = -1;
21 static int32_t g_currentSessionId4Proxy = -1;
22 static int32_t g_waitFlag = WAIT_DEF_VALUE;
23 static int32_t g_waitFlag4Data = WAIT_DEF_VALUE;
24 static int32_t g_waitFlag4Ctl = WAIT_DEF_VALUE;
25 static int32_t g_waitFlag4Proxy = WAIT_DEF_VALUE;
26 static int32_t g_waitFlag4Stream = WAIT_DEF_VALUE;
27 static int32_t g_passiveOpenRetFlag = SOFTBUS_OK;
28 static int32_t g_nodeOnlineCount = 0;
29 static int32_t g_nodeOfflineCount = 0;
30 static int32_t g_msgCount4Data = 0;
31 static int32_t g_byteCount4Data = 0;
32 static int32_t g_sessionCloseCount4Data = 0;
33 static int32_t g_sessionCloseCount4Ctrl = 0;
34 static int32_t g_sessionOpenCount4Data = 0;
35 static int32_t g_sessionOpenCount4Ctrl = 0;
36 static int32_t g_sessionOpenCount4Proxy = 0;
37 
38 static SessionAttribute* g_sessionAttr4Data = NULL;
39 static SessionAttribute* g_sessionAttr4Ctl = NULL;
40 static SessionAttribute* g_sessionAttr4Pass = NULL;
41 static SessionAttribute* g_sessionAttr4Perf = NULL;
42 static SessionAttribute* g_sessionAttr4Proxy = NULL;
43 static SessionAttribute* g_p2pattribute = NULL;
44 static SessionAttribute* g_p2pattributeProxy = NULL;
45 static ISessionListener* g_sessionlistener4Data = NULL;
46 static ISessionListener* g_sessionlistener4Ctl = NULL;
47 static ISessionListener* g_sessionlistener4Pass = NULL;
48 static ISessionListener* g_sessionlistener4Perf = NULL;
49 static ISessionListener* g_sessionlistener4Proxy = NULL;
50 static ISessionListener* g_sessionlistener4Stream = NULL;
51 
52 static IFileSendListener* g_fileSendListener = NULL;
53 static IFileReceiveListener* g_fileRecvListener = NULL;
54 
55 static char g_networkId[NETWORK_ID_BUF_LEN] = { 0 };
56 static INodeStateCb g_defNodeStateCallback;
57 static ConnectionAddr g_ethAddr = {
58     .type = CONNECTION_ADDR_WLAN,
59 };
60 
61 static int* g_sId4Task2;
62 static int* g_sId4Task3;
63 static char g_fillContentChar = 'd';
64 static unsigned int g_expectDataSize = 0;
65 static char* g_expectDataContent = NULL;
66 static char* g_expectMessageContent = NULL;
67 static int32_t g_recvMsgStat4Control[MAX_SESSION_NUM] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
68 static int32_t g_recvByteStat4Control[MAX_SESSION_NUM] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
69 static uint64_t g_discoverTimeEnd;
70 static uint64_t g_openSessionTimeEnd;
71 static uint64_t g_transTimeEnd;
72 static pthread_barrier_t* g_barrier = NULL;
73 /* discovery */
74 static IDiscoveryCallback g_defDiscCallback;
75 
sleepn(int n)76 void sleepn(int n)
77 {
78     for(int i = 0; i < n; i++) {
79         sleep(1);
80     }
81 }
Wait(int timeout)82 int Wait(int timeout)
83 {
84     LOG("start wait,timeout:%d", timeout);
85     int count = 0;
86     int t = timeout;
87     while (t > 0) {
88         sleep(1);
89         if (g_waitFlag != WAIT_DEF_VALUE) {
90             LOG("Wait success[flag:%d] [time:%d]", g_waitFlag, count);
91             break;
92         }
93         t--;
94         count++;
95     }
96     if (g_waitFlag != WAIT_SUCCESS_VALUE) {
97         LOG("Wait fail[exp:%d, real:%d][used time:%d]", WAIT_SUCCESS_VALUE, g_waitFlag, count);
98         return SOFTBUS_ERR;
99     }
100     return SOFTBUS_OK;
101 }
102 
Wait4Session(int timeout,WaitSessionType type)103 int Wait4Session(int timeout, WaitSessionType type)
104 {
105     int hitFlag = -1;
106     int t = timeout;
107     while (t > 0) {
108         sleep(1);
109         switch (type) {
110             case SESSION_4CTL:
111                 if (g_waitFlag4Ctl != WAIT_DEF_VALUE) {
112                     LOG("Wait4Session[ctrl] succ, flag:%d", g_waitFlag4Ctl);
113                     hitFlag = 1;
114                 }
115                 break;
116             case SESSION_4DATA:
117                 if (g_waitFlag4Data != WAIT_DEF_VALUE) {
118                     LOG("Wait4Session[data] succ,flag:%d", g_waitFlag4Data);
119                     hitFlag = 1;
120                 }
121                 break;
122             case SESSION_4PROXY:
123                 if (g_waitFlag4Proxy != WAIT_DEF_VALUE) {
124                     LOG("Wait4Session[proxy] succ,flag:%d", g_waitFlag4Proxy);
125                     hitFlag = 1;
126                 }
127                 break;
128             case SESSION_4STREAM:
129                 if (g_waitFlag4Stream != WAIT_DEF_VALUE) {
130                     LOG("Wait4Session[proxy] succ,flag:%d", g_waitFlag4Stream);
131                     hitFlag = 1;
132                 }
133                 break;
134             default:
135                 LOG("Wait4Session type error");
136                 hitFlag = 1;
137                 break;
138         }
139         if (hitFlag != -1) {
140             break;
141         }
142         t--;
143     }
144     switch (type) {
145         case SESSION_4CTL:
146             if (g_waitFlag4Ctl != WAIT_SUCCESS_VALUE) {
147                 LOG("Wait4Session[ctrl] fail[exp:%d, real:%d]", WAIT_SUCCESS_VALUE, g_waitFlag4Ctl);
148                 return SOFTBUS_ERR;
149             }
150             break;
151         case SESSION_4DATA:
152             if (g_waitFlag4Data != WAIT_SUCCESS_VALUE) {
153                 LOG("Wait4Session[data] fail[exp:%d, real:%d]", WAIT_SUCCESS_VALUE, g_waitFlag4Data);
154                 return SOFTBUS_ERR;
155             }
156             break;
157         case SESSION_4PROXY:
158             if (g_waitFlag4Proxy != WAIT_SUCCESS_VALUE) {
159                 LOG("Wait4Session[proxy] fail[exp:%d, real:%d]", WAIT_SUCCESS_VALUE, g_waitFlag4Proxy);
160                 return SOFTBUS_ERR;
161             }
162             break;
163          case SESSION_4STREAM:
164             if (g_waitFlag4Stream != WAIT_SUCCESS_VALUE) {
165                 LOG("Wait4Session[proxy] fail[exp:%d, real:%d]", WAIT_SUCCESS_VALUE, g_waitFlag4Stream);
166                 return SOFTBUS_ERR;
167             }
168             break;
169         default:
170             return SOFTBUS_ERR;
171     }
172     return SOFTBUS_OK;
173 }
174 
GetCurrentTimeOfMs(void)175 uint64_t GetCurrentTimeOfMs(void)
176 {
177     int unit = 1000;
178     struct timeval tv;
179     gettimeofday(&tv, NULL);
180     return (tv.tv_sec * unit + tv.tv_usec / unit);
181 }
182 
GetSoftbusPid(void)183 char* GetSoftbusPid(void)
184 {
185     FILE* file = NULL;
186     int buffSize = 20;
187     char* buffer = (char*)malloc(buffSize);
188     if (buffer == NULL) {
189         LOG("malloc fail");
190         return NULL;
191     }
192 
193     file = popen(CMD_PIDOF_SOFTBUS, "r");
194     if (file == NULL) {
195         LOG("call popen fail");
196         free(buffer);
197         return NULL;
198     }
199     (void)fgets(buffer, buffSize, file);
200     LOG("##################Softbus PID:%s", buffer);
201     pclose(file);
202 
203     return buffer;
204 }
205 
OnReceiveFileStarted(int sessionId,const char * files,int fileCnt)206 static int OnReceiveFileStarted(int sessionId, const char* files, int fileCnt)
207 {
208     LOG("[recv file]start,sid:%d, fileCnt:%d", sessionId, fileCnt);
209     return 0;
210 }
211 
OnReceiveFileProcess(int sessionId,const char * firstFile,uint64_t bytesUpload,uint64_t bytesTotal)212 static int OnReceiveFileProcess(int sessionId, const char* firstFile, uint64_t bytesUpload, uint64_t bytesTotal)
213 {
214     return 0;
215 }
216 
OnReceiveFileFinished(int sessionId,const char * files,int fileCnt)217 static void OnReceiveFileFinished(int sessionId, const char* files, int fileCnt)
218 {
219     LOG("[recv file]finish,sid:%d, fileCnt:%d", sessionId, fileCnt);
220 }
221 
OnRecvFileTransError(int sessionId)222 static void OnRecvFileTransError(int sessionId)
223 {
224     LOG("[recv file]trans error,sid:%d", sessionId);
225 }
226 
OnSendFileProcess(int sessionId,uint64_t bytesUpload,uint64_t bytesTotal)227 static int OnSendFileProcess(int sessionId, uint64_t bytesUpload, uint64_t bytesTotal)
228 {
229     return 0;
230 }
231 
OnSendFileFinished(int sessionId,const char * firstFile)232 static int OnSendFileFinished(int sessionId, const char* firstFile)
233 {
234     g_waitFlag = WAIT_SUCCESS_VALUE;
235     LOG("[send file]finish,sid:%d, firstFile:%s\n", sessionId, firstFile);
236     return 0;
237 }
238 
OnSendFileTransError(int sessionId)239 static void OnSendFileTransError(int sessionId)
240 {
241     g_waitFlag = WAIT_FAIL_VALUE;
242     LOG("[send file]trans error,sid = %d\n", sessionId);
243 }
244 
OnDefDeviceFound(const DeviceInfo * device)245 static void OnDefDeviceFound(const DeviceInfo* device)
246 {
247     g_discoverTimeEnd = GetCurrentTimeOfMs();
248 
249     if (device == NULL) {
250         LOG("[cb]device found,but is null");
251         g_waitFlag = WAIT_FAIL_VALUE;
252         return;
253     }
254 
255     uint16_t port = device->addr[0].info.ip.port;
256     char ipTmp[IP_STR_MAX_LEN];
257     if (strncpy_s(ipTmp, IP_STR_MAX_LEN, device->addr[0].info.ip.ip, strlen(device->addr[0].info.ip.ip)) != 0) {
258         LOG("[cb]device found, strncpy_s ipTmp fail");
259         return;
260     }
261     LOG("[cb]device found, type:%d", device->addr[0].type);
262     LOG("[cb]device found, addr:%s, prot:%d", ipTmp, port);
263 
264     g_ethAddr.info.ip.port = port;
265     if (strncpy_s(g_ethAddr.info.ip.ip, IP_STR_MAX_LEN, ipTmp, strlen(ipTmp)) != 0) {
266         LOG("[cb]device found, strncpy_s ip fail");
267         g_waitFlag = WAIT_FAIL_VALUE;
268         return;
269     }
270 
271     g_waitFlag = WAIT_SUCCESS_VALUE;
272 }
273 
OnDefDiscoverFail(int subscribeId,DiscoveryFailReason failReason)274 static void OnDefDiscoverFail(int subscribeId, DiscoveryFailReason failReason)
275 {
276     LOG("[cb]discover fail, sub id:%d, reason:%d", subscribeId, failReason);
277 }
278 
OnDefDiscoverSuccess(int subscribeId)279 static void OnDefDiscoverSuccess(int subscribeId)
280 {
281     LOG("[cb]discover success, sub id:%d", subscribeId);
282 }
283 
OnDefNodeOnline(NodeBasicInfo * info)284 static void OnDefNodeOnline(NodeBasicInfo* info)
285 {
286     if (info == NULL) {
287         LOG("[cb]Online: info is null");
288         return;
289     }
290     (void)strncpy_s(g_networkId, NETWORK_ID_BUF_LEN, info->networkId, NETWORK_ID_BUF_LEN);
291     LOG("[cb]Online id:%s, name:%s, type id:%u", info->networkId, info->deviceName, info->deviceTypeId);
292     g_nodeOnlineCount++;
293 }
294 
OnDefNodeOffline(NodeBasicInfo * info)295 static void OnDefNodeOffline(NodeBasicInfo* info)
296 {
297     if (info == NULL) {
298         LOG("[cb]Offline: info is null");
299         return;
300     }
301     LOG("[cb]Offline id:%s, name:%s, type id:%u", info->networkId, info->deviceName, info->deviceTypeId);
302     g_nodeOfflineCount++;
303 }
304 
OnDefNodeBasicInfoChanged(NodeBasicInfoType type,NodeBasicInfo * info)305 static void OnDefNodeBasicInfoChanged(NodeBasicInfoType type, NodeBasicInfo* info)
306 {
307     if (info == NULL) {
308         LOG("[cb]InfoChanged: info is null, type[%d]", type);
309         return;
310     }
311     LOG("[cb]InfoChanged id: %s,name: %s", info->networkId, info->deviceName);
312 }
313 
onDefNodeStatusChanged(NodeStatusType type,NodeStatus * status)314 static void onDefNodeStatusChanged(NodeStatusType type, NodeStatus *status)
315 {
316     if (status == NULL) {
317         LOG("[cb]StatusChanged: info is null, type[%d]", type);
318         return;
319     }
320     LOG("[cb]StatusChanged id: %s,status: %d", status->basicInfo.networkId, status->authStatus);
321 }
322 
DataSessionOpened(int sessionId,int result)323 static int DataSessionOpened(int sessionId, int result)
324 {
325     // wait 1s, ensure set current session id
326     sleep(1);
327     g_sessionOpenCount4Data++;
328     if (result == SOFTBUS_OK) {
329         if (sessionId == g_currentSessionId4Data) {
330             LOG("[cb][data]open session check success sid[%d]", sessionId);
331             g_waitFlag4Data = WAIT_SUCCESS_VALUE;
332         } else {
333             LOG("[cb][data]open session callback sid[%d] not match open "
334                 "sid[%d]",
335                 sessionId, g_currentSessionId4Data);
336             g_waitFlag4Data = WAIT_FAIL_VALUE;
337         }
338     } else {
339         g_waitFlag4Data = WAIT_FAIL_VALUE;
340     }
341     return SOFTBUS_OK;
342 }
343 
DataSessionClosed(int sessionId)344 static void DataSessionClosed(int sessionId)
345 {
346     g_sessionCloseCount4Data++;
347     if (sessionId == g_currentSessionId4Data) {
348         LOG("[cb][data]closed session,check sid[%d] success", sessionId);
349         g_currentSessionId4Data = -1;
350         g_waitFlag4Data = WAIT_SUCCESS_VALUE;
351     } else {
352         LOG("[cb][data]closed session, callback sid[%d] not match open sid[%d]", sessionId, g_currentSessionId4Data);
353         g_waitFlag4Data = WAIT_FAIL_VALUE;
354     }
355 }
356 
CheckReceiveData(int sessionId,const void * data,unsigned int dataLen)357 static void CheckReceiveData(int sessionId, const void* data, unsigned int dataLen)
358 {
359     // check session id
360     if (sessionId == g_currentSessionId4Data && dataLen == g_expectDataSize) {
361         LOG("[check]sid/size ok[sid:%d,size:%u]", sessionId, dataLen);
362     } else {
363         LOG("[check]sid/size fail[sid exp:%d,real:%d][size exp:%u,real:%u]", g_currentSessionId4Data, sessionId,
364             g_expectDataSize, dataLen);
365         g_waitFlag = WAIT_FAIL_VALUE;
366         return;
367     }
368 
369     // check data content
370     int cmpRst = strncmp(g_expectDataContent, (char*)data, dataLen);
371     if (cmpRst == 0) {
372         LOG("[check] cmp content ok");
373         g_waitFlag = WAIT_SUCCESS_VALUE;
374     } else {
375         LOG("[check] cmp content fail[exp:%s,real:%s]", g_expectDataContent, (char*)data);
376         g_waitFlag = WAIT_FAIL_VALUE;
377     }
378 }
379 
DataBytesReceived(int sessionId,const void * data,unsigned int dataLen)380 static void DataBytesReceived(int sessionId, const void* data, unsigned int dataLen)
381 {
382     LOG("[cb][data]ByteRec start");
383     CheckReceiveData(sessionId, data, dataLen);
384     g_byteCount4Data++;
385     LOG("[cb][data]ByteRec end");
386 }
387 
DataMessageReceived(int sessionId,const void * data,unsigned int dataLen)388 static void DataMessageReceived(int sessionId, const void* data, unsigned int dataLen)
389 {
390     LOG("[cb][data]MessageRec start");
391     CheckReceiveData(sessionId, data, dataLen);
392     g_msgCount4Data++;
393     LOG("[cb][data]MessageRec end");
394 }
395 
ControlSessionOpened(int sessionId,int result)396 static int ControlSessionOpened(int sessionId, int result)
397 {
398     // wait 1s, ensure set current session id
399     sleep(1);
400     g_sessionOpenCount4Ctrl++;
401     if (result == SOFTBUS_OK) {
402         if (sessionId == g_currentSessionId4Ctl) {
403             LOG("[cb][ctrl]open session check success sid[%d]", sessionId);
404             g_waitFlag4Ctl = WAIT_SUCCESS_VALUE;
405         } else {
406             LOG("[cb][ctrl]open session callback sid[%d] not match open "
407                 "sid[%d]",
408                 sessionId, g_currentSessionId4Ctl);
409             g_waitFlag4Ctl = WAIT_FAIL_VALUE;
410         }
411     } else {
412         g_waitFlag4Ctl = WAIT_FAIL_VALUE;
413     }
414     return SOFTBUS_OK;
415 }
416 
ControlSessionClosed(int sessionId)417 static void ControlSessionClosed(int sessionId)
418 {
419     g_sessionCloseCount4Ctrl++;
420     if (sessionId == g_currentSessionId4Ctl) {
421         LOG("[cb][ctrl]closed session check sid[%d] success", sessionId);
422         g_currentSessionId4Ctl = -1;
423         g_waitFlag4Ctl = WAIT_SUCCESS_VALUE;
424     } else {
425         LOG("[cb][ctrl]closed session callback sid[%d] not match open sid[%d]", sessionId, g_currentSessionId4Ctl);
426         g_waitFlag4Ctl = WAIT_FAIL_VALUE;
427     }
428 }
429 
ControlBytesReceived(int sessionId,const void * data,unsigned int dataLen)430 static void ControlBytesReceived(int sessionId, const void* data, unsigned int dataLen)
431 {
432     LOG("[cb][ctrl]ByteRec sid:%d, data len:%u", sessionId, dataLen);
433     if (sessionId < 0 || sessionId > MAX_SESSION_NUM) {
434         LOG("[cb][ctrl]ByteRec invalid session sid[%d]", sessionId);
435         return;
436     }
437     if (data == NULL) {
438         LOG("[cb][ctrl]ByteRec invalid data=null sid[%d]", sessionId);
439         return;
440     }
441     int sid = sessionId % MAX_SESSION_NUM;
442     g_recvByteStat4Control[sid]++;
443 }
444 
ControlMessageReceived(int sessionId,const void * data,unsigned int dataLen)445 static void ControlMessageReceived(int sessionId, const void* data, unsigned int dataLen)
446 {
447     LOG("[cb][ctrl]MessageRec sid:%d, data len:%u", sessionId, dataLen);
448     if (sessionId < 0 || sessionId > MAX_SESSION_NUM) {
449         LOG("[cb][ctrl]MessageRec invalid session sid[%d]", sessionId);
450         return;
451     }
452     if (data == NULL) {
453         LOG("[cb][ctrl]MessageRec invalid data=null sid[%d]", sessionId);
454         return;
455     }
456     int sid = sessionId % MAX_SESSION_NUM;
457     g_recvMsgStat4Control[sid]++;
458 }
459 
PassiveSessionOpened(int sessionId,int result)460 static int PassiveSessionOpened(int sessionId, int result)
461 {
462     LOG("[cb][pass]open session passive sid[%d],rst[%d]", sessionId, result);
463     if (result == SOFTBUS_OK) {
464         g_waitFlag = WAIT_SUCCESS_VALUE;
465     } else {
466         g_waitFlag = WAIT_FAIL_VALUE;
467     }
468     if (g_passiveOpenRetFlag == SOFTBUS_ERR) {
469         LOG("[cb][pass]return fail");
470         g_passiveOpenRetFlag = SOFTBUS_OK;
471         return SOFTBUS_ERR;
472     } else {
473         return SOFTBUS_OK;
474     }
475 }
476 
PassiveSessionClosed(int sessionId)477 static void PassiveSessionClosed(int sessionId)
478 {
479     LOG("[cb][pass]close session passive sid[%d]", sessionId);
480     g_waitFlag = WAIT_SUCCESS_VALUE;
481 }
482 
PassiveBytesReceived(int sessionId,const void * data,unsigned int dataLen)483 static void PassiveBytesReceived(int sessionId, const void* data, unsigned int dataLen)
484 {
485     LOG("[cb][pass]ByteRec sid:%d, data len:%u", sessionId, dataLen);
486     if (data == NULL) {
487         LOG("[cb][pass]ByteRec invalid data=null sid[%d]", sessionId);
488         g_waitFlag = WAIT_FAIL_VALUE;
489         return;
490     }
491     int ret = SendBytes(sessionId, data, dataLen);
492     LOG("[cb][pass]ByteRec send back ret[%d]", ret);
493     if (ret == SOFTBUS_OK) {
494         g_waitFlag = WAIT_SUCCESS_VALUE;
495     } else {
496         g_waitFlag = WAIT_FAIL_VALUE;
497     }
498 }
499 
PassiveMessageReceived(int sessionId,const void * data,unsigned int dataLen)500 static void PassiveMessageReceived(int sessionId, const void* data, unsigned int dataLen)
501 {
502     LOG("[cb][pass]MessageRec sid:%d, data len:%u", sessionId, dataLen);
503     if (data == NULL) {
504         LOG("[cb][pass]MessageRec invalid data=null sid[%d]", sessionId);
505         g_waitFlag = WAIT_FAIL_VALUE;
506         return;
507     }
508     int ret = SendMessage(sessionId, data, dataLen);
509     LOG("[cb][pass]MessageRec send back ret[%d]", ret);
510     if (ret == SOFTBUS_OK) {
511         g_waitFlag = WAIT_SUCCESS_VALUE;
512     } else {
513         g_waitFlag = WAIT_FAIL_VALUE;
514     }
515 }
516 
PerfSessionOpened(int sessionId,int result)517 static int PerfSessionOpened(int sessionId, int result)
518 {
519     g_openSessionTimeEnd = GetCurrentTimeOfMs();
520 
521     if (result == SOFTBUS_OK) {
522         g_waitFlag = WAIT_SUCCESS_VALUE;
523     } else {
524         LOG("[cb][perf]open session result fail, ret:%d", result);
525         g_waitFlag = WAIT_FAIL_VALUE;
526     }
527     return SOFTBUS_OK;
528 }
529 
PerfSessionClosed(int sessionId)530 static void PerfSessionClosed(int sessionId)
531 {
532     LOG("[cb][perf]closed session,sid[%d]", sessionId);
533 }
534 
PerfBytesReceived(int sessionId,const void * data,unsigned int dataLen)535 static void PerfBytesReceived(int sessionId, const void* data, unsigned int dataLen)
536 {
537     g_transTimeEnd = GetCurrentTimeOfMs();
538 
539     LOG("[cb][perf]Byte recv");
540     g_waitFlag = WAIT_SUCCESS_VALUE;
541 }
542 
PerfMessageReceived(int sessionId,const void * data,unsigned int dataLen)543 static void PerfMessageReceived(int sessionId, const void* data, unsigned int dataLen)
544 {
545     LOG("[cb][perf]Message recv");
546 }
547 
ProxySessionOpened(int sessionId,int result)548 static int ProxySessionOpened(int sessionId, int result)
549 {
550     LOG("[cb][proxy]open session proxy sid[%d],rst[%d]", sessionId, result);
551     if (result == SOFTBUS_OK) {
552         g_waitFlag4Proxy = WAIT_SUCCESS_VALUE;
553     } else {
554         g_waitFlag4Proxy = WAIT_FAIL_VALUE;
555     }
556     return SOFTBUS_OK;
557 }
558 
ProxySessionClosed(int sessionId)559 static void ProxySessionClosed(int sessionId)
560 {
561     LOG("[cb][proxy]close session proxy sid[%d]", sessionId);
562     g_waitFlag4Proxy = WAIT_SUCCESS_VALUE;
563 }
564 
ProxyBytesReceived(int sessionId,const void * data,unsigned int dataLen)565 static void ProxyBytesReceived(int sessionId, const void* data, unsigned int dataLen)
566 {
567     LOG("[cb][proxy]ByteRec sid:%d, data len:%u", sessionId, dataLen);
568     if (data == NULL) {
569         LOG("[cb][proxy]ByteRec invalid data=null sid:[%d]", sessionId);
570         g_waitFlag4Proxy = WAIT_FAIL_VALUE;
571     } else {
572         g_waitFlag4Proxy = WAIT_SUCCESS_VALUE;
573     }
574 }
575 
ProxyMessageReceived(int sessionId,const void * data,unsigned int dataLen)576 static void ProxyMessageReceived(int sessionId, const void* data, unsigned int dataLen)
577 {
578     LOG("[cb][proxy]MessageRec sid:%d, data len:%u", sessionId, dataLen);
579     if (data == NULL) {
580         LOG("[cb][proxy]MessageRec invalid data=null sid[%d]", sessionId);
581         g_waitFlag4Proxy = WAIT_FAIL_VALUE;
582     } else {
583         g_waitFlag4Proxy = WAIT_SUCCESS_VALUE;
584     }
585 }
586 
StreamSessionOpened(int sessionId,int result)587 static int StreamSessionOpened(int sessionId, int result)
588 {
589     LOG("[cb][stream]open session sessionId[%d],rst[%d]", sessionId, result);
590     if (result == SOFTBUS_OK) {
591         g_waitFlag4Stream = WAIT_SUCCESS_VALUE;
592     } else {
593         g_waitFlag4Stream = WAIT_FAIL_VALUE;
594     }
595     return SOFTBUS_OK;
596 }
597 
StreamSessionClosed(int sessionId)598 static void StreamSessionClosed(int sessionId)
599 {
600     LOG("[cb][stream]close session sessionId[%d]", sessionId);
601 }
602 
603 
StreamReceived(int sessionId,const StreamData * data,const StreamData * ext,const StreamFrameInfo * frame)604 static void StreamReceived(int sessionId, const StreamData *data, const StreamData *ext, const StreamFrameInfo *frame)
605 {
606     if (data != NULL) {
607         LOG("[cb][stream]ByteRec sessionId:%d, data= %.*s.\n", sessionId, data->bufLen, data->buf);
608         g_waitFlag4Stream = WAIT_SUCCESS_VALUE;
609     }
610 }
611 
ResetWaitFlag(void)612 void ResetWaitFlag(void)
613 {
614     g_waitFlag = WAIT_DEF_VALUE;
615 }
616 
ResetWaitFlag4Data(void)617 void ResetWaitFlag4Data(void)
618 {
619     g_waitFlag4Data = WAIT_DEF_VALUE;
620 }
621 
ResetWaitFlag4Ctl(void)622 void ResetWaitFlag4Ctl(void)
623 {
624     g_waitFlag4Ctl = WAIT_DEF_VALUE;
625 }
626 
ResetWaitFlag4Proxy(void)627 void ResetWaitFlag4Proxy(void)
628 {
629     g_waitFlag4Proxy = WAIT_DEF_VALUE;
630 }
631 
ResetwaitCount4Online(void)632 void ResetwaitCount4Online(void)
633 {
634     g_nodeOnlineCount = 0;
635 }
636 
ResetwaitCount4Offline(void)637 void ResetwaitCount4Offline(void)
638 {
639     g_nodeOfflineCount = 0;
640 }
641 
RegisterDeviceStateDefCallback(void)642 int RegisterDeviceStateDefCallback(void)
643 {
644     return RegNodeDeviceStateCb(DEF_PKG_NAME, &g_defNodeStateCallback);
645 }
646 
UnRegisterDeviceStateDefCallback(void)647 int UnRegisterDeviceStateDefCallback(void)
648 {
649     return UnregNodeDeviceStateCb(&g_defNodeStateCallback);
650 }
651 
CreateSsAndOpenSession4Data(void)652 int CreateSsAndOpenSession4Data(void)
653 {
654     int ret;
655     int timeout = 10;
656     ret = CreateSessionServer(DEF_PKG_NAME, SESSION_NAME_DATA, g_sessionlistener4Data);
657     if (ret != SOFTBUS_OK) {
658         LOG("call CreateSessionServer[data] fail, ret:%d", ret);
659         return ret;
660     }
661 
662     ResetWaitFlag4Data();
663     int sessionId;
664     sessionId = OpenSession(SESSION_NAME_DATA, SESSION_NAME_DATA, g_networkId, DEF_GROUP_ID, g_sessionAttr4Data);
665     if (sessionId < SESSION_ID_MIN) {
666         LOG("call OpenSession[data] fail, ret sid:%d,netId:%s", sessionId, g_networkId);
667         ret = RemoveSessionServer(DEF_PKG_NAME, SESSION_NAME_DATA);
668         if (ret != SOFTBUS_OK) {
669             LOG("RemoveSessionServer[data] fail, ret:%d", ret);
670         }
671         return SOFTBUS_ERR;
672     }
673     SetCurrentSessionId4Data(sessionId);
674     LOG("call OpenSession[data] success,sid:%d", sessionId);
675 
676     ret = Wait4Session(timeout, SESSION_4DATA);
677     if (ret != SOFTBUS_OK) {
678         LOG("OpenSession[data] fail");
679         ret = RemoveSessionServer(DEF_PKG_NAME, SESSION_NAME_DATA);
680         if (ret != SOFTBUS_OK) {
681             LOG("RemoveSessionServer[data] fail, ret:%d", ret);
682         }
683         return SOFTBUS_ERR;
684     }
685     return ret;
686 }
687 
CreateSsAndOpenSession4Ctl(void)688 int CreateSsAndOpenSession4Ctl(void)
689 {
690     int ret;
691     int timeout = 10;
692     ret = CreateSessionServer(DEF_PKG_NAME, SESSION_NAME_CTL, g_sessionlistener4Ctl);
693     if (ret != SOFTBUS_OK) {
694         LOG("call CreateSessionServer[ctl] fail, ret:%d", ret);
695         return ret;
696     }
697 
698     int sessionId;
699     ResetWaitFlag4Ctl();
700     sessionId = OpenSession(SESSION_NAME_CTL, SESSION_NAME_CTL, g_networkId, DEF_GROUP_ID, g_sessionAttr4Ctl);
701     if (sessionId < SESSION_ID_MIN) {
702         LOG("call OpenSession[ctl] fail, ret sid:%d", sessionId);
703         ret = RemoveSessionServer(DEF_PKG_NAME, SESSION_NAME_CTL);
704         if (ret != SOFTBUS_OK) {
705             LOG("RemoveSessionServer[ctl] fail, ret:%d", ret);
706         }
707         return SOFTBUS_ERR;
708     }
709     SetCurrentSessionId4Ctl(sessionId);
710     LOG("call OpenSession[ctl] success,sid:%d", sessionId);
711 
712     ret = Wait4Session(timeout, SESSION_4CTL);
713     if (ret != SOFTBUS_OK) {
714         LOG("OpenSession[ctl] fail");
715         ret = RemoveSessionServer(DEF_PKG_NAME, SESSION_NAME_CTL);
716         if (ret != SOFTBUS_OK) {
717             LOG("RemoveSessionServer[ctl] fail, ret:%d", ret);
718         }
719         return SOFTBUS_ERR;
720     }
721     return ret;
722 }
723 
CreateSsAndOpenSession4Proxy(void)724 int CreateSsAndOpenSession4Proxy(void)
725 {
726     int ret;
727     int timeout = 10;
728     ret = CreateSessionServer(DEF_PKG_NAME, SESSION_NAME_PROXY, g_sessionlistener4Proxy);
729     if (ret != SOFTBUS_OK) {
730         LOG("call CreateSessionServer[Proxy] fail, ret:%d", ret);
731         return ret;
732     }
733 
734     int sessionId;
735     ResetWaitFlag();
736     sessionId = OpenSession(SESSION_NAME_PROXY, SESSION_NAME_PROXY, g_networkId, DEF_GROUP_ID, g_sessionAttr4Proxy);
737     if (sessionId < SESSION_ID_MIN) {
738         LOG("call OpenSession[Proxy] fail, ret sid:%d", sessionId);
739         return SOFTBUS_ERR;
740     }
741     SetCurrentSessionId4Proxy(sessionId);
742     LOG("call OpenSession[Proxy] success,sid:%d", sessionId);
743 
744     ret = Wait4Session(timeout, SESSION_4PROXY);
745     if (ret != SOFTBUS_OK) {
746         LOG("OpenSession[Proxy] fail");
747         return SOFTBUS_ERR;
748     }
749     return ret;
750 }
751 
SendData4Data(DataType type,int size)752 int SendData4Data(DataType type, int size)
753 {
754     int ret;
755     if (size > 0) {
756         g_expectDataContent = (char*)calloc(1, size);
757         if (g_expectDataContent == NULL) {
758             LOG("[send data]calloc fail");
759             return SOFTBUS_ERR;
760         }
761         (void)memset_s(g_expectDataContent, size, g_fillContentChar, size);
762     } else {
763         LOG("[send data]invalid param[size>=1]");
764         return SOFTBUS_ERR;
765     }
766 
767     g_expectDataSize = size;
768     ResetWaitFlag();
769     if (type == DATA_TYPE_MSG) {
770         ret = SendMessage(g_currentSessionId4Data, g_expectDataContent, size);
771     } else if (type == DATA_TYPE_BYTE) {
772         ret = SendBytes(g_currentSessionId4Data, g_expectDataContent, size);
773     } else {
774         LOG("[send data]invalid param[DataType]");
775         free(g_expectDataContent);
776         return SOFTBUS_ERR;
777     }
778     if (ret != SOFTBUS_OK) {
779         LOG("[send data]call SendX fail, ret:%d", ret);
780         free(g_expectDataContent);
781         return SOFTBUS_ERR;
782     }
783     LOG("[send data]call SendX success");
784     int sleeptime = 15;
785     int timeout = 5;
786     if (size > SIZE_1K * SIZE_1K) {
787         timeout = sleeptime;
788     }
789     ret = Wait(timeout);
790     free(g_expectDataContent);
791     g_expectDataContent = NULL;
792     return ret;
793 }
794 
SendData4Message(DataType type,int size)795 int SendData4Message(DataType type, int size)
796 {
797     int ret;
798     if (size > 0) {
799         g_expectMessageContent = (char*)calloc(1, size);
800         if (g_expectMessageContent == NULL) {
801             LOG("[send data]calloc fail");
802             return SOFTBUS_ERR;
803         }
804         (void)memset_s(g_expectMessageContent, size, g_fillContentChar, size);
805     } else {
806         LOG("[send data]invalid param[size>=1]");
807         return SOFTBUS_ERR;
808     }
809 
810     g_expectDataSize = size;
811     ResetWaitFlag();
812     if (type == DATA_TYPE_MSG) {
813         ret = SendMessage(g_currentSessionId4Proxy, g_expectMessageContent, size);
814     } else if (type == DATA_TYPE_BYTE) {
815         ret = SendBytes(g_currentSessionId4Proxy, g_expectMessageContent, size);
816     } else {
817         LOG("[send data]invalid param[DataType]");
818         free(g_expectMessageContent);
819         return SOFTBUS_ERR;
820     }
821     if (ret != SOFTBUS_OK) {
822         LOG("[send data]call SendX fail, ret:%d", ret);
823         free(g_expectMessageContent);
824         return SOFTBUS_ERR;
825     }
826     free(g_expectMessageContent);
827     g_expectMessageContent = NULL;
828     return ret;
829 }
830 
CloseSessionAndRemoveSs4Data(void)831 int CloseSessionAndRemoveSs4Data(void)
832 {
833     int ret4Close;
834     int timeout = 2;
835     // 主动close不回调
836     ResetWaitFlag4Data();
837     CloseSession(g_currentSessionId4Data);
838     ret4Close = Wait4Session(timeout, SESSION_4DATA);
839     if (ret4Close == SOFTBUS_OK) {
840         LOG("CloseSession[data] recv callback");
841     }
842 
843     int ret4Ss;
844     ret4Ss = RemoveSessionServer(DEF_PKG_NAME, SESSION_NAME_DATA);
845     if (ret4Ss != SOFTBUS_OK) {
846         LOG("RemoveSessionServer[data] fail, ret:%d", ret4Ss);
847     }
848 
849     if (ret4Ss != SOFTBUS_OK || ret4Close == SOFTBUS_OK) {
850         return SOFTBUS_ERR;
851     } else {
852         return SOFTBUS_OK;
853     }
854 }
855 
CloseSessionAndRemoveSs4Ctl(void)856 int CloseSessionAndRemoveSs4Ctl(void)
857 {
858     int ret4Close;
859     int timeout = 2;
860     ResetWaitFlag4Ctl();
861     CloseSession(g_currentSessionId4Ctl);
862     ret4Close = Wait4Session(timeout, SESSION_4CTL);
863     if (ret4Close == SOFTBUS_OK) {
864         LOG("CloseSession[ctrl] recv callback");
865     }
866 
867     int ret4Ss;
868     ret4Ss = RemoveSessionServer(DEF_PKG_NAME, SESSION_NAME_CTL);
869     if (ret4Ss != SOFTBUS_OK) {
870         LOG("RemoveSessionServer[ctl] fail, ret:%d", ret4Ss);
871     }
872 
873     if (ret4Ss != SOFTBUS_OK || ret4Close == SOFTBUS_OK) {
874         return SOFTBUS_ERR;
875     } else {
876         return SOFTBUS_OK;
877     }
878 }
879 
CloseSessionAndRemoveSs4Proxy(void)880 int CloseSessionAndRemoveSs4Proxy(void)
881 {
882     int ret4Close;
883     int timeout = 2;
884     ResetWaitFlag();
885     CloseSession(g_currentSessionId4Proxy);
886     ret4Close = Wait(timeout);
887     if (ret4Close == SOFTBUS_OK) {
888         LOG("CloseSession[Proxy] recv callback");
889     }
890 
891     int ret4Ss;
892     ret4Ss = RemoveSessionServer(DEF_PKG_NAME, SESSION_NAME_PROXY);
893     if (ret4Ss != SOFTBUS_OK) {
894         LOG("RemoveSessionServer[Proxy] fail, ret:%d", ret4Ss);
895     }
896 
897     if (ret4Ss != SOFTBUS_OK || ret4Close == SOFTBUS_OK) {
898         return SOFTBUS_ERR;
899     } else {
900         return SOFTBUS_OK;
901     }
902 }
903 
OpenSession4DataByP2p(void)904 int OpenSession4DataByP2p(void)
905 {
906     int ret;
907     int timeout = 10;
908     ResetWaitFlag4Data();
909     int sId = OpenSession(SESSION_NAME_DATA, SESSION_NAME_DATA, g_networkId, DEF_GROUP_ID, g_p2pattribute);
910     if (sId < SESSION_ID_MIN) {
911         LOG("openSession[data] fail,id:%d, netId:%s", sId, g_networkId);
912         return  SOFTBUS_ERR;
913     }
914     SetCurrentSessionId4Data(sId);
915     ret = Wait4Session(timeout, SESSION_4DATA);
916     if (ret != SOFTBUS_OK) {
917         LOG("Wait4Session[data] fail");
918         return SOFTBUS_ERR;
919     }
920     return ret;
921 }
922 
OpenSession4ProxyByP2p(void)923 int OpenSession4ProxyByP2p(void)
924 {
925     int ret;
926     int timeout = 10;
927     ResetWaitFlag4Proxy();
928     int sId = OpenSession(SESSION_NAME_PROXY, SESSION_NAME_PROXY, g_networkId, DEF_GROUP_ID, g_p2pattributeProxy);
929     if (sId < SESSION_ID_MIN) {
930         LOG("openSession[data] fail,id:%d, netId:%s", sId, g_networkId);
931         return SOFTBUS_ERR;
932     }
933     SetCurrentSessionId4Proxy(sId);
934     ret = Wait4Session(timeout, SESSION_4PROXY);
935     if (ret != SOFTBUS_OK) {
936         LOG("Wait4Session[data] fail");
937         return SOFTBUS_ERR;
938     }
939 
940     return ret;
941 }
942 
OpenSessionBatch4Data(char groupId[][GROUP_ID_LEN],int * sessionId,int count)943 int OpenSessionBatch4Data(char groupId[][GROUP_ID_LEN], int* sessionId, int count)
944 {
945     int ret;
946     int timeout = 10;
947     int rstFlag = SOFTBUS_OK;
948     for (int i = 0; i < count; i++) {
949         ResetWaitFlag4Data();
950         int sId = OpenSession(SESSION_NAME_DATA, SESSION_NAME_DATA, g_networkId, groupId[i], g_sessionAttr4Data);
951         if (sId < SESSION_ID_MIN) {
952             LOG("call OpenSession[data] fail,sid:%d,i:%d", sId, i);
953             rstFlag = SOFTBUS_ERR;
954             continue;
955         }
956         SetCurrentSessionId4Data(sId);
957         *(sessionId + i) = sId;
958         ret = Wait4Session(timeout, SESSION_4DATA);
959         if (ret != SOFTBUS_OK) {
960             LOG("Wait4Session[data] fail,i:%d", i);
961             rstFlag = SOFTBUS_ERR;
962         }
963     }
964     return rstFlag;
965 }
966 
OpenSessionBatch4Ctl(char groupId[][GROUP_ID_LEN],int * sessionId,int count)967 int OpenSessionBatch4Ctl(char groupId[][GROUP_ID_LEN], int* sessionId, int count)
968 {
969     int ret;
970     int timeout = 10;
971     int rstFlag = SOFTBUS_OK;
972     for (int i = 0; i < count; i++) {
973         ResetWaitFlag4Ctl();
974         int sId = OpenSession(SESSION_NAME_CTL, SESSION_NAME_CTL, g_networkId, groupId[i], g_sessionAttr4Ctl);
975         if (sId < SESSION_ID_MIN) {
976             LOG("call OpenSession[ctrl] fail,sid:%d,i:%d", sId, i);
977             rstFlag = SOFTBUS_ERR;
978             continue;
979         }
980         SetCurrentSessionId4Ctl(sId);
981         *(sessionId + i) = sId;
982         ret = Wait4Session(timeout, SESSION_4CTL);
983         if (ret != SOFTBUS_OK) {
984             LOG("Wait4Session[ctrl] fail,i:%d", i);
985             rstFlag = SOFTBUS_ERR;
986         }
987     }
988     return rstFlag;
989 }
990 
CloseSessionBatch4Data(int * sessionId,int count)991 int CloseSessionBatch4Data(int* sessionId, int count)
992 {
993     for (int i = 0; i < count; i++) {
994         int sid = *(sessionId + i);
995         LOG("close session[data] sid:%d", sid);
996 
997         SetCurrentSessionId4Data(sid);
998         CloseSession(sid);
999     }
1000     return SOFTBUS_OK;
1001 }
1002 
CloseSessionBatch4Ctl(int * sessionId,int count)1003 int CloseSessionBatch4Ctl(int* sessionId, int count)
1004 {
1005     for (int i = 0; i < count; i++) {
1006         int sid = *(sessionId + i);
1007         LOG("close session[ctrl] sid:%d", sid);
1008 
1009         SetCurrentSessionId4Ctl(sid);
1010         CloseSession(sid);
1011     }
1012     return SOFTBUS_OK;
1013 }
1014 
WaitNodeCount(int timeout,WaitNodeStateType state,int expectCount)1015 int WaitNodeCount(int timeout, WaitNodeStateType state, int expectCount)
1016 {
1017     LOG("Wait4Node,timeout:%d, type:%d, exp count:%d", timeout, state, expectCount);
1018     int hitFlag = -1;
1019     while (timeout > 0) {
1020         sleep(ONE_SECOND);
1021         switch (state) {
1022             case STATE_ONLINE:
1023                 if (g_nodeOnlineCount == expectCount) {
1024                     LOG("Wait4Node[online] succ,timeout:%d", timeout);
1025                     hitFlag = 1;
1026                 }
1027                 break;
1028             case STATE_OFFLINE:
1029                 if (g_nodeOfflineCount == expectCount) {
1030                     LOG("Wait4Node[offline] succ,timeout:%d", timeout);
1031                     hitFlag = 1;
1032                 }
1033                 break;
1034             default:
1035                 LOG("Wait4Node state error");
1036                 hitFlag = 1;
1037                 break;
1038         }
1039         if (hitFlag != -1) {
1040             break;
1041         }
1042         timeout--;
1043     }
1044     switch (state) {
1045         case STATE_ONLINE:
1046             if (g_nodeOnlineCount != expectCount) {
1047                 LOG("Wait4Node[online] fail[exp:%d, real:%d]", expectCount, g_nodeOnlineCount);
1048                 return SOFTBUS_ERR;
1049             }
1050             break;
1051         case STATE_OFFLINE:
1052             if (g_nodeOfflineCount != expectCount) {
1053                 LOG("Wait4Node[offline] fail[exp:%d, real:%d]", expectCount, g_nodeOfflineCount);
1054                 return SOFTBUS_ERR;
1055             }
1056             break;
1057         default:
1058             return SOFTBUS_ERR;
1059     }
1060     return SOFTBUS_OK;
1061 }
1062 
1063 // set/get function
ResetClosedSessionCount4Data(void)1064 void ResetClosedSessionCount4Data(void)
1065 {
1066     g_sessionCloseCount4Data = 0;
1067 }
1068 
ResetClosedSessionCount4Ctrl(void)1069 void ResetClosedSessionCount4Ctrl(void)
1070 {
1071     g_sessionCloseCount4Ctrl = 0;
1072 }
1073 
GetClosedSessionCount4Data(void)1074 int GetClosedSessionCount4Data(void)
1075 {
1076     return g_sessionCloseCount4Data;
1077 }
1078 
GetClosedSessionCount4Ctrl(void)1079 int GetClosedSessionCount4Ctrl(void)
1080 {
1081     return g_sessionCloseCount4Ctrl;
1082 }
1083 
GetNetworkId(void)1084 char* GetNetworkId(void)
1085 {
1086     return g_networkId;
1087 }
1088 
1089 // To one device only
CheckRemoteDeviceIsNull(int isSetNetId)1090 int CheckRemoteDeviceIsNull(int isSetNetId)
1091 {
1092     int nodeNum = 0;
1093     NodeBasicInfo* nodeInfo = NULL;
1094     int ret = GetAllNodeDeviceInfo(DEF_PKG_NAME, &nodeInfo, &nodeNum);
1095     LOG("[check]get node number is:%d, ret:%d", nodeNum, ret);
1096     if (nodeInfo != NULL && nodeNum > 0) {
1097         LOG("[check]get netid is:%s", nodeInfo->networkId);
1098         if (isSetNetId == BOOL_TRUE) {
1099             (void)strncpy_s(g_networkId, NETWORK_ID_BUF_LEN, nodeInfo->networkId, NETWORK_ID_BUF_LEN);
1100         }
1101         FreeNodeInfo(nodeInfo);
1102         return SOFTBUS_OK;
1103     } else {
1104         LOG("[check]get nodeInfo is null");
1105         return SOFTBUS_ERR;
1106     }
1107 }
1108 
GetSessionListenser4Data(void)1109 ISessionListener* GetSessionListenser4Data(void)
1110 {
1111     return g_sessionlistener4Data;
1112 }
1113 
GetSessionListenser4Ctl(void)1114 ISessionListener* GetSessionListenser4Ctl(void)
1115 {
1116     return g_sessionlistener4Ctl;
1117 }
1118 
GetSessionListenser4Proxy(void)1119 ISessionListener* GetSessionListenser4Proxy(void)
1120 {
1121     return g_sessionlistener4Proxy;
1122 }
1123 
GetSessionListenser4Stream(void)1124 ISessionListener* GetSessionListenser4Stream(void)
1125 {
1126     return g_sessionlistener4Stream;
1127 }
1128 
GetSendFileListener(void)1129 IFileSendListener* GetSendFileListener(void)
1130 {
1131     return g_fileSendListener;
1132 }
1133 
GetRecvFileListener(void)1134 IFileReceiveListener* GetRecvFileListener(void)
1135 {
1136     return g_fileRecvListener;
1137 }
1138 
GetSessionAttr4Data(void)1139 SessionAttribute* GetSessionAttr4Data(void)
1140 {
1141     return g_sessionAttr4Data;
1142 }
1143 
GetSessionAttr4DataP2p(void)1144 SessionAttribute* GetSessionAttr4DataP2p(void)
1145 {
1146     return g_p2pattribute;
1147 }
1148 
GetSessionAttr4ProxyP2p(void)1149 SessionAttribute* GetSessionAttr4ProxyP2p(void)
1150 {
1151     return g_p2pattributeProxy;
1152 }
GetSessionAttr4Ctl(void)1153 SessionAttribute* GetSessionAttr4Ctl(void)
1154 {
1155     return g_sessionAttr4Ctl;
1156 }
1157 
GetSessionAttr4Proxy(void)1158 SessionAttribute* GetSessionAttr4Proxy(void)
1159 {
1160     return g_sessionAttr4Proxy;
1161 }
1162 
SetCurrentSessionId4Data(int sessionId)1163 void SetCurrentSessionId4Data(int sessionId)
1164 {
1165     g_currentSessionId4Data = sessionId;
1166 }
1167 
SetCurrentSessionId4Ctl(int sessionId)1168 void SetCurrentSessionId4Ctl(int sessionId)
1169 {
1170     g_currentSessionId4Ctl = sessionId;
1171 }
1172 
SetCurrentSessionId4Proxy(int sessionId)1173 void SetCurrentSessionId4Proxy(int sessionId)
1174 {
1175     g_currentSessionId4Proxy = sessionId;
1176 }
1177 
GetCurrentSessionId4Data(void)1178 int GetCurrentSessionId4Data(void)
1179 {
1180     return g_currentSessionId4Data;
1181 }
1182 
GetCurrentSessionId4Ctl(void)1183 int GetCurrentSessionId4Ctl(void)
1184 {
1185     return g_currentSessionId4Ctl;
1186 }
1187 
GetCurrentSessionId4Proxy(void)1188 int GetCurrentSessionId4Proxy(void)
1189 {
1190     return g_currentSessionId4Proxy;
1191 }
1192 
GetOpenSessionCount4Proxy(void)1193 int GetOpenSessionCount4Proxy(void)
1194 {
1195     return g_sessionOpenCount4Proxy;
1196 }
1197 
TestSetUp(void)1198 void TestSetUp(void)
1199 {
1200     g_defNodeStateCallback.events = EVENT_NODE_STATE_MASK;
1201     g_defNodeStateCallback.onNodeOnline = OnDefNodeOnline;
1202     g_defNodeStateCallback.onNodeOffline = OnDefNodeOffline;
1203     g_defNodeStateCallback.onNodeBasicInfoChanged = OnDefNodeBasicInfoChanged;
1204     g_defNodeStateCallback.onNodeStatusChanged = onDefNodeStatusChanged;
1205     g_defDiscCallback.OnDeviceFound = OnDefDeviceFound;
1206     g_defDiscCallback.OnDiscoverFailed = OnDefDiscoverFail;
1207     g_defDiscCallback.OnDiscoverySuccess = OnDefDiscoverSuccess;
1208 
1209     if (g_sessionlistener4Data == NULL) {
1210         g_sessionlistener4Data = (ISessionListener*)calloc(1, sizeof(ISessionListener));
1211         g_sessionlistener4Data->OnSessionOpened = DataSessionOpened;
1212         g_sessionlistener4Data->OnSessionClosed = DataSessionClosed;
1213         g_sessionlistener4Data->OnMessageReceived = DataMessageReceived;
1214         g_sessionlistener4Data->OnBytesReceived = DataBytesReceived;
1215     }
1216     if (g_sessionlistener4Ctl == NULL) {
1217         g_sessionlistener4Ctl = (ISessionListener*)calloc(1, sizeof(ISessionListener));
1218         g_sessionlistener4Ctl->OnSessionOpened = ControlSessionOpened;
1219         g_sessionlistener4Ctl->OnSessionClosed = ControlSessionClosed;
1220         g_sessionlistener4Ctl->OnMessageReceived = ControlMessageReceived;
1221         g_sessionlistener4Ctl->OnBytesReceived = ControlBytesReceived;
1222     }
1223     if (g_sessionlistener4Pass == NULL) {
1224         g_sessionlistener4Pass = (ISessionListener*)calloc(1, sizeof(ISessionListener));
1225         g_sessionlistener4Pass->OnSessionOpened = PassiveSessionOpened;
1226         g_sessionlistener4Pass->OnSessionClosed = PassiveSessionClosed;
1227         g_sessionlistener4Pass->OnMessageReceived = PassiveMessageReceived;
1228         g_sessionlistener4Pass->OnBytesReceived = PassiveBytesReceived;
1229     }
1230     if (g_sessionlistener4Perf == NULL) {
1231         g_sessionlistener4Perf = (ISessionListener*)calloc(1, sizeof(ISessionListener));
1232         g_sessionlistener4Perf->OnSessionOpened = PerfSessionOpened;
1233         g_sessionlistener4Perf->OnSessionClosed = PerfSessionClosed;
1234         g_sessionlistener4Perf->OnMessageReceived = PerfMessageReceived;
1235         g_sessionlistener4Perf->OnBytesReceived = PerfBytesReceived;
1236     }
1237     if (g_sessionlistener4Proxy == NULL) {
1238         g_sessionlistener4Proxy = (ISessionListener*)calloc(1, sizeof(ISessionListener));
1239         g_sessionlistener4Proxy->OnSessionOpened = ProxySessionOpened;
1240         g_sessionlistener4Proxy->OnSessionClosed = ProxySessionClosed;
1241         g_sessionlistener4Proxy->OnMessageReceived = ProxyMessageReceived;
1242         g_sessionlistener4Proxy->OnBytesReceived = ProxyBytesReceived;
1243     }
1244     if (g_sessionlistener4Stream == NULL) {
1245         g_sessionlistener4Stream = (ISessionListener*)calloc(1, sizeof(ISessionListener));
1246         g_sessionlistener4Stream->OnSessionOpened = StreamSessionOpened;
1247         g_sessionlistener4Stream->OnSessionClosed = StreamSessionClosed;
1248         g_sessionlistener4Stream->OnStreamReceived = StreamReceived;
1249     }
1250     if (g_fileSendListener == NULL) {
1251         g_fileSendListener = (IFileSendListener*)calloc(1, sizeof(IFileSendListener));
1252         g_fileSendListener->OnSendFileProcess = OnSendFileProcess;
1253         g_fileSendListener->OnSendFileFinished = OnSendFileFinished;
1254         g_fileSendListener->OnFileTransError = OnSendFileTransError;
1255     };
1256     if (g_fileRecvListener == NULL) {
1257         g_fileRecvListener = (IFileReceiveListener*)calloc(1, sizeof(IFileReceiveListener));
1258         g_fileRecvListener->OnReceiveFileStarted = OnReceiveFileStarted;
1259         g_fileRecvListener->OnReceiveFileProcess = OnReceiveFileProcess;
1260         g_fileRecvListener->OnReceiveFileFinished = OnReceiveFileFinished;
1261         g_fileRecvListener->OnFileTransError = OnRecvFileTransError;
1262     }
1263     if (g_sessionAttr4Data == NULL) {
1264         g_sessionAttr4Data = (SessionAttribute*)calloc(1, sizeof(SessionAttribute));
1265         g_sessionAttr4Data->dataType = TYPE_BYTES;
1266     }
1267     if (g_sessionAttr4Ctl == NULL) {
1268         g_sessionAttr4Ctl = (SessionAttribute*)calloc(1, sizeof(SessionAttribute));
1269         g_sessionAttr4Ctl->dataType = TYPE_BYTES;
1270     }
1271     if (g_sessionAttr4Pass == NULL) {
1272         g_sessionAttr4Pass = (SessionAttribute*)calloc(1, sizeof(SessionAttribute));
1273         g_sessionAttr4Pass->dataType = TYPE_BYTES;
1274     }
1275     if (g_sessionAttr4Perf == NULL) {
1276         g_sessionAttr4Perf = (SessionAttribute*)calloc(1, sizeof(SessionAttribute));
1277         g_sessionAttr4Perf->dataType = TYPE_BYTES;
1278     }
1279     if (g_sessionAttr4Proxy == NULL) {
1280         g_sessionAttr4Proxy = (SessionAttribute*)calloc(1, sizeof(SessionAttribute));
1281         g_sessionAttr4Proxy->dataType = TYPE_MESSAGE;
1282     }
1283     if (g_p2pattribute == NULL) {
1284         g_p2pattribute = (SessionAttribute*)calloc(1, sizeof(SessionAttribute));
1285         g_p2pattribute->dataType = TYPE_BYTES;
1286         g_p2pattribute->linkTypeNum = 1;
1287         g_p2pattribute->linkType[0] = LINK_TYPE_WIFI_P2P;
1288     }
1289     if (g_p2pattributeProxy == NULL) {
1290         g_p2pattributeProxy = (SessionAttribute*)calloc(1, sizeof(SessionAttribute));
1291         g_p2pattributeProxy->dataType = TYPE_MESSAGE;
1292         g_p2pattributeProxy->linkTypeNum = 1;
1293         g_p2pattributeProxy->linkType[0] = LINK_TYPE_WIFI_P2P;
1294     }
1295     if (g_barrier == NULL) {
1296         g_barrier = (pthread_barrier_t*)calloc(1, sizeof(pthread_barrier_t));
1297     }
1298     if (g_sId4Task2 == NULL) {
1299         g_sId4Task2 = (int*)malloc(sizeof(int) * DEF_SEND_DATA_SID_COUNT);
1300     }
1301     if (g_sId4Task3 == NULL) {
1302         g_sId4Task3 = (int*)malloc(sizeof(int) * DEF_SEND_DATA_SID_COUNT);
1303     }
1304 }
1305 
TestTearDown(void)1306 void TestTearDown(void)
1307 {
1308     if (g_sessionlistener4Data != NULL) {
1309         free(g_sessionlistener4Data);
1310         g_sessionlistener4Data = NULL;
1311     }
1312     if (g_sessionlistener4Ctl != NULL) {
1313         free(g_sessionlistener4Ctl);
1314         g_sessionlistener4Ctl = NULL;
1315     }
1316     if (g_sessionlistener4Pass != NULL) {
1317         free(g_sessionlistener4Pass);
1318         g_sessionlistener4Pass = NULL;
1319     }
1320     if (g_sessionlistener4Perf != NULL) {
1321         free(g_sessionlistener4Perf);
1322         g_sessionlistener4Perf = NULL;
1323     }
1324     if (g_sessionlistener4Proxy != NULL) {
1325         free(g_sessionlistener4Proxy);
1326         g_sessionlistener4Proxy = NULL;
1327     }
1328     if (g_sessionlistener4Stream != NULL) {
1329         free(g_sessionlistener4Stream);
1330         g_sessionlistener4Stream = NULL;
1331     }
1332     if (g_fileRecvListener != NULL) {
1333         free(g_fileRecvListener);
1334         g_fileRecvListener = NULL;
1335     }
1336     if (g_fileSendListener != NULL) {
1337         free(g_fileSendListener);
1338         g_fileSendListener = NULL;
1339     }
1340     if (g_sessionAttr4Data != NULL) {
1341         free(g_sessionAttr4Data);
1342         g_sessionAttr4Data = NULL;
1343     }
1344     if (g_sessionAttr4Ctl != NULL) {
1345         free(g_sessionAttr4Ctl);
1346         g_sessionAttr4Ctl = NULL;
1347     }
1348     if (g_sessionAttr4Pass != NULL) {
1349         free(g_sessionAttr4Pass);
1350         g_sessionAttr4Pass = NULL;
1351     }
1352     if (g_sessionAttr4Perf != NULL) {
1353         free(g_sessionAttr4Perf);
1354         g_sessionAttr4Perf = NULL;
1355     }
1356     if (g_sessionAttr4Proxy != NULL) {
1357         free(g_sessionAttr4Proxy);
1358         g_sessionAttr4Proxy = NULL;
1359     }
1360     if (g_p2pattribute != NULL) {
1361         free(g_p2pattribute);
1362         g_p2pattribute = NULL;
1363     }
1364     if (g_p2pattributeProxy != NULL) {
1365         free(g_p2pattributeProxy);
1366         g_p2pattributeProxy = NULL;
1367     }
1368     if (g_barrier != NULL) {
1369         free(g_barrier);
1370         g_barrier = NULL;
1371     }
1372     if (g_sId4Task2 != NULL) {
1373         free(g_sId4Task2);
1374         g_sId4Task2 = NULL;
1375     }
1376     if (g_sId4Task3 != NULL) {
1377         free(g_sId4Task3);
1378         g_sId4Task3 = NULL;
1379     }
1380 }
1381 
AddPermission(void)1382 void AddPermission(void)
1383 {
1384     uint64_t tokenId;
1385     const char *perms[2];
1386     perms[0] = OHOS_PERMISSION_DISTRIBUTED_SOFTBUS_CENTER;
1387     perms[1] = OHOS_PERMISSION_DISTRIBUTED_DATASYNC;
1388     NativeTokenInfoParams infoTnstance = {
1389         .dcapsNum = 0,
1390         .permsNum = 2,
1391         .aclsNum = 0,
1392         .dcaps = NULL,
1393         .perms = perms,
1394         .acls = NULL,
1395         .processName = "dsoftbus_test_service",
1396         .aplStr = "system_core",
1397     };
1398     tokenId = GetAccessTokenId(&infoTnstance);
1399     SetSelfTokenID(tokenId);
1400 }