📄 tos-deluge
字号:
print print "ERROR: Unable to write to the flash volume (file offset: %d)" % sreqpkt.offset return False length -= sreqpkt.length sreqpkt.offset += sreqpkt.length print '\r' + ' ' * 52, elasped_time = time.time() - start_time print "\r%s bytes in %.2f seconds (%.4f bytes/s)" % (total_length, elasped_time, int(total_length) / (elasped_time)) return True # Checks for valid CRC and timestampdef verifyIdent(i): if i != None: if crc16(i.payload()[0:10]) == i.crc and i.timestamp != 0xFFFFFFFF: return True else: print "No valid image was detected." return Falsedef getIdent(imgNum): r = read(imgNum, DELUGE_IDENT_OFFSET, DELUGE_IDENT_SIZE) if r: return Ident(r) print "ERROR: Unable to retrieve the ident." return Nonedef formatIdent(i): r = " Prog Name: %s\n" % (i.appname) r += " UID: 0x%08X\n" % (i.uidhash) r += " Compiled On: %s\n" % (datetime.fromtimestamp(i.timestamp).strftime('%a %h %d %T %Y')) r += " Platform: %s\n" % (i.platform) r += " User ID: %s\n" % (i.username) r += " Host Name: %s\n" % (i.hostname) r += " User Hash: 0x%08X\n" % (i.userhash) r += " Size: %d\n" % (i.size) r += " Num Pages: %d" % (i.pages) return rdef formatShortIdent(i): r = " Prog Name: %s\n" % (i.appname) r += " UID: 0x%08X\n" % (i.uidhash) r += " Compiled On: %s\n" % (datetime.fromtimestamp(i.timestamp).strftime('%a %h %d %T %Y')) r += " Node ID: %d\n" % (i.nodeid) return r# Injects an image (specified by tos_image_xml) to an image volumedef inject(imgNum, tos_image_xml): # Checks for valid file path try: os.stat(tos_image_xml) # Checks whether tos_image_xml is a valid file except: print "ERROR: Unable to find the TOS image XML, \"%s\"" % tos_image_xml return False try: os.stat(PATH_PY_BUILD_IMAGE) # Checks whether PATH_PY_BUILD_IMAGE is a valid file except: print "ERROR: Unable to find the image building utility, \"%s\"" % PATH_PY_BUILD_IMAGE return False # Gets status information of stored image i = getIdent(imgNum) if ident: print "Connected to Deluge nodes." if verifyIdent(i): print "--------------------------------------------------" print "Stored image %d" % imgNum print formatIdent(i) else: return False # Creates binary image from the TOS image XML 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) (out, err) = p.communicate(None) print err, print "--------------------------------------------------" # Writes the new binary image image = [struct.unpack("B", c)[0] for c in out] if len(image) > 0 and erase(imgNum): if write(imgNum, image): if sync(imgNum): print "--------------------------------------------------" print "Replace image with:" print formatIdent(getIdent(imgNum)) print "--------------------------------------------------" return Falsedef ping(imgNum): uid = ident() # Prints out image status print "--------------------------------------------------" print "Currently Executing:" print formatShortIdent(ident()) i = getIdent(imgNum) if verifyIdent(i): print "Stored image %d" % imgNum print formatIdent(i) print "--------------------------------------------------" return True print "--------------------------------------------------" return Falsedef boot(): sreqpkt = DMReqPacket((DM_CMD_BOOT, 0)) 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 = 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 = am.write(sreqpkt, DM_AMID) return handleResponse(success, "ERROR: Unable to disseminate")def disseminateAndReboot(imgNum): sreqpkt = DMReqPacket((DM_CMD_DISSEMINATE_AND_REPROGRAM, imgNum)) success = am.write(sreqpkt, DM_AMID) return handleResponse(success, "ERROR: Unable to disseminate-and-reboot")def stop(): sreqpkt = DMReqPacket((DM_CMD_STOP, 0)) 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 = am.write(sreqpkt, DM_AMID) return handleResponse(success, "ERROR: Unable to initiate the local stop")def print_usage(): print "Usage: %s <device_port> <baud_rate> <-p|-i|-r|-d|-e|-s> image_number [options]" % sys.argv[0] print " <baud_rate> Either the platform name (micaz or telosv) or a baudrate value" print " -p --ping Provide status of the image in the external flash" print " -i --inject Inject a compiled TinyOS application" print " [options]: tos_image.xml file path" print " -e --erase Erase an image in the external flash" print " -b --boot Force a reboot of the mote" print " -r --reprogram Reprogram the mote" print " -d --disseminate Disseminate the image in the external flash to the network" print " -dr --disseminate-and-reprogram" print " -s --stop Stop the dissemination " print " -ls --local-stop Stop the dissemination only on the local mote"# print " -b --reprogram_bs\n Reprogram only the directly-connected mote"# print " -s --reset\n Reset the versioning information for a given image"def checkImgNum(): global imgNum # Checks for valid image number format try: imgNum = int(sys.argv[4]) except: print "ERROR: Image number is not valid" sys.exit(-1) return imgNum# ======== MAIN ======== #if len(sys.argv) >= 4: if sys.argv[2] in BAUDRATES: baudrate = BAUDRATES[sys.argv[2]] else: try: baudrate = int(sys.argv[2]) except: print "ERROR: Wrong baudrate" sys.exit(-1) # Initializes serial port communication try: 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) if sys.argv[3] in ["-p", "--ping"]: checkImgNum() print "Pinging node ..." ping(imgNum) elif sys.argv[3] in ["-i", "--inject"] and len(sys.argv) == 6: checkImgNum() print "Pinging node ..." inject(imgNum, sys.argv[5]) elif sys.argv[3] in ["-e", "--erase"]: checkImgNum() if erase(imgNum): print "Image number %d erased" % imgNum elif sys.argv[3] in ["-b", "--boot"]: if boot(): print "Command sent" elif sys.argv[3] in ["-r", "--reprogram"]: checkImgNum() if reprogram(imgNum): print "Command sent" elif sys.argv[3] in ["-d", "--disseminate"]: checkImgNum() if disseminate(imgNum): print "Command sent" elif sys.argv[3] in ["-dr", "--disseminate-and-reboot"]: checkImgNum() if disseminateAndReboot(imgNum): print "Command sent" elif sys.argv[3] in ["-s", "--stop"]: if stop(): print "Command sent" elif sys.argv[3] in ["-ls", "--local-stop"]: if localstop(): print "Command sent" else: print_usage()else: print_usage()sys.exit()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -