connectors/ERPC/__init__.py
author Edouard Tisserant <edouard.tisserant@gmail.com>
Thu, 28 Nov 2024 14:39:26 +0100
changeset 4054 079099299603
parent 4043 b217fb367574
permissions -rw-r--r--
Merge remote-tracking branch 'hggit/python3' into python3
3884
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/env python
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     3
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     4
# Written by Edouard TISSERANT (C) 2024
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     5
# This file is part of Beremiz IDE
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     6
# See COPYING file for copyrights details.
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     7
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     8
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     9
import os.path
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    10
import re
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    11
import traceback
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    12
from inspect import getmembers, isfunction
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    13
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    14
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    15
import erpc
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    16
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    17
# eRPC service code
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    18
from erpc_interface.erpc_PLCObject.interface import IBeremizPLCObjectService
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    19
from erpc_interface.erpc_PLCObject.client import BeremizPLCObjectServiceClient
3887
2df45e4bd500 Fix variable forcing. Now works with eRPC. PLCObject API changed.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3885
diff changeset
    20
from erpc_interface.erpc_PLCObject.common import trace_order, extra_file, PLCstatus_enum
3884
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    21
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    22
import PSKManagement as PSK
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    23
from connectors.ERPC.PSK_Adapter import SSLPSKClientTransport
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    24
from connectors.ConnectorBase import ConnectorBase
4041
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
    25
from connectors.ERPC_URI import per_scheme_model
3884
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    26
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    27
enum_to_PLCstatus = dict(map(lambda t:(t[1],t[0]),getmembers(PLCstatus_enum, lambda x:type(x)==int)))
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    28
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    29
class MissingCallException(Exception):
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    30
    pass
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    31
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    32
def ExceptionFromERPCReturn(ret):
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    33
    return {1:Exception,
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    34
            2:MissingCallException}.get(ret,ValueError)
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    35
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    36
def ReturnAsLastOutput(client_method, obj, args_wrapper, *args):
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    37
    retval = erpc.Reference()
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    38
    ret = client_method(obj, *args_wrapper(*args), retval)
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    39
    if ret != 0:
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    40
        raise ExceptionFromERPCReturn(ret)(client_method.__name__)
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    41
    return retval.value
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    42
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    43
def TranslatedReturnAsLastOutput(translator):
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    44
    def wrapper(client_method, obj, args_wrapper, *args):
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    45
        res = ReturnAsLastOutput(client_method, obj, args_wrapper, *args)
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    46
        return translator(res)
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    47
    return wrapper
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    48
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    49
ReturnWrappers = {
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    50
    "AppendChunkToBlob":ReturnAsLastOutput,
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    51
    "GetLogMessage":TranslatedReturnAsLastOutput(
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    52
        lambda res:(res.msg, res.tick, res.sec, res.nsec)),
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    53
    "GetPLCID":TranslatedReturnAsLastOutput(
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    54
        lambda res:(res.ID, res.PSK)),
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    55
    "GetPLCstatus":TranslatedReturnAsLastOutput(
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    56
        lambda res:(enum_to_PLCstatus[res.PLCstatus], res.logcounts)),
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    57
    "GetTraceVariables":TranslatedReturnAsLastOutput(
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    58
        lambda res:(enum_to_PLCstatus[res.PLCstatus],
3885
22a009561502 Fix variable tracing in new eRPC interface.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3884
diff changeset
    59
                    [(sample.tick, bytes(sample.TraceBuffer)) for sample in res.traces])),
3884
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    60
    "MatchMD5":ReturnAsLastOutput,
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    61
    "NewPLC":ReturnAsLastOutput,
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    62
    "SeedBlob":ReturnAsLastOutput,
3885
22a009561502 Fix variable tracing in new eRPC interface.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3884
diff changeset
    63
    "SetTraceVariablesList": ReturnAsLastOutput,
22a009561502 Fix variable tracing in new eRPC interface.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3884
diff changeset
    64
    "StopPLC":ReturnAsLastOutput,
4032
1ffff67678ad Add ExtendedCall to PLCObject as a replacement of RemoteExec and reflect it in eRPC interface.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3948
diff changeset
    65
    "ExtendedCall":ReturnAsLastOutput,
3884
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    66
}
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    67
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    68
ArgsWrappers = {
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    69
    "NewPLC":
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    70
        lambda md5sum, plcObjectBlobID, extrafiles: (
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    71
            md5sum, plcObjectBlobID, [extra_file(*f) for f in extrafiles]),
3887
2df45e4bd500 Fix variable forcing. Now works with eRPC. PLCObject API changed.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3885
diff changeset
    72
    "SetTraceVariablesList":
3885
22a009561502 Fix variable tracing in new eRPC interface.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3884
diff changeset
    73
        lambda orders : ([
3887
2df45e4bd500 Fix variable forcing. Now works with eRPC. PLCObject API changed.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3885
diff changeset
    74
            trace_order(idx, b"" if force is None else force) 
2df45e4bd500 Fix variable forcing. Now works with eRPC. PLCObject API changed.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3885
diff changeset
    75
            for idx, force in orders],)
3884
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    76
}
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    77
4040
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
    78
def rpc_wrapper(method_name, confnodesroot):
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
    79
    client_method = getattr(BeremizPLCObjectServiceClient, method_name)
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
    80
    return_wrapper = ReturnWrappers.get(
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
    81
        method_name, 
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
    82
        lambda client_method, obj, args_wrapper, *args: client_method(obj, *args_wrapper(*args)))
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
    83
    args_wrapper = ArgsWrappers.get(method_name, lambda *x:x)
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
    84
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
    85
    def exception_wrapper(self, *args):
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
    86
        try:
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
    87
            return return_wrapper(client_method, self, args_wrapper, *args)
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
    88
        except erpc.transport.ConnectionClosed as e:
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
    89
            confnodesroot._SetConnector(None)
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
    90
            confnodesroot.logger.write_error(_("Connection lost!\n"))
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
    91
        except erpc.codec.CodecError as e:
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
    92
            confnodesroot.logger.write_warning(_("ERPC codec error: %s\n") % e)
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
    93
        except erpc.client.RequestError as e:
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
    94
            confnodesroot.logger.write_error(_("ERPC request error: %s\n") % e)                
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
    95
        except MissingCallException as e:
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
    96
            confnodesroot.logger.write_warning(_("Remote call not supported: %s\n") % e)
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
    97
        except Exception as e:
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
    98
            errmess = _("Exception calling remote PLC object fucntion %s:\n") % method_name \
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
    99
                        + traceback.format_exc()
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
   100
            confnodesroot.logger.write_error(errmess + "\n")
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
   101
            confnodesroot._SetConnector(None)
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
   102
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
   103
        return self.PLCObjDefaults.get(method_name)
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
   104
    return exception_wrapper
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
   105
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
   106
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
   107
3884
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   108
def ERPC_connector_factory(uri, confnodesroot):
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   109
    """
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   110
    returns the ERPC connector
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   111
    """
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   112
    confnodesroot.logger.write(_("ERPC connecting to URI : %s\n") % uri)
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   113
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   114
    # TODO add parsing for serial URI
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   115
    # ERPC:///dev/ttyXX:baudrate or ERPC://:COM4:baudrate
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   116
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   117
    try:
4041
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   118
        scheme, location = uri.split("://",1)
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   119
        _model, _useID, parser, _builder = per_scheme_model[scheme]
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   120
        location_data = parser(location)
3884
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   121
    except Exception as e:
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   122
        confnodesroot.logger.write_error(
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   123
            'Malformed URI "%s": %s\n' % (uri, str(e)))
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   124
        return None
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   125
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   126
    PLCObjectERPCProxy = type(
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   127
        "PLCObjectERPCProxy",
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   128
        (ConnectorBase, BeremizPLCObjectServiceClient),
4040
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
   129
        {name: rpc_wrapper(name, confnodesroot)
3884
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   130
            for name,_func in getmembers(IBeremizPLCObjectService, isfunction)})
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   131
4041
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   132
    if scheme in ["ERPCS", "ERPC"]:
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   133
        if scheme=="ERPCS":
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   134
            ID = location_data["ID"]
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   135
            if not ID:
3884
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   136
                confnodesroot.logger.write_error(
4041
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   137
                    f'Invalid URI "{uri}": ERPCS requires PLC ID after "#"\n')
3884
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   138
                return None
4041
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   139
            default_port = 4000
3884
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   140
        else:
4041
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   141
            ID = None
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   142
            if "#" in location:
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   143
                confnodesroot.logger.write_error(
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   144
                    f'URI "{uri}": Non-encrypted ERPC does not take a PLC ID after "#"\n')
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   145
                return None
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   146
            default_port = 3000
3884
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   147
4041
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   148
        host = location_data["host"]
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   149
        port = location_data["port"]
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   150
        port = int(port) if port else default_port
3884
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   151
4041
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   152
        try:
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   153
            if ID:
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   154
                # load PSK from project
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   155
                secpath = os.path.join(confnodesroot.ProjectPath, 'psk', ID + '.secret')
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   156
                if not os.path.exists(secpath):
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   157
                    confnodesroot.logger.write_error(
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   158
                        'Error: Pre-Shared-Key Secret in %s is missing!\n' % secpath)
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   159
                    return None
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   160
                secret = open(secpath).read().partition(':')[2].rstrip('\n\r').encode()
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   161
                transport = SSLPSKClientTransport(host, port, (secret, ID.encode())) # type: ignore
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   162
            else:
3884
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   163
4041
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   164
                transport = erpc.transport.TCPTransport(host, port, False)
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   165
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   166
            clientManager = erpc.client.ClientManager(transport, erpc.basic_codec.BasicCodec)
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   167
            client = PLCObjectERPCProxy(clientManager)
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   168
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   169
        except Exception as e:
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   170
            confnodesroot.logger.write_error(
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   171
                _("Connection to {loc} failed with exception {ex}\n").format(
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   172
                    loc=uri, ex=str(e)))
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   173
            return None
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   174
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   175
    else:
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   176
        # TODO if serial URI then 
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   177
        # transport = erpc.transport.SerialTransport(device, baudrate)
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   178
3884
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   179
        confnodesroot.logger.write_error(
4041
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   180
            _("Unknown scheme {scheme} in URI {uri}\n").format(
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   181
                scheme=scheme, uri=uri))
3884
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   182
        return None
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   183
    # Check connection is effective.
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   184
    IDPSK = client.GetPLCID()
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   185
    if IDPSK:
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   186
        ID, secret = IDPSK
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   187
        PSK.UpdateID(confnodesroot.ProjectPath, ID, secret, uri)
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   188
    else:
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   189
        confnodesroot.logger.write_warning(_("PLC did not provide identity and security infomation.\n"))
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   190
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   191
    return client