From e483c507fede47cb8a17fc89420054d32c9fe4c3 Mon Sep 17 00:00:00 2001 From: rashevskyv Date: Wed, 24 Nov 2021 17:58:53 +0200 Subject: [PATCH] added dbi backend --- dbibackend | 278 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 278 insertions(+) create mode 100644 dbibackend diff --git a/dbibackend b/dbibackend new file mode 100644 index 0000000..e3a38bc --- /dev/null +++ b/dbibackend @@ -0,0 +1,278 @@ +#!/usr/bin/python3 +# This script depends on PyUSB. You can get it with pip install pyusb. +# You will also need libusb installed + +import usb.core +import usb.util +import struct +import sys +import time +import threading + +from binascii import hexlify as hx, unhexlify as uhx +from pathlib import Path + +from tkinter import filedialog +import tkinter as tk +from tkinter import * + +from tkinter import scrolledtext + +CMD_ID_EXIT = 0 +CMD_ID_LIST_OLD = 1 +CMD_ID_FILE_RANGE = 2 +CMD_ID_LIST = 3 + +CMD_TYPE_REQUEST = 0 +CMD_TYPE_RESPONSE = 1 +CMD_TYPE_ACK = 2 + +BUFFER_SEGMENT_DATA_SIZE = 0x100000 + +in_ep = None +out_ep = None + +file_list = {} + +def LOG(line): + if int(text1.index('end').split('.')[0]) - 1 > 1000: # max 1000 lines of log are shown + text1.delete('1.0', tk.END) + text1.insert(tk.END, line+'\n') + text1.see(tk.END) + +def process_file_range_command(data_size): + global file_list + + LOG('File range') + out_ep.write(struct.pack('<4sIII', b'DBI0', CMD_TYPE_ACK, CMD_ID_FILE_RANGE, data_size)) + + file_range_header = in_ep.read(data_size) + + range_size = struct.unpack('= end_off: + read_size = end_off - curr_off + + buf = f.read(read_size) + out_ep.write(data=buf, timeout=0) + curr_off += read_size + +def poll_commands(): + LOG('Entering command loop') + while True: + try: + cmd_header = bytes(in_ep.read(16, timeout=0)) + magic = cmd_header[:4] + + if magic != b'DBI0': + continue + + cmd_type = struct.unpack(' 0: + ack = bytes(in_ep.read(16, timeout=0)) + magic = ack[:4] + cmd_type = struct.unpack(' 0: + startServerButton['state'] = 'normal' + else: + startServerButton['state'] = 'disabled' + +def gui_choose_dir(): + global file_list + filename = filedialog.askdirectory() + if filename == () or filename == '': + return + d = Path(filename) + for file_path in [f for f in d.iterdir() if f.is_file()]: + file_list.update({file_path.name : file_path.resolve()}) + + updateFileList() + +def gui_choose_files(): + global file_list + filenames = filedialog.askopenfilenames() + for file_path in filenames: + d = Path(file_path) + file_list.update({d.name : d.resolve()}) + + updateFileList() + +def gui_clear_list(): + global file_list + + file_list.clear() + updateFileList() + +def start_gui(start): + global addFolderButton + global addFilesButton + global clearListButton + global startServerButton + global text1 + global flist1 + global root + + root = tk.Tk() + root.resizable(False, False) + root.title('DBI backend GUI') + + canvas1 = tk.Canvas(root, width = 800, height = 500) + canvas1.pack() + addFolderButton = tk.Button(text='Add folder',command=gui_choose_dir,width=10) + canvas1.create_window(0, 0, window=addFolderButton, anchor=tk.NW) + addFilesButton = tk.Button(text='Add files',command=gui_choose_files,width=10) + canvas1.create_window(100, 0, window=addFilesButton, anchor=tk.NW) + clearListButton = tk.Button(text='Clear list',command=gui_clear_list,width=10) + canvas1.create_window(200, 0, window=clearListButton, anchor=tk.NW) + + flist1 = tk.scrolledtext.ScrolledText(height=20, width=112) + canvas1.create_window(0, 30, window=flist1, anchor=tk.NW) + + startServerButton = tk.Button(text='Start server',state=tk.DISABLED, command=do_start_server,width=10) + canvas1.create_window(0, 325, window=startServerButton, anchor=tk.NW) + + text1 = tk.scrolledtext.ScrolledText(height=10, width=112) + canvas1.create_window(0, 355, window=text1, anchor=tk.NW) + + if start: + updateFileList() + do_start_server() + root.mainloop() + + +if len(sys.argv) < 2: + start_gui(False) +else: + for filename in sys.argv[1:]: + d = Path(filename) + if d.is_file(): + file_list.update({d.name: d.resolve()}) + else: + for file_path in [f for f in d.iterdir() if f.is_file()]: + file_list.update({file_path.name : file_path.resolve()}) + + start_gui(True) +