Skip to content

Commit

Permalink
Test for *nix pipes
Browse files Browse the repository at this point in the history
  • Loading branch information
GXTX committed Mar 1, 2024
1 parent af4d0a9 commit 028c8e9
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
39 changes: 39 additions & 0 deletions Includes/SerIo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ bool SerIo::Open()
m_isPipe = true;
return true;
}
#else
if (m_portSettings->devicePath.find("pipe") != std::string::npos) {
m_pipeHandle = open(m_portSettings->devicePath.c_str(), O_RDWR | O_NOCTTY | O_SYNC | O_NDELAY);

if (!m_pipeHandle)
{
g_logger->critical("SerIo::Init: Failed to create pipe");
return false;
}
}
m_isPipe = true;
return true;
#endif

sp_new_config(&m_portConfig);
Expand Down Expand Up @@ -109,6 +121,8 @@ SerIo::Status SerIo::Write()
DWORD dwRet = 0;
WriteFile(m_pipeHandle, &m_writeBuffer[0], static_cast<DWORD>(m_writeBuffer.size()), &dwRet, NULL);
ret = dwRet;
#else
ret = write(m_pipeHandle, &m_writeBuffer[0], m_writeBuffer.size());
#endif
} else {
ret = sp_blocking_write(m_portHandle, &m_writeBuffer[0], m_writeBuffer.size(), 0);
Expand Down Expand Up @@ -145,6 +159,25 @@ SerIo::Status SerIo::Read()
return Status::ReadError;
}
bytes = dwBytes;
#else
fd_set fd_serial;
struct timeval tv;

FD_ZERO(&fd_serial);
FD_SET(m_pipeHandle, &fd_serial);

tv.tv_sec = 0;
tv.tv_usec = 1000 * 1000;

int filesReadyToRead = select(m_pipeHandle + 1, &fd_serial, NULL, NULL, &tv);

if (filesReadyToRead < 1)
return Status::ReadError;

if (!FD_ISSET(m_pipeHandle, &fd_serial))
return Status::ReadError;

bytes = filesReadyToRead;
#endif
} else {
bytes = sp_input_waiting(m_portHandle);
Expand All @@ -164,6 +197,12 @@ SerIo::Status SerIo::Read()
DWORD dwRet = 0;
BOOL bRet = ReadFile(m_pipeHandle, &m_readBuffer[originalSize], static_cast<DWORD>(bytes), &dwRet, NULL);
ret = bRet;
#else
std::vector<uint8_t> temp = {};
temp.resize(m_readBuffer.capacity() + 1);

read(m_pipeHandle, &temp[0], m_readBuffer.capacity());
std::copy(temp.begin(), temp.end(), std::back_inserter(m_readBuffer));
#endif
} else {
ret = sp_nonblocking_read(m_portHandle, &m_readBuffer[originalSize], static_cast<size_t>(bytes));
Expand Down
5 changes: 5 additions & 0 deletions Includes/SerIo.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@

#ifdef _WIN32
#include <windows.h>
#else
#include <fcntl.h>
#include <unistd.h>
#endif

#include <iostream>
Expand Down Expand Up @@ -68,6 +71,8 @@ class SerIo
private:
#ifdef _WIN32
HANDLE m_pipeHandle = INVALID_HANDLE_VALUE;
#else linux
int m_pipeHandle = NULL;
#endif

bool m_isPipe = false;
Expand Down

0 comments on commit 028c8e9

Please sign in to comment.