nsapi - Added standardized return types for size and errors

nsapi_error_t         - enum of errors or 0 for NSAPI_ERROR_OK
nsapi_size_t          - unsigned size of data that could be sent
nsapi_size_or_error_t - either a non-negative size or negative error
pull/3075/head
Christopher Haster 2016-10-18 14:48:46 -05:00
parent fe80efe1bb
commit ba748ac1f8
17 changed files with 216 additions and 168 deletions

View File

@ -39,8 +39,10 @@ public:
* @param apn Optional name of the network to connect to * @param apn Optional name of the network to connect to
* @param user Optional username for the APN * @param user Optional username for the APN
* @param pass Optional password fot the APN * @param pass Optional password fot the APN
* @return 0 on success, negative error code on failure
*/ */
virtual int set_credentials(const char *apn, const char *user = 0, const char *pass = 0) = 0; virtual nsapi_error_t set_credentials(const char *apn,
const char *username = 0, const char *password = 0) = 0;
/** Start the interface /** Start the interface
* *
@ -49,7 +51,8 @@ public:
* @param password Optional password for your APN * @param password Optional password for your APN
* @return 0 on success, negative error code on failure * @return 0 on success, negative error code on failure
*/ */
virtual int connect(const char *apn, const char *username = 0, const char *password = 0) = 0; virtual nsapi_error_t connect(const char *apn,
const char *username = 0, const char *password = 0) = 0;
/** Start the interface /** Start the interface
* *
@ -57,13 +60,13 @@ public:
* *
* @return 0 on success, negative error code on failure * @return 0 on success, negative error code on failure
*/ */
virtual int connect() = 0; virtual nsapi_error_t connect() = 0;
/** Stop the interface /** Stop the interface
* *
* @return 0 on success, negative error code on failure * @return 0 on success, negative error code on failure
*/ */
virtual int disconnect() = 0; virtual nsapi_error_t disconnect() = 0;
}; };

View File

@ -40,27 +40,27 @@ const char *NetworkInterface::get_gateway()
return 0; return 0;
} }
int NetworkInterface::set_network(const char *ip_address, const char *netmask, const char *gateway) nsapi_error_t NetworkInterface::set_network(const char *ip_address, const char *netmask, const char *gateway)
{ {
return NSAPI_ERROR_UNSUPPORTED; return NSAPI_ERROR_UNSUPPORTED;
} }
int NetworkInterface::set_dhcp(bool dhcp) nsapi_error_t NetworkInterface::set_dhcp(bool dhcp)
{ {
if (!dhcp) { if (!dhcp) {
return NSAPI_ERROR_UNSUPPORTED; return NSAPI_ERROR_UNSUPPORTED;
} else { } else {
return 0; return NSAPI_ERROR_OK;
} }
} }
// DNS operations go through the underlying stack by default // DNS operations go through the underlying stack by default
int NetworkInterface::gethostbyname(const char *name, SocketAddress *address, nsapi_version_t version) nsapi_error_t NetworkInterface::gethostbyname(const char *name, SocketAddress *address, nsapi_version_t version)
{ {
return get_stack()->gethostbyname(name, address, version); return get_stack()->gethostbyname(name, address, version);
} }
int NetworkInterface::add_dns_server(const SocketAddress &address) nsapi_error_t NetworkInterface::add_dns_server(const SocketAddress &address)
{ {
return get_stack()->add_dns_server(address); return get_stack()->add_dns_server(address);
} }

View File

@ -78,7 +78,8 @@ public:
* @param gateway Null-terminated representation of the local gateway * @param gateway Null-terminated representation of the local gateway
* @return 0 on success, negative error code on failure * @return 0 on success, negative error code on failure
*/ */
virtual int set_network(const char *ip_address, const char *netmask, const char *gateway); virtual nsapi_error_t set_network(
const char *ip_address, const char *netmask, const char *gateway);
/** Enable or disable DHCP on the network /** Enable or disable DHCP on the network
* *
@ -89,19 +90,19 @@ public:
* @param dhcp True to enable DHCP * @param dhcp True to enable DHCP
* @return 0 on success, negative error code on failure * @return 0 on success, negative error code on failure
*/ */
virtual int set_dhcp(bool dhcp); virtual nsapi_error_t set_dhcp(bool dhcp);
/** Start the interface /** Start the interface
* *
* @return 0 on success, negative error code on failure * @return 0 on success, negative error code on failure
*/ */
virtual int connect() = 0; virtual nsapi_error_t connect() = 0;
/** Stop the interface /** Stop the interface
* *
* @return 0 on success, negative error code on failure * @return 0 on success, negative error code on failure
*/ */
virtual int disconnect() = 0; virtual nsapi_error_t disconnect() = 0;
/** Translates a hostname to an IP address with specific version /** Translates a hostname to an IP address with specific version
* *
@ -117,14 +118,15 @@ public:
* version is chosen by the stack (defaults to NSAPI_UNSPEC) * version is chosen by the stack (defaults to NSAPI_UNSPEC)
* @return 0 on success, negative error code on failure * @return 0 on success, negative error code on failure
*/ */
virtual int gethostbyname(const char *host, SocketAddress *address, nsapi_version_t version = NSAPI_UNSPEC); virtual nsapi_error_t gethostbyname(const char *host,
SocketAddress *address, nsapi_version_t version = NSAPI_UNSPEC);
/** Add a domain name server to list of servers to query /** Add a domain name server to list of servers to query
* *
* @param addr Destination for the host address * @param addr Destination for the host address
* @return 0 on success, negative error code on failure * @return 0 on success, negative error code on failure
*/ */
virtual int add_dns_server(const SocketAddress &address); virtual nsapi_error_t add_dns_server(const SocketAddress &address);
protected: protected:
friend class Socket; friend class Socket;

View File

@ -22,7 +22,7 @@
// Default NetworkStack operations // Default NetworkStack operations
int NetworkStack::gethostbyname(const char *name, SocketAddress *address, nsapi_version_t version) nsapi_error_t NetworkStack::gethostbyname(const char *name, SocketAddress *address, nsapi_version_t version)
{ {
// check for simple ip addresses // check for simple ip addresses
if (address->set_ip_address(name)) { if (address->set_ip_address(name)) {
@ -30,7 +30,7 @@ int NetworkStack::gethostbyname(const char *name, SocketAddress *address, nsapi_
return NSAPI_ERROR_DNS_FAILURE; return NSAPI_ERROR_DNS_FAILURE;
} }
return 0; return NSAPI_ERROR_OK;
} }
// if the version is unspecified, try to guess the version from the // if the version is unspecified, try to guess the version from the
@ -45,27 +45,27 @@ int NetworkStack::gethostbyname(const char *name, SocketAddress *address, nsapi_
return nsapi_dns_query(this, name, address, version); return nsapi_dns_query(this, name, address, version);
} }
int NetworkStack::add_dns_server(const SocketAddress &address) nsapi_error_t NetworkStack::add_dns_server(const SocketAddress &address)
{ {
return nsapi_dns_add_server(address); return nsapi_dns_add_server(address);
} }
int NetworkStack::setstackopt(int level, int optname, const void *optval, unsigned optlen) nsapi_error_t NetworkStack::setstackopt(int level, int optname, const void *optval, unsigned optlen)
{ {
return NSAPI_ERROR_UNSUPPORTED; return NSAPI_ERROR_UNSUPPORTED;
} }
int NetworkStack::getstackopt(int level, int optname, void *optval, unsigned *optlen) nsapi_error_t NetworkStack::getstackopt(int level, int optname, void *optval, unsigned *optlen)
{ {
return NSAPI_ERROR_UNSUPPORTED; return NSAPI_ERROR_UNSUPPORTED;
} }
int NetworkStack::setsockopt(void *handle, int level, int optname, const void *optval, unsigned optlen) nsapi_error_t NetworkStack::setsockopt(void *handle, int level, int optname, const void *optval, unsigned optlen)
{ {
return NSAPI_ERROR_UNSUPPORTED; return NSAPI_ERROR_UNSUPPORTED;
} }
int NetworkStack::getsockopt(void *handle, int level, int optname, void *optval, unsigned *optlen) nsapi_error_t NetworkStack::getsockopt(void *handle, int level, int optname, void *optval, unsigned *optlen)
{ {
return NSAPI_ERROR_UNSUPPORTED; return NSAPI_ERROR_UNSUPPORTED;
} }
@ -99,19 +99,19 @@ public:
return address->get_ip_address(); return address->get_ip_address();
} }
virtual int gethostbyname(const char *name, SocketAddress *address, nsapi_version_t version) virtual nsapi_error_t gethostbyname(const char *name, SocketAddress *address, nsapi_version_t version)
{ {
if (!_stack_api()->gethostbyname) { if (!_stack_api()->gethostbyname) {
return NetworkStack::gethostbyname(name, address, version); return NetworkStack::gethostbyname(name, address, version);
} }
nsapi_addr_t addr = {NSAPI_UNSPEC, 0}; nsapi_addr_t addr = {NSAPI_UNSPEC, 0};
int err = _stack_api()->gethostbyname(_stack(), name, &addr, version); nsapi_error_t err = _stack_api()->gethostbyname(_stack(), name, &addr, version);
address->set_addr(addr); address->set_addr(addr);
return err; return err;
} }
virtual int add_dns_server(const SocketAddress &address) virtual nsapi_error_t add_dns_server(const SocketAddress &address)
{ {
if (!_stack_api()->add_dns_server) { if (!_stack_api()->add_dns_server) {
return NetworkStack::add_dns_server(address); return NetworkStack::add_dns_server(address);
@ -120,7 +120,7 @@ public:
return _stack_api()->add_dns_server(_stack(), address.get_addr()); return _stack_api()->add_dns_server(_stack(), address.get_addr());
} }
virtual int setstackopt(int level, int optname, const void *optval, unsigned optlen) virtual nsapi_error_t setstackopt(int level, int optname, const void *optval, unsigned optlen)
{ {
if (!_stack_api()->setstackopt) { if (!_stack_api()->setstackopt) {
return NSAPI_ERROR_UNSUPPORTED; return NSAPI_ERROR_UNSUPPORTED;
@ -129,7 +129,7 @@ public:
return _stack_api()->setstackopt(_stack(), level, optname, optval, optlen); return _stack_api()->setstackopt(_stack(), level, optname, optval, optlen);
} }
virtual int getstackopt(int level, int optname, void *optval, unsigned *optlen) virtual nsapi_error_t getstackopt(int level, int optname, void *optval, unsigned *optlen)
{ {
if (!_stack_api()->getstackopt) { if (!_stack_api()->getstackopt) {
return NSAPI_ERROR_UNSUPPORTED; return NSAPI_ERROR_UNSUPPORTED;
@ -139,7 +139,7 @@ public:
} }
protected: protected:
virtual int socket_open(nsapi_socket_t *socket, nsapi_protocol_t proto) virtual nsapi_error_t socket_open(nsapi_socket_t *socket, nsapi_protocol_t proto)
{ {
if (!_stack_api()->socket_open) { if (!_stack_api()->socket_open) {
return NSAPI_ERROR_UNSUPPORTED; return NSAPI_ERROR_UNSUPPORTED;
@ -148,7 +148,7 @@ protected:
return _stack_api()->socket_open(_stack(), socket, proto); return _stack_api()->socket_open(_stack(), socket, proto);
} }
virtual int socket_close(nsapi_socket_t socket) virtual nsapi_error_t socket_close(nsapi_socket_t socket)
{ {
if (!_stack_api()->socket_close) { if (!_stack_api()->socket_close) {
return NSAPI_ERROR_UNSUPPORTED; return NSAPI_ERROR_UNSUPPORTED;
@ -157,7 +157,7 @@ protected:
return _stack_api()->socket_close(_stack(), socket); return _stack_api()->socket_close(_stack(), socket);
} }
virtual int socket_bind(nsapi_socket_t socket, const SocketAddress &address) virtual nsapi_error_t socket_bind(nsapi_socket_t socket, const SocketAddress &address)
{ {
if (!_stack_api()->socket_bind) { if (!_stack_api()->socket_bind) {
return NSAPI_ERROR_UNSUPPORTED; return NSAPI_ERROR_UNSUPPORTED;
@ -166,7 +166,7 @@ protected:
return _stack_api()->socket_bind(_stack(), socket, address.get_addr(), address.get_port()); return _stack_api()->socket_bind(_stack(), socket, address.get_addr(), address.get_port());
} }
virtual int socket_listen(nsapi_socket_t socket, int backlog) virtual nsapi_error_t socket_listen(nsapi_socket_t socket, int backlog)
{ {
if (!_stack_api()->socket_listen) { if (!_stack_api()->socket_listen) {
return NSAPI_ERROR_UNSUPPORTED; return NSAPI_ERROR_UNSUPPORTED;
@ -175,7 +175,7 @@ protected:
return _stack_api()->socket_listen(_stack(), socket, backlog); return _stack_api()->socket_listen(_stack(), socket, backlog);
} }
virtual int socket_connect(nsapi_socket_t socket, const SocketAddress &address) virtual nsapi_error_t socket_connect(nsapi_socket_t socket, const SocketAddress &address)
{ {
if (!_stack_api()->socket_connect) { if (!_stack_api()->socket_connect) {
return NSAPI_ERROR_UNSUPPORTED; return NSAPI_ERROR_UNSUPPORTED;
@ -184,7 +184,7 @@ protected:
return _stack_api()->socket_connect(_stack(), socket, address.get_addr(), address.get_port()); return _stack_api()->socket_connect(_stack(), socket, address.get_addr(), address.get_port());
} }
virtual int socket_accept(nsapi_socket_t server, nsapi_socket_t *socket, SocketAddress *address) virtual nsapi_error_t socket_accept(nsapi_socket_t server, nsapi_socket_t *socket, SocketAddress *address)
{ {
if (!_stack_api()->socket_accept) { if (!_stack_api()->socket_accept) {
return NSAPI_ERROR_UNSUPPORTED; return NSAPI_ERROR_UNSUPPORTED;
@ -193,7 +193,7 @@ protected:
nsapi_addr_t addr = {NSAPI_IPv4, 0}; nsapi_addr_t addr = {NSAPI_IPv4, 0};
uint16_t port = 0; uint16_t port = 0;
int err = _stack_api()->socket_accept(_stack(), server, socket, &addr, &port); nsapi_error_t err = _stack_api()->socket_accept(_stack(), server, socket, &addr, &port);
if (address) { if (address) {
address->set_addr(addr); address->set_addr(addr);
@ -203,7 +203,7 @@ protected:
return err; return err;
} }
virtual int socket_send(nsapi_socket_t socket, const void *data, unsigned size) virtual nsapi_size_or_error_t socket_send(nsapi_socket_t socket, const void *data, nsapi_size_t size)
{ {
if (!_stack_api()->socket_send) { if (!_stack_api()->socket_send) {
return NSAPI_ERROR_UNSUPPORTED; return NSAPI_ERROR_UNSUPPORTED;
@ -212,7 +212,7 @@ protected:
return _stack_api()->socket_send(_stack(), socket, data, size); return _stack_api()->socket_send(_stack(), socket, data, size);
} }
virtual int socket_recv(nsapi_socket_t socket, void *data, unsigned size) virtual nsapi_size_or_error_t socket_recv(nsapi_socket_t socket, void *data, nsapi_size_t size)
{ {
if (!_stack_api()->socket_recv) { if (!_stack_api()->socket_recv) {
return NSAPI_ERROR_UNSUPPORTED; return NSAPI_ERROR_UNSUPPORTED;
@ -221,7 +221,7 @@ protected:
return _stack_api()->socket_recv(_stack(), socket, data, size); return _stack_api()->socket_recv(_stack(), socket, data, size);
} }
virtual int socket_sendto(nsapi_socket_t socket, const SocketAddress &address, const void *data, unsigned size) virtual nsapi_size_or_error_t socket_sendto(nsapi_socket_t socket, const SocketAddress &address, const void *data, nsapi_size_t size)
{ {
if (!_stack_api()->socket_sendto) { if (!_stack_api()->socket_sendto) {
return NSAPI_ERROR_UNSUPPORTED; return NSAPI_ERROR_UNSUPPORTED;
@ -230,7 +230,7 @@ protected:
return _stack_api()->socket_sendto(_stack(), socket, address.get_addr(), address.get_port(), data, size); return _stack_api()->socket_sendto(_stack(), socket, address.get_addr(), address.get_port(), data, size);
} }
virtual int socket_recvfrom(nsapi_socket_t socket, SocketAddress *address, void *data, unsigned size) virtual nsapi_size_or_error_t socket_recvfrom(nsapi_socket_t socket, SocketAddress *address, void *data, nsapi_size_t size)
{ {
if (!_stack_api()->socket_recvfrom) { if (!_stack_api()->socket_recvfrom) {
return NSAPI_ERROR_UNSUPPORTED; return NSAPI_ERROR_UNSUPPORTED;
@ -239,7 +239,7 @@ protected:
nsapi_addr_t addr = {NSAPI_IPv4, 0}; nsapi_addr_t addr = {NSAPI_IPv4, 0};
uint16_t port = 0; uint16_t port = 0;
int err = _stack_api()->socket_recvfrom(_stack(), socket, &addr, &port, data, size); nsapi_size_or_error_t err = _stack_api()->socket_recvfrom(_stack(), socket, &addr, &port, data, size);
if (address) { if (address) {
address->set_addr(addr); address->set_addr(addr);
@ -258,7 +258,7 @@ protected:
return _stack_api()->socket_attach(_stack(), socket, callback, data); return _stack_api()->socket_attach(_stack(), socket, callback, data);
} }
virtual int setsockopt(nsapi_socket_t socket, int level, int optname, const void *optval, unsigned optlen) virtual nsapi_error_t setsockopt(nsapi_socket_t socket, int level, int optname, const void *optval, unsigned optlen)
{ {
if (!_stack_api()->setsockopt) { if (!_stack_api()->setsockopt) {
return NSAPI_ERROR_UNSUPPORTED; return NSAPI_ERROR_UNSUPPORTED;
@ -267,7 +267,7 @@ protected:
return _stack_api()->setsockopt(_stack(), socket, level, optname, optval, optlen); return _stack_api()->setsockopt(_stack(), socket, level, optname, optval, optlen);
} }
virtual int getsockopt(nsapi_socket_t socket, int level, int optname, void *optval, unsigned *optlen) virtual nsapi_error_t getsockopt(nsapi_socket_t socket, int level, int optname, void *optval, unsigned *optlen)
{ {
if (!_stack_api()->getsockopt) { if (!_stack_api()->getsockopt) {
return NSAPI_ERROR_UNSUPPORTED; return NSAPI_ERROR_UNSUPPORTED;

View File

@ -58,14 +58,15 @@ public:
* version is chosen by the stack (defaults to NSAPI_UNSPEC) * version is chosen by the stack (defaults to NSAPI_UNSPEC)
* @return 0 on success, negative error code on failure * @return 0 on success, negative error code on failure
*/ */
virtual int gethostbyname(const char *host, SocketAddress *address, nsapi_version_t version = NSAPI_UNSPEC); virtual nsapi_error_t gethostbyname(const char *host,
SocketAddress *address, nsapi_version_t version = NSAPI_UNSPEC);
/** Add a domain name server to list of servers to query /** Add a domain name server to list of servers to query
* *
* @param addr Destination for the host address * @param addr Destination for the host address
* @return 0 on success, negative error code on failure * @return 0 on success, negative error code on failure
*/ */
virtual int add_dns_server(const SocketAddress &address); virtual nsapi_error_t add_dns_server(const SocketAddress &address);
/* Set stack-specific stack options /* Set stack-specific stack options
* *
@ -79,7 +80,7 @@ public:
* @param optlen Length of the option value * @param optlen Length of the option value
* @return 0 on success, negative error code on failure * @return 0 on success, negative error code on failure
*/ */
virtual int setstackopt(int level, int optname, const void *optval, unsigned optlen); virtual nsapi_error_t setstackopt(int level, int optname, const void *optval, unsigned optlen);
/* Get stack-specific stack options /* Get stack-specific stack options
* *
@ -93,7 +94,7 @@ public:
* @param optlen Length of the option value * @param optlen Length of the option value
* @return 0 on success, negative error code on failure * @return 0 on success, negative error code on failure
*/ */
virtual int getstackopt(int level, int optname, void *optval, unsigned *optlen); virtual nsapi_error_t getstackopt(int level, int optname, void *optval, unsigned *optlen);
protected: protected:
friend class Socket; friend class Socket;
@ -113,7 +114,7 @@ protected:
* @param proto Protocol of socket to open, NSAPI_TCP or NSAPI_UDP * @param proto Protocol of socket to open, NSAPI_TCP or NSAPI_UDP
* @return 0 on success, negative error code on failure * @return 0 on success, negative error code on failure
*/ */
virtual int socket_open(nsapi_socket_t *handle, nsapi_protocol_t proto) = 0; virtual nsapi_error_t socket_open(nsapi_socket_t *handle, nsapi_protocol_t proto) = 0;
/** Close the socket /** Close the socket
* *
@ -123,7 +124,7 @@ protected:
* @param handle Socket handle * @param handle Socket handle
* @return 0 on success, negative error code on failure * @return 0 on success, negative error code on failure
*/ */
virtual int socket_close(nsapi_socket_t handle) = 0; virtual nsapi_error_t socket_close(nsapi_socket_t handle) = 0;
/** Bind a specific address to a socket /** Bind a specific address to a socket
* *
@ -134,7 +135,7 @@ protected:
* @param address Local address to bind * @param address Local address to bind
* @return 0 on success, negative error code on failure. * @return 0 on success, negative error code on failure.
*/ */
virtual int socket_bind(nsapi_socket_t handle, const SocketAddress &address) = 0; virtual nsapi_error_t socket_bind(nsapi_socket_t handle, const SocketAddress &address) = 0;
/** Listen for connections on a TCP socket /** Listen for connections on a TCP socket
* *
@ -146,7 +147,7 @@ protected:
* simultaneously * simultaneously
* @return 0 on success, negative error code on failure * @return 0 on success, negative error code on failure
*/ */
virtual int socket_listen(nsapi_socket_t handle, int backlog) = 0; virtual nsapi_error_t socket_listen(nsapi_socket_t handle, int backlog) = 0;
/** Connects TCP socket to a remote host /** Connects TCP socket to a remote host
* *
@ -157,7 +158,7 @@ protected:
* @param address The SocketAddress of the remote host * @param address The SocketAddress of the remote host
* @return 0 on success, negative error code on failure * @return 0 on success, negative error code on failure
*/ */
virtual int socket_connect(nsapi_socket_t handle, const SocketAddress &address) = 0; virtual nsapi_error_t socket_connect(nsapi_socket_t handle, const SocketAddress &address) = 0;
/** Accepts a connection on a TCP socket /** Accepts a connection on a TCP socket
* *
@ -177,7 +178,8 @@ protected:
* @param address Destination for the remote address or NULL * @param address Destination for the remote address or NULL
* @return 0 on success, negative error code on failure * @return 0 on success, negative error code on failure
*/ */
virtual int socket_accept(nsapi_socket_t server, nsapi_socket_t *handle, SocketAddress *address=0) = 0; virtual nsapi_error_t socket_accept(nsapi_socket_t server,
nsapi_socket_t *handle, SocketAddress *address=0) = 0;
/** Send data over a TCP socket /** Send data over a TCP socket
* *
@ -193,7 +195,8 @@ protected:
* @return Number of sent bytes on success, negative error * @return Number of sent bytes on success, negative error
* code on failure * code on failure
*/ */
virtual int socket_send(nsapi_socket_t handle, const void *data, unsigned size) = 0; virtual nsapi_size_or_error_t socket_send(nsapi_socket_t handle,
const void *data, nsapi_size_t size) = 0;
/** Receive data over a TCP socket /** Receive data over a TCP socket
* *
@ -209,7 +212,8 @@ protected:
* @return Number of received bytes on success, negative error * @return Number of received bytes on success, negative error
* code on failure * code on failure
*/ */
virtual int socket_recv(nsapi_socket_t handle, void *data, unsigned size) = 0; virtual nsapi_size_or_error_t socket_recv(nsapi_socket_t handle,
void *data, nsapi_size_t size) = 0;
/** Send a packet over a UDP socket /** Send a packet over a UDP socket
* *
@ -226,7 +230,8 @@ protected:
* @return Number of sent bytes on success, negative error * @return Number of sent bytes on success, negative error
* code on failure * code on failure
*/ */
virtual int socket_sendto(nsapi_socket_t handle, const SocketAddress &address, const void *data, unsigned size) = 0; virtual nsapi_size_or_error_t socket_sendto(nsapi_socket_t handle, const SocketAddress &address,
const void *data, nsapi_size_t size) = 0;
/** Receive a packet over a UDP socket /** Receive a packet over a UDP socket
* *
@ -243,7 +248,8 @@ protected:
* @return Number of received bytes on success, negative error * @return Number of received bytes on success, negative error
* code on failure * code on failure
*/ */
virtual int socket_recvfrom(nsapi_socket_t handle, SocketAddress *address, void *buffer, unsigned size) = 0; virtual nsapi_size_or_error_t socket_recvfrom(nsapi_socket_t handle, SocketAddress *address,
void *buffer, nsapi_size_t size) = 0;
/** Register a callback on state change of the socket /** Register a callback on state change of the socket
* *
@ -273,7 +279,8 @@ protected:
* @param optlen Length of the option value * @param optlen Length of the option value
* @return 0 on success, negative error code on failure * @return 0 on success, negative error code on failure
*/ */
virtual int setsockopt(nsapi_socket_t handle, int level, int optname, const void *optval, unsigned optlen); virtual nsapi_error_t setsockopt(nsapi_socket_t handle, int level,
int optname, const void *optval, unsigned optlen);
/* Get stack-specific socket options /* Get stack-specific socket options
* *
@ -288,7 +295,8 @@ protected:
* @param optlen Length of the option value * @param optlen Length of the option value
* @return 0 on success, negative error code on failure * @return 0 on success, negative error code on failure
*/ */
virtual int getsockopt(nsapi_socket_t handle, int level, int optname, void *optval, unsigned *optlen); virtual nsapi_error_t getsockopt(nsapi_socket_t handle, int level,
int optname, void *optval, unsigned *optlen);
}; };

View File

@ -24,7 +24,7 @@ Socket::Socket()
{ {
} }
int Socket::open(NetworkStack *stack) nsapi_error_t Socket::open(NetworkStack *stack)
{ {
_lock.lock(); _lock.lock();
@ -35,7 +35,7 @@ int Socket::open(NetworkStack *stack)
_stack = stack; _stack = stack;
nsapi_socket_t socket; nsapi_socket_t socket;
int err = _stack->socket_open(&socket, get_proto()); nsapi_error_t err = _stack->socket_open(&socket, get_proto());
if (err) { if (err) {
_lock.unlock(); _lock.unlock();
return err; return err;
@ -46,14 +46,14 @@ int Socket::open(NetworkStack *stack)
_stack->socket_attach(_socket, Callback<void()>::thunk, &_event); _stack->socket_attach(_socket, Callback<void()>::thunk, &_event);
_lock.unlock(); _lock.unlock();
return 0; return NSAPI_ERROR_OK;
} }
int Socket::close() nsapi_error_t Socket::close()
{ {
_lock.lock(); _lock.lock();
int ret = 0; nsapi_error_t ret = NSAPI_ERROR_OK;
if (_socket) { if (_socket) {
_stack->socket_attach(_socket, 0, 0); _stack->socket_attach(_socket, 0, 0);
nsapi_socket_t socket = _socket; nsapi_socket_t socket = _socket;
@ -69,24 +69,24 @@ int Socket::close()
return ret; return ret;
} }
int Socket::bind(uint16_t port) nsapi_error_t Socket::bind(uint16_t port)
{ {
// Underlying bind is thread safe // Underlying bind is thread safe
SocketAddress addr(0, port); SocketAddress addr(0, port);
return bind(addr); return bind(addr);
} }
int Socket::bind(const char *address, uint16_t port) nsapi_error_t Socket::bind(const char *address, uint16_t port)
{ {
// Underlying bind is thread safe // Underlying bind is thread safe
SocketAddress addr(address, port); SocketAddress addr(address, port);
return bind(addr); return bind(addr);
} }
int Socket::bind(const SocketAddress &address) nsapi_error_t Socket::bind(const SocketAddress &address)
{ {
_lock.lock(); _lock.lock();
int ret; nsapi_error_t ret;
if (!_socket) { if (!_socket) {
ret = NSAPI_ERROR_NO_SOCKET; ret = NSAPI_ERROR_NO_SOCKET;
@ -117,10 +117,10 @@ void Socket::set_timeout(int timeout)
_lock.unlock(); _lock.unlock();
} }
int Socket::setsockopt(int level, int optname, const void *optval, unsigned optlen) nsapi_error_t Socket::setsockopt(int level, int optname, const void *optval, unsigned optlen)
{ {
_lock.lock(); _lock.lock();
int ret; nsapi_error_t ret;
if (!_socket) { if (!_socket) {
ret = NSAPI_ERROR_NO_SOCKET; ret = NSAPI_ERROR_NO_SOCKET;
@ -132,10 +132,10 @@ int Socket::setsockopt(int level, int optname, const void *optval, unsigned optl
return ret; return ret;
} }
int Socket::getsockopt(int level, int optname, void *optval, unsigned *optlen) nsapi_error_t Socket::getsockopt(int level, int optname, void *optval, unsigned *optlen)
{ {
_lock.lock(); _lock.lock();
int ret; nsapi_error_t ret;
if (!_socket) { if (!_socket) {
ret = NSAPI_ERROR_NO_SOCKET; ret = NSAPI_ERROR_NO_SOCKET;

View File

@ -46,10 +46,10 @@ public:
* @param stack Network stack as target for socket * @param stack Network stack as target for socket
* @return 0 on success, negative error code on failure * @return 0 on success, negative error code on failure
*/ */
int open(NetworkStack *stack); nsapi_error_t open(NetworkStack *stack);
template <typename S> template <typename S>
int open(S *stack) { nsapi_error_t open(S *stack) {
return open(nsapi_create_stack(stack)); return open(nsapi_create_stack(stack));
} }
@ -60,7 +60,7 @@ public:
* *
* @return 0 on success, negative error code on failure * @return 0 on success, negative error code on failure
*/ */
int close(); nsapi_error_t close();
/** Bind a specific address to a socket /** Bind a specific address to a socket
* *
@ -70,7 +70,7 @@ public:
* @param port Local port to bind * @param port Local port to bind
* @return 0 on success, negative error code on failure. * @return 0 on success, negative error code on failure.
*/ */
int bind(uint16_t port); nsapi_error_t bind(uint16_t port);
/** Bind a specific address to a socket /** Bind a specific address to a socket
* *
@ -81,7 +81,7 @@ public:
* @param port Local port to bind * @param port Local port to bind
* @return 0 on success, negative error code on failure. * @return 0 on success, negative error code on failure.
*/ */
int bind(const char *address, uint16_t port); nsapi_error_t bind(const char *address, uint16_t port);
/** Bind a specific address to a socket /** Bind a specific address to a socket
* *
@ -91,7 +91,7 @@ public:
* @param address Local address to bind * @param address Local address to bind
* @return 0 on success, negative error code on failure. * @return 0 on success, negative error code on failure.
*/ */
int bind(const SocketAddress &address); nsapi_error_t bind(const SocketAddress &address);
/** Set blocking or non-blocking mode of the socket /** Set blocking or non-blocking mode of the socket
* *
@ -132,7 +132,7 @@ public:
* @param optlen Length of the option value * @param optlen Length of the option value
* @return 0 on success, negative error code on failure * @return 0 on success, negative error code on failure
*/ */
int setsockopt(int level, int optname, const void *optval, unsigned optlen); nsapi_error_t setsockopt(int level, int optname, const void *optval, unsigned optlen);
/* Get stack-specific socket options /* Get stack-specific socket options
* *
@ -146,7 +146,7 @@ public:
* @param optlen Length of the option value * @param optlen Length of the option value
* @return 0 on success, negative error code on failure * @return 0 on success, negative error code on failure
*/ */
int getsockopt(int level, int optname, void *optval, unsigned *optlen); nsapi_error_t getsockopt(int level, int optname, void *optval, unsigned *optlen);
/** Register a callback on state change of the socket /** Register a callback on state change of the socket
* *

View File

@ -32,10 +32,10 @@ nsapi_protocol_t TCPServer::get_proto()
return NSAPI_TCP; return NSAPI_TCP;
} }
int TCPServer::listen(int backlog) nsapi_error_t TCPServer::listen(int backlog)
{ {
_lock.lock(); _lock.lock();
int ret; nsapi_error_t ret;
if (!_socket) { if (!_socket) {
ret = NSAPI_ERROR_NO_SOCKET; ret = NSAPI_ERROR_NO_SOCKET;
@ -47,10 +47,10 @@ int TCPServer::listen(int backlog)
return ret; return ret;
} }
int TCPServer::accept(TCPSocket *connection, SocketAddress *address) nsapi_error_t TCPServer::accept(TCPSocket *connection, SocketAddress *address)
{ {
_lock.lock(); _lock.lock();
int ret; nsapi_error_t ret;
while (true) { while (true) {
if (!_socket) { if (!_socket) {

View File

@ -66,7 +66,7 @@ public:
* simultaneously, defaults to 1 * simultaneously, defaults to 1
* @return 0 on success, negative error code on failure * @return 0 on success, negative error code on failure
*/ */
int listen(int backlog = 1); nsapi_error_t listen(int backlog = 1);
/** Accepts a connection on a TCP socket /** Accepts a connection on a TCP socket
* *
@ -82,7 +82,7 @@ public:
* @param address Destination for the remote address or NULL * @param address Destination for the remote address or NULL
* @return 0 on success, negative error code on failure * @return 0 on success, negative error code on failure
*/ */
int accept(TCPSocket *connection, SocketAddress *address = NULL); nsapi_error_t accept(TCPSocket *connection, SocketAddress *address = NULL);
protected: protected:
virtual nsapi_protocol_t get_proto(); virtual nsapi_protocol_t get_proto();

View File

@ -34,10 +34,10 @@ nsapi_protocol_t TCPSocket::get_proto()
return NSAPI_TCP; return NSAPI_TCP;
} }
int TCPSocket::connect(const SocketAddress &address) nsapi_error_t TCPSocket::connect(const SocketAddress &address)
{ {
_lock.lock(); _lock.lock();
int ret; nsapi_error_t ret;
if (!_socket) { if (!_socket) {
ret = NSAPI_ERROR_NO_SOCKET; ret = NSAPI_ERROR_NO_SOCKET;
@ -49,10 +49,10 @@ int TCPSocket::connect(const SocketAddress &address)
return ret; return ret;
} }
int TCPSocket::connect(const char *host, uint16_t port) nsapi_error_t TCPSocket::connect(const char *host, uint16_t port)
{ {
SocketAddress address; SocketAddress address;
int err = _stack->gethostbyname(host, &address); nsapi_error_t err = _stack->gethostbyname(host, &address);
if (err) { if (err) {
return NSAPI_ERROR_DNS_FAILURE; return NSAPI_ERROR_DNS_FAILURE;
} }
@ -63,10 +63,10 @@ int TCPSocket::connect(const char *host, uint16_t port)
return connect(address); return connect(address);
} }
int TCPSocket::send(const void *data, unsigned size) nsapi_size_or_error_t TCPSocket::send(const void *data, nsapi_size_t size)
{ {
_lock.lock(); _lock.lock();
int ret; nsapi_size_or_error_t ret;
// If this assert is hit then there are two threads // If this assert is hit then there are two threads
// performing a send at the same time which is undefined // performing a send at the same time which is undefined
@ -81,7 +81,7 @@ int TCPSocket::send(const void *data, unsigned size)
} }
_pending = 0; _pending = 0;
int sent = _stack->socket_send(_socket, data, size); nsapi_size_or_error_t sent = _stack->socket_send(_socket, data, size);
if ((0 == _timeout) || (NSAPI_ERROR_WOULD_BLOCK != sent)) { if ((0 == _timeout) || (NSAPI_ERROR_WOULD_BLOCK != sent)) {
ret = sent; ret = sent;
break; break;
@ -107,10 +107,10 @@ int TCPSocket::send(const void *data, unsigned size)
return ret; return ret;
} }
int TCPSocket::recv(void *data, unsigned size) nsapi_size_or_error_t TCPSocket::recv(void *data, nsapi_size_t size)
{ {
_lock.lock(); _lock.lock();
int ret; nsapi_size_or_error_t ret;
// If this assert is hit then there are two threads // If this assert is hit then there are two threads
// performing a recv at the same time which is undefined // performing a recv at the same time which is undefined
@ -125,7 +125,7 @@ int TCPSocket::recv(void *data, unsigned size)
} }
_pending = 0; _pending = 0;
int recv = _stack->socket_recv(_socket, data, size); nsapi_size_or_error_t recv = _stack->socket_recv(_socket, data, size);
if ((0 == _timeout) || (NSAPI_ERROR_WOULD_BLOCK != recv)) { if ((0 == _timeout) || (NSAPI_ERROR_WOULD_BLOCK != recv)) {
ret = recv; ret = recv;
break; break;

View File

@ -66,7 +66,7 @@ public:
* @param port Port of the remote host * @param port Port of the remote host
* @return 0 on success, negative error code on failure * @return 0 on success, negative error code on failure
*/ */
int connect(const char *host, uint16_t port); nsapi_error_t connect(const char *host, uint16_t port);
/** Connects TCP socket to a remote host /** Connects TCP socket to a remote host
* *
@ -76,7 +76,7 @@ public:
* @param address The SocketAddress of the remote host * @param address The SocketAddress of the remote host
* @return 0 on success, negative error code on failure * @return 0 on success, negative error code on failure
*/ */
int connect(const SocketAddress &address); nsapi_error_t connect(const SocketAddress &address);
/** Send data over a TCP socket /** Send data over a TCP socket
* *
@ -92,7 +92,7 @@ public:
* @return Number of sent bytes on success, negative error * @return Number of sent bytes on success, negative error
* code on failure * code on failure
*/ */
int send(const void *data, unsigned size); nsapi_size_or_error_t send(const void *data, nsapi_size_t size);
/** Receive data over a TCP socket /** Receive data over a TCP socket
* *
@ -108,7 +108,7 @@ public:
* @return Number of received bytes on success, negative error * @return Number of received bytes on success, negative error
* code on failure * code on failure
*/ */
int recv(void *data, unsigned size); nsapi_size_or_error_t recv(void *data, nsapi_size_t size);
protected: protected:
friend class TCPServer; friend class TCPServer;

View File

@ -34,10 +34,10 @@ nsapi_protocol_t UDPSocket::get_proto()
return NSAPI_UDP; return NSAPI_UDP;
} }
int UDPSocket::sendto(const char *host, uint16_t port, const void *data, unsigned size) nsapi_size_or_error_t UDPSocket::sendto(const char *host, uint16_t port, const void *data, nsapi_size_t size)
{ {
SocketAddress address; SocketAddress address;
int err = _stack->gethostbyname(host, &address); nsapi_size_or_error_t err = _stack->gethostbyname(host, &address);
if (err) { if (err) {
return NSAPI_ERROR_DNS_FAILURE; return NSAPI_ERROR_DNS_FAILURE;
} }
@ -48,10 +48,10 @@ int UDPSocket::sendto(const char *host, uint16_t port, const void *data, unsigne
return sendto(address, data, size); return sendto(address, data, size);
} }
int UDPSocket::sendto(const SocketAddress &address, const void *data, unsigned size) nsapi_size_or_error_t UDPSocket::sendto(const SocketAddress &address, const void *data, nsapi_size_t size)
{ {
_lock.lock(); _lock.lock();
int ret; nsapi_size_or_error_t ret;
// If this assert is hit then there are two threads // If this assert is hit then there are two threads
// performing a send at the same time which is undefined // performing a send at the same time which is undefined
@ -66,7 +66,7 @@ int UDPSocket::sendto(const SocketAddress &address, const void *data, unsigned s
} }
_pending = 0; _pending = 0;
int sent = _stack->socket_sendto(_socket, address, data, size); nsapi_size_or_error_t sent = _stack->socket_sendto(_socket, address, data, size);
if ((0 == _timeout) || (NSAPI_ERROR_WOULD_BLOCK != sent)) { if ((0 == _timeout) || (NSAPI_ERROR_WOULD_BLOCK != sent)) {
ret = sent; ret = sent;
break; break;
@ -92,10 +92,10 @@ int UDPSocket::sendto(const SocketAddress &address, const void *data, unsigned s
return ret; return ret;
} }
int UDPSocket::recvfrom(SocketAddress *address, void *buffer, unsigned size) nsapi_size_or_error_t UDPSocket::recvfrom(SocketAddress *address, void *buffer, nsapi_size_t size)
{ {
_lock.lock(); _lock.lock();
int ret; nsapi_size_or_error_t ret;
// If this assert is hit then there are two threads // If this assert is hit then there are two threads
// performing a recv at the same time which is undefined // performing a recv at the same time which is undefined
@ -110,7 +110,7 @@ int UDPSocket::recvfrom(SocketAddress *address, void *buffer, unsigned size)
} }
_pending = 0; _pending = 0;
int recv = _stack->socket_recvfrom(_socket, address, buffer, size); nsapi_size_or_error_t recv = _stack->socket_recvfrom(_socket, address, buffer, size);
if ((0 == _timeout) || (NSAPI_ERROR_WOULD_BLOCK != recv)) { if ((0 == _timeout) || (NSAPI_ERROR_WOULD_BLOCK != recv)) {
ret = recv; ret = recv;
break; break;

View File

@ -74,7 +74,8 @@ public:
* @return Number of sent bytes on success, negative error * @return Number of sent bytes on success, negative error
* code on failure * code on failure
*/ */
int sendto(const char *host, uint16_t port, const void *data, unsigned size); nsapi_size_or_error_t sendto(const char *host, uint16_t port,
const void *data, nsapi_size_t size);
/** Send a packet over a UDP socket /** Send a packet over a UDP socket
* *
@ -91,7 +92,8 @@ public:
* @return Number of sent bytes on success, negative error * @return Number of sent bytes on success, negative error
* code on failure * code on failure
*/ */
int sendto(const SocketAddress &address, const void *data, unsigned size); nsapi_size_or_error_t sendto(const SocketAddress &address,
const void *data, nsapi_size_t size);
/** Receive a packet over a UDP socket /** Receive a packet over a UDP socket
* *
@ -108,7 +110,8 @@ public:
* @return Number of received bytes on success, negative error * @return Number of received bytes on success, negative error
* code on failure * code on failure
*/ */
int recvfrom(SocketAddress *address, void *data, unsigned size); nsapi_size_or_error_t recvfrom(SocketAddress *address,
void *data, nsapi_size_t size);
protected: protected:
virtual nsapi_protocol_t get_proto(); virtual nsapi_protocol_t get_proto();

View File

@ -44,14 +44,15 @@ public:
* (defaults to NSAPI_SECURITY_NONE) * (defaults to NSAPI_SECURITY_NONE)
* @return 0 on success, or error code on failure * @return 0 on success, or error code on failure
*/ */
virtual int set_credentials(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE) = 0; virtual nsapi_error_t set_credentials(const char *ssid, const char *pass,
nsapi_security_t security = NSAPI_SECURITY_NONE) = 0;
/** Set the WiFi network channel /** Set the WiFi network channel
* *
* @param channel Channel on which the connection is to be made, or 0 for any (Default: 0) * @param channel Channel on which the connection is to be made, or 0 for any (Default: 0)
* @return 0 on success, or error code on failure * @return 0 on success, or error code on failure
*/ */
virtual int set_channel(uint8_t channel) = 0; virtual nsapi_error_t set_channel(uint8_t channel) = 0;
/** Gets the current radio signal strength for active connection /** Gets the current radio signal strength for active connection
* *
@ -70,9 +71,8 @@ public:
* @param channel Channel on which the connection is to be made, or 0 for any (Default: 0) * @param channel Channel on which the connection is to be made, or 0 for any (Default: 0)
* @return 0 on success, or error code on failure * @return 0 on success, or error code on failure
*/ */
virtual int connect(const char *ssid, const char *pass, virtual nsapi_error_t connect(const char *ssid, const char *pass,
nsapi_security_t security = NSAPI_SECURITY_NONE, nsapi_security_t security = NSAPI_SECURITY_NONE, uint8_t channel = 0) = 0;
uint8_t channel = 0) = 0;
/** Start the interface /** Start the interface
* *
@ -81,13 +81,13 @@ public:
* *
* @return 0 on success, negative error code on failure * @return 0 on success, negative error code on failure
*/ */
virtual int connect() = 0; virtual nsapi_error_t connect() = 0;
/** Stop the interface /** Stop the interface
* *
* @return 0 on success, or error code on failure * @return 0 on success, or error code on failure
*/ */
virtual int disconnect() = 0; virtual nsapi_error_t disconnect() = 0;
/** Scan for available networks /** Scan for available networks
* *
@ -99,10 +99,11 @@ public:
* @param ap Pointer to allocated array to store discovered AP * @param ap Pointer to allocated array to store discovered AP
* @param count Size of allocated @a res array, or 0 to only count available AP * @param count Size of allocated @a res array, or 0 to only count available AP
* @param timeout Timeout in milliseconds; 0 for no timeout (Default: 0) * @param timeout Timeout in milliseconds; 0 for no timeout (Default: 0)
* @return Number of entries in @a, or if @a count was 0 number of available networks, negative on error * @return Number of entries in @a, or if @a count was 0 number of available networks,
* negative on error
* see @a nsapi_error * see @a nsapi_error
*/ */
virtual int scan(WiFiAccessPoint *res, unsigned count) = 0; virtual nsapi_size_or_error_t scan(WiFiAccessPoint *res, nsapi_size_t count) = 0;
}; };
#endif #endif

View File

@ -39,13 +39,13 @@ nsapi_addr_t dns_servers[DNS_SERVERS_SIZE] = {
}; };
// DNS server configuration // DNS server configuration
extern "C" int nsapi_dns_add_server(nsapi_addr_t addr) extern "C" nsapi_error_t nsapi_dns_add_server(nsapi_addr_t addr)
{ {
memmove(&dns_servers[1], &dns_servers[0], memmove(&dns_servers[1], &dns_servers[0],
(DNS_SERVERS_SIZE-1)*sizeof(nsapi_addr_t)); (DNS_SERVERS_SIZE-1)*sizeof(nsapi_addr_t));
dns_servers[0] = addr; dns_servers[0] = addr;
return 0; return NSAPI_ERROR_OK;
} }
@ -193,7 +193,7 @@ static int dns_scan_response(const uint8_t **p, nsapi_addr_t *addr, unsigned add
} }
// core query function // core query function
static int nsapi_dns_query_multiple(NetworkStack *stack, const char *host, static nsapi_size_or_error_t nsapi_dns_query_multiple(NetworkStack *stack, const char *host,
nsapi_addr_t *addr, unsigned addr_count, nsapi_version_t version) nsapi_addr_t *addr, unsigned addr_count, nsapi_version_t version)
{ {
// check for valid host name // check for valid host name
@ -217,7 +217,7 @@ static int nsapi_dns_query_multiple(NetworkStack *stack, const char *host,
return NSAPI_ERROR_NO_MEMORY; return NSAPI_ERROR_NO_MEMORY;
} }
int result = NSAPI_ERROR_DNS_FAILURE; nsapi_size_or_error_t result = NSAPI_ERROR_DNS_FAILURE;
// check against each dns server // check against each dns server
for (unsigned i = 0; i < DNS_SERVERS_SIZE; i++) { for (unsigned i = 0; i < DNS_SERVERS_SIZE; i++) {
@ -265,18 +265,18 @@ static int nsapi_dns_query_multiple(NetworkStack *stack, const char *host,
} }
// convenience functions for other forms of queries // convenience functions for other forms of queries
extern "C" int nsapi_dns_query_multiple(nsapi_stack_t *stack, const char *host, extern "C" nsapi_size_or_error_t nsapi_dns_query_multiple(nsapi_stack_t *stack, const char *host,
nsapi_addr_t *addr, unsigned addr_count, nsapi_version_t version) nsapi_addr_t *addr, nsapi_size_t addr_count, nsapi_version_t version)
{ {
NetworkStack *nstack = nsapi_create_stack(stack); NetworkStack *nstack = nsapi_create_stack(stack);
return nsapi_dns_query_multiple(nstack, host, addr, addr_count, version); return nsapi_dns_query_multiple(nstack, host, addr, addr_count, version);
} }
int nsapi_dns_query_multiple(NetworkStack *stack, const char *host, nsapi_size_or_error_t nsapi_dns_query_multiple(NetworkStack *stack, const char *host,
SocketAddress *addresses, unsigned addr_count, nsapi_version_t version) SocketAddress *addresses, nsapi_size_t addr_count, nsapi_version_t version)
{ {
nsapi_addr_t *addrs = new nsapi_addr_t[addr_count]; nsapi_addr_t *addrs = new nsapi_addr_t[addr_count];
int result = nsapi_dns_query_multiple(stack, host, addrs, addr_count, version); nsapi_size_or_error_t result = nsapi_dns_query_multiple(stack, host, addrs, addr_count, version);
if (result > 0) { if (result > 0) {
for (int i = 0; i < result; i++) { for (int i = 0; i < result; i++) {
@ -288,19 +288,19 @@ int nsapi_dns_query_multiple(NetworkStack *stack, const char *host,
return result; return result;
} }
extern "C" int nsapi_dns_query(nsapi_stack_t *stack, const char *host, extern "C" nsapi_error_t nsapi_dns_query(nsapi_stack_t *stack, const char *host,
nsapi_addr_t *addr, nsapi_version_t version) nsapi_addr_t *addr, nsapi_version_t version)
{ {
NetworkStack *nstack = nsapi_create_stack(stack); NetworkStack *nstack = nsapi_create_stack(stack);
int result = nsapi_dns_query_multiple(nstack, host, addr, 1, version); nsapi_size_or_error_t result = nsapi_dns_query_multiple(nstack, host, addr, 1, version);
return (result > 0) ? 0 : result; return (nsapi_error_t)((result > 0) ? 0 : result);
} }
int nsapi_dns_query(NetworkStack *stack, const char *host, nsapi_error_t nsapi_dns_query(NetworkStack *stack, const char *host,
SocketAddress *address, nsapi_version_t version) SocketAddress *address, nsapi_version_t version)
{ {
nsapi_addr_t addr; nsapi_addr_t addr;
int result = nsapi_dns_query_multiple(stack, host, &addr, 1, version); nsapi_size_or_error_t result = nsapi_dns_query_multiple(stack, host, &addr, 1, version);
address->set_addr(addr); address->set_addr(addr);
return (result > 0) ? 0 : result; return (nsapi_error_t)((result > 0) ? 0 : result);
} }

View File

@ -37,7 +37,7 @@
* @return 0 on success, negative error code on failure * @return 0 on success, negative error code on failure
* NSAPI_ERROR_DNS_FAILURE indicates the host could not be found * NSAPI_ERROR_DNS_FAILURE indicates the host could not be found
*/ */
int nsapi_dns_query(nsapi_stack_t *stack, const char *host, nsapi_error_t nsapi_dns_query(nsapi_stack_t *stack, const char *host,
nsapi_addr_t *addr, nsapi_version_t version); nsapi_addr_t *addr, nsapi_version_t version);
/** Query a domain name server for multiple IP address of a given hostname /** Query a domain name server for multiple IP address of a given hostname
@ -50,15 +50,15 @@ int nsapi_dns_query(nsapi_stack_t *stack, const char *host,
* @return Number of addresses found on success, negative error code on failure * @return Number of addresses found on success, negative error code on failure
* NSAPI_ERROR_DNS_FAILURE indicates the host could not be found * NSAPI_ERROR_DNS_FAILURE indicates the host could not be found
*/ */
int nsapi_dns_query_multiple(nsapi_stack_t *stack, const char *host, nsapi_size_or_error_t nsapi_dns_query_multiple(nsapi_stack_t *stack, const char *host,
nsapi_addr_t *addr, unsigned addr_count, nsapi_version_t version); nsapi_addr_t *addr, nsapi_size_t addr_count, nsapi_version_t version);
/** Add a domain name server to list of servers to query /** Add a domain name server to list of servers to query
* *
* @param addr Destination for the host address * @param addr Destination for the host address
* @return 0 on success, negative error code on failure * @return 0 on success, negative error code on failure
*/ */
int nsapi_dns_add_server(nsapi_addr_t addr); nsapi_error_t nsapi_dns_add_server(nsapi_addr_t addr);
#else #else
@ -73,7 +73,7 @@ int nsapi_dns_add_server(nsapi_addr_t addr);
* @return 0 on success, negative error code on failure * @return 0 on success, negative error code on failure
* NSAPI_ERROR_DNS_FAILURE indicates the host could not be found * NSAPI_ERROR_DNS_FAILURE indicates the host could not be found
*/ */
int nsapi_dns_query(NetworkStack *stack, const char *host, nsapi_error_t nsapi_dns_query(NetworkStack *stack, const char *host,
SocketAddress *addr, nsapi_version_t version = NSAPI_IPv4); SocketAddress *addr, nsapi_version_t version = NSAPI_IPv4);
/** Query a domain name server for an IP address of a given hostname /** Query a domain name server for an IP address of a given hostname
@ -85,7 +85,7 @@ int nsapi_dns_query(NetworkStack *stack, const char *host,
* @return 0 on success, negative error code on failure * @return 0 on success, negative error code on failure
* NSAPI_ERROR_DNS_FAILURE indicates the host could not be found * NSAPI_ERROR_DNS_FAILURE indicates the host could not be found
*/ */
extern "C" int nsapi_dns_query(nsapi_stack_t *stack, const char *host, extern "C" nsapi_error_t nsapi_dns_query(nsapi_stack_t *stack, const char *host,
nsapi_addr_t *addr, nsapi_version_t version = NSAPI_IPv4); nsapi_addr_t *addr, nsapi_version_t version = NSAPI_IPv4);
/** Query a domain name server for an IP address of a given hostname /** Query a domain name server for an IP address of a given hostname
@ -98,7 +98,7 @@ extern "C" int nsapi_dns_query(nsapi_stack_t *stack, const char *host,
* NSAPI_ERROR_DNS_FAILURE indicates the host could not be found * NSAPI_ERROR_DNS_FAILURE indicates the host could not be found
*/ */
template <typename S> template <typename S>
int nsapi_dns_query(S *stack, const char *host, nsapi_error_t nsapi_dns_query(S *stack, const char *host,
SocketAddress *addr, nsapi_version_t version = NSAPI_IPv4) SocketAddress *addr, nsapi_version_t version = NSAPI_IPv4)
{ {
return nsapi_dns_query(nsapi_create_stack(stack), host, addr, version); return nsapi_dns_query(nsapi_create_stack(stack), host, addr, version);
@ -114,8 +114,8 @@ int nsapi_dns_query(S *stack, const char *host,
* @return Number of addresses found on success, negative error code on failure * @return Number of addresses found on success, negative error code on failure
* NSAPI_ERROR_DNS_FAILURE indicates the host could not be found * NSAPI_ERROR_DNS_FAILURE indicates the host could not be found
*/ */
int nsapi_dns_query_multiple(NetworkStack *stack, const char *host, nsapi_size_or_error_t nsapi_dns_query_multiple(NetworkStack *stack, const char *host,
SocketAddress *addr, unsigned addr_count, nsapi_version_t version = NSAPI_IPv4); SocketAddress *addr, nsapi_size_t addr_count, nsapi_version_t version = NSAPI_IPv4);
/** Query a domain name server for multiple IP address of a given hostname /** Query a domain name server for multiple IP address of a given hostname
* *
@ -127,8 +127,8 @@ int nsapi_dns_query_multiple(NetworkStack *stack, const char *host,
* @return Number of addresses found on success, negative error code on failure * @return Number of addresses found on success, negative error code on failure
* NSAPI_ERROR_DNS_FAILURE indicates the host could not be found * NSAPI_ERROR_DNS_FAILURE indicates the host could not be found
*/ */
extern "C" int nsapi_dns_query_multiple(nsapi_stack_t *stack, const char *host, extern "C" nsapi_size_or_error_t nsapi_dns_query_multiple(nsapi_stack_t *stack, const char *host,
nsapi_addr_t *addr, unsigned addr_count, nsapi_version_t version = NSAPI_IPv4); nsapi_addr_t *addr, nsapi_size_t addr_count, nsapi_version_t version = NSAPI_IPv4);
/** Query a domain name server for multiple IP address of a given hostname /** Query a domain name server for multiple IP address of a given hostname
* *
@ -141,8 +141,8 @@ extern "C" int nsapi_dns_query_multiple(nsapi_stack_t *stack, const char *host,
* NSAPI_ERROR_DNS_FAILURE indicates the host could not be found * NSAPI_ERROR_DNS_FAILURE indicates the host could not be found
*/ */
template <typename S> template <typename S>
int nsapi_dns_query_multiple(S *stack, const char *host, nsapi_size_or_error_t nsapi_dns_query_multiple(S *stack, const char *host,
SocketAddress *addr, unsigned addr_count, nsapi_version_t version = NSAPI_IPv4) SocketAddress *addr, nsapi_size_t addr_count, nsapi_version_t version = NSAPI_IPv4)
{ {
return nsapi_dns_query_multiple(nsapi_create_stack(stack), return nsapi_dns_query_multiple(nsapi_create_stack(stack),
host, addr, addr_count, version); host, addr, addr_count, version);
@ -153,14 +153,14 @@ int nsapi_dns_query_multiple(S *stack, const char *host,
* @param addr Destination for the host address * @param addr Destination for the host address
* @return 0 on success, negative error code on failure * @return 0 on success, negative error code on failure
*/ */
extern "C" int nsapi_dns_add_server(nsapi_addr_t addr); extern "C" nsapi_error_t nsapi_dns_add_server(nsapi_addr_t addr);
/** Add a domain name server to list of servers to query /** Add a domain name server to list of servers to query
* *
* @param addr Destination for the host address * @param addr Destination for the host address
* @return 0 on success, negative error code on failure * @return 0 on success, negative error code on failure
*/ */
static inline int nsapi_dns_add_server(const SocketAddress &address) static inline nsapi_error_t nsapi_dns_add_server(const SocketAddress &address)
{ {
return nsapi_dns_add_server(address.get_addr()); return nsapi_dns_add_server(address.get_addr());
} }
@ -170,7 +170,7 @@ static inline int nsapi_dns_add_server(const SocketAddress &address)
* @param addr Destination for the host address * @param addr Destination for the host address
* @return 0 on success, negative error code on failure * @return 0 on success, negative error code on failure
*/ */
static inline int nsapi_dns_add_server(const char *address) static inline nsapi_error_t nsapi_dns_add_server(const char *address)
{ {
return nsapi_dns_add_server(SocketAddress(address)); return nsapi_dns_add_server(SocketAddress(address));
} }

View File

@ -34,7 +34,7 @@ extern "C" {
* *
* @enum nsapi_error_t * @enum nsapi_error_t
*/ */
typedef enum nsapi_error { enum nsapi_error {
NSAPI_ERROR_OK = 0, /*!< no error */ NSAPI_ERROR_OK = 0, /*!< no error */
NSAPI_ERROR_WOULD_BLOCK = -3001, /*!< no data is not available but call is non-blocking */ NSAPI_ERROR_WOULD_BLOCK = -3001, /*!< no data is not available but call is non-blocking */
NSAPI_ERROR_UNSUPPORTED = -3002, /*!< unsupported functionality */ NSAPI_ERROR_UNSUPPORTED = -3002, /*!< unsupported functionality */
@ -48,7 +48,25 @@ typedef enum nsapi_error {
NSAPI_ERROR_DHCP_FAILURE = -3010, /*!< DHCP failed to complete successfully */ NSAPI_ERROR_DHCP_FAILURE = -3010, /*!< DHCP failed to complete successfully */
NSAPI_ERROR_AUTH_FAILURE = -3011, /*!< connection to access point failed */ NSAPI_ERROR_AUTH_FAILURE = -3011, /*!< connection to access point failed */
NSAPI_ERROR_DEVICE_ERROR = -3012, /*!< failure interfacing with the network processor */ NSAPI_ERROR_DEVICE_ERROR = -3012, /*!< failure interfacing with the network processor */
} nsapi_error_t; };
/** Type used to represent error codes
*
* This is a separate type from enum nsapi_error to avoid breaking
* compatibility in type-sensitive overloads
*/
typedef signed int nsapi_error_t;
/** Type used to represent the size of data passed through sockets
*/
typedef unsigned int nsapi_size_t;
/** Type used to represent either a size or error pased through sockets
*
* A valid nsapi_size_or_error_t is either a non-negative size or a
* negative error code from the nsapi_error_t
*/
typedef signed int nsapi_size_or_error_t;
/** Enum of encryption types /** Enum of encryption types
* *
@ -233,14 +251,14 @@ typedef struct nsapi_stack_api
* @param version Address family * @param version Address family
* @return 0 on success, negative error code on failure * @return 0 on success, negative error code on failure
*/ */
int (*gethostbyname)(nsapi_stack_t *stack, const char *host, nsapi_addr_t *addr, nsapi_version_t version); nsapi_error_t (*gethostbyname)(nsapi_stack_t *stack, const char *host, nsapi_addr_t *addr, nsapi_version_t version);
/** Add a domain name server to list of servers to query /** Add a domain name server to list of servers to query
* *
* @param addr Destination for the host address * @param addr Destination for the host address
* @return 0 on success, negative error code on failure * @return 0 on success, negative error code on failure
*/ */
int (*add_dns_server)(nsapi_stack_t *stack, nsapi_addr_t addr); nsapi_error_t (*add_dns_server)(nsapi_stack_t *stack, nsapi_addr_t addr);
/* Set stack-specific stack options /* Set stack-specific stack options
* *
@ -255,7 +273,8 @@ typedef struct nsapi_stack_api
* @param optlen Length of the option value * @param optlen Length of the option value
* @return 0 on success, negative error code on failure * @return 0 on success, negative error code on failure
*/ */
int (*setstackopt)(nsapi_stack_t *stack, int level, int optname, const void *optval, unsigned optlen); nsapi_error_t (*setstackopt)(nsapi_stack_t *stack, int level,
int optname, const void *optval, unsigned optlen);
/* Get stack-specific stack options /* Get stack-specific stack options
* *
@ -270,7 +289,8 @@ typedef struct nsapi_stack_api
* @param optlen Length of the option value * @param optlen Length of the option value
* @return 0 on success, negative error code on failure * @return 0 on success, negative error code on failure
*/ */
int (*getstackopt)(nsapi_stack_t *stack, int level, int optname, void *optval, unsigned *optlen); nsapi_error_t (*getstackopt)(nsapi_stack_t *stack, int level,
int optname, void *optval, unsigned *optlen);
/** Opens a socket /** Opens a socket
* *
@ -285,7 +305,8 @@ typedef struct nsapi_stack_api
* @param proto Protocol of socket to open, NSAPI_TCP or NSAPI_UDP * @param proto Protocol of socket to open, NSAPI_TCP or NSAPI_UDP
* @return 0 on success, negative error code on failure * @return 0 on success, negative error code on failure
*/ */
int (*socket_open)(nsapi_stack_t *stack, nsapi_socket_t *socket, nsapi_protocol_t proto); nsapi_error_t (*socket_open)(nsapi_stack_t *stack, nsapi_socket_t *socket,
nsapi_protocol_t proto);
/** Close the socket /** Close the socket
* *
@ -296,7 +317,7 @@ typedef struct nsapi_stack_api
* @param socket Socket handle * @param socket Socket handle
* @return 0 on success, negative error code on failure * @return 0 on success, negative error code on failure
*/ */
int (*socket_close)(nsapi_stack_t *stack, nsapi_socket_t socket); nsapi_error_t (*socket_close)(nsapi_stack_t *stack, nsapi_socket_t socket);
/** Bind a specific address to a socket /** Bind a specific address to a socket
* *
@ -309,7 +330,8 @@ typedef struct nsapi_stack_api
* @param port Local port to bind * @param port Local port to bind
* @return 0 on success, negative error code on failure. * @return 0 on success, negative error code on failure.
*/ */
int (*socket_bind)(nsapi_stack_t *stack, nsapi_socket_t socket, nsapi_addr_t addr, uint16_t port); nsapi_error_t (*socket_bind)(nsapi_stack_t *stack, nsapi_socket_t socket,
nsapi_addr_t addr, uint16_t port);
/** Listen for connections on a TCP socket /** Listen for connections on a TCP socket
* *
@ -322,7 +344,7 @@ typedef struct nsapi_stack_api
* simultaneously * simultaneously
* @return 0 on success, negative error code on failure * @return 0 on success, negative error code on failure
*/ */
int (*socket_listen)(nsapi_stack_t *stack, nsapi_socket_t socket, int backlog); nsapi_error_t (*socket_listen)(nsapi_stack_t *stack, nsapi_socket_t socket, int backlog);
/** Connects TCP socket to a remote host /** Connects TCP socket to a remote host
* *
@ -335,7 +357,8 @@ typedef struct nsapi_stack_api
* @param port The port of the remote host * @param port The port of the remote host
* @return 0 on success, negative error code on failure * @return 0 on success, negative error code on failure
*/ */
int (*socket_connect)(nsapi_stack_t *stack, nsapi_socket_t socket, nsapi_addr_t addr, uint16_t port); nsapi_error_t (*socket_connect)(nsapi_stack_t *stack, nsapi_socket_t socket,
nsapi_addr_t addr, uint16_t port);
/** Accepts a connection on a TCP socket /** Accepts a connection on a TCP socket
* *
@ -357,7 +380,8 @@ typedef struct nsapi_stack_api
* @param port Destination for the port of the remote host * @param port Destination for the port of the remote host
* @return 0 on success, negative error code on failure * @return 0 on success, negative error code on failure
*/ */
int (*socket_accept)(nsapi_stack_t *stack, nsapi_socket_t server, nsapi_socket_t *socket, nsapi_addr_t *addr, uint16_t *port); nsapi_error_t (*socket_accept)(nsapi_stack_t *stack, nsapi_socket_t server,
nsapi_socket_t *socket, nsapi_addr_t *addr, uint16_t *port);
/** Send data over a TCP socket /** Send data over a TCP socket
* *
@ -374,7 +398,8 @@ typedef struct nsapi_stack_api
* @return Number of sent bytes on success, negative error * @return Number of sent bytes on success, negative error
* code on failure * code on failure
*/ */
int (*socket_send)(nsapi_stack_t *stack, nsapi_socket_t socket, const void *data, unsigned size); nsapi_size_or_error_t (*socket_send)(nsapi_stack_t *stack, nsapi_socket_t socket,
const void *data, nsapi_size_t size);
/** Receive data over a TCP socket /** Receive data over a TCP socket
* *
@ -391,7 +416,8 @@ typedef struct nsapi_stack_api
* @return Number of received bytes on success, negative error * @return Number of received bytes on success, negative error
* code on failure * code on failure
*/ */
int (*socket_recv)(nsapi_stack_t *stack, nsapi_socket_t socket, void *data, unsigned size); nsapi_size_or_error_t (*socket_recv)(nsapi_stack_t *stack, nsapi_socket_t socket,
void *data, nsapi_size_t size);
/** Send a packet over a UDP socket /** Send a packet over a UDP socket
* *
@ -410,7 +436,8 @@ typedef struct nsapi_stack_api
* @return Number of sent bytes on success, negative error * @return Number of sent bytes on success, negative error
* code on failure * code on failure
*/ */
int (*socket_sendto)(nsapi_stack_t *stack, nsapi_socket_t socket, nsapi_addr_t addr, uint16_t port, const void *data, unsigned size); nsapi_size_or_error_t (*socket_sendto)(nsapi_stack_t *stack, nsapi_socket_t socket,
nsapi_addr_t addr, uint16_t port, const void *data, nsapi_size_t size);
/** Receive a packet over a UDP socket /** Receive a packet over a UDP socket
* *
@ -429,7 +456,8 @@ typedef struct nsapi_stack_api
* @return Number of received bytes on success, negative error * @return Number of received bytes on success, negative error
* code on failure * code on failure
*/ */
int (*socket_recvfrom)(nsapi_stack_t *stack, nsapi_socket_t socket, nsapi_addr_t *addr, uint16_t *port, void *buffer, unsigned size); nsapi_size_or_error_t (*socket_recvfrom)(nsapi_stack_t *stack, nsapi_socket_t socket,
nsapi_addr_t *addr, uint16_t *port, void *buffer, nsapi_size_t size);
/** Register a callback on state change of the socket /** Register a callback on state change of the socket
* *
@ -445,7 +473,8 @@ typedef struct nsapi_stack_api
* @param callback Function to call on state change * @param callback Function to call on state change
* @param data Argument to pass to callback * @param data Argument to pass to callback
*/ */
void (*socket_attach)(nsapi_stack_t *stack, nsapi_socket_t socket, void (*callback)(void *), void *data); void (*socket_attach)(nsapi_stack_t *stack, nsapi_socket_t socket,
void (*callback)(void *), void *data);
/* Set stack-specific socket options /* Set stack-specific socket options
* *
@ -461,7 +490,8 @@ typedef struct nsapi_stack_api
* @param optlen Length of the option value * @param optlen Length of the option value
* @return 0 on success, negative error code on failure * @return 0 on success, negative error code on failure
*/ */
int (*setsockopt)(nsapi_stack_t *stack, nsapi_socket_t socket, int level, int optname, const void *optval, unsigned optlen); nsapi_error_t (*setsockopt)(nsapi_stack_t *stack, nsapi_socket_t socket, int level,
int optname, const void *optval, unsigned optlen);
/* Get stack-specific socket options /* Get stack-specific socket options
* *
@ -477,7 +507,8 @@ typedef struct nsapi_stack_api
* @param optlen Length of the option value * @param optlen Length of the option value
* @return 0 on success, negative error code on failure * @return 0 on success, negative error code on failure
*/ */
int (*getsockopt)(nsapi_stack_t *stack, nsapi_socket_t socket, int level, int optname, void *optval, unsigned *optlen); nsapi_error_t (*getsockopt)(nsapi_stack_t *stack, nsapi_socket_t socket, int level,
int optname, void *optval, unsigned *optlen);
} nsapi_stack_api_t; } nsapi_stack_api_t;