From c7e2a98451a0b212109728d44bc30323e83a18df Mon Sep 17 00:00:00 2001 From: Filippo Mutta Date: Thu, 11 Jul 2024 13:00:09 +0200 Subject: [PATCH] Functional discovery for LOCAL_MEMSHD --- src/pc/PlatformData.c | 10 +++-- src/pc/PlatformDeviceSearch.c | 13 +++--- src/pc/protocols/local_memshd.cpp | 15 ++++--- src/pc/protocols/tcpip_memshd.cpp | 70 ++++++++++++++++++------------- 4 files changed, 62 insertions(+), 46 deletions(-) diff --git a/src/pc/PlatformData.c b/src/pc/PlatformData.c index ae6b38f..10918c4 100644 --- a/src/pc/PlatformData.c +++ b/src/pc/PlatformData.c @@ -96,10 +96,11 @@ int XLinkPlatformWrite(xLinkDeviceHandle_t *deviceHandle, void *data, int size) return tcpipPlatformWrite(deviceHandle->xLinkFD, data, size); #if defined(__unix__) - case X_LINK_TCP_IP_OR_LOCAL_SHDMEM: case X_LINK_LOCAL_SHDMEM: return shdmemPlatformWrite(deviceHandle->xLinkFD, data, size); #endif + case X_LINK_TCP_IP_OR_LOCAL_SHDMEM: + mvLog(MVLOG_ERROR, "Failed to write with TCP_IP_OR_LOCAL_SHDMEM\n"); default: return X_LINK_PLATFORM_INVALID_PARAMETERS; @@ -114,7 +115,6 @@ int XLinkPlatformWriteFd(xLinkDeviceHandle_t *deviceHandle, const long fd, void switch (deviceHandle->protocol) { #if defined(__unix__) - case X_LINK_TCP_IP_OR_LOCAL_SHDMEM: case X_LINK_LOCAL_SHDMEM: return shdmemPlatformWriteFd(deviceHandle->xLinkFD, fd, data2, size2); @@ -163,6 +163,8 @@ int XLinkPlatformWriteFd(xLinkDeviceHandle_t *deviceHandle, const long fd, void return result; } #endif + case X_LINK_TCP_IP_OR_LOCAL_SHDMEM: + mvLog(MVLOG_ERROR, "Failed to write FD with TCP_IP_OR_LOCAL_SHDMEM\n"); default: return X_LINK_PLATFORM_INVALID_PARAMETERS; } @@ -186,11 +188,11 @@ int XLinkPlatformRead(xLinkDeviceHandle_t *deviceHandle, void *data, int size, l return tcpipPlatformRead(deviceHandle->xLinkFD, data, size); #if defined(__unix__) - case X_LINK_TCP_IP_OR_LOCAL_SHDMEM: case X_LINK_LOCAL_SHDMEM: return shdmemPlatformRead(deviceHandle->xLinkFD, data, size, fd); #endif - + case X_LINK_TCP_IP_OR_LOCAL_SHDMEM: + mvLog(MVLOG_ERROR, "Failed to read with TCP_IP_OR_LOCAL_SHDMEM\n"); default: return X_LINK_PLATFORM_INVALID_PARAMETERS; } diff --git a/src/pc/PlatformDeviceSearch.c b/src/pc/PlatformDeviceSearch.c index 7067739..762b827 100644 --- a/src/pc/PlatformDeviceSearch.c +++ b/src/pc/PlatformDeviceSearch.c @@ -80,10 +80,9 @@ xLinkPlatformErrorCode_t XLinkPlatformFindDevices(const deviceDesc_t in_deviceRe if(!XLinkIsProtocolInitialized(in_deviceRequirements.protocol)) { return X_LINK_PLATFORM_DRIVER_NOT_LOADED+in_deviceRequirements.protocol; } - return getLocalShdmemDevices(in_deviceRequirements, out_foundDevices, sizeFoundDevices, &numFoundDevices); + return getLocalShdmemDevices(in_deviceRequirements, out_foundDevices, sizeFoundDevices, out_amountOfFoundDevices); case X_LINK_ANY_PROTOCOL: - // If USB protocol is initialized if(XLinkIsProtocolInitialized(X_LINK_USB_VSC)) { // Find first correct USB Device @@ -118,10 +117,9 @@ xLinkPlatformErrorCode_t XLinkPlatformFindDevices(const deviceDesc_t in_deviceRe */ case X_LINK_TCP_IP_OR_LOCAL_SHDMEM: - // Try find TCPIP device - if(XLinkIsProtocolInitialized(X_LINK_TCP_IP)) { + if(XLinkIsProtocolInitialized(X_LINK_LOCAL_SHDMEM)) { numFoundDevices = 0; - TCPIP_rc = getTcpIpDevices(in_deviceRequirements, out_foundDevices, sizeFoundDevices, &numFoundDevices); + SHDMEM_rc = getLocalShdmemDevices(in_deviceRequirements, out_foundDevices, sizeFoundDevices, &numFoundDevices); *out_amountOfFoundDevices += numFoundDevices; out_foundDevices += numFoundDevices; // Found enough devices, return @@ -132,9 +130,10 @@ xLinkPlatformErrorCode_t XLinkPlatformFindDevices(const deviceDesc_t in_deviceRe } } - if(XLinkIsProtocolInitialized(X_LINK_LOCAL_SHDMEM)) { + // Try find TCPIP device + if(XLinkIsProtocolInitialized(X_LINK_TCP_IP)) { numFoundDevices = 0; - SHDMEM_rc = getLocalShdmemDevices(in_deviceRequirements, out_foundDevices, sizeFoundDevices, &numFoundDevices); + TCPIP_rc = getTcpIpDevices(in_deviceRequirements, out_foundDevices, sizeFoundDevices, &numFoundDevices); *out_amountOfFoundDevices += numFoundDevices; out_foundDevices += numFoundDevices; // Found enough devices, return diff --git a/src/pc/protocols/local_memshd.cpp b/src/pc/protocols/local_memshd.cpp index c5680c1..d7b4161 100644 --- a/src/pc/protocols/local_memshd.cpp +++ b/src/pc/protocols/local_memshd.cpp @@ -220,18 +220,21 @@ xLinkPlatformErrorCode_t shdmemGetDevices(const deviceDesc_t in_deviceRequiremen if (access(SHDMEM_DEFAULT_SOCKET, F_OK) != 0) { return X_LINK_PLATFORM_ERROR; } - + // Status - out_foundDevices[0].status = in_deviceRequirements.status; + out_foundDevices[0].status = X_LINK_SUCCESS; // IP memset(out_foundDevices[0].name, 0, sizeof(out_foundDevices[0].name)); - strncpy(out_foundDevices[0].name, "127.0.0.1", sizeof(out_foundDevices[0].name)); + strncpy(out_foundDevices[0].name, SHDMEM_DEFAULT_SOCKET, sizeof(out_foundDevices[0].name)); + // MXID + memset(out_foundDevices[0].mxid, 0, sizeof(out_foundDevices[0].mxid)); + strncpy(out_foundDevices[0].mxid, in_deviceRequirements.mxid, sizeof(out_foundDevices[0].mxid)); // Platform - out_foundDevices[0].platform = in_deviceRequirements.platform; + out_foundDevices[0].platform = X_LINK_MYRIAD_X; // Protocol - out_foundDevices[0].protocol = in_deviceRequirements.protocol; + out_foundDevices[0].protocol = X_LINK_LOCAL_SHDMEM; // State - out_foundDevices[0].state = in_deviceRequirements.state; + out_foundDevices[0].state = X_LINK_BOOTED; *out_amountOfFoundDevices = 1; diff --git a/src/pc/protocols/tcpip_memshd.cpp b/src/pc/protocols/tcpip_memshd.cpp index 370235d..db2be93 100644 --- a/src/pc/protocols/tcpip_memshd.cpp +++ b/src/pc/protocols/tcpip_memshd.cpp @@ -2,6 +2,9 @@ #include "local_memshd.h" #include "tcpip_memshd.h" +#define MVLOG_UNIT_NAME tcpip_memshd +#include "XLinkLog.h" + #include #include @@ -24,6 +27,21 @@ int tcpipOrLocalShdmemPlatformServer(XLinkProtocol_t *protocol, const char *devP void *fdTcpIp = nullptr, *fdShdmem = nullptr; long tcpIpSockFd = -1, shdmemSockFd = -1; + auto threadShdmem = std::thread([&connectionMutex, + &cv, + &isShdmemThreadFinished, + &retShdmem, + &fdShdmem, + &shdmemSockFd](){ + auto ret = shdmemPlatformServer(SHDMEM_DEFAULT_SOCKET, SHDMEM_DEFAULT_SOCKET, &fdShdmem, &shdmemSockFd); + { + std::unique_lock l(connectionMutex); + retShdmem = ret; + isShdmemThreadFinished = true; + } + cv.notify_one(); + }); + auto threadTcpip = std::thread([&connectionMutex, &cv, &isTcpIpThreadFinished, @@ -39,21 +57,6 @@ int tcpipOrLocalShdmemPlatformServer(XLinkProtocol_t *protocol, const char *devP isTcpIpThreadFinished = true; } cv.notify_one(); - }); - - auto threadShdmem = std::thread([&connectionMutex, - &cv, - &isShdmemThreadFinished, - &retShdmem, - &fdShdmem, - &shdmemSockFd](){ - auto ret = shdmemPlatformServer(SHDMEM_DEFAULT_SOCKET, SHDMEM_DEFAULT_SOCKET, &fdShdmem, &shdmemSockFd); - { - std::unique_lock l(connectionMutex); - retShdmem = ret; - isShdmemThreadFinished = true; - } - cv.notify_one(); }); { @@ -64,27 +67,30 @@ int tcpipOrLocalShdmemPlatformServer(XLinkProtocol_t *protocol, const char *devP // As soon as either one finishes, the other can be cleaned // Use signals, as "accept" cannot be unblocked by "close"ing the underlying socket - if(!isShdmemThreadFinished) { - if(shdmemSockFd >= 0) { - shutdown(shdmemSockFd, SHUT_RDWR); + if(!isTcpIpThreadFinished) { + if(tcpIpSockFd >= 0) { + shutdown(tcpIpSockFd, SHUT_RDWR); #if defined(SO_LINGER) const int set = 0; - setsockopt(shdmemSockFd, SOL_SOCKET, SO_LINGER, (const char*)&set, sizeof(set)); + setsockopt(tcpIpSockFd, SOL_SOCKET, SO_LINGER, (const char*)&set, sizeof(set)); #endif - close(shdmemSockFd); + close(tcpIpSockFd); } - + + mvLog(MVLOG_ERROR, "Failed to start server with TCP/IP"); } - if(!isTcpIpThreadFinished) { - if(tcpIpSockFd >= 0) { - shutdown(tcpIpSockFd, SHUT_RDWR); + if(!isShdmemThreadFinished) { + if(shdmemSockFd >= 0) { + shutdown(shdmemSockFd, SHUT_RDWR); #if defined(SO_LINGER) const int set = 0; - setsockopt(tcpIpSockFd, SOL_SOCKET, SO_LINGER, (const char*)&set, sizeof(set)); + setsockopt(shdmemSockFd, SOL_SOCKET, SO_LINGER, (const char*)&set, sizeof(set)); #endif - close(tcpIpSockFd); + close(shdmemSockFd); } + + mvLog(MVLOG_ERROR, "Failed to start server with SHDMEM"); } // Wait for both threads to wrap up @@ -112,11 +118,17 @@ int tcpipOrLocalShdmemPlatformServer(XLinkProtocol_t *protocol, const char *devP int tcpipOrLocalShdmemPlatformConnect(XLinkProtocol_t *protocol, const char *devPathRead, const char *devPathWrite, void **fd) { if(shdmemPlatformConnect(SHDMEM_DEFAULT_SOCKET, SHDMEM_DEFAULT_SOCKET, fd) == X_LINK_SUCCESS) { + mvLog(MVLOG_ERROR, "Failed to connect with SHDMEM"); return shdmemSetProtocol(protocol, devPathRead, devPathWrite); } - - *protocol = X_LINK_TCP_IP; - return tcpipPlatformConnect(devPathRead, devPathWrite, fd); + + if (tcpipPlatformConnect(devPathRead, devPathWrite, fd) == X_LINK_SUCCESS) { + mvLog(MVLOG_ERROR, "Failed to connect with TCP/IP"); + *protocol = X_LINK_TCP_IP; + return X_LINK_SUCCESS; + } + + return X_LINK_ERROR; } #else