author | Laurent Bessard |
Sun, 12 May 2013 23:30:00 +0200 | |
changeset 1122 | 84de51ab40d2 |
parent 1075 | 8078c01ae464 |
child 1433 | 4a45f6642523 |
permissions | -rw-r--r-- |
592 | 1 |
#!/usr/bin/env python |
2 |
# -*- coding: utf-8 -*- |
|
3 |
# |
|
4 |
#Copyright (C) 2011: Edouard TISSERANT and Laurent BESSARD |
|
5 |
# |
|
6 |
#See COPYING file for copyrights details. |
|
7 |
# |
|
8 |
#This library is free software; you can redistribute it and/or |
|
9 |
#modify it under the terms of the GNU General Public |
|
10 |
#License as published by the Free Software Foundation; either |
|
11 |
#version 2.1 of the License, or (at your option) any later version. |
|
12 |
# |
|
13 |
#This library is distributed in the hope that it will be useful, |
|
14 |
#but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 |
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
16 |
#General Public License for more details. |
|
17 |
# |
|
18 |
#You should have received a copy of the GNU General Public |
|
19 |
#License along with this library; if not, write to the Free Software |
|
20 |
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
21 |
||
22 |
from ctypes import * |
|
593 | 23 |
from datetime import timedelta as td |
592 | 24 |
|
25 |
class IEC_STRING(Structure): |
|
26 |
""" |
|
27 |
Must be changed according to changes in iec_types.h |
|
28 |
""" |
|
29 |
_fields_ = [("len", c_uint8), |
|
30 |
("body", c_char * 126)] |
|
31 |
||
32 |
class IEC_TIME(Structure): |
|
33 |
""" |
|
34 |
Must be changed according to changes in iec_types.h |
|
35 |
""" |
|
36 |
_fields_ = [("s", c_long), #tv_sec |
|
37 |
("ns", c_long)] #tv_nsec |
|
38 |
||
39 |
def _t(t, u=lambda x:x.value, p=lambda t,x:t(x)): return (t, u, p) |
|
595
6348c0110e0f
Added support for handling ANY_DATE types in debugger type translation
Edouard Tisserant
parents:
594
diff
changeset
|
40 |
def _ttime(): return (IEC_TIME, |
6348c0110e0f
Added support for handling ANY_DATE types in debugger type translation
Edouard Tisserant
parents:
594
diff
changeset
|
41 |
lambda x:td(0, x.s, x.ns/1000), |
6348c0110e0f
Added support for handling ANY_DATE types in debugger type translation
Edouard Tisserant
parents:
594
diff
changeset
|
42 |
lambda t,x:t(x.days * 24 * 3600 + x.seconds, x.microseconds*1000)) |
592 | 43 |
|
44 |
SameEndianessTypeTranslator = { |
|
45 |
"BOOL" : _t(c_uint8, lambda x:x.value!=0), |
|
46 |
"STEP" : _t(c_uint8), |
|
47 |
"TRANSITION" : _t(c_uint8), |
|
48 |
"ACTION" : _t(c_uint8), |
|
49 |
"SINT" : _t(c_int8), |
|
50 |
"USINT" : _t(c_uint8), |
|
51 |
"BYTE" : _t(c_uint8), |
|
595
6348c0110e0f
Added support for handling ANY_DATE types in debugger type translation
Edouard Tisserant
parents:
594
diff
changeset
|
52 |
"STRING" : (IEC_STRING, |
592 | 53 |
lambda x:x.body[:x.len], |
54 |
lambda t,x:t(len(x),x)), |
|
55 |
"INT" : _t(c_int16), |
|
56 |
"UINT" : _t(c_uint16), |
|
57 |
"WORD" : _t(c_uint16), |
|
58 |
"DINT" : _t(c_int32), |
|
59 |
"UDINT" : _t(c_uint32), |
|
60 |
"DWORD" : _t(c_uint32), |
|
61 |
"LINT" : _t(c_int64), |
|
62 |
"ULINT" : _t(c_uint64), |
|
63 |
"LWORD" : _t(c_uint64), |
|
64 |
"REAL" : _t(c_float), |
|
65 |
"LREAL" : _t(c_double), |
|
595
6348c0110e0f
Added support for handling ANY_DATE types in debugger type translation
Edouard Tisserant
parents:
594
diff
changeset
|
66 |
"TIME" : _ttime(), |
6348c0110e0f
Added support for handling ANY_DATE types in debugger type translation
Edouard Tisserant
parents:
594
diff
changeset
|
67 |
"TOD" : _ttime(), |
6348c0110e0f
Added support for handling ANY_DATE types in debugger type translation
Edouard Tisserant
parents:
594
diff
changeset
|
68 |
"DATE" : _ttime(), |
6348c0110e0f
Added support for handling ANY_DATE types in debugger type translation
Edouard Tisserant
parents:
594
diff
changeset
|
69 |
"DT" : _ttime(), |
592 | 70 |
} |
71 |
||
72 |
SwapedEndianessTypeTranslator = { |
|
73 |
#TODO |
|
74 |
} |
|
75 |
||
1075
8078c01ae464
Now Debug Buffer Unpacking is a separate function, declared in typemapping.py
Edouard Tisserant
parents:
972
diff
changeset
|
76 |
TypeTranslator=SameEndianessTypeTranslator |
8078c01ae464
Now Debug Buffer Unpacking is a separate function, declared in typemapping.py
Edouard Tisserant
parents:
972
diff
changeset
|
77 |
|
592 | 78 |
# Construct debugger natively supported types |
79 |
DebugTypesSize = dict([(key,sizeof(t)) for key,(t,p,u) in SameEndianessTypeTranslator.iteritems() if t is not None]) |
|
80 |
||
1075
8078c01ae464
Now Debug Buffer Unpacking is a separate function, declared in typemapping.py
Edouard Tisserant
parents:
972
diff
changeset
|
81 |
def UnpackDebugBuffer(buff, size, indexes): |
8078c01ae464
Now Debug Buffer Unpacking is a separate function, declared in typemapping.py
Edouard Tisserant
parents:
972
diff
changeset
|
82 |
res = [] |
8078c01ae464
Now Debug Buffer Unpacking is a separate function, declared in typemapping.py
Edouard Tisserant
parents:
972
diff
changeset
|
83 |
offset = 0 |
8078c01ae464
Now Debug Buffer Unpacking is a separate function, declared in typemapping.py
Edouard Tisserant
parents:
972
diff
changeset
|
84 |
for idx, iectype, forced in indexes: |
8078c01ae464
Now Debug Buffer Unpacking is a separate function, declared in typemapping.py
Edouard Tisserant
parents:
972
diff
changeset
|
85 |
cursor = c_void_p(buff.value + offset) |
8078c01ae464
Now Debug Buffer Unpacking is a separate function, declared in typemapping.py
Edouard Tisserant
parents:
972
diff
changeset
|
86 |
c_type,unpack_func, pack_func = \ |
8078c01ae464
Now Debug Buffer Unpacking is a separate function, declared in typemapping.py
Edouard Tisserant
parents:
972
diff
changeset
|
87 |
TypeTranslator.get(iectype, |
8078c01ae464
Now Debug Buffer Unpacking is a separate function, declared in typemapping.py
Edouard Tisserant
parents:
972
diff
changeset
|
88 |
(None,None,None)) |
8078c01ae464
Now Debug Buffer Unpacking is a separate function, declared in typemapping.py
Edouard Tisserant
parents:
972
diff
changeset
|
89 |
if c_type is not None and offset < size: |
8078c01ae464
Now Debug Buffer Unpacking is a separate function, declared in typemapping.py
Edouard Tisserant
parents:
972
diff
changeset
|
90 |
res.append(unpack_func( |
8078c01ae464
Now Debug Buffer Unpacking is a separate function, declared in typemapping.py
Edouard Tisserant
parents:
972
diff
changeset
|
91 |
cast(cursor, |
8078c01ae464
Now Debug Buffer Unpacking is a separate function, declared in typemapping.py
Edouard Tisserant
parents:
972
diff
changeset
|
92 |
POINTER(c_type)).contents)) |
8078c01ae464
Now Debug Buffer Unpacking is a separate function, declared in typemapping.py
Edouard Tisserant
parents:
972
diff
changeset
|
93 |
offset += sizeof(c_type) if iectype != "STRING" else len(res[-1])+1 |
8078c01ae464
Now Debug Buffer Unpacking is a separate function, declared in typemapping.py
Edouard Tisserant
parents:
972
diff
changeset
|
94 |
else: |
8078c01ae464
Now Debug Buffer Unpacking is a separate function, declared in typemapping.py
Edouard Tisserant
parents:
972
diff
changeset
|
95 |
#if c_type is None: |
8078c01ae464
Now Debug Buffer Unpacking is a separate function, declared in typemapping.py
Edouard Tisserant
parents:
972
diff
changeset
|
96 |
# PLCprint("Debug error - " + iectype + |
8078c01ae464
Now Debug Buffer Unpacking is a separate function, declared in typemapping.py
Edouard Tisserant
parents:
972
diff
changeset
|
97 |
# " not supported !") |
8078c01ae464
Now Debug Buffer Unpacking is a separate function, declared in typemapping.py
Edouard Tisserant
parents:
972
diff
changeset
|
98 |
#if offset >= size: |
8078c01ae464
Now Debug Buffer Unpacking is a separate function, declared in typemapping.py
Edouard Tisserant
parents:
972
diff
changeset
|
99 |
# PLCprint("Debug error - buffer too small ! %d != %d"%(offset, size)) |
8078c01ae464
Now Debug Buffer Unpacking is a separate function, declared in typemapping.py
Edouard Tisserant
parents:
972
diff
changeset
|
100 |
break |
8078c01ae464
Now Debug Buffer Unpacking is a separate function, declared in typemapping.py
Edouard Tisserant
parents:
972
diff
changeset
|
101 |
if offset and offset == size: |
8078c01ae464
Now Debug Buffer Unpacking is a separate function, declared in typemapping.py
Edouard Tisserant
parents:
972
diff
changeset
|
102 |
return res |
8078c01ae464
Now Debug Buffer Unpacking is a separate function, declared in typemapping.py
Edouard Tisserant
parents:
972
diff
changeset
|
103 |
return None |
8078c01ae464
Now Debug Buffer Unpacking is a separate function, declared in typemapping.py
Edouard Tisserant
parents:
972
diff
changeset
|
104 |
|
8078c01ae464
Now Debug Buffer Unpacking is a separate function, declared in typemapping.py
Edouard Tisserant
parents:
972
diff
changeset
|
105 |
|
8078c01ae464
Now Debug Buffer Unpacking is a separate function, declared in typemapping.py
Edouard Tisserant
parents:
972
diff
changeset
|
106 |
|
917 | 107 |
LogLevels = ["CRITICAL","WARNING","INFO","DEBUG"] |
108 |
LogLevelsCount = len(LogLevels) |
|
109 |
LogLevelsDict = dict(zip(LogLevels,range(LogLevelsCount))) |
|
110 |
LogLevelsDefault = LogLevelsDict["DEBUG"] |
|
972 | 111 |