Skip to content

Commit

Permalink
Merge pull request #335 from tim-ist/python-fixes
Browse files Browse the repository at this point in the history
Fixes and some cleanup in the python tools
  • Loading branch information
andrasbiro committed Apr 7, 2015
2 parents 8bb461d + 225778c commit 1489a03
Show file tree
Hide file tree
Showing 13 changed files with 33 additions and 369 deletions.
1 change: 0 additions & 1 deletion tools/configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,6 @@ AC_OUTPUT(
tinyos/python/misc/Makefile
tinyos/python/packet/Makefile
tinyos/python/tossim/Makefile
tinyos/python/utils/Makefile
tinyos/safe/Makefile
tinyos/safe/tos-decode-flid
tinyos/safe/tos-ramsize
Expand Down
2 changes: 1 addition & 1 deletion tools/tinyos/python/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
SUBDIRS = message misc packet tossim utils
SUBDIRS = message misc packet tossim

tospy_PYTHON = tos.py __init__.py
tospydir = $(pythondir)/tinyos
Expand Down
2 changes: 1 addition & 1 deletion tools/tinyos/python/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@
# Author: Geoffrey Mainland <[email protected]>
#

__all__ = ["message", "packet", "utils", "tossim", "misc"]
__all__ = ["message", "packet", "tossim", "misc"]
7 changes: 2 additions & 5 deletions tools/tinyos/python/message/MoteIF.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@
import struct
import sys
import traceback
from tinyos.utils.Watcher import Watcher

from tinyos.packet.Serial import Serial
from tinyos.packet.SerialH import Serial
from tinyos.message.SerialPacket import SerialPacket
import tinyos.packet.PacketDispatcher
import tinyos.packet.PacketSource
Expand All @@ -55,7 +54,6 @@ def __init__(self, *args):
class MoteIF:
def __init__(self):
self.listeners = {}
self.watcher = Watcher.getInstance()

def addListener(self, listener, msgClass):
if listener not in self.listeners:
Expand Down Expand Up @@ -93,8 +91,7 @@ def dispatchPacket(self, source, packet):
print >>sys.stderr, x
print >>sys.stderr, traceback.print_tb(sys.exc_info()[2])

for l in self.listeners:
amTypes = self.listeners[l]
for l, amTypes in self.listeners.items():
if amType in amTypes:
try:
msgClass = amTypes[amType]
Expand Down
4 changes: 2 additions & 2 deletions tools/tinyos/python/packet/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ tospy_PYTHON = avrmote.py SerialIO.py SFProtocol.py __init__.py \
SerialSource.py ThreadTask.py

tospydir = $(pythondir)/tinyos/packet
BUILT_SOURCES = Serial.py
BUILT_SOURCES = SerialH.py

SERIAL_H = $(TINYOS_OS_DIR)/lib/serial/Serial.h
TINYOS_OS_DIR ?= ../../../../tos
TFLAGS = -I$(TINYOS_OS_DIR)/lib/serial -I$(TINYOS_OS_DIR)/types

Serial.py:
SerialH.py:
nescc-ncg -o $@ $(TFLAGS) -python-classname=Serial python $(SERIAL_H) Serial.h
3 changes: 3 additions & 0 deletions tools/tinyos/python/packet/PacketSource.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def __init__(self, dispatcher):
self.semaphore = Semaphore(1)
self.semaphore.acquire()

# this will be called in the new thread
def __call__(self):
try:
self.open()
Expand Down Expand Up @@ -112,6 +113,8 @@ def __call__(self):
print "Unknown exception when dispatching packet"
# break

self.cancel()

try:
self.close()
except:
Expand Down
12 changes: 8 additions & 4 deletions tools/tinyos/python/packet/SerialIO.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,21 @@ def __init__(self, device, baud):

def open(self):
self.serial = serial.Serial(port=self.device,
baudrate=self.baud)
baudrate=self.baud, timeout=1)

def close(self):
self.serial.close()

def read(self, count):
while self.serial.inWaiting() < count:
data = ""
while count - len(data) > 0:
if self.isDone():
raise IODone()

return self.serial.read(count)
p = self.serial.read(count)
if len(p) == 0:
self.serial.inWaiting() # A workaround: raises IOException if USB serial device is disconnected
data += p
return data

def write(self, data):
return self.serial.write(data)
Expand Down
10 changes: 8 additions & 2 deletions tools/tinyos/python/packet/SerialProtocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

from threading import Lock, Condition, Thread
from IO import IODone
from Serial import Serial
from SerialH import Serial

SYNC_BYTE = Serial.HDLC_FLAG_BYTE
ESCAPE_BYTE = Serial.HDLC_CTLESC_BYTE
Expand Down Expand Up @@ -88,15 +88,17 @@ def run(self):
#OK, kind of ugly. finishing the SerialSource (ThreadTask)
# leads (ultimately) to an IODone exception coming up
# through here. At this point, the thread should complete.
except IODone:
except Exception, e:
with self.prot.ackCV:
self.prot.lastAck = None
self.prot.ackCV.notify()
with self.prot.dataCV:
self.prot.read_exception = e # storing exception to inform the other thread
self.prot.lastData = None
self.prot.dataCV.notify()
break


class SerialProtocol:
def __init__(self, ins, outs):
self.ins = ins
Expand All @@ -115,6 +117,7 @@ def __init__(self, ins, outs):
self.ackCV = Condition(rxLock)
self.lastData = None
self.lastAck = None
self.read_exception = None

#also a little ugly: can't start this thread until the
# serial.Serial object has been opened. This should all be
Expand All @@ -125,7 +128,10 @@ def open(self):

def readPacket(self):
with self.dataCV:
self.read_exception = None
self.dataCV.wait()
if self.read_exception != None:
raise self.read_exception # an exception from the other thread
return self.lastData

def readFramedPacket(self):
Expand Down
12 changes: 8 additions & 4 deletions tools/tinyos/python/packet/SocketIO.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ def open(self):
self.socket.settimeout(1)

def close(self):
self.socket.close()
self.socket = None
if self.socket is not None:
self.socket.close()
self.socket = None

def read(self, count):
data = ""
Expand All @@ -67,8 +68,11 @@ def read(self, count):
raise IODone()

try:
data += self.socket.recv(count - len(data))
except:
p = self.socket.recv(count - len(data))
if len(p) == 0:
raise IODone() # the remote side closed the connection
data += p
except socket.timeout:
pass

return data
Expand Down
4 changes: 0 additions & 4 deletions tools/tinyos/python/utils/Makefile.am

This file was deleted.

Loading

0 comments on commit 1489a03

Please sign in to comment.