runtime/typemapping.py
author Edouard Tisserant <edouard.tisserant@gmail.com>
Fri, 26 Apr 2024 09:24:26 +0200
changeset 3938 fc4af5685aa3
parent 3887 2df45e4bd500
permissions -rw-r--r--
merge
592
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents:
diff changeset
     1
#!/usr/bin/env python
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents:
diff changeset
     3
#
1667
cefc9219bb48 runtime is licensed under LGPLv2.1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
     4
# See COPYING.Runtime file for copyrights details.
592
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents:
diff changeset
     5
#
1881
091005ec69c4 fix pylint py3k conversion warning: "(no-absolute-import) import missing `from __future__ import absolute_import`"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1847
diff changeset
     6
1783
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1774
diff changeset
     7
from ctypes import *
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1774
diff changeset
     8
from datetime import timedelta as td
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1774
diff changeset
     9
592
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents:
diff changeset
    10
class IEC_STRING(Structure):
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents:
diff changeset
    11
    """
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents:
diff changeset
    12
    Must be changed according to changes in iec_types.h
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents:
diff changeset
    13
    """
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents:
diff changeset
    14
    _fields_ = [("len", c_uint8),
1433
4a45f6642523 Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents: 1075
diff changeset
    15
                ("body", c_char * 126)]
592
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents:
diff changeset
    16
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1733
diff changeset
    17
592
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents:
diff changeset
    18
class IEC_TIME(Structure):
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents:
diff changeset
    19
    """
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents:
diff changeset
    20
    Must be changed according to changes in iec_types.h
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents:
diff changeset
    21
    """
1737
a39c2918c015 clean-up: fix PEP8 E261 at least two spaces before inline comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
    22
    _fields_ = [("s", c_long),   # tv_sec
a39c2918c015 clean-up: fix PEP8 E261 at least two spaces before inline comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
    23
                ("ns", c_long)]  # tv_nsec
592
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents:
diff changeset
    24
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1733
diff changeset
    25
3813
6f3e20690172 Runtime: Fix IEC/ctypes/python3 type mapping
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3771
diff changeset
    26
def _t(t, u=lambda x: x.value, p=lambda t, x: t(x)):
1835
7533061a6d82 fix more than one statement on a single line
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1783
diff changeset
    27
    return (t, u, p)
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1733
diff changeset
    28
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1733
diff changeset
    29
1835
7533061a6d82 fix more than one statement on a single line
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1783
diff changeset
    30
def _ttime():
7533061a6d82 fix more than one statement on a single line
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1783
diff changeset
    31
    return (IEC_TIME,
3813
6f3e20690172 Runtime: Fix IEC/ctypes/python3 type mapping
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3771
diff changeset
    32
            lambda x: td(0, x.s, x.ns/1000.0),
1835
7533061a6d82 fix more than one statement on a single line
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1783
diff changeset
    33
            lambda t, x: t(x.days * 24 * 3600 + x.seconds, x.microseconds*1000))
592
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents:
diff changeset
    34
1749
d73b64672238 clean-up: fix PEP8 E305 expected 2 blank lines after class or function definition
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1748
diff changeset
    35
592
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents:
diff changeset
    36
SameEndianessTypeTranslator = {
3813
6f3e20690172 Runtime: Fix IEC/ctypes/python3 type mapping
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3771
diff changeset
    37
    "BOOL":       _t(c_uint8, lambda x: bool(x.value)),
1739
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1738
diff changeset
    38
    "STEP":       _t(c_uint8),
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1738
diff changeset
    39
    "TRANSITION": _t(c_uint8),
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1738
diff changeset
    40
    "ACTION":     _t(c_uint8),
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1738
diff changeset
    41
    "SINT":       _t(c_int8),
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1738
diff changeset
    42
    "USINT":      _t(c_uint8),
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1738
diff changeset
    43
    "BYTE":       _t(c_uint8),
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1738
diff changeset
    44
    "STRING":     (IEC_STRING,
3886
2adfa4c60bff Fix strings displaying as repr(bytes) in debug view.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3832
diff changeset
    45
                   lambda x: x.body[:x.len].decode(),
3823
a95b5ee1a0ea Runtime: py3 fix forcing of STRING
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3813
diff changeset
    46
                   lambda t, x: t(len(x), x.encode() if type(x)==str else x)),
1739
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1738
diff changeset
    47
    "INT":        _t(c_int16),
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1738
diff changeset
    48
    "UINT":       _t(c_uint16),
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1738
diff changeset
    49
    "WORD":       _t(c_uint16),
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1738
diff changeset
    50
    "DINT":       _t(c_int32),
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1738
diff changeset
    51
    "UDINT":      _t(c_uint32),
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1738
diff changeset
    52
    "DWORD":      _t(c_uint32),
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1738
diff changeset
    53
    "LINT":       _t(c_int64),
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1738
diff changeset
    54
    "ULINT":      _t(c_uint64),
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1738
diff changeset
    55
    "LWORD":      _t(c_uint64),
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1738
diff changeset
    56
    "REAL":       _t(c_float),
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1738
diff changeset
    57
    "LREAL":      _t(c_double),
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1738
diff changeset
    58
    "TIME":       _ttime(),
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1738
diff changeset
    59
    "TOD":        _ttime(),
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1738
diff changeset
    60
    "DATE":       _ttime(),
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1738
diff changeset
    61
    "DT":         _ttime(),
1433
4a45f6642523 Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents: 1075
diff changeset
    62
    }
592
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents:
diff changeset
    63
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents:
diff changeset
    64
SwapedEndianessTypeTranslator = {
1733
dea107dce0c4 clean-up: fix some PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1667
diff changeset
    65
    # TODO
1433
4a45f6642523 Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents: 1075
diff changeset
    66
    }
592
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents:
diff changeset
    67
1742
92932cd370a4 clean-up: fix PEP8 E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1741
diff changeset
    68
TypeTranslator = SameEndianessTypeTranslator
1075
8078c01ae464 Now Debug Buffer Unpacking is a separate function, declared in typemapping.py
Edouard Tisserant
parents: 972
diff changeset
    69
592
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents:
diff changeset
    70
# Construct debugger natively supported types
3750
f62625418bff automated conversion using 2to3-3.9 tool
GP Orcullo <kinsamanka@gmail.com>
parents: 3395
diff changeset
    71
DebugTypesSize = dict([(key, sizeof(t)) for key, (t, p, u) in SameEndianessTypeTranslator.items() if t is not None])
592
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents:
diff changeset
    72
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1733
diff changeset
    73
1433
4a45f6642523 Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents: 1075
diff changeset
    74
def UnpackDebugBuffer(buff, indexes):
1758
845ca626db09 clean-up: fix PEP8 E222 multiple spaces after operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1749
diff changeset
    75
    res = []
1433
4a45f6642523 Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents: 1075
diff changeset
    76
    buffoffset = 0
4a45f6642523 Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents: 1075
diff changeset
    77
    buffsize = len(buff)
3771
67a0df6478b3 ctypes fixes
GP Orcullo <kinsamanka@gmail.com>
parents: 3756
diff changeset
    78
    buffptr = cast(cast(buff, c_char_p), c_void_p).value
1433
4a45f6642523 Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents: 1075
diff changeset
    79
    for iectype in indexes:
3771
67a0df6478b3 ctypes fixes
GP Orcullo <kinsamanka@gmail.com>
parents: 3756
diff changeset
    80
        c_type, unpack_func, _pack_func = TypeTranslator.get(iectype,
67a0df6478b3 ctypes fixes
GP Orcullo <kinsamanka@gmail.com>
parents: 3756
diff changeset
    81
                                                             (None, None, None))
3395
93ad018fb602 RUNTIME: Variable forcing now uses limited list and buffer instead of systematical instance tree traversal and in-tree "fvalue" to keep track of forced value for pointed variables (external, located). Pointer swapping is performed when forcing externals and located, with backup being restored when forcing is reset. Retain still uses tree traversal.
Edouard Tisserant
parents: 2741
diff changeset
    82
93ad018fb602 RUNTIME: Variable forcing now uses limited list and buffer instead of systematical instance tree traversal and in-tree "fvalue" to keep track of forced value for pointed variables (external, located). Pointer swapping is performed when forcing externals and located, with backup being restored when forcing is reset. Retain still uses tree traversal.
Edouard Tisserant
parents: 2741
diff changeset
    83
        cursor = c_void_p(buffptr + buffoffset)
93ad018fb602 RUNTIME: Variable forcing now uses limited list and buffer instead of systematical instance tree traversal and in-tree "fvalue" to keep track of forced value for pointed variables (external, located). Pointer swapping is performed when forcing externals and located, with backup being restored when forcing is reset. Retain still uses tree traversal.
Edouard Tisserant
parents: 2741
diff changeset
    84
        if iectype == "STRING":
93ad018fb602 RUNTIME: Variable forcing now uses limited list and buffer instead of systematical instance tree traversal and in-tree "fvalue" to keep track of forced value for pointed variables (external, located). Pointer swapping is performed when forcing externals and located, with backup being restored when forcing is reset. Retain still uses tree traversal.
Edouard Tisserant
parents: 2741
diff changeset
    85
            # strlen is stored in c_uint8 and sizeof(c_uint8) is 1
93ad018fb602 RUNTIME: Variable forcing now uses limited list and buffer instead of systematical instance tree traversal and in-tree "fvalue" to keep track of forced value for pointed variables (external, located). Pointer swapping is performed when forcing externals and located, with backup being restored when forcing is reset. Retain still uses tree traversal.
Edouard Tisserant
parents: 2741
diff changeset
    86
            # first check we can read size
93ad018fb602 RUNTIME: Variable forcing now uses limited list and buffer instead of systematical instance tree traversal and in-tree "fvalue" to keep track of forced value for pointed variables (external, located). Pointer swapping is performed when forcing externals and located, with backup being restored when forcing is reset. Retain still uses tree traversal.
Edouard Tisserant
parents: 2741
diff changeset
    87
            if (buffoffset + 1) <= buffsize:
93ad018fb602 RUNTIME: Variable forcing now uses limited list and buffer instead of systematical instance tree traversal and in-tree "fvalue" to keep track of forced value for pointed variables (external, located). Pointer swapping is performed when forcing externals and located, with backup being restored when forcing is reset. Retain still uses tree traversal.
Edouard Tisserant
parents: 2741
diff changeset
    88
                size = 1 + cast(cursor,POINTER(c_type)).contents.len
93ad018fb602 RUNTIME: Variable forcing now uses limited list and buffer instead of systematical instance tree traversal and in-tree "fvalue" to keep track of forced value for pointed variables (external, located). Pointer swapping is performed when forcing externals and located, with backup being restored when forcing is reset. Retain still uses tree traversal.
Edouard Tisserant
parents: 2741
diff changeset
    89
            else:
93ad018fb602 RUNTIME: Variable forcing now uses limited list and buffer instead of systematical instance tree traversal and in-tree "fvalue" to keep track of forced value for pointed variables (external, located). Pointer swapping is performed when forcing externals and located, with backup being restored when forcing is reset. Retain still uses tree traversal.
Edouard Tisserant
parents: 2741
diff changeset
    90
                return None
93ad018fb602 RUNTIME: Variable forcing now uses limited list and buffer instead of systematical instance tree traversal and in-tree "fvalue" to keep track of forced value for pointed variables (external, located). Pointer swapping is performed when forcing externals and located, with backup being restored when forcing is reset. Retain still uses tree traversal.
Edouard Tisserant
parents: 2741
diff changeset
    91
        else:
93ad018fb602 RUNTIME: Variable forcing now uses limited list and buffer instead of systematical instance tree traversal and in-tree "fvalue" to keep track of forced value for pointed variables (external, located). Pointer swapping is performed when forcing externals and located, with backup being restored when forcing is reset. Retain still uses tree traversal.
Edouard Tisserant
parents: 2741
diff changeset
    92
            size = sizeof(c_type)
93ad018fb602 RUNTIME: Variable forcing now uses limited list and buffer instead of systematical instance tree traversal and in-tree "fvalue" to keep track of forced value for pointed variables (external, located). Pointer swapping is performed when forcing externals and located, with backup being restored when forcing is reset. Retain still uses tree traversal.
Edouard Tisserant
parents: 2741
diff changeset
    93
93ad018fb602 RUNTIME: Variable forcing now uses limited list and buffer instead of systematical instance tree traversal and in-tree "fvalue" to keep track of forced value for pointed variables (external, located). Pointer swapping is performed when forcing externals and located, with backup being restored when forcing is reset. Retain still uses tree traversal.
Edouard Tisserant
parents: 2741
diff changeset
    94
        if c_type is not None and (buffoffset + size) <= buffsize:
3830
c7cc292bb82e Runtime: fix unpacking of variable trace data
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3823
diff changeset
    95
            ptr = cast(cursor, POINTER(c_type))
c7cc292bb82e Runtime: fix unpacking of variable trace data
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3823
diff changeset
    96
            value = unpack_func(ptr.contents)
3395
93ad018fb602 RUNTIME: Variable forcing now uses limited list and buffer instead of systematical instance tree traversal and in-tree "fvalue" to keep track of forced value for pointed variables (external, located). Pointer swapping is performed when forcing externals and located, with backup being restored when forcing is reset. Retain still uses tree traversal.
Edouard Tisserant
parents: 2741
diff changeset
    97
            buffoffset += size
1433
4a45f6642523 Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents: 1075
diff changeset
    98
            res.append(value)
1075
8078c01ae464 Now Debug Buffer Unpacking is a separate function, declared in typemapping.py
Edouard Tisserant
parents: 972
diff changeset
    99
        else:
2741
3cc5663af196 IDE: Cleaned up some useless tests in variable trace data handling code, changed from bare numpy arrays to RingBuffers inorder to avoid RAM outage and crash after long tracing session.
Edouard Tisserant
parents: 1973
diff changeset
   100
            return None
1433
4a45f6642523 Moved trace buffer unpacking in the IDE. Latest traced variable samples are now passed as a single string
Edouard Tisserant
parents: 1075
diff changeset
   101
    if buffoffset and buffoffset == buffsize:
1075
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
3887
2df45e4bd500 Fix variable forcing. Now works with eRPC. PLCObject API changed.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3886
diff changeset
   104
2df45e4bd500 Fix variable forcing. Now works with eRPC. PLCObject API changed.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3886
diff changeset
   105
def ValueToIECBytes(iectype, value):
2df45e4bd500 Fix variable forcing. Now works with eRPC. PLCObject API changed.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3886
diff changeset
   106
    if value is None:
2df45e4bd500 Fix variable forcing. Now works with eRPC. PLCObject API changed.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3886
diff changeset
   107
        return None
2df45e4bd500 Fix variable forcing. Now works with eRPC. PLCObject API changed.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3886
diff changeset
   108
    c_type, _unpack_func, pack_func = TypeTranslator[iectype]
2df45e4bd500 Fix variable forcing. Now works with eRPC. PLCObject API changed.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3886
diff changeset
   109
    return bytes(pack_func(c_type, value))