3. add xts_dcts.server

Signed-off-by: wuxiaodan <wuxiaodan5@huawei.com>
This commit is contained in:
wuxiaodan 2022-07-04 15:24:39 +08:00
parent 03a2ef9477
commit 6728dc99ed
9 changed files with 911 additions and 35 deletions

View File

@ -65,23 +65,38 @@ void initShm(void)
int readDataFromShm(char* buf) int readDataFromShm(char* buf)
{ {
LOG("readDataFromShm begin...");
if (shared == nullptr) { if (shared == nullptr) {
return -1; return -1;
} }
LOG("11--readDataFromShm buf= %s", buf);
LOG("11--readDataFromShm shared->data= %s", shared->data);
if (shared->written != 0) { if (shared->written != 0) {
strcpy_s(buf, strlen(shared->data) + 1, shared->data); strcpy_s(buf, strlen(shared->data) + 1, shared->data);
LOG("readDataFromShm buf= %s", buf);
LOG("readDataFromShm shared->data= %s", shared->data);
shared->written = 0; shared->written = 0;
LOG("22--readDataFromShm buf= %s", buf);
LOG("22--readDataFromShm shared->data= %s", shared->data);
memset_s(shared->data, MAX_DATA_LENGTH, 0, MAX_DATA_LENGTH); memset_s(shared->data, MAX_DATA_LENGTH, 0, MAX_DATA_LENGTH);
} else { } else {
return -1; return -1;
} }
LOG("33--SUCCESS: readDataFromShm return 0"); LOG("SUCCESS: readDataFromShm return 0");
return 0;
}
int readDataFromShmNoClear(char* buf)
{
LOG("readDataFromShmNoClear begin...");
if (shared == nullptr) {
return -1;
}
if (shared->written != 0) {
strcpy_s(buf, strlen(shared->data) + 1, shared->data);
LOG("readDataFromShmNoClear buf= %s", buf);
LOG("readDataFromShmNoClear shared->data= %s", shared->data);
} else {
return -1;
}
LOG("SUCCESS: readDataFromShmNoClear return 0");
return 0; return 0;
} }
@ -94,48 +109,63 @@ int waitDataWithCode(char* code, char* data)
return RES_FAIL; return RES_FAIL;
} }
while (i < timeout) { while (i < timeout) {
if (readDataFromShmNoClear(str) != 0 || strncmp(code, str, CODE_HEAD) != 0) {
i++;
sleep(1);
LOG("while: waitDataWithCode 9999 str= %s, i=%d", str, i);
continue;
}
if (readDataFromShm(str) == 0 && strncmp(code, str, CODE_HEAD) == 0) { if (readDataFromShm(str) == 0 && strncmp(code, str, CODE_HEAD) == 0) {
LOG("11--readDataFromShm str= %s", str); if (strncpy_s(data, strlen("0") + 1, str + STR_KEY, 1) != EOK) {
LOG("11--readDataFromShm code= %s", code); LOG("ERR:strncpy_s");
LOG("11--readDataFromShm data= %s", data);
errno_t ret = 1;
ret = strncpy_s(data, strlen("0") + 1, str + STR_KEY, 1);
if (ret != EOK) {
LOG("ERR:ret=%d", ret);
return RES_FAIL; return RES_FAIL;
} }
LOG("22--readDataFromShm str= %s", str); LOG("waitDataWithCode 9999 str= %s", str);
LOG("22--readDataFromShm code= %s", code); LOG("waitDataWithCode 9999 data= %s", data);
LOG("22--readDataFromShm data= %s", data); LOG("SUCCESS:waitDataWithCode return 0");
LOG("33--SUCCESS:waitDataWithCode return 0");
return 0; return 0;
} }
i++; i++;
sleep(1); sleep(1);
LOG("while: waitDataWithCode 9999 str= %s, i=%d", str, i);
} }
LOG("ERR :waitDataWithCode ");
return RES_FAIL; return RES_FAIL;
} }
int writeCodeDataToShm(int code, char* buf) int writeCodeDataToShm(int code, char* buf)
{ {
char str[MAX_DATA_LENGTH] = { 0 }; LOG("writeCodeDataToShm, begin");
char* str = (char*)malloc(MAX_DATA_LENGTH);
if (str == nullptr) {
LOG("malloc fail");
return nullptr;
}
(void)memset_s(str, MAX_DATA_LENGTH, 0, MAX_DATA_LENGTH);
char codeStr[5] = { 0 }; char codeStr[5] = { 0 };
char* str2 = Int2String(code, codeStr); char* str2 = Int2String(code, codeStr);
if (str2 == nullptr) { if (str2 == nullptr) {
LOG("ERROR: str2 == nullptr");
return -1; return -1;
} }
if (strcpy_s(str, strlen(codeStr) + 1, codeStr) != EOK) {
if (strcpy_s(str, MAX_DATA_LENGTH, codeStr) != EOK) {
LOG("ERROR: strcpy_s != EOK");
return -1; return -1;
} }
if (strcat_s(str, strlen(":") + 1, ":") != EOK) { if (strcat_s(str, MAX_DATA_LENGTH, ":") != EOK) {
LOG("ERROR: 1. strcat_s!= EOK ");
return -1; return -1;
} }
if (buf == nullptr) { if (buf == nullptr) {
LOG("ERROR:buf == nullptr ");
return -1; return -1;
} }
if (strcat_s(str, strlen(buf) + 1, buf) != EOK) { if (strcat_s(str, MAX_DATA_LENGTH, buf) != EOK) {
LOG("ERROR:2. strcat_s != EOK");
return -1; return -1;
} }
int nres = writeDataToShm(str); int nres = writeDataToShm(str);
@ -158,6 +188,9 @@ int writeDataToShm(char* buf)
memset_s(shared->data, SHARED_DATA_LEN, 0, SHARED_DATA_LEN); memset_s(shared->data, SHARED_DATA_LEN, 0, SHARED_DATA_LEN);
strcpy_s(shared->data, strlen(buf) + 1, buf); strcpy_s(shared->data, strlen(buf) + 1, buf);
shared->written = 1; shared->written = 1;
LOG("writeDataToShm shared->data= %s", shared->data);
LOG("writeDataToShm shared->written= %d", shared->written);
sleep(WAITTIME); sleep(WAITTIME);
return 0; return 0;
} }

View File

@ -56,5 +56,5 @@ int deleteShm(void);
char* Int2String(int num, char* str); char* Int2String(int num, char* str);
void initShm(void); void initShm(void);
int readDataFromShm(char* buf); int readDataFromShm(char* buf);
int readDataFromShmNoClear(char* buf);
#endif #endif

View File

@ -16,6 +16,7 @@ group("distributeddatatest") {
if (is_standard_system) { if (is_standard_system) {
deps = [ deps = [
":disDataProcess", ":disDataProcess",
"../server/disServer:disTest",
"cpptest/distributed_kv_store:DctsdisDataTest", "cpptest/distributed_kv_store:DctsdisDataTest",
"jstest/distributed_kv_store/client/hap:distributekvdisjs_test", "jstest/distributed_kv_store/client/hap:distributekvdisjs_test",
"jstest/distributed_kv_store/server/hap:distributekvdisjs_server", "jstest/distributed_kv_store/server/hap:distributekvdisjs_server",

View File

@ -14,6 +14,7 @@
*/ */
#define LOG_TAG "disDataTest" #define LOG_TAG "disDataTest"
#include <cmath>
#include <condition_variable> #include <condition_variable>
#include <cstdint> #include <cstdint>
#include <cstring> #include <cstring>
@ -328,8 +329,7 @@ void DistributedKvDataManagerTest::SetUp(void)
return; return;
} }
memset_s(str, MAX_DATA_LENGTH, 0, MAX_DATA_LENGTH); memset_s(str, MAX_DATA_LENGTH, 0, MAX_DATA_LENGTH);
strcpy_s(str, strlen("") + 1, ""); DisKvTest::RemoteDeleteKV(str);
DisKvTest::RemoteCreateKV(str);
free(str); free(str);
// S3.本地创建数据库 // S3.本地创建数据库
LOG("%s s3.Local---GetSingleKvStore ", LOGSTR); LOG("%s s3.Local---GetSingleKvStore ", LOGSTR);
@ -984,7 +984,7 @@ HWTEST_F(DistributedKvDataManagerTest, DistribitedKvDataManager_Sync_Pull_0200,
EXPECT_EQ(status, Status::SUCCESS); EXPECT_EQ(status, Status::SUCCESS);
float aaa = TransferByteArrayToType<float>(valueRetInt.Data()); float aaa = TransferByteArrayToType<float>(valueRetInt.Data());
float delta = aaa - 9.99f; float delta = aaa - 9.99f;
EXPECT_LE(std::abs(delta), DEFDELTA) << "ERR:8.本地get数据 与 float:9.99比较"; EXPECT_LE(std::fabs(delta), DEFDELTA) << "ERR:8.本地get数据 与 float:9.99比较";
// 解注册回调 // 解注册回调
} }
@ -1090,7 +1090,7 @@ HWTEST_F(DistributedKvDataManagerTest, DistribitedKvDataManager_Sync_Pull_0300,
double delta = aaa - 999.999; double delta = aaa - 999.999;
std::cout << "aaa = " << aaa << std::endl; std::cout << "aaa = " << aaa << std::endl;
std::cout << "delta = " << delta << std::endl; std::cout << "delta = " << delta << std::endl;
EXPECT_LE(std::abs(delta), DEFDELTA) << "ERR:8.本地get数据 与double:999.999比较"; EXPECT_LE(std::fabs(delta), DEFDELTA) << "ERR:8.本地get数据 与double:999.999比较";
// 解注册回调 // 解注册回调
} }
@ -2259,7 +2259,7 @@ HWTEST_F(DistributedKvDataManagerTest, SubscribeWithQuery_0200, TestSize.Level1)
EXPECT_EQ(status, Status::SUCCESS) << "LOGdisDataTest--ERR:Get(keyInt, valueRetInt)"; EXPECT_EQ(status, Status::SUCCESS) << "LOGdisDataTest--ERR:Get(keyInt, valueRetInt)";
float aaa = TransferByteArrayToType<float>(valueRetInt.Data()); float aaa = TransferByteArrayToType<float>(valueRetInt.Data());
float delta = aaa - 9.99f; float delta = aaa - 9.99f;
EXPECT_LE(std::abs(delta), DEFDELTA) << "ERR:本地get数据与float 9.99比较"; EXPECT_LE(std::fabs(delta), DEFDELTA) << "ERR:本地get数据与float 9.99比较";
// 取消订阅 // 取消订阅
auto unSubscribeStatus = DisKvTest::KvStorePtr->UnsubscribeWithQuery(deviceList, dataQuery); auto unSubscribeStatus = DisKvTest::KvStorePtr->UnsubscribeWithQuery(deviceList, dataQuery);
EXPECT_EQ(unSubscribeStatus, Status::SUCCESS) << "LOGdisDataTest--ERR:SubscribeWithQuery"; EXPECT_EQ(unSubscribeStatus, Status::SUCCESS) << "LOGdisDataTest--ERR:SubscribeWithQuery";
@ -2312,7 +2312,7 @@ HWTEST_F(DistributedKvDataManagerTest, SubscribeWithQuery_0300, TestSize.Level1)
double delta = aaa - 999.999; double delta = aaa - 999.999;
std::cout << "aaa = " << aaa << std::endl; std::cout << "aaa = " << aaa << std::endl;
std::cout << "delta = " << delta << std::endl; std::cout << "delta = " << delta << std::endl;
EXPECT_LE(std::abs(delta), DEFDELTA) << "ERR:本地get数据与double 999.999比较"; EXPECT_LE(std::fabs(delta), DEFDELTA) << "ERR:本地get数据与double 999.999比较";
// 取消订阅 // 取消订阅
auto unSubscribeStatus = DisKvTest::KvStorePtr->UnsubscribeWithQuery(deviceList, dataQuery); auto unSubscribeStatus = DisKvTest::KvStorePtr->UnsubscribeWithQuery(deviceList, dataQuery);
@ -2619,7 +2619,7 @@ HWTEST_F(DistributedKvDataManagerTest, SyncWithCondition_0200, TestSize.Level1)
EXPECT_EQ(status, Status::SUCCESS) << "LOGdisDataTest--ERR:Get(keyInt, valueRetInt)"; EXPECT_EQ(status, Status::SUCCESS) << "LOGdisDataTest--ERR:Get(keyInt, valueRetInt)";
float aaa = TransferByteArrayToType<float>(valueRetInt.Data()); float aaa = TransferByteArrayToType<float>(valueRetInt.Data());
float delta = aaa - 9.99f; float delta = aaa - 9.99f;
EXPECT_LE(std::abs(delta), DEFDELTA) << "ERR:本地get数据与float 9.99比较"; EXPECT_LE(std::fabs(delta), DEFDELTA) << "ERR:本地get数据与float 9.99比较";
} }
/** /**
@ -2669,7 +2669,7 @@ HWTEST_F(DistributedKvDataManagerTest, SyncWithCondition_0300, TestSize.Level1)
double delta = aaa - 999.999; double delta = aaa - 999.999;
std::cout << "aaa = " << aaa << std::endl; std::cout << "aaa = " << aaa << std::endl;
std::cout << "delta = " << delta << std::endl; std::cout << "delta = " << delta << std::endl;
EXPECT_LE(std::abs(delta), DEFDELTA) << "ERR:本地get数据与double 999.999比较"; EXPECT_LE(std::fabs(delta), DEFDELTA) << "ERR:本地get数据与double 999.999比较";
} }
/** /**

View File

@ -13,9 +13,9 @@
* limitations under the License. * limitations under the License.
*/ */
#include <cmath>
#include <cstdint> #include <cstdint>
#include <cstdio> #include <cstdio>
#include <cstdlib>
#include <cstring> #include <cstring>
#include <iostream> #include <iostream>
#include <securec.h> #include <securec.h>
@ -25,7 +25,6 @@
#include "distributed_kv_data_manager.h" #include "distributed_kv_data_manager.h"
#include "types.h" #include "types.h"
#include "process_msg.h" #include "process_msg.h"
using namespace OHOS::DistributedKv; using namespace OHOS::DistributedKv;
using namespace std; using namespace std;
@ -267,7 +266,7 @@ int processGetData(char* putData)
LOG("LOGdisDataTest--faaa= %f", faaa); LOG("LOGdisDataTest--faaa= %f", faaa);
LOG("LOGdisDataTest--f2 = %f", f2); LOG("LOGdisDataTest--f2 = %f", f2);
LOG("LOGdisDataTest--fdelta = %f", fdelta); LOG("LOGdisDataTest--fdelta = %f", fdelta);
if (std::abs(fdelta) <= DEFDELTA) { if (std::fabs(fdelta) <= DEFDELTA) {
return RESULT_OK; return RESULT_OK;
} }
} else if (strcmp(result[1], "math_score_double") == 0) { } else if (strcmp(result[1], "math_score_double") == 0) {
@ -278,7 +277,7 @@ int processGetData(char* putData)
LOG("LOGdisDataTest--daaa= %f", daaa); LOG("LOGdisDataTest--daaa= %f", daaa);
LOG("LOGdisDataTest--d2 = %f", d2); LOG("LOGdisDataTest--d2 = %f", d2);
LOG("LOGdisDataTest--delta = %f", delta); LOG("LOGdisDataTest--delta = %f", delta);
if (std::abs(delta) <= DEFDELTA) { if (std::fabs(delta) <= DEFDELTA) {
return RESULT_OK; return RESULT_OK;
} }
} else if (strcmp(result[1], "math_score_int64_t") == 0) { } else if (strcmp(result[1], "math_score_int64_t") == 0) {

20
server/BUILD.gn Normal file
View File

@ -0,0 +1,20 @@
# Copyright (c) 2021 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import("//build/ohos_var.gni")
group("disServer") {
testonly = true
if (is_standard_system) {
deps = [ "disServer:disTest" ]
}
}

48
server/disServer/BUILD.gn Normal file
View File

@ -0,0 +1,48 @@
# Copyright (C) 2022 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import("//build/test.gni")
module_output_path = "dcts/server"
###############################################################################################
config("module_private_config") {
visibility = [ ":*" ]
include_dirs = [
"//third_party/cJSON",
"//utils/native/base/include",
"//foundation/communication/dsoftbus/adapter/common/include",
"//foundation/communication/dsoftbus/core/common/include",
"//foundation/communication/dsoftbus/interfaces/kits/common",
"//foundation/communication/dsoftbus/interfaces/kits/bus_center",
"//foundation/communication/dsoftbus/interfaces/kits/discovery",
"//foundation/communication/dsoftbus/interfaces/kits/transport",
"//test/xts/dcts/common",
]
}
ohos_unittest("disTest") {
module_out_path = module_output_path
configs = [ ":module_private_config" ]
sources = [
"../../common/shm_utils.cpp",
"net_trans_common.cpp",
]
deps = [
"//foundation/communication/dsoftbus/sdk:softbus_client",
"//utils/native/base:utils",
]
}
###########################################################################################

View File

@ -0,0 +1,642 @@
/*
* Copyright (C) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <arpa/inet.h>
#include <cstdio>
#include <dlfcn.h>
#include <gtest/gtest.h>
#include <netdb.h>
#include <pthread.h>
#include <sys/socket.h>
#include <vector>
#include "softbus_permission.h"
#include "unistd.h"
#include "net_trans_common.h"
using namespace std;
using namespace NetTrans;
using namespace testing::ext;
const int MAX_DATA_LENGTH = 1024;
const char DEF_GROUP_ID[50] = "DEF_GROUP_ID";
const char DEF_PKG_NAME[50] = "com.communication.demo";
const char SESSION_NAME_DATA[50] = "com.communication.demo.data";
const char SHM_DATA_RES[50] = "9999";
const int WAIT_DEF_VALUE = -1;
const int WAIT_SUCCESS_VALUE = 1;
const int WAIT_FAIL_VALUE = 0;
const int SESSION_ID_MIN = 1;
const int MAX_SESSION_NUM = 16;
const int INT_TRUE = 1;
const int FALSE = 0;
const int RET_SUCCESS = 0;
const int STR_PREFIX_FOUR = 4;
const int SLEEP_SECOND_ONE = 1;
const int SLEEP_SECOND_TEO = 2;
const int SLEEP_SECOND_TEN = 10;
const int CODE_LEN_FIVE = 5;
const int CODE_PREFIX_FOUR = 4;
static int32_t g_currentSessionId4Data = -1;
static int32_t g_waitFlag = WAIT_DEF_VALUE;
static int32_t g_waitFlag4Ctl = WAIT_DEF_VALUE;
static int32_t g_waitFlag4Data = WAIT_DEF_VALUE;
static int32_t g_nodeOnlineCount = 0;
static int32_t g_nodeOfflineCount = 0;
static int32_t g_sessionOpenCount4Data = 0;
static SessionAttribute* g_sessionAttr4Data = nullptr;
static ISessionListener* g_sessionlistener4Data = nullptr;
static char g_networkId[NETWORK_ID_BUF_LEN] = { 0 };
static INodeStateCb g_defNodeStateCallback;
static ConnectionAddr g_ethAddr = {
.type = CONNECTION_ADDR_WLAN,
};
static vector<string> g_networkIds;
static int g_subscribeId = 0;
static bool g_sessionClosed = false;
namespace NetTrans {
class NetTransCommon : public testing::Test {
public:
static void SetUpTestCase(void);
static void TearDownTestCase(void);
void SetUp();
void TearDown();
NetTransCommon();
};
void NetTransCommon::SetUpTestCase(void) {}
void NetTransCommon::TearDownTestCase(void) {}
void NetTransCommon::SetUp(void) {}
void NetTransCommon::TearDown(void) {}
NetTransCommon::NetTransCommon(void) {}
int Wait4Session(int timeout, WaitSessionType type)
{
int hitFlag = -1;
while (timeout > 0) {
sleep(SLEEP_SECOND_ONE);
switch (type) {
case WaitSessionType::SESSION_4CTL:
if (g_waitFlag4Ctl != WAIT_DEF_VALUE) {
LOG("Wait4Session success,flag:%d", g_waitFlag4Ctl);
hitFlag = 1;
}
break;
case WaitSessionType::SESSION_4DATA:
if (g_waitFlag4Data != WAIT_DEF_VALUE) {
LOG("Wait4Session success,flag:%d", g_waitFlag4Ctl);
hitFlag = 1;
}
break;
default:
LOG("Wait4Session type error");
hitFlag = 1;
break;
}
if (hitFlag != -1) {
break;
}
timeout--;
}
switch (type) {
case WaitSessionType::SESSION_4CTL:
if (g_waitFlag4Ctl != WAIT_SUCCESS_VALUE) {
LOG("Wait4Session FAIL,flag:%d", g_waitFlag4Ctl);
return SOFTBUS_ERR;
}
break;
case WaitSessionType::SESSION_4DATA:
if (g_waitFlag4Data != WAIT_SUCCESS_VALUE) {
LOG("Wait4Session FAIL,flag:%d", g_waitFlag4Ctl);
return SOFTBUS_ERR;
}
break;
default:
LOG("Wait4Session type error");
return SOFTBUS_ERR;
}
return SOFTBUS_OK;
}
void OnDefNodeOnline(NodeBasicInfo* info)
{
LOG("OnDefNodeOnline");
if (info == nullptr) {
LOG("OnDefNodeOnline info is nullptr");
return;
}
if (strncpy_s(g_networkId, NETWORK_ID_BUF_LEN, info->networkId, NETWORK_ID_BUF_LEN) != RET_SUCCESS) {
return;
}
LOG("Online id:%s,name:%s,type id:%u", info->networkId, info->deviceName, info->deviceTypeId);
if (g_networkIds.empty()) {
g_nodeOnlineCount++;
g_networkIds.push_back(info->networkId);
LOG("test_wifi_onoff: g_networkIds.empty() g_nodeOnlineCount = %d", g_nodeOnlineCount);
LOG("test_wifi_onoff: g_networkIds.empty() g_sessionClosed= %d", g_sessionClosed);
for (const auto& networkId : g_networkIds) {
LOG("networkId = %s", networkId.c_str());
}
return;
}
std::vector<string>::const_iterator it;
for (it = g_networkIds.begin(); it != g_networkIds.end(); it++) {
LOG("test_wifi_onoff: *it = %s", (*it).c_str());
LOG("test_wifi_onoff: info->networkId = %s", info->networkId);
if (strcmp(info->networkId, (*it).c_str()) == 0) {
LOG("test_wifi_onoff: strcmp(info->networkId, *it) == 0");
return;
} else {
g_nodeOnlineCount++;
g_networkIds.push_back(info->networkId);
LOG("test_wifi_onoff: g_nodeOnlineCount = %d", g_nodeOnlineCount);
LOG("test_wifi_onoff: OnDefNodeOnline g_sessionClosed= %d", g_sessionClosed);
return;
}
}
}
void OnDefNodeOffline(NodeBasicInfo* info)
{
LOG("OnDefNodeOffline");
if (info == nullptr) {
LOG("OnDefNodeOffline info is nullptr");
return;
}
LOG("Offline id:%s,name:%s,type id:%u", info->networkId, info->deviceName, info->deviceTypeId);
if (g_networkIds.empty()) {
LOG("test_wifi_onoff: g_networkIds.empty() g_nodeOnlineCount = %d", g_nodeOnlineCount);
LOG("test_wifi_onoff: g_networkIds.empty() g_nodeOfflineCount = %d", g_nodeOfflineCount);
LOG("test_wifi_onoff: g_networkIds.empty() g_sessionClosed= %d", g_sessionClosed);
return;
}
std::vector<string>::const_iterator it;
for (it = g_networkIds.begin(); it != g_networkIds.end(); it++) {
LOG("*it = %s", (*it).c_str());
LOG("info->networkId = %s", info->networkId);
if (strcmp(info->networkId, (*it).c_str()) == 0) {
g_nodeOfflineCount++;
g_nodeOnlineCount--;
it = g_networkIds.erase(it);
LOG("test_wifi_onoff: g_nodeOnlineCount = %d", g_nodeOnlineCount);
LOG("test_wifi_onoff: g_nodeOfflineCount = %d", g_nodeOfflineCount);
LOG("test_wifi_onoff: g_sessionClosed= %d", g_sessionClosed);
return;
} else {
LOG("test_wifi_onoff: strcmp(info->networkId, *it) !!= 0");
return;
}
}
}
void OnDefNodeBasicInfoChanged(NodeBasicInfoType type, NodeBasicInfo* info)
{
if (info == nullptr) {
LOG("OnDefNodeBasicInfoChanged info is nullptr");
return;
}
LOG("InfoChanged id:%s,name:%s", info->networkId, info->deviceName);
}
int DataSessionOpened(int sessionId, int result)
{
LOG("DataSessionOpened sessionId=%d,result=%d", sessionId, result);
g_sessionOpenCount4Data++;
if (result == SOFTBUS_OK) {
g_sessionClosed = false;
SetCurrentSessionId4Data(sessionId);
if (sessionId == g_currentSessionId4Data) {
LOG("openSession check success sessionId=%d", sessionId);
g_waitFlag4Data = WAIT_SUCCESS_VALUE;
} else {
LOG("open session callback %d not match open sid %d", sessionId, g_currentSessionId4Data);
g_waitFlag4Data = WAIT_FAIL_VALUE;
}
} else {
g_waitFlag4Data = WAIT_FAIL_VALUE;
}
return SOFTBUS_OK;
}
void ResetWaitFlag4Data(void)
{
g_waitFlag = WAIT_DEF_VALUE;
}
void ResetWaitCount4Offline(void)
{
g_nodeOfflineCount = 0;
}
void ResetWaitCount4Online(void)
{
g_nodeOnlineCount = 0;
}
void DataSessionClosed(int sessionId)
{
g_sessionClosed = true;
LOG("close session %d", sessionId);
LOG("DataSessionClosed g_sessionClosed= %d", g_sessionClosed);
}
void DataBytesReceived(int sessionId, const void* data, unsigned int dataLen)
{
LOG("byteRec start");
}
void DataMessageReceived(int sessionId, const void* data, unsigned int dataLen)
{
LOG("MsgRec start sessionId=%d, dataLen = %d, data=%s", sessionId, dataLen, (char*)data);
unsigned int maxLen = MAX_DATA_LENGTH;
if (dataLen <= maxLen) {
int* code = (int*)malloc(sizeof(int));
char* buf = (char*)malloc(MAX_DATA_LENGTH);
(void)memset_s(buf, MAX_DATA_LENGTH, 0, MAX_DATA_LENGTH);
if (strncpy_s(buf, MAX_DATA_LENGTH, (char*)data, dataLen) != RET_SUCCESS) {
return;
}
if (*code != -1) {
pthread_t thread;
int ret = pthread_create(&thread, nullptr, DataOperateTask, buf);
pthread_join(thread, nullptr);
LOG("create thread ret:%d", ret);
} else {
free(code);
}
}
}
int RegisterDeviceStateDefCallback(void)
{
return RegNodeDeviceStateCb(DEF_PKG_NAME, &g_defNodeStateCallback);
}
int UnRegisterDeviceStateDefCallback(void)
{
return UnregNodeDeviceStateCb(&g_defNodeStateCallback);
}
int CreateSsAndOpenSession4Data()
{
int ret = CreateSessionServer(DEF_PKG_NAME, SESSION_NAME_DATA, g_sessionlistener4Data);
if (ret != SOFTBUS_OK) {
LOG("call createSessionServer fail, ret:%d", ret);
return ret;
}
pthread_t sendThread;
pthread_create(&sendThread, nullptr, SendMsgTask, nullptr);
pthread_join(sendThread, nullptr);
return ret;
}
int OpenSession4Data(void)
{
int sessionId;
sessionId = OpenSession(SESSION_NAME_DATA, SESSION_NAME_DATA, g_networkId, DEF_GROUP_ID, g_sessionAttr4Data);
if (sessionId < SESSION_ID_MIN) {
LOG("call open session fail ssid:%d", sessionId);
return SOFTBUS_ERR;
}
LOG("call open session SUCCESS ssid:%d", sessionId);
sleep(SLEEP_SECOND_TEN);
return 0;
}
int SendDataMsgToRemote(CtrlCodeType code, char* data)
{
int ret = -1;
int size = MAX_DATA_LENGTH;
char* msg = (char*)malloc(size);
if (msg == nullptr) {
return ret;
}
(void)memset_s(msg, size, 0, size);
if (strcpy_s(msg, size, data) != RET_SUCCESS) {
return ret;
}
ret = SendMessage(g_currentSessionId4Data, msg, strlen(msg));
LOG("send msg ret:%d", ret);
free(data);
return ret;
}
int CloseSessionAndRemoveSs4Data(void)
{
int ret;
int timeout = 10;
ResetWaitFlag4Data();
CloseSession(g_currentSessionId4Data);
ret = Wait4Session(timeout, WaitSessionType::SESSION_4DATA);
if (ret == SOFTBUS_OK) {
LOG("close session success");
}
int retss = RemoveSessionServer(DEF_PKG_NAME, SESSION_NAME_DATA);
if (retss != SOFTBUS_OK) {
LOG("remove session ret:%d", retss);
}
if (ret != SOFTBUS_OK || retss == SOFTBUS_OK) {
return SOFTBUS_ERR;
} else {
return SOFTBUS_OK;
}
}
int IncrementSubId(void)
{
g_subscribeId++;
return g_subscribeId;
}
void OnDataMessageReceived(int sessionId, const char* data, unsigned int dataLen)
{
LOG("msg received %s", data);
if (sessionId < 0 || sessionId > MAX_SESSION_NUM) {
LOG("message received invalid session %d", sessionId);
return;
}
LOG("msg received sid:%d, data-len:%d", sessionId, dataLen);
unsigned int maxLen = MAX_DATA_LENGTH;
if (dataLen <= maxLen) {
int* code = (int*)malloc(sizeof(int));
char* buf = (char*)malloc(MAX_DATA_LENGTH);
if (strcpy_s(buf, MAX_DATA_LENGTH, data) != RET_SUCCESS) {
return;
}
if (*code != -1) {
pthread_t thread;
int ret = pthread_create(&thread, nullptr, DataOperateTask, buf);
pthread_join(thread, nullptr);
LOG("create thread ret:%d", ret);
} else {
free(code);
}
}
}
void* SendMsgTask(void* param)
{
LOG("SendMsgTask send...%s", (char*)param);
int sessionId;
int timeout = 10;
int ret = 0;
ResetWaitFlag4Data();
sessionId = OpenSession(SESSION_NAME_DATA, SESSION_NAME_DATA, g_networkId, DEF_GROUP_ID, g_sessionAttr4Data);
if (sessionId < SESSION_ID_MIN) {
LOG("call open session faild ret:%d", sessionId);
}
SetCurrentSessionId4Data(sessionId);
LOG("call open session success sid:%d", sessionId);
ret = Wait4Session(timeout, WaitSessionType::SESSION_4DATA);
if (ret != SOFTBUS_OK) {
LOG("open session fail");
}
if (createShm(SHM_SEND_KEY) == -1) {
LOG("create shm faild");
return nullptr;
}
initShm();
char str[MAX_DATA_LENGTH] = { 0 };
LOG("test_wifi_onoff: while readDataFromShmNoClear ");
LOG("test_wifi_onoff: while g_sessionClosed=%d ", g_sessionClosed);
while (INT_TRUE) {
if (g_sessionClosed) {
break;
}
if (readDataFromShmNoClear(str) != 0) {
sleep(SLEEP_SECOND_TEO);
continue;
}
if (strncmp(SHM_DATA_RES, str, STR_PREFIX_FOUR) == 0) {
if (readDataFromShm(str) == 0) {
LOG("read result");
}
} else {
if (readDataFromShm(str) == 0) {
LOG("SendData send...%s", str);
SendMessage(g_currentSessionId4Data, str, strlen(str));
(void)memset_s(str, strlen(str) + 1, 0, strlen(str) + 1);
}
}
sleep(SLEEP_SECOND_TEO);
}
LOG("sendMsgTask end");
return nullptr;
}
void* DataOperateTask(void* param)
{
LOG("operate start...");
int code = -1;
char* codeType = (char*)malloc(CODE_LEN_FIVE);
if (codeType == nullptr) {
LOG("malloc fail");
return nullptr;
}
(void)memset_s(codeType, CODE_LEN_FIVE, 0, CODE_LEN_FIVE);
if (strncpy_s(codeType, CODE_LEN_FIVE, (char*)param, CODE_PREFIX_FOUR) != RET_SUCCESS) {
return nullptr;
}
if (sscanf_s(codeType, "%d", &code) <= RET_SUCCESS) {
return nullptr;
}
LOG("code :%d", code);
void* handle = nullptr;
int (*ProcessData)(int, char*);
char* error;
int ret = 0;
if (code == int(CtrlCodeType::CTRL_CODE_RESULT_TYPE)) {
writeDataToShm((char*)param);
free((char*)param);
return nullptr;
} else if (code > int(CtrlCodeType::CTRL_CODE_DATAMGR_TYPE) && code < int(CtrlCodeType::CTRL_CODE_DM_TYPE)) {
handle = dlopen("/system/lib64/libdisDataProcess.z.so", RTLD_LAZY);
if (!handle) {
LOG("dlopen failed %s", dlerror());
}
ProcessData = (int (*)(int, char*))dlsym(handle, "_Z14ProcessDataMgriPc");
if ((error = dlerror()) != nullptr) {
LOG("dlsym failed %s", dlerror());
}
ret = (*ProcessData)(code, (char*)param);
LOG("code:%d", ret);
}
char* str = (char*)malloc(MAX_DATA_LENGTH);
if (str == nullptr) {
return nullptr;
}
(void)memset_s(str, MAX_DATA_LENGTH, 0, MAX_DATA_LENGTH);
int resSprint = sprintf_s(str, MAX_DATA_LENGTH, "%d:%d", CtrlCodeType::CTRL_CODE_RESULT_TYPE, ret);
if (resSprint < FALSE) {
return nullptr;
}
SendDataMsgToRemote(CtrlCodeType::CTRL_CODE_RESULT_TYPE, str);
if (handle) {
dlclose(handle);
}
free((char*)param);
LOG("operate end");
return nullptr;
}
int CheckRemoteDeviceIsNull(int isSetNetId)
{
int nodeNum = 0;
NodeBasicInfo* nodeInfo = nullptr;
int ret = GetAllNodeDeviceInfo(DEF_PKG_NAME, &nodeInfo, &nodeNum);
LOG("get node number is %d, ret %d", nodeNum, ret);
if (nodeInfo != nullptr && nodeNum > 0) {
LOG("get neiId is %s", nodeInfo->networkId);
if (isSetNetId == INT_TRUE) {
if (strncpy_s(g_networkId, NETWORK_ID_BUF_LEN, nodeInfo->networkId, NETWORK_ID_BUF_LEN) != RET_SUCCESS) {
return SOFTBUS_ERR;
}
}
FreeNodeInfo(nodeInfo);
return SOFTBUS_OK;
} else {
LOG("nodeInfo is nullptr");
return SOFTBUS_ERR;
}
}
ISessionListener* GetSessionListenser4Data(void)
{
return g_sessionlistener4Data;
}
void SetCurrentSessionId4Data(int sessionId)
{
g_currentSessionId4Data = sessionId;
}
int GetCurrentSessionId4Data(void)
{
return g_currentSessionId4Data;
}
ConnectionAddr* GetConnectAddr(void)
{
return &g_ethAddr;
}
void init(void)
{
g_defNodeStateCallback.events = EVENT_NODE_STATE_MASK;
g_defNodeStateCallback.onNodeOnline = OnDefNodeOnline;
g_defNodeStateCallback.onNodeOffline = OnDefNodeOffline;
g_defNodeStateCallback.onNodeBasicInfoChanged = OnDefNodeBasicInfoChanged;
if (g_sessionlistener4Data == nullptr) {
g_sessionlistener4Data = (ISessionListener*)calloc(1, sizeof(ISessionListener));
g_sessionlistener4Data->OnSessionOpened = DataSessionOpened;
g_sessionlistener4Data->OnSessionClosed = DataSessionClosed;
g_sessionlistener4Data->OnMessageReceived = DataMessageReceived;
g_sessionlistener4Data->OnBytesReceived = DataBytesReceived;
}
if (g_sessionAttr4Data == nullptr) {
g_sessionAttr4Data = (SessionAttribute*)calloc(1, sizeof(SessionAttribute));
g_sessionAttr4Data->dataType = TYPE_BYTES;
}
// SoftBusPermission::AddPermission(DEF_PKG_NAME);
int ret = RegisterDeviceStateDefCallback();
if (ret != SOFTBUS_OK) {
LOG("RegisterDeviceStateDefCallback FAILED ret=%d", ret);
}
}
void destroy(void)
{
if (g_sessionlistener4Data != nullptr) {
free(g_sessionlistener4Data);
g_sessionlistener4Data = nullptr;
}
}
/**
* @tc.number: DisTest_0001
* @tc.name: net trans common
* @tc.desc: net trans common
* @tc.type: FUNC
*/
HWTEST_F(NetTransCommon, DisTest_0001, TestSize.Level1)
{
LOG("enter main");
init();
while (INT_TRUE) {
int ret = CheckRemoteDeviceIsNull(INT_TRUE);
if (ret == SOFTBUS_OK) {
break;
} else {
sleep(SLEEP_SECOND_TEO);
}
}
int ret = CreateSsAndOpenSession4Data();
if (ret != SOFTBUS_OK) {
LOG("CreateSsAndOpenSession4Ctl failed ret=%d", ret);
}
while (INT_TRUE) {
LOG("enter while success");
LOG("while g_nodeOnlineCount = %d", g_nodeOnlineCount);
LOG("while g_sessionClosed= %d", g_sessionClosed);
if (g_sessionClosed && (g_nodeOnlineCount > 0)) {
LOG("test_wifi_onoff:while (INT_TRUE)");
ret = CreateSsAndOpenSession4Data();
if (ret != SOFTBUS_OK) {
LOG("CreateSsAndOpenSession4Ctl failed ret=%d", ret);
}
}
sleep(SLEEP_SECOND_TEN);
}
}
}; // namespace NetTrans

View File

@ -0,0 +1,133 @@
/*
* Copyright (C) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef NET_TRANS_COMMON_H
#define NET_TRANS_COMMON_H
#include <cerrno>
#include <cstdarg>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <pthread.h>
#include <securec.h>
#include <sys/time.h>
#include <unistd.h>
#include "common_list.h"
#include "discovery_service.h"
#include "session.h"
#include "shm_utils.h"
#include "softbus_bus_center.h"
#include "softbus_def.h"
#include "softbus_errcode.h"
#include "softbus_utils.h"
#ifdef __cplusplus
extern "C" {
#endif
enum class WaitSessionType : int {
SESSION_4DATA = 1,
SESSION_4CTL,
};
enum class DataType : int {
DATA_TYPE_MSG = 1,
DATA_TYPE_BYTE,
};
enum class WaitNodeStateType : int {
STATE_ONLINE = 1,
STATE_OFFLINE,
};
enum class ConcurrentType {
CONC_CLOSE_SESSION = 1,
};
enum class CtrlCodeType : int {
CTRL_CODE_SOFTBUS_TYPE = 1000,
CTRL_CODE_DATAMGR_TYPE = 2000,
CTRL_CODE_DM_TYPE = 3000,
CTRL_CODE_FILEMGR_TYPE = 4000,
CTRL_CODE_DP_TYPE = 5000,
CTRL_CODE_SEC_TYPE = 6000,
CTRL_CODE_MEDIA_TYPE = 7000,
CTRL_CODE_RESULT_TYPE = 9999,
};
namespace NetTrans {
int Wait4Session(int timeout, WaitSessionType type);
int CheckRemoteDeviceIsNull(int isSetNetId);
void OnDefNodeOnline(NodeBasicInfo* info);
void OnDefNodeOffline(NodeBasicInfo* info);
void OnDefNodeBasicInfoChanged(NodeBasicInfoType type, NodeBasicInfo* info);
int DataSessionOpened(int sessionId, int result);
void ResetWaitFlag4Data(void);
void ResetWaitCount4Offline(void);
void ResetWaitCount4Online(void);
int GetCurrentSessionId4Data(void);
void DataSessionClosed(int sessionId);
void DataBytesReceived(int sessionId, const void* data, unsigned int dataLen);
void DataMessageReceived(int sessionId, const void* data, unsigned int dataLen);
int RegisterDeviceStateDefCallback(void);
int UnRegisterDeviceStateDefCallback(void);
int CreateSsAndOpenSession4Data();
int OpenSession4Data(void);
int SendDataMsgToRemote(CtrlCodeType code, char* data);
int CloseSessionAndRemoveSs4Data(void);
int IncrementSubId(void);
void OnDataMessageReceived(int sessionId, const char* data, unsigned int dataLen);
void* SendMsgTask(void* param);
void* DataOperateTask(void* param);
ISessionListener* GetSessionListenser4Data(void);
void SetCurrentSessionId4Data(int sessionId);
ConnectionAddr* GetConnectAddr(void);
void init(void);
void destroy(void);
}; // namespace NetTrans
#ifdef __cplusplus
}
#endif
#endif