Improve the I/O completion errors handling on Windows

* It is necessary to ignore the NoError error code.

* In case of error from GetOverlappedResult() it is necessary to stop
the I/O operation by a immediate return from the function, because I/O
processing does not make sense. This is consistent behavior similar to
starting I/O operations.

* All errors from the read completion should be interpreted as ReadError.
Exception is only the critical ResourceError error which it is necessary
to leave without changes. This is also consistent behavior similar to
starting I/O operations.

* The write completion error handling should be similar to the read
completion handling.

* For the communication completion we have no specific error codes.
Therefore error handling shall be without modification of their error
code.

Also were made small cosmetic fixes to the related start I/O methods.

Tested on Windows 7/8 with the on-board, the PL2303 and the virtual
com0com serial ports using Qt4 and then Qt5. Testing are made by means
of examples of library and also by means of auto tests, no regressions
found.

Change-Id: I0d08b53627431c42ab5147d4330f1aaf819b4d63
Reviewed-by: Peter Kümmel <syntheticpp@gmx.net>
Reviewed-by: Sergey Belyashov <Sergey.Belyashov@gmail.com>
Reviewed-by: Denis Shienkov <denis.shienkov@gmail.com>
Improve the ResourceError handling on Windows

To the ResourceError handling we generally relied on triggering of the
_q_completeAsyncCommunication(), in case of ejection of an USB device
from a host. But it worked with not all USB devices (for example it did
not work with the ZTE modems). Even when this worked (for example with
the PL2303 devices), then we got the non informative "Unknown error" of
the ResourceError string representation.

It is more reasonable not to do any custom checks of an event mask when
triggered of the _q_completeAsyncCommunication() method. We need allow
to detect an errors with means of the
ReadFile/GetOverlappedResult/WaitCommEvent syscalls on an invalid device
handle.

An important note is that returns the different error codes for a
different types of devices.

For example:

* For the USB ZTE and the Samsung Android USB modem returns the
ERROR_ACCESS_DENIED error code.

* For the FTDI and the Samsung Android Diagnostic Serial Port returns
the ERROR_OPERATION_ABORTED error code.

* For the USB PL2303 device returns the ERROR_BAD_COMMAND error code.

Thus, there is no universal solution of this issue. Each vendor of the
driver implements it own way. Therefore for each new type of the device
it is necessary to add processing its error code, if we have no it yet.

Tested on Windows 7/8 with the USB ZTE and the Android modems, with the
USB PL2303 and the FTDI devices and with the Android diagnostic serial
port using Qt4 and then Qt5, no regressions found.

Task-number: QTBUG-32020
Change-Id: I61c02078ee98221ecef18a27cde3d9bae3674d70
Reviewed-by: Denis Shienkov <denis.shienkov@gmail.com>
Improve the serial number handling on Windows

The standard USB CDC driver (usbser.sys) does not add information of
serial number to the device instance identifier of the child devices
(serial ports), so our algorithm returns an empty string.

But information about a serial number is stored in the device identifier
of the parental node. Thus, it is necessary to get this parent
identifier, using these recommendations from MSDN:

http://msdn.microsoft.com/en-us/library/windows/hardware/ff549417%28v=vs.85%29.aspx

For this purpose is used the set of new functions from the
"Configuration Manager Functions" family.

Therefore it was necessary:

* Rewrite the deviceInstanceIdentifier() function.

Now this function accepts one DEVINST parameter and returns the
corresponding device instance identifier string.

* Add the new parentDeviceInstanceNumber() function.

This function accepts a DEVINST of the child device and returns a
DEVINST of the parent device.

Thus, the new algorithm tries to extract a serial number from the
current (child) device and in case it is empty, try to extract from
the parent.

Tested on Windows 7/8 with the Segger JLink device (provides the CDC
serial port) using Qt4 (MSVC2010) and then Qt5 (MSVC2012, MinGW).

Also are checked other devices for possible regressions:

* on-board serial port
* com0com virtual serial port
* USB FTDI serial port

no regressions found.

Change-Id: Ic6afc258f45aa374ec77dc0b586d479b5286359f
Reviewed-by: Denis Shienkov <denis.shienkov@gmail.com>
Abort waitForReadyRead() when there is an error

When in readNotification() or completeAsyncWrite() an error
is emitted and the loop is not exited, this error signal
will flood the eventloop.

Change-Id: I77b8a4c337041258862b19a07917bef059c11cc1
Reviewed-by: Sergey Belyashov <Sergey.Belyashov@gmail.com>
Reviewed-by: Denis Shienkov <denis.shienkov@gmail.com>
Unify handling of errors of the overlapped I/O result

Each q_completeXXX() slot does call of GetOverlappedResult()
and differently interprets an error depending on the type of
I/O operation (read, write or communication).

It is more reasonable to make it in the separate method
handleOverlappedResult() which returns the number of the
transferred bytes or -1 in case of error.

Besides, this method accepts the additional integer parameter
which expresses type of overlapped operation using the
existing QSerialPort::Direction enumeration. The type of the
communication operation has not separate value, so is used
zero value for it.

Tested build on Windows 7/8 using Qt4 and then Qt5.

Change-Id: I8482392d06279bc430656e50b1e4392c513885c6
Reviewed-by: Sergey Belyashov <Sergey.Belyashov@gmail.com>
Reviewed-by: Peter Kümmel <syntheticpp@gmx.net>
Reviewed-by: Denis Shienkov <denis.shienkov@gmail.com>
Improve error handling for the waitForXX() methods on Windows

The slots _q_completeXX() returns empty values, therefore the
waitForXX() methods continue to work even if occurs an error
from the I/O completion.

Is reasonable allow to return of boolean values for the I/O
completion slots.

Besides, it allows avoiding indirect detection of an error,
comparing of the sizes of I/O buffers before and after the
operation completion. Though, for the waitForReadyRead(),
for this purpose it is necessary to add additional
modifications in a following patches.

Tested on Windows 7/8 with the virtual com0com ports using
Qt4 and then Qt5. Testing was made using of autotests
and examples.

Change-Id: I95a76461af4595f6658f0cad766a4fff14eb7afc
Reviewed-by: Peter Kümmel <syntheticpp@gmx.net>
Reviewed-by: Denis Shienkov <denis.shienkov@gmail.com>
Make independent implementation of QSerialPortInfo for OS X

Building in OS X uses some shared code from the serialportinfo_unix.cpp
module with the OS X specific code from the serialportinfo_mac.cpp module.

Thus, all code of the serialportinfo_unix.cpp module, which not related
with the OS X, is shielded by a macro. It adds an excessive garbage for
readability in this module and also some confusion in the *.pri file.

It makes sense to make implementation of the serialportinfo_mac.cpp
module completely independent, that will simplify maintaining of source
code.

Besides are added tests which can reveal declared but not implemented
methods in building, and also to check a correctness of default values
at running.

Tested build on OS X 10.8.4 with Qt4, an then on Android with Qt5.

Change-Id: I67935b64e2b623fb8d4c14d59e1b87f1eac71c3e
Reviewed-by: Bernard Pratz <guyzmo+qt@m0g.net>
Reviewed-by: Peter Kümmel <syntheticpp@gmx.net>
Reviewed-by: Denis Shienkov <denis.shienkov@gmail.com>
Refactor of the QSerialPortInfo for OS X

The current implementation of the qserialportinfo_mac.cpp module is a
little confused and complicated.

It is reasonable to re-write it using some ready wrappers of CFTypeRef,
CFStringRef and others objects from the private qcore_mac_p.h file.

Thus, it will allow to bypass of manual releasing of the allocated
resources (using the RAII idiom where it is possible), and also to
simplify a code.

Tested on OS X 10.8.4 with the on-board, USB PL2303, USB FTDI,
USB Android serial ports and with the USB ZTE Modem using Qt4.

Change-Id: Ibbac03a17fdd41bbaf530f863e7c690a15708bd2
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Denis Shienkov <denis.shienkov@gmail.com>
Do not continue waitForReadyRead() if an error occurred

This patch improves workaround implemented in the previous commit
d34fce4a5d12789ded107631e22cb6ef54d35eee

Change-Id: I013b19b47ee343ab00f242d7ee1021b92c07d18f
Reviewed-by: Peter Kümmel <syntheticpp@gmx.net>
Reviewed-by: Sergey Belyashov <Sergey.Belyashov@gmail.com>
Reviewed-by: Denis Shienkov <denis.shienkov@gmail.com>
Wrap of getting the port properties on Windows

It is reasonable to wrap implementation of getting properties of port
from availablePorts() to the separate functions. It is simplifies a
code and improves its understanding.

Tested on Windows 7/8 with the on-board, virtual com0com, USB FTDI
serial ports using Qt4 and then Qt5.

Change-Id: I2345fa49b3633960412715b299e068daa8fdcfd0
Reviewed-by: Sergey Belyashov <Sergey.Belyashov@gmail.com>
Reviewed-by: Denis Shienkov <denis.shienkov@gmail.com>
Merge remote-tracking branch 'origin/5.3' into dev

Change-Id: I64b13e135d58980ea4cf7429224aa1877778cb46