1 /*
2  * Copyright (c) 2022 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 "test_suite.h"
17 
18 #include <getopt.h>
19 #include <cstddef>
20 #include <cstdint>
21 #include <cstdio>
22 #include <sys/time.h>
23 #include <sys/times.h>
24 #include <ctime>
25 #include <unistd.h>
26 #include <cinttypes>
27 
28 #include "transport/session.h"
29 #include "softbus_error_code.h"
30 
31 volatile bool g_sessionEnabled = false;
32 int g_sessionId = -1;
33 
EsOnSessionOpened(int sessionId,int result)34 static int EsOnSessionOpened(int sessionId, int result)
35 {
36     LOG("%s:enter", __func__);
37     if (result != SOFTBUS_OK) {
38         LOG("%s:OpenSession failed!errCode=%d", __func__, result);
39         return 0;
40     }
41     if (sessionId == g_sessionId) {
42         LOG("%s:Session %d opened!", __func__, sessionId);
43         g_sessionEnabled = true;
44     }
45     LOG("%s:Unexpected session %d opened!", __func__, sessionId);
46     return 0;
47 }
48 
EsOnSessionClosed(int sessionId)49 static void EsOnSessionClosed(int sessionId)
50 {
51     LOG("%s:enter", __func__);
52     if (sessionId == g_sessionId) {
53         g_sessionEnabled = false;
54         g_sessionId = -1;
55     }
56 }
57 
TsOnReceiveFileStarted(int sessionId,const char * files,int fileCnt)58 static int TsOnReceiveFileStarted(int sessionId, const char *files, int fileCnt)
59 {
60     LOG("%s:session=%d, files=%s, count=%d", __func__, sessionId, files, fileCnt);
61     return 0;
62 }
63 
TsOnReceiveFileProcess(int sessionId,const char * firstFile,uint64_t bytesUpload,uint64_t bytesTotal)64 static int TsOnReceiveFileProcess(int sessionId, const char *firstFile, uint64_t bytesUpload, uint64_t bytesTotal)
65 {
66     LOG("%s:session=%d, firstFile=%s, bytesUpload=%" PRIu64 ", bytesTotal=%" PRIu64, __func__, sessionId, firstFile,
67         bytesUpload, bytesTotal);
68     return 0;
69 }
TsOnReceiveFileFinished(int sessionId,const char * files,int fileCnt)70 static void TsOnReceiveFileFinished(int sessionId, const char *files, int fileCnt)
71 {
72     LOG("%s:session=%d, files=%s, count=%d", __func__, sessionId, files, fileCnt);
73 }
TsOnFileTransError(int sessionId)74 static void TsOnFileTransError(int sessionId)
75 {
76     LOG("%s:session=%d", __func__, sessionId);
77 }
78 
ExecTestSuite(void)79 static int ExecTestSuite(void)
80 {
81     static ISessionListener listener = {.OnSessionOpened = EsOnSessionOpened,
82         .OnSessionClosed = EsOnSessionClosed,
83         .OnBytesReceived = EsOnDataReceived,
84         .OnMessageReceived = EsOnDataReceived,
85         .OnStreamReceived = EsOnStreamReceived,
86         .OnQosEvent = EsOnQosEvent};
87 
88     int ret = CreateSessionServer(ECHO_SERVICE_PKGNAME, ECHO_SERVICE_SESSION_NAME, &listener);
89     if (ret != SOFTBUS_OK) {
90         LOG("%s:create session server failed!ret=%d", __func__, ret);
91         return ret;
92     }
93 
94     static IFileReceiveListener fileRecvListener = {
95         .OnReceiveFileStarted = TsOnReceiveFileStarted,
96         .OnReceiveFileProcess = TsOnReceiveFileProcess,
97         .OnReceiveFileFinished = TsOnReceiveFileFinished,
98         .OnFileTransError = TsOnFileTransError,
99     };
100 
101     ret =
102         SetFileReceiveListener(ECHO_SERVICE_PKGNAME, ECHO_SERVICE_SESSION_NAME, &fileRecvListener, "/data/recv_files");
103     if (ret != SOFTBUS_OK) {
104         LOG("%s:set file receive listener failed! ret=%d", __func__, ret);
105         return ret;
106     }
107 
108     LOG("type x to exit:");
109     char c = '0';
110     do {
111         c = getchar();
112     } while (c != 'x');
113 
114     ret = RemoveSessionServer(ECHO_SERVICE_PKGNAME, ECHO_SERVICE_SESSION_NAME);
115     if (ret != SOFTBUS_OK) {
116         LOG("%s: remove session server failed! ret= %d", __func__, ret);
117     }
118 
119     return ret;
120 }
121 
main(int argc,char * const * argv)122 int main(int argc, char * const *argv)
123 {
124     LOG("%s:started", __func__);
125 
126     int ret = ExecTestSuite();
127     if (ret != SOFTBUS_OK) {
128         LOG("%s:test failed!ret=%d", __func__, ret);
129     }
130     return ret;
131 }