def publish_signals(stub):
    source = common_pb2.ClientId(id="app_identifier")
    namespace = common_pb2.NameSpace(name="VirtualCanInterface")

    signal = common_pb2.SignalId(name="BenchC_c_5", namespace=namespace)
    signal_with_payload = network_api_pb2.Signal(id=signal)
    signal_with_payload.integer = 4

    signal2 = common_pb2.SignalId(name="BenchC_c_2", namespace=namespace)
    signal_with_payload_2 = network_api_pb2.Signal(id=signal2)
    signal_with_payload_2.double = 3.4

    signal3 = common_pb2.SignalId(name="BenchC_d_2", namespace=namespace)
    signal_with_payload_3 = network_api_pb2.Signal(id=signal3)
    signal_with_payload_3.arbitration = True

    publisher_info = network_api_pb2.PublisherConfig(
        clientId=source,
        signals=network_api_pb2.Signals(
            signal=[signal_with_payload, signal_with_payload_2]),
        frequency=0)
    try:
        stub.PublishSignals(publisher_info)
        time.sleep(1)
    except grpc._channel._Rendezvous as err:
        print(err)
Example #2
0
def ecu_A(stub, pause):
    while True:
        global increasing_counter
        namespace = "ecu_A"
        clientId = common_pb2.ClientId(id="id_ecu_A")
        # counter
        counter = common_pb2.SignalId(name="counter", namespace=common_pb2.NameSpace(name = namespace))
        counter_with_payload = network_api_pb2.Signal(id = counter, integer = increasing_counter)
        # TestFr06_Child02 - another signal
        testFr06_Child02 = common_pb2.SignalId(name="TestFr06_Child02", namespace=common_pb2.NameSpace(name = namespace))
        testFr06_Child02_with_payload = network_api_pb2.Signal(id = testFr06_Child02, integer = increasing_counter+1)
        # TestFr04 - now a frame
        testFr04 = common_pb2.SignalId(name="TestFr04", namespace=common_pb2.NameSpace(name = namespace))
        testFr04_with_payload = network_api_pb2.Signal(id = testFr04, integer = increasing_counter+2)

        print("\necu_A, seed is ", increasing_counter)
        publish_signals(clientId, stub, [counter_with_payload, testFr06_Child02_with_payload, testFr04_with_payload])
        
        time.sleep(pause)

        # read the other value and output result
        counter_times_2 = common_pb2.SignalId(name="counter_times_2", namespace=common_pb2.NameSpace(name = namespace))
        read_counter_times_2 = read_signal(stub, counter_times_2)

        print("ecu_A, (result) counter_times_2 is ", read_counter_times_2.signal[0].integer)
        increasing_counter = (increasing_counter + 1) % 10
def publish_signals(client_id, stub, diag_frame_resp):

    signal_with_payload = network_api_pb2.Signal(id=diag_frame_resp)
    signal_with_payload.raw = b'\x01\x02\x03\x04\x05\x06\x07\x08'
    # signal_with_payload.raw = binascii.unhexlify("0102030405060708")
    publisher_info = network_api_pb2.PublisherConfig(
        clientId=client_id,
        signals=network_api_pb2.Signals(signal=[signal_with_payload]),
        frequency=0)
    try:
        stub.PublishSignals(publisher_info)
    except grpc._channel._Rendezvous as err:
        print(err)
Example #4
0
def ecu_B_subscribe(stub):
    namespace = "ecu_B"
    client_id = common_pb2.ClientId(id="id_ecu_B")
    counter = common_pb2.SignalId(name="counter", namespace=common_pb2.NameSpace(name = namespace))

    sub_info = network_api_pb2.SubscriberConfig(clientId=client_id, signals=network_api_pb2.SignalIds(signalId=[counter]), onChange=False)
    try:
        for subs_counter in stub.SubscribeToSignals(sub_info):
            print("ecu_B, (subscribe) counter is ", subs_counter.signal[0].integer)
            counter_times_2 = common_pb2.SignalId(name="counter_times_2", namespace=common_pb2.NameSpace(name = namespace))
            signal_with_payload = network_api_pb2.Signal(id = counter_times_2, integer = subs_counter.signal[0].integer * 2)
            publish_signals(client_id, stub, [signal_with_payload])
            
    except grpc._channel._Rendezvous as err:
            print(err)
    def signal_with_payload(self,
                            name,
                            namespace_name,
                            value_pair,
                            allow_malformed=False):
        signal = self.signal(name, namespace_name)

        key, value = value_pair
        types = ["integer", "double", "raw", "arbitration"]
        if key not in types:
            raise Exception(f"type must be one of: {types}")
        if key is "raw" and allow_malformed is False:
            assert len(value) * 8 == self._sinfos[(
                namespace_name, name
            )].size, f"payload size missmatch, expected {self._sinfos[(namespace_name, name)].size/8} bytes"
        params = {"id": signal, key: value}
        return network_api_pb2.Signal(**params)
def ecu_B_subscribe(stub):
    """Subscribe to a value published by ecu_A and publish doubled value back to ecu_A

    Parameters
    ----------
    stub : NetworkServiceStub
        Object instance of class

    """
    namespace = "ecu_B"
    client_id = common_pb2.ClientId(id="id_ecu_B")

    # Subscribe to value 'counter'
    counter = common_pb2.SignalId(
        name="counter", namespace=common_pb2.NameSpace(name=namespace)
    )
    sub_info = network_api_pb2.SubscriberConfig(
        clientId=client_id,
        signals=network_api_pb2.SignalIds(signalId=[counter]),
        onChange=True,
    )

    # Publish doubled value as 'counter_times_2'
    try:
        for subs_counter in stub.SubscribeToSignals(sub_info):
            print("ecu_B, (subscribe) counter is ", subs_counter.signal[0].integer)
            counter_times_2 = common_pb2.SignalId(
                name="counter_times_2", namespace=common_pb2.NameSpace(name=namespace)
            )
            signal_with_payload = network_api_pb2.Signal(
                id=counter_times_2, integer=subs_counter.signal[0].integer * 2
            )
            publish_signals(client_id, stub, [signal_with_payload])

    except grpc._channel._Rendezvous as err:
        print(err)
Example #7
0
def ecu_A(stub, pause):
    """Publishes a value, read other value (published by ecu_B)

    Parameters
    ----------
    stub : NetworkServiceStub
        Object instance of class
    pause : int
        Amount of time to pause, in seconds

    """
    while True:
        global increasing_counter
        namespace = "ecu_A"
        clientId = common_pb2.ClientId(id="id_ecu_A")

        # Publishes value 'counter'
        counter = common_pb2.SignalId(
            name="counter", namespace=common_pb2.NameSpace(name=namespace))
        counter_with_payload = network_api_pb2.Signal(
            id=counter, integer=increasing_counter)
        publish_signals(clientId, stub, [counter_with_payload])
        print("\necu_A, seed is ", increasing_counter)

        time.sleep(pause)

        # Read the other value 'counter_times_2' and output result
        counter_times_2 = common_pb2.SignalId(
            name="counter_times_2",
            namespace=common_pb2.NameSpace(name=namespace))
        read_counter_times_2 = read_signal(stub, counter_times_2)
        print(
            "ecu_A, (result) counter_times_2 is ",
            read_counter_times_2.signal[0].integer,
        )
        increasing_counter = (increasing_counter + 1) % 4
Example #8
0
    reload_configuration(system_stub)
    # create the identifier of *this* client
    client_id = common_pb2.ClientId(id="virtual_example_pub")
    # For 10 messages
    for _ in range(10):
        input_value = input("Enter a number: ")
        try:
            signal_value = int(input_value)
        except ValueError:
            print(f"{input_value} is not a number. Only numbers are allowed")
        else:
            # Create a signal
            namespace = common_pb2.NameSpace(name="VirtualInterface")
            signal = common_pb2.SignalId(name="my_madeup_virtual_signal",
                                         namespace=namespace)
            # Add payload to the signal
            signal_with_payload = network_api_pb2.Signal(id=signal)
            # 20 bytes chosen as an arbitrary number
            signal_with_payload.raw = signal_value.to_bytes(20, 'big')
            # long vectors are valid on virtual networks!
            # signal_with_payload.integer = signal_value
            # Create a publisher config
            signals = network_api_pb2.Signals(signal=(signal_with_payload, ))
            publisher_info = network_api_pb2.PublisherConfig(
                clientId=client_id, signals=signals, frequency=0)
            # Publish
            try:
                network_stub.PublishSignals(publisher_info)
            except grpc._channel._Rendezvous as err:
                print(err)
Example #9
0
def run(argv):
    """Main function, checking arguments passed to script, setting up stubs, configuration and starting Threads.

    Parameters
    ----------
    argv : list
        Arguments passed when starting script

    """
    # Checks argument passed to script, ecu.py will use below ip-address if no argument is passed to the script
    ip = "127.0.0.1"
    # Keep this port
    port = ":50051"
    try:
        opts, args = getopt.getopt(argv, "h", ["ip="])
    except getopt.GetoptError:
        print("Usage: ecu.py --ip <ip_address>")
        sys.exit(2)
    for opt, arg in opts:
        if opt == "-h":
            print("Usage: ecu.py --ip <ip_address>")
            sys.exit(2)
        elif opt == "--ip":
            ip = arg

    # Setting up stubs and configuration
    channel = grpc.insecure_channel(ip + port)
    network_stub = network_api_pb2_grpc.NetworkServiceStub(channel)
    system_stub = system_api_pb2_grpc.SystemServiceStub(channel)
    # check_license(system_stub)

    upload_folder(system_stub, "configuration_lin")
    reload_configuration(system_stub)

    # grace period to let clients time to pull their configuration
    time.sleep(5)

    clientId = common_pb2.ClientId(id="this_python_app_identifier")

    # slave, prepare to answer to diag request, this gets loaded pening in the transiever (waiting for aribitration)
    namespace_slave = "ecu_B"
    signal = common_pb2.SignalId(
        name="SlaveResp", namespace=common_pb2.NameSpace(name=namespace_slave))
    signal_with_payload = network_api_pb2.Signal(
        id=signal, raw=b"\x81\x09\x0a\x0b\x0c\x0d\x0e\x0f")
    publish_signals(network_stub, clientId, [signal_with_payload], 0)

    print("Slave is ready to reply to diag request %s" % (signal_with_payload))

    # master, subscribe to answer from client (just listen)
    namespace_master = "ecu_A"

    frame = common_pb2.SignalId(
        name="SlaveResp",
        namespace=common_pb2.NameSpace(name=namespace_master))
    # frame = common_pb2.SignalId(name="counter_times_2", namespace=common_pb2.NameSpace(name = namespace_master))
    ecu_m_subscribe = Thread(target=ecu_master_subscribe,
                             args=(network_stub, clientId, [frame], False))
    ecu_m_subscribe.daemon = True  # allow this thread to be automatically stopped on exit.
    ecu_m_subscribe.start()

    # Just listen from slave, for debug purposes
    # frame2 = common_pb2.SignalId(name="MasterReq", namespace=common_pb2.NameSpace(name = namespace_slave))
    # ecu_s_subscribe  = Thread(target = ecu_slave_subscribe, args = (network_stub, clientId, [frame, frame2], False))
    # ecu_s_subscribe.start()

    # # send arbitration from master (not needed arbitration is enabled inte inderfaces.json), expect nothing back! (send arbitration)
    #
    # signal = common_pb2.SignalId(name="SlaveResp", namespace=common_pb2.NameSpace(name = namespace_master))
    # signal_with_payload = network_api_pb2.Signal(id = signal, arbitration = True)
    # publish_signals(network_stub, clientId, [signal_with_payload], 0)

    # print("sent %s from master expect nothing back" % (signal_with_payload))
    # time.sleep(3)

    # send master request, now with propriate NAD, DEVS2 has NAD 0x81 (which is the fist byte below)
    signal = common_pb2.SignalId(
        name="MasterReq",
        namespace=common_pb2.NameSpace(name=namespace_master))
    signal_with_payload = network_api_pb2.Signal(
        id=signal, raw=b"\x81\x02\x03\x04\x05\x06\x07\x08")
    publish_signals(network_stub, clientId, [signal_with_payload], 0)

    print("sent %s from master expect ANSWER" % (signal_with_payload))
    time.sleep(3)

    # # send arbitration from master, expect answer since nad is matching
    # signal = common_pb2.SignalId(name="SlaveResp", namespace=common_pb2.NameSpace(name = namespace_master))
    # signal_with_payload = network_api_pb2.Signal(id = signal, arbitration = True)
    # publish_signals(network_stub, clientId, [signal_with_payload], 0)

    # print("sent %s from master expect ANSWER" % (signal_with_payload))
    # time.sleep(3)

    # send master request, now with another NAD, make sure client doesn't (which is the fist byte below)
    signal = common_pb2.SignalId(
        name="MasterReq",
        namespace=common_pb2.NameSpace(name=namespace_master))
    signal_with_payload = network_api_pb2.Signal(
        id=signal, raw=b"\x82\x02\x03\x04\x05\x06\x07\x08")
    publish_signals(network_stub, clientId, [signal_with_payload], 0)

    # send arbitration from master, expect nothing back (nad is not matching)!
    # signal = common_pb2.SignalId(name="SlaveResp", namespace=common_pb2.NameSpace(name = namespace_master))
    # signal_with_payload = network_api_pb2.Signal(id = signal, arbitration = True)
    # publish_signals(network_stub, clientId, [signal_with_payload], 0)

    print("sent %s from master NOT expecting ANSWER" % (signal_with_payload))
    time.sleep(3)

    print("still quiet.... done")