author | etisserant |
Fri, 22 Aug 2008 15:30:09 +0200 | |
changeset 217 | f3eb35df4d87 |
parent 216 | 11124e129a28 |
child 219 | 43d65f0179e2 |
permissions | -rwxr-xr-x |
203 | 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 |
|
209
08dc3d064cb5
Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
203
diff
changeset
|
27 |
import ctypes, os, commands |
203 | 28 |
|
29 |
if os.name == ("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): |
|
209
08dc3d064cb5
Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
203
diff
changeset
|
43 |
def __init__(self, workingdir, daemon, argv): |
203 | 44 |
pyro.ObjBase.__init__(self) |
209
08dc3d064cb5
Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
203
diff
changeset
|
45 |
self.argv=argv |
203 | 46 |
self.workingdir = workingdir |
47 |
self.PLCStatus = "Stopped" |
|
48 |
self.PLClibraryHandle = None |
|
49 |
# Creates fake C funcs proxies |
|
50 |
self._FreePLC() |
|
51 |
self.daemon = daemon |
|
52 |
||
53 |
# Get the last transfered PLC if connector must be restart |
|
54 |
try: |
|
55 |
self.CurrentPLCFilename=open( |
|
56 |
self._GetMD5FileName(), |
|
57 |
"r").read().strip() + lib_ext |
|
58 |
except Exception, e: |
|
59 |
self.PLCStatus = "Empty" |
|
60 |
self.CurrentPLCFilename=None |
|
61 |
||
62 |
def _GetMD5FileName(self): |
|
63 |
return os.path.join(self.workingdir, "lasttransferedPLC.md5") |
|
64 |
||
65 |
def _GetLibFileName(self): |
|
66 |
return os.path.join(self.workingdir,self.CurrentPLCFilename) |
|
67 |
||
68 |
||
69 |
def _LoadNewPLC(self): |
|
70 |
""" |
|
71 |
Load PLC library |
|
72 |
Declare all functions, arguments and return values |
|
73 |
""" |
|
74 |
print "Load PLC" |
|
75 |
try: |
|
76 |
self._PLClibraryHandle = dlopen(self._GetLibFileName()) |
|
77 |
self.PLClibraryHandle = ctypes.CDLL(self.CurrentPLCFilename, handle=self._PLClibraryHandle) |
|
78 |
||
79 |
self._startPLC = self.PLClibraryHandle.startPLC |
|
80 |
self._startPLC.restype = ctypes.c_int |
|
81 |
self._startPLC.argtypes = [ctypes.c_int, ctypes.POINTER(ctypes.c_char_p)] |
|
82 |
||
83 |
self._stopPLC = self.PLClibraryHandle.stopPLC |
|
84 |
self._stopPLC.restype = None |
|
85 |
||
86 |
self._ResetDebugVariables = self.PLClibraryHandle.ResetDebugVariables |
|
87 |
self._ResetDebugVariables.restype = None |
|
88 |
||
89 |
self._RegisterDebugVariable = self.PLClibraryHandle.ResetDebugVariables |
|
90 |
self._RegisterDebugVariable.restype = None |
|
91 |
||
92 |
self._IterDebugData = self.PLClibraryHandle.IterDebugData |
|
93 |
self._IterDebugData.restype = ctypes.c_void_p |
|
94 |
self._IterDebugData.argtypes = [ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_char_p)] |
|
95 |
||
96 |
self._FreeDebugData = self.PLClibraryHandle.FreeDebugData |
|
97 |
self._FreeDebugData.restype = None |
|
98 |
return True |
|
99 |
except: |
|
100 |
print traceback.format_exc() |
|
101 |
return False |
|
102 |
||
103 |
def _FreePLC(self): |
|
104 |
""" |
|
105 |
Unload PLC library. |
|
106 |
This is also called by __init__ to create dummy C func proxies |
|
107 |
""" |
|
108 |
# Forget all refs to library |
|
109 |
self._startPLC = lambda:None |
|
110 |
self._stopPLC = lambda:None |
|
111 |
self._ResetDebugVariables = lambda:None |
|
112 |
self._RegisterDebugVariable = lambda x:None |
|
113 |
self._IterDebugData = lambda x,y:None |
|
114 |
self._FreeDebugData = lambda:None |
|
115 |
self.PLClibraryHandle = None |
|
116 |
# Unload library explicitely |
|
117 |
if getattr(self,"_PLClibraryHandle",None) is not None: |
|
118 |
print "Unload PLC" |
|
119 |
dlclose(self._PLClibraryHandle) |
|
120 |
res = self._DetectDirtyLibs() |
|
121 |
else: |
|
122 |
res = False |
|
123 |
||
124 |
self._PLClibraryHandle = None |
|
125 |
||
126 |
return res |
|
127 |
||
128 |
def _DetectDirtyLibs(self): |
|
129 |
# Detect dirty libs |
|
130 |
# Get lib dependencies (for dirty lib detection) |
|
131 |
if os.name == "posix": |
|
132 |
# parasiting libs listed with ldd |
|
133 |
badlibs = [ toks.split()[0] for toks in commands.getoutput( |
|
134 |
"ldd "+self._GetLibFileName()).splitlines() ] |
|
135 |
for badlib in badlibs: |
|
136 |
if badlib[:6] in ["libwx_", |
|
137 |
"libwxs", |
|
138 |
"libgtk", |
|
139 |
"libgdk", |
|
140 |
"libatk", |
|
141 |
"libpan", |
|
142 |
"libX11", |
|
143 |
]: |
|
209
08dc3d064cb5
Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
203
diff
changeset
|
144 |
#badhandle = dlopen(badlib, dl.RTLD_NOLOAD) |
203 | 145 |
print "Dirty lib detected :" + badlib |
146 |
#dlclose(badhandle) |
|
147 |
return True |
|
148 |
return False |
|
149 |
||
150 |
||
151 |
def StartPLC(self): |
|
152 |
print "StartPLC" |
|
153 |
if self.CurrentPLCFilename is not None and self.PLCStatus == "Stopped": |
|
154 |
c_argv = ctypes.c_char_p * len(sys.argv) |
|
209
08dc3d064cb5
Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
203
diff
changeset
|
155 |
if self._LoadNewPLC() and self._startPLC(len(self.argv),c_argv(*self.argv)) == 0: |
203 | 156 |
self.PLCStatus = "Started" |
157 |
return True |
|
158 |
else: |
|
159 |
print "_StartPLC did not return 0 !" |
|
209
08dc3d064cb5
Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
203
diff
changeset
|
160 |
self._DoStopPLC() |
08dc3d064cb5
Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
203
diff
changeset
|
161 |
return False |
08dc3d064cb5
Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
203
diff
changeset
|
162 |
|
08dc3d064cb5
Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
203
diff
changeset
|
163 |
def _DoStopPLC(self): |
08dc3d064cb5
Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
203
diff
changeset
|
164 |
self._stopPLC() |
08dc3d064cb5
Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
203
diff
changeset
|
165 |
self.PLCStatus = "Stopped" |
08dc3d064cb5
Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
203
diff
changeset
|
166 |
if self._FreePLC(): |
08dc3d064cb5
Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
203
diff
changeset
|
167 |
self.PLCStatus = "Dirty" |
08dc3d064cb5
Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
203
diff
changeset
|
168 |
return True |
203 | 169 |
|
170 |
def StopPLC(self): |
|
171 |
if self.PLCStatus == "Started": |
|
209
08dc3d064cb5
Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
203
diff
changeset
|
172 |
self._DoStopPLC() |
216 | 173 |
return True |
203 | 174 |
return False |
175 |
||
176 |
def _Reload(self): |
|
177 |
self.daemon.shutdown(True) |
|
178 |
self.daemon.sock.close() |
|
179 |
os.execv(sys.executable,[sys.executable]+sys.argv[:]) |
|
180 |
# never reached |
|
181 |
return 0 |
|
182 |
||
183 |
def ForceReload(self): |
|
184 |
# respawn python interpreter |
|
185 |
Timer(0.1,self._Reload).start() |
|
186 |
return True |
|
187 |
||
188 |
def GetPLCstatus(self): |
|
189 |
return self.PLCStatus |
|
190 |
||
191 |
def NewPLC(self, md5sum, data, extrafiles): |
|
192 |
print "NewPLC (%s)"%md5sum |
|
193 |
if self.PLCStatus in ["Stopped", "Empty", "Dirty"]: |
|
194 |
NewFileName = md5sum + lib_ext |
|
195 |
extra_files_log = os.path.join(self.workingdir,"extra_files.txt") |
|
196 |
try: |
|
197 |
os.remove(os.path.join(self.workingdir, |
|
198 |
self.CurrentPLCFilename)) |
|
199 |
for filename in file(extra_files_log, "r").readlines() + extra_files_log: |
|
200 |
try: |
|
201 |
os.remove(os.path.join(self.workingdir, filename)) |
|
202 |
except: |
|
203 |
pass |
|
204 |
except: |
|
205 |
pass |
|
206 |
||
207 |
try: |
|
208 |
# Create new PLC file |
|
209 |
open(os.path.join(self.workingdir,NewFileName), |
|
210 |
'wb').write(data) |
|
211 |
||
212 |
# Store new PLC filename based on md5 key |
|
213 |
open(self._GetMD5FileName(), "w").write(md5sum) |
|
214 |
||
215 |
# Then write the files |
|
216 |
log = file(extra_files_log, "w") |
|
217 |
for fname,fdata in extrafiles: |
|
218 |
fpath = os.path.join(self.workingdir,fname) |
|
219 |
open(fpath, "wb").write(fdata) |
|
220 |
log.write(fname+'\n') |
|
221 |
||
222 |
# Store new PLC filename |
|
223 |
self.CurrentPLCFilename = NewFileName |
|
224 |
except: |
|
225 |
print traceback.format_exc() |
|
226 |
return False |
|
227 |
if self.PLCStatus == "Empty": |
|
228 |
self.PLCStatus = "Stopped" |
|
229 |
return True |
|
230 |
return False |
|
231 |
||
232 |
def MatchMD5(self, MD5): |
|
233 |
try: |
|
234 |
last_md5 = open(self._GetMD5FileName(), "r").read() |
|
235 |
return last_md5 == MD5 |
|
236 |
except: |
|
237 |
return False |
|
238 |
||
239 |
def SetTraceVariablesList(self, idxs): |
|
240 |
""" |
|
241 |
Call ctype imported function to append |
|
242 |
these indexes to registred variables in PLC debugger |
|
243 |
""" |
|
244 |
# keep a copy of requested idx |
|
245 |
self._Idxs = idxs[:] |
|
246 |
self._ResetDebugVariables() |
|
247 |
for idx in idxs: |
|
248 |
self._RegisterDebugVariable(idx) |
|
249 |
||
250 |
def GetTraceVariables(self): |
|
251 |
""" |
|
252 |
Return a list of variables, corresponding to the list of requiered idx |
|
253 |
""" |
|
254 |
self._WaitDebugData() |
|
255 |
||
256 |
for idx in self._Idxs: |
|
257 |
buffer=self._IterDebugData() |
|
258 |
self._FreeDebugData() |
|
259 |
||
260 |
||
261 |
||
262 |