Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import socket, time
- from threading import Thread, Lock
- from tkinter import *
- from tkinter import ttk
- from tkinter import messagebox
- HOST = "127.0.0.1"
- PORT = 7447
- sendBuffer = []
- sendBufferMutex = Lock()
- receiveBuffer = []
- receiveBufferMutex = Lock()
- def RunTCPConnection():
- socketInstance = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- socketInstance.connect((HOST, PORT))
- socketInstance.setblocking(False)
- while 1:
- try:
- amsPacket = socketInstance.recv(1000)
- except socket.error:
- pass
- else:
- with receiveBufferMutex:
- receiveBuffer.append(amsPacket.decode("utf-8"))
- HandleAMSPackets()
- with sendBufferMutex:
- if len(sendBuffer) > 0:
- socketInstance.send(str.encode(sendBuffer[0]))
- del sendBuffer[0]
- time.sleep(0.1)
- t = Thread(target = RunTCPConnection)
- t.start()
- tkRoot = Tk()
- tkForm = ttk.Frame(tkRoot, padding = 10)
- tkForm.master.title("YACAN-CSP-AMS")
- tkForm.grid()
- tkRoot.resizable(False, False)
- AMS_PORT_AAS = 16
- AMS_PORT_PAS = 17
- commandType = IntVar()
- actionID = StringVar()
- transactionID = IntVar()
- transactionIDAuto = BooleanVar()
- amsVersion = StringVar()
- commandArgument = StringVar()
- pasAccessType = IntVar()
- amsVersion.set("cbe2")
- transactionIDAuto.set(True)
- commandType.set(AMS_PORT_AAS)
- pasAccessType.set(3)
- def SendForm():
- if transactionID.get() > 0xFF:
- transactionID.set(0)
- # AMS header
- command = f"send 01{format(transactionID.get(), '02x')}"
- if len(amsVersion.get()) == 4:
- try:
- int(amsVersion.get(), 16)
- except:
- messagebox.showerror("Error", "The AMS version is invalid hex")
- return
- else:
- messagebox.showerror("Error", "The AMS version is not 2 bytes")
- return
- if commandType.get() == AMS_PORT_AAS:
- command += f"0102{amsVersion.get()}0000{actionID.get()}{commandArgument.get()}"
- elif commandType.get() == AMS_PORT_PAS:
- command += f"{format(pasAccessType.get(), '02x')}02{amsVersion.get()}0000{actionID.get()}{commandArgument.get()}"
- else:
- messagebox.showerror("Error", "An invalid command type is set - Please make sure you've selected either AAS or PAS")
- return
- if transactionIDAuto.get() == True:
- transactionID.set(transactionID.get() + 1)
- with sendBufferMutex:
- sendBuffer.append(command)
- print(f"Sending command to YACAN-CSP: \"{command}\"")
- def UpdateConfiguration():
- commands = []
- commands.append(f"source_node = 17")
- commands.append(f"source_port = 48")
- commands.append(f"destination_node = 15")
- commands.append(f"priority = 0")
- commands.append(f"destination_port = {commandType.get()}")
- with sendBufferMutex:
- for command in commands:
- sendBuffer.append(command)
- ttk.Label(tkForm, text = "Command Type").grid(column = 0, row = 0, sticky = W)
- W_RadiobuttonAAS = Radiobutton(tkForm, text = "AAS", variable = commandType, value = AMS_PORT_AAS, command = UpdateConfiguration).grid(column = 0, row = 1, padx = 135, sticky = W)
- W_RadiobuttonPAS = Radiobutton(tkForm, text = "PAS", variable = commandType, value = AMS_PORT_PAS, command = UpdateConfiguration).grid(column = 0, row = 1, padx = 140, sticky = E)
- ttk.Label(tkForm, text = "PAS Access Type").grid(column = 0, row = 2, sticky = W)
- W_RadiobuttonAAS = Radiobutton(tkForm, text = "Get Scalar", variable = pasAccessType, value = 3).grid(column = 0, row = 3, padx = 135, sticky = W)
- W_RadiobuttonPAS = Radiobutton(tkForm, text = "Set Scalar", variable = pasAccessType, value = 4).grid(column = 0, row = 3, padx = 100, sticky = E)
- ttk.Label(tkForm, text = "Action ID / Param ID (hexadecimal)").grid(column = 0, row = 5, sticky = W)
- W_EntryActionID = Entry(tkForm, textvariable = actionID).grid(column = 0, row = 6)
- ttk.Label(tkForm, text = "Transaction ID (decimal)").grid(column = 0, row = 7, sticky = W)
- W_EntryTransactionID = Entry(tkForm, textvariable = transactionID).grid(column = 0, row = 8)
- W_CheckbuttonAutoTransactionID = Checkbutton(tkForm, text = "Auto", variable = transactionIDAuto, onvalue = 1, offvalue = 0).grid(column = 0, row = 8, padx = 80, sticky = E)
- ttk.Label(tkForm, text = "AMS Version (hexadecimal)").grid(column = 0, row = 9, sticky = W)
- W_EntryTransactionID = Entry(tkForm, textvariable = amsVersion).grid(column = 0, row = 10)
- ttk.Label(tkForm, text = "Arguments (hexadecimal)").grid(column = 0, row = 11, sticky = W)
- W_EntryCommandArgument = Entry(tkForm, textvariable = commandArgument).grid(column = 0, row = 12)
- W_ButtonSendPacket = Button(tkForm, text = "Send Packet", command = SendForm, width = 20).grid(column = 0, row = 13, pady = 10)
- ttk.Label(tkForm, text = "Response Log (top is most recent)").grid(column = 0, row = 14, sticky = W)
- W_TreeviewResponseLog = ttk.Treeview(tkForm, column = ("Transaction ID", "Data"), show = "headings", height = 6)
- W_TreeviewResponseLog.grid(column = 0, row = 15)
- W_TreeviewResponseLog.heading("#1", text="Transaction ID (decimal)")
- W_TreeviewResponseLog.heading("#2", text="Data (hexadecimal)")
- ttk.Label(tkForm, text = "Error Log (top is most recent)").grid(column = 0, row = 16, sticky = W)
- W_TreeviewErrorLog = ttk.Treeview(tkForm, column = ("Transaction ID", "Status code"), show = "headings", height = 6)
- W_TreeviewErrorLog.grid(column = 0, row = 17)
- W_TreeviewErrorLog.heading("#1", text="Transaction ID (decimal)")
- W_TreeviewErrorLog.heading("#2", text="Status Code")
- def HandleAMSPackets():
- with receiveBufferMutex:
- for packet in receiveBuffer:
- print(f"Received AMS Packet: 0x{packet}")
- if len(packet) < 4:
- print(f"Received an AMS packet which is too short (length is {len(packet) / 2} bytes)")
- continue
- responseType = packet[0:2]
- transactionID = packet[2:4]
- if responseType == "01":
- responseData = packet[4:-8] # account for hash on the end which is 4 bytes
- displayResponseData = responseData
- if displayResponseData == "":
- displayResponseData = "(no response)"
- W_TreeviewResponseLog.insert("", 0, values = [int(transactionID, 16), f"{displayResponseData}"])
- elif responseType == "02":
- statusCode = packet[4:12]
- statusCodeText = [
- "Success",
- "Generic Failure",
- "Invalid Parameter",
- "Timeout",
- "Insufficient Resources",
- "Not Implemented"
- ]
- statusCodeValue = int(statusCode, 16)
- W_TreeviewErrorLog.insert("", 0, values = [int(transactionID, 16), f"{statusCodeText[statusCodeValue]} ({statusCodeValue})"])
- else:
- print(f"[Error] Received an unknown response type: {responseType}")
- continue
- receiveBuffer.clear()
- tkRoot.mainloop()
- t.join()
Advertisement
Add Comment
Please, Sign In to add comment