connectors/ERPC/__init__.py
author Edouard Tisserant <edouard.tisserant@gmail.com>
Mon, 18 Nov 2024 22:42:11 +0100
changeset 4041 af7671a9945a
parent 4040 ea2db66b185f
child 4043 b217fb367574
permissions -rw-r--r--
IDE: re-use eRPC URI definitions in connector factory.

Preparing for serial and US eRPC connection.
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,
3884
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    65
}
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
ArgsWrappers = {
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    68
    "NewPLC":
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    69
        lambda md5sum, plcObjectBlobID, extrafiles: (
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    70
            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
    71
    "SetTraceVariablesList":
3885
22a009561502 Fix variable tracing in new eRPC interface.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3884
diff changeset
    72
        lambda orders : ([
3887
2df45e4bd500 Fix variable forcing. Now works with eRPC. PLCObject API changed.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3885
diff changeset
    73
            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
    74
            for idx, force in orders],)
3884
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    75
}
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    76
4040
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
    77
def rpc_wrapper(method_name, confnodesroot):
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
    78
    client_method = getattr(BeremizPLCObjectServiceClient, method_name)
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
    79
    return_wrapper = ReturnWrappers.get(
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
    80
        method_name, 
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
    81
        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
    82
    args_wrapper = ArgsWrappers.get(method_name, lambda *x:x)
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
    83
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
    84
    def exception_wrapper(self, *args):
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
    85
        try:
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
    86
            return return_wrapper(client_method, self, args_wrapper, *args)
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
    87
        except erpc.transport.ConnectionClosed as e:
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
    88
            confnodesroot._SetConnector(None)
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
    89
            confnodesroot.logger.write_error(_("Connection lost!\n"))
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
    90
        except erpc.codec.CodecError as e:
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
    91
            confnodesroot.logger.write_warning(_("ERPC codec error: %s\n") % e)
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
    92
        except erpc.client.RequestError as e:
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
    93
            confnodesroot.logger.write_error(_("ERPC request error: %s\n") % e)                
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
    94
        except MissingCallException as e:
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
    95
            confnodesroot.logger.write_warning(_("Remote call not supported: %s\n") % e)
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
    96
        except Exception as e:
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
    97
            errmess = _("Exception calling remote PLC object fucntion %s:\n") % method_name \
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
    98
                        + traceback.format_exc()
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
    99
            confnodesroot.logger.write_error(errmess + "\n")
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
   100
            confnodesroot._SetConnector(None)
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
   101
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
   102
        return self.PLCObjDefaults.get(method_name)
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
   103
    return exception_wrapper
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
   104
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
3884
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   107
def ERPC_connector_factory(uri, confnodesroot):
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   108
    """
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   109
    returns the ERPC connector
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   110
    """
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   111
    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
   112
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   113
    # TODO add parsing for serial URI
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   114
    # 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
   115
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   116
    try:
4041
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   117
        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
   118
        _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
   119
        location_data = parser(location)
3884
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   120
    except Exception as e:
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   121
        confnodesroot.logger.write_error(
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   122
            '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
   123
        return None
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   124
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   125
    PLCObjectERPCProxy = type(
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   126
        "PLCObjectERPCProxy",
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   127
        (ConnectorBase, BeremizPLCObjectServiceClient),
4040
ea2db66b185f Cosmetic changes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3948
diff changeset
   128
        {name: rpc_wrapper(name, confnodesroot)
3884
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   129
            for name,_func in getmembers(IBeremizPLCObjectService, isfunction)})
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   130
4041
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   131
    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
   132
        if scheme=="ERPCS":
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   133
            ID = location_data["ID"]
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   134
            if not ID:
3884
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   135
                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
   136
                    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
   137
                return None
4041
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   138
            default_port = 4000
3884
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   139
        else:
4041
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   140
            ID = None
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   141
            if "#" in location:
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   142
                confnodesroot.logger.write_error(
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   143
                    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
   144
                return None
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   145
            default_port = 3000
3884
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   146
4041
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   147
        host = location_data["host"]
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   148
        port = location_data["port"]
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   149
        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
   150
4041
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   151
        try:
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   152
            if ID:
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   153
                # load PSK from project
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   154
                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
   155
                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
   156
                    confnodesroot.logger.write_error(
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   157
                        '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
   158
                    return None
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   159
                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
   160
                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
   161
            else:
3884
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   162
4041
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   163
                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
   164
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   165
            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
   166
            client = PLCObjectERPCProxy(clientManager)
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   167
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   168
        except Exception as e:
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   169
            confnodesroot.logger.write_error(
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   170
                _("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
   171
                    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
   172
            return None
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   173
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   174
    else:
af7671a9945a IDE: re-use eRPC URI definitions in connector factory.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 4040
diff changeset
   175
        # 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
   176
        # 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
   177
3884
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   178
        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
   179
            _("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
   180
                scheme=scheme, uri=uri))
3884
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   181
        return None
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   182
    # Check connection is effective.
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   183
    IDPSK = client.GetPLCID()
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   184
    if IDPSK:
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   185
        ID, secret = IDPSK
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   186
        PSK.UpdateID(confnodesroot.ProjectPath, ID, secret, uri)
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   187
    else:
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   188
        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
   189
34da877021d5 Replace PYRO with ERPC. Work In Progress.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   190
    return client