author | greg |
Wed, 22 Oct 2008 14:22:54 +0200 | |
changeset 261 | 5299c6746fa8 |
parent 239 | 112b4bc523b3 |
child 269 | d29c5f71574f |
permissions | -rwxr-xr-x |
229 | 1 |
#!/usr/bin/env python |
2 |
# -*- coding: utf-8 -*- |
|
3 |
||
4 |
#This file is part of Beremiz, a Integrated Development Environment for |
|
5 |
#programming IEC 61131-3 automates supporting plcopen standard and CanFestival. |
|
6 |
# |
|
7 |
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD |
|
8 |
# |
|
9 |
#See COPYING file for copyrights details. |
|
10 |
# |
|
11 |
#This library is free software; you can redistribute it and/or |
|
12 |
#modify it under the terms of the GNU General Public |
|
13 |
#License as published by the Free Software Foundation; either |
|
14 |
#version 2.1 of the License, or (at your option) any later version. |
|
15 |
# |
|
16 |
#This library is distributed in the hope that it will be useful, |
|
17 |
#but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18 |
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
19 |
#General Public License for more details. |
|
20 |
# |
|
21 |
#You should have received a copy of the GNU General Public |
|
22 |
#License along with this library; if not, write to the Free Software |
|
23 |
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
24 |
||
25 |
import Pyro.core as pyro |
|
26 |
from threading import Timer |
|
27 |
import ctypes, os, commands |
|
28 |
||
29 |
if os.name in ("nt", "ce"): |
|
30 |
from _ctypes import LoadLibrary as dlopen |
|
31 |
from _ctypes import FreeLibrary as dlclose |
|
32 |
elif os.name == "posix": |
|
33 |
from _ctypes import dlopen, dlclose |
|
34 |
||
35 |
import os,sys,traceback |
|
36 |
||
37 |
lib_ext ={ |
|
38 |
"linux2":".so", |
|
39 |
"win32":".dll", |
|
40 |
}.get(sys.platform, "") |
|
41 |
||
42 |
class PLCObject(pyro.ObjBase): |
|
235 | 43 |
_Idxs = [] |
229 | 44 |
def __init__(self, workingdir, daemon, argv): |
45 |
pyro.ObjBase.__init__(self) |
|
46 |
self.argv = [workingdir] + argv # force argv[0] to be "path" to exec... |
|
47 |
self.workingdir = workingdir |
|
48 |
self.PLCStatus = "Stopped" |
|
49 |
self.PLClibraryHandle = None |
|
50 |
# Creates fake C funcs proxies |
|
51 |
self._FreePLC() |
|
52 |
self.daemon = daemon |
|
53 |
||
54 |
# Get the last transfered PLC if connector must be restart |
|
55 |
try: |
|
56 |
self.CurrentPLCFilename=open( |
|
57 |
self._GetMD5FileName(), |
|
58 |
"r").read().strip() + lib_ext |
|
59 |
except Exception, e: |
|
60 |
self.PLCStatus = "Empty" |
|
61 |
self.CurrentPLCFilename=None |
|
62 |
||
63 |
def _GetMD5FileName(self): |
|
64 |
return os.path.join(self.workingdir, "lasttransferedPLC.md5") |
|
65 |
||
66 |
def _GetLibFileName(self): |
|
67 |
return os.path.join(self.workingdir,self.CurrentPLCFilename) |
|
68 |
||
69 |
||
70 |
def _LoadNewPLC(self): |
|
71 |
""" |
|
72 |
Load PLC library |
|
73 |
Declare all functions, arguments and return values |
|
74 |
""" |
|
75 |
print "Load PLC" |
|
76 |
try: |
|
77 |
self._PLClibraryHandle = dlopen(self._GetLibFileName()) |
|
78 |
self.PLClibraryHandle = ctypes.CDLL(self.CurrentPLCFilename, handle=self._PLClibraryHandle) |
|
79 |
||
80 |
self._startPLC = self.PLClibraryHandle.startPLC |
|
81 |
self._startPLC.restype = ctypes.c_int |
|
82 |
self._startPLC.argtypes = [ctypes.c_int, ctypes.POINTER(ctypes.c_char_p)] |
|
83 |
||
84 |
self._stopPLC = self.PLClibraryHandle.stopPLC |
|
85 |
self._stopPLC.restype = None |
|
86 |
||
87 |
self._ResetDebugVariables = self.PLClibraryHandle.ResetDebugVariables |
|
88 |
self._ResetDebugVariables.restype = None |
|
89 |
||
235 | 90 |
self._RegisterDebugVariable = self.PLClibraryHandle.RegisterDebugVariable |
229 | 91 |
self._RegisterDebugVariable.restype = None |
235 | 92 |
self._RegisterDebugVariable.argtypes = [ctypes.c_int] |
229 | 93 |
|
94 |
self._IterDebugData = self.PLClibraryHandle.IterDebugData |
|
95 |
self._IterDebugData.restype = ctypes.c_void_p |
|
96 |
self._IterDebugData.argtypes = [ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_char_p)] |
|
97 |
||
98 |
self._FreeDebugData = self.PLClibraryHandle.FreeDebugData |
|
99 |
self._FreeDebugData.restype = None |
|
100 |
||
101 |
self._WaitDebugData = self.PLClibraryHandle.WaitDebugData |
|
102 |
self._WaitDebugData.restype = ctypes.c_int |
|
235 | 103 |
|
104 |
self._suspendDebug = self.PLClibraryHandle.suspendDebug |
|
105 |
self._suspendDebug.restype = None |
|
106 |
||
107 |
self._resumeDebug = self.PLClibraryHandle.resumeDebug |
|
108 |
self._resumeDebug.restype = None |
|
109 |
||
229 | 110 |
return True |
111 |
except: |
|
112 |
print traceback.format_exc() |
|
113 |
return False |
|
114 |
||
115 |
def _FreePLC(self): |
|
116 |
""" |
|
117 |
Unload PLC library. |
|
118 |
This is also called by __init__ to create dummy C func proxies |
|
119 |
""" |
|
120 |
# Forget all refs to library |
|
121 |
self._startPLC = lambda:None |
|
122 |
self._stopPLC = lambda:None |
|
123 |
self._ResetDebugVariables = lambda:None |
|
124 |
self._RegisterDebugVariable = lambda x:None |
|
125 |
self._IterDebugData = lambda x,y:None |
|
126 |
self._FreeDebugData = lambda:None |
|
235 | 127 |
self._WaitDebugData = lambda:-1 |
128 |
self._suspendDebug = lambda:None |
|
129 |
self._resumeDebug = lambda:None |
|
229 | 130 |
self.PLClibraryHandle = None |
131 |
# Unload library explicitely |
|
132 |
if getattr(self,"_PLClibraryHandle",None) is not None: |
|
133 |
print "Unload PLC" |
|
134 |
dlclose(self._PLClibraryHandle) |
|
135 |
res = self._DetectDirtyLibs() |
|
136 |
else: |
|
137 |
res = False |
|
138 |
||
139 |
self._PLClibraryHandle = None |
|
140 |
||
141 |
return res |
|
142 |
||
143 |
def _DetectDirtyLibs(self): |
|
144 |
# Detect dirty libs |
|
145 |
# Get lib dependencies (for dirty lib detection) |
|
146 |
if os.name == "posix": |
|
147 |
# parasiting libs listed with ldd |
|
148 |
badlibs = [ toks.split()[0] for toks in commands.getoutput( |
|
149 |
"ldd "+self._GetLibFileName()).splitlines() ] |
|
150 |
for badlib in badlibs: |
|
151 |
if badlib[:6] in ["libwx_", |
|
152 |
"libwxs", |
|
153 |
"libgtk", |
|
154 |
"libgdk", |
|
155 |
"libatk", |
|
156 |
"libpan", |
|
157 |
"libX11", |
|
158 |
]: |
|
159 |
#badhandle = dlopen(badlib, dl.RTLD_NOLOAD) |
|
160 |
print "Dirty lib detected :" + badlib |
|
161 |
#dlclose(badhandle) |
|
162 |
return True |
|
163 |
return False |
|
164 |
||
165 |
||
235 | 166 |
def StartPLC(self, debug=False): |
229 | 167 |
print "StartPLC" |
168 |
if self.CurrentPLCFilename is not None and self.PLCStatus == "Stopped": |
|
169 |
c_argv = ctypes.c_char_p * len(self.argv) |
|
170 |
if self._LoadNewPLC() and self._startPLC(len(self.argv),c_argv(*self.argv)) == 0: |
|
235 | 171 |
if debug: |
172 |
self._resumeDebug() |
|
229 | 173 |
self.PLCStatus = "Started" |
174 |
return True |
|
175 |
else: |
|
176 |
print "_StartPLC did not return 0 !" |
|
177 |
self._DoStopPLC() |
|
178 |
return False |
|
179 |
||
180 |
def _DoStopPLC(self): |
|
181 |
self._stopPLC() |
|
182 |
self.PLCStatus = "Stopped" |
|
183 |
if self._FreePLC(): |
|
184 |
self.PLCStatus = "Dirty" |
|
185 |
return True |
|
186 |
||
187 |
def StopPLC(self): |
|
188 |
if self.PLCStatus == "Started": |
|
189 |
self._DoStopPLC() |
|
190 |
return True |
|
191 |
return False |
|
192 |
||
193 |
def _Reload(self): |
|
194 |
self.daemon.shutdown(True) |
|
195 |
self.daemon.sock.close() |
|
196 |
os.execv(sys.executable,[sys.executable]+sys.argv[:]) |
|
197 |
# never reached |
|
198 |
return 0 |
|
199 |
||
200 |
def ForceReload(self): |
|
201 |
# respawn python interpreter |
|
202 |
Timer(0.1,self._Reload).start() |
|
203 |
return True |
|
204 |
||
205 |
def GetPLCstatus(self): |
|
206 |
return self.PLCStatus |
|
207 |
||
208 |
def NewPLC(self, md5sum, data, extrafiles): |
|
209 |
print "NewPLC (%s)"%md5sum |
|
210 |
if self.PLCStatus in ["Stopped", "Empty", "Dirty"]: |
|
211 |
NewFileName = md5sum + lib_ext |
|
212 |
extra_files_log = os.path.join(self.workingdir,"extra_files.txt") |
|
213 |
try: |
|
214 |
os.remove(os.path.join(self.workingdir, |
|
215 |
self.CurrentPLCFilename)) |
|
216 |
for filename in file(extra_files_log, "r").readlines() + extra_files_log: |
|
217 |
try: |
|
218 |
os.remove(os.path.join(self.workingdir, filename)) |
|
219 |
except: |
|
220 |
pass |
|
221 |
except: |
|
222 |
pass |
|
223 |
||
224 |
try: |
|
225 |
# Create new PLC file |
|
226 |
open(os.path.join(self.workingdir,NewFileName), |
|
227 |
'wb').write(data) |
|
228 |
||
229 |
# Store new PLC filename based on md5 key |
|
230 |
open(self._GetMD5FileName(), "w").write(md5sum) |
|
231 |
||
232 |
# Then write the files |
|
233 |
log = file(extra_files_log, "w") |
|
234 |
for fname,fdata in extrafiles: |
|
235 |
fpath = os.path.join(self.workingdir,fname) |
|
236 |
open(fpath, "wb").write(fdata) |
|
237 |
log.write(fname+'\n') |
|
238 |
||
239 |
# Store new PLC filename |
|
240 |
self.CurrentPLCFilename = NewFileName |
|
241 |
except: |
|
242 |
print traceback.format_exc() |
|
243 |
return False |
|
244 |
if self.PLCStatus == "Empty": |
|
245 |
self.PLCStatus = "Stopped" |
|
246 |
return True |
|
247 |
return False |
|
248 |
||
249 |
def MatchMD5(self, MD5): |
|
250 |
try: |
|
251 |
last_md5 = open(self._GetMD5FileName(), "r").read() |
|
252 |
return last_md5 == MD5 |
|
253 |
except: |
|
254 |
return False |
|
255 |
||
256 |
def SetTraceVariablesList(self, idxs): |
|
257 |
""" |
|
258 |
Call ctype imported function to append |
|
259 |
these indexes to registred variables in PLC debugger |
|
260 |
""" |
|
235 | 261 |
self._suspendDebug() |
229 | 262 |
# keep a copy of requested idx |
263 |
self._Idxs = idxs[:] |
|
264 |
self._ResetDebugVariables() |
|
265 |
for idx in idxs: |
|
266 |
self._RegisterDebugVariable(idx) |
|
235 | 267 |
self._resumeDebug() |
229 | 268 |
|
238 | 269 |
TypeTranslator = {"BOOL" : (ctypes.c_uint8, lambda x:x.value!=0), |
270 |
"STEP" : (ctypes.c_uint8, lambda x:x.value), |
|
271 |
"TRANSITION" : (ctypes.c_uint8, lambda x:x.value), |
|
272 |
"ACTION" : (ctypes.c_uint8, lambda x:x.value), |
|
273 |
"SINT" : (ctypes.c_int8, lambda x:x.value), |
|
274 |
"USINT" : (ctypes.c_uint8, lambda x:x.value), |
|
275 |
"BYTE" : (ctypes.c_uint8, lambda x:x.value), |
|
276 |
"STRING" : (None, None),#TODO |
|
277 |
"INT" : (ctypes.c_int16, lambda x:x.value), |
|
278 |
"UINT" : (ctypes.c_uint16, lambda x:x.value), |
|
279 |
"WORD" : (ctypes.c_uint16, lambda x:x.value), |
|
280 |
"WSTRING" : (None, None),#TODO |
|
281 |
"DINT" : (ctypes.c_int32, lambda x:x.value), |
|
282 |
"UDINT" : (ctypes.c_uint32, lambda x:x.value), |
|
283 |
"DWORD" : (ctypes.c_uint32, lambda x:x.value), |
|
284 |
"LINT" : (ctypes.c_int64, lambda x:x.value), |
|
285 |
"ULINT" : (ctypes.c_uint64, lambda x:x.value), |
|
286 |
"LWORD" : (ctypes.c_uint64, lambda x:x.value), |
|
287 |
"REAL" : (ctypes.c_float, lambda x:x.value), |
|
288 |
"LREAL" : (ctypes.c_double, lambda x:x.value), |
|
229 | 289 |
} |
290 |
||
291 |
def GetTraceVariables(self): |
|
292 |
""" |
|
293 |
Return a list of variables, corresponding to the list of requiered idx |
|
294 |
""" |
|
295 |
tick = self._WaitDebugData() |
|
235 | 296 |
if tick == -1: |
237 | 297 |
res = None |
298 |
else: |
|
299 |
idx = ctypes.c_int() |
|
300 |
typename = ctypes.c_char_p() |
|
301 |
res = [] |
|
302 |
||
303 |
for given_idx in self._Idxs: |
|
304 |
buffer=self._IterDebugData(ctypes.byref(idx), ctypes.byref(typename)) |
|
239
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
238
diff
changeset
|
305 |
c_type,unpack_func = self.TypeTranslator.get(typename.value, (None,None)) |
237 | 306 |
if c_type is not None and given_idx == idx.value: |
239
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
238
diff
changeset
|
307 |
res.append(unpack_func(ctypes.cast(buffer, |
238 | 308 |
ctypes.POINTER(c_type)).contents)) |
237 | 309 |
else: |
310 |
print "Debug error idx : %d, expected_idx %d, type : %s"%(idx.value, given_idx,typename.value) |
|
311 |
res.append(None) |
|
229 | 312 |
self._FreeDebugData() |
235 | 313 |
return tick, res |
229 | 314 |
|
315 |
||
316 |