diff --git a/Subsystems/cmdGui/MiniCmdUtil.py b/Subsystems/cmdGui/MiniCmdUtil.py index cc47bb8..ec32fe2 100644 --- a/Subsystems/cmdGui/MiniCmdUtil.py +++ b/Subsystems/cmdGui/MiniCmdUtil.py @@ -24,6 +24,7 @@ import socket import sys from pathlib import Path +from collections import namedtuple ROOTDIR = Path().resolve().parent sys.path.append(ROOTDIR.parent.parent) @@ -35,6 +36,23 @@ class MiniCmdUtil: ccsdsExt = bytearray(4) cfsCmdSecHdr = bytearray(2) + TypeSignature = namedtuple("TypeSignature", 'byteLen, signed, endian') + dataTypes = { + ("b", "int8", "byte"): TypeSignature(1, True, None), + ("m", "uint8"): TypeSignature(1, False, None), + ("h", "int16", "half"): TypeSignature(2, True, None), + ("n", "uint16"): TypeSignature(2, False, None), + ("l", "int32", "long", "word"): TypeSignature(4, True, None), + ("o", "uint32"): TypeSignature(4, False, None), + ("q", "int64"): TypeSignature(8, True, None), + ("p", "uint64"): TypeSignature(8, False, None), + ("i", "int16b"): TypeSignature(2, True, "big"), + ("j", "int32b"): TypeSignature(4, True, "big"), + ("k", "int64b"): TypeSignature(8, True, "big"), + ("w", "uint16b"): TypeSignature(2, False, "big"), + ("x", "uint32b"): TypeSignature(4, False, "big"), + ("y", "uint64b"): TypeSignature(8, False, "big") + } def __init__(self, host="127.0.0.1", @@ -44,12 +62,10 @@ def __init__(self, pktID=None, cmdCode=None, payload=None): - endians = {"BE": ">", "LE": "<"} - self.dataTypes = {"byte": 8, "half": 16, "long": 32} self.host = host self.port = port - self.endian = ">" if endian == "BE" else "<" + self.endian = "big" if endian == "BE" else "little" self.pktID = pktID self.cmdCode = cmdCode self.hdrVer = hdrVer @@ -68,4 +84,25 @@ def assemblePriHeader(self): def assemblePayload(self): - pass \ No newline at end of file + completePayload = bytearray(0) + paramList = self.payload.split(" ") + for param in paramList: + if "--string" not in param: ## non-string param + items = param.split("=") ## e.g. ["--uint16", "2"] + dataType = items[0].strip("-") ## Remove "--" prefix + dataVal = int(items[1]) ## "2" --> 2 + for key in self.dataTypes: ## Loop thru dictionary + if dataType in key: ## Check if "uint16" in key tuple + ## Get the TypeSignature tuple + typeSig = self.dataTypes[key] + break ## Stop checking dictionary + ## If TypeSignature endian is None, get the user-provided endian + ## Otherwise get the TypeSignature endian + endian = typeSig.endian or self.endian + ## Convert to bytes of correct length, endianess, and sign + dataValb = dataVal.to_bytes(typeSig.byteLen, + byteorder=endian, + signed=typeSig.signed) + completePayload.extend(dataValb) ## Add data to bytearray + else: + pass