alesi2000

ASAP2DoIPExample

Aug 1st, 2021 (edited)
1,437
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.55 KB | None | 0 0
  1. using jnsoft.Diagnose.Comm.UDS;
  2. using jnsoft.Helpers;
  3. using System.Net;
  4.  
  5. namespace jnsoft.Diagnose.Comm.DoIp.Examples;
  6.  
  7. /// <summary>
  8. /// ASAP2 DoIP Example.
  9. ///
  10. /// This example collects DoIP Vehicle announcements on the specified local IPAddress.
  11. /// - A DoIP Client is created to access a DoIP gateway
  12. /// - Two UDS client are created to request TesterPresent commands
  13. ///
  14. /// Usage: ASAP2DoIPExample 192.168.0.2
  15. /// </summary>
  16. class Program
  17. {
  18.   static readonly List<UDSClient> UDSClients = [];
  19.   static IPAddress LocalIp;
  20.   static IPAddress RemoteIp;
  21.  
  22.   static int Main(string[] args)
  23.   {
  24.     var orgColor = Console.ForegroundColor;
  25.     try
  26.     {
  27.       Console.ForegroundColor = ConsoleColor.White;
  28.       if (args.Length < 1)
  29.       { // no args -> present usage
  30.         var appName = Extensions.AppName;
  31.         Console.WriteLine($"{appName}({Extensions.AppVersion})");
  32.         Console.WriteLine("UDS over DoIP example...");
  33.         Console.WriteLine("Required input: the IP address of the local network to use");
  34.         Console.WriteLine($"Usage: {appName} Local_IPV4_or_IPV6_Address [Remote_IPV4_or_IPV6_Address]");
  35.         Console.WriteLine($"Example: {appName} 192.168.0.2");
  36.         return -1;
  37.       }
  38.  
  39.       LocalIp = IPAddress.Parse(args[0]);
  40.  
  41.       if (args.Length > 1)
  42.       { // try to connect to remote DoIP äentity
  43.         RemoteIp = args.Length > 1 ? IPAddress.Parse(args[1]) : null;
  44.         var gateway = new DoIPClient(RemoteIp, LocalIp, Activation.Default, DoIPClient.DefaultTesterAdr, DoIPClient.DefaultEntityAdr);
  45.         Console.WriteLine($"DoIP client successfully connected to Server:{gateway.RemoteEp}");
  46.         CreateUDSClients(gateway);
  47.       }
  48.       else
  49.       { // wait for vehicle announcement
  50.         DoIPClient.EntityAnnounced += DoIPClient_EntityAnnounced;
  51.         DoIPClient.StartListenToEntityAnnouncement(LocalIp);
  52.         Console.WriteLine($"Listening on {LocalIp} for vehicle announcements");
  53.       }
  54.  
  55.       Console.WriteLine($"'T' to execute TesterPresent for created UDS Clients..., 'Q' to quit");
  56.  
  57.       MsgState udsState;
  58.       bool quitPressed = false;
  59.       while (!quitPressed)
  60.       {
  61.         var ci = Console.ReadKey(true);
  62.         switch (ci.Key)
  63.         {
  64.           case ConsoleKey.Q: quitPressed = true; break;
  65.  
  66.           case ConsoleKey.T:
  67.             lock (UDSClients)
  68.             {
  69.               Console.ForegroundColor = ConsoleColor.White;
  70.               Console.WriteLine($"Requesting TesterPresent on {UDSClients.Count} UDS clients...");
  71.               foreach (var udsClient in UDSClients)
  72.               {
  73.                 if (MsgState.Success == (udsState = udsClient.TesterPresent(out var udsResponse)))
  74.                 {
  75.                   Console.ForegroundColor = ConsoleColor.White;
  76.                   Console.WriteLine($"{udsClient.DoIPClient.SrcAdr:X} successfully received a Tester present response from {udsClient.DoIPClient.DstAdr:X} ({udsClient.DoIPClient.RemoteEp})");
  77.                 }
  78.                 else
  79.                 {
  80.                   Console.ForegroundColor = ConsoleColor.Yellow;
  81.                   Console.WriteLine($"Failed to receive Tester present request ({udsState})");
  82.                 }
  83.               }
  84.             }
  85.             break;
  86.         }
  87.       }
  88.       return 0;
  89.     }
  90.     catch (Exception ex)
  91.     {
  92.       Console.ForegroundColor = ConsoleColor.Red;
  93.       Console.WriteLine($"Something failed!\nDetails:\n{ex.Message}");
  94.       return -2;
  95.     }
  96.     finally
  97.     { // cleanup
  98.       DoIPClient.StopListenToEntityAnnouncement();
  99.  
  100.       foreach (var udsClient in UDSClients)
  101.         udsClient.Dispose();
  102.  
  103.       Console.ForegroundColor = orgColor;
  104.     }
  105.   }
  106.  
  107.   static void DoIPClient_EntityAnnounced(object sender, AnnoncementEventArgs e)
  108.   {
  109.     var vehicleId = e.Entity.VehicleId;
  110.     var status = e.Entity.EntityStatus;
  111.     var mode = e.Entity.PowerMode;
  112.     Console.WriteLine($"DoIP Vehicle announced from Server:{vehicleId.Frame.Sender} Protocolversion:{vehicleId.Frame.Version}");
  113.     Console.WriteLine($"Logical Address:{vehicleId.LogicalAdr:X} VIN:{vehicleId.VIN}, EID:{BitConverter.ToString(vehicleId.EID)}, Type:{status.NodeType}, PowerMode:{mode.PowerMode}");
  114.  
  115.     switch (status.NodeType)
  116.     {
  117.       case NodeType.Gateway:
  118.         var gateway = e.Entity.Client;
  119.         CreateUDSClients(gateway);
  120.         break;
  121.     }
  122.   }
  123.  
  124.   static void CreateUDSClients(DoIPClient gateway)
  125.   {
  126.     for (int i = 1; i <= 2; ++i)
  127.     {
  128.       Console.ForegroundColor = ConsoleColor.White;
  129.  
  130.       // Create DoIP Client
  131.       var srcAdr = (ushort)(gateway.SrcAdr + i);
  132.       var dstAdr = (ushort)(gateway.DstAdr + i);
  133.       var doIpClient = new DoIPClient(gateway.RemoteEp.Address, gateway.LocalIp, Activation.Default, srcAdr, dstAdr);
  134.  
  135.       ResponseState state;
  136.       if (ResponseState.Ok == (state = doIpClient.RoutingActivation(out var activation)))
  137.       {
  138.         Console.WriteLine($"Routing activation result: {activation.ActivationResult}");
  139.         switch (activation.ActivationResult)
  140.         {
  141.           case ActivationCode.RoutingActivated:
  142.             lock (UDSClients)
  143.               UDSClients.Add(new UDSClient(doIpClient, 200));
  144.             break;
  145.           default:
  146.             Console.ForegroundColor = ConsoleColor.Yellow;
  147.             Console.WriteLine($"Routing activation denied with ({activation.ActivationResult})");
  148.             break;
  149.         }
  150.       }
  151.       else
  152.       {
  153.         Console.ForegroundColor = ConsoleColor.Yellow;
  154.         Console.WriteLine($"Failed to request Routing activation ({state})");
  155.       }
  156.     }
  157.   }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment