COSA
An Object-Oriented Platform for Arduino Programming
Socket Class Referenceabstract

#include <Socket.hh>

Inheritance diagram for Socket:
Inheritance graph
Collaboration diagram for Socket:
Collaboration graph

Public Types

enum  Protocol {
  TCP = SOCK_STREAM, UDP = SOCK_DGRAM, IPRAW = SOCK_RAW, MACRAW = SOCK_RDM,
  PPPoE = SOCK_SEQPACKET
}
 

Public Member Functions

 Socket ()
 
void src (INET::addr_t &addr) const
 
Protocol proto () const
 
uint16_t port () const
 
virtual int write (const void *buf, size_t size)
 
virtual int write_P (const void *buf, size_t size)
 
virtual int getchar ()
 
virtual int read (void *buf, size_t size)
 
virtual int open (Protocol proto, uint16_t port, uint8_t flag)=0
 
virtual int close ()=0
 
virtual int listen ()=0
 
virtual int accept ()=0
 
virtual int connect (uint8_t addr[4], uint16_t port)=0
 
virtual int connect (const char *hostname, uint16_t port)=0
 
virtual int is_connected ()=0
 
virtual int disconnect ()=0
 
virtual int datagram (uint8_t addr[4], uint16_t port)=0
 
int send (const void *buf, size_t len)
 
int send_P (const void *buf, size_t len)
 
virtual int recv (void *buf, size_t len)=0
 
int send (const void *buf, size_t len, uint8_t dest[4], uint16_t port)
 
int send_P (const void *buf, size_t len, uint8_t dest[4], uint16_t port)
 
virtual int recv (void *buf, size_t len, uint8_t src[4], uint16_t &port)=0
 
void non_blocking ()
 
void blocking ()
 
bool is_blocking () const
 
void eol (Mode mode)
 
Mode eol () const
 
virtual int available ()
 
virtual int room ()
 
virtual int putchar (char c)
 
virtual int puts (const char *s)
 
virtual int puts (str_P s)
 
virtual int write (const iovec_t *vec)
 
virtual int peekchar ()
 
virtual int peekchar (char c)
 
virtual char * gets (char *s, size_t count)
 
virtual int read (iovec_t *vec)
 
virtual int flush ()
 
virtual void empty ()
 

Static Public Attributes

static const uint16_t DYNAMIC_PORT = 49152
 

Protected Member Functions

virtual int write (const void *buf, size_t size, bool progmem)
 
virtual int send (const void *buf, size_t len, bool progmem)=0
 
virtual int send (const void *buf, size_t len, uint8_t dest[4], uint16_t port, bool progmem)=0
 

Protected Attributes

INET::addr_t m_src
 
uint8_t m_proto
 
uint16_t m_port
 
bool m_blocking
 
Mode m_eol
 

Detailed Description

Abstract Interface for Internet Sockets.

Definition at line 31 of file Socket.hh.

Member Enumeration Documentation

Socket type.

Enumerator
TCP 
UDP 
IPRAW 
MACRAW 
PPPoE 

Definition at line 37 of file Socket.hh.

Constructor & Destructor Documentation

Socket::Socket ( )
inline

Socket constructor. Initial state of socket.

Definition at line 48 of file Socket.hh.

Member Function Documentation

virtual int Socket::accept ( )
pure virtual

Check for incoming requests from clients. Return zero if the socket has accepted a request and a connection is established.

Returns
zero if successful otherwise negative error code.

Implemented in W5200::Driver, W5500::Driver, W5100::Driver, and CC3000::Driver.

int IOStream::Device::available ( )
virtualinherited

Number of bytes available (possible to read).

Returns
bytes.

Reimplemented in W5200::Driver, W5500::Driver, W5100::Driver, Soft::UART, UART, IOBuffer< SIZE >, and CC3000::Driver.

Definition at line 24 of file IOStream_Device.cpp.

void IOStream::Device::blocking ( )
inlineinherited

Set blocking mode.

Definition at line 85 of file IOStream.hh.

virtual int Socket::close ( )
pure virtual

Close the socket.

Returns
zero if successful otherwise negative error code.

Implemented in W5200::Driver, W5500::Driver, W5100::Driver, and CC3000::Driver.

virtual int Socket::connect ( uint8_t  addr[4],
uint16_t  port 
)
pure virtual

Connect the socket to the given address and port; client mode.

Parameters
[in]addrdestination address.
[in]portdestination port.
Returns
zero if successful otherwise negative error code.

Implemented in W5200::Driver, W5500::Driver, W5100::Driver, and CC3000::Driver.

virtual int Socket::connect ( const char *  hostname,
uint16_t  port 
)
pure virtual

Connect the socket to the given hostname and port; client mode. Returns zero if connection established otherwise negative error code.

Parameters
[in]hostnamestring.
[in]portdestination port.
Returns
zero if successful otherwise negative error code.

Implemented in W5200::Driver, W5500::Driver, W5100::Driver, and CC3000::Driver.

virtual int Socket::datagram ( uint8_t  addr[4],
uint16_t  port 
)
pure virtual

Start the construction of a datagram to the given address and port.

Parameters
[in]addrdestination address.
[in]portdestination port.
Returns
zero if successful otherwise negative error code.

Implemented in W5200::Driver, W5500::Driver, W5100::Driver, and CC3000::Driver.

virtual int Socket::disconnect ( )
pure virtual

Disconnect socket from server.

Returns
zero if successful otherwise negative error code.

Implemented in W5200::Driver, W5500::Driver, W5100::Driver, and CC3000::Driver.

void IOStream::Device::empty ( )
virtualinherited

Empty internal device buffers.

Reimplemented in Soft::UART, UART, and IOBuffer< SIZE >.

Definition at line 175 of file IOStream_Device.cpp.

void IOStream::Device::eol ( Mode  mode)
inlineinherited

Set end of line mode.

Parameters
[in]modefor end of line.

Definition at line 103 of file IOStream.hh.

Mode IOStream::Device::eol ( ) const
inlineinherited

Get end of line mode.

Returns
mode.

Definition at line 112 of file IOStream.hh.

int IOStream::Device::flush ( )
virtualinherited

Flush internal device buffers. Wait for device to become idle.

Returns
zero(0) or negative error code.

Reimplemented in W5200::Driver, W5500::Driver, W5100::Driver, UART, IOBuffer< SIZE >, CC3000::Driver, and WIO.

Definition at line 169 of file IOStream_Device.cpp.

virtual int Socket::getchar ( )
inlinevirtual

Read character from device.

Returns
character or EOF(-1).

Reimplemented from IOStream::Device.

Definition at line 115 of file Socket.hh.

char * IOStream::Device::gets ( char *  s,
size_t  count 
)
virtualinherited

Read string terminated by new-line or until size into given string buffer. Returns pointer to string or NULL if empty line.

Parameters
[in]sstring buffer to read into.
[in]countmax number of bytes to read.
Returns
string pointer or NULL.

Definition at line 118 of file IOStream_Device.cpp.

bool IOStream::Device::is_blocking ( ) const
inlineinherited

Is blocking mode?

Returns
bool.

Definition at line 94 of file IOStream.hh.

virtual int Socket::is_connected ( )
pure virtual

Returns positive integer if a connection is established, zero is not yet established, otherwise a negative error code.

Returns
positive integer connected, zero if not otherwise negative error code.

Implemented in W5200::Driver, W5500::Driver, W5100::Driver, and CC3000::Driver.

virtual int Socket::listen ( )
pure virtual

Mark socket for incoming requests; server mode.

Returns
zero if successful otherwise negative error code.

Implemented in W5200::Driver, W5500::Driver, W5100::Driver, and CC3000::Driver.

void IOStream::Device::non_blocking ( )
inlineinherited

Set non-blocking mode.

Definition at line 77 of file IOStream.hh.

virtual int Socket::open ( Protocol  proto,
uint16_t  port,
uint8_t  flag 
)
pure virtual

Initiate socket to the given protocol and possible port.

Parameters
[in]protoprotocol.
[in]portsource port.
[in]flagsocket options.
Returns
zero if successful otherwise negative error code.

Implemented in W5200::Driver, W5500::Driver, W5100::Driver, and CC3000::Driver.

int IOStream::Device::peekchar ( )
virtualinherited

Peek at the next character from device.

Returns
character or EOF(-1).

Reimplemented in Soft::UART, UART, and IOBuffer< SIZE >.

Definition at line 99 of file IOStream_Device.cpp.

int IOStream::Device::peekchar ( char  c)
virtualinherited

Peek for the given character in device buffer. Return number of characters or EOF(-1).

Parameters
[in]ccharacter to peek for.
Returns
available or EOF(-1).

Reimplemented in Soft::UART, UART, and IOBuffer< SIZE >.

Definition at line 105 of file IOStream_Device.cpp.

uint16_t Socket::port ( ) const
inline

Get socket port.

Returns
port.

Definition at line 78 of file Socket.hh.

Protocol Socket::proto ( ) const
inline

Get socket protocol.

Returns
protocol.

Definition at line 69 of file Socket.hh.

int IOStream::Device::putchar ( char  c)
virtualinherited

Write character to device.

Parameters
[in]ccharacter to write.
Returns
character written or EOF(-1).

Reimplemented in FAT16::File, HD44780, ST7565, PCD8544, MAX72XX, VLCD, UART, Textbox, IOBuffer< SIZE >, RS485, WIO, and Soft::UAT.

Definition at line 36 of file IOStream_Device.cpp.

int IOStream::Device::puts ( const char *  s)
virtualinherited

Write null terminated string to device. Terminating null is not written.

Parameters
[in]sstring to write.
Returns
zero(0) or negative error code.

Definition at line 43 of file IOStream_Device.cpp.

int IOStream::Device::puts ( str_P  s)
virtualinherited

Write null terminated string from program memory to device. Terminating null is not written.

Parameters
[in]sstring in program memory to write.
Returns
zero(0) or negative error code.

Definition at line 54 of file IOStream_Device.cpp.

virtual int Socket::read ( void *  buf,
size_t  size 
)
inlinevirtual

Read data to given buffer with given size from device.

Parameters
[in]bufbuffer to read into.
[in]sizenumber of bytes to read.
Returns
number of bytes read or EOF(-1).

Reimplemented from IOStream::Device.

Reimplemented in W5200::Driver, W5500::Driver, W5100::Driver, and CC3000::Driver.

Definition at line 132 of file Socket.hh.

int IOStream::Device::read ( iovec_t vec)
virtualinherited

Read data to given buffers in null terminated io vector.

Parameters
[in]vecio vector with buffers to read into.
Returns
number of bytes read or EOF(-1).

Definition at line 157 of file IOStream_Device.cpp.

virtual int Socket::recv ( void *  buf,
size_t  len 
)
pure virtual

Receive data from connection-oriented socket. The data is stored in given buffer with given maximum number of bytes. Return number of bytes or negative error code.

Parameters
[in]bufbuffer pointer.
[in]lennumber of bytes in buffer.
Returns
number of bytes sent if successful otherwise negative error code.

Implemented in W5200::Driver, W5500::Driver, W5100::Driver, and CC3000::Driver.

virtual int Socket::recv ( void *  buf,
size_t  len,
uint8_t  src[4],
uint16_t &  port 
)
pure virtual

Receive datagram on connectionless socket into given buffer with given maximum size. Returns zero(0) if successful with information in Datagram otherwise negative error code.

Parameters
[in]bufbuffer pointer.
[in]lennumber of bytes in buffer.
[in]srcsource address.
[in]portsource port.
Returns
number of bytes received if successful otherwise negative error code.

Implemented in W5200::Driver, W5500::Driver, W5100::Driver, and CC3000::Driver.

int IOStream::Device::room ( )
virtualinherited

Number of bytes room (write without blocking).

Returns
bytes.

Reimplemented in W5200::Driver, W5500::Driver, W5100::Driver, UART, IOBuffer< SIZE >, CC3000::Driver, and WIO.

Definition at line 30 of file IOStream_Device.cpp.

int Socket::send ( const void *  buf,
size_t  len 
)
inline

Send given data in buffer on connection-oriented socket. Return number of bytes or negative error code.

Parameters
[in]bufbuffer pointer.
[in]lennumber of bytes in buffer.
Returns
number of bytes sent if successful otherwise negative error code.

Definition at line 222 of file Socket.hh.

int Socket::send ( const void *  buf,
size_t  len,
uint8_t  dest[4],
uint16_t  port 
)
inline

Send given data in buffer on connectionless socket as a datagram to given destination address (dest:port). Return number of bytes sent or negative error code.

Parameters
[in]bufbuffer pointer.
[in]lennumber of bytes in buffer.
[in]destdestination address.
[in]portdestination port.
Returns
number of bytes sent if successful otherwise negative error code.

Definition at line 264 of file Socket.hh.

virtual int Socket::send ( const void *  buf,
size_t  len,
bool  progmem 
)
protectedpure virtual

Send given data in buffer on connection-oriented socket. Boolean flag progmem defined if the buffer is in program memory. Return number of bytes or negative error code.

Parameters
[in]bufbuffer pointer.
[in]lennumber of bytes in buffer.
[in]progmemprogram memory pointer flag.
Returns
number of bytes sent if successful otherwise negative error code.

Implemented in W5200::Driver, W5500::Driver, W5100::Driver, and CC3000::Driver.

virtual int Socket::send ( const void *  buf,
size_t  len,
uint8_t  dest[4],
uint16_t  port,
bool  progmem 
)
protectedpure virtual

Send given data on connectionless socket as a datagram to given destination address (dest:port). Return number of bytes sent or negative error code.

Parameters
[in]bufbuffer pointer.
[in]lennumber of bytes in buffer.
[in]destdestination address.
[in]portdestination port.
[in]progmemprogram memory pointer flag.
Returns
number of bytes sent if successful otherwise negative error code.

Implemented in W5200::Driver, W5500::Driver, W5100::Driver, and CC3000::Driver.

int Socket::send_P ( const void *  buf,
size_t  len 
)
inline

Send given data in program memory buffer on connection-oriented socket. Return number of bytes or negative error code.

Parameters
[in]bufprogram memory pointer.
[in]lennumber of bytes in buffer.
Returns
number of bytes sent if successful otherwise negative error code.

Definition at line 236 of file Socket.hh.

int Socket::send_P ( const void *  buf,
size_t  len,
uint8_t  dest[4],
uint16_t  port 
)
inline

Send given data in program memory buffer on connectionless socket as a datagram to given destination address (dest:port). Return number of bytes sent or negative error code.

Parameters
[in]bufbuffer pointer.
[in]lennumber of bytes in buffer.
[in]destdestination address.
[in]portdestination port.
Returns
number of bytes sent if successful otherwise negative error code.

Definition at line 281 of file Socket.hh.

void Socket::src ( INET::addr_t addr) const
inline

Get source machine address, network address and port.

Parameters
[out]addrnetwork address.

Definition at line 60 of file Socket.hh.

virtual int Socket::write ( const void *  buf,
size_t  size 
)
inlinevirtual

Write data from buffer with given size to device.

Parameters
[in]bufbuffer to write.
[in]sizenumber of bytes to write.
Returns
number of bytes written or EOF(-1).

Reimplemented from IOStream::Device.

Reimplemented in CC3000::Driver.

Definition at line 93 of file Socket.hh.

int IOStream::Device::write ( const iovec_t vec)
virtualinherited

Write data from buffers in null terminated io vector.

Parameters
[in]vecio vector with buffers to write.
Returns
number of bytes written or EOF(-1).

Definition at line 87 of file IOStream_Device.cpp.

virtual int Socket::write ( const void *  buf,
size_t  size,
bool  progmem 
)
inlineprotectedvirtual

Write data from buffer with given size to device. Boolean flag progmem defined if the buffer is in program memory. Return number of bytes or negative error code.

Parameters
[in]bufbuffer to write.
[in]sizenumber of bytes to write.
[in]progmemprogram memory pointer flag.
Returns
number of bytes written or EOF(-1).

Reimplemented in W5200::Driver, W5500::Driver, W5100::Driver, and CC3000::Driver.

Definition at line 320 of file Socket.hh.

virtual int Socket::write_P ( const void *  buf,
size_t  size 
)
inlinevirtual

Write data from buffer in program memory with given size to device.

Parameters
[in]bufbuffer to write.
[in]sizenumber of bytes to write.
Returns
number of bytes written or EOF(-1).

Reimplemented from IOStream::Device.

Reimplemented in CC3000::Driver.

Definition at line 105 of file Socket.hh.

Member Data Documentation

const uint16_t Socket::DYNAMIC_PORT = 49152
static

First dynamic, private or ephemeral port number.

Definition at line 34 of file Socket.hh.

bool IOStream::Device::m_blocking
protectedinherited

Blocking state

Definition at line 248 of file IOStream.hh.

Mode IOStream::Device::m_eol
protectedinherited

End of line mode

Definition at line 251 of file IOStream.hh.

uint16_t Socket::m_port
protected

Socket port

Definition at line 308 of file Socket.hh.

uint8_t Socket::m_proto
protected

Socket protocol if open otherwise zero(0).

Definition at line 305 of file Socket.hh.

INET::addr_t Socket::m_src
protected

Source address; MAC, IP and port.

Definition at line 302 of file Socket.hh.


The documentation for this class was generated from the following file: