#!/usr/bin/env python
+# Copyright (c) 2007 Johns Hopkins University.
+# All rights reserved.
+#
+# Permission to use, copy, modify, and distribute this software and its
+# documentation for any purpose, without fee, and without written
+# agreement is hereby granted, provided that the above copyright
+# notice, the (updated) modification history and the author appear in
+# all copies of this source code.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
+# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, LOSS OF USE, DATA,
+# OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+# THE POSSIBILITY OF SUCH DAMAGE.
+
+# @author Razvan Musaloiu-E. <razvanm@cs.jhu.edu>
+# @author Chieh-Jan Mike Liang <cliang4@cs.jhu.edu>
+
import sys, stat, struct, subprocess, time
-import tinyos
+import tos
from datetime import datetime
import os.path
# Path to the python script that builds Deluge image from XML
-PATH_PY_BUILD_IMAGE = './tos-build-deluge-image'
+PATH_PY_BUILD_IMAGE = os.path.join(os.path.dirname(sys.argv[0]), 'tos-build-deluge-image')
# TinyOS serial communication parameters
FM_AMID = 0xAB
DM_AMID = 0xAC
SERIAL_DATA_LENGTH = 28 - 1 - 1 - 2 - 2
BAUDRATES = {'micaz': 57600,
- 'telosb': 115200}
+ 'telosb': 115200,
+ 'iris': 57600}
# Commands for FlashManager
FM_CMD_ERASE = 0
DELUGE_IDENT_OFFSET = 0
DELUGE_IDENT_SIZE = 128
-class FMReqPacket(tinyos.GenericPacket):
+class FMReqPacket(tos.Packet):
def __init__(self, packet = None):
- tinyos.GenericPacket.__init__(self,
- [('cmd', 'int', 1),
- ('imgNum', 'int', 1),
- ('offset', 'int', 2),
- ('length', 'int', 2),
- ('data', 'blob', None)],
- packet)
-
-class DMReqPacket(tinyos.GenericPacket):
+ tos.Packet.__init__(self,
+ [('cmd', 'int', 1),
+ ('imgNum', 'int', 1),
+ ('offset', 'int', 2),
+ ('length', 'int', 2),
+ ('data', 'blob', None)],
+ packet)
+
+class DMReqPacket(tos.Packet):
def __init__(self, packet = None):
- tinyos.GenericPacket.__init__(self,
- [('cmd', 'int', 1),
- ('imgNum', 'int', 1)],
- packet)
+ tos.Packet.__init__(self,
+ [('cmd', 'int', 1),
+ ('imgNum', 'int', 1)],
+ packet)
-class SerialReplyPacket(tinyos.GenericPacket):
+class SerialReplyPacket(tos.Packet):
def __init__(self, packet = None):
- tinyos.GenericPacket.__init__(self,
- [('error', 'int', 1),
- ('data', 'blob', None)],
- packet)
+ tos.Packet.__init__(self,
+ [('error', 'int', 1),
+ ('data', 'blob', None)],
+ packet)
-class Ident(tinyos.GenericPacket):
+class Ident(tos.Packet):
def __init__(self, packet = None):
- tinyos.GenericPacket.__init__(self,
- [('uidhash', 'int', 4),
- ('size', 'int', 4),
- ('pages', 'int', 1),
- ('reserved', 'int', 1),
- ('crc', 'int', 2),
- ('appname', 'string', 16),
- ('username', 'string', 16),
- ('hostname', 'string', 16),
- ('platform', 'string', 16),
- ('timestamp','int', 4),
- ('userhash', 'int', 4)],
- packet)
-
-class ShortIdent(tinyos.GenericPacket):
+ tos.Packet.__init__(self,
+ [('uidhash', 'int', 4),
+ ('size', 'int', 4),
+ ('pages', 'int', 1),
+ ('reserved', 'int', 1),
+ ('crc', 'int', 2),
+ ('appname', 'string', 16),
+ ('username', 'string', 16),
+ ('hostname', 'string', 16),
+ ('platform', 'string', 16),
+ ('timestamp','int', 4),
+ ('userhash', 'int', 4)],
+ packet)
+
+class ShortIdent(tos.Packet):
def __init__(self, packet = None):
- tinyos.GenericPacket.__init__(self,
- [('appname', 'string', 16),
- ('timestamp','int', 4),
- ('uidhash', 'int', 4),
- ('nodeid', 'int', 2)],
- packet)
+ tos.Packet.__init__(self,
+ [('appname', 'string', 16),
+ ('timestamp','int', 4),
+ ('uidhash', 'int', 4),
+ ('nodeid', 'int', 2)],
+ packet)
# Computes 16-bit CRC
def handleResponse(success, msg):
if success == True:
- packet = s.sniff_am()
+ packet = am.read(timeout=1)
while packet and packet.type == 100:
- print "".join([chr(i) for i in p.data])
- packet = s.sniff_am()
+ print "".join([chr(i) for i in packet.data])
+ packet = am.read()
+ if not packet:
+ print "No response"
+ return False
reply = SerialReplyPacket(packet.data)
if reply.error == ERROR_SUCCESS:
- print reply
return True
else:
print msg, reply
def ident():
sreqpkt = FMReqPacket((FM_CMD_IDENT, 0, 0, 0, []))
- if s.write(tinyos.ActiveMessage(sreqpkt, am_id=FM_AMID)):
- packet = s.sniff_am()
+ if am.write(sreqpkt, FM_AMID):
+ packet = am.read()
reply = SerialReplyPacket(packet.data)
if reply.error == ERROR_SUCCESS:
return ShortIdent(reply.data)
if sreqpkt.length > SERIAL_DATA_LENGTH:
sreqpkt.length = SERIAL_DATA_LENGTH
- if s.write(tinyos.ActiveMessage(sreqpkt, am_id=FM_AMID)):
- packet = s.sniff_am()
+ if am.write(sreqpkt, FM_AMID):
+ packet = am.read()
reply = SerialReplyPacket(packet.data)
if reply.error == ERROR_SUCCESS:
r.extend(reply.data)
def erase(imgNum):
sreqpkt = FMReqPacket((FM_CMD_ERASE, imgNum, 0, 0, []))
- success = s.write(tinyos.ActiveMessage(sreqpkt, am_id=FM_AMID))
+ success = am.write(sreqpkt, FM_AMID)
return handleResponse(success, "ERROR: Unable to erase the flash volume")
def sync(imgNum):
sreqpkt = FMReqPacket((FM_CMD_SYNC, imgNum, 0, 0, []))
- success = s.write(tinyos.ActiveMessage(sreqpkt, am_id=FM_AMID))
+ success = am.write(sreqpkt, FM_AMID)
return handleResponse(success, "ERROR: Unable to sync the flash volume")
def write(imgNum, data):
sreqpkt.data = data[sreqpkt.offset:sreqpkt.offset+sreqpkt.length]
# Sends over serial to the mote
- if not s.write(tinyos.ActiveMessage(sreqpkt, am_id=FM_AMID)):
+ if not am.write(sreqpkt, FM_AMID):
print
print "ERROR: Unable to send the last serial packet (file offset: %d)" % sreqpkt.offset
return False
# Waiting for confirmation
- packet = s.sniff_am()
+ packet = am.read()
reply = SerialReplyPacket(packet.data)
if reply.error != ERROR_SUCCESS:
print
cmd = [PATH_PY_BUILD_IMAGE, "-i", str(imgNum), tos_image_xml]
print "Create image:", ' '.join(cmd)
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
- print p.stderr.read(),
+ (out, err) = p.communicate(None)
+ print err,
print "--------------------------------------------------"
# Writes the new binary image
- image = [struct.unpack("B", c)[0] for c in p.stdout.read()]
+ image = [struct.unpack("B", c)[0] for c in out]
if len(image) > 0 and erase(imgNum):
if write(imgNum, image):
if sync(imgNum):
def boot():
sreqpkt = DMReqPacket((DM_CMD_BOOT, 0))
- success = s.write(tinyos.ActiveMessage(sreqpkt, am_id=DM_AMID))
+ success = am.write(sreqpkt, DM_AMID)
return handleResponse(success, "ERROR: Unable to boot the mote")
def reprogram(imgNum):
sreqpkt = DMReqPacket((DM_CMD_REPROGRAM, imgNum))
- success = s.write(tinyos.ActiveMessage(sreqpkt, am_id=DM_AMID))
+ success = am.write(sreqpkt, DM_AMID)
return handleResponse(success, "ERROR: Unable to reprogram the mote")
def disseminate(imgNum):
sreqpkt = DMReqPacket((DM_CMD_ONLY_DISSEMINATE, imgNum))
- success = s.write(tinyos.ActiveMessage(sreqpkt, am_id=DM_AMID))
+ success = am.write(sreqpkt, DM_AMID)
return handleResponse(success, "ERROR: Unable to disseminate")
def disseminateAndReboot(imgNum):
sreqpkt = DMReqPacket((DM_CMD_DISSEMINATE_AND_REPROGRAM, imgNum))
- success = s.write(tinyos.ActiveMessage(sreqpkt, am_id=DM_AMID))
+ success = am.write(sreqpkt, DM_AMID)
return handleResponse(success, "ERROR: Unable to disseminate-and-reboot")
def stop():
sreqpkt = DMReqPacket((DM_CMD_STOP, 0))
- success = s.write(tinyos.ActiveMessage(sreqpkt, am_id=DM_AMID))
+ success = am.write(sreqpkt, DM_AMID)
return handleResponse(success, "ERROR: Unable to initiate the stop")
def localstop():
sreqpkt = DMReqPacket((DM_CMD_LOCAL_STOP, 0))
- success = s.write(tinyos.ActiveMessage(sreqpkt, am_id=DM_AMID))
+ success = am.write(sreqpkt, DM_AMID)
return handleResponse(success, "ERROR: Unable to initiate the local stop")
def print_usage():
# Initializes serial port communication
try:
- s = tinyos.Serial(sys.argv[1], baudrate, flush=False)
+ s = tos.Serial(sys.argv[1], baudrate, flush=True, debug=False)
+ am = tos.AM(s)
except:
print "ERROR: Unable to initialize serial port connection to", sys.argv[1]
sys.exit(-1)
elif sys.argv[3] in ["-ls", "--local-stop"]:
if localstop():
print "Command sent"
-
-
-# elif sys.argv[3] in ["-b", "--reprogram_bs"]:
-# if op_reprog_bs(imgNum):
-# print "Command sent"
-# elif sys.argv[3] in ["-s", "--reset"]:
-# if op_reset(imgNum):
-# print "Successfully reset image versioning information"
else:
print_usage()
sys.exit()
-# Testing ------------------------------------------------------------
-
-s = tinyos.Serial('/dev/ttyUSB0', 115200, flush=False)
-# print "ident: 0x%x" % ident()
-# r = read(0, 0, 10)
-# print "read:", len(r), r
-
-# erase(0)
-# write(0, [i % 256 for i in range(20*1024)])
-# r = read(0, 0, 10)
-# print "read:", len(r), r
-
-# inject(0, 'build/telosb/tos_image.xml')
-ping(0)
-
-# print formatDescription(getMetadata(0), getIdent(0))
-# print getMetadata(0)
-# print getIdent(0)
-
-# print verifyDescription(getMetadata(0), getIdent(0))
-
-# print formatShortIdent(ident())
-
-# def encode(val, dim):
-# output = []
-# for i in range(dim):
-# output.append(val & 0xFF)
-# val = val >> 8
-# return output