Skip to content

Commit

Permalink
Merge pull request #1 from Guru-98/macos
Browse files Browse the repository at this point in the history
Add MacOS support
  • Loading branch information
rnayabed authored Aug 1, 2023
2 parents 6b9fa07 + 950bb88 commit 4cc788c
Show file tree
Hide file tree
Showing 4 changed files with 234 additions and 5 deletions.
12 changes: 9 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,15 @@ target_compile_options(vegadude PRIVATE
$<$<CONFIG:RelWithDebInfo>:-O3 -ggdb3>
)

target_link_options(vegadude PRIVATE
$<$<OR:$<CONFIG:Release>,$<CONFIG:MinSizeRel>>:-ffunction-sections -fdata-sections -Wl,--gc-sections -s>
)
if(APPLE)
target_link_options(vegadude PRIVATE
$<$<OR:$<CONFIG:Release>,$<CONFIG:MinSizeRel>>:-ffunction-sections -fdata-sections -Wl>
)
else()
target_link_options(vegadude PRIVATE
$<$<OR:$<CONFIG:Release>,$<CONFIG:MinSizeRel>>:-ffunction-sections -fdata-sections -Wl,--gc-sections -s>
)
endif()

install(TARGETS vegadude
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
Expand Down
4 changes: 2 additions & 2 deletions logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ template Logger& operator<<(Logger&, double);
template Logger& operator<<(Logger&, char);
template Logger& operator<<(Logger&, char*);
template Logger& operator<<(Logger&, char const*);
template Logger& operator<<(Logger&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>);
template Logger& operator<<(Logger&, std::basic_string<char, std::char_traits<char>, std::allocator<char>>);
template Logger& operator<<(Logger&, std::basic_string_view<char, std::char_traits<char>>);
template Logger& operator<<(Logger&, std::filesystem::__cxx11::path);
template Logger& operator<<(Logger&, std::filesystem::path);
template Logger& operator<<(Logger&, unsigned short);
216 changes: 216 additions & 0 deletions serialdevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,4 +373,220 @@ bool SerialDevice::write(std::span<const unsigned char> bytes)
}
}

#elif __APPLE__

bool SerialDevice::open()
{
m_macFD = ::open(m_devicePath.c_str(),
(O_RDWR | O_NOCTTY));

if (m_macFD < 0)
{
m_error = Error::FAILED_TO_OPEN_DEVICE;
return false;
}

struct termios tty;

if (ioctl(m_macFD, TIOCGETA, &tty) < 0)
{
m_error = Error::FAILED_TO_GET_FD_ATTRS;
return false;
}

// Parity
if (m_deviceProperties.parity)
tty.c_cflag |= PARENB;
else
tty.c_cflag &= ~PARENB;


// Stop Bit
if (m_deviceProperties.stopBits == 1)
tty.c_cflag &= ~CSTOPB;
else
tty.c_cflag |= CSTOPB;


//Bits per byte
tty.c_cflag &= ~CSIZE;
if (m_deviceProperties.bits == 5)
tty.c_cflag |= CS5;
else if (m_deviceProperties.bits == 6)
tty.c_cflag |= CS6;
else if (m_deviceProperties.bits == 7)
tty.c_cflag |= CS7;
else if (m_deviceProperties.bits == 8)
tty.c_cflag |= CS8;


// RTS/CTS
if (m_deviceProperties.rtsCts)
tty.c_cflag |= CRTSCTS;
else
tty.c_cflag &= ~CRTSCTS;


tty.c_cflag |= CREAD | CLOCAL;

// CREAD = allows us to read data
// CLOCAL = disables "carrier detect"
// disables SIGHUP signal to be sent when disconnected


// Local modes flags

// Disable Canonical mode
// Prevents presence of backspace to erase previous byte
// Prevents taking newline as signal to process input
tty.c_lflag &= ~ICANON;

// Disabling canon mode is equivalent to disabling 3
// below. but need to check
tty.c_lflag &= ~ECHO; // Disable echo
tty.c_lflag &= ~ECHOE; // Disable erasure
tty.c_lflag &= ~ECHONL; // Disable new-line echo


tty.c_lflag &= ~ISIG; // Disable interpretation of INTR, QUIT and SUSP

// Input modes flags

tty.c_iflag &= ~(IXON | IXOFF | IXANY); // Turn off s/w flow ctrl

tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL); // Disable any special handling of received bytes

// Output modes flags

tty.c_oflag &= ~OPOST; // Prevent special interpretation of output bytes (e.g. newline chars)
tty.c_oflag &= ~ONLCR; // Prevent conversion of newline to carriage return/line feed


// Line discpline not covered

// Control characters (cc) (VMIN, VTIME)
tty.c_cc[VMIN] = 0;
tty.c_cc[VTIME] = m_readTimeout / 100; // milli -> deci

// For some reason cfsetspeed does not work well with integers
speed_t speed;
switch(m_deviceProperties.baudRate)
{
case 50:
speed = B50; break;
case 75:
speed = B75; break;
case 110:
speed = B110; break;
case 134:
speed = B134; break;
case 150:
speed = B150; break;
case 200:
speed = B200; break;
case 300:
speed = B300; break;
case 600:
speed = B600; break;
case 1200:
speed = B1200; break;
case 1800:
speed = B1800; break;
case 2400:
speed = B2400; break;
case 4800:
speed = B4800; break;
case 9600:
speed = B9600; break;
case 19200:
speed = B19200; break;
case 38400:
speed = B38400; break;
case 57600:
speed = B57600; break;
case 115200:
speed = B115200; break;
case 230400:
speed = B230400; break;
default:
m_error = Error::INVALID_BAUD_RATE;
return false;
}

cfsetspeed(&tty, speed);

if (!ioctl(m_macFD, TIOCSETA, &tty))
{
m_error = Error::NONE;
return true;
}
else
{
m_error = Error::FAILED_TO_SET_FD_ATTRS;
return false;
}
}

bool SerialDevice::close()
{
ioctl(m_macFD, TIOCFLUSH, 2);

if (!::close(m_macFD))
{
m_error = Error::NONE;
return true;
}
else
{
m_error = Error::FAILED_TO_SET_FD_ATTRS;
return false;
}
}

bool SerialDevice::read(std::span<unsigned char> bytes)
{
if (m_macFD == -1)
{
m_error = Error::DEVICE_NOT_OPEN;
return false;
}
else
{
if (::read(m_macFD, bytes.data(), bytes.size()) != -1)
{
m_error = Error::NONE;
return true;
}
else
{
m_error = Error::READ_FAILED;
return false;
}
}
}

bool SerialDevice::write(std::span<const unsigned char> bytes)
{
if (m_macFD == -1)
{
m_error = Error::DEVICE_NOT_OPEN;
return false;
}
else
{
if (::write(m_macFD, bytes.data(), bytes.size()) == static_cast<ssize_t>(bytes.size()))
{
m_error = Error::NONE;
return true;
}
else
{
m_error = Error::WRITE_FAILED;
return false;
}
}
m_error = Error::NOT_SUPPORTED;
return false;
}

#endif
7 changes: 7 additions & 0 deletions serialdevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
#include <Windows.h>
#include <stdio.h>
#include <string.h>
#elif __APPLE__
#include <fcntl.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <unistd.h>
#endif

class SerialDevice : public Device
Expand Down Expand Up @@ -89,6 +94,8 @@ class SerialDevice : public Device
int32_t m_linuxFD = -1;
#elif __WIN32
HANDLE m_winHandle = NULL;
#elif __APPLE__
int32_t m_macFD = -1;
#endif

bool openLinux();
Expand Down

0 comments on commit 4cc788c

Please sign in to comment.