atMETEO
An ATmega based weather station
ethernet.h
Go to the documentation of this file.
1 /*
2  * atMETEO - An ATmega based weather station
3  * Copyright (C) 2014-2015 Christian Fetzer
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #pragma once
21 
38 #include <inttypes.h> // AVR toolchain doesn't offer cinttypes header
39 
40 namespace Avr
41 {
42 
43 class Wiznet;
44 
54 {
55 public:
66  MacAddress(uint8_t first_octet, uint8_t second_octet,
67  uint8_t third_octet, uint8_t fourth_octet,
68  uint8_t fifth_octet, uint8_t sixth_octet)
69  {
70  m_address[0] = first_octet;
71  m_address[1] = second_octet;
72  m_address[2] = third_octet;
73  m_address[3] = fourth_octet;
74  m_address[4] = fifth_octet;
75  m_address[5] = sixth_octet;
76  }
77 
78 private:
79  uint8_t m_address[6];
80 
81  friend class Wiznet;
82  const uint8_t *rawAddress() const { return m_address; }
83 };
84 
88 class IpAddress
89 {
90 public:
95  {
96  }
97 
106  IpAddress(uint8_t first_octet, uint8_t second_octet,
107  uint8_t third_octet, uint8_t fourth_octet)
108  {
109  m_address[0] = first_octet;
110  m_address[1] = second_octet;
111  m_address[2] = third_octet;
112  m_address[3] = fourth_octet;
113  }
114 
115 private:
116  uint8_t m_address[4];
117 
118  friend class Wiznet;
119  const uint8_t *rawAddress() { return m_address; }
120 };
121 
139 template <typename TDriver>
140 class Ethernet : private TDriver
141 {
142 public:
154  : TDriver(mac, ip, subnet)
155  {
156  }
157 
170  bool sendUdpMessage(IpAddress dest, uint16_t port, const char *message)
171  {
172  return TDriver::internalSendUdpMessage(dest, port, message);
173  }
174 };
175  // \addtogroup libtarget_ethernet
177 
178 } // namespace Avr
Represents an Ethernet MAC address.
Definition: ethernet.h:53
IpAddress()
Initializes an IP address to 0.0.0.0.
Definition: ethernet.h:94
Namespace containing all symbols of the AVR C++ utilities library.
Definition: adc.h:48
Avr::Ethernet driver for WIZnet Ethernet modules (W5100, W5200, W5300, W5500).
Definition: wiznet.h:42
bool sendUdpMessage(IpAddress dest, uint16_t port, const char *message)
Sends an UDP message to the specified destination.
Definition: ethernet.h:170
A C++ wrapper for accessing Ethernet communication interfaces.
Definition: ethernet.h:140
IpAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet)
Initializes an IP address from octets.
Definition: ethernet.h:106
MacAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet, uint8_t fifth_octet, uint8_t sixth_octet)
Initializes a MAC address from octets.
Definition: ethernet.h:66
Represents an IPv4 address.
Definition: ethernet.h:88
Ethernet(MacAddress mac, IpAddress ip, IpAddress subnet)
Initializes the Ethernet driver.
Definition: ethernet.h:153