Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
CC32xxSocket.cxx File Reference
#include "CC32xxSocket.hxx"
#include "CC32xxWiFi.hxx"
#include "CC32xxHelper.hxx"
#include <memory>
#include <fcntl.h>
#include <unistd.h>
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <ifaddrs.h>
#include <sys/socket.h>
#include <ti/drivers/net/wifi/simplelink.h>
#include "utils/format_utils.hxx"
#include "utils/logging.h"

Go to the source code of this file.

Macros

#define SUPPORT_SL_R1_API
 
#define CC32XX_SOCKET_RESERVED   ((CC32xxSocket *)(&reservedPtr))
 Guard value indivating a reserved socket that is currently being allocated.
 

Functions

int socket (int domain, int type, int protocol)
 Create an unbound socket in a communications domain.
 
int bind (int socket, const struct sockaddr *address, socklen_t address_len)
 Bind a name to a socket.
 
int listen (int socket, int backlog)
 Mark a connection-mode socket, specified by the socket argument, as accepting connections.
 
int accept (int socket, struct sockaddr *address, socklen_t *address_len)
 Accept a new connection on a socket.
 
int connect (int socket, const struct sockaddr *address, socklen_t address_len)
 Connect a socket.
 
ssize_t recv (int socket, void *buffer, size_t length, int flags)
 Receive a message from a connection-mode or connectionless-mode socket.
 
ssize_t send (int socket, const void *buffer, size_t length, int flags)
 Initiate transmission of a message from the specified socket.
 
int setsockopt (int socket, int level, int option_name, const void *option_value, socklen_t option_len)
 Set the socket options.
 
int getsockopt (int socket, int level, int option_name, void *option_value, socklen_t *option_len)
 Get the socket options.
 
const char * gai_strerror (int __ecode)
 see 'man gai_strerror'
 
void freeaddrinfo (struct addrinfo *ai)
 see 'man freeaddrinfo'
 
int getaddrinfo (const char *nodename, const char *servname, const struct addrinfo *hints, struct addrinfo **res)
 see 'man getaddrinfo'
 
const char * inet_ntop (int af, const void *src, char *dst, socklen_t size)
 Convert the network address in src to a character string in src.
 
int getifaddrs (struct ifaddrs **ifap)
 Create a linked list of structures describing the network interfaces of the local system.
 
void freeifaddrs (struct ifaddrs *ifa)
 Free a previously generated linked list of structures describing the network interfaces of the local system.
 

Variables

static CC32xxSocketcc32xxSockets [SL_MAX_SOCKETS]
 Existing (allocated) sockets.
 
static volatile uint8_t reservedPtr
 Dummy pointer address that can be used as a reserved indicator.
 

Detailed Description

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

This file intantiates and initilizes the CC32xx Wi-Fi.

Author
Stuart W. Baker
Date
18 March 2016

Definition in file net_cc32xx/CC32xxSocket.cxx.

Macro Definition Documentation

◆ CC32XX_SOCKET_RESERVED

#define CC32XX_SOCKET_RESERVED   ((CC32xxSocket *)(&reservedPtr))

Guard value indivating a reserved socket that is currently being allocated.

Definition at line 71 of file net_cc32xx/CC32xxSocket.cxx.

◆ SUPPORT_SL_R1_API

#define SUPPORT_SL_R1_API

Definition at line 34 of file net_cc32xx/CC32xxSocket.cxx.

Function Documentation

◆ accept()

int accept ( int  socket,
struct sockaddr address,
socklen_t address_len 
)

Accept a new connection on a socket.

Parameters
socketthe socket file descriptor
addresseither a null pointer, or a pointer to a sockaddr structure where the address of the connecting socket shall be returned
address_leneither a null pointer, if address is a null pointer, or a pointer to a socklen_t object which on input specifies the length of the supplied sockaddr structure, and on output specifies the length of the stored address
Returns
shall return the non-negative file descriptor of the accepted socket upon success, otherwise, -1 shall be returned and errno set to indicate the error

Definition at line 1007 of file net_cc32xx/CC32xxSocket.cxx.

◆ bind()

int bind ( int  socket,
const struct sockaddr address,
socklen_t  address_len 
)

Bind a name to a socket.

Parameters
socketfile descriptor of the socket to be bound
addresspoints to a sockaddr structure containing the address to be bound to the socket
address_lenspecifies the length of the sockaddr structure pointed to by the address argument
Returns
shall return on success, otherwise, -1 shall be returned and errno set to indicate the error

Definition at line 974 of file net_cc32xx/CC32xxSocket.cxx.

◆ connect()

int connect ( int  socket,
const struct sockaddr address,
socklen_t  address_len 
)

Connect a socket.

Parameters
socketthe socket file descriptor
addresspoints to a sockaddr structure containing the peer address
address_lenspecifies the length of the sockaddr structure pointed to by the address argument
Returns
shall return 0 upon success, otherwise, -1 shall be returned and errno set to indicate the error

Definition at line 1020 of file net_cc32xx/CC32xxSocket.cxx.

◆ freeaddrinfo()

void freeaddrinfo ( struct addrinfo ai)

see 'man freeaddrinfo'

Parameters
aiis the addrinfo structure, allocated by getaddrinfo, to be released.

Definition at line 1112 of file net_cc32xx/CC32xxSocket.cxx.

◆ freeifaddrs()

void freeifaddrs ( struct ifaddrs ifa)

Free a previously generated linked list of structures describing the network interfaces of the local system.

Parameters
ifapointer to the list that will be freed

Definition at line 1356 of file net_cc32xx/CC32xxSocket.cxx.

◆ gai_strerror()

const char * gai_strerror ( int  __ecode)

see 'man gai_strerror'

Parameters
__ecodeis the error code
Returns
error string (statically allocated)

Definition at line 1093 of file net_cc32xx/CC32xxSocket.cxx.

◆ getaddrinfo()

int getaddrinfo ( const char *  nodename,
const char *  servname,
const struct addrinfo hints,
struct addrinfo **  res 
)

see 'man getaddrinfo'

Parameters
nodenamewhat to look up (host name typically)
servnamelocal mDNS service name to look up
hintsnot sure
resan addrinfo strcture will be allocated into here
Returns
0 on success, -1 on error

Definition at line 1131 of file net_cc32xx/CC32xxSocket.cxx.

◆ getifaddrs()

int getifaddrs ( struct ifaddrs **  ifap)

Create a linked list of structures describing the network interfaces of the local system.

Parameters
ifapthe first item in the list is in *ifap
Returns
0 upon success, else -1 with errno set appropriately
Todo:
support netmask and broadcast
Todo:
we don't support/check flags

Definition at line 1284 of file net_cc32xx/CC32xxSocket.cxx.

◆ getsockopt()

int getsockopt ( int  socket,
int  level,
int  option_name,
void *  option_value,
socklen_t option_len 
)

Get the socket options.

Parameters
socketthe socket file descriptor
levelspecifies the protocol level at which the option resides
option_namespecifies a single option to get
option_valuethe metadata that belongs to the option_name
option_lenthe length of the metadata that belongs to the option_name
Returns
shall return 0 upon success, otherwise, -1 shall be returned and errno set to indicate the error

Definition at line 1081 of file net_cc32xx/CC32xxSocket.cxx.

◆ inet_ntop()

const char * inet_ntop ( int  af,
const void *  src,
char *  dst,
socklen_t  size 
)

Convert the network address in src to a character string in src.

Parameters
afaddress family, AF_INET or AF_INET6
srcsource address in network byte order
dstresulting address string
sizemax number characters allowed for the result
Returns
on success non-null pointer to dst, else NULL with errno set on error

Definition at line 1244 of file net_cc32xx/CC32xxSocket.cxx.

◆ listen()

int listen ( int  socket,
int  backlog 
)

Mark a connection-mode socket, specified by the socket argument, as accepting connections.

Parameters
socketthe socket file descriptor
backlogprovides a hint to the implementation which the implementation shall use to limit the number of outstanding connections in the socket's listen queue
Returns
shall return 0 upon success, otherwise, -1 shall be returned and errno set to indicate the error

Definition at line 988 of file net_cc32xx/CC32xxSocket.cxx.

◆ recv()

ssize_t recv ( int  socket,
void *  buffer,
size_t  length,
int  flags 
)

Receive a message from a connection-mode or connectionless-mode socket.

Parameters
socketthe socket file descriptor
bufferbuffer where the message should be stored
lengthlength in bytes of the buffer pointed to by the buffer argument
flagsSpecifies the type of message reception
Returns
the length of the message in bytes, if no messages are available to be received and the peer has performed an orderly shutdown, recv() shall return 0. Otherwise, -1 shall be returned and errno set to indicate the error

Definition at line 1036 of file net_cc32xx/CC32xxSocket.cxx.

◆ send()

ssize_t send ( int  socket,
const void *  buffer,
size_t  length,
int  flags 
)

Initiate transmission of a message from the specified socket.

Parameters
socketthe socket file descriptor
bufferbuffer containing the message to send
lengthlength of the message in bytes
flagsthe type of message transmission
Returns
the number of bytes sent, otherwise, -1 shall be returned and errno set to indicate the error

Definition at line 1049 of file net_cc32xx/CC32xxSocket.cxx.

◆ setsockopt()

int setsockopt ( int  socket,
int  level,
int  option_name,
const void *  option_value,
socklen_t  option_len 
)

Set the socket options.

Parameters
socketthe socket file descriptor
levelspecifies the protocol level at which the option resides
option_namespecifies a single option to set
option_valuethe metadata that belongs to the option_name
option_lenthe length of the metadata that belongs to the option_name
Returns
shall return 0 upon success, otherwise, -1 shall be returned and errno set to indicate the error

Definition at line 1064 of file net_cc32xx/CC32xxSocket.cxx.

◆ socket()

int socket ( int  domain,
int  type,
int  protocol 
)

Create an unbound socket in a communications domain.

Parameters
domainspecifies the communications domain in which a socket is to be created
typespecifies the type of socket to be created
protocolspecifies a particular protocol to be used with the socket, specifying a protocol of 0 causes socket() to use an unspecified default protocol appropriate for the requested socket type
Returns
a non-negative integer on success, the socket file descriptor, otherwise, a value of -1 shall be returned and errno set to indicate the error

Definition at line 960 of file net_cc32xx/CC32xxSocket.cxx.

Variable Documentation

◆ cc32xxSockets

CC32xxSocket* cc32xxSockets[SL_MAX_SOCKETS]
static

Existing (allocated) sockets.

Todo:
(Stuart Baker) since there is only a max of 16 sockets, would it be more memory efficient to just allocate all the memory statically as an array and perform placement construction instead of incuring the overhead of a heap allocation?
Note
(Balazs Racz) I don't think so, because most applications will only ever use one socket, maybe two.

we would also #define CC32XX_SD_UNUSED -1 and #define CC32XX_SD_RESERVED -2

Definition at line 65 of file net_cc32xx/CC32xxSocket.cxx.

◆ reservedPtr

volatile uint8_t reservedPtr
static

Dummy pointer address that can be used as a reserved indicator.

Definition at line 68 of file net_cc32xx/CC32xxSocket.cxx.