friendev EtherDune TCP/IP library
PingTest.ino
Go to the documentation of this file.
1 // EtherDune ICMP Ping example
2 // Author: Javier Peletier <jm@friendev.com>
3 // Summary: Demonstrates how to use ICMPPinger and ICMPPingAutoReply classes
4 //
5 // Copyright (c) 2015 All Rights Reserved, http://friendev.com
6 //
7 // This source is subject to the GPLv2 license.
8 // Please see the License.txt file for more information.
9 // All other rights reserved.
10 //
11 // THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
12 // KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
13 // IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
14 // PARTICULAR PURPOSE.
15 
18 
20 
21 #include <ACross.h>
22 #include <Checksum.h>
23 #include <UDPSocket.h>
24 #include <inet.h>
25 #include <ENC28J60.h>
26 #include <FlowScanner.h>
27 #include <ICMPPingAutoReply.h>
28 #include <ICMPPinger.h>
29 #include <DHCP.h>
30 
31 #define AC_LOGLEVEL 6
32 #include <ACLog.h>
33 ACROSS_MODULE("PingTest");
34 
35 static const uint8_t CS_PIN = 10; //Put here what pin you are using for your ENC28J60's chip select
36 static MACAddress_P mymac = { 0x66, 0x72, 0x69, 0x65, 0x6e, 0x64 };
37 
38 // Instantiate the DHCP service
39 DHCP dhcp;
40 
41 // Instantiate ICMPPingAutoReply service, takes care of replying to incoming echo requests. No other action is necessary.
42 ICMPPingAutoReply pingAutoReply;
43 
44 class ICMPHandler : ICMPPinger
45 {
46  static const uint8_t PING_PERIOD = NTICKS(1000); //ms
47  uint8_t timer;
48  IPAddress target;
49 
50  void onPingReply(uint16_t time)
51  {
52  printf_P(PSTR("Reply from %d.%d.%d.%d: bytes=%d time=%dms TTL=%d\n"),
53  packet.ip.sourceIP.b[0], packet.ip.sourceIP.b[1], packet.ip.sourceIP.b[2], packet.ip.sourceIP.b[3],
54  packet.ip.totalLength - sizeof(IPHeader) - sizeof(ICMPHeader),
55  time, packet.ip.TTL);
56 
57  }
58 
59  void tick()
60  {
61  timer--;
62 
63  if (timer == 0)
64  {
65  timer = PING_PERIOD;
66  ping(target);
67  }
68 
69 
71  }
72 
73 public:
74  void start(const IPAddress& targetIP)
75  {
76  timer = 1;
77  target = targetIP;
78  tick();
79  }
80 
81 
82 }pingTest;
83 
84 
85 void setup()
86 {
87 
88  Serial.begin(115200);
89  ACross::init();
90 #ifdef ACROSS_ARDUINO
91  ACross::printf_serial_init();
92 #endif
93 
94  printf_P(PSTR("ICMP Ping EtherDune sample\n"));
95  printf_P(PSTR("Free RAM: %d"),ACross::getFreeRam());
96  printf_P(PSTR("Press any key to start...\n"));
97 
98  while (!Serial.available());
99 
101 
102  if (!net::begin(CS_PIN))
103  ACERROR("failed to start EtherDune");
104 
105  ACINFO("waiting for link...");
106 
107  while (!net::isLinkUp());
108 
109  ACINFO("link is up");
110 
111 
112  if (!dhcp.dhcpSetup())
113  {
114  printf_P(PSTR("DHCP setup failed\n"));
115  ACross::halt(1);
116  }
117 
118  printf_P(PSTR("DHCP setup OK\n"));
119 
120  printf_P(PSTR("Local IP is %d.%d.%d.%d. Try pinging me!\n\n"),
121  net::localIP.b[0], net::localIP.b[1], net::localIP.b[2], net::localIP.b[3]);
122 
123  IPAddress targetIP;
124  targetIP = IPADDR_P(8, 8, 8, 8); // ping Google's DNS 8.8.8.8
125  pingTest.start(targetIP);
126 
127 }
128 
129 void loop()
130 {
131  net::loop(); //process packets and run all services.
132 
133 }
134 
135 /// \endcond
Implements an ICMP Echo reply service.
#define MACAddress_P
Definition: inet.h:242
EtherDune DHCP Service.
Definition: DHCP.h:122
virtual void onPingReply(uint16_t roundtripTime)=0
static bool begin(uint8_t cspin)
Initializes EtherDune and the underlying hardware
represents an IP address in memory
Definition: inet.h:181
ACROSS_MODULE("ARP")
static const uint8_t CS_PIN
void ping(const IPAddress &targetIP)
Send an ICMP Echo request (ping) to the specified target IP address.
Definition: ICMPPinger.cpp:44
#define IPADDR_P(b0, b1, b2, b3)
defines an IP address as stored in PROGMEM
Definition: inet.h:177
bool dhcpSetup()
Attempts to configure the IP settings: local IP, subnet mask, gateway and DNS via DHCP...
Definition: DHCP.cpp:50
virtual void tick()
This is a timer function that is called every NETWORK_TIMER_RESOLUTION milliseconds on every service...
IP header.
Definition: inet.h:296
static MACAddress_P mymac
static bool isLinkUp()
Determines whether the network link is ready
Definition: ENC28J60.cpp:306
ICMP Header and Echo request/reply.
Definition: inet.h:431
void setup()
DHCP dhcp
static MACAddress localMAC
Ethernet MAC address.
Base data structures for Internet communication.
static void loop()
Gives processing time to EtherDune so that it can check for incoming packets or send queued packets...
static IPAddress localIP
IP address of this application.
Implements an ICMP Echo request service.
Definition: ICMPPinger.h:30
void loop()