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 int32_t g_sessionId = -1;
33 
EsOnSessionOpened(int32_t sessionId, int32_t result)34 static int32_t EsOnSessionOpened(int32_t sessionId, int32_t 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(int32_t sessionId)49 static void EsOnSessionClosed(int32_t sessionId)
50 {
51     LOG("%s:enter", __func__);
52     if (sessionId == g_sessionId) {
53         g_sessionEnabled = false;
54         g_sessionId = -1;
55     }
56 }
57 
TsOnReceiveFileStarted(int32_t sessionId, const char *files, int32_t fileCnt)58 static int32_t TsOnReceiveFileStarted(int32_t sessionId, const char *files, int32_t fileCnt)
59 {
60     LOG("%s:session=%d, files=%s, count=%d", __func__, sessionId, files, fileCnt);
61     return 0;
62 }
63 
TsOnReceiveFileProcess(int32_t sessionId, const char *firstFile, uint64_t bytesUpload, uint64_t bytesTotal)64 static int32_t TsOnReceiveFileProcess(int32_t sessionId, const char *firstFile,
65                                       uint64_t bytesUpload, uint64_t bytesTotal)
66 {
67     LOG("%s:session=%d, firstFile=%s, bytesUpload=%" PRIu64 ", bytesTotal=%" PRIu64, __func__, sessionId, firstFile,
68         bytesUpload, bytesTotal);
69     return 0;
70 }
TsOnReceiveFileFinished(int32_t sessionId, const char *files, int32_t fileCnt)71 static void TsOnReceiveFileFinished(int32_t sessionId, const char *files, int32_t fileCnt)
72 {
73     LOG("%s:session=%d, files=%s, count=%d", __func__, sessionId, files, fileCnt);
74 }
TsOnFileTransError(int32_t sessionId)75 static void TsOnFileTransError(int32_t sessionId)
76 {
77     LOG("%s:session=%d", __func__, sessionId);
78 }
79 
ExecTestSuite(void)80 static int32_t ExecTestSuite(void)
81 {
82     static ISessionListener listener = {.OnSessionOpened = EsOnSessionOpened,
83         .OnSessionClosed = EsOnSessionClosed,
84         .OnBytesReceived = EsOnDataReceived,
85         .OnMessageReceived = EsOnDataReceived,
86         .OnStreamReceived = EsOnStreamReceived,
87         .OnQosEvent = EsOnQosEvent};
88 
89     int32_t ret = CreateSessionServer(ECHO_SERVICE_PKGNAME, ECHO_SERVICE_SESSION_NAME, &listener);
90     if (ret != SOFTBUS_OK) {
91         LOG("%s:create session server failed!ret=%d", __func__, ret);
92         return ret;
93     }
94 
95     static IFileReceiveListener fileRecvListener = {
96         .OnReceiveFileStarted = TsOnReceiveFileStarted,
97         .OnReceiveFileProcess = TsOnReceiveFileProcess,
98         .OnReceiveFileFinished = TsOnReceiveFileFinished,
99         .OnFileTransError = TsOnFileTransError,
100     };
101 
102     ret =
103         SetFileReceiveListener(ECHO_SERVICE_PKGNAME, ECHO_SERVICE_SESSION_NAME, &fileRecvListener, "/data/recv_files");
104     if (ret != SOFTBUS_OK) {
105         LOG("%s:set file receive listener failed! ret=%d", __func__, ret);
106         return ret;
107     }
108 
109     LOG("type x to exit:");
110     char c = '0';
111     do {
112         c = getchar();
113     } while (c != 'x');
114 
115     ret = RemoveSessionServer(ECHO_SERVICE_PKGNAME, ECHO_SERVICE_SESSION_NAME);
116     if (ret != SOFTBUS_OK) {
117         LOG("%s: remove session server failed! ret= %d", __func__, ret);
118     }
119 
120     return ret;
121 }
122 
main(int32_t argc, char * const *argv)123 int32_t main(int32_t argc, char * const *argv)
124 {
125     LOG("%s:started", __func__);
126 
127     int32_t ret = ExecTestSuite();
128     if (ret != SOFTBUS_OK) {
129         LOG("%s:test failed!ret=%d", __func__, ret);
130     }
131     return ret;
132 }
133