mirror of
https://github.com/uhi22/pyPLC.git
synced 2024-11-20 01:13:58 +00:00
non blocking reception works
This commit is contained in:
parent
ec03fd05ce
commit
07f33d3bc8
3 changed files with 65 additions and 22 deletions
15
demo_pcap.py
Normal file
15
demo_pcap.py
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
import pcap
|
||||||
|
|
||||||
|
|
||||||
|
print("Interfaces:\n" + '\n'.join(pcap.findalldevs()))
|
||||||
|
print("ex_name")
|
||||||
|
for i in range(0, 10):
|
||||||
|
strInterfaceName = pcap.ex_name("eth"+str(i))
|
||||||
|
if (strInterfaceName == '\\Device\\NPF_{E4B8176C-8516-4D48-88BC-85225ABCF259}'):
|
||||||
|
print("This is the wanted Ethernet adaptor.")
|
||||||
|
print("eth"+ str(i) + " is " + strInterfaceName)
|
||||||
|
|
||||||
|
sniffer = pcap.pcap(name=None, promisc=True, immediate=True, timeout_ms=50)
|
||||||
|
addr = lambda pkt, offset: '.'.join(str(pkt[i]) for i in range(offset, offset + 4))
|
||||||
|
for ts, pkt in sniffer:
|
||||||
|
print('%d\tSRC %-16s\tDST %-16s' % (ts, addr(pkt, sniffer.dloff + 12), addr(pkt, sniffer.dloff + 16)))
|
|
@ -128,29 +128,62 @@ class pyPlcHomeplug():
|
||||||
self.composeTestFrame()
|
self.composeTestFrame()
|
||||||
self.sniffer.sendpacket(bytes(self.mytransmitbuffer))
|
self.sniffer.sendpacket(bytes(self.mytransmitbuffer))
|
||||||
|
|
||||||
|
def findEthernetAdaptor(self):
|
||||||
|
self.strInterfaceName="eth0" # default, if the real is not found
|
||||||
|
print("Interfaces:\n" + '\n'.join(pcap.findalldevs()))
|
||||||
|
for i in range(0, 10):
|
||||||
|
strInterfaceName = pcap.ex_name("eth"+str(i))
|
||||||
|
if (strInterfaceName == '\\Device\\NPF_{E4B8176C-8516-4D48-88BC-85225ABCF259}'):
|
||||||
|
print("This is the wanted Ethernet adaptor.")
|
||||||
|
self.strInterfaceName="eth"+str(i)
|
||||||
|
print("eth"+ str(i) + " is " + strInterfaceName)
|
||||||
|
|
||||||
def __init__(self, callbackAddToTrace=None, callbackShowStatus=None):
|
def __init__(self, callbackAddToTrace=None, callbackShowStatus=None):
|
||||||
self.mytransmitbuffer = bytearray("Hallo das ist ein Test", 'UTF-8')
|
self.mytransmitbuffer = bytearray("Hallo das ist ein Test", 'UTF-8')
|
||||||
|
self.nPacketsReceived = 0
|
||||||
self.callbackAddToTrace = callbackAddToTrace
|
self.callbackAddToTrace = callbackAddToTrace
|
||||||
self.callbackShowStatus = callbackShowStatus #self.sniffer = pcap.pcap(name=None, promisc=True, immediate=True, timeout_ms=50)
|
self.callbackShowStatus = callbackShowStatus
|
||||||
# eth3 bedeutet: Dritter Eintrag von hinten, in der Liste der Interfaces, die von pcap.findalldevs geliefert wird.
|
#self.sniffer = pcap.pcap(name=None, promisc=True, immediate=True, timeout_ms=50)
|
||||||
# Verbesserungsbedarf: Interface namensbasiert auswählen.
|
# eth3 means: Third entry from back, in the list of interfaces, which is provided by pcap.findalldevs.
|
||||||
self.sniffer = pcap.pcap(name="eth3", promisc=True, immediate=True, timeout_ms=50)
|
# Improvement necessary: select the interface based on the name.
|
||||||
|
# For debugging of the interface names, we can patch the file
|
||||||
|
# C:\Users\uwemi\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pcap\_pcap_ex.py,
|
||||||
|
# in the function
|
||||||
|
# def name(name: bytes) -> bytes:
|
||||||
|
# in the place after
|
||||||
|
# if i == idx:
|
||||||
|
# print("index match at " + str(i) + " dev name=" + str(dev.name) + " dev.description=" + str(dev.description))
|
||||||
|
# This will print the description of the used interface.
|
||||||
|
#
|
||||||
|
# Patch for non-blocking read-iteration:
|
||||||
|
# in _pcap.py, function def __next__(self), in the case of timeout (if n==0), we need to "raise StopIteration" instead of "continue".
|
||||||
|
#
|
||||||
|
self.findEthernetAdaptor()
|
||||||
|
self.sniffer = pcap.pcap(name=self.strInterfaceName, promisc=True, immediate=True, timeout_ms=50)
|
||||||
|
self.sniffer.setnonblock(True)
|
||||||
|
print("sniffer created at " + self.strInterfaceName)
|
||||||
|
|
||||||
def addToTrace(s):
|
def addToTrace(s):
|
||||||
self.callbackAddToTrace(s)
|
self.callbackAddToTrace(s)
|
||||||
|
|
||||||
|
def showStatus(self, s):
|
||||||
|
self.callbackShowStatus(s)
|
||||||
|
|
||||||
def mainfunction(self):
|
def mainfunction(self):
|
||||||
|
# print("will evaluate self.sniffer")
|
||||||
|
for ts, pkt in self.sniffer: # attention: for using this in non-blocking manner, we need the patch described above.
|
||||||
|
self.nPacketsReceived+=1
|
||||||
|
print('%d' % (ts))
|
||||||
|
#if (self.isHomeplug(pkt)):
|
||||||
|
# self.showMacAddresses(pkt)
|
||||||
|
# showAsHex(pkt)
|
||||||
|
# if (pkt[15]==0x64): #SLAC_Request
|
||||||
|
# self.sendTestFrame()
|
||||||
|
#
|
||||||
|
#else:
|
||||||
|
# addToTrace("(other)")
|
||||||
|
self.showStatus("nPacketsReceived=" + str(self.nPacketsReceived))
|
||||||
|
|
||||||
for ts, pkt in self.sniffer:
|
|
||||||
#print('%d' % (ts))
|
|
||||||
if (self.isHomeplug(pkt)):
|
|
||||||
self.showMacAddresses(pkt)
|
|
||||||
showAsHex(pkt)
|
|
||||||
if (pkt[15]==0x64): #SLAC_Request
|
|
||||||
self.sendTestFrame()
|
|
||||||
|
|
||||||
else:
|
|
||||||
addToTrace("(other)")
|
|
||||||
def close(self):
|
def close(self):
|
||||||
self.sniffer.close()
|
self.sniffer.close()
|
||||||
|
|
||||||
|
|
|
@ -6,9 +6,6 @@
|
||||||
#------------------------------------------------------------
|
#------------------------------------------------------------
|
||||||
import pyPlcHomeplug
|
import pyPlcHomeplug
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class pyPlcWorker():
|
class pyPlcWorker():
|
||||||
def __init__(self, callbackAddToTrace=None, callbackShowStatus=None):
|
def __init__(self, callbackAddToTrace=None, callbackShowStatus=None):
|
||||||
print("initializing pyPlcWorker")
|
print("initializing pyPlcWorker")
|
||||||
|
@ -27,10 +24,8 @@ class pyPlcWorker():
|
||||||
|
|
||||||
def mainfunction(self):
|
def mainfunction(self):
|
||||||
self.nMainFunctionCalls+=1
|
self.nMainFunctionCalls+=1
|
||||||
self.showStatus("pyPlcWorker loop " + str(self.nMainFunctionCalls))
|
#self.showStatus("pyPlcWorker loop " + str(self.nMainFunctionCalls))
|
||||||
#self.hp.mainfunction()
|
self.hp.mainfunction() # call the lower-level worker
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def handleUserAction(self, strAction):
|
def handleUserAction(self, strAction):
|
||||||
self.strUserAction = strAction
|
self.strUserAction = strAction
|
||||||
|
|
Loading…
Reference in a new issue