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)
def ecu_B_subscribe_(stub): """Subscribe to a value published by ecu_A and output value Parameters ---------- stub : NetworkServiceStub Object instance of class """ namespace = "custom_can" client_id = common_pb2.ClientId(id="id_ecu_B") # Subscribe to value 'SteerAngle' steer_angle = common_pb2.SignalId( name="SteerAngle", namespace=common_pb2.NameSpace(name=namespace)) sub_info = network_api_pb2.SubscriberConfig( clientId=client_id, signals=network_api_pb2.SignalIds(signalId=[steer_angle]), onChange=True, ) # Output subscribed signal try: for subs_counter in stub.SubscribeToSignals(sub_info): # For clean exit when stopping script if exit_event.is_set(): break print("ecu_B, (subscribe) SteerAngle is ", subs_counter.signal[0]) except grpc._channel._Rendezvous as err: print(err)
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 set_fan_speed(stub, value, freq): source_g = common_pb2.ClientId(id="app_identifier") value_g = functional_api_pb2.Value(payload=value) response = stub.SetFanSpeed( functional_api_pb2.SenderInfo(clientId=source_g, value=value_g, frequency=freq)) print("executed call %s" % response)
def ecu_B_read(stub, pause): while True: namespace = "ecu_B" client_id = common_pb2.ClientId(id="id_ecu_B") counter = common_pb2.SignalId(name="counter", namespace=common_pb2.NameSpace(name = namespace)) read_counter = read_signal(stub, counter) print("ecu_B, (read) counter is ", read_counter.signal[0].integer) time.sleep(pause)
def subscribe_to_arbitration(stub): source = common_pb2.ClientId(id="app_identifier") namespace = common_pb2.NameSpace(name = "VirtualCanInterface") signal = common_pb2.SignalId(name="BenchC_c_5", namespace=namespace) sub_info = network_api_pb2.SubscriberConfig(clientId=source, signals=network_api_pb2.SignalIds(signalId=[signal]), onChange=False) try: for response in stub.SubscribeToSignals(sub_info): print(response) except grpc._channel._Rendezvous as err: print(err)
def read_diagnostics_odb(stub): source = common_pb2.ClientId(id="app_identifier") namespace = common_pb2.NameSpace(name = "PropulsionCANhs") upLink = common_pb2.SignalId(name="EtcToAllCarbPropDiagReqFrame", namespace=namespace) downLink = common_pb2.SignalId(name="EcmToEtcCarbPropDiagResFrame", namespace=namespace) request = diagnostics_api_pb2.DiagnosticsRequest(upLink = upLink, downLink = downLink, serviceId = b'\x01', dataIdentifier = b'\x42') try: response = stub.SendDiagnosticsQuery(request) print(response) print(binascii.hexlify(response.raw)) except grpc._channel._Rendezvous as err: print(err)
def ecu_B_subscribe_2(stub): """Shows possibility to subscribe to same signal multiple times, also using array to subscribe to additional signals. Logs on purpose tabbed with single space. Parameters ---------- stub : NetworkServiceStub Object instance of class """ namespace = "ecu_B" client_id = common_pb2.ClientId(id="id_ecu_B") # Signal value 'counter' counter = common_pb2.SignalId( name="counter", namespace=common_pb2.NameSpace(name=namespace) ) # Signal value 'TestFr06_Child02' testFr06_Child02 = common_pb2.SignalId( name="TestFr06_Child02", namespace=common_pb2.NameSpace(name=namespace) ) # Frame value 'TestFr04' testFr04 = common_pb2.SignalId( name="TestFr04", namespace=common_pb2.NameSpace(name=namespace) ) # Subscribes to 'counter', 'TestFr06_Child02' and 'TestFr04' sub_info = network_api_pb2.SubscriberConfig( clientId=client_id, signals=network_api_pb2.SignalIds( signalId=[counter, testFr06_Child02, testFr04] ), onChange=True, ) try: for response in stub.SubscribeToSignals(sub_info): # Since we subscribed to a set of signals we need to check which arrived. for signal in response.signal: if signal.id.name == "counter": print(" ecu_B, (subscribe_2) counter is ", signal.integer) if signal.id.name == "TestFr06_Child02": print( " ecu_B signal: " + signal.id.name + " arrived: " + str(signal.integer) ) if signal.id.name == "TestFr04": print(" ecu_B, (subscribe_2) raw is ", binascii.hexlify(signal.raw)) except grpc._channel._Rendezvous as err: print(err)
def subscribe_to_signal(stub): source = common_pb2.ClientId(id="app_identifier") signals = [ common_pb2.SignalId(name="SteerWhlAgSafe", namespace=common_pb2.NameSpace(name = "ChassisCANhs")), common_pb2.SignalId(name="DrvrDesDir", namespace=common_pb2.NameSpace(name = "ChassisCANhs")), common_pb2.SignalId(name="RecOfImpctSafeCntr", namespace=common_pb2.NameSpace(name = "BodyCANhs")), ] sub_info = network_api_pb2.SubscriberConfig(clientId=source, signals=network_api_pb2.SignalIds(signalId=signals), onChange=False) try: for response in stub.SubscribeToSignals(sub_info): print(response) except grpc._channel._Rendezvous as err: print(err)
def read_diagnostics_vin(stub): source = common_pb2.ClientId(id="app_identifier") namespace = common_pb2.NameSpace(name = "ChassisCANhs") upLink = common_pb2.SignalId(name="VddmToAllFuncChasDiagReqFrame", namespace=namespace) downLink = common_pb2.SignalId(name="PscmToVddmChasDiagResFrame", namespace=namespace) request = diagnostics_api_pb2.DiagnosticsRequest(upLink = upLink, downLink = downLink, serviceId = b'\x22', dataIdentifier = b'\xF1\x90') try: response = stub.SendDiagnosticsQuery(request) print(response) print(binascii.hexlify(response.raw)) except grpc._channel._Rendezvous as err: print(err)
def read_diagnostics_odb(stub): source = common_pb2.ClientId(id="app_identifier") namespace = common_pb2.NameSpace(name = "DiagnosticsCanInterface") upLink = common_pb2.SignalId(name="DiagReqBroadCastFrame_2015", namespace=namespace) # if you dont see any response try the other resp frames defined in the diagnostics dbc file. downLink = common_pb2.SignalId(name="DiagResFrame_2024", namespace=namespace) # service 01 pid 12 - engine rpm request = diagnostics_api_pb2.DiagnosticsRequest(upLink = upLink, downLink = downLink, serviceId = b'\x01', dataIdentifier = b'\x12') try: response = stub.SendDiagnosticsQuery(request) print(response) print(binascii.hexlify(response.raw)) except grpc._channel._Rendezvous as err: print(err)
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 """ increasing_counter = 0 namespace = "ecu_A" clientId = common_pb2.ClientId(id="id_ecu_A") while True: print("\necu_A, seed/counter is ", increasing_counter) # Publishes value 'counter' publish_signals( clientId, stub, [ signal_creator.signal_with_payload( "counter", namespace, ("integer", increasing_counter) ), # signal_creator.signal_with_payload( # "TestFr07_Child01_UB", namespace, ("integer", 1) # ), # signal_creator.signal_with_payload( # "TestFr07_Child02_UB", namespace, ("integer", 1) # ), # add any number of signals here, make sure that all signals/frames are unique. # signal_creator.signal_with_payload( # "TestFr04", namespace, ("raw", binascii.unhexlify("0a0b0c0d")), False # ), ], ) time.sleep(pause) # Read the other value 'counter_times_2' and output result read_signal_response = read_signals( stub, signal_creator.signal("counter_times_2", namespace) ) for signal in read_signal_response.signal: print(f"ecu_A, (result) {signal.id.name} is {get_value(signal)}") increasing_counter = (increasing_counter + 1) % 4
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 read_diagnostics_engine_speed(stub): while True: source = common_pb2.ClientId(id="app_identifier") namespace = common_pb2.NameSpace(name = "DiagnosticsCanInterface") upLink = common_pb2.SignalId(name="DiagReqBroadCastFrame_2015", namespace=namespace) downLink = common_pb2.SignalId(name="DiagResFrame_2024", namespace=namespace) request = diagnostics_api_pb2.DiagnosticsRequest(upLink = upLink, downLink = downLink, serviceId = b'\x01', dataIdentifier = b'\x0C') try: response = stub.SendDiagnosticsQuery(request) print(response) # print(int.from_bytes(response.raw)) python 3.2 print(int(codecs.encode(response.raw, 'hex'), 16)) print(binascii.hexlify(response.raw)) except grpc._channel._Rendezvous as err: print(err)
def run(): channel = grpc.insecure_channel('192.168.1.184:50051') network_stub = network_api_pb2_grpc.NetworkServiceStub(channel) system_stub = system_api_pb2_grpc.SystemServiceStub(channel) client_id = common_pb2.ClientId(id="app_identifier") # If you need to change the id of the req/resp modify here configuration/can/diagnostics.dbc diag_frame_req = common_pb2.SignalId( name="DiagReqFrame_2016", namespace=common_pb2.NameSpace(name="DiagnosticsCanInterface")) diag_frame_resp = signal = common_pb2.SignalId( name="DiagResFrame_2024", namespace=common_pb2.NameSpace(name="DiagnosticsCanInterface")) upload_folder(system_stub, "configuration") reload_configuration(system_stub) print( "-------------- Subscribe to diag_req, on request submit resp_frame --------------" ) subscribe_to_diag(client_id, network_stub, diag_frame_req, diag_frame_resp)
def ecu_B_read(stub, pause): """Read a value published by ecu_A Parameters ---------- stub : NetworkServiceStub Object instance of class pause : int Amount of time to pause, in seconds """ while True: namespace = "ecu_B" client_id = common_pb2.ClientId(id="id_ecu_B") # Read value 'counter' counter = common_pb2.SignalId( name="counter", namespace=common_pb2.NameSpace(name=namespace)) read_counter = read_signal(stub, counter) print("ecu_B, (read) counter is ", read_counter.signal[0].integer) time.sleep(pause)
def ecu_B_read(stub, pause): """Read some value published by ecu_A Parameters ---------- stub : NetworkServiceStub Object instance of class pause : int Amount of time to pause, in seconds """ while not exit_event.is_set(): namespace = "custom_can" client_id = common_pb2.ClientId(id="id_ecu_B") # Read value 'SteerAngle' steer_angle = common_pb2.SignalId( name="SteerAngle", namespace=common_pb2.NameSpace(name=namespace)) response = read_signal(stub, steer_angle) print("ecu_B, (read) SteerAngle is ", response.signal[0].double) time.sleep(pause)
def run(argv): # This script 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: resp_to_diag_req.py --ip <ip_address>") sys.exit(2) for opt, arg in opts: if opt == "-h": print("Usage: resp_to_diag_req.py --ip <ip_address>") sys.exit(2) elif opt == "--ip": ip = arg channel = grpc.insecure_channel(ip + port) network_stub = network_api_pb2_grpc.NetworkServiceStub(channel) system_stub = system_api_pb2_grpc.SystemServiceStub(channel) client_id = common_pb2.ClientId(id="app_identifier") # If you need to change the id of the req/resp modify here configuration/can/diagnostics.dbc diag_frame_req = common_pb2.SignalId( name="DiagReqFrame_2016", namespace=common_pb2.NameSpace(name="DiagnosticsCanInterface"), ) diag_frame_resp = signal = common_pb2.SignalId( name="DiagResFrame_2024", namespace=common_pb2.NameSpace(name="DiagnosticsCanInterface"), ) upload_folder(system_stub, "configuration") reload_configuration(system_stub) print( "-------------- Subscribe to diag_req, on request submit resp_frame --------------" ) subscribe_to_diag(client_id, network_stub, diag_frame_req, diag_frame_resp)
def ecu_B_subscribe_2(stub): namespace = "ecu_B" # specify some signals client_id = common_pb2.ClientId(id="id_ecu_B") counter = common_pb2.SignalId(name="counter", namespace=common_pb2.NameSpace(name = namespace)) testFr06_Child02 = common_pb2.SignalId(name="TestFr06_Child02", namespace=common_pb2.NameSpace(name = namespace)) testFr04 = common_pb2.SignalId(name="TestFr04", namespace=common_pb2.NameSpace(name = namespace)) sub_info = network_api_pb2.SubscriberConfig(clientId=client_id, signals=network_api_pb2.SignalIds(signalId=[counter, testFr06_Child02, testFr04]), onChange=False) try: for response in stub.SubscribeToSignals(sub_info): # since we subscribe to a set of signal we need to check which arrived. for signal in response.signal: if (signal.id.name == "counter"): print(" ecu_B, (subscribe_2) counter is ", signal.integer) if (signal.id.name == "TestFr06_Child02"): print(" ecu_B signal: " + signal.id.name + " arrived: " + str(signal.integer)) if (signal.id.name == "TestFr04"): print(" ecu_B, (subscribe_2) raw is ", binascii.hexlify(signal.raw)) except grpc._channel._Rendezvous as err: print(err)
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
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)
def run(ip, port): """Main function, checking arguments passed to script, setting up stubs, configuration and starting Threads.""" # 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_udp") # upload_folder(system_stub, "configuration_lin") upload_folder(system_stub, "configuration_can") # upload_folder(system_stub, "configuration_canfd") reload_configuration(system_stub) global signal_creator signal_creator = SignalCreator(system_stub) # Starting Threads ##################################################################### # ECU under test (DUT) is located on namespace ecu_B which and it's # specified behaviour is just to to bounce (emit) counter_times_2 as soon as # counter arrive ecu_under_test_id = common_pb2.ClientId(id="ecu_under_test_id") ecu_under_test_namespace = "ecu_B" def bounce(network_stub, client_id, trigger, signals): global time_stamp_ref global time_diff_a_b for signal in signals: if signal.id == trigger: publish_signals( client_id, network_stub, [ signal_creator.signal_with_payload( "counter_times_2", ecu_under_test_namespace, ("integer", get_value(signal)), ), ], ) if time_stamp_ref != {} and (get_value(signal) == get_value(time_stamp_ref)): print( f"time difference (counter @ {time_stamp_ref.id.namespace.name} -> counter @ {signal.id.namespace.name}) is {signal.timestamp - time_stamp_ref.timestamp}" ) time_diff_a_b.append(signal.timestamp - time_stamp_ref.timestamp) Thread( target=act_on_signal, args=( ecu_under_test_id, network_stub, [ # this is what triggers our bounce signal_creator.signal("counter", ecu_under_test_namespace), ], False, # True: only report when signal changes lambda signals: bounce( network_stub, ecu_under_test_id, signal_creator.signal("counter", ecu_under_test_namespace), signals, ), lambda subscripton: (q.put((ecu_under_test_id, subscripton))), ), ).start() # wait for subscription to settle ecu, subscription = q.get() ##################################################################### ##################################################################### # set up an subscriber which listens to emitted signal and received. # NOTE: This subscriber needs to have unique "Client_id", which is different # from the publishing client, in order to not be filtered away time_measurer_id = common_pb2.ClientId(id="time_measurer_id") time_measurer_namespace = "ecu_A" def on_subscription(trigger_a, trigger_b, signals): global time_stamp_ref global time_diff_a_a for signal in signals: if signal.id == trigger_a: time_stamp_ref = signal elif signal.id == trigger_b and (time_stamp_ref != {} and (get_value(signal) == get_value(time_stamp_ref))): print( f"time difference (counter @ {time_measurer_namespace} -> counter_times_2 @ {time_measurer_namespace}) is {signal.timestamp - time_stamp_ref.timestamp}" ) time_diff_a_a.append(signal.timestamp - time_stamp_ref.timestamp) else: time_stamp_ref = {} Thread( target=act_on_signal, args=( time_measurer_id, network_stub, [ # this is what we will emit. signal_creator.signal("counter", time_measurer_namespace), # this will bounce back form ecu_under_test_id signal_creator.signal("counter_times_2", time_measurer_namespace), ], False, # True: only report when signal changes lambda signals: on_subscription( signal_creator.signal("counter", time_measurer_namespace), signal_creator.signal("counter_times_2", time_measurer_namespace), signals, ), lambda subscripton: (q.put((time_measurer_id, subscripton))), ), ).start() # wait for subscription to settle ecu, subscription = q.get() ##################################################################### ##################################################################### # Emit signals form ecu_a which till trigger ECU under test ecu_tester_id = common_pb2.ClientId(id="ecu_tester_id") ecu_tester_namespace = "ecu_A" Thread( target=ecu_tester, args=(network_stub, ecu_tester_id, ecu_tester_namespace, 0.05), ).start()
def run(ip, port): """Main function, checking arguments passed to script, setting up stubs, configuration and starting Threads.""" # 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_can") reload_configuration(system_stub) global signal_creator signal_creator = SignalCreator(system_stub) # ecu a, we do this with lambda refering to modify_signal_publish_frame. reflector_client_id = common_pb2.ClientId(id="reflector_client_id") def modify_signals_publish_frame( network_stub, client_id, destination_namespace_name, signals ): # work in dictonary domain for easier access. signal_dict = {signal.id.name: signal for signal in signals} # example, lets update TestFr07_Child02 (type, value) = get_value_pair(signal_dict["TestFr07_Child02"]) signal_dict["TestFr07_Child02"] = signal_creator.signal_with_payload( "TestFr07_Child02", destination_namespace_name, (type, value + 1) ) # example, lets update TestFr07_Child01_UB just invert this single bit (type, value) = get_value_pair(signal_dict["TestFr07_Child01_UB"]) signal_dict["TestFr07_Child01_UB"] = signal_creator.signal_with_payload( "TestFr07_Child01_UB", destination_namespace_name, (type, 1 - value) ) # example, lets compute counter_times_2 using some formula (type, value) = get_value_pair(signal_dict["counter_times_2"]) signal_dict["counter_times_2"] = signal_creator.signal_with_payload( "counter_times_2", destination_namespace_name, ( type, some_function_to_calculate_crc( id, destination_namespace_name, [ ( "TestFr07_Child02", get_value_pair(signal_dict["TestFr07_Child02"])[0], ), ( "TestFr07_Child01_UB", get_value_pair(signal_dict["TestFr07_Child01_UB"])[0], ), ], ), ), ) publish_list = signal_dict.values() # update destination namespace for all entrys in list change_namespace(publish_list, destination_namespace_name) # print(f"updates lists {publish_list}") publish_signals(client_id, network_stub, publish_list) Thread( target=act_on_signal, args=( reflector_client_id, network_stub, # get all childs in frame, alternatively we could do # all_siblings("counter_times_2", "ecu_A") signal_creator.signals_in_frame("TestFr07", "ecu_A"), False, # True = only report when signal changes lambda signals: modify_signals_publish_frame( network_stub, reflector_client_id, "ecu_B", signals, ), ), ).start()
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")
__version__ = "0.0.1" __maintainer__ = "Alvaro Alonso" __email__ = "*****@*****.**" __status__ = "Development" if __name__ == "__main__": # Create a channel channel = grpc.insecure_channel("localhost:50051") # Create the stub 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") 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
def run(ip, port): """Main function, checking arguments passed to script, setting up stubs, configuration and starting Threads.""" # 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_udp") # upload_folder(system_stub, "configuration_lin") # upload_folder(system_stub, "configuration_can") # upload_folder(system_stub, "configuration_canfd") reload_configuration(system_stub) global signal_creator signal_creator = SignalCreator(system_stub) # Lists available signals configuration = system_stub.GetConfiguration(common_pb2.Empty()) for networkInfo in configuration.networkInfo: print( "signals in namespace ", networkInfo.namespace.name, system_stub.ListSignals(networkInfo.namespace), ) # Starting Threads # ecu b, we do this with lambda refering to double_and_publish. ecu_b_client_id = common_pb2.ClientId(id="id_ecu_B") ecu_B_sub_thread = Thread( target=act_on_signal, args=( ecu_b_client_id, network_stub, [ signal_creator.signal("counter", "ecu_B"), # here you can add any signal from any namespace # signal_creator.signal("TestFr04", "ecu_B"), ], True, # True: only report when signal changes lambda signals: double_and_publish( network_stub, ecu_b_client_id, signal_creator.signal("counter", "ecu_B"), signals, ), lambda subscripton: (q.put(("id_ecu_B", subscripton))), ), ) ecu_B_sub_thread.start() # wait for subscription to settle ecu, subscription = q.get() # ecu a, this is where we publish, and ecu_A_thread = Thread( target=ecu_A, args=( network_stub, 1, ), ) ecu_A_thread.start() # ecu b, bonus, periodically, read using timer. signals = [ signal_creator.signal("counter", "ecu_B"), # add any number of signals from any namespace # signal_creator.signal("TestFr04", "ecu_B"), ] ecu_read_on_timer = Thread(target=read_on_timer, args=(network_stub, signals, 1)) ecu_read_on_timer.start()