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

Implement ClearCommError #79

Merged
merged 4 commits into from
May 1, 2016
Merged
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
1 change: 1 addition & 0 deletions docs/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Notable enhancements and changes are:
* :issue:`75` - :func:`pywincffi.kernel32.events.ResetEvent`
* :issue:`76` - :func:`pywincffi.kernel32.process.TerminateProcess`
* :issue:`78` - :func:`pywincffi.kernel32.handle.DuplicateHandle`
* :issue:`79` - :func:`pywincffi.kernel32.process.ClearCommError`

0.2.0
~~~~~
Expand Down
13 changes: 13 additions & 0 deletions pywincffi/core/cdefs/headers/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,19 @@
#define STARTF_USESIZE ...
#define STARTF_USESTDHANDLES ...

// ClearCommError
#define CE_BREAK ...
#define CE_FRAME ...
#define CE_OVERRUN ...
#define CE_RXOVER ...
#define CE_RXPARITY ...
#define CE_DNS ...
#define CE_IOE ...
#define CE_MODE ...
#define CE_OOP ...
#define CE_PTO ...
#define CE_TXFULL ...

// For the moment, we can't define this here. When cffi
// parses the header this returns -1 and cffi seems to
// only handle positive integers right now.
Expand Down
3 changes: 3 additions & 0 deletions pywincffi/core/cdefs/headers/functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,6 @@ BOOL DuplicateHandle(HANDLE, HANDLE, HANDLE, LPHANDLE, DWORD, BOOL, DWORD);
HANDLE CreateEvent(LPSECURITY_ATTRIBUTES, BOOL, BOOL, LPCTSTR);
HANDLE OpenEvent(DWORD, BOOL, LPCTSTR);
BOOL ResetEvent(HANDLE);

// Communications
BOOL ClearCommError(HANDLE, LPDWORD, LPCOMSTAT);
14 changes: 14 additions & 0 deletions pywincffi/core/cdefs/headers/structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,17 @@ typedef struct _STARTUPINFO {
HANDLE hStdOutput;
HANDLE hStdError;
} STARTUPINFO, *LPSTARTUPINFO;

// https://msdn.microsoft.com/en-us/library/aa363200
typedef struct _COMSTAT {
DWORD fCtsHold :1;
DWORD fDsrHold :1;
DWORD fRlsdHold :1;
DWORD fXoffHold :1;
DWORD fXoffSent :1;
DWORD fEof :1;
DWORD fTxim :1;
DWORD fReserved :25;
DWORD cbInQue;
DWORD cbOutQue;
} COMSTAT, *LPCOMSTAT;
1 change: 1 addition & 0 deletions pywincffi/kernel32/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@
GetProcessId, GetCurrentProcess, OpenProcess, GetExitCodeProcess,
pid_exists, TerminateProcess)
from pywincffi.kernel32.events import CreateEvent, OpenEvent, ResetEvent
from pywincffi.kernel32.comms import ClearCommError
42 changes: 42 additions & 0 deletions pywincffi/kernel32/comms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""
Communications
--------------

A module containing Windows functions related to communications.
"""

from pywincffi.core import dist
from pywincffi.core.checks import Enums, input_check, error_check


def ClearCommError(hFile):
"""
REtrieves information about a communications error and reports the
current status of a communications device.

.. seealso::

https://msdn.microsoft.com/en-us/aa363180

:param handle hFile:
A handle to the communications device, typically created by
:func:`CreateFile`

:rtype: tuple
:return:
Returns a two element tuple containing the ``lpErrors`` and
``lpStat`` result objects.

* ``lpErrors`` - Contains the mast indicating the type of error
* ``lpStat`` - A ``COMSTAT`` structure which contains the device's
information.
"""
input_check("hFile", hFile, Enums.HANDLE)

ffi, library = dist.load()

lpErrors = ffi.new("LPDWORD")
lpStat = ffi.new("LPCOMSTAT")
code = library.ClearCommError(hFile, lpErrors, lpStat)
error_check("ClearCommError", code=code, expected=Enums.NON_ZERO)
return lpErrors, lpStat
33 changes: 33 additions & 0 deletions tests/test_kernel32/test_comms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from pywincffi.core import dist
from pywincffi.dev.testutil import TestCase
from pywincffi.kernel32 import CreateFile, ClearCommError, CloseHandle
from pywincffi.exceptions import WindowsAPIError


class TestClearCommError(TestCase):
def test_clear_error(self):
ffi, library = dist.load()

# Try to find a COM device, skip the test if we can't
# find one. The 'maximum' comes from here:
# https://support.microsoft.com/en-us/kb/100111
found_com_device = False
for i in range(256):
try:
handle = CreateFile(
"\\\\.\\COM%d" % i,
library.GENERIC_READ | library.GENERIC_WRITE,
dwCreationDisposition=library.OPEN_EXISTING,
dwShareMode=0
)
found_com_device = True
self.addCleanup(CloseHandle, handle)
ClearCommError(handle)
break
except WindowsAPIError:
code, _ = ffi.getwinerror()
if code != library.ERROR_FILE_NOT_FOUND:
self.fail("Unexpected Windows API error: %s" % code)

if not found_com_device:
self.skipTest("No COM devices present.")