Skip to content

Commit

Permalink
Functional discovery for LOCAL_MEMSHD
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMutta committed Jul 11, 2024
1 parent 084a8e0 commit c7e2a98
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 46 deletions.
10 changes: 6 additions & 4 deletions src/pc/PlatformData.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
13 changes: 6 additions & 7 deletions src/pc/PlatformDeviceSearch.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
15 changes: 9 additions & 6 deletions src/pc/protocols/local_memshd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
70 changes: 41 additions & 29 deletions src/pc/protocols/tcpip_memshd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
#include "local_memshd.h"
#include "tcpip_memshd.h"

#define MVLOG_UNIT_NAME tcpip_memshd
#include "XLinkLog.h"

#include <signal.h>

#include <atomic>
Expand All @@ -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<std::mutex> l(connectionMutex);
retShdmem = ret;
isShdmemThreadFinished = true;
}
cv.notify_one();
});

auto threadTcpip = std::thread([&connectionMutex,
&cv,
&isTcpIpThreadFinished,
Expand All @@ -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<std::mutex> l(connectionMutex);
retShdmem = ret;
isShdmemThreadFinished = true;
}
cv.notify_one();
});

{
Expand All @@ -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
Expand Down Expand 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
Expand Down

0 comments on commit c7e2a98

Please sign in to comment.