summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaszlo Papp <lpapp@kde.org>2014-02-01 19:03:52 +0000
committerThe Qt Project <gerrit-noreply@qt-project.org>2014-02-08 19:49:45 +0100
commit7101b2f0c72e20bcce0e5ef11620208c0080c416 (patch)
treed161e7571519e85f803452f07eddfbfe47444994
parent78b8866c4d3e846f3dd142bc4bac38385298dc40 (diff)
downloadqtserialport-7101b2f0c72e20bcce0e5ef11620208c0080c416.tar.gz
Add error handling to some missing functions
Change-Id: If8dee1819d493fa4b27eba99de611c61bfdfe7a3 Reviewed-by: Denis Shienkov <denis.shienkov@gmail.com> Reviewed-by: Sergey Belyashov <Sergey.Belyashov@gmail.com>
-rw-r--r--src/serialport/qserialport_unix.cpp84
-rw-r--r--src/serialport/qserialport_unix_p.h4
-rw-r--r--src/serialport/qserialport_win.cpp53
3 files changed, 112 insertions, 29 deletions
diff --git a/src/serialport/qserialport_unix.cpp b/src/serialport/qserialport_unix.cpp
index b994a0e..0542731 100644
--- a/src/serialport/qserialport_unix.cpp
+++ b/src/serialport/qserialport_unix.cpp
@@ -227,7 +227,8 @@ bool QSerialPortPrivate::open(QIODevice::OpenMode mode)
}
#ifdef TIOCEXCL
- ::ioctl(descriptor, TIOCEXCL);
+ if (::ioctl(descriptor, TIOCEXCL) == -1)
+ q->setError(decodeSystemError());
#endif
if (::tcgetattr(descriptor, &restoredTermios) == -1) {
@@ -258,16 +259,23 @@ bool QSerialPortPrivate::open(QIODevice::OpenMode mode)
void QSerialPortPrivate::close()
{
+ Q_Q(QSerialPort);
+
if (settingsRestoredOnClose) {
- ::tcsetattr(descriptor, TCSANOW, &restoredTermios);
+ if (::tcsetattr(descriptor, TCSANOW, &restoredTermios) == -1)
+ q->setError(decodeSystemError());
+
#ifdef Q_OS_LINUX
- if (isCustomBaudRateSupported)
- ::ioctl(descriptor, TIOCSSERIAL, &restoredSerialInfo);
+ if (isCustomBaudRateSupported) {
+ if (::ioctl(descriptor, TIOCSSERIAL, &restoredSerialInfo) == -1)
+ q->setError(decodeSystemError());
+ }
#endif
}
#ifdef TIOCNXCL
- ::ioctl(descriptor, TIOCNXCL);
+ if (::ioctl(descriptor, TIOCNXCL) == -1)
+ q->setError(decodeSystemError());
#endif
if (readNotifier) {
@@ -288,7 +296,8 @@ void QSerialPortPrivate::close()
exceptionNotifier = 0;
}
- ::close(descriptor);
+ if (::close(descriptor) == -1)
+ q->setError(decodeSystemError());
if (lockFileScopedPointer->isLocked())
lockFileScopedPointer->unlock();
@@ -358,14 +367,28 @@ QSerialPort::PinoutSignals QSerialPortPrivate::pinoutSignals()
bool QSerialPortPrivate::setDataTerminalReady(bool set)
{
+ Q_Q(QSerialPort);
+
int status = TIOCM_DTR;
- return ::ioctl(descriptor, set ? TIOCMBIS : TIOCMBIC, &status) != -1;
+ if (::ioctl(descriptor, set ? TIOCMBIS : TIOCMBIC, &status) == -1) {
+ q->setError(decodeSystemError());
+ return false;
+ }
+
+ return true;
}
bool QSerialPortPrivate::setRequestToSend(bool set)
{
+ Q_Q(QSerialPort);
+
int status = TIOCM_RTS;
- return ::ioctl(descriptor, set ? TIOCMBIS : TIOCMBIC, &status) != -1;
+ if (::ioctl(descriptor, set ? TIOCMBIS : TIOCMBIC, &status) == -1) {
+ q->setError(decodeSystemError());
+ return false;
+ }
+
+ return true;
}
bool QSerialPortPrivate::flush()
@@ -380,36 +403,65 @@ bool QSerialPortPrivate::flush()
bool QSerialPortPrivate::clear(QSerialPort::Directions directions)
{
- return ::tcflush(descriptor, (directions == QSerialPort::AllDirections)
- ? TCIOFLUSH : (directions & QSerialPort::Input) ? TCIFLUSH : TCOFLUSH) != -1;
+ Q_Q(QSerialPort);
+
+ if (::tcflush(descriptor, (directions == QSerialPort::AllDirections)
+ ? TCIOFLUSH : (directions & QSerialPort::Input) ? TCIFLUSH : TCOFLUSH) == -1) {
+ q->setError(decodeSystemError());
+ return false;
+ }
+
+ return true;
}
bool QSerialPortPrivate::sendBreak(int duration)
{
- return ::tcsendbreak(descriptor, duration) != -1;
+ Q_Q(QSerialPort);
+
+ if (::tcsendbreak(descriptor, duration) == -1) {
+ q->setError(decodeSystemError());
+ return false;
+ }
+
+ return true;
}
bool QSerialPortPrivate::setBreakEnabled(bool set)
{
- return ::ioctl(descriptor, set ? TIOCSBRK : TIOCCBRK) != -1;
+ Q_Q(QSerialPort);
+
+ if (::ioctl(descriptor, set ? TIOCSBRK : TIOCCBRK) == -1) {
+ q->setError(decodeSystemError());
+ return false;
+ }
+
+ return true;
}
-qint64 QSerialPortPrivate::systemInputQueueSize () const
+qint64 QSerialPortPrivate::systemInputQueueSize ()
{
+ Q_Q(QSerialPort);
+
int nbytes = 0;
#ifdef TIOCINQ
- if (::ioctl(descriptor, TIOCINQ, &nbytes) == -1)
+ if (::ioctl(descriptor, TIOCINQ, &nbytes) == -1) {
+ q->setError(decodeSystemError());
return -1;
+ }
#endif
return nbytes;
}
-qint64 QSerialPortPrivate::systemOutputQueueSize () const
+qint64 QSerialPortPrivate::systemOutputQueueSize ()
{
+ Q_Q(QSerialPort);
+
int nbytes = 0;
#ifdef TIOCOUTQ
- if (::ioctl(descriptor, TIOCOUTQ, &nbytes) == -1)
+ if (::ioctl(descriptor, TIOCOUTQ, &nbytes) == -1) {
+ q->setError(decodeSystemError());
return -1;
+ }
#endif
return nbytes;
}
diff --git a/src/serialport/qserialport_unix_p.h b/src/serialport/qserialport_unix_p.h
index 7dbb760..c1bb685 100644
--- a/src/serialport/qserialport_unix_p.h
+++ b/src/serialport/qserialport_unix_p.h
@@ -108,8 +108,8 @@ public:
bool sendBreak(int duration);
bool setBreakEnabled(bool set);
- qint64 systemInputQueueSize () const;
- qint64 systemOutputQueueSize () const;
+ qint64 systemInputQueueSize ();
+ qint64 systemOutputQueueSize ();
void startWriting();
diff --git a/src/serialport/qserialport_win.cpp b/src/serialport/qserialport_win.cpp
index 0a9aec5..fef47a0 100644
--- a/src/serialport/qserialport_win.cpp
+++ b/src/serialport/qserialport_win.cpp
@@ -266,6 +266,7 @@ QSerialPort::PinoutSignals QSerialPortPrivate::pinoutSignals()
if (!::DeviceIoControl(descriptor, IOCTL_SERIAL_GET_DTRRTS, NULL, 0,
&modemStat, sizeof(modemStat),
&bytesReturned, NULL)) {
+ q->setError(decodeSystemError());
return ret;
}
@@ -279,12 +280,26 @@ QSerialPort::PinoutSignals QSerialPortPrivate::pinoutSignals()
bool QSerialPortPrivate::setDataTerminalReady(bool set)
{
- return ::EscapeCommFunction(descriptor, set ? SETDTR : CLRDTR);
+ Q_Q(QSerialPort);
+
+ if (!::EscapeCommFunction(descriptor, set ? SETDTR : CLRDTR)) {
+ q->setError(decodeSystemError());
+ return false;
+ }
+
+ return true;
}
bool QSerialPortPrivate::setRequestToSend(bool set)
{
- return ::EscapeCommFunction(descriptor, set ? SETRTS : CLRRTS);
+ Q_Q(QSerialPort);
+
+ if (!::EscapeCommFunction(descriptor, set ? SETRTS : CLRRTS)) {
+ q->setError(decodeSystemError());
+ return false;
+ }
+
+ return true;
}
#ifndef Q_OS_WINCE
@@ -296,6 +311,8 @@ bool QSerialPortPrivate::flush()
bool QSerialPortPrivate::clear(QSerialPort::Directions directions)
{
+ Q_Q(QSerialPort);
+
DWORD flags = 0;
if (directions & QSerialPort::Input)
flags |= PURGE_RXABORT | PURGE_RXCLEAR;
@@ -303,25 +320,39 @@ bool QSerialPortPrivate::clear(QSerialPort::Directions directions)
flags |= PURGE_TXABORT | PURGE_TXCLEAR;
writeSequenceStarted = false;
}
- return ::PurgeComm(descriptor, flags);
+ if (!::PurgeComm(descriptor, flags)) {
+ q->setError(decodeSystemError());
+ return false;
+ }
+
+ return true;
}
#endif
bool QSerialPortPrivate::sendBreak(int duration)
{
- // FIXME:
- if (setBreakEnabled(true)) {
- ::Sleep(duration);
- if (setBreakEnabled(false))
- return true;
- }
- return false;
+ if (!setBreakEnabled(true))
+ return false;
+
+ ::Sleep(duration);
+
+ if (!setBreakEnabled(false))
+ return false;
+
+ return true;
}
bool QSerialPortPrivate::setBreakEnabled(bool set)
{
- return (set ? ::SetCommBreak(descriptor) : ::ClearCommBreak(descriptor));
+ Q_Q(QSerialPort);
+
+ if (set ? !::SetCommBreak(descriptor) : !::ClearCommBreak(descriptor)) {
+ q->setError(decodeSystemError());
+ return false;
+ }
+
+ return true;
}
qint64 QSerialPortPrivate::systemInputQueueSize ()