ada_enet_ea315488/demos/ping_text_io/src/network.adb

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
--  SPDX-FileCopyrightText: 2024 Max Reznik <reznikmm@gmail.com>
--
--  SPDX-License-Identifier: Apache-2.0
----------------------------------------------------------------

with Ada.Text_IO;
with Interfaces;

with Ethernet.MDIO;
with Ethernet.PHY_Management;

with Net.Headers;
with Net.Protos.Icmp;
with Net.Utils;

with Net.Generic_Receiver;

package body Network is

   package LAN_Receiver is new Net.Generic_Receiver
     (Net.Interfaces.Ifnet_Type'Class (STM32_MAC));

   ------------------
   -- ICMP_Handler --
   ------------------

   procedure ICMP_Handler
     (Ifnet  : in out Net.Interfaces.Ifnet_Type'Class;
      Packet : in out Net.Buffers.Buffer_Type)
   is
      use type Net.Uint8;
      IP : constant Net.Headers.IP_Header_Access := Packet.IP;
      ICMP : constant Net.Headers.ICMP_Header_Access := Packet.ICMP;
   begin
      if ICMP.Icmp_Type = Net.Headers.ICMP_ECHO_REPLY then
         Ada.Text_IO.Put (Packet.Get_Length'Image);
         Ada.Text_IO.Put (" bytes from ");
         Ada.Text_IO.Put (Net.Utils.To_String (IP.Ip_Src));
         Ada.Text_IO.Put (" seq=");
         Ada.Text_IO.Put (Net.Headers.To_Host (ICMP.Icmp_Seq)'Image);
         Ada.Text_IO.New_Line;
      else
         Net.Protos.Icmp.Receive (Ifnet, Packet);
      end if;
   end ICMP_Handler;

   ----------------
   -- Initialize --
   ----------------

   procedure Initialize is
   begin
      STM32_MAC.Configure (Net.STM32_Interfaces.STM32F407_Pins, RMII => True);
      LAN_Receiver.Start;
      DHCP.Initialize (STM32_MAC'Access);
   end Initialize;

end Network;