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 #ifndef NET_TRANS_COMMON_H 17 #define NET_TRANS_COMMON_H 18 19 #include <pthread.h> 20 #include <securec.h> 21 #include <sys/time.h> 22 #include <unistd.h> 23 24 #include "common_list.h" 25 #include "discovery_service.h" 26 #include "session.h" 27 #include "softbus_bus_center.h" 28 #include "softbus_common.h" 29 #include "softbus_def.h" 30 #include "softbus_errcode.h" 31 #include "softbus_utils.h" 32 33 #include "nativetoken_kit.h" 34 #include "token_setproc.h" 35 36 #ifdef __cplusplus 37 extern "C" { 38 #endif 39 40 #define DEF_GROUP_ID "DEF_GROUP_ID" 41 #define DEF_SESSION_KEY "DEF_SESSION_KEY" 42 #define DEF_PKG_NAME "com.communication.demo1" 43 #define PKG_NAME_TWO "com.communication.demo2" 44 #define PKG_NAME_THREE "com.communication.demo3" 45 #define PKG_NAME_FOUR "com.communication.demo4" 46 #define SESSIONNAME_MAX "com.communication.demo.max.len.12345678901234567\ 47 0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789\ 48 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678" 49 #define SESSIONNAME_MAX_OUT "com.communication.demo.max.len.12345678901234567\ 50 0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789\ 51 0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" 52 #define PKGNAME_MAX "com.communication.demo.max.len.123456789012345678901234567890123" 53 #define PKGNAME_MAX_OUT "com.communication.demo.max.len.1234567890123456789012345678901234" 54 55 #define SESSION_NAME_DATA "com.communication.demo1.data" 56 #define SESSION_NAME_CTL "com.communication.demo1.control" 57 #define SESSION_NAME_PASS "com.communication.demo1.passive" 58 #define SESSION_NAME_FAIL "com.communication.demo1.fail" 59 #define SESSION_NAME_PERF "com.communication.demo1.perf" 60 #define SESSION_NAME_PROXY "com.communication.demo1.proxy" 61 #define SESSION_NAME_FILE "com.communication.demo1.file" 62 #define SESSION_NAME_STREAM "com.communication.demo1.stream" 63 #define DSOFT_PKG_NAME "com.plrdtest.dsoftbus" 64 #define SESSION_NAME_DSOFT_DATA "com.plrdtest.dsoftbus.data" 65 #define RECV_FILE_PATH "/data/" 66 67 #define CTRL_MSG_CLOSE_WIFI_THREE_SEC "close wifi 3sec" 68 #define CTRL_MSG_CLOSE_WIFI_TEN_MIN "close wifi 10min" 69 #define CTRL_MSG_CLOSE_WIFI_FIVE_MIN "close wifi 5min" 70 #define CTRL_MSG_CLOSE_WIFI_TEN_SEC "close wifi 10sec" 71 #define CTRL_MSG_CHANGE_WIFI_TEN_SEC "change wifi 10sec" 72 #define CTRL_MSG_CHANGE_WIFI_SIXTY_SEC "change wifi 60sec" 73 #define CTRL_MSG_CLOSE_WIFI_LOOP20 "close wifi loop 20" 74 #define CTRL_MSG_CHANGE_WIFI_LOOP20 "change wifi loop 20" 75 #define CTRL_MSG_CLOSE_WIFI_LOOP100 "close wifi loop 100" 76 #define CTRL_MSG_CHANGE_WIFI_LOOP100 "change wifi loop 100" 77 78 #define CTRL_MSG_OPEN_SESSION "open session" 79 #define CTRL_MSG_OPEN_SESSION_MSG "open session msg" 80 #define CTRL_MSG_OPEN_SESSION_NOT_EXIST "open session not exist" 81 #define CTRL_MSG_OPEN_SESSION_FAIL "open session fail" 82 #define CTRL_MSG_CLOSE_SESSION "close session" 83 84 #define CTRL_MSG_CLOSE_BR "close br" 85 #define CTRL_MSG_CLOSE_OPEN_BR "close-open br" 86 #define CTRL_MSG_CLOSE_AIR "close air" 87 #define CTRL_MSG_CLOSE_OPEN_AIR "close-open air" 88 #define CTRL_MSG_SEND_BIG_DATA "send big data" 89 90 #define WAIT_DEF_VALUE (-1) 91 #define WAIT_SUCCESS_VALUE 1 92 #define WAIT_FAIL_VALUE 0 93 #define SESSION_ID_MIN 1 94 #define MAX_SESSION_NUM 16 95 #define BOOL_TRUE 1 96 #define BOOL_FALSE 0 97 #define OPEN_SESSION_TIMEOUT 19 98 #define MULTI_REMOTE_DEV_COUNT 3 99 #define DEF_REMOTE_DEV_COUNT 1 100 #define SIZE_1K 1024 101 102 #define CMD_DOWN_ETH "ifconfig eth0 down" 103 #define CMD_PIDOF_SOFTBUS "pidof softbus_server" 104 #define MAX_SESSION_SERVER_NUM_CLIENT 8 105 #define MAX_SESSION_SERVER_NUM_SERVER 32 106 #define TRANS_BYTES_LENGTH_MAX (4 * 1024 * 1024) 107 #define TRANS_MESSAGE_LENGTH_MAX (4 * 1024) 108 #define TRANS_PROXY_BYTES_LENGTH_MAX (4 * 1024 * 1024) 109 #define TRANS_PROXY_MESSAGE_LENGTH_MAX (4 * 1024) 110 #define DEF_SEND_DATA_SID_COUNT 4 111 #define GROUP_ID_LEN 4 112 113 #define CASE_0 0 114 #define CASE_1 1 115 #define CASE_2 2 116 #define CASE_3 3 117 #define CASE_4 4 118 #define CASE_5 5 119 #define CASE_6 6 120 #define CASE_7 7 121 #define CASE_8 8 122 #define CASE_9 9 123 #define CASE_10 10 124 125 typedef enum { 126 SESSION_4DATA = 1, 127 SESSION_4CTL, 128 SESSION_4PROXY, 129 SESSION_4STREAM, 130 } WaitSessionType; 131 132 typedef enum { 133 DATA_TYPE_MSG = 1, 134 DATA_TYPE_BYTE, 135 } DataType; 136 137 typedef enum { 138 STATE_ONLINE = 1, 139 STATE_OFFLINE, 140 } WaitNodeStateType; 141 142 typedef enum { 143 CONC_CLOSE_SESSION = 1, 144 CONC_LEAVE_NET, 145 CONC_DIS_NET_RECOVERY, 146 CONC_DIS_NET_NOT_RECOVERY, 147 CONC_DOWN_NET, 148 CONC_SEND_DATA_ONLY, 149 CONC_CLOSE_SESSION_REMOTE, 150 } ConcurrentType; 151 152 typedef enum { 153 CTRL_CODE_CLOSE_WIFI_TEN_SEC = 1, 154 CTRL_CODE_CLOSE_WIFI_TEN_MIN, 155 CTRL_CODE_CLOSE_WIFI_FIVE_MIN, 156 CTRL_CODE_CLOSE_WIFI_THREE_SEC, 157 CTRL_CODE_CHANGE_WIFI_TEN_SEC, 158 CTRL_CODE_CHANGE_WIFI_SIXTY_SEC, 159 CTRL_CODE_CHANGE_WIFI_LOOP20, 160 CTRL_CODE_CLOSE_WIFI_LOOP20, 161 CTRL_CODE_CHANGE_WIFI_LOOP100, 162 CTRL_CODE_CLOSE_WIFI_LOOP100, 163 CTRL_CODE_OPEN_SESSION, 164 CTRL_CODE_OPEN_SESSION_MSG, 165 CTRL_CODE_OPEN_SESSION_FAIL, 166 CTRL_CODE_OPEN_SESSION_NOT_EXIST, 167 CTRL_CODE_CLOSE_SESSION, 168 CTRL_CODE_CLOSE_BR, 169 CTRL_CODE_CLOSE_OPEN_BR, 170 CTRL_CODE_CLOSE_AIR, 171 CTRL_CODE_CLOSE_OPEN_AIR, 172 CTRL_CODE_SEND_BIG_DATA, 173 } CtrlCodeType; 174 175 #define LOG(format, ...) \ 176 do { \ 177 time_t timeSec; \ 178 time(&timeSec); \ 179 struct tm tmRst; \ 180 localtime_r(&timeSec, &tmRst); \ 181 char strTime[10]; \ 182 strftime(strTime, sizeof(strTime), "%H:%M:%S", &tmRst); \ 183 fprintf(stdout, "[Test-softbus] %s " format "\n", strTime, ##__VA_ARGS__); \ 184 } while (0) 185 186 int Wait(int timeout); 187 int Wait4Session(int timeout, WaitSessionType type); 188 int WaitNodeCount(int timeout, WaitNodeStateType state, int expectCount); 189 190 void AddPermission(void); 191 void sleepn(int n); 192 void TestSetUp(void); 193 void TestTearDown(void); 194 195 int RegisterDeviceStateDefCallback(void); 196 int UnRegisterDeviceStateDefCallback(void); 197 int CheckRemoteDeviceIsNull(int isSetNetId); 198 199 uint64_t GetCurrentTimeOfMs(void); 200 201 void ResetWaitFlag(void); 202 void ResetWaitFlag4Data(void); 203 void ResetWaitFlag4Ctl(void); 204 void ResetWaitFlag4Proxy(void); 205 void ResetwaitCount4Online(void); 206 void ResetwaitCount4Offline(void); 207 208 int SendData4Data(DataType type, int size); 209 int SendData4Message(DataType type, int size); 210 211 int CreateSsAndOpenSession4Data(void); 212 int CreateSsAndOpenSession4Ctl(void); 213 int CreateSsAndOpenSession4Proxy(void); 214 int OpenSessionBatch4Data(char groupId[][GROUP_ID_LEN], int* sessionId, int count); 215 int OpenSessionBatch4Ctl(char groupId[][GROUP_ID_LEN], int* sessionId, int count); 216 int OpenSession4DataByP2p(void); 217 int OpenSession4ProxyByP2p(void); 218 int CloseSessionBatch4Data(int* sessionId, int count); 219 int CloseSessionBatch4Ctl(int* sessionId, int count); 220 221 char* GetSoftbusPid(void); 222 char* GetNetworkId(void); 223 int GetCurrentSessionId4Data(void); 224 int GetCurrentSessionId4Ctl(void); 225 int GetCurrentSessionId4Proxy(void); 226 void SetCurrentSessionId4Data(int sessionId); 227 void SetCurrentSessionId4Ctl(int sessionId); 228 void SetCurrentSessionId4Proxy(int sessionId); 229 ISessionListener* GetSessionListenser4Data(void); 230 ISessionListener* GetSessionListenser4Ctl(void); 231 ISessionListener* GetSessionListenser4Proxy(void); 232 ISessionListener* GetSessionListenser4Stream(void); 233 234 SessionAttribute* GetSessionAttr4Ctl(void); 235 SessionAttribute* GetSessionAttr4Data(void); 236 SessionAttribute* GetSessionAttr4Proxy(void); 237 SessionAttribute* GetSessionAttr4DataP2p(void); 238 SessionAttribute* GetSessionAttr4ProxyP2p(void); 239 IFileReceiveListener* GetRecvFileListener(void); 240 IFileSendListener* GetSendFileListener(void); 241 242 void ResetClosedSessionCount4Data(void); 243 void ResetClosedSessionCount4Ctrl(void); 244 int GetClosedSessionCount4Data(void); 245 int GetClosedSessionCount4Ctrl(void); 246 247 int CloseSessionAndRemoveSs4Data(void); 248 int CloseSessionAndRemoveSs4Ctl(void); 249 int CloseSessionAndRemoveSs4Proxy(void); 250 251 #ifdef __cplusplus 252 } 253 #endif 254 #endif