Skip to content

Commit 1489a03

Browse files
committed
Merge pull request #335 from tim-ist/python-fixes
Fixes and some cleanup in the python tools
2 parents 8bb461d + 225778c commit 1489a03

File tree

13 files changed

+33
-369
lines changed

13 files changed

+33
-369
lines changed

tools/configure.ac

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,6 @@ AC_OUTPUT(
290290
tinyos/python/misc/Makefile
291291
tinyos/python/packet/Makefile
292292
tinyos/python/tossim/Makefile
293-
tinyos/python/utils/Makefile
294293
tinyos/safe/Makefile
295294
tinyos/safe/tos-decode-flid
296295
tinyos/safe/tos-ramsize

tools/tinyos/python/Makefile.am

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
SUBDIRS = message misc packet tossim utils
1+
SUBDIRS = message misc packet tossim
22

33
tospy_PYTHON = tos.py __init__.py
44
tospydir = $(pythondir)/tinyos

tools/tinyos/python/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@
2929
# Author: Geoffrey Mainland <[email protected]>
3030
#
3131

32-
__all__ = ["message", "packet", "utils", "tossim", "misc"]
32+
__all__ = ["message", "packet", "tossim", "misc"]

tools/tinyos/python/message/MoteIF.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,8 @@
3434
import struct
3535
import sys
3636
import traceback
37-
from tinyos.utils.Watcher import Watcher
3837

39-
from tinyos.packet.Serial import Serial
38+
from tinyos.packet.SerialH import Serial
4039
from tinyos.message.SerialPacket import SerialPacket
4140
import tinyos.packet.PacketDispatcher
4241
import tinyos.packet.PacketSource
@@ -55,7 +54,6 @@ def __init__(self, *args):
5554
class MoteIF:
5655
def __init__(self):
5756
self.listeners = {}
58-
self.watcher = Watcher.getInstance()
5957

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

96-
for l in self.listeners:
97-
amTypes = self.listeners[l]
94+
for l, amTypes in self.listeners.items():
9895
if amType in amTypes:
9996
try:
10097
msgClass = amTypes[amType]

tools/tinyos/python/packet/Makefile.am

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ tospy_PYTHON = avrmote.py SerialIO.py SFProtocol.py __init__.py \
44
SerialSource.py ThreadTask.py
55

66
tospydir = $(pythondir)/tinyos/packet
7-
BUILT_SOURCES = Serial.py
7+
BUILT_SOURCES = SerialH.py
88

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

13-
Serial.py:
13+
SerialH.py:
1414
nescc-ncg -o $@ $(TFLAGS) -python-classname=Serial python $(SERIAL_H) Serial.h

tools/tinyos/python/packet/PacketSource.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ def __init__(self, dispatcher):
5858
self.semaphore = Semaphore(1)
5959
self.semaphore.acquire()
6060

61+
# this will be called in the new thread
6162
def __call__(self):
6263
try:
6364
self.open()
@@ -112,6 +113,8 @@ def __call__(self):
112113
print "Unknown exception when dispatching packet"
113114
# break
114115

116+
self.cancel()
117+
115118
try:
116119
self.close()
117120
except:

tools/tinyos/python/packet/SerialIO.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,21 @@ def __init__(self, device, baud):
4141

4242
def open(self):
4343
self.serial = serial.Serial(port=self.device,
44-
baudrate=self.baud)
44+
baudrate=self.baud, timeout=1)
4545

4646
def close(self):
4747
self.serial.close()
4848

4949
def read(self, count):
50-
while self.serial.inWaiting() < count:
50+
data = ""
51+
while count - len(data) > 0:
5152
if self.isDone():
5253
raise IODone()
53-
54-
return self.serial.read(count)
54+
p = self.serial.read(count)
55+
if len(p) == 0:
56+
self.serial.inWaiting() # A workaround: raises IOException if USB serial device is disconnected
57+
data += p
58+
return data
5559

5660
def write(self, data):
5761
return self.serial.write(data)

tools/tinyos/python/packet/SerialProtocol.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
from threading import Lock, Condition, Thread
3737
from IO import IODone
38-
from Serial import Serial
38+
from SerialH import Serial
3939

4040
SYNC_BYTE = Serial.HDLC_FLAG_BYTE
4141
ESCAPE_BYTE = Serial.HDLC_CTLESC_BYTE
@@ -88,15 +88,17 @@ def run(self):
8888
#OK, kind of ugly. finishing the SerialSource (ThreadTask)
8989
# leads (ultimately) to an IODone exception coming up
9090
# through here. At this point, the thread should complete.
91-
except IODone:
91+
except Exception, e:
9292
with self.prot.ackCV:
9393
self.prot.lastAck = None
9494
self.prot.ackCV.notify()
9595
with self.prot.dataCV:
96+
self.prot.read_exception = e # storing exception to inform the other thread
9697
self.prot.lastData = None
9798
self.prot.dataCV.notify()
9899
break
99100

101+
100102
class SerialProtocol:
101103
def __init__(self, ins, outs):
102104
self.ins = ins
@@ -115,6 +117,7 @@ def __init__(self, ins, outs):
115117
self.ackCV = Condition(rxLock)
116118
self.lastData = None
117119
self.lastAck = None
120+
self.read_exception = None
118121

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

126129
def readPacket(self):
127130
with self.dataCV:
131+
self.read_exception = None
128132
self.dataCV.wait()
133+
if self.read_exception != None:
134+
raise self.read_exception # an exception from the other thread
129135
return self.lastData
130136

131137
def readFramedPacket(self):

tools/tinyos/python/packet/SocketIO.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@ def open(self):
5757
self.socket.settimeout(1)
5858

5959
def close(self):
60-
self.socket.close()
61-
self.socket = None
60+
if self.socket is not None:
61+
self.socket.close()
62+
self.socket = None
6263

6364
def read(self, count):
6465
data = ""
@@ -67,8 +68,11 @@ def read(self, count):
6768
raise IODone()
6869

6970
try:
70-
data += self.socket.recv(count - len(data))
71-
except:
71+
p = self.socket.recv(count - len(data))
72+
if len(p) == 0:
73+
raise IODone() # the remote side closed the connection
74+
data += p
75+
except socket.timeout:
7276
pass
7377

7478
return data

tools/tinyos/python/utils/Makefile.am

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)