From f497a45f4e7cca3b207df031f5c40a9b75a1138c Mon Sep 17 00:00:00 2001
From: "Robin.Mueller" <robin.mueller.m@gmail.com>
Date: Sat, 31 Oct 2020 17:07:41 +0100
Subject: [PATCH] new CLI argument for listener mode

---
 config/obsw_config.py       |  4 +++-
 core/tmtc_backend.py        |  5 ++---
 core/tmtc_client_core.py    | 13 ++++++-------
 tmtc_core                   |  2 +-
 utility/obsw_args_parser.py |  4 ++++
 5 files changed, 16 insertions(+), 12 deletions(-)

diff --git a/config/obsw_config.py b/config/obsw_config.py
index f5fdb83..dae9523 100644
--- a/config/obsw_config.py
+++ b/config/obsw_config.py
@@ -65,6 +65,7 @@ G_SCRIPT_MODE = 1
 G_MODE_ID: ModeList = ModeList.ListenerMode
 G_SERVICE = 17
 G_OP_CODE = 0
+G_LISTENER_AFTER_OP = False
 G_DISPLAY_MODE = "long"
 
 # General TMTC Settings
@@ -118,7 +119,7 @@ def set_globals(args):
     global G_ETHERNET_RECV_ADDRESS, G_ETHERNET_SEND_ADDRESS, G_SCRIPT_MODE, G_MODE_ID, G_SERVICE, \
         G_DISPLAY_MODE, G_COM_IF, G_COM_PORT, G_SERIAL_TIMEOUT, G_TM_TIMEOUT, \
         G_TC_SEND_TIMEOUT_FACTOR, G_PRINT_TO_FILE, G_PRINT_HK_DATA, G_PRINT_RAW_TM, G_PRINT_TM, \
-        G_OP_CODE, G_RESEND_TC
+        G_OP_CODE, G_RESEND_TC, G_LISTENER_AFTER_OP
 
     if args.shortDisplayMode:
         G_DISPLAY_MODE = "short"
@@ -171,6 +172,7 @@ def set_globals(args):
     G_COM_PORT = args.com_port
     G_TM_TIMEOUT = args.tm_timeout
     G_RESEND_TC = args.resend_tc
+    G_LISTENER_AFTER_OP = args.listener
 
     from config.obsw_user_code import global_setup_hook
     global_setup_hook()
diff --git a/core/tmtc_backend.py b/core/tmtc_backend.py
index 3936828..392a009 100644
--- a/core/tmtc_backend.py
+++ b/core/tmtc_backend.py
@@ -93,11 +93,10 @@ class TmTcHandler:
         try:
             self.__core_operation(self.one_shot_operation)
         except KeyboardInterrupt:
-            LOGGER.info("Closing TMTC client.")
+            LOGGER.info("Keyboard Interrupt.")
             sys.exit()
         except IOError as e:
-            LOGGER.exception(e)
-            LOGGER.info("Closing TMTC client.")
+            LOGGER.error("IO Error occured!")
             sys.exit()
 
     def __handle_action(self):
diff --git a/core/tmtc_client_core.py b/core/tmtc_client_core.py
index 01ed6fd..c3f46ba 100755
--- a/core/tmtc_client_core.py
+++ b/core/tmtc_client_core.py
@@ -56,18 +56,17 @@ def run_tmtc_client(use_gui: bool):
 
     # Currently does not work, problems with QEMU / Asyncio
     if not use_gui:
-        tmtc_handler_task = TmTcHandler.prepare_tmtc_handler_start(g.G_MODE_ID)
+        tmtc_handler = TmTcHandler(g.G_MODE_ID)
+        tmtc_handler.set_one_shot_or_loop_handling(g.G_LISTENER_AFTER_OP)
+        tmtc_handler.initialize()
+        tmtc_handler.perform_operation()
     else:
         tmtc_handler_task = TmTcHandler.prepare_tmtc_handler_start()
-
-    if use_gui:
         tmtc_frontend = TmTcFrontend()
         tmtc_frontend_task = tmtc_frontend.prepare_start(tmtc_frontend)
         tmtc_frontend_task.start()
-    tmtc_handler_task.start()
-
-    tmtc_handler_task.join()
-    if use_gui:
+        tmtc_handler_task.start()
+        tmtc_handler_task.join()
         tmtc_frontend_task.join()
 
 
diff --git a/tmtc_core b/tmtc_core
index bef5d1f..3a72844 160000
--- a/tmtc_core
+++ b/tmtc_core
@@ -1 +1 @@
-Subproject commit bef5d1f8e52aff053a953e91bc287e7772ff3148
+Subproject commit 3a728442dc9b5a9200272205bd6972762b3ca06f
diff --git a/utility/obsw_args_parser.py b/utility/obsw_args_parser.py
index d226d02..47f2018 100644
--- a/utility/obsw_args_parser.py
+++ b/utility/obsw_args_parser.py
@@ -31,6 +31,10 @@ def parse_input_arguments():
     arg_parser.add_argument(
         '--boardIP', help='Board IP. Default: Localhost 127.0.0.1', default="127.0.0.1")
     arg_parser.add_argument('-s', '--service', help='Service to test. Default: 17', default=17)
+    arg_parser.add_argument(
+        '-l','--listener',  help='Determine whether the listener mode will be active after '
+                                 'performing the operation',
+        action='store_false')
     arg_parser.add_argument(
         '-t', '--tm_timeout', type=float, help='TM Timeout when listening to verification sequence.'
         ' Default: 5 seconds', default=5.0)
-- 
GitLab