etherlab/runtime_etherlab.py
author Andrey Skvortsov <andrej.skvortzov@gmail.com>
Sat, 29 Sep 2018 15:24:07 +0300
changeset 2383 6cc3460be285
parent 2375 cfa68a06a24d
child 2389 f86eefc8d378
permissions -rw-r--r--
clean etherlab: pep8, W191 indentation contains tabs
2357
7c67286cddbe cleanup etherlab: pep8, E401 multiple imports on one line
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2355
diff changeset
     1
import os
7c67286cddbe cleanup etherlab: pep8, E401 multiple imports on one line
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2355
diff changeset
     2
import subprocess
7c67286cddbe cleanup etherlab: pep8, E401 multiple imports on one line
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2355
diff changeset
     3
import sys
7c67286cddbe cleanup etherlab: pep8, E401 multiple imports on one line
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2355
diff changeset
     4
import ctypes
2086
8e4992e0f147 Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff changeset
     5
from threading import Thread
2357
7c67286cddbe cleanup etherlab: pep8, E401 multiple imports on one line
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2355
diff changeset
     6
import ctypes
7c67286cddbe cleanup etherlab: pep8, E401 multiple imports on one line
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2355
diff changeset
     7
import time
7c67286cddbe cleanup etherlab: pep8, E401 multiple imports on one line
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2355
diff changeset
     8
import re
2115
edb49073227e Added error logging when SDO FB fails
Edouard Tisserant
parents: 2114
diff changeset
     9
from targets.typemapping import LogLevelsDict
2086
8e4992e0f147 Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff changeset
    10
8e4992e0f147 Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff changeset
    11
SDOAnswered = PLCBinary.SDOAnswered
8e4992e0f147 Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff changeset
    12
SDOAnswered.restype = None
8e4992e0f147 Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff changeset
    13
SDOAnswered.argtypes = []
8e4992e0f147 Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff changeset
    14
8e4992e0f147 Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff changeset
    15
SDOThread = None
2132
9f5e4dc43053 Added support for stopping SDOThreadProc when stopping PLC
Laurent Bessard
parents: 2116
diff changeset
    16
SDOProc = None
2086
8e4992e0f147 Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff changeset
    17
Result = None
8e4992e0f147 Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff changeset
    18
2360
2a3d022a7dac cleanup etherlab: pep8, E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2358
diff changeset
    19
2086
8e4992e0f147 Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff changeset
    20
def SDOThreadProc(*params):
2132
9f5e4dc43053 Added support for stopping SDOThreadProc when stopping PLC
Laurent Bessard
parents: 2116
diff changeset
    21
    global Result, SDOProc
2086
8e4992e0f147 Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff changeset
    22
    if params[0] == "upload":
2115
edb49073227e Added error logging when SDO FB fails
Edouard Tisserant
parents: 2114
diff changeset
    23
        cmdfmt = "ethercat upload -p %d -t %s 0x%.4x 0x%.2x"
2086
8e4992e0f147 Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff changeset
    24
    else:
2115
edb49073227e Added error logging when SDO FB fails
Edouard Tisserant
parents: 2114
diff changeset
    25
        cmdfmt = "ethercat download -p %d -t %s 0x%.4x 0x%.2x %s"
2355
fec77f2b9e07 cleanup etherlab: pep8, W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2353
diff changeset
    26
2115
edb49073227e Added error logging when SDO FB fails
Edouard Tisserant
parents: 2114
diff changeset
    27
    command = cmdfmt % params[1:]
2132
9f5e4dc43053 Added support for stopping SDOThreadProc when stopping PLC
Laurent Bessard
parents: 2116
diff changeset
    28
    SDOProc = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)
9f5e4dc43053 Added support for stopping SDOThreadProc when stopping PLC
Laurent Bessard
parents: 2116
diff changeset
    29
    res = SDOProc.wait()
9f5e4dc43053 Added support for stopping SDOThreadProc when stopping PLC
Laurent Bessard
parents: 2116
diff changeset
    30
    output = SDOProc.communicate()[0]
2355
fec77f2b9e07 cleanup etherlab: pep8, W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2353
diff changeset
    31
2086
8e4992e0f147 Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff changeset
    32
    if params[0] == "upload":
8e4992e0f147 Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff changeset
    33
        Result = None
8e4992e0f147 Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff changeset
    34
        if res == 0:
8e4992e0f147 Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff changeset
    35
            if params[2] in ["float", "double"]:
8e4992e0f147 Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff changeset
    36
                Result = float(output)
8e4992e0f147 Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff changeset
    37
            elif params[2] in ["string", "octet_string", "unicode_string"]:
8e4992e0f147 Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff changeset
    38
                Result = output
8e4992e0f147 Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff changeset
    39
            else:
8e4992e0f147 Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff changeset
    40
                hex_value, dec_value = output.split()
8e4992e0f147 Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff changeset
    41
                if int(hex_value, 16) == int(dec_value):
8e4992e0f147 Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff changeset
    42
                    Result = int(dec_value)
8e4992e0f147 Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff changeset
    43
    else:
8e4992e0f147 Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff changeset
    44
        Result = res == 0
2355
fec77f2b9e07 cleanup etherlab: pep8, W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2353
diff changeset
    45
2086
8e4992e0f147 Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff changeset
    46
    SDOAnswered()
2375
cfa68a06a24d clean etherlab: pep8, E203 whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2372
diff changeset
    47
    if res != 0:
2115
edb49073227e Added error logging when SDO FB fails
Edouard Tisserant
parents: 2114
diff changeset
    48
        PLCObject.LogMessage(
2355
fec77f2b9e07 cleanup etherlab: pep8, W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2353
diff changeset
    49
            LogLevelsDict["WARNING"],
2363
9c7da6ff6a34 cleanup etherlab: pep8, E231 missing whitespace after ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2360
diff changeset
    50
            "%s : %s" % (command, output))
2355
fec77f2b9e07 cleanup etherlab: pep8, W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2353
diff changeset
    51
2360
2a3d022a7dac cleanup etherlab: pep8, E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2358
diff changeset
    52
2086
8e4992e0f147 Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff changeset
    53
def EthercatSDOUpload(pos, index, subindex, var_type):
8e4992e0f147 Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff changeset
    54
    global SDOThread
8e4992e0f147 Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff changeset
    55
    SDOThread = Thread(target=SDOThreadProc, args=["upload", pos, var_type, index, subindex])
8e4992e0f147 Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff changeset
    56
    SDOThread.start()
2355
fec77f2b9e07 cleanup etherlab: pep8, W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2353
diff changeset
    57
2360
2a3d022a7dac cleanup etherlab: pep8, E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2358
diff changeset
    58
2086
8e4992e0f147 Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff changeset
    59
def EthercatSDODownload(pos, index, subindex, var_type, value):
8e4992e0f147 Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff changeset
    60
    global SDOThread
8e4992e0f147 Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff changeset
    61
    SDOThread = Thread(target=SDOThreadProc, args=["download", pos, var_type, index, subindex, value])
8e4992e0f147 Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff changeset
    62
    SDOThread.start()
8e4992e0f147 Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff changeset
    63
2360
2a3d022a7dac cleanup etherlab: pep8, E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2358
diff changeset
    64
2086
8e4992e0f147 Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff changeset
    65
def GetResult():
8e4992e0f147 Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff changeset
    66
    global Result
8e4992e0f147 Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff changeset
    67
    return Result
2114
fc1bc441cf71 Added logging based on collecting Kernel logs
Edouard Tisserant
parents: 2086
diff changeset
    68
2370
e40f3914e55f cleanup etherlab: pep8, E305 expected 2 blank lines after class or function definition, found X
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2366
diff changeset
    69
2365
bc07b9910cdb cleanup etherlab: pep8, E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2363
diff changeset
    70
KMSGPollThread = None
bc07b9910cdb cleanup etherlab: pep8, E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2363
diff changeset
    71
StopKMSGThread = False
2360
2a3d022a7dac cleanup etherlab: pep8, E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2358
diff changeset
    72
2a3d022a7dac cleanup etherlab: pep8, E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2358
diff changeset
    73
2114
fc1bc441cf71 Added logging based on collecting Kernel logs
Edouard Tisserant
parents: 2086
diff changeset
    74
def KMSGPollThreadProc():
fc1bc441cf71 Added logging based on collecting Kernel logs
Edouard Tisserant
parents: 2086
diff changeset
    75
    """
fc1bc441cf71 Added logging based on collecting Kernel logs
Edouard Tisserant
parents: 2086
diff changeset
    76
    Logs Kernel messages starting with EtherCAT
fc1bc441cf71 Added logging based on collecting Kernel logs
Edouard Tisserant
parents: 2086
diff changeset
    77
    Uses GLibc wrapper to Linux syscall "klogctl"
2355
fec77f2b9e07 cleanup etherlab: pep8, W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2353
diff changeset
    78
    Last 4 KB are polled, and lines compared to last
2114
fc1bc441cf71 Added logging based on collecting Kernel logs
Edouard Tisserant
parents: 2086
diff changeset
    79
    captured line to detect new lines
fc1bc441cf71 Added logging based on collecting Kernel logs
Edouard Tisserant
parents: 2086
diff changeset
    80
    """
fc1bc441cf71 Added logging based on collecting Kernel logs
Edouard Tisserant
parents: 2086
diff changeset
    81
    global StopKMSGThread
2365
bc07b9910cdb cleanup etherlab: pep8, E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2363
diff changeset
    82
    libc = ctypes.CDLL("libc.so.6")
2114
fc1bc441cf71 Added logging based on collecting Kernel logs
Edouard Tisserant
parents: 2086
diff changeset
    83
    klog = libc.klogctl
fc1bc441cf71 Added logging based on collecting Kernel logs
Edouard Tisserant
parents: 2086
diff changeset
    84
    klog.argtypes = [ctypes.c_int, ctypes.c_char_p, ctypes.c_int]
fc1bc441cf71 Added logging based on collecting Kernel logs
Edouard Tisserant
parents: 2086
diff changeset
    85
    klog.restype = ctypes.c_int
2365
bc07b9910cdb cleanup etherlab: pep8, E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2363
diff changeset
    86
    s = ctypes.create_string_buffer(4*1024)
2114
fc1bc441cf71 Added logging based on collecting Kernel logs
Edouard Tisserant
parents: 2086
diff changeset
    87
    last = None
fc1bc441cf71 Added logging based on collecting Kernel logs
Edouard Tisserant
parents: 2086
diff changeset
    88
    while not StopKMSGThread:
2372
51f3acc89a58 clean etherlab: pep8, E741 ambiguous variable name 'l'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2370
diff changeset
    89
        bytes_to_read = klog(3, s, len(s)-1)
51f3acc89a58 clean etherlab: pep8, E741 ambiguous variable name 'l'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2370
diff changeset
    90
        log = s.value[:bytes_to_read-1]
2375
cfa68a06a24d clean etherlab: pep8, E203 whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2372
diff changeset
    91
        if last:
2114
fc1bc441cf71 Added logging based on collecting Kernel logs
Edouard Tisserant
parents: 2086
diff changeset
    92
            log = log.rpartition(last)[2]
2375
cfa68a06a24d clean etherlab: pep8, E203 whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2372
diff changeset
    93
        if log:
2114
fc1bc441cf71 Added logging based on collecting Kernel logs
Edouard Tisserant
parents: 2086
diff changeset
    94
            last = log.rpartition('\n')[2]
2363
9c7da6ff6a34 cleanup etherlab: pep8, E231 missing whitespace after ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2360
diff changeset
    95
            for lvl, msg in re.findall(
2116
2b1980a038b1 Better logging, saffer failure at init (allows restart)
Edouard Tisserant
parents: 2115
diff changeset
    96
                            r'<(\d)>\[\s*\d*\.\d*\]\s*(EtherCAT\s*.*)$',
2b1980a038b1 Better logging, saffer failure at init (allows restart)
Edouard Tisserant
parents: 2115
diff changeset
    97
                            log, re.MULTILINE):
2b1980a038b1 Better logging, saffer failure at init (allows restart)
Edouard Tisserant
parents: 2115
diff changeset
    98
                PLCObject.LogMessage(
2b1980a038b1 Better logging, saffer failure at init (allows restart)
Edouard Tisserant
parents: 2115
diff changeset
    99
                    LogLevelsDict[{
2363
9c7da6ff6a34 cleanup etherlab: pep8, E231 missing whitespace after ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2360
diff changeset
   100
                        "4": "WARNING",
9c7da6ff6a34 cleanup etherlab: pep8, E231 missing whitespace after ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2360
diff changeset
   101
                        "3": "CRITICAL"}.get(lvl, "DEBUG")],
2116
2b1980a038b1 Better logging, saffer failure at init (allows restart)
Edouard Tisserant
parents: 2115
diff changeset
   102
                    msg)
2355
fec77f2b9e07 cleanup etherlab: pep8, W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2353
diff changeset
   103
        time.sleep(0.5)
2114
fc1bc441cf71 Added logging based on collecting Kernel logs
Edouard Tisserant
parents: 2086
diff changeset
   104
2360
2a3d022a7dac cleanup etherlab: pep8, E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2358
diff changeset
   105
2114
fc1bc441cf71 Added logging based on collecting Kernel logs
Edouard Tisserant
parents: 2086
diff changeset
   106
def _runtime_etherlab_init():
fc1bc441cf71 Added logging based on collecting Kernel logs
Edouard Tisserant
parents: 2086
diff changeset
   107
    global KMSGPollThread, StopKMSGThread
fc1bc441cf71 Added logging based on collecting Kernel logs
Edouard Tisserant
parents: 2086
diff changeset
   108
    StopKMSGThread = False
2366
d635680e4c2c cleanup etherlab: pep8, E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2365
diff changeset
   109
    KMSGPollThread = Thread(target=KMSGPollThreadProc)
2114
fc1bc441cf71 Added logging based on collecting Kernel logs
Edouard Tisserant
parents: 2086
diff changeset
   110
    KMSGPollThread.start()
fc1bc441cf71 Added logging based on collecting Kernel logs
Edouard Tisserant
parents: 2086
diff changeset
   111
2360
2a3d022a7dac cleanup etherlab: pep8, E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2358
diff changeset
   112
2114
fc1bc441cf71 Added logging based on collecting Kernel logs
Edouard Tisserant
parents: 2086
diff changeset
   113
def _runtime_etherlab_cleanup():
2132
9f5e4dc43053 Added support for stopping SDOThreadProc when stopping PLC
Laurent Bessard
parents: 2116
diff changeset
   114
    global KMSGPollThread, StopKMSGThread, SDOProc, SDOThread
9f5e4dc43053 Added support for stopping SDOThreadProc when stopping PLC
Laurent Bessard
parents: 2116
diff changeset
   115
    try:
9f5e4dc43053 Added support for stopping SDOThreadProc when stopping PLC
Laurent Bessard
parents: 2116
diff changeset
   116
        os.kill(SDOProc.pid, SIGTERM)
2353
8f1a2846b2f5 cleanup etherlab: pep8, E722 do not use bare except
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2132
diff changeset
   117
    except Exception:
2132
9f5e4dc43053 Added support for stopping SDOThreadProc when stopping PLC
Laurent Bessard
parents: 2116
diff changeset
   118
        pass
9f5e4dc43053 Added support for stopping SDOThreadProc when stopping PLC
Laurent Bessard
parents: 2116
diff changeset
   119
    SDOThread = None
2114
fc1bc441cf71 Added logging based on collecting Kernel logs
Edouard Tisserant
parents: 2086
diff changeset
   120
    StopKMSGThread = True
fc1bc441cf71 Added logging based on collecting Kernel logs
Edouard Tisserant
parents: 2086
diff changeset
   121
    KMSGPollThread = None