From df593dc416fc727445aa382bfc63d85e49f22e54 Mon Sep 17 00:00:00 2001 From: drunsinn Date: Sat, 12 Dec 2020 11:58:10 +0100 Subject: [PATCH] check filename anf use binary mode if necessary --- README.md | 2 +- pyLSV2/__init__.py | 2 +- pyLSV2/client.py | 29 ++++++++++++++++++++++------- scripts/check_for_LSV2_commands.py | 1 - 4 files changed, 24 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 3318ffb..bd0bb05 100644 --- a/README.md +++ b/README.md @@ -81,7 +81,7 @@ DNC login is only possible if the option is set on the control, without the option you get an error when trying to login with DNC. -### Creating a log file +### Creating a log file - not implemented By recording the communiction with Wireshark between the control and TNCremo the following sequence was accired. ``` diff --git a/pyLSV2/__init__.py b/pyLSV2/__init__.py index 0580373..2c7e66a 100644 --- a/pyLSV2/__init__.py +++ b/pyLSV2/__init__.py @@ -3,4 +3,4 @@ """A pure Python3 implementation of the LSV2 protocol""" from .client import LSV2 -__version__ = '0.5.0' +__version__ = '0.5.1' diff --git a/pyLSV2/client.py b/pyLSV2/client.py index 15c6097..b6ce3d7 100644 --- a/pyLSV2/client.py +++ b/pyLSV2/client.py @@ -30,8 +30,10 @@ class LSV2(): DRIVE_TNC = 'PLC:' DRIVE_LOG = 'LOG:' - BIN_FILES = '.ads .bak .bck .bin .bmp .bmx .chm .cyc .cy% .dmp .dll .eak .elf .enc .exe .gds .gif .hbi .he .ioc .iocp .jpg .jpeg .map .mds .mo .omf .pdf .png .pyc .s .sds .sk .str .xml .xls .xrs .zip'.split( - ' ') + BIN_FILES = ('.ads', '.bak', '.bck', '.bin', '.bmp', '.bmx', '.chm', '.cyc', '.cy%', + '.dmp', '.dll', '.eak', '.elf', '.enc', '.exe', '.gds', '.gif', '.hbi', '.he', '.ioc', + '.iocp', '.jpg', '.jpeg', '.map', '.mds', '.mo', '.omf', '.pdf', '.png', '.pyc', '.s', + '.sds', '.sk', '.str', '.xml', '.xls', '.xrs', '.zip') # const for login LOGIN_INSPECT = 'INSPECT' # nur lesende Funktionen ausführbar @@ -209,6 +211,7 @@ class LSV2(): COMMAND_R_DR_MODE_DRIVES = 0x02 C_FL_MODE_BINARY = 0x01 # is set by TNCcmd, seems to work for all filetypes + R_FL_MODE_BINARY = 0x01 # enable binary file transfer, see also C_FL_MODE_BINARY def __init__(self, hostname, port=0, timeout=15.0, safe_mode=True): """init object variables and create socket""" @@ -810,7 +813,9 @@ def move_local_file(self, source_path, target_path): return True def send_file(self, local_path, remote_path, override_file=False, binary_mode=False): - """send file to the control, parameter override_file allowes replacing an existing file""" + """send file to the control, parameter override_file allowes replacing an existing file + with parameter binary mode you can select the transfer mode. it it is not set the filename is + checked against a know list of binary extentions""" local_file = Path(local_path) if not local_file.is_file(): @@ -859,8 +864,9 @@ def send_file(self, local_path, remote_path, override_file=False, binary_mode=Fa payload = bytearray() payload.extend(map(ord, remote_folder + '/' + remote_file_name)) payload.append(0x00) - if binary_mode: + if binary_mode or self._is_file_type_binary(local_path): payload.append(LSV2.C_FL_MODE_BINARY) + logging.info('useing binary transfer mode') response, content = self._llcom.telegram( LSV2.COMMAND_C_FL, payload, buffer_size=self._buffer_size) @@ -900,7 +906,9 @@ def send_file(self, local_path, remote_path, override_file=False, binary_mode=Fa return True def recive_file(self, remote_path, local_path, override_file=False, binary_mode=False): - '''send file to the control, parameter override_file allowes replacing an existing file''' + '''send file to the control, parameter override_file allowes replacing an existing file + with parameter binary mode you can select the transfer mode. it it is not set the filename is + checked against a know list of binary extentions''' remote_file_info = self.get_file_info(remote_path) if not remote_file_info: @@ -925,8 +933,9 @@ def recive_file(self, remote_path, local_path, override_file=False, binary_mode= payload = bytearray() payload.extend(map(ord, remote_path)) payload.append(0x00) - if binary_mode: - payload.append(0x01) # force binary transfer? + if binary_mode or self._is_file_type_binary(remote_path): + payload.append(LSV2.R_FL_MODE_BINARY) # force binary transfer + logging.info('useing binary transfer mode') response, content = self._llcom.telegram( LSV2.COMMAND_R_FL, payload, buffer_size=self._buffer_size) @@ -977,6 +986,12 @@ def recive_file(self, remote_path, local_path, override_file=False, binary_mode= return True + def _is_file_type_binary(self, file_name): + for bin_type in self.BIN_FILES: + if file_name.endswith(bin_type): + return True + return False + def _test_command(self, command_string, payload=None): """check commands for validity""" response, content = self._llcom.telegram( diff --git a/scripts/check_for_LSV2_commands.py b/scripts/check_for_LSV2_commands.py index f6c84cc..8b85221 100644 --- a/scripts/check_for_LSV2_commands.py +++ b/scripts/check_for_LSV2_commands.py @@ -1,6 +1,5 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -import struct import logging import string import time