Skip to content

Commit

Permalink
check filename anf use binary mode if necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
drunsinn committed Dec 12, 2020
1 parent 35e56cb commit df593dc
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

```
Expand Down
2 changes: 1 addition & 1 deletion pyLSV2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
"""A pure Python3 implementation of the LSV2 protocol"""
from .client import LSV2

__version__ = '0.5.0'
__version__ = '0.5.1'
29 changes: 22 additions & 7 deletions pyLSV2/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"""
Expand Down Expand Up @@ -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():
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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:
Expand All @@ -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)

Expand Down Expand Up @@ -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(
Expand Down
1 change: 0 additions & 1 deletion scripts/check_for_LSV2_commands.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import struct
import logging
import string
import time
Expand Down

0 comments on commit df593dc

Please sign in to comment.