• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
EsOnDataReceived(int sessionId,const void * data,unsigned int dataLen)58 static void EsOnDataReceived(int sessionId, const void *data, unsigned int dataLen)
59 {
60     LOG("%s:enter", __func__);
61 }
62 
EsOnStreamReceived(int sessionId,const StreamData * data,const StreamData * ext,const StreamFrameInfo * param)63 static void EsOnStreamReceived(
64     int sessionId, const StreamData *data, const StreamData *ext, const StreamFrameInfo *param)
65 {
66     LOG("%s:enter", __func__);
67 }
EsOnQosEvent(int sessionId,int eventId,int tvCount,const QosTv * tvList)68 static void EsOnQosEvent(int sessionId, int eventId, int tvCount, const QosTv *tvList)
69 {
70     LOG("%s:enter", __func__);
71 }
72 
TsOnReceiveFileStarted(int sessionId,const char * files,int fileCnt)73 static int TsOnReceiveFileStarted(int sessionId, const char *files, int fileCnt)
74 {
75     LOG("%s:session=%d, files=%s, count=%d", __func__, sessionId, files, fileCnt);
76     return 0;
77 }
78 
TsOnReceiveFileProcess(int sessionId,const char * firstFile,uint64_t bytesUpload,uint64_t bytesTotal)79 static int TsOnReceiveFileProcess(int sessionId, const char *firstFile, uint64_t bytesUpload, uint64_t bytesTotal)
80 {
81     LOG("%s:session=%d, firstFile=%s, bytesUpload=%" PRIu64 ", bytesTotal=%" PRIu64, __func__, sessionId, firstFile,
82         bytesUpload, bytesTotal);
83     return 0;
84 }
TsOnReceiveFileFinished(int sessionId,const char * files,int fileCnt)85 static void TsOnReceiveFileFinished(int sessionId, const char *files, int fileCnt)
86 {
87     LOG("%s:session=%d, files=%s, count=%d", __func__, sessionId, files, fileCnt);
88 }
TsOnFileTransError(int sessionId)89 static void TsOnFileTransError(int sessionId)
90 {
91     LOG("%s:session=%d", __func__, sessionId);
92 }
93 
ExecTestSuite(void)94 static int ExecTestSuite(void)
95 {
96     static ISessionListener listener = {.OnSessionOpened = EsOnSessionOpened,
97         .OnSessionClosed = EsOnSessionClosed,
98         .OnBytesReceived = EsOnDataReceived,
99         .OnMessageReceived = EsOnDataReceived,
100         .OnStreamReceived = EsOnStreamReceived,
101         .OnQosEvent = EsOnQosEvent};
102 
103     int ret = CreateSessionServer(ECHO_SERVICE_PKGNAME, ECHO_SERVICE_SESSION_NAME, &listener);
104     if (ret != SOFTBUS_OK) {
105         LOG("%s:create session server failed!ret=%d", __func__, ret);
106         return ret;
107     }
108 
109     static IFileReceiveListener fileRecvListener = {
110         .OnReceiveFileStarted = TsOnReceiveFileStarted,
111         .OnReceiveFileProcess = TsOnReceiveFileProcess,
112         .OnReceiveFileFinished = TsOnReceiveFileFinished,
113         .OnFileTransError = TsOnFileTransError,
114     };
115 
116     ret =
117         SetFileReceiveListener(ECHO_SERVICE_PKGNAME, ECHO_SERVICE_SESSION_NAME, &fileRecvListener, "/data/recv_files");
118     if (ret != SOFTBUS_OK) {
119         LOG("%s:set file receive listener failed! ret=%d", __func__, ret);
120         return ret;
121     }
122 
123     LOG("type x to exit:");
124     char c = '0';
125     do {
126         c = getchar();
127     } while (c != 'x');
128 
129     ret = RemoveSessionServer(ECHO_SERVICE_PKGNAME, ECHO_SERVICE_SESSION_NAME);
130     if (ret != SOFTBUS_OK) {
131         LOG("%s: remove session server failed! ret= %d", __func__, ret);
132     }
133 
134     return ret;
135 }
136 
main(int argc,char * const * argv)137 int main(int argc, char * const *argv)
138 {
139     LOG("%s:started", __func__);
140 
141     int ret = ExecTestSuite();
142     if (ret != SOFTBUS_OK) {
143         LOG("%s:test failed!ret=%d", __func__, ret);
144     }
145     return ret;
146 }