📄 tos-deluge
字号:
#!/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, timeimport tosfrom datetime import datetimeimport os.path # Path to the python script that builds Deluge image from XMLPATH_PY_BUILD_IMAGE = os.path.join(os.path.dirname(sys.argv[0]), 'tos-build-deluge-image')# TinyOS serial communication parametersFM_AMID = 0xABDM_AMID = 0xACSERIAL_DATA_LENGTH = 28 - 1 - 1 - 2 - 2BAUDRATES = {'micaz': 57600, 'telosb': 115200, 'iris': 57600}# Commands for FlashManagerFM_CMD_ERASE = 0FM_CMD_WRITE = 1FM_CMD_READ = 2FM_CMD_CRC = 3FM_CMD_ADDR = 4FM_CMD_SYNC = 5FM_CMD_IDENT = 6# Commands for DelugeManagerDM_CMD_STOP = 1DM_CMD_LOCAL_STOP = 2DM_CMD_ONLY_DISSEMINATE = 3DM_CMD_DISSEMINATE_AND_REPROGRAM = 4DM_CMD_REPROGRAM = 5DM_CMD_BOOT = 6ERROR_SUCCESS = 0 # T2-compatibleERROR_FAIL = 1 # T2-compatible# Deluge parametersDELUGE_MAX_PAGES = 128DELUGE_IDENT_OFFSET = 0DELUGE_IDENT_SIZE = 128class FMReqPacket(tos.Packet): def __init__(self, packet = None): 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): tos.Packet.__init__(self, [('cmd', 'int', 1), ('imgNum', 'int', 1)], packet)class SerialReplyPacket(tos.Packet): def __init__(self, packet = None): tos.Packet.__init__(self, [('error', 'int', 1), ('data', 'blob', None)], packet)class Ident(tos.Packet): def __init__(self, packet = None): 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): tos.Packet.__init__(self, [('appname', 'string', 16), ('timestamp','int', 4), ('uidhash', 'int', 4), ('nodeid', 'int', 2)], packet)# Computes 16-bit CRCdef crc16(data): crc = 0 for b in data: crc = crc ^ (b << 8) for i in range(0, 8): if crc & 0x8000 == 0x8000: crc = (crc << 1) ^ 0x1021 else: crc = crc << 1 crc = crc & 0xffff return crcdef handleResponse(success, msg): if success == True: packet = am.read(timeout=1) while packet and packet.type == 100: 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: return True else: print msg, reply return False print "ERROR: Unable to send the command" return Falsedef ident(): sreqpkt = FMReqPacket((FM_CMD_IDENT, 0, 0, 0, [])) if am.write(sreqpkt, FM_AMID): packet = am.read() reply = SerialReplyPacket(packet.data) if reply.error == ERROR_SUCCESS: return ShortIdent(reply.data) return 0def read(imgNum, offset, length): r = [] sreqpkt = FMReqPacket((FM_CMD_READ, imgNum, offset, length, [])) while True: if sreqpkt.length > SERIAL_DATA_LENGTH: sreqpkt.length = SERIAL_DATA_LENGTH if am.write(sreqpkt, FM_AMID): packet = am.read() reply = SerialReplyPacket(packet.data) if reply.error == ERROR_SUCCESS: r.extend(reply.data) else: r = None break else: r = None break sreqpkt.offset += sreqpkt.length if sreqpkt.offset >= (offset + length): break sreqpkt.length = (offset + length) - sreqpkt.offset return rdef erase(imgNum): sreqpkt = FMReqPacket((FM_CMD_ERASE, imgNum, 0, 0, [])) 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 = am.write(sreqpkt, FM_AMID) return handleResponse(success, "ERROR: Unable to sync the flash volume")def write(imgNum, data): sreqpkt = FMReqPacket((FM_CMD_WRITE, imgNum, 0, 0, [])) length = len(data) total_length = length # For progress bar next_tick = 100 # For progress bar start_time = time.time() print "[0% 25% 50% 75% 100%]\r[", sreqpkt.offset = 0 while length > 0: if ((length * 100) / total_length) < next_tick: next_tick = next_tick - 2 sys.stdout.write('-') sys.stdout.flush() # Calculates the payload size for the current packet if length >= SERIAL_DATA_LENGTH: sreqpkt.length = SERIAL_DATA_LENGTH else: sreqpkt.length = length sreqpkt.data = data[sreqpkt.offset:sreqpkt.offset+sreqpkt.length] # Sends over serial to the mote 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 = am.read() reply = SerialReplyPacket(packet.data) if reply.error != ERROR_SUCCESS:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -