From 50c32e3408e5273fe4a72a3534f52be2db191807 Mon Sep 17 00:00:00 2001 From: zhengliming Date: Thu, 30 Jun 2022 11:45:33 +0800 Subject: [PATCH 1/5] Add testcase for dsoftbus. Signed-off-by: zhengliming --- dsoftbus/test/softbus_main.cpp | 364 +++++++++++++++++++++++++++++++++ 1 file changed, 364 insertions(+) create mode 100644 dsoftbus/test/softbus_main.cpp diff --git a/dsoftbus/test/softbus_main.cpp b/dsoftbus/test/softbus_main.cpp new file mode 100644 index 00000000..45799fe8 --- /dev/null +++ b/dsoftbus/test/softbus_main.cpp @@ -0,0 +1,364 @@ +#include +#include +#include +#include +#include +#include + +#include "dsoftbus/discovery_service.h" +#include "dsoftbus/softbus_bus_center.h" +#include "dsoftbus/session.h" + +std::vector< std::string > devList; +char receivedData[256]; + +bool hasPublish = false; +bool hasDiscovery = false; +const size_t LINE_WIDTH = 80; + +// #define ImReceive + +#define clientName "Client" + +void PrintMargin() +{ + for (size_t i = 0; i < LINE_WIDTH; ++i) { + printw("#"); + } + printw("\n"); +} + +void PrintTitle(const char *msg) +{ + size_t len = strlen(msg); + if (len + 2 <= LINE_WIDTH) { + size_t emptyLen = LINE_WIDTH - len - 2; + size_t preEmptyLen = emptyLen / 2; + size_t postEmptyLen = emptyLen - preEmptyLen; + printw("#"); + while (preEmptyLen--) { + printw(" "); + } + printw("%s", msg); + while (postEmptyLen--) { + printw(" "); + } + printw("#\n"); + } else { + printw("#%s#\n", msg); + } +} + +void PrintMessage(const char *msg) +{ + size_t len = strlen(msg); + if (len + 6 <= LINE_WIDTH) { + size_t emptyLen = LINE_WIDTH - len - 6; + printw("# "); + printw("%s", msg); + while (emptyLen--) { + printw(" "); + } + printw(" #\n"); + } else { + printw("#%s#\n", msg); + } +} + +void DisplayTitle() +{ + erase(); + PrintMargin(); + PrintTitle("openEuler"); + PrintTitle("SoftBus"); + PrintTitle(""); + PrintTitle(""); +} + +void DisplayMain() +{ + DisplayTitle(); + PrintTitle(""); + PrintTitle(""); + PrintTitle(""); + PrintMessage("Press key:"); + PrintMessage("[D]DiscoveryService [T]DataTransmission"); + PrintTitle(""); + PrintTitle(""); + PrintMargin(); +} + +void DisplayDevList() +{ + char msg[256]; + DisplayTitle(); + PrintTitle(""); + PrintTitle(""); + PrintMessage("Press key:"); + PrintMessage("[S]StopDiscovery"); + PrintMessage("Devices list:"); + for (size_t i = 0; i < devList.size(); ++i) { + sprintf(msg, " %lu. %s", i, devList[i].c_str()); + PrintMessage(msg); + } + PrintTitle(""); + PrintMargin(); +} + +void DisplayReceivedData() +{ + DisplayTitle(); + PrintTitle(""); + PrintTitle(""); + PrintTitle(""); + PrintTitle(""); +// PrintMessage("Press key:"); +// PrintMessage("[Esc]Back"); + PrintMessage("Received data:"); + PrintMessage(receivedData); + PrintTitle(""); + PrintTitle(""); + PrintMargin(); +} + +void PublishSuccess(int publishId) +{ + // printf("CB: publish %d done", publishId); +} + +void PublishFailed(int publishId, PublishFailReason reason) +{ + // printf("CB: publish %d failed, reason=%d\n", publishId, (int)reason); +} + +void DeviceFound(const DeviceInfo *device) +{ + devList.push_back(device->devName); + //DisplayDevList(); +} + +void DiscoverySuccess(int subscribeId) +{ + // printf("CB: discover subscribeId=%d\n", subscribeId); +} + +void DiscoveryFailed(int subscribeId, DiscoveryFailReason reason) +{ + // printf("CB: discover subscribeId=%d failed, reason=%d\n", subscribeId, (int)reason); +} + +int SessionOpened(int sessionId, int result) +{ + // printf("CB: session %d open ret=%d\n", sessionId, result); + return 0; +} + +void SessionClosed(int sessionId) +{ + // printf("CB: session %d closed\n", sessionId); +} + +void ByteRecived(int sessionId, const void *data, unsigned int dataLen) +{ + memset(receivedData, 0, sizeof(receivedData)); + memcpy(receivedData, data, dataLen); + DisplayReceivedData(); + // printf("CB: session %d received %u bytes data=%s\n", sessionId, dataLen, (const char *)data); +} + +void MessageReceived(int sessionId, const void *data, unsigned int dataLen) +{ + // printf("CB: session %d received %u bytes message=%s\n", sessionId, dataLen, (const char *)data); +} + +unsigned char cData[] = "My Client Test"; + +int PublishServiceInterface() +{ + PublishInfo info = { + .publishId = 123, + .mode = DISCOVER_MODE_PASSIVE, + .medium = COAP, + .freq = LOW, + .capability = "hicall", + .capabilityData = cData, + .dataLen = sizeof(cData), + }; + IPublishCallback cb = { + .OnPublishSuccess = PublishSuccess, + .OnPublishFail = PublishFailed, + }; + return PublishService(clientName, &info, &cb); +} + +int DiscoveryInterface() +{ + SubscribeInfo info = { + .subscribeId = 123, + .mode = DISCOVER_MODE_ACTIVE, + .medium = COAP, + .freq = LOW, + .isSameAccount = false, + .isWakeRemote = false, + .capability = "hicall", + .capabilityData = cData, + .dataLen = sizeof(cData), + }; + IDiscoveryCallback cb = { + .OnDeviceFound = DeviceFound, + .OnDiscoverFailed = DiscoveryFailed, + .OnDiscoverySuccess = DiscoverySuccess, + }; + return StartDiscovery(clientName, &info, &cb); +} + +int CreateSessionServerInterface(const char *SessionName) +{ + ISessionListener cb = { + .OnSessionOpened = SessionOpened, + .OnSessionClosed = SessionClosed, + .OnBytesReceived = ByteRecived, + .OnMessageReceived = MessageReceived, + }; + return CreateSessionServer(clientName, SessionName, &cb); +} + +int OpenSessionInterface(const char *SessionName, const char *peerName, const char *peerId) +{ + SessionAttribute attr = { + .dataType = TYPE_BYTES, + .lintTypeNum = 1, + .attr = {RAW_STREAM}, + }; + int lt = LINK_TYPE_WIFI_P2P; + attr.lintType = < + return OpenSession(SessionName, peerName, peerId, "MyGroup", &attr); +} + +void Discovering() +{ + devList.clear(); + DiscoveryInterface(); + while (true) { + DisplayDevList(); + char op = getch(); + if (op == 'S' || op == 's') { + break; + } + } + StopDiscovery(clientName, 123); +} + +void SendData() +{ + NodeBasicInfo *dev; + int32_t dev_num; + GetAllNodeDeviceInfo(clientName, &dev, &dev_num); + CreateSessionServerInterface("SessionTest1"); + char op; + int32_t selectId = 0; + char input[128]; + while (true) { + DisplayTitle(); + PrintTitle(""); + PrintTitle(""); + PrintMessage("Press key:"); + PrintMessage("[U]Up [D]Down [S]Select"); + PrintTitle(""); + for (int32_t i = 0; i < dev_num; ++i) { + strcpy(input, i == selectId ? "*" : " "); + strcat(input, dev->deviceName); + PrintMessage(input); + } + PrintTitle(""); + PrintMargin(); + op = getch(); + if (op == 'U' || op == 'u') { + --selectId; + if (selectId < 0) { + selectId = dev_num - 1; + } + } else if (op == 'D' || op == 'd') { + ++selectId; + if (selectId == dev_num) { + selectId = 0; + } + } else if (op == 'S' || op == 's') { + break; + } + } + size_t inputLen = 0; + if (dev_num) { + int sessionId = OpenSessionInterface("SessionTest1", "SessionTest2", dev[selectId].networkId); + while (sessionId >= 0) { + input[inputLen] = '\0'; + DisplayTitle(); + PrintTitle(""); + PrintTitle(""); + PrintMessage("Press key:"); + PrintMessage("[Enter]Send [Esc]Back"); + PrintMessage("Message:"); + PrintMessage(input); + PrintTitle(""); + PrintMargin(); + op = getch(); + if (op == 10) { + SendBytes(sessionId, input, strlen(input)); + inputLen = 0; + } else if (op == 8) { + if (inputLen) { + --inputLen; + } + } else if (op == 27) { + break; + } else { + input[inputLen] = op; + ++inputLen; + } + } + CloseSession(selectId); + } +} + +void ReceiveData() +{ + DisplayReceivedData(); + CreateSessionServerInterface("SessionTest2"); + while (true) { +#if 0 + char op = getch(); + if (op == 27) { + break; + } +#endif + } +} + +int main(int argc, char **argv) +{ + bool needContinue = true; + initscr(); +#ifdef ImReceive + PublishServiceInterface(); + ReceiveData(); +// UnPublishService(clientName, 123); +#else + while (needContinue) { + DisplayMain(); + char op = getch(); + switch(op) { + case 'D': + case 'd': + Discovering(); + break; + case 'T': + case 't': + SendData(); + case 27: + needContinue = false; + } + } +#endif + endwin(); + return 0; +} -- Gitee From 252709f910ba0436b031e5d480766b2b20e4dadd Mon Sep 17 00:00:00 2001 From: zhengliming Date: Thu, 30 Jun 2022 14:12:29 +0800 Subject: [PATCH 2/5] Display interfaces reconstruction. Signed-off-by: zhengliming --- dsoftbus/test/softbus_main.cpp | 381 ++++++++++++--------------------- 1 file changed, 135 insertions(+), 246 deletions(-) diff --git a/dsoftbus/test/softbus_main.cpp b/dsoftbus/test/softbus_main.cpp index 45799fe8..22e65d83 100644 --- a/dsoftbus/test/softbus_main.cpp +++ b/dsoftbus/test/softbus_main.cpp @@ -1,174 +1,51 @@ #include -#include -#include #include #include #include - -#include "dsoftbus/discovery_service.h" -#include "dsoftbus/softbus_bus_center.h" -#include "dsoftbus/session.h" - -std::vector< std::string > devList; -char receivedData[256]; - -bool hasPublish = false; -bool hasDiscovery = false; -const size_t LINE_WIDTH = 80; - -// #define ImReceive - -#define clientName "Client" - -void PrintMargin() -{ - for (size_t i = 0; i < LINE_WIDTH; ++i) { - printw("#"); - } - printw("\n"); -} - -void PrintTitle(const char *msg) -{ - size_t len = strlen(msg); - if (len + 2 <= LINE_WIDTH) { - size_t emptyLen = LINE_WIDTH - len - 2; - size_t preEmptyLen = emptyLen / 2; - size_t postEmptyLen = emptyLen - preEmptyLen; - printw("#"); - while (preEmptyLen--) { - printw(" "); - } - printw("%s", msg); - while (postEmptyLen--) { - printw(" "); - } - printw("#\n"); - } else { - printw("#%s#\n", msg); - } -} - -void PrintMessage(const char *msg) -{ - size_t len = strlen(msg); - if (len + 6 <= LINE_WIDTH) { - size_t emptyLen = LINE_WIDTH - len - 6; - printw("# "); - printw("%s", msg); - while (emptyLen--) { - printw(" "); - } - printw(" #\n"); - } else { - printw("#%s#\n", msg); - } -} - -void DisplayTitle() -{ - erase(); - PrintMargin(); - PrintTitle("openEuler"); - PrintTitle("SoftBus"); - PrintTitle(""); - PrintTitle(""); -} - -void DisplayMain() -{ - DisplayTitle(); - PrintTitle(""); - PrintTitle(""); - PrintTitle(""); - PrintMessage("Press key:"); - PrintMessage("[D]DiscoveryService [T]DataTransmission"); - PrintTitle(""); - PrintTitle(""); - PrintMargin(); -} - -void DisplayDevList() -{ - char msg[256]; - DisplayTitle(); - PrintTitle(""); - PrintTitle(""); - PrintMessage("Press key:"); - PrintMessage("[S]StopDiscovery"); - PrintMessage("Devices list:"); - for (size_t i = 0; i < devList.size(); ++i) { - sprintf(msg, " %lu. %s", i, devList[i].c_str()); - PrintMessage(msg); - } - PrintTitle(""); - PrintMargin(); -} - -void DisplayReceivedData() -{ - DisplayTitle(); - PrintTitle(""); - PrintTitle(""); - PrintTitle(""); - PrintTitle(""); -// PrintMessage("Press key:"); -// PrintMessage("[Esc]Back"); - PrintMessage("Received data:"); - PrintMessage(receivedData); - PrintTitle(""); - PrintTitle(""); - PrintMargin(); -} - +/* void PublishSuccess(int publishId) { - // printf("CB: publish %d done", publishId); + printf("CB: publish %d done", publishId); } void PublishFailed(int publishId, PublishFailReason reason) { - // printf("CB: publish %d failed, reason=%d\n", publishId, (int)reason); + printf("CB: publish %d failed, reason=%d\n", publishId, (int)reason); } void DeviceFound(const DeviceInfo *device) { - devList.push_back(device->devName); - //DisplayDevList(); + printf("CB: a device found id=%s name=%s\n", device->devId, device->devName); } void DiscoverySuccess(int subscribeId) { - // printf("CB: discover subscribeId=%d\n", subscribeId); + printf("CB: discover subscribeId=%d\n", subscribeId); } void DiscoveryFailed(int subscribeId, DiscoveryFailReason reason) { - // printf("CB: discover subscribeId=%d failed, reason=%d\n", subscribeId, (int)reason); + printf("CB: discover subscribeId=%d failed, reason=%d\n", subscribeId, (int)reason); } -int SessionOpened(int sessionId, int result) +void SessionOpened(int sessionId, int result) { - // printf("CB: session %d open ret=%d\n", sessionId, result); - return 0; + printf("CB: session %d open ret=%d\n", subscribeId, result); } void SessionClosed(int sessionId) { - // printf("CB: session %d closed\n", sessionId); + printf("CB: session $d closed\n", sessionId); } void ByteRecived(int sessionId, const void *data, unsigned int dataLen) { - memset(receivedData, 0, sizeof(receivedData)); - memcpy(receivedData, data, dataLen); - DisplayReceivedData(); - // printf("CB: session %d received %u bytes data=%s\n", sessionId, dataLen, (const char *)data); + printf("CB: session %d received %u bytes data=%s\n", sessionId, dataLen, (const char *)data); } void MessageReceived(int sessionId, const void *data, unsigned int dataLen) { - // printf("CB: session %d received %u bytes message=%s\n", sessionId, dataLen, (const char *)data); + printf("CB: session %d received %u bytes message=%s\n", sessionId, dataLen, (const char *)data); } unsigned char cData[] = "My Client Test"; @@ -177,15 +54,15 @@ int PublishServiceInterface() { PublishInfo info = { .publishId = 123, - .mode = DISCOVER_MODE_PASSIVE, + .mode = DISCOVER_MODE_ACTIVE, .medium = COAP, .freq = LOW, .capability = "hicall", - .capabilityData = cData, - .dataLen = sizeof(cData), + capabilityData = cData, + dataLen = sizeof(cData), }; IPublishCallback cb = { - .OnPublishSuccess = PublishSuccess, + .OnPublishSucess = PublishSuccess, .OnPublishFail = PublishFailed, }; return PublishService(clientName, &info, &cb); @@ -207,7 +84,7 @@ int DiscoveryInterface() IDiscoveryCallback cb = { .OnDeviceFound = DeviceFound, .OnDiscoverFailed = DiscoveryFailed, - .OnDiscoverySuccess = DiscoverySuccess, + .OnDiscoverySuccesss = DiscoverySuccess, }; return StartDiscovery(clientName, &info, &cb); } @@ -225,140 +102,152 @@ int CreateSessionServerInterface(const char *SessionName) int OpenSessionInterface(const char *SessionName, const char *peerName, const char *peerId) { - SessionAttribute attr = { + SessionAttrbute attr = { .dataType = TYPE_BYTES, .lintTypeNum = 1, - .attr = {RAW_STREAM}, + attr = {RAW_STREAM}, }; int lt = LINK_TYPE_WIFI_P2P; attr.lintType = < return OpenSession(SessionName, peerName, peerId, "MyGroup", &attr); } +*/ -void Discovering() +bool hasPublish = false; +bool hasDiscovery = false; +const size_t LINE_WIDTH = 80; + +void PrintMargin() { - devList.clear(); - DiscoveryInterface(); - while (true) { - DisplayDevList(); - char op = getch(); - if (op == 'S' || op == 's') { - break; - } + for (size_t i = 0; i < LINE_WIDTH; ++i) { + printw("#"); } - StopDiscovery(clientName, 123); + printw("\n"); } -void SendData() +void PrintTitle(const char *msg) { - NodeBasicInfo *dev; - int32_t dev_num; - GetAllNodeDeviceInfo(clientName, &dev, &dev_num); - CreateSessionServerInterface("SessionTest1"); - char op; - int32_t selectId = 0; - char input[128]; - while (true) { - DisplayTitle(); - PrintTitle(""); - PrintTitle(""); - PrintMessage("Press key:"); - PrintMessage("[U]Up [D]Down [S]Select"); - PrintTitle(""); - for (int32_t i = 0; i < dev_num; ++i) { - strcpy(input, i == selectId ? "*" : " "); - strcat(input, dev->deviceName); - PrintMessage(input); + size_t len = strlen(msg); + if (len + 2 <= LINE_WIDTH) { + size_t emptyLen = LINE_WIDTH - len - 2; + size_t preEmptyLen = emptyLen / 2; + size_t postEmptyLen = emptyLen - preEmptyLen; + printw("#"); + while (preEmptyLen--) { + printw(" "); } - PrintTitle(""); - PrintMargin(); - op = getch(); - if (op == 'U' || op == 'u') { - --selectId; - if (selectId < 0) { - selectId = dev_num - 1; - } - } else if (op == 'D' || op == 'd') { - ++selectId; - if (selectId == dev_num) { - selectId = 0; - } - } else if (op == 'S' || op == 's') { - break; + printw("%s", msg); + while (postEmptyLen--) { + printw(" "); } + printw("#\n"); + } else { + printw("#%s#\n", msg); } - size_t inputLen = 0; - if (dev_num) { - int sessionId = OpenSessionInterface("SessionTest1", "SessionTest2", dev[selectId].networkId); - while (sessionId >= 0) { - input[inputLen] = '\0'; - DisplayTitle(); - PrintTitle(""); - PrintTitle(""); - PrintMessage("Press key:"); - PrintMessage("[Enter]Send [Esc]Back"); - PrintMessage("Message:"); - PrintMessage(input); - PrintTitle(""); - PrintMargin(); - op = getch(); - if (op == 10) { - SendBytes(sessionId, input, strlen(input)); - inputLen = 0; - } else if (op == 8) { - if (inputLen) { - --inputLen; - } - } else if (op == 27) { - break; - } else { - input[inputLen] = op; - ++inputLen; - } +} + +void PrintMessage(const char *msg) +{ + size_t len = strlen(msg); + if (len + 6 <= LINE_WIDTH) { + size_t emptyLen = LINE_WIDTH - len - 6; + printw("# "); + printw("%s", msg); + while (emptyLen--) { + printw(" "); } - CloseSession(selectId); + printw(" #\n"); + } else { + printw("#%s#\n", msg); } } -void ReceiveData() +void DisplayMain() { - DisplayReceivedData(); - CreateSessionServerInterface("SessionTest2"); + char msg[256]; + erase(); + PrintMargin(); + PrintTitle("openEuler"); + PrintTitle("SoftBus"); + PrintTitle(""); + PrintTitle(""); + PrintTitle(""); + PrintTitle(""); + PrintMessage("Press key:"); + strcpy(msg, hasPublish ? "[U]UnpublishService" : "[P]PublishService "); + strcat(msg, " "); + strcat(msg, hasDiscovery ? "[S]StopDiscovery " : "[D]DiscoveryService"); + PrintMessage(msg); + PrintMessage("[T]DataTransmission"); + PrintTitle(""); + PrintTitle(""); + PrintMargin(); +} + +void DataTransmission() +{ + NodeBasicInfo *dev; + int32_t dev_num; + GetAllNodeDeviceInfo(clientName, &dev, &dev_num); + CreateSessionServerInterface("SessionTest1"); + char op; while (true) { -#if 0 - char op = getch(); - if (op == 27) { - break; - } -#endif + DisplayDevList(); + op = getch(); + } + OpenSession("SessionTest1", "SessionTest2", peerId) +} + +void ProcessKeyMain() +{ + char op = getch(); + switch(op) { + case 'P': + case 'p': + hasPublish = true; + break; + case 'U': + case 'u': + hasPublish = false; + break; + case 'D': + case 'd': + hasDiscovery = true; + break; + case 'S': + case 's': + hasDiscovery = false; + break; + case 'T': + case 't': + DataTransmission(); } } int main(int argc, char **argv) { - bool needContinue = true; - initscr(); -#ifdef ImReceive - PublishServiceInterface(); - ReceiveData(); -// UnPublishService(clientName, 123); -#else - while (needContinue) { - DisplayMain(); - char op = getch(); - switch(op) { - case 'D': - case 'd': - Discovering(); - break; - case 'T': - case 't': - SendData(); - case 27: - needContinue = false; + pid_t pid = fork(); + if (pid < 0) { + puts("Fork failed"); + } else if (pid > 0) { + initscr(); + while (true) { + DisplayMain(); + ProcessKey(); } + endwin(); + /*if (argc > 1) { + CreateSessionServerInterface("SessionTest2"); + } else { + + + + }*/ + } else { + /*InitSoftBusServer(); + while (true) { + pause(); + }*/ } -#endif - endwin(); return 0; -} +} \ No newline at end of file -- Gitee From 66f3b7ccf5dbacd9b68ce916034e46860966db79 Mon Sep 17 00:00:00 2001 From: zhengliming Date: Thu, 30 Jun 2022 14:15:13 +0800 Subject: [PATCH 3/5] Fix compile error. Signed-off-by: zhengliming --- dsoftbus/test/softbus_main.cpp | 300 ++++++++++++++++++++------------- 1 file changed, 182 insertions(+), 118 deletions(-) diff --git a/dsoftbus/test/softbus_main.cpp b/dsoftbus/test/softbus_main.cpp index 22e65d83..a94d2f4c 100644 --- a/dsoftbus/test/softbus_main.cpp +++ b/dsoftbus/test/softbus_main.cpp @@ -1,8 +1,104 @@ #include +#include #include #include #include -/* + +#include "dsoftbus/discovery_service.h" +#include "dsoftbus/softbus_bus_center.h" +#include "dsoftbus/session.h" + +std::vector devList; + +bool hasPublish = false; +bool hasDiscovery = false; +const size_t LINE_WIDTH = 80; + +#define clientName "Client" + +void PrintMargin() +{ + for (size_t i = 0; i < LINE_WIDTH; ++i) { + printw("#"); + } + printw("\n"); +} + +void PrintTitle(const char *msg) +{ + size_t len = strlen(msg); + if (len + 2 <= LINE_WIDTH) { + size_t emptyLen = LINE_WIDTH - len - 2; + size_t preEmptyLen = emptyLen / 2; + size_t postEmptyLen = emptyLen - preEmptyLen; + printw("#"); + while (preEmptyLen--) { + printw(" "); + } + printw("%s", msg); + while (postEmptyLen--) { + printw(" "); + } + printw("#\n"); + } else { + printw("#%s#\n", msg); + } +} + +void PrintMessage(const char *msg) +{ + size_t len = strlen(msg); + if (len + 6 <= LINE_WIDTH) { + size_t emptyLen = LINE_WIDTH - len - 6; + printw("# "); + printw("%s", msg); + while (emptyLen--) { + printw(" "); + } + printw(" #\n"); + } else { + printw("#%s#\n", msg); + } +} + +void DisplayTitle() +{ + erase(); + PrintMargin(); + PrintTitle("openEuler"); + PrintTitle("SoftBus"); + PrintTitle(""); + PrintTitle(""); +} + +void DisplayMain() +{ + DisplayTitle(); + PrintTitle(""); + PrintTitle(""); + PrintTitle(""); + PrintMessage("Press key:"); + PrintMessage("[D]DiscoveryService [T]DataTransmission"); + PrintTitle(""); + PrintTitle(""); + PrintMargin(); +} + +void DisplayDevList() +{ + char msg[256]; + DisplayTitle(); + PrintTitle(""); + PrintTitle(""); + PrintMessage("Press key:"); + PrintMessage("[S]StopDiscovery"); + PrintMessage("Devices list:"); + for (size_t i = 0; i < devList.size(); ++i) + PrintMessage(devList[i]->devId); + PrintTitle(""); + PrintMargin(); +} + void PublishSuccess(int publishId) { printf("CB: publish %d done", publishId); @@ -15,7 +111,8 @@ void PublishFailed(int publishId, PublishFailReason reason) void DeviceFound(const DeviceInfo *device) { - printf("CB: a device found id=%s name=%s\n", device->devId, device->devName); + devList.push_back(device); + DisplayDevList(); } void DiscoverySuccess(int subscribeId) @@ -28,14 +125,15 @@ void DiscoveryFailed(int subscribeId, DiscoveryFailReason reason) printf("CB: discover subscribeId=%d failed, reason=%d\n", subscribeId, (int)reason); } -void SessionOpened(int sessionId, int result) +int SessionOpened(int sessionId, int result) { - printf("CB: session %d open ret=%d\n", subscribeId, result); + printf("CB: session %d open ret=%d\n", sessionId, result); + return 0; } void SessionClosed(int sessionId) { - printf("CB: session $d closed\n", sessionId); + printf("CB: session %d closed\n", sessionId); } void ByteRecived(int sessionId, const void *data, unsigned int dataLen) @@ -58,11 +156,11 @@ int PublishServiceInterface() .medium = COAP, .freq = LOW, .capability = "hicall", - capabilityData = cData, - dataLen = sizeof(cData), + .capabilityData = cData, + .dataLen = sizeof(cData), }; IPublishCallback cb = { - .OnPublishSucess = PublishSuccess, + .OnPublishSuccess = PublishSuccess, .OnPublishFail = PublishFailed, }; return PublishService(clientName, &info, &cb); @@ -84,7 +182,7 @@ int DiscoveryInterface() IDiscoveryCallback cb = { .OnDeviceFound = DeviceFound, .OnDiscoverFailed = DiscoveryFailed, - .OnDiscoverySuccesss = DiscoverySuccess, + .OnDiscoverySuccess = DiscoverySuccess, }; return StartDiscovery(clientName, &info, &cb); } @@ -102,152 +200,118 @@ int CreateSessionServerInterface(const char *SessionName) int OpenSessionInterface(const char *SessionName, const char *peerName, const char *peerId) { - SessionAttrbute attr = { + SessionAttribute attr = { .dataType = TYPE_BYTES, .lintTypeNum = 1, - attr = {RAW_STREAM}, + .attr = {RAW_STREAM}, }; int lt = LINK_TYPE_WIFI_P2P; attr.lintType = < return OpenSession(SessionName, peerName, peerId, "MyGroup", &attr); } -*/ - -bool hasPublish = false; -bool hasDiscovery = false; -const size_t LINE_WIDTH = 80; - -void PrintMargin() -{ - for (size_t i = 0; i < LINE_WIDTH; ++i) { - printw("#"); - } - printw("\n"); -} - -void PrintTitle(const char *msg) -{ - size_t len = strlen(msg); - if (len + 2 <= LINE_WIDTH) { - size_t emptyLen = LINE_WIDTH - len - 2; - size_t preEmptyLen = emptyLen / 2; - size_t postEmptyLen = emptyLen - preEmptyLen; - printw("#"); - while (preEmptyLen--) { - printw(" "); - } - printw("%s", msg); - while (postEmptyLen--) { - printw(" "); - } - printw("#\n"); - } else { - printw("#%s#\n", msg); - } -} -void PrintMessage(const char *msg) +void Discovering() { - size_t len = strlen(msg); - if (len + 6 <= LINE_WIDTH) { - size_t emptyLen = LINE_WIDTH - len - 6; - printw("# "); - printw("%s", msg); - while (emptyLen--) { - printw(" "); + DiscoveryInterface(); + while (true) { + DisplayDevList(); + char op = getch(); + if (op == 'S' || op == 's') { + break; } - printw(" #\n"); - } else { - printw("#%s#\n", msg); } } -void DisplayMain() -{ - char msg[256]; - erase(); - PrintMargin(); - PrintTitle("openEuler"); - PrintTitle("SoftBus"); - PrintTitle(""); - PrintTitle(""); - PrintTitle(""); - PrintTitle(""); - PrintMessage("Press key:"); - strcpy(msg, hasPublish ? "[U]UnpublishService" : "[P]PublishService "); - strcat(msg, " "); - strcat(msg, hasDiscovery ? "[S]StopDiscovery " : "[D]DiscoveryService"); - PrintMessage(msg); - PrintMessage("[T]DataTransmission"); - PrintTitle(""); - PrintTitle(""); - PrintMargin(); -} - -void DataTransmission() +void SendData() { NodeBasicInfo *dev; int32_t dev_num; GetAllNodeDeviceInfo(clientName, &dev, &dev_num); CreateSessionServerInterface("SessionTest1"); char op; + int32_t selectId = 0; + char input[128]; while (true) { - DisplayDevList(); + DisplayTitle(); + PrintTitle(""); + PrintTitle(""); + PrintMessage("Press key:"); + PrintMessage("[U]Up [D]Down [S]Select"); + PrintTitle(""); + for (int32_t i = 0; i < dev_num; ++i) { + strcpy(input, i == selectId ? "*" : " "); + strcat(input, dev->networkId); + PrintMessage(input); + } + PrintTitle(""); + PrintMargin(); + // to do op = getch(); + if (op == 'U' || op == 'u') { + --selectId; + if (selectId < 0) { + selectId = dev_num - 1; + } + } else if (op == 'D' || op == 'd') { + ++selectId; + if (selectId == dev_num) { + selectId = 0; + } + } else if (op == 'S' || op == 's') { + break; + } + } + size_t inputLen = 0; + if (dev_num) { + int sessionId = OpenSessionInterface("SessionTest1", "SessionTest2", dev[selectId].networkId); + while (sessionId >= 0) { + input[inputLen] = '\0'; + DisplayTitle(); + PrintTitle(""); + PrintTitle(""); + PrintMessage("Press key:"); + PrintMessage("[]"); + PrintMessage("Message:"); + PrintMessage(input); + PrintTitle(""); + PrintMargin(); + op = getch(); + if (op == 13) { + SendBytes(sessionId, input, strlen(input)); + inputLen = 0; + } else if (op == 8) { + if (inputLen) { + --inputLen; + } + } else if (op == 27) { + break; + } + } + CloseSession(selectId); } - OpenSession("SessionTest1", "SessionTest2", peerId) } void ProcessKeyMain() { char op = getch(); switch(op) { - case 'P': - case 'p': - hasPublish = true; - break; - case 'U': - case 'u': - hasPublish = false; - break; case 'D': case 'd': - hasDiscovery = true; - break; - case 'S': - case 's': - hasDiscovery = false; + Discovering(); break; case 'T': case 't': - DataTransmission(); + SendData(); } } int main(int argc, char **argv) { - pid_t pid = fork(); - if (pid < 0) { - puts("Fork failed"); - } else if (pid > 0) { - initscr(); - while (true) { - DisplayMain(); - ProcessKey(); - } - endwin(); - /*if (argc > 1) { - CreateSessionServerInterface("SessionTest2"); - } else { - - - - }*/ - } else { - /*InitSoftBusServer(); - while (true) { - pause(); - }*/ + initscr(); + while (true) { + DisplayMain(); + ProcessKeyMain(); } + endwin(); return 0; -} \ No newline at end of file +} -- Gitee From 22bc3af3a29db944d89d5d1e11688a7f20ca76d8 Mon Sep 17 00:00:00 2001 From: zhengliming Date: Thu, 30 Jun 2022 14:16:43 +0800 Subject: [PATCH 4/5] Add receiving process. Signed-off-by: zhengliming --- dsoftbus/test/softbus_main.cpp | 83 +++++++++++++++++++++++++--------- 1 file changed, 62 insertions(+), 21 deletions(-) diff --git a/dsoftbus/test/softbus_main.cpp b/dsoftbus/test/softbus_main.cpp index a94d2f4c..bba26e55 100644 --- a/dsoftbus/test/softbus_main.cpp +++ b/dsoftbus/test/softbus_main.cpp @@ -9,11 +9,14 @@ #include "dsoftbus/session.h" std::vector devList; +char receivedData[256]; bool hasPublish = false; bool hasDiscovery = false; const size_t LINE_WIDTH = 80; +// #define ImReceive + #define clientName "Client" void PrintMargin() @@ -99,14 +102,28 @@ void DisplayDevList() PrintMargin(); } +void DisplayReceivedData() +{ + DisplayTitle(); + PrintTitle(""); + PrintTitle(""); + PrintMessage("Press key:"); + PrintMessage("[Esc]Back"); + PrintMessage("Received data:"); + PrintMessage(receivedData); + PrintTitle(""); + PrintTitle(""); + PrintMargin(); +} + void PublishSuccess(int publishId) { - printf("CB: publish %d done", publishId); + // printf("CB: publish %d done", publishId); } void PublishFailed(int publishId, PublishFailReason reason) { - printf("CB: publish %d failed, reason=%d\n", publishId, (int)reason); + // printf("CB: publish %d failed, reason=%d\n", publishId, (int)reason); } void DeviceFound(const DeviceInfo *device) @@ -117,33 +134,36 @@ void DeviceFound(const DeviceInfo *device) void DiscoverySuccess(int subscribeId) { - printf("CB: discover subscribeId=%d\n", subscribeId); + // printf("CB: discover subscribeId=%d\n", subscribeId); } void DiscoveryFailed(int subscribeId, DiscoveryFailReason reason) { - printf("CB: discover subscribeId=%d failed, reason=%d\n", subscribeId, (int)reason); + // printf("CB: discover subscribeId=%d failed, reason=%d\n", subscribeId, (int)reason); } int SessionOpened(int sessionId, int result) { - printf("CB: session %d open ret=%d\n", sessionId, result); + // printf("CB: session %d open ret=%d\n", sessionId, result); return 0; } void SessionClosed(int sessionId) { - printf("CB: session %d closed\n", sessionId); + // printf("CB: session %d closed\n", sessionId); } void ByteRecived(int sessionId, const void *data, unsigned int dataLen) { - printf("CB: session %d received %u bytes data=%s\n", sessionId, dataLen, (const char *)data); + memset(ReceiveData, 0, sizeof(ReceiveData)); + memcpy(ReceiveData, data, dataLen); + DisplayReceivedData(); + // printf("CB: session %d received %u bytes data=%s\n", sessionId, dataLen, (const char *)data); } void MessageReceived(int sessionId, const void *data, unsigned int dataLen) { - printf("CB: session %d received %u bytes message=%s\n", sessionId, dataLen, (const char *)data); + // printf("CB: session %d received %u bytes message=%s\n", sessionId, dataLen, (const char *)data); } unsigned char cData[] = "My Client Test"; @@ -212,6 +232,7 @@ int OpenSessionInterface(const char *SessionName, const char *peerName, const ch void Discovering() { + devList.clear(); DiscoveryInterface(); while (true) { DisplayDevList(); @@ -270,7 +291,7 @@ void SendData() PrintTitle(""); PrintTitle(""); PrintMessage("Press key:"); - PrintMessage("[]"); + PrintMessage("[Enter]Send [Esc]Back"); PrintMessage("Message:"); PrintMessage(input); PrintTitle(""); @@ -285,32 +306,52 @@ void SendData() } } else if (op == 27) { break; + } else { + input[inputLen] = op; + ++inputLen; } } CloseSession(selectId); } } -void ProcessKeyMain() +void ReceiveData() { - char op = getch(); - switch(op) { - case 'D': - case 'd': - Discovering(); - break; - case 'T': - case 't': - SendData(); + NodeBasicInfo *dev; + int32_t dev_num; + GetAllNodeDeviceInfo(clientName, &dev, &dev_num); + CreateSessionServerInterface("SessionTest2"); + while (true) { + DisplayReceivedData(); + char op = getch(); + if (op == 27) { + break; + } } } int main(int argc, char **argv) { + bool needContinue = true; initscr(); - while (true) { + while (needContinue) { DisplayMain(); - ProcessKeyMain(); + char op = getch(); + switch(op) { + case 'D': + case 'd': + Discovering(); + break; + case 'T': + case 't': +#ifdef ImReceive + ReceiveData(); +#else + SendData(); +#endif + case 27: + needContinue = false; + } } endwin(); return 0; -- Gitee From 6c52b6ed3c50f10e526ac310c1af038e356c812d Mon Sep 17 00:00:00 2001 From: zhengliming Date: Thu, 30 Jun 2022 14:18:39 +0800 Subject: [PATCH 5/5] Fix the compile error of receiving process. Signed-off-by: zhengliming --- dsoftbus/test/softbus_main.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dsoftbus/test/softbus_main.cpp b/dsoftbus/test/softbus_main.cpp index bba26e55..0e5a9c5c 100644 --- a/dsoftbus/test/softbus_main.cpp +++ b/dsoftbus/test/softbus_main.cpp @@ -155,8 +155,8 @@ void SessionClosed(int sessionId) void ByteRecived(int sessionId, const void *data, unsigned int dataLen) { - memset(ReceiveData, 0, sizeof(ReceiveData)); - memcpy(ReceiveData, data, dataLen); + memset(receivedData, 0, sizeof(receivedData)); + memcpy(receivedData, data, dataLen); DisplayReceivedData(); // printf("CB: session %d received %u bytes data=%s\n", sessionId, dataLen, (const char *)data); } -- Gitee