Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] add fail safes if handle doesn't exist #344

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions utils/serial/src/serial/unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

uint32_t DPSerial::getAvailableByteCount(FILEHANDLE s_handle)
{
if (!s_handle)
return 0;

uint32_t available = 0;
if (ioctl(fileno(s_handle), FIONREAD, &available) < 0)
{
Expand All @@ -15,11 +18,15 @@ uint32_t DPSerial::getAvailableByteCount(FILEHANDLE s_handle)

void DPSerial::tearDown()
{
fclose(s_handle);
if (s_handle)
fclose(s_handle);
}

bool DPSerial::readBytesFromSerial(void* target, uint32_t length)
bool DPSerial::readBytesFromSerial(void *target, uint32_t length)
{
if (!s_handle)
return false;

const uint32_t result = fread(target, 1, length, s_handle);
const bool valid = result == length;
if (!valid)
Expand All @@ -40,9 +47,10 @@ bool DPSerial::readBytesFromSerial(void* target, uint32_t length)
return valid;
}

void DPSerial::write(const uint8_t* const data, const uint32_t length)
void DPSerial::write(const uint8_t *const data, const uint32_t length)
{
::write(fileno(s_handle), data, length);
if (s_handle)
::write(fileno(s_handle), data, length);
}

bool DPSerial::setup(std::string path)
Expand Down
19 changes: 14 additions & 5 deletions utils/serial/src/serial/win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

uint32_t DPSerial::getAvailableByteCount(FILEHANDLE s_handle)
{
if (!s_handle)
return 0;
DWORD commerr;
COMSTAT comstat;
if (!ClearCommError(s_handle, &commerr, &comstat))
Expand All @@ -13,20 +15,27 @@ uint32_t DPSerial::getAvailableByteCount(FILEHANDLE s_handle)

void DPSerial::tearDown()
{
CloseHandle(s_handle);
if (s_handle)
CloseHandle(s_handle);
}

bool DPSerial::readBytesFromSerial(void* target, uint32_t length)
bool DPSerial::readBytesFromSerial(void *target, uint32_t length)
{
if (!s_handle)
return false;

DWORD bytesRead;
ReadFile(s_handle, target, length, &bytesRead, NULL);
return bytesRead == length;
}

void DPSerial::write(const uint8_t* const data, const uint32_t length)
void DPSerial::write(const uint8_t *const data, const uint32_t length)
{
DWORD bytesWritten = 0;
WriteFile(s_handle, data, length, &bytesWritten, NULL);
if (s_handle)
{
DWORD bytesWritten = 0;
WriteFile(s_handle, data, length, &bytesWritten, NULL);
}
}

bool DPSerial::setup(std::string path)
Expand Down