Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using jnsoft.Diagnose.Comm.UDS;
- using jnsoft.Helpers;
- using System;
- using System.Collections.Generic;
- using System.Net;
- namespace jnsoft.Diagnose.Comm.DoIp.Examples
- {
- /// <summary>
- /// ASAP2 DoIP Example.
- ///
- /// This example collects DoIP Vehicle announcements on the specified local IPAddress.
- /// - A DoIP Client is created to access a DoIP gateway
- /// - Two UDS client are created to request TesterPresent commands
- ///
- /// Usage: ASAP2DoIPExample 192.168.0.2
- /// </summary>
- class Program
- {
- static readonly List<UDSClient> UDSClients = new List<UDSClient>();
- static IPAddress LocalIp;
- static IPAddress RemoteIp;
- static int Main(string[] args)
- {
- var orgColor = Console.ForegroundColor;
- try
- {
- Console.ForegroundColor = ConsoleColor.White;
- if (args.Length < 1)
- { // no args -> present usage
- var appName = Extensions.AppName;
- Console.WriteLine($"{appName}({Extensions.AppVersion})");
- Console.WriteLine("UDS over DoIP example...");
- Console.WriteLine("Required input: the IP address of the local network to use");
- Console.WriteLine($"Usage: {appName} Local_IPV4_or_IPV6_Address [Remote_IPV4_or_IPV6_Address]");
- Console.WriteLine($"Example: {appName} 192.168.0.2");
- return -1;
- }
- LocalIp = IPAddress.Parse(args[0]);
- if (args.Length > 1)
- { // try to connect to remote DoIP äentity
- RemoteIp = args.Length > 1 ? IPAddress.Parse(args[1]) : null;
- var gateway = new DoIPClient(RemoteIp, LocalIp, Activation.Default, DoIPClient.DefaultTesterAdr, DoIPClient.DefaultEntityAdr);
- Console.WriteLine($"DoIP client successfully connected to Server:{gateway.RemoteEp}");
- CreateUDSClients(gateway);
- }
- else
- { // wait for vehicle announcement
- DoIPClient.EntityAnnounced += DoIPClient_EntityAnnounced;
- DoIPClient.StartListenToEntityAnnouncement(LocalIp);
- Console.WriteLine($"Listening on {LocalIp} for vehicle announcements");
- }
- Console.WriteLine($"'T' to execute TesterPresent for created UDS Clients..., 'Q' to quit");
- MsgState udsState;
- bool quitPressed = false;
- while (!quitPressed)
- {
- var ci = Console.ReadKey(true);
- switch (ci.Key)
- {
- case ConsoleKey.Q: quitPressed = true; break;
- case ConsoleKey.T:
- lock (UDSClients)
- {
- Console.ForegroundColor = ConsoleColor.White;
- Console.WriteLine($"Requesting TesterPresent on {UDSClients.Count} UDS clients...");
- foreach (var udsClient in UDSClients)
- {
- if (MsgState.Success == (udsState = udsClient.TesterPresent(out RespSFBase udsResponse)))
- {
- Console.ForegroundColor = ConsoleColor.White;
- Console.WriteLine($"{udsClient.DoIPClient.SrcAdr:X} successfully received a Tester present response from {udsClient.DoIPClient.DstAdr:X} ({udsClient.DoIPClient.RemoteEp})");
- }
- else
- {
- Console.ForegroundColor = ConsoleColor.Yellow;
- Console.WriteLine($"Failed to receive Tester present request ({udsState})");
- }
- }
- }
- break;
- }
- }
- return 0;
- }
- catch (Exception ex)
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine($"Something failed!\nDetails:\n{ex.Message}");
- return -2;
- }
- finally
- { // cleanup
- DoIPClient.StopListenToEntityAnnouncement();
- foreach (var udsClient in UDSClients)
- udsClient.Dispose();
- Console.ForegroundColor = orgColor;
- }
- }
- static void DoIPClient_EntityAnnounced(object sender, AnnoncementEventArgs e)
- {
- var vehicleId = e.Entity.VehicleId;
- var status = e.Entity.EntityStatus;
- var mode = e.Entity.PowerMode;
- Console.WriteLine($"DoIP Vehicle announced from Server:{vehicleId.Frame.Sender} Protocolversion:{vehicleId.Frame.Version}");
- Console.WriteLine($"Logical Address:{vehicleId.LogicalAdr:X} VIN:{vehicleId.VIN}, EID:{BitConverter.ToString(vehicleId.EID)}, Type:{status.NodeType}, PowerMode:{mode.PowerMode}");
- switch (status.NodeType)
- {
- case NodeType.Gateway:
- var gateway = e.Entity.Client;
- CreateUDSClients(gateway);
- break;
- }
- }
- static void CreateUDSClients(DoIPClient gateway)
- {
- for (int i = 1; i <= 2; ++i)
- {
- Console.ForegroundColor = ConsoleColor.White;
- // Create DoIP Client
- var srcAdr = (ushort)(gateway.SrcAdr + i);
- var dstAdr = (ushort)(gateway.DstAdr + i);
- var doIpClient = new DoIPClient(gateway.RemoteEp.Address, gateway.LocalIp, Activation.Default, srcAdr, dstAdr);
- ResponseState state;
- if (ResponseState.Ok == (state = doIpClient.RoutingActivation(out RoutingActivationResponse activation)))
- {
- Console.WriteLine($"Routing activation result: {activation.ActivationResult}");
- switch (activation.ActivationResult)
- {
- case ActivationCode.RoutingActivated:
- lock (UDSClients)
- UDSClients.Add(new UDSClient(doIpClient, 200));
- break;
- default:
- Console.ForegroundColor = ConsoleColor.Yellow;
- Console.WriteLine($"Routing activation denied with ({activation.ActivationResult})");
- break;
- }
- }
- else
- {
- Console.ForegroundColor = ConsoleColor.Yellow;
- Console.WriteLine($"Failed to request Routing activation ({state})");
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment