1060ff233Sopenharmony_ci/* 2060ff233Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 3060ff233Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4060ff233Sopenharmony_ci * you may not use this file except in compliance with the License. 5060ff233Sopenharmony_ci * You may obtain a copy of the License at 6060ff233Sopenharmony_ci * 7060ff233Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8060ff233Sopenharmony_ci * 9060ff233Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10060ff233Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11060ff233Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12060ff233Sopenharmony_ci * See the License for the specific language governing permissions and 13060ff233Sopenharmony_ci * limitations under the License. 14060ff233Sopenharmony_ci */ 15060ff233Sopenharmony_ci 16060ff233Sopenharmony_ci#include "test_suite.h" 17060ff233Sopenharmony_ci 18060ff233Sopenharmony_ci#include <getopt.h> 19060ff233Sopenharmony_ci#include <cstddef> 20060ff233Sopenharmony_ci#include <cstdint> 21060ff233Sopenharmony_ci#include <cstdio> 22060ff233Sopenharmony_ci#include <sys/time.h> 23060ff233Sopenharmony_ci#include <sys/times.h> 24060ff233Sopenharmony_ci#include <ctime> 25060ff233Sopenharmony_ci#include <unistd.h> 26060ff233Sopenharmony_ci#include <cinttypes> 27060ff233Sopenharmony_ci 28060ff233Sopenharmony_ci#include "transport/session.h" 29060ff233Sopenharmony_ci#include "softbus_error_code.h" 30060ff233Sopenharmony_ci 31060ff233Sopenharmony_civolatile bool g_sessionEnabled = false; 32060ff233Sopenharmony_ciint32_t g_sessionId = -1; 33060ff233Sopenharmony_ci 34060ff233Sopenharmony_cistatic int32_t EsOnSessionOpened(int32_t sessionId, int32_t result) 35060ff233Sopenharmony_ci{ 36060ff233Sopenharmony_ci LOG("%s:enter", __func__); 37060ff233Sopenharmony_ci if (result != SOFTBUS_OK) { 38060ff233Sopenharmony_ci LOG("%s:OpenSession failed!errCode=%d", __func__, result); 39060ff233Sopenharmony_ci return 0; 40060ff233Sopenharmony_ci } 41060ff233Sopenharmony_ci if (sessionId == g_sessionId) { 42060ff233Sopenharmony_ci LOG("%s:Session %d opened!", __func__, sessionId); 43060ff233Sopenharmony_ci g_sessionEnabled = true; 44060ff233Sopenharmony_ci } 45060ff233Sopenharmony_ci LOG("%s:Unexpected session %d opened!", __func__, sessionId); 46060ff233Sopenharmony_ci return 0; 47060ff233Sopenharmony_ci} 48060ff233Sopenharmony_ci 49060ff233Sopenharmony_cistatic void EsOnSessionClosed(int32_t sessionId) 50060ff233Sopenharmony_ci{ 51060ff233Sopenharmony_ci LOG("%s:enter", __func__); 52060ff233Sopenharmony_ci if (sessionId == g_sessionId) { 53060ff233Sopenharmony_ci g_sessionEnabled = false; 54060ff233Sopenharmony_ci g_sessionId = -1; 55060ff233Sopenharmony_ci } 56060ff233Sopenharmony_ci} 57060ff233Sopenharmony_ci 58060ff233Sopenharmony_cistatic int32_t TsOnReceiveFileStarted(int32_t sessionId, const char *files, int32_t fileCnt) 59060ff233Sopenharmony_ci{ 60060ff233Sopenharmony_ci LOG("%s:session=%d, files=%s, count=%d", __func__, sessionId, files, fileCnt); 61060ff233Sopenharmony_ci return 0; 62060ff233Sopenharmony_ci} 63060ff233Sopenharmony_ci 64060ff233Sopenharmony_cistatic int32_t TsOnReceiveFileProcess(int32_t sessionId, const char *firstFile, 65060ff233Sopenharmony_ci uint64_t bytesUpload, uint64_t bytesTotal) 66060ff233Sopenharmony_ci{ 67060ff233Sopenharmony_ci LOG("%s:session=%d, firstFile=%s, bytesUpload=%" PRIu64 ", bytesTotal=%" PRIu64, __func__, sessionId, firstFile, 68060ff233Sopenharmony_ci bytesUpload, bytesTotal); 69060ff233Sopenharmony_ci return 0; 70060ff233Sopenharmony_ci} 71060ff233Sopenharmony_cistatic void TsOnReceiveFileFinished(int32_t sessionId, const char *files, int32_t fileCnt) 72060ff233Sopenharmony_ci{ 73060ff233Sopenharmony_ci LOG("%s:session=%d, files=%s, count=%d", __func__, sessionId, files, fileCnt); 74060ff233Sopenharmony_ci} 75060ff233Sopenharmony_cistatic void TsOnFileTransError(int32_t sessionId) 76060ff233Sopenharmony_ci{ 77060ff233Sopenharmony_ci LOG("%s:session=%d", __func__, sessionId); 78060ff233Sopenharmony_ci} 79060ff233Sopenharmony_ci 80060ff233Sopenharmony_cistatic int32_t ExecTestSuite(void) 81060ff233Sopenharmony_ci{ 82060ff233Sopenharmony_ci static ISessionListener listener = {.OnSessionOpened = EsOnSessionOpened, 83060ff233Sopenharmony_ci .OnSessionClosed = EsOnSessionClosed, 84060ff233Sopenharmony_ci .OnBytesReceived = EsOnDataReceived, 85060ff233Sopenharmony_ci .OnMessageReceived = EsOnDataReceived, 86060ff233Sopenharmony_ci .OnStreamReceived = EsOnStreamReceived, 87060ff233Sopenharmony_ci .OnQosEvent = EsOnQosEvent}; 88060ff233Sopenharmony_ci 89060ff233Sopenharmony_ci int32_t ret = CreateSessionServer(ECHO_SERVICE_PKGNAME, ECHO_SERVICE_SESSION_NAME, &listener); 90060ff233Sopenharmony_ci if (ret != SOFTBUS_OK) { 91060ff233Sopenharmony_ci LOG("%s:create session server failed!ret=%d", __func__, ret); 92060ff233Sopenharmony_ci return ret; 93060ff233Sopenharmony_ci } 94060ff233Sopenharmony_ci 95060ff233Sopenharmony_ci static IFileReceiveListener fileRecvListener = { 96060ff233Sopenharmony_ci .OnReceiveFileStarted = TsOnReceiveFileStarted, 97060ff233Sopenharmony_ci .OnReceiveFileProcess = TsOnReceiveFileProcess, 98060ff233Sopenharmony_ci .OnReceiveFileFinished = TsOnReceiveFileFinished, 99060ff233Sopenharmony_ci .OnFileTransError = TsOnFileTransError, 100060ff233Sopenharmony_ci }; 101060ff233Sopenharmony_ci 102060ff233Sopenharmony_ci ret = 103060ff233Sopenharmony_ci SetFileReceiveListener(ECHO_SERVICE_PKGNAME, ECHO_SERVICE_SESSION_NAME, &fileRecvListener, "/data/recv_files"); 104060ff233Sopenharmony_ci if (ret != SOFTBUS_OK) { 105060ff233Sopenharmony_ci LOG("%s:set file receive listener failed! ret=%d", __func__, ret); 106060ff233Sopenharmony_ci return ret; 107060ff233Sopenharmony_ci } 108060ff233Sopenharmony_ci 109060ff233Sopenharmony_ci LOG("type x to exit:"); 110060ff233Sopenharmony_ci char c = '0'; 111060ff233Sopenharmony_ci do { 112060ff233Sopenharmony_ci c = getchar(); 113060ff233Sopenharmony_ci } while (c != 'x'); 114060ff233Sopenharmony_ci 115060ff233Sopenharmony_ci ret = RemoveSessionServer(ECHO_SERVICE_PKGNAME, ECHO_SERVICE_SESSION_NAME); 116060ff233Sopenharmony_ci if (ret != SOFTBUS_OK) { 117060ff233Sopenharmony_ci LOG("%s: remove session server failed! ret= %d", __func__, ret); 118060ff233Sopenharmony_ci } 119060ff233Sopenharmony_ci 120060ff233Sopenharmony_ci return ret; 121060ff233Sopenharmony_ci} 122060ff233Sopenharmony_ci 123060ff233Sopenharmony_ciint32_t main(int32_t argc, char * const *argv) 124060ff233Sopenharmony_ci{ 125060ff233Sopenharmony_ci LOG("%s:started", __func__); 126060ff233Sopenharmony_ci 127060ff233Sopenharmony_ci int32_t ret = ExecTestSuite(); 128060ff233Sopenharmony_ci if (ret != SOFTBUS_OK) { 129060ff233Sopenharmony_ci LOG("%s:test failed!ret=%d", __func__, ret); 130060ff233Sopenharmony_ci } 131060ff233Sopenharmony_ci return ret; 132060ff233Sopenharmony_ci} 133