From c54705909a47b502a2f7d13dcbb0894574723285 Mon Sep 17 00:00:00 2001
From: "Robin.Mueller" <robin.mueller.m@gmail.com>
Date: Tue, 6 Oct 2020 01:14:14 +0200
Subject: [PATCH] cleaning up a bit

---
 config/obsw_config.py           |  2 +-
 obsw_tmtc_client.py             |  4 +--
 utility/obsw_binary_uploader.py | 51 ++++++++++++++++++++++-----------
 3 files changed, 38 insertions(+), 19 deletions(-)

diff --git a/config/obsw_config.py b/config/obsw_config.py
index 55c878e..24dd993 100644
--- a/config/obsw_config.py
+++ b/config/obsw_config.py
@@ -69,7 +69,7 @@ G_APID = 0x73
 
 # Binary Upload Settings
 G_MAX_BINARY_FRAME_LENGTH = 1500
-G_MAX_APP_DATA_LENGTH = G_MAX_BINARY_FRAME_LENGTH - 10
+G_MAX_APP_DATA_LENGTH = G_MAX_BINARY_FRAME_LENGTH - 100
 
 G_COM_IF = 2
 # COM Port for serial communication
diff --git a/obsw_tmtc_client.py b/obsw_tmtc_client.py
index c5405fc..21785be 100755
--- a/obsw_tmtc_client.py
+++ b/obsw_tmtc_client.py
@@ -48,7 +48,7 @@ from tmtc_core.utility.obsw_logger import set_tmtc_logger, get_logger
 from test.obsw_pus_service_test import run_selected_pus_tests
 from tc.obsw_pus_tc_packer import create_total_tc_queue, ServiceQueuePacker
 from utility.obsw_args_parser import parse_input_arguments
-from utility.obsw_binary_uploader import perform_binary_upload
+from utility.obsw_binary_uploader import perform_file_upload
 
 from gui.obsw_tmtc_gui import TmTcGUI
 from gui.obsw_backend_test import TmTcBackend
@@ -205,7 +205,7 @@ class TmTcHandler:
         elif self.mode == g.ModeList.BinaryUploadMode:
             # Upload binary, prompt user for input, in the end prompt for new mode and enter that
             # mode
-            perform_binary_upload(self.communication_interface, self.tmtc_printer)
+            perform_file_upload(self.communication_interface, self.tmtc_printer)
             self.command_received = True
             self.mode = g.ModeList.ListenerMode
 
diff --git a/utility/obsw_binary_uploader.py b/utility/obsw_binary_uploader.py
index d28fd1d..d6e4671 100644
--- a/utility/obsw_binary_uploader.py
+++ b/utility/obsw_binary_uploader.py
@@ -21,11 +21,14 @@ from tmtc_core.utility.obsw_logger import get_logger
 LOGGER = get_logger()
 
 
-def perform_binary_upload(com_if: CommunicationInterface, tmtc_printer: TmTcPrinter):
+def perform_file_upload(com_if: CommunicationInterface, tmtc_printer: TmTcPrinter):
+    # TODO: prompt whether this is a binary upload or a normal file upload. Or use op code
+    #       two different commands to achieve the same.
     print("Please select file to upload")
     root = tk.Tk()
     root.withdraw()
     root.wm_attributes('-topmost', 1)
+
     # TODO: implement command line version which just parses the _bin folder, displays the options
     #       and asks for a selection.
     file_path = filedialog.askopenfilename(parent=root)
@@ -38,6 +41,34 @@ def perform_binary_upload(com_if: CommunicationInterface, tmtc_printer: TmTcPrin
         calc_hamming_code = False
         print("Hamming code will not be calculated")
 
+    iobc_prompt = input("iOBC? [y/n]: ")
+    if iobc_prompt in ['y', 'yes', 1]:
+        iobc_prompt = True
+    else:
+        iobc_prompt = False
+
+    bootloader_prompt = input("Bootloader (0) or Software Image (1)? [0/1]: ")
+    if bootloader_prompt == 0:
+        bootloader_prompt = True
+    else:
+        bootloader_prompt = False
+
+    if bootloader_prompt:
+        file_name = "bl.bin"
+    else:
+        file_name = "obsw_up.bin"
+
+    if iobc_prompt:
+        if bootloader_prompt:
+            repository_name = "BIN/IOBC/BL"
+        else:
+            repository_name = "BIN/IOBC/OBSW"
+    else:
+        if bootloader_prompt:
+            repository_name = "BIN/AT91/BL"
+        else:
+            repository_name = "BIN/AT91/OBSW"
+
     # Right now, the size of PUS packets is limited to 1500 bytes which also limits the app
     # data length.
     frame_length = g.G_MAX_APP_DATA_LENGTH
@@ -56,23 +87,11 @@ def perform_binary_upload(com_if: CommunicationInterface, tmtc_printer: TmTcPrin
     print("Generating folder structure..")
     tc_queue = deque()
 
-    # generate_generic_folder_structure(tc_queue, 0)
-    # while tc_queue:
-    #     (tc_packet, tc_info) = tc_queue.pop()
-    #     if not isinstance(tc_packet, str):
-    #         com_if.send_telecommand(tc_packet, tc_info)
-    #     elif tc_packet == "print":
-    #         LOGGER.info(tc_info)
-    #
-    #     time.sleep(0.5)
-    #     tm_list = com_if.receive_telemetry()
-    #     LOGGER.info(str(len(tm_list)) + " TM packets received..")
-
     # Delete existing binary file first, otherwise data might be appended to otherwise
     # valid file which already exists.
     file_transfer_helper = FileTransferHelper(
-        tc_queue=tc_queue, max_size_of_app_data=1400, target_repository="BIN/AT91/BL",
-        target_filename="bl.bin")
+        tc_queue=tc_queue, max_size_of_app_data=frame_length, target_repository=repository_name,
+        target_filename=file_name)
 
     init_ssc = 0
     tmtc_printer.set_display_mode(DisplayMode.SHORT)
@@ -94,7 +113,7 @@ def perform_binary_upload(com_if: CommunicationInterface, tmtc_printer: TmTcPrin
         elif tc_packet == "print":
             LOGGER.info(tc_info)
 
-        time.sleep(0.2)
+        time.sleep(0.5)
         tm_list = com_if.receive_telemetry()
         for tm_packet in tm_list:
             # gotta configure tm listener for this to work..
-- 
GitLab