blob: 88a1335199bf23a82dd90ad08b0e6430d5da116d [file] [log] [blame]
Chris Liechtifbdd8a02015-08-09 02:37:45 +02001#! python
2#
3# (C) 2001-2015 Chris Liechti <cliechti@gmx.net>
4#
5# SPDX-License-Identifier: BSD-3-Clause
6
cliechti183d4ae2009-07-23 22:03:51 +00007from ctypes import *
8from ctypes.wintypes import HANDLE
9from ctypes.wintypes import BOOL
10from ctypes.wintypes import LPCWSTR
11_stdcall_libraries = {}
12_stdcall_libraries['kernel32'] = WinDLL('kernel32')
13from ctypes.wintypes import DWORD
14from ctypes.wintypes import WORD
15from ctypes.wintypes import BYTE
16
cliechtie37b6a82009-07-24 12:19:50 +000017INVALID_HANDLE_VALUE = HANDLE(-1).value
cliechti183d4ae2009-07-23 22:03:51 +000018
cliechti251e5ce2011-08-25 01:24:49 +000019# some details of the windows API differ between 32 and 64 bit systems..
20def is_64bit():
21 """Returns true when running on a 64 bit system"""
22 return sizeof(c_ulong) != sizeof(c_void_p)
23
cliechtide432902011-08-18 22:51:41 +000024# ULONG_PTR is a an ordinary number, not a pointer and contrary to the name it
25# is either 32 or 64 bits, depending on the type of windows...
26# so test if this a 32 bit windows...
cliechti251e5ce2011-08-25 01:24:49 +000027if is_64bit():
cliechtide432902011-08-18 22:51:41 +000028 # assume 64 bits
29 ULONG_PTR = c_int64
cliechti251e5ce2011-08-25 01:24:49 +000030else:
31 # 32 bits
32 ULONG_PTR = c_ulong
cliechtide432902011-08-18 22:51:41 +000033
34
cliechti183d4ae2009-07-23 22:03:51 +000035class _SECURITY_ATTRIBUTES(Structure):
36 pass
37LPSECURITY_ATTRIBUTES = POINTER(_SECURITY_ATTRIBUTES)
38
cliechtie37b6a82009-07-24 12:19:50 +000039
cliechti8a345132011-08-05 02:33:14 +000040try:
41 CreateEventW = _stdcall_libraries['kernel32'].CreateEventW
42except AttributeError:
43 # Fallback to non wide char version for old OS...
44 from ctypes.wintypes import LPCSTR
45 CreateEventA = _stdcall_libraries['kernel32'].CreateEventA
46 CreateEventA.restype = HANDLE
47 CreateEventA.argtypes = [LPSECURITY_ATTRIBUTES, BOOL, BOOL, LPCSTR]
48 CreateEvent=CreateEventA
49
50 CreateFileA = _stdcall_libraries['kernel32'].CreateFileA
51 CreateFileA.restype = HANDLE
52 CreateFileA.argtypes = [LPCSTR, DWORD, DWORD, LPSECURITY_ATTRIBUTES, DWORD, DWORD, HANDLE]
53 CreateFile = CreateFileA
54else:
55 CreateEventW.restype = HANDLE
56 CreateEventW.argtypes = [LPSECURITY_ATTRIBUTES, BOOL, BOOL, LPCWSTR]
57 CreateEvent = CreateEventW # alias
58
59 CreateFileW = _stdcall_libraries['kernel32'].CreateFileW
60 CreateFileW.restype = HANDLE
61 CreateFileW.argtypes = [LPCWSTR, DWORD, DWORD, LPSECURITY_ATTRIBUTES, DWORD, DWORD, HANDLE]
62 CreateFile = CreateFileW # alias
cliechti183d4ae2009-07-23 22:03:51 +000063
64class _OVERLAPPED(Structure):
65 pass
66OVERLAPPED = _OVERLAPPED
67
68class _COMSTAT(Structure):
69 pass
70COMSTAT = _COMSTAT
71
72class _DCB(Structure):
73 pass
74DCB = _DCB
75
76class _COMMTIMEOUTS(Structure):
77 pass
78COMMTIMEOUTS = _COMMTIMEOUTS
79
80GetLastError = _stdcall_libraries['kernel32'].GetLastError
81GetLastError.restype = DWORD
82GetLastError.argtypes = []
83
84LPOVERLAPPED = POINTER(_OVERLAPPED)
85LPDWORD = POINTER(DWORD)
86
87GetOverlappedResult = _stdcall_libraries['kernel32'].GetOverlappedResult
88GetOverlappedResult.restype = BOOL
89GetOverlappedResult.argtypes = [HANDLE, LPOVERLAPPED, LPDWORD, BOOL]
90
91ResetEvent = _stdcall_libraries['kernel32'].ResetEvent
92ResetEvent.restype = BOOL
93ResetEvent.argtypes = [HANDLE]
94
95LPCVOID = c_void_p
96
97WriteFile = _stdcall_libraries['kernel32'].WriteFile
98WriteFile.restype = BOOL
99WriteFile.argtypes = [HANDLE, LPCVOID, DWORD, LPDWORD, LPOVERLAPPED]
100
101LPVOID = c_void_p
102
103ReadFile = _stdcall_libraries['kernel32'].ReadFile
104ReadFile.restype = BOOL
105ReadFile.argtypes = [HANDLE, LPVOID, DWORD, LPDWORD, LPOVERLAPPED]
106
107CloseHandle = _stdcall_libraries['kernel32'].CloseHandle
108CloseHandle.restype = BOOL
109CloseHandle.argtypes = [HANDLE]
110
111ClearCommBreak = _stdcall_libraries['kernel32'].ClearCommBreak
112ClearCommBreak.restype = BOOL
113ClearCommBreak.argtypes = [HANDLE]
114
115LPCOMSTAT = POINTER(_COMSTAT)
116
117ClearCommError = _stdcall_libraries['kernel32'].ClearCommError
118ClearCommError.restype = BOOL
119ClearCommError.argtypes = [HANDLE, LPDWORD, LPCOMSTAT]
120
121SetupComm = _stdcall_libraries['kernel32'].SetupComm
122SetupComm.restype = BOOL
123SetupComm.argtypes = [HANDLE, DWORD, DWORD]
124
125EscapeCommFunction = _stdcall_libraries['kernel32'].EscapeCommFunction
126EscapeCommFunction.restype = BOOL
127EscapeCommFunction.argtypes = [HANDLE, DWORD]
128
129GetCommModemStatus = _stdcall_libraries['kernel32'].GetCommModemStatus
130GetCommModemStatus.restype = BOOL
131GetCommModemStatus.argtypes = [HANDLE, LPDWORD]
132
133LPDCB = POINTER(_DCB)
134
135GetCommState = _stdcall_libraries['kernel32'].GetCommState
136GetCommState.restype = BOOL
137GetCommState.argtypes = [HANDLE, LPDCB]
138
139LPCOMMTIMEOUTS = POINTER(_COMMTIMEOUTS)
140
141GetCommTimeouts = _stdcall_libraries['kernel32'].GetCommTimeouts
142GetCommTimeouts.restype = BOOL
143GetCommTimeouts.argtypes = [HANDLE, LPCOMMTIMEOUTS]
144
145PurgeComm = _stdcall_libraries['kernel32'].PurgeComm
146PurgeComm.restype = BOOL
147PurgeComm.argtypes = [HANDLE, DWORD]
148
149SetCommBreak = _stdcall_libraries['kernel32'].SetCommBreak
150SetCommBreak.restype = BOOL
151SetCommBreak.argtypes = [HANDLE]
152
153SetCommMask = _stdcall_libraries['kernel32'].SetCommMask
154SetCommMask.restype = BOOL
155SetCommMask.argtypes = [HANDLE, DWORD]
156
157SetCommState = _stdcall_libraries['kernel32'].SetCommState
158SetCommState.restype = BOOL
159SetCommState.argtypes = [HANDLE, LPDCB]
160
161SetCommTimeouts = _stdcall_libraries['kernel32'].SetCommTimeouts
162SetCommTimeouts.restype = BOOL
163SetCommTimeouts.argtypes = [HANDLE, LPCOMMTIMEOUTS]
164
cliechtie37b6a82009-07-24 12:19:50 +0000165WaitForSingleObject = _stdcall_libraries['kernel32'].WaitForSingleObject
166WaitForSingleObject.restype = DWORD
167WaitForSingleObject.argtypes = [HANDLE, DWORD]
168
cliechti183d4ae2009-07-23 22:03:51 +0000169ONESTOPBIT = 0 # Variable c_int
170TWOSTOPBITS = 2 # Variable c_int
171ONE5STOPBITS = 1
172
173NOPARITY = 0 # Variable c_int
174ODDPARITY = 1 # Variable c_int
175EVENPARITY = 2 # Variable c_int
176MARKPARITY = 3
177SPACEPARITY = 4
178
cliechtie37b6a82009-07-24 12:19:50 +0000179RTS_CONTROL_HANDSHAKE = 2 # Variable c_int
180RTS_CONTROL_DISABLE = 0 # Variable c_int
181RTS_CONTROL_ENABLE = 1 # Variable c_int
cliechti1f89a0a2011-08-05 02:53:24 +0000182RTS_CONTROL_TOGGLE = 3 # Variable c_int
cliechtie37b6a82009-07-24 12:19:50 +0000183SETRTS = 3
184CLRRTS = 4
185
186DTR_CONTROL_HANDSHAKE = 2 # Variable c_int
187DTR_CONTROL_DISABLE = 0 # Variable c_int
188DTR_CONTROL_ENABLE = 1 # Variable c_int
189SETDTR = 5
190CLRDTR = 6
191
cliechti183d4ae2009-07-23 22:03:51 +0000192MS_DSR_ON = 32 # Variable c_ulong
193EV_RING = 256 # Variable c_int
194EV_PERR = 512 # Variable c_int
195EV_ERR = 128 # Variable c_int
196SETXOFF = 1 # Variable c_int
197EV_RXCHAR = 1 # Variable c_int
198GENERIC_WRITE = 1073741824 # Variable c_long
199PURGE_TXCLEAR = 4 # Variable c_int
200FILE_FLAG_OVERLAPPED = 1073741824 # Variable c_int
201EV_DSR = 16 # Variable c_int
Chris Liechti68340d72015-08-03 14:15:48 +0200202MAXDWORD = 4294967295 # Variable c_uint
cliechti183d4ae2009-07-23 22:03:51 +0000203EV_RLSD = 32 # Variable c_int
204ERROR_IO_PENDING = 997 # Variable c_long
205MS_CTS_ON = 16 # Variable c_ulong
206EV_EVENT1 = 2048 # Variable c_int
cliechti183d4ae2009-07-23 22:03:51 +0000207EV_RX80FULL = 1024 # Variable c_int
208PURGE_RXABORT = 2 # Variable c_int
209FILE_ATTRIBUTE_NORMAL = 128 # Variable c_int
210PURGE_TXABORT = 1 # Variable c_int
211SETXON = 2 # Variable c_int
212OPEN_EXISTING = 3 # Variable c_int
cliechti183d4ae2009-07-23 22:03:51 +0000213MS_RING_ON = 64 # Variable c_ulong
214EV_TXEMPTY = 4 # Variable c_int
215EV_RXFLAG = 2 # Variable c_int
cliechti183d4ae2009-07-23 22:03:51 +0000216MS_RLSD_ON = 128 # Variable c_ulong
Chris Liechti68340d72015-08-03 14:15:48 +0200217GENERIC_READ = 2147483648 # Variable c_ulong
cliechti183d4ae2009-07-23 22:03:51 +0000218EV_EVENT2 = 4096 # Variable c_int
219EV_CTS = 8 # Variable c_int
220EV_BREAK = 64 # Variable c_int
221PURGE_RXCLEAR = 8 # Variable c_int
Chris Liechti68340d72015-08-03 14:15:48 +0200222INFINITE = 0xFFFFFFFF
cliechti183d4ae2009-07-23 22:03:51 +0000223
cliechtide432902011-08-18 22:51:41 +0000224
cliechti183d4ae2009-07-23 22:03:51 +0000225class N11_OVERLAPPED4DOLLAR_48E(Union):
226 pass
227class N11_OVERLAPPED4DOLLAR_484DOLLAR_49E(Structure):
228 pass
229N11_OVERLAPPED4DOLLAR_484DOLLAR_49E._fields_ = [
230 ('Offset', DWORD),
231 ('OffsetHigh', DWORD),
232]
233
234PVOID = c_void_p
235
236N11_OVERLAPPED4DOLLAR_48E._anonymous_ = ['_0']
237N11_OVERLAPPED4DOLLAR_48E._fields_ = [
238 ('_0', N11_OVERLAPPED4DOLLAR_484DOLLAR_49E),
239 ('Pointer', PVOID),
240]
241_OVERLAPPED._anonymous_ = ['_0']
242_OVERLAPPED._fields_ = [
243 ('Internal', ULONG_PTR),
244 ('InternalHigh', ULONG_PTR),
245 ('_0', N11_OVERLAPPED4DOLLAR_48E),
246 ('hEvent', HANDLE),
247]
248_SECURITY_ATTRIBUTES._fields_ = [
249 ('nLength', DWORD),
250 ('lpSecurityDescriptor', LPVOID),
251 ('bInheritHandle', BOOL),
252]
253_COMSTAT._fields_ = [
254 ('fCtsHold', DWORD, 1),
255 ('fDsrHold', DWORD, 1),
256 ('fRlsdHold', DWORD, 1),
257 ('fXoffHold', DWORD, 1),
258 ('fXoffSent', DWORD, 1),
259 ('fEof', DWORD, 1),
260 ('fTxim', DWORD, 1),
261 ('fReserved', DWORD, 25),
262 ('cbInQue', DWORD),
263 ('cbOutQue', DWORD),
264]
265_DCB._fields_ = [
266 ('DCBlength', DWORD),
267 ('BaudRate', DWORD),
268 ('fBinary', DWORD, 1),
269 ('fParity', DWORD, 1),
270 ('fOutxCtsFlow', DWORD, 1),
271 ('fOutxDsrFlow', DWORD, 1),
272 ('fDtrControl', DWORD, 2),
273 ('fDsrSensitivity', DWORD, 1),
274 ('fTXContinueOnXoff', DWORD, 1),
275 ('fOutX', DWORD, 1),
276 ('fInX', DWORD, 1),
277 ('fErrorChar', DWORD, 1),
278 ('fNull', DWORD, 1),
279 ('fRtsControl', DWORD, 2),
280 ('fAbortOnError', DWORD, 1),
281 ('fDummy2', DWORD, 17),
282 ('wReserved', WORD),
283 ('XonLim', WORD),
284 ('XoffLim', WORD),
285 ('ByteSize', BYTE),
286 ('Parity', BYTE),
287 ('StopBits', BYTE),
288 ('XonChar', c_char),
289 ('XoffChar', c_char),
290 ('ErrorChar', c_char),
291 ('EofChar', c_char),
292 ('EvtChar', c_char),
293 ('wReserved1', WORD),
294]
295_COMMTIMEOUTS._fields_ = [
296 ('ReadIntervalTimeout', DWORD),
297 ('ReadTotalTimeoutMultiplier', DWORD),
298 ('ReadTotalTimeoutConstant', DWORD),
299 ('WriteTotalTimeoutMultiplier', DWORD),
300 ('WriteTotalTimeoutConstant', DWORD),
301]
302__all__ = ['GetLastError', 'MS_CTS_ON', 'FILE_ATTRIBUTE_NORMAL',
303 'DTR_CONTROL_ENABLE', '_COMSTAT', 'MS_RLSD_ON',
304 'GetOverlappedResult', 'SETXON', 'PURGE_TXABORT',
305 'PurgeComm', 'N11_OVERLAPPED4DOLLAR_48E', 'EV_RING',
306 'ONESTOPBIT', 'SETXOFF', 'PURGE_RXABORT', 'GetCommState',
307 'RTS_CONTROL_ENABLE', '_DCB', 'CreateEvent',
308 '_COMMTIMEOUTS', '_SECURITY_ATTRIBUTES', 'EV_DSR',
309 'EV_PERR', 'EV_RXFLAG', 'OPEN_EXISTING', 'DCB',
310 'FILE_FLAG_OVERLAPPED', 'EV_CTS', 'SetupComm',
311 'LPOVERLAPPED', 'EV_TXEMPTY', 'ClearCommBreak',
312 'LPSECURITY_ATTRIBUTES', 'SetCommBreak', 'SetCommTimeouts',
313 'COMMTIMEOUTS', 'ODDPARITY', 'EV_RLSD',
314 'GetCommModemStatus', 'EV_EVENT2', 'PURGE_TXCLEAR',
315 'EV_BREAK', 'EVENPARITY', 'LPCVOID', 'COMSTAT', 'ReadFile',
316 'PVOID', '_OVERLAPPED', 'WriteFile', 'GetCommTimeouts',
317 'ResetEvent', 'EV_RXCHAR', 'LPCOMSTAT', 'ClearCommError',
318 'ERROR_IO_PENDING', 'EscapeCommFunction', 'GENERIC_READ',
319 'RTS_CONTROL_HANDSHAKE', 'OVERLAPPED',
320 'DTR_CONTROL_HANDSHAKE', 'PURGE_RXCLEAR', 'GENERIC_WRITE',
321 'LPDCB', 'CreateEventW', 'SetCommMask', 'EV_EVENT1',
322 'SetCommState', 'LPVOID', 'CreateFileW', 'LPDWORD',
323 'EV_RX80FULL', 'TWOSTOPBITS', 'LPCOMMTIMEOUTS', 'MAXDWORD',
324 'MS_DSR_ON', 'MS_RING_ON',
325 'N11_OVERLAPPED4DOLLAR_484DOLLAR_49E', 'EV_ERR',
326 'ULONG_PTR', 'CreateFile', 'NOPARITY', 'CloseHandle']