COSA
An Object-Oriented Platform for Arduino Programming
Socket.hh
Go to the documentation of this file.
1 
21 #ifndef COSA_SOCKET_HH
22 #define COSA_SOCKET_HH
23 
24 #include "Cosa/Types.h"
25 #include "Cosa/INET.hh"
26 #include "Cosa/IOStream.hh"
27 
31 class Socket : public IOStream::Device {
32 public:
34  static const uint16_t DYNAMIC_PORT = 49152;
35 
37  enum Protocol {
43  } __attribute__((packed));
44 
48  Socket() :
49  IOStream::Device(),
50  m_src(),
51  m_proto(0),
52  m_port(0)
53  {
54  }
55 
60  void src(INET::addr_t& addr) const
61  {
62  addr = m_src;
63  }
64 
69  Protocol proto() const
70  {
71  return ((Protocol) m_proto);
72  }
73 
78  uint16_t port() const
79  {
80  return (m_port);
81  }
82 
85 
93  virtual int write(const void* buf, size_t size)
94  {
95  return (write(buf, size, false));
96  }
97 
105  virtual int write_P(const void* buf, size_t size)
106  {
107  return (write(buf, size, true));
108  }
109 
115  virtual int getchar()
116  {
117  char c;
118  int res = recv(&c, sizeof(c));
119  return (res == sizeof(c) ? (c & 0xff) : res);
120  }
121 
124 
132  virtual int read(void* buf, size_t size)
133  {
134  return (recv(buf, size));
135  }
136 
145  virtual int open(Protocol proto, uint16_t port, uint8_t flag) = 0;
146 
152  virtual int close() = 0;
153 
159  virtual int listen() = 0;
160 
168  virtual int accept() = 0;
169 
177  virtual int connect(uint8_t addr[4], uint16_t port) = 0;
178 
187  virtual int connect(const char* hostname, uint16_t port) = 0;
188 
196  virtual int is_connected() = 0;
197 
203  virtual int disconnect() = 0;
204 
213  virtual int datagram(uint8_t addr[4], uint16_t port) = 0;
214 
222  int send(const void* buf, size_t len)
223  {
224  return (send(buf, len, false));
225  }
226 
236  int send_P(const void* buf, size_t len)
237  {
238  return (send(buf, len, true));
239  }
240 
251  virtual int recv(void* buf, size_t len) = 0;
252 
264  int send(const void* buf, size_t len, uint8_t dest[4], uint16_t port)
265  {
266  return (send(buf, len, dest, port, false));
267  }
268 
281  int send_P(const void* buf, size_t len, uint8_t dest[4], uint16_t port)
282  {
283  return (send(buf, len, dest, port, true));
284  }
285 
298  virtual int recv(void* buf, size_t len, uint8_t src[4], uint16_t& port) = 0;
299 
300 protected:
303 
305  uint8_t m_proto;
306 
308  uint16_t m_port;
309 
320  virtual int write(const void* buf, size_t size, bool progmem)
321  {
322  return (send(buf, size, progmem));
323  }
324 
336  virtual int send(const void* buf, size_t len, bool progmem) = 0;
337 
351  virtual int send(const void* buf, size_t len,
352  uint8_t dest[4], uint16_t port,
353  bool progmem) = 0;
354 };
355 #endif
virtual int listen()=0
Definition: Socket.hh:31
virtual int recv(void *buf, size_t len)=0
INET::addr_t m_src
Definition: Socket.hh:302
virtual int write(const void *buf, size_t size)
Definition: Socket.hh:93
int send_P(const void *buf, size_t len, uint8_t dest[4], uint16_t port)
Definition: Socket.hh:281
virtual int write(const void *buf, size_t size)
virtual int read(void *buf, size_t size)
virtual int open(Protocol proto, uint16_t port, uint8_t flag)=0
virtual int connect(uint8_t addr[4], uint16_t port)=0
int send(const void *buf, size_t len)
Definition: Socket.hh:222
Protocol
Definition: Socket.hh:37
virtual int write(const void *buf, size_t size, bool progmem)
Definition: Socket.hh:320
virtual int getchar()
Definition: Socket.hh:115
virtual int datagram(uint8_t addr[4], uint16_t port)=0
int send_P(const void *buf, size_t len)
Definition: Socket.hh:236
virtual int write_P(const void *buf, size_t size)
Definition: Socket.hh:105
uint16_t port() const
Definition: Socket.hh:78
Definition: INET.hh:41
virtual int is_connected()=0
void src(INET::addr_t &addr) const
Definition: Socket.hh:60
virtual int close()=0
uint8_t m_proto
Definition: Socket.hh:305
virtual int disconnect()=0
Protocol proto() const
Definition: Socket.hh:69
uint16_t m_port
Definition: Socket.hh:308
Socket()
Definition: Socket.hh:48
int send(const void *buf, size_t len, uint8_t dest[4], uint16_t port)
Definition: Socket.hh:264
Definition: INET.hh:40
virtual int accept()=0
virtual int read(void *buf, size_t size)
Definition: Socket.hh:132
static const uint16_t DYNAMIC_PORT
Definition: Socket.hh:34