COSA
An Object-Oriented Platform for Arduino Programming
INET.hh
Go to the documentation of this file.
1 
21 #ifndef COSA_INET_HH
22 #define COSA_INET_HH
23 
24 #include "Cosa/Types.h"
25 #include "Cosa/IOStream.hh"
26 
30 enum {
31  AF_INET = 2
32 };
33 
37 enum {
40  SOCK_RAW = 3,
41  SOCK_RDM = 4,
43 };
44 
49 enum {
58 };
59 
60 class Socket;
61 
65 class INET {
66 public:
67  static const uint8_t PATH_MAX = 64;
68  static const uint8_t MAC_MAX = 6;
69  static const uint8_t IP_MAX = 4;
70 
74  struct addr_t {
75  uint8_t mac[MAC_MAX];
76  uint8_t ip[IP_MAX];
77  uint16_t port;
78 
79  addr_t() :
80  port(0)
81  {
82  memset(mac, 0, MAC_MAX);
83  memset(ip, 0, IP_MAX);
84  }
85  };
86 
91  static bool is_illegal(uint8_t addr[IP_MAX], uint16_t port);
92 
101  static int aton(const char* addr, uint8_t ip[IP_MAX], bool progmem = false);
102 
110  static int aton_P(const char* addr, uint8_t ip[IP_MAX])
111  __attribute__((always_inline))
112  {
113  return (aton(addr, ip, true));
114  }
115 
127  static int nametopath(const char* hostname, char* path, bool progmem = false);
128 
139  static int nametopath_P(const char* hostname, char* path)
140  __attribute__((always_inline))
141  {
142  return (nametopath(hostname, path, true));
143  }
144 
150  static void print_path(IOStream& outs, const char* path);
151 
158  static void print_mac(IOStream& outs, const uint8_t mac[MAC_MAX]);
159 
167  static void print_addr(IOStream& outs, const uint8_t addr[IP_MAX], uint16_t port = 0);
168 
177  static uint16_t checksum(const void* buf, size_t count);
178 
184  class Server {
185  public:
192  Server(IOStream& ios) :
193  m_ios(ios),
194  m_connected(false)
195  {}
196 
202  {
203  return ((Socket*) m_ios.device());
204  }
205 
210  void client(INET::addr_t& addr);
211 
220  virtual bool begin(Socket* sock);
221 
231  virtual int run(uint32_t ms = 0L);
232 
239  virtual bool end();
240 
248  virtual bool on_accept(IOStream& ios)
249  {
250  UNUSED(ios);
251  return (true);
252  }
253 
260  virtual void on_connect(IOStream& ios)
261  {
262  UNUSED(ios);
263  }
264 
271  virtual void on_request(IOStream& ios) = 0;
272 
277  virtual void on_disconnect() {}
278 
279  protected:
282 
285  };
286 };
287 
288 #endif
Definition: Socket.hh:31
static void print_mac(IOStream &outs, const uint8_t mac[MAC_MAX])
Definition: INET.cpp:111
uint16_t port
Service port.
Definition: INET.hh:77
static int aton(const char *addr, uint8_t ip[IP_MAX], bool progmem=false)
Definition: INET.cpp:42
virtual bool on_accept(IOStream &ios)
Definition: INET.hh:248
IOStream & m_ios
Definition: INET.hh:281
static bool is_illegal(uint8_t addr[IP_MAX], uint16_t port)
Definition: INET.cpp:25
Socket * socket()
Definition: INET.hh:201
bool m_connected
Definition: INET.hh:284
addr_t()
Definition: INET.hh:79
static uint16_t checksum(const void *buf, size_t count)
Definition: INET.cpp:128
static int aton_P(const char *addr, uint8_t ip[IP_MAX])
Definition: INET.hh:110
Definition: INET.hh:41
uint8_t ip[IP_MAX]
Network address.
Definition: INET.hh:76
static void print_path(IOStream &outs, const char *path)
Definition: INET.cpp:92
#define UNUSED(x)
Definition: ATmega328P.hh:31
static const uint8_t IP_MAX
Definition: INET.hh:69
Definition: INET.hh:65
uint8_t mac[MAC_MAX]
Hardware address.
Definition: INET.hh:75
Server(IOStream &ios)
Definition: INET.hh:192
virtual void on_connect(IOStream &ios)
Definition: INET.hh:260
virtual void on_disconnect()
Definition: INET.hh:277
static const uint8_t MAC_MAX
Definition: INET.hh:68
static void print_addr(IOStream &outs, const uint8_t addr[IP_MAX], uint16_t port=0)
Definition: INET.cpp:119
static const uint8_t PATH_MAX
Definition: INET.hh:67
Definition: INET.hh:31
Definition: INET.hh:40
static int nametopath_P(const char *hostname, char *path)
Definition: INET.hh:139
static int nametopath(const char *hostname, char *path, bool progmem=false)
Definition: INET.cpp:63