forked from gijzelaerr/python-snap7
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.py
More file actions
170 lines (147 loc) · 5.38 KB
/
error.py
File metadata and controls
170 lines (147 loc) · 5.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
"""
Snap7 library error codes.
we define all error codes here, but we don't use them (yet/anymore).
The error code formatting of the snap7 library as already quite good,
so we are using that now. But maybe we will use this in the future again.
"""
from _ctypes import Array
from ctypes import c_char, c_int32, c_int
from functools import cache
from typing import Callable, Any, Hashable
from .common import logger, load_library
from .type import Context
s7_client_errors = {
0x00100000: "errNegotiatingPDU",
0x00200000: "errCliInvalidParams",
0x00300000: "errCliJobPending",
0x00400000: "errCliTooManyItems",
0x00500000: "errCliInvalidWordLen",
0x00600000: "errCliPartialDataWritten",
0x00700000: "errCliSizeOverPDU",
0x00800000: "errCliInvalidPlcAnswer",
0x00900000: "errCliAddressOutOfRange",
0x00A00000: "errCliInvalidTransportSize",
0x00B00000: "errCliWriteDataSizeMismatch",
0x00C00000: "errCliItemNotAvailable",
0x00D00000: "errCliInvalidValue",
0x00E00000: "errCliCannotStartPLC",
0x00F00000: "errCliAlreadyRun",
0x01000000: "errCliCannotStopPLC",
0x01100000: "errCliCannotCopyRamToRom",
0x01200000: "errCliCannotCompress",
0x01300000: "errCliAlreadyStop",
0x01400000: "errCliFunNotAvailable",
0x01500000: "errCliUploadSequenceFailed",
0x01600000: "errCliInvalidDataSizeRecvd",
0x01700000: "errCliInvalidBlockType",
0x01800000: "errCliInvalidBlockNumber",
0x01900000: "errCliInvalidBlockSize",
0x01A00000: "errCliDownloadSequenceFailed",
0x01B00000: "errCliInsertRefused",
0x01C00000: "errCliDeleteRefused",
0x01D00000: "errCliNeedPassword",
0x01E00000: "errCliInvalidPassword",
0x01F00000: "errCliNoPasswordToSetOrClear",
0x02000000: "errCliJobTimeout",
0x02100000: "errCliPartialDataRead",
0x02200000: "errCliBufferTooSmall",
0x02300000: "errCliFunctionRefused",
0x02400000: "errCliDestroying",
0x02500000: "errCliInvalidParamNumber",
0x02600000: "errCliCannotChangeParam",
}
isotcp_errors = {
0x00010000: "errIsoConnect",
0x00020000: "errIsoDisconnect",
0x00030000: "errIsoInvalidPDU",
0x00040000: "errIsoInvalidDataSize",
0x00050000: "errIsoNullPointer",
0x00060000: "errIsoShortPacket",
0x00070000: "errIsoTooManyFragments",
0x00080000: "errIsoPduOverflow",
0x00090000: "errIsoSendPacket",
0x000A0000: "errIsoRecvPacket",
0x000B0000: "errIsoInvalidParams",
0x000C0000: "errIsoResvd_1",
0x000D0000: "errIsoResvd_2",
0x000E0000: "errIsoResvd_3",
0x000F0000: "errIsoResvd_4",
}
tcp_errors = {
0x00000001: "evcServerStarted",
0x00000002: "evcServerStopped",
0x00000004: "evcListenerCannotStart",
0x00000008: "evcClientAdded",
0x00000010: "evcClientRejected",
0x00000020: "evcClientNoRoom",
0x00000040: "evcClientException",
0x00000080: "evcClientDisconnected",
0x00000100: "evcClientTerminated",
0x00000200: "evcClientsDropped",
0x00000400: "evcReserved_00000400",
0x00000800: "evcReserved_00000800",
0x00001000: "evcReserved_00001000",
0x00002000: "evcReserved_00002000",
0x00004000: "evcReserved_00004000",
0x00008000: "evcReserved_00008000",
}
s7_server_errors = {
0x00100000: "errSrvCannotStart",
0x00200000: "errSrvDBNullPointer",
0x00300000: "errSrvAreaAlreadyExists",
0x00400000: "errSrvUnknownArea",
0x00500000: "verrSrvInvalidParams",
0x00600000: "errSrvTooManyDB",
0x00700000: "errSrvInvalidParamNumber",
0x00800000: "errSrvCannotChangeParam",
}
client_errors = s7_client_errors.copy()
client_errors.update(isotcp_errors)
client_errors.update(tcp_errors)
server_errors = s7_server_errors.copy()
server_errors.update(isotcp_errors)
server_errors.update(tcp_errors)
def error_wrap(context: Context) -> Callable[..., Callable[..., None]]:
"""Parses a s7 error code returned the decorated function."""
def middle(func: Callable[..., int]) -> Any:
def inner(*args: tuple[Any, ...], **kwargs: dict[Hashable, Any]) -> None:
code = func(*args, **kwargs)
check_error(code, context=context)
return inner
return middle
@cache
def check_error(code: int, context: Context = "client") -> None:
"""Check if the error code is set. If so, a Python log message is generated
and an error is raised.
Args:
code: error code number.
context: context in which is called.
Raises:
RuntimeError: if the code exists and is different from 1.
"""
if code and code != 1:
error = error_text(code, context)
logger.error(error)
raise RuntimeError(error)
def error_text(error: int, context: Context = "client") -> bytes:
"""Returns a textual explanation of a given error number
Args:
error: an error integer
context: context in which is called from, server, client or partner
Returns:
The error.
Raises:
TypeError: if the context is not in `["client", "server", "partner"]`
"""
logger.debug(f"error text for {hex(error)}")
len_ = 1024
text_type = c_char * len_
text = text_type()
library = load_library()
error_text_func: Callable[[c_int32, Array[c_char], c_int], int] = {
"client": library.Cli_ErrorText,
"server": library.Srv_ErrorText,
"partner": library.Par_ErrorText,
}[context]
error_text_func(c_int32(error), text, c_int(len_))
return text.value