Initial TIME support in debugger
authorEdouard Tisserant
Thu, 31 Mar 2011 19:09:49 +0200
changeset 592 c6408f92da0a
parent 591 3ece9ba72aaf
child 593 726f58cf97e3
Initial TIME support in debugger
connectors/LPC/LPCAppObject.py
plugger.py
runtime/PLCObject.py
targets/Linux/plc_Linux_main.c
targets/typemapping.py
--- a/connectors/LPC/LPCAppObject.py	Thu Mar 31 19:04:03 2011 +0200
+++ b/connectors/LPC/LPCAppObject.py	Thu Mar 31 19:09:49 2011 +0200
@@ -25,6 +25,7 @@
 import ctypes
 from LPCAppProto import *
 from LPCObject import *
+from targets.typemapping import SameEndianessTypeTranslator as TypeTranslator
 
 class LPCAppObject(LPCObject):
     def connect(self,comport):
--- a/plugger.py	Thu Mar 31 19:04:03 2011 +0200
+++ b/plugger.py	Thu Mar 31 19:09:49 2011 +0200
@@ -680,29 +680,11 @@
 from TextViewer import TextViewer
 from plcopen.structures import IEC_KEYWORDS, TypeHierarchy_list
 
-# Construct debugger natively supported types
-DebugTypes = [t for t in zip(*TypeHierarchy_list)[0] if not t.startswith("ANY")]
-DebugTypesSize =  {"BOOL" :       1,
-                   "SINT" :       1,
-                   "USINT" :      1,
-                   "BYTE" :       1,
-                   "STRING" :     128,
-                   "INT" :        2,
-                   "UINT" :       2,
-                   "WORD" :       2,
-                   "WSTRING" :    0, #TODO
-                   "DINT" :       4,
-                   "UDINT" :      4,
-                   "DWORD" :      4,
-                   "LINT" :       4,
-                   "ULINT" :      8,
-                   "LWORD" :      8,
-                   "REAL" :       4,
-                   "LREAL" :      8,
-                  } 
 
 import re, tempfile
 import targets
+from targets.typemapping import DebugTypesSize
+
 import connectors
 from discovery import DiscoveryDialog
 from weakref import WeakKeyDictionary
@@ -1346,7 +1328,7 @@
                 "IN":"    (*fp)((void*)&%(C_path)s,%(type)s_P_ENUM);\n",
                 "OUT":"    (*fp)((void*)&%(C_path)s,%(type)s_O_ENUM);\n",
                 "VAR":"    (*fp)((void*)&%(C_path)s,%(type)s_ENUM);\n"}[v["vartype"]]%v
-                for v in self._VariablesList if v["vartype"] != "FB" and v["type"] in DebugTypes ]),
+                for v in self._VariablesList if v["vartype"] != "FB" and v["type"] in DebugTypesSize ]),
            "find_variable_case_code":"\n".join([
                "    case %(num)s:\n"%v+
                "        *varp = (void*)&%(C_path)s;\n"%v+
@@ -1354,7 +1336,7 @@
                 "IN":"        return %(type)s_P_ENUM;\n",
                 "OUT":"        return %(type)s_O_ENUM;\n",
                 "VAR":"        return %(type)s_ENUM;\n"}[v["vartype"]]%v
-                for v in self._VariablesList if v["vartype"] != "FB" and v["type"] in DebugTypes ])}
+                for v in self._VariablesList if v["vartype"] != "FB" and v["type"] in DebugTypesSize ])}
         
         return debug_code
         
@@ -1604,7 +1586,10 @@
                     # Convert 
                     Idx, IEC_Type = self._IECPathToIdx.get(IECPath,(None,None))
                     if Idx is not None:
-                        Idxs.append((Idx, IEC_Type, fvalue, IECPath))
+                        if IEC_Type in DebugTypesSize: 
+                            Idxs.append((Idx, IEC_Type, fvalue, IECPath))
+                        else:
+                            self.logger.write_warning(_("Debug : Unsuppoted type to debug %s\n")%IEC_Type)
                     else:
                         self.logger.write_warning(_("Debug : Unknown variable %s\n")%IECPath)
             for IECPathToPop in IECPathsToPop:
--- a/runtime/PLCObject.py	Thu Mar 31 19:04:03 2011 +0200
+++ b/runtime/PLCObject.py	Thu Mar 31 19:09:49 2011 +0200
@@ -25,6 +25,8 @@
 import Pyro.core as pyro
 from threading import Timer, Thread, Lock
 import ctypes, os, commands, types, sys
+from datetime import timedelta as td
+from targets.typemapping import SameEndianessTypeTranslator as TypeTranslator
 
 if os.name in ("nt", "ce"):
     from _ctypes import LoadLibrary as dlopen
@@ -320,35 +322,7 @@
             return False
     
 
-    class IEC_STRING(ctypes.Structure):
-        """
-        Must be changed according to changes in iec_types.h
-        """
-        _fields_ = [("len", ctypes.c_uint8),
-                    ("body", ctypes.c_char * 126)] 
-    
-    TypeTranslator = {"BOOL" :       (ctypes.c_uint8,  lambda x:x.value!=0,     lambda t,x:t(x)),
-                      "STEP" :       (ctypes.c_uint8,  lambda x:x.value,        lambda t,x:t(x)),
-                      "TRANSITION" : (ctypes.c_uint8,  lambda x:x.value,        lambda t,x:t(x)),
-                      "ACTION" :     (ctypes.c_uint8,  lambda x:x.value,        lambda t,x:t(x)),
-                      "SINT" :       (ctypes.c_int8,   lambda x:x.value,        lambda t,x:t(x)),
-                      "USINT" :      (ctypes.c_uint8,  lambda x:x.value,        lambda t,x:t(x)),
-                      "BYTE" :       (ctypes.c_uint8,  lambda x:x.value,        lambda t,x:t(x)),
-                      "STRING" :     (IEC_STRING,      lambda x:x.body[:x.len], lambda t,x:t(len(x),x)),
-                      "INT" :        (ctypes.c_int16,  lambda x:x.value,        lambda t,x:t(x)),
-                      "UINT" :       (ctypes.c_uint16, lambda x:x.value,        lambda t,x:t(x)),
-                      "WORD" :       (ctypes.c_uint16, lambda x:x.value,        lambda t,x:t(x)),
-                      "WSTRING" :    (None,            None,                    None),#TODO
-                      "DINT" :       (ctypes.c_int32,  lambda x:x.value,        lambda t,x:t(x)),
-                      "UDINT" :      (ctypes.c_uint32, lambda x:x.value,        lambda t,x:t(x)),
-                      "DWORD" :      (ctypes.c_uint32, lambda x:x.value,        lambda t,x:t(x)),
-                      "LINT" :       (ctypes.c_int64,  lambda x:x.value,        lambda t,x:t(x)),
-                      "ULINT" :      (ctypes.c_uint64, lambda x:x.value,        lambda t,x:t(x)),
-                      "LWORD" :      (ctypes.c_uint64, lambda x:x.value,        lambda t,x:t(x)),
-                      "REAL" :       (ctypes.c_float,  lambda x:x.value,        lambda t,x:t(x)),
-                      "LREAL" :      (ctypes.c_double, lambda x:x.value,        lambda t,x:t(x)),
-                      } 
-
+    
     def SetTraceVariablesList(self, idxs):
         """
         Call ctype imported function to append 
@@ -392,7 +366,7 @@
                         c_type,unpack_func, pack_func = \
                             self.TypeTranslator.get(iectype,
                                                     (None,None,None))
-                        if c_type is not None and offset < size:
+                        if c_type is not None and offset < size.value:
                             res.append(unpack_func(
                                         ctypes.cast(cursor,
                                          ctypes.POINTER(c_type)).contents))
@@ -401,14 +375,14 @@
                             if c_type is None:
                                 PLCprint("Debug error - " + iectype +
                                          " not supported !")
-                            if offset >= size:
-                                PLCprint("Debug error - buffer too small !")
+                            #if offset >= size.value:
+                                #PLCprint("Debug error - buffer too small ! %d != %d"%(offset, size.value))
                             break
                 self._FreeDebugData()
                 self.PLClibraryLock.release()
             if offset and offset == size.value:
                 return self.PLCStatus, tick.value, res
-            elif size.value:
-                PLCprint("Debug error - wrong buffer unpack !")
-        return self.PLCStatus, None, None
-
+            #elif size.value:
+                #PLCprint("Debug error - wrong buffer unpack ! %d != %d"%(offset, size.value))
+        return self.PLCStatus, None, []
+
--- a/targets/Linux/plc_Linux_main.c	Thu Mar 31 19:04:03 2011 +0200
+++ b/targets/Linux/plc_Linux_main.c	Thu Mar 31 19:09:49 2011 +0200
@@ -20,7 +20,10 @@
 
 void PLC_GetTime(IEC_TIME *CURRENT_TIME)
 {
-    clock_gettime(CLOCK_REALTIME, CURRENT_TIME);
+    struct timespec tmp;
+    clock_gettime(CLOCK_REALTIME, &tmp);
+    CURRENT_TIME->tv_sec = tmp.tv_sec;
+    CURRENT_TIME->tv_nsec = tmp.tv_nsec;
 }
 
 void PLC_timer_notify(sigval_t val)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/typemapping.py	Thu Mar 31 19:09:49 2011 +0200
@@ -0,0 +1,73 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+#
+#Copyright (C) 2011: Edouard TISSERANT and Laurent BESSARD
+#
+#See COPYING file for copyrights details.
+#
+#This library is free software; you can redistribute it and/or
+#modify it under the terms of the GNU General Public
+#License as published by the Free Software Foundation; either
+#version 2.1 of the License, or (at your option) any later version.
+#
+#This library is distributed in the hope that it will be useful,
+#but WITHOUT ANY WARRANTY; without even the implied warranty of
+#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+#General Public License for more details.
+#
+#You should have received a copy of the GNU General Public
+#License along with this library; if not, write to the Free Software
+#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+from ctypes import *
+
+class IEC_STRING(Structure):
+    """
+    Must be changed according to changes in iec_types.h
+    """
+    _fields_ = [("len", c_uint8),
+                ("body", c_char * 126)] 
+
+class IEC_TIME(Structure):
+    """
+    Must be changed according to changes in iec_types.h
+    """
+    _fields_ = [("s", c_long), #tv_sec
+                ("ns", c_long)] #tv_nsec
+
+def _t(t, u=lambda x:x.value, p=lambda t,x:t(x)): return  (t, u, p)
+
+SameEndianessTypeTranslator = {
+    "BOOL" :       _t(c_uint8,  lambda x:x.value!=0),
+    "STEP" :       _t(c_uint8),
+    "TRANSITION" : _t(c_uint8),
+    "ACTION" :     _t(c_uint8),
+    "SINT" :       _t(c_int8),
+    "USINT" :      _t(c_uint8),
+    "BYTE" :       _t(c_uint8),
+    "STRING" :     _t(IEC_STRING, 
+                      lambda x:x.body[:x.len], 
+                      lambda t,x:t(len(x),x)),
+    "INT" :        _t(c_int16),
+    "UINT" :       _t(c_uint16),
+    "WORD" :       _t(c_uint16),
+    "DINT" :       _t(c_int32),
+    "UDINT" :      _t(c_uint32),
+    "DWORD" :      _t(c_uint32),
+    "LINT" :       _t(c_int64),
+    "ULINT" :      _t(c_uint64),
+    "LWORD" :      _t(c_uint64),
+    "REAL" :       _t(c_float),
+    "LREAL" :      _t(c_double),
+    "TIME" :       _t(IEC_TIME, 
+                      lambda x:td(0, x.s, x.ns/1000), 
+                      lambda t,x:t(x.seconds, x.microseconds*1000)),
+    } 
+
+SwapedEndianessTypeTranslator = {
+    #TODO
+    } 
+
+# Construct debugger natively supported types
+DebugTypesSize =  dict([(key,sizeof(t)) for key,(t,p,u) in SameEndianessTypeTranslator.iteritems() if t is not None])
+