blob: 0ad9b6d4f1c9b2beeeaa537c81bcd5293192b5bb [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
cliechti183d4ae2009-07-23 22:03:51 +000011from ctypes.wintypes import DWORD
12from ctypes.wintypes import WORD
13from ctypes.wintypes import BYTE
Chris Liechti033f17c2015-08-30 21:28:04 +020014_stdcall_libraries = {}
15_stdcall_libraries['kernel32'] = WinDLL('kernel32')
cliechti183d4ae2009-07-23 22:03:51 +000016
cliechtie37b6a82009-07-24 12:19:50 +000017INVALID_HANDLE_VALUE = HANDLE(-1).value
cliechti183d4ae2009-07-23 22:03:51 +000018
Chris Liechti033f17c2015-08-30 21:28:04 +020019
cliechti251e5ce2011-08-25 01:24:49 +000020# some details of the windows API differ between 32 and 64 bit systems..
21def is_64bit():
22 """Returns true when running on a 64 bit system"""
23 return sizeof(c_ulong) != sizeof(c_void_p)
24
cliechtide432902011-08-18 22:51:41 +000025# ULONG_PTR is a an ordinary number, not a pointer and contrary to the name it
26# is either 32 or 64 bits, depending on the type of windows...
27# so test if this a 32 bit windows...
cliechti251e5ce2011-08-25 01:24:49 +000028if is_64bit():
cliechtide432902011-08-18 22:51:41 +000029 # assume 64 bits
30 ULONG_PTR = c_int64
cliechti251e5ce2011-08-25 01:24:49 +000031else:
32 # 32 bits
33 ULONG_PTR = c_ulong
cliechtide432902011-08-18 22:51:41 +000034
35
cliechti183d4ae2009-07-23 22:03:51 +000036class _SECURITY_ATTRIBUTES(Structure):
37 pass
38LPSECURITY_ATTRIBUTES = POINTER(_SECURITY_ATTRIBUTES)
39
cliechtie37b6a82009-07-24 12:19:50 +000040
cliechti8a345132011-08-05 02:33:14 +000041try:
42 CreateEventW = _stdcall_libraries['kernel32'].CreateEventW
43except AttributeError:
44 # Fallback to non wide char version for old OS...
45 from ctypes.wintypes import LPCSTR
46 CreateEventA = _stdcall_libraries['kernel32'].CreateEventA
47 CreateEventA.restype = HANDLE
48 CreateEventA.argtypes = [LPSECURITY_ATTRIBUTES, BOOL, BOOL, LPCSTR]
Chris Liechti033f17c2015-08-30 21:28:04 +020049 CreateEvent = CreateEventA
cliechti8a345132011-08-05 02:33:14 +000050
51 CreateFileA = _stdcall_libraries['kernel32'].CreateFileA
52 CreateFileA.restype = HANDLE
53 CreateFileA.argtypes = [LPCSTR, DWORD, DWORD, LPSECURITY_ATTRIBUTES, DWORD, DWORD, HANDLE]
54 CreateFile = CreateFileA
55else:
56 CreateEventW.restype = HANDLE
57 CreateEventW.argtypes = [LPSECURITY_ATTRIBUTES, BOOL, BOOL, LPCWSTR]
Chris Liechti033f17c2015-08-30 21:28:04 +020058 CreateEvent = CreateEventW # alias
cliechti8a345132011-08-05 02:33:14 +000059
60 CreateFileW = _stdcall_libraries['kernel32'].CreateFileW
61 CreateFileW.restype = HANDLE
62 CreateFileW.argtypes = [LPCWSTR, DWORD, DWORD, LPSECURITY_ATTRIBUTES, DWORD, DWORD, HANDLE]
Chris Liechti033f17c2015-08-30 21:28:04 +020063 CreateFile = CreateFileW # alias
64
cliechti183d4ae2009-07-23 22:03:51 +000065
66class _OVERLAPPED(Structure):
67 pass
Chris Liechti033f17c2015-08-30 21:28:04 +020068
cliechti183d4ae2009-07-23 22:03:51 +000069OVERLAPPED = _OVERLAPPED
70
Chris Liechti033f17c2015-08-30 21:28:04 +020071
cliechti183d4ae2009-07-23 22:03:51 +000072class _COMSTAT(Structure):
73 pass
Chris Liechti033f17c2015-08-30 21:28:04 +020074
cliechti183d4ae2009-07-23 22:03:51 +000075COMSTAT = _COMSTAT
76
Chris Liechti033f17c2015-08-30 21:28:04 +020077
cliechti183d4ae2009-07-23 22:03:51 +000078class _DCB(Structure):
79 pass
Chris Liechti033f17c2015-08-30 21:28:04 +020080
cliechti183d4ae2009-07-23 22:03:51 +000081DCB = _DCB
82
Chris Liechti033f17c2015-08-30 21:28:04 +020083
cliechti183d4ae2009-07-23 22:03:51 +000084class _COMMTIMEOUTS(Structure):
85 pass
Chris Liechti033f17c2015-08-30 21:28:04 +020086
cliechti183d4ae2009-07-23 22:03:51 +000087COMMTIMEOUTS = _COMMTIMEOUTS
88
89GetLastError = _stdcall_libraries['kernel32'].GetLastError
90GetLastError.restype = DWORD
91GetLastError.argtypes = []
92
93LPOVERLAPPED = POINTER(_OVERLAPPED)
94LPDWORD = POINTER(DWORD)
95
96GetOverlappedResult = _stdcall_libraries['kernel32'].GetOverlappedResult
97GetOverlappedResult.restype = BOOL
98GetOverlappedResult.argtypes = [HANDLE, LPOVERLAPPED, LPDWORD, BOOL]
99
100ResetEvent = _stdcall_libraries['kernel32'].ResetEvent
101ResetEvent.restype = BOOL
102ResetEvent.argtypes = [HANDLE]
103
104LPCVOID = c_void_p
105
106WriteFile = _stdcall_libraries['kernel32'].WriteFile
107WriteFile.restype = BOOL
108WriteFile.argtypes = [HANDLE, LPCVOID, DWORD, LPDWORD, LPOVERLAPPED]
109
110LPVOID = c_void_p
111
112ReadFile = _stdcall_libraries['kernel32'].ReadFile
113ReadFile.restype = BOOL
114ReadFile.argtypes = [HANDLE, LPVOID, DWORD, LPDWORD, LPOVERLAPPED]
115
116CloseHandle = _stdcall_libraries['kernel32'].CloseHandle
117CloseHandle.restype = BOOL
118CloseHandle.argtypes = [HANDLE]
119
120ClearCommBreak = _stdcall_libraries['kernel32'].ClearCommBreak
121ClearCommBreak.restype = BOOL
122ClearCommBreak.argtypes = [HANDLE]
123
124LPCOMSTAT = POINTER(_COMSTAT)
125
126ClearCommError = _stdcall_libraries['kernel32'].ClearCommError
127ClearCommError.restype = BOOL
128ClearCommError.argtypes = [HANDLE, LPDWORD, LPCOMSTAT]
129
130SetupComm = _stdcall_libraries['kernel32'].SetupComm
131SetupComm.restype = BOOL
132SetupComm.argtypes = [HANDLE, DWORD, DWORD]
133
134EscapeCommFunction = _stdcall_libraries['kernel32'].EscapeCommFunction
135EscapeCommFunction.restype = BOOL
136EscapeCommFunction.argtypes = [HANDLE, DWORD]
137
138GetCommModemStatus = _stdcall_libraries['kernel32'].GetCommModemStatus
139GetCommModemStatus.restype = BOOL
140GetCommModemStatus.argtypes = [HANDLE, LPDWORD]
141
142LPDCB = POINTER(_DCB)
143
144GetCommState = _stdcall_libraries['kernel32'].GetCommState
145GetCommState.restype = BOOL
146GetCommState.argtypes = [HANDLE, LPDCB]
147
148LPCOMMTIMEOUTS = POINTER(_COMMTIMEOUTS)
149
150GetCommTimeouts = _stdcall_libraries['kernel32'].GetCommTimeouts
151GetCommTimeouts.restype = BOOL
152GetCommTimeouts.argtypes = [HANDLE, LPCOMMTIMEOUTS]
153
154PurgeComm = _stdcall_libraries['kernel32'].PurgeComm
155PurgeComm.restype = BOOL
156PurgeComm.argtypes = [HANDLE, DWORD]
157
158SetCommBreak = _stdcall_libraries['kernel32'].SetCommBreak
159SetCommBreak.restype = BOOL
160SetCommBreak.argtypes = [HANDLE]
161
162SetCommMask = _stdcall_libraries['kernel32'].SetCommMask
163SetCommMask.restype = BOOL
164SetCommMask.argtypes = [HANDLE, DWORD]
165
166SetCommState = _stdcall_libraries['kernel32'].SetCommState
167SetCommState.restype = BOOL
168SetCommState.argtypes = [HANDLE, LPDCB]
169
170SetCommTimeouts = _stdcall_libraries['kernel32'].SetCommTimeouts
171SetCommTimeouts.restype = BOOL
172SetCommTimeouts.argtypes = [HANDLE, LPCOMMTIMEOUTS]
173
cliechtie37b6a82009-07-24 12:19:50 +0000174WaitForSingleObject = _stdcall_libraries['kernel32'].WaitForSingleObject
175WaitForSingleObject.restype = DWORD
176WaitForSingleObject.argtypes = [HANDLE, DWORD]
177
Chris Liechti033f17c2015-08-30 21:28:04 +0200178ONESTOPBIT = 0 # Variable c_int
179TWOSTOPBITS = 2 # Variable c_int
cliechti183d4ae2009-07-23 22:03:51 +0000180ONE5STOPBITS = 1
181
Chris Liechti033f17c2015-08-30 21:28:04 +0200182NOPARITY = 0 # Variable c_int
183ODDPARITY = 1 # Variable c_int
184EVENPARITY = 2 # Variable c_int
cliechti183d4ae2009-07-23 22:03:51 +0000185MARKPARITY = 3
186SPACEPARITY = 4
187
Chris Liechti033f17c2015-08-30 21:28:04 +0200188RTS_CONTROL_HANDSHAKE = 2 # Variable c_int
189RTS_CONTROL_DISABLE = 0 # Variable c_int
190RTS_CONTROL_ENABLE = 1 # Variable c_int
191RTS_CONTROL_TOGGLE = 3 # Variable c_int
cliechtie37b6a82009-07-24 12:19:50 +0000192SETRTS = 3
193CLRRTS = 4
194
Chris Liechti033f17c2015-08-30 21:28:04 +0200195DTR_CONTROL_HANDSHAKE = 2 # Variable c_int
196DTR_CONTROL_DISABLE = 0 # Variable c_int
197DTR_CONTROL_ENABLE = 1 # Variable c_int
cliechtie37b6a82009-07-24 12:19:50 +0000198SETDTR = 5
199CLRDTR = 6
200
Chris Liechti033f17c2015-08-30 21:28:04 +0200201MS_DSR_ON = 32 # Variable c_ulong
202EV_RING = 256 # Variable c_int
203EV_PERR = 512 # Variable c_int
204EV_ERR = 128 # Variable c_int
205SETXOFF = 1 # Variable c_int
206EV_RXCHAR = 1 # Variable c_int
207GENERIC_WRITE = 1073741824 # Variable c_long
208PURGE_TXCLEAR = 4 # Variable c_int
209FILE_FLAG_OVERLAPPED = 1073741824 # Variable c_int
210EV_DSR = 16 # Variable c_int
211MAXDWORD = 4294967295 # Variable c_uint
212EV_RLSD = 32 # Variable c_int
Chris Liechti7731e982015-11-02 23:46:07 +0100213ERROR_SUCCESS = 0
Chris Liechti033f17c2015-08-30 21:28:04 +0200214ERROR_IO_PENDING = 997 # Variable c_long
215MS_CTS_ON = 16 # Variable c_ulong
216EV_EVENT1 = 2048 # Variable c_int
217EV_RX80FULL = 1024 # Variable c_int
218PURGE_RXABORT = 2 # Variable c_int
219FILE_ATTRIBUTE_NORMAL = 128 # Variable c_int
220PURGE_TXABORT = 1 # Variable c_int
221SETXON = 2 # Variable c_int
222OPEN_EXISTING = 3 # Variable c_int
223MS_RING_ON = 64 # Variable c_ulong
224EV_TXEMPTY = 4 # Variable c_int
225EV_RXFLAG = 2 # Variable c_int
226MS_RLSD_ON = 128 # Variable c_ulong
227GENERIC_READ = 2147483648 # Variable c_ulong
228EV_EVENT2 = 4096 # Variable c_int
229EV_CTS = 8 # Variable c_int
230EV_BREAK = 64 # Variable c_int
231PURGE_RXCLEAR = 8 # Variable c_int
Chris Liechti68340d72015-08-03 14:15:48 +0200232INFINITE = 0xFFFFFFFF
cliechti183d4ae2009-07-23 22:03:51 +0000233
cliechtide432902011-08-18 22:51:41 +0000234
cliechti183d4ae2009-07-23 22:03:51 +0000235class N11_OVERLAPPED4DOLLAR_48E(Union):
236 pass
Chris Liechti033f17c2015-08-30 21:28:04 +0200237
238
cliechti183d4ae2009-07-23 22:03:51 +0000239class N11_OVERLAPPED4DOLLAR_484DOLLAR_49E(Structure):
240 pass
Chris Liechti033f17c2015-08-30 21:28:04 +0200241
242
cliechti183d4ae2009-07-23 22:03:51 +0000243N11_OVERLAPPED4DOLLAR_484DOLLAR_49E._fields_ = [
244 ('Offset', DWORD),
245 ('OffsetHigh', DWORD),
246]
247
248PVOID = c_void_p
249
250N11_OVERLAPPED4DOLLAR_48E._anonymous_ = ['_0']
251N11_OVERLAPPED4DOLLAR_48E._fields_ = [
252 ('_0', N11_OVERLAPPED4DOLLAR_484DOLLAR_49E),
253 ('Pointer', PVOID),
254]
255_OVERLAPPED._anonymous_ = ['_0']
256_OVERLAPPED._fields_ = [
257 ('Internal', ULONG_PTR),
258 ('InternalHigh', ULONG_PTR),
259 ('_0', N11_OVERLAPPED4DOLLAR_48E),
260 ('hEvent', HANDLE),
261]
262_SECURITY_ATTRIBUTES._fields_ = [
263 ('nLength', DWORD),
264 ('lpSecurityDescriptor', LPVOID),
265 ('bInheritHandle', BOOL),
266]
267_COMSTAT._fields_ = [
268 ('fCtsHold', DWORD, 1),
269 ('fDsrHold', DWORD, 1),
270 ('fRlsdHold', DWORD, 1),
271 ('fXoffHold', DWORD, 1),
272 ('fXoffSent', DWORD, 1),
273 ('fEof', DWORD, 1),
274 ('fTxim', DWORD, 1),
275 ('fReserved', DWORD, 25),
276 ('cbInQue', DWORD),
277 ('cbOutQue', DWORD),
278]
279_DCB._fields_ = [
280 ('DCBlength', DWORD),
281 ('BaudRate', DWORD),
282 ('fBinary', DWORD, 1),
283 ('fParity', DWORD, 1),
284 ('fOutxCtsFlow', DWORD, 1),
285 ('fOutxDsrFlow', DWORD, 1),
286 ('fDtrControl', DWORD, 2),
287 ('fDsrSensitivity', DWORD, 1),
288 ('fTXContinueOnXoff', DWORD, 1),
289 ('fOutX', DWORD, 1),
290 ('fInX', DWORD, 1),
291 ('fErrorChar', DWORD, 1),
292 ('fNull', DWORD, 1),
293 ('fRtsControl', DWORD, 2),
294 ('fAbortOnError', DWORD, 1),
295 ('fDummy2', DWORD, 17),
296 ('wReserved', WORD),
297 ('XonLim', WORD),
298 ('XoffLim', WORD),
299 ('ByteSize', BYTE),
300 ('Parity', BYTE),
301 ('StopBits', BYTE),
302 ('XonChar', c_char),
303 ('XoffChar', c_char),
304 ('ErrorChar', c_char),
305 ('EofChar', c_char),
306 ('EvtChar', c_char),
307 ('wReserved1', WORD),
308]
309_COMMTIMEOUTS._fields_ = [
310 ('ReadIntervalTimeout', DWORD),
311 ('ReadTotalTimeoutMultiplier', DWORD),
312 ('ReadTotalTimeoutConstant', DWORD),
313 ('WriteTotalTimeoutMultiplier', DWORD),
314 ('WriteTotalTimeoutConstant', DWORD),
315]
316__all__ = ['GetLastError', 'MS_CTS_ON', 'FILE_ATTRIBUTE_NORMAL',
317 'DTR_CONTROL_ENABLE', '_COMSTAT', 'MS_RLSD_ON',
318 'GetOverlappedResult', 'SETXON', 'PURGE_TXABORT',
319 'PurgeComm', 'N11_OVERLAPPED4DOLLAR_48E', 'EV_RING',
320 'ONESTOPBIT', 'SETXOFF', 'PURGE_RXABORT', 'GetCommState',
321 'RTS_CONTROL_ENABLE', '_DCB', 'CreateEvent',
322 '_COMMTIMEOUTS', '_SECURITY_ATTRIBUTES', 'EV_DSR',
323 'EV_PERR', 'EV_RXFLAG', 'OPEN_EXISTING', 'DCB',
324 'FILE_FLAG_OVERLAPPED', 'EV_CTS', 'SetupComm',
325 'LPOVERLAPPED', 'EV_TXEMPTY', 'ClearCommBreak',
326 'LPSECURITY_ATTRIBUTES', 'SetCommBreak', 'SetCommTimeouts',
327 'COMMTIMEOUTS', 'ODDPARITY', 'EV_RLSD',
328 'GetCommModemStatus', 'EV_EVENT2', 'PURGE_TXCLEAR',
329 'EV_BREAK', 'EVENPARITY', 'LPCVOID', 'COMSTAT', 'ReadFile',
330 'PVOID', '_OVERLAPPED', 'WriteFile', 'GetCommTimeouts',
331 'ResetEvent', 'EV_RXCHAR', 'LPCOMSTAT', 'ClearCommError',
332 'ERROR_IO_PENDING', 'EscapeCommFunction', 'GENERIC_READ',
333 'RTS_CONTROL_HANDSHAKE', 'OVERLAPPED',
334 'DTR_CONTROL_HANDSHAKE', 'PURGE_RXCLEAR', 'GENERIC_WRITE',
335 'LPDCB', 'CreateEventW', 'SetCommMask', 'EV_EVENT1',
336 'SetCommState', 'LPVOID', 'CreateFileW', 'LPDWORD',
337 'EV_RX80FULL', 'TWOSTOPBITS', 'LPCOMMTIMEOUTS', 'MAXDWORD',
338 'MS_DSR_ON', 'MS_RING_ON',
339 'N11_OVERLAPPED4DOLLAR_484DOLLAR_49E', 'EV_ERR',
340 'ULONG_PTR', 'CreateFile', 'NOPARITY', 'CloseHandle']