Update vendortools/nfutil.py

This commit is contained in:
wp 2024-02-07 23:01:18 -06:00
parent be821d9ce3
commit 9540132fb2
1 changed files with 65 additions and 27 deletions

View File

@ -8,6 +8,7 @@ import time
import sys
import socket
now = time.time # now(): return current time in seconds as decimal
NETFINDER_SERVER_PORT = 3040
@ -74,7 +75,7 @@ TEST_FMT = HEADER_FMT
TEST_REPLY_FMT = "!32s"
MAX_ATTEMPTS = 10
MAX_TIMEOUT = 0.5
MAX_TIMEOUT = 0.5 # seconds
#---- NOTES ----
# Header Format: !2cH6s2x
@ -107,7 +108,7 @@ def MkSeq():
#-----------------------------------------------------------------------------
def MkHeader(header, seq, eth_addr):
"""Generate a network header packet.
"""Generate a network header packet. (12 bytes)
:param header: The header byte that identifies the payload
:type header: byte
@ -136,28 +137,64 @@ def MkIdentify(seq):
#-----------------------------------------------------------------------------
def MkIdentifyReply(seq, vDev):
"""Generate an IdentifyReply packet."""
# header: 12 bytes
# IdentifyReplyData: 64 bytes
# header
# NF_MAGIC
# NF_IDENTIFY_REPLY
# seq
"""Generate an IdentifyReply packet.
:param seq: The SEQ parameter. Should match the SEQ that was sent in
initial Identify request.
:type seq: int
:param vDev:
:type vDev:
"""
# Header Format: !2cH6s2x
# ! : Big Endian, std format
# 2c: 2 bytes
# H : unsigned short 0-65535 (2 bytes)
# 6s: 6 byte array
# 2x: 2 pad (null) bytes
#
# NF_MAGIC: 1 byte
# COMMAND: 1 byte
# SEQUENCE: 2 bytes
# ETH_ADDR (MAC): 6 bytes
# PAD: 2 bytes
# Total: 12 bytes
#
# Example: b'Z\x00V\x81\xff\xff\xff\xff\xff\xff\x00\x00'
# NF_MAGIC: b'Z' (HEX: 5A, DEC: 90)
# COMMAND: b'\x00' (NF_IDENTIFY)
# SEQUENCE: b'V\x81' (22145)
# ETH_ADDR (MAC): b'\xff\xff\xff\xff\xff\xff' (FF:FF:FF:FF:FF:FF)
# PAD: b'\x00\x00'
# header: 12 bytes (See MkHeader for details)
# IdentifyReplyData Format: !H6c4s4s4s4s4s4s32s
# ! : Network (Big Endian) byte order
# H : unsigned short - Int 0-65535 (2 bytes) [uptime_days]
# 6c : 6 bytes [uptime_hrs; uptime_mins; uptime_secs;
# mode; alert; ip_type]
# 4s : 4 byte array [ip_addr]
# 4s : 4 byte array [ip_netmask]
# 4s : 4 byte array [ip_gw]
# 4s : 4 byte array [app_ver]
# 4s : 4 byte array [boot_ver]
# 4s : 4 byte array [hw_ver]
# 32s: 32 byte array [name]
# IdentifyReplyData
# uptime_days [0-65535]
# uptime_hrs [0-23]
# uptime_min [0-59]
# uptime_mins [0-59]
# uptime_secs [0-59]
# mode []
# alert []
# mode [0: Bootloader mode; 1: Application (operating) mode]
# alert [0: OK; 1: WARNING; 255: ERROR]
# ip_type [static|dynamic]
# ip_addr [255.255.255.255]
# ip_netmask [255.255.255.255]
# ip_gw [255.255.255.255]
# app_ver [255.255.255.255]
# boot_ver [255.255.255.255]
# hw_ver [255.255.255.255]
# name [32 chars]
# ip_addr [255.255.255.255] (IP addr of device)
# ip_netmask [255.255.255.255] (Netmask of device)
# ip_gw [255.255.255.255] (gateway of davice)
# app_ver [255.255.255.255] (application firmware version)
# boot_ver [255.255.255.255] (bootloader firmware version)
# hw_ver [255.255.255.255] (hardware version)
# name [32 chars] (Hostname)
# todo: setup vDev class, VirtualDevice, which will provide the answers
# for the reply data
return MkHeader(NF_IDENTIFY_REPLY, seq, vDev.addr) + \
@ -429,8 +466,8 @@ def Discover(s, r):
msg = MkIdentify(seq)
if (SendMsg(s, msg)):
exp = time.time() + MAX_TIMEOUT
while time.time() < exp:
exp = now() + MAX_TIMEOUT
while now() < exp:
sys.stdout.write('.')
reply = RecvMsg(r)
if len(reply) != struct.calcsize(HEADER_FMT) + struct.calcsize(IDENTIFY_REPLY_FMT):
@ -461,9 +498,10 @@ def Identify(s, r, eth_addr):
msg = MkIdentify(seq)
if (SendMsg(s, msg)):
exp = time.time() + 2 # Longer timeout
exp = now() + 2 # timeout
while time.time() < exp:
while now() < exp:
# wait for a valid response
sys.stdout.write('.')
reply = RecvMsg(r)
if len(reply) != struct.calcsize(HEADER_FMT) + struct.calcsize(IDENTIFY_REPLY_FMT):
@ -532,9 +570,9 @@ def FlashErase(s, r, eth_addr):
msg = MkFlashErase(seq, eth_addr)
if (SendMsg(s, msg)):
exp = time.time() + 10 # Flash erase could take a while
exp = now() + 10 # Flash erase could take a while
while time.time() < exp:
while now() < exp:
sys.stdout.write('.')
reply = RecvMsg(r)
if len(reply) != struct.calcsize(HEADER_FMT):
@ -567,9 +605,9 @@ def BlockSize(s, r, eth_addr):
msg = MkBlockSize(seq, eth_addr)
if (SendMsg(s, msg)):
exp = time.time() + MAX_TIMEOUT
exp = now() + MAX_TIMEOUT
while time.time() < exp:
while now() < exp:
sys.stdout.write('.')
reply = RecvMsg(r)
if len(reply) != struct.calcsize(HEADER_FMT) + struct.calcsize(BLOCK_SIZE_REPLY_FMT):