Skip to content

Commit

Permalink
now using int64_t instead of ssize_t (except for internal use)
Browse files Browse the repository at this point in the history
  • Loading branch information
noah1510 committed Dec 14, 2023
1 parent bc313b3 commit eb62c70
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 37 deletions.
1 change: 0 additions & 1 deletion include/rs232_baudrate_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
#ifndef B256000
#define B256000 256000
#endif

#endif

namespace sakurajin {
Expand Down
19 changes: 9 additions & 10 deletions include/rs232_native.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,6 @@ namespace sakurajin {
#endif
};

#if defined(_MSC_VER)
#include <BaseTsd.h>
typedef SSIZE_T ssize_t;
#endif

RS232_EXPORT_MACRO std::vector<std::string> getAvailablePorts() noexcept;
RS232_EXPORT_MACRO std::vector<std::string> getMatchingPorts(const std::regex& pattern) noexcept;

Expand Down Expand Up @@ -123,6 +118,9 @@ namespace sakurajin {
*/
std::shared_mutex dataAccessMutex;

template <typename retVal>
using encapsulatedFunction = std::function<retVal()>;

/**
* @brief A method to make a function call with a lock on the mutex
* This method is used to prevent code duplication.
Expand All @@ -135,7 +133,8 @@ namespace sakurajin {
* @param func The function that should be called
* @return ssize_t The return value of the function or -1 if something went wrong
*/
ssize_t callWithOptionalLock(bool block, const std::function<ssize_t()>& func) {
template <typename retT>
int64_t callWithOptionalLock(const encapsulatedFunction<retT>& func, bool block = true) {
// lock the mutex if the lock parameter is true
// otherwise exit if the lock cannot be acquired
if (block) {
Expand All @@ -147,7 +146,7 @@ namespace sakurajin {
// call the function and unlock the mutex before passing the return value.
auto retVal = func();
dataAccessMutex.unlock();
return retVal;
return static_cast<int64_t>(retVal);
}

public:
Expand Down Expand Up @@ -187,7 +186,7 @@ namespace sakurajin {
* @return int the number of bytes that were read from the port
*/
[[nodiscard]]
ssize_t readRawData(char* data_location, int length, bool block = true) noexcept;
int64_t readRawData(char* data_location, int length, bool block = true) noexcept;

/**
* @brief The platform specific function to write a string to the port
Expand All @@ -197,7 +196,7 @@ namespace sakurajin {
* @return int the number of bytes that were written to the port
*/
[[nodiscard]]
ssize_t writeRawData(char* data_location, int length, bool block = true) noexcept;
int64_t writeRawData(char* data_location, int length, bool block = true) noexcept;

/**
* @brief Checks if the connection was started successfully
Expand Down Expand Up @@ -230,7 +229,7 @@ namespace sakurajin {
* @return int the flags that are set
*/
[[nodiscard]]
ssize_t retrieveFlags(bool block = true) noexcept;
int64_t retrieveFlags(bool block = true) noexcept;
};

} // namespace sakurajin
Expand Down
2 changes: 1 addition & 1 deletion src/rs232_native_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ bool sakurajin::RS232_native::checkForFlag(sakurajin::portStatusFlags flag, bool
if (flags < 0) {
return false;
}
return (flags & static_cast<ssize_t>(flag)) != 0;
return (flags & static_cast<int64_t>(flag)) != 0;
}

std::vector<std::string> sakurajin::getAvailablePorts() noexcept {
Expand Down
27 changes: 15 additions & 12 deletions src/rs232_native_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ sakurajin::connectionStatus sakurajin::RS232_native::connect(Baudrate baudrate,
return connStatus;
}

ssize_t sakurajin::RS232_native::readRawData(char* data_location, int length, bool lock) noexcept {
int64_t sakurajin::RS232_native::readRawData(char* data_location, int length, bool block) noexcept {
if (connStatus != connectionStatus::connected) {
return -1;
}
Expand All @@ -108,15 +108,16 @@ ssize_t sakurajin::RS232_native::readRawData(char* data_location, int length, bo

length = std::clamp(length, 0, limit);

return callWithOptionalLock(lock, [this, data_location, length]() { return read(getPort(portHandle), data_location, length); });
return callWithOptionalLock<ssize_t>([this, data_location, length]() { return read(getPort(portHandle), data_location, length); },
block);
}

ssize_t sakurajin::RS232_native::writeRawData(char* data_location, int length, bool lock) noexcept {
int64_t sakurajin::RS232_native::writeRawData(char* data_location, int length, bool block) noexcept {
if (connStatus != connectionStatus::connected) {
return -1;
}

return callWithOptionalLock(lock, [this, data_location, length]() { return write(getPort(portHandle), data_location, length); });
return callWithOptionalLock<ssize_t>([this, data_location, length]() { return write(getPort(portHandle), data_location, length); }, block);
}

void sakurajin::RS232_native::disconnect() noexcept {
Expand All @@ -140,16 +141,18 @@ void sakurajin::RS232_native::disconnect() noexcept {
portConfig = nullptr;
}

ssize_t sakurajin::RS232_native::retrieveFlags(bool block) noexcept {
int64_t sakurajin::RS232_native::retrieveFlags(bool block) noexcept {
if (connStatus != connectionStatus::connected) {
return -1;
}

return callWithOptionalLock(block, [this]() {
ssize_t status;
if (ioctl(getPort(portHandle), TIOCMGET, &status) < 0) {
return (ssize_t)(-1);
}
return status;
});
return callWithOptionalLock<ssize_t>(
[this]() {
int64_t status;
if (ioctl(getPort(portHandle), TIOCMGET, &status) < 0) {
return (int64_t)(-1);
}
return status;
},
block);
}
26 changes: 13 additions & 13 deletions src/rs232_native_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,44 +125,44 @@ void sakurajin::RS232_native::disconnect() noexcept {
connStatus = connectionStatus::disconnected;
}

ssize_t sakurajin::RS232_native::readRawData(char* data_location, int length, bool block) noexcept {
int64_t sakurajin::RS232_native::readRawData(char* data_location, int length, bool block) noexcept {
if (connStatus != connectionStatus::connected) {
return -1;
}

return callWithOptionalLock(block, [this, data_location, length]() {
return callWithOptionalLock<int64_t>([this, data_location, length]() {
int n = 0;
auto local_len = std::clamp(length, 0, 4096);

auto success = ReadFile(getCport(portHandle), data_location, local_len, (LPDWORD)((void*)&n), NULL);
return (ssize_t)(success ? n : -1);
});
return (int64_t)(success ? n : -1);
}, block);
}

ssize_t sakurajin::RS232_native::writeRawData(char* data_location, int length, bool block) noexcept {
int64_t sakurajin::RS232_native::writeRawData(char* data_location, int length, bool block) noexcept {
if (connStatus != connectionStatus::connected) {
return -1;
}

return callWithOptionalLock(block, [this, data_location, length]() {
return callWithOptionalLock<int64_t>([this, data_location, length]() {
int n = 0;
auto local_len = std::clamp(length, 0, 4096);

auto success = WriteFile(getCport(portHandle), data_location, local_len, (LPDWORD)((void*)&n), NULL);
return (ssize_t)(success ? n : -1);
});
return (int64_t)(success ? n : -1);
}, block);
}

ssize_t sakurajin::RS232_native::retrieveFlags(bool block) noexcept {
int64_t sakurajin::RS232_native::retrieveFlags(bool block) noexcept {
if (connStatus != connectionStatus::connected) {
return -1;
}

return callWithOptionalLock(block, [this](){
return callWithOptionalLock<int64_t>([this](){
DWORD flags;
if (!GetCommModemStatus(getCport(portHandle), &flags)) {
return (ssize_t)(-1);
return (int64_t)(-1);
}
return (ssize_t)flags;
});
return (int64_t)flags;
}, block);
}

0 comments on commit eb62c70

Please sign in to comment.