Base class for IP Based Networking Libraries
Dependencies: DnsQuery
Dependents: TempTower BSDInterfaceTests HelloBSDInterface ESP8266InterfaceTests ... more
For a complete getting started guide see the wiki...
Network Socket API
The Network Socket API provides a common interface for using sockets on network devices. The API provides a simple class-based interface that should be familiar to users experienced with other socket APIs. Additionally, the API provides a simple interface for implementing network devices, making it easy to connect hardware agnostic programs to new devices.
Network Interfaces
The NetworkInterface provides an abstract class for network devices that support sockets. Devices should provide a DeviceInterface class that inherits this interface and adds implementation specific methods for using the device. A NetworkInterface must be provided to a Socket constructor to open a socket on the interface. Currently two subclasses are defined for common devices, EthernetInterface
and WiFiInterface.
Sockets
The Socket class is used for managing network sockets. Once opened, the socket provides a pipe through which data can sent and recieved to a specific endpoint. The socket class can be instantiated as either a TCPSocket
or a UDPSocket
which defines the protocol used for the connection.
Revision 89:b1d417383c0d, committed 2016-04-19
- Comitter:
- Christopher Haster
- Date:
- Tue Apr 19 18:20:38 2016 -0500
- Parent:
- 88:6cfd38609828
- Child:
- 90:0a988e4abb72
- Commit message:
- Adopt mbed style doxygen comments
Changed in this revision
--- a/EthernetInterface.h Wed Apr 06 13:50:19 2016 +0000 +++ b/EthernetInterface.h Tue Apr 19 18:20:38 2016 -0500 @@ -1,39 +1,39 @@ -/* Socket - * Copyright (c) 2015 ARM Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef ETHERNET_INTERFACE_H -#define ETHERNET_INTERFACE_H - -#include "NetworkInterface.h" - -/** EthernetInterface class - * Common interface that is shared between ethernet hardware - */ -class EthernetInterface : public NetworkInterface -{ -public: - /** Start the interface - * @return 0 on success, negative on failure - */ - virtual int connect() = 0; - - /** Stop the interface - * @return 0 on success, negative on failure - */ - virtual int disconnect() = 0; -}; - -#endif +/* Socket + * Copyright (c) 2015 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ETHERNET_INTERFACE_H +#define ETHERNET_INTERFACE_H + +#include "NetworkInterface.h" + +/** EthernetInterface class + * Common interface that is shared between ethernet hardware + */ +class EthernetInterface : public NetworkInterface +{ +public: + /** Start the interface + * @return 0 on success, negative on failure + */ + virtual int connect() = 0; + + /** Stop the interface + * @return 0 on success, negative on failure + */ + virtual int disconnect() = 0; +}; + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MeshInterface.h Tue Apr 19 18:20:38 2016 -0500 @@ -0,0 +1,39 @@ +/* Socket + * Copyright (c) 2015 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef MESH_INTERFACE_H +#define MESH_INTERFACE_H + +#include "NetworkInterface.h" + +/** MeshInterface class + * Common interface that is shared between ethernet hardware + */ +class MeshInterface : public NetworkInterface +{ +public: + /** Start the interface + * @return 0 on success, negative on failure + */ + virtual int connect() = 0; + + /** Stop the interface + * @return 0 on success, negative on failure + */ + virtual int disconnect() = 0; +}; + +#endif
--- a/NetworkInterface.cpp Wed Apr 06 13:50:19 2016 +0000 +++ b/NetworkInterface.cpp Tue Apr 19 18:20:38 2016 -0500 @@ -1,23 +1,23 @@ -/* Socket - * Copyright (c) 2015 ARM Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "DnsQuery.h" -#include "mbed.h" - -int NetworkInterface::gethostbyname(const char *name, char *dest) -{ - return dnsQuery(this, name, dest); -} +/* Socket + * Copyright (c) 2015 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "DnsQuery.h" +#include "mbed.h" + +int NetworkInterface::gethostbyname(const char *name, char *dest) +{ + return dnsQuery(this, name, dest); +}
--- a/NetworkInterface.h Wed Apr 06 13:50:19 2016 +0000 +++ b/NetworkInterface.h Tue Apr 19 18:20:38 2016 -0500 @@ -1,238 +1,233 @@ -/* Socket - * Copyright (c) 2015 ARM Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef NETWORK_INTERFACE_H -#define NETWORK_INTERFACE_H - -#ifndef MBED_OPERATORS -#define MBED_OPERATORS -#endif -#include "FunctionPointer.h" -#include "SocketAddress.h" - -/** - * @enum ns_error_t - * @brief enum of standardized error codes - */ -enum nsapi_error_t { - NSAPI_ERROR_WOULD_BLOCK = -3001, /*!< no data is not available but call is non-blocking */ - NSAPI_ERROR_UNSUPPORTED = -3002, /*!< unsupported configuration */ - NSAPI_ERROR_NO_CONNECTION = -3003, /*!< not connected to a network */ - NSAPI_ERROR_NO_SOCKET = -3004, /*!< socket not available for use */ - NSAPI_ERROR_NO_ADDRESS = -3005, /*!< IP address is not known */ - NSAPI_ERROR_NO_MEMORY = -3006, /*!< memory resource not available */ - NSAPI_ERROR_DNS_FAILURE = -3007, /*!< DNS failed to complete successfully */ - NSAPI_ERROR_DHCP_FAILURE = -3008, /*!< DHCP failed to complete successfully */ - NSAPI_ERROR_AUTH_FAILURE = -3009, /*!< connection to access point faield */ - NSAPI_ERROR_DEVICE_ERROR = -3010, /*!< failure interfacing with the network procesor */ -}; - -/** - * @enum ns_opt_t - * @brief enum of available options - */ -enum ns_opt_t { -}; - -/** Enum of socket protocols -/enum protocol_t -*/ -enum nsapi_protocol_t { - NSAPI_TCP, /*!< Socket is of TCP type */ - NSAPI_UDP, /*!< Socket is of UDP type */ -}; - -/** NetworkInterface class - * Common interface that is shared between all hardware that - * can connect to a network over IP. - */ -class NetworkInterface -{ -public: - virtual ~NetworkInterface() {}; - - /** Get the internally stored IP address - /return IP address of the interface or null if not yet connected - */ - virtual const char *get_ip_address() = 0; - - /** Get the internally stored MAC address - /return MAC address of the interface - */ - virtual const char *get_mac_address() = 0; - - /** Get the current status of the interface - /return true if connected - */ - virtual bool is_connected() { - return get_ip_address() != NULL; - } - - /** Looks up the specified host's IP address - /param name Hostname to lookup - /param dest Destination for IP address, must have space for SocketAddress::IP_SIZE - /return 0 on success, negative on failure - */ - virtual int gethostbyname(const char *name, char *dest); - -protected: - friend class Socket; - friend class UDPSocket; - friend class TCPSocket; - friend class TCPServer; - - /** Create a socket - /param proto The type of socket to open, TCP or UDP - /return The alocated socket or null on failure - */ - virtual void *socket_create(nsapi_protocol_t proto) = 0; - - /** Destroy a socket - /param socket Previously allocated socket - */ - virtual void socket_destroy(void *handle) = 0; - - /** Set socket options - \param handle Socket handle - \param optname Option ID - \param optval Option value - \param optlen Length of the option value - \return 0 on success, negative on failure - */ - virtual int socket_set_option(void *handle, int optname, const void *optval, unsigned int optlen) = 0; - - /** Get socket options - \param handle Socket handle - \param optname Option ID - \param optval Buffer pointer where to write the option value - \param optlen Length of the option value - \return 0 on success, negative on failure - */ - virtual int socket_get_option(void *handle, int optname, void *optval, unsigned int *optlen) = 0; - - /** Bind a server socket to a specific port - \param handle Socket handle - \param port The port to listen for incoming connections on - \return 0 on success, negative on failure. - */ - virtual int socket_bind(void *handle, int port) = 0; - - /** Start listening for incoming connections - \param handle Socket handle - \param backlog Number of pending connections that can be queued up at any - one time [Default: 1] - \return 0 on success, negative on failure - */ - virtual int socket_listen(void *handle, int backlog) = 0; - - /** Connects this TCP socket to the server - \param handle Socket handle - \param address SocketAddress to connect to - \return 0 on success, negative on failure - */ - virtual int socket_connect(void *handle, const SocketAddress &address) = 0; - - /** Check if the socket is connected - \param handle Socket handle - \return true if connected, false otherwise - */ - virtual bool socket_is_connected(void *handle) = 0; - - /** Accept a new connection. - \param handle Socket handle - \param socket A TCPSocket instance that will handle the incoming connection. - \return 0 on success, negative on failure. - \note This call is not-blocking, if this call would block, must - immediately return NSAPI_ERROR_WOULD_WAIT - */ - virtual int socket_accept(void *handle, void **connection) = 0; - - /** Send data to the remote host - \param handle Socket handle - \param data The buffer to send to the host - \param size The length of the buffer to send - \return Number of written bytes on success, negative on failure - \note This call is not-blocking, if this call would block, must - immediately return NSAPI_ERROR_WOULD_WAIT - */ - virtual int socket_send(void *handle, const void *data, unsigned size) = 0; - - /** Receive data from the remote host - \param handle Socket handle - \param data The buffer in which to store the data received from the host - \param size The maximum length of the buffer - \return Number of received bytes on success, negative on failure - \note This call is not-blocking, if this call would block, must - immediately return NSAPI_ERROR_WOULD_WAIT - */ - virtual int socket_recv(void *handle, void *data, unsigned size) = 0; - - /** Send a packet to a remote endpoint - \param handle Socket handle - \param address The remote SocketAddress - \param data The packet to be sent - \param size The length of the packet to be sent - \return the number of written bytes on success, negative on failure - \note This call is not-blocking, if this call would block, must - immediately return NSAPI_ERROR_WOULD_WAIT - */ - virtual int socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size) = 0; - - /** Receive a packet from a remote endpoint - \param handle Socket handle - \param address Destination for the remote SocketAddress or null - \param buffer The buffer for storing the incoming packet data - If a packet is too long to fit in the supplied buffer, - excess bytes are discarded - \param size The length of the buffer - \return the number of received bytes on success, negative on failure - \note This call is not-blocking, if this call would block, must - immediately return NSAPI_ERROR_WOULD_WAIT - */ - virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size) = 0; - - /** Close the socket - \param handle Socket handle - \param shutdown free the left-over data in message queues - */ - virtual int socket_close(void *handle, bool shutdown) = 0; - - /** Register a callback on when a new connection is ready - \param handle Socket handle - \param callback Function to call when accept will succeed, may be called in - interrupt context. - \param id Argument to pass to callback - */ - virtual void socket_attach_accept(void *handle, void (*callback)(void *), void *id) = 0; - - /** Register a callback on when send is ready - \param handle Socket handle - \param callback Function to call when accept will succeed, may be called in - interrupt context. - \param id Argument to pass to callback - */ - virtual void socket_attach_send(void *handle, void (*callback)(void *), void *id) = 0; - - /** Register a callback on when recv is ready - \param handle Socket handle - \param callback Function to call when accept will succeed, may be called in - interrupt context. - \param id Argument to pass to callback - */ - virtual void socket_attach_recv(void *handle, void (*callback)(void *), void *id) = 0; -}; - -#endif +/* Socket + * Copyright (c) 2015 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef NETWORK_INTERFACE_H +#define NETWORK_INTERFACE_H + +#include "mbed.h" +#include "SocketAddress.h" + +/** Enum of standardized error codes + * @enum ns_error_t + */ +enum nsapi_error_t { + NSAPI_ERROR_WOULD_BLOCK = -3001, /*!< no data is not available but call is non-blocking */ + NSAPI_ERROR_UNSUPPORTED = -3002, /*!< unsupported configuration */ + NSAPI_ERROR_NO_CONNECTION = -3003, /*!< not connected to a network */ + NSAPI_ERROR_NO_SOCKET = -3004, /*!< socket not available for use */ + NSAPI_ERROR_NO_ADDRESS = -3005, /*!< IP address is not known */ + NSAPI_ERROR_NO_MEMORY = -3006, /*!< memory resource not available */ + NSAPI_ERROR_DNS_FAILURE = -3007, /*!< DNS failed to complete successfully */ + NSAPI_ERROR_DHCP_FAILURE = -3008, /*!< DHCP failed to complete successfully */ + NSAPI_ERROR_AUTH_FAILURE = -3009, /*!< connection to access point faield */ + NSAPI_ERROR_DEVICE_ERROR = -3010, /*!< failure interfacing with the network procesor */ +}; + +/** Enum of available options + * @enum ns_opt_t + */ +enum ns_opt_t { +}; + +/** Enum of socket protocols + * @enum protocol_t + */ +enum nsapi_protocol_t { + NSAPI_TCP, /*!< Socket is of TCP type */ + NSAPI_UDP, /*!< Socket is of UDP type */ +}; + +/** NetworkInterface class + * Common interface that is shared between all hardware that + * can connect to a network over IP. + */ +class NetworkInterface +{ +public: + virtual ~NetworkInterface() {}; + + /** Get the internally stored IP address + * @return IP address of the interface or null if not yet connected + */ + virtual const char *get_ip_address() = 0; + + /** Get the internally stored MAC address + * @return MAC address of the interface + */ + virtual const char *get_mac_address() = 0; + + /** Get the current status of the interface + * @return true if connected + */ + virtual bool is_connected() { + return get_ip_address() != NULL; + } + + /** Looks up the specified host's IP address + * @param name Hostname to lookup + * @param dest Destination for IP address, must have space for SocketAddress::IP_SIZE + * @return 0 on success, negative on failure + */ + virtual int gethostbyname(const char *name, char *dest); + +protected: + friend class Socket; + friend class UDPSocket; + friend class TCPSocket; + friend class TCPServer; + + /** Create a socket + * @param proto The type of socket to open, TCP or UDP + * @return The alocated socket or null on failure + */ + virtual void *socket_create(nsapi_protocol_t proto) = 0; + + /** Destroy a socket + * @param socket Previously allocated socket + */ + virtual void socket_destroy(void *handle) = 0; + + /** Set socket options + * @param handle Socket handle + * @param optname Option ID + * @param optval Option value + * @param optlen Length of the option value + * @return 0 on success, negative on failure + */ + virtual int socket_set_option(void *handle, int optname, const void *optval, unsigned int optlen) = 0; + + /** Get socket options + * @param handle Socket handle + * @param optname Option ID + * @param optval Buffer pointer where to write the option value + * @param optlen Length of the option value + * @return 0 on success, negative on failure + */ + virtual int socket_get_option(void *handle, int optname, void *optval, unsigned int *optlen) = 0; + + /** Bind a server socket to a specific port + * @param handle Socket handle + * @param port The port to listen for incoming connections on + * @return 0 on success, negative on failure. + */ + virtual int socket_bind(void *handle, int port) = 0; + + /** Start listening for incoming connections + * @param handle Socket handle + * @param backlog Number of pending connections that can be queued up at any + * one time [Default: 1] + * @return 0 on success, negative on failure + */ + virtual int socket_listen(void *handle, int backlog) = 0; + + /** Connects this TCP socket to the server + * @param handle Socket handle + * @param address SocketAddress to connect to + * @return 0 on success, negative on failure + */ + virtual int socket_connect(void *handle, const SocketAddress &address) = 0; + + /** Check if the socket is connected + * @param handle Socket handle + * @return true if connected, false otherwise + */ + virtual bool socket_is_connected(void *handle) = 0; + + /** Accept a new connection. + * @param handle Socket handle + * @param socket A TCPSocket instance that will handle the incoming connection. + * @return 0 on success, negative on failure. + * @note This call is not-blocking, if this call would block, must + * immediately return NSAPI_ERROR_WOULD_WAIT + */ + virtual int socket_accept(void *handle, void **connection) = 0; + + /** Send data to the remote host + * @param handle Socket handle + * @param data The buffer to send to the host + * @param size The length of the buffer to send + * @return Number of written bytes on success, negative on failure + * @note This call is not-blocking, if this call would block, must + * immediately return NSAPI_ERROR_WOULD_WAIT + */ + virtual int socket_send(void *handle, const void *data, unsigned size) = 0; + + /** Receive data from the remote host + * @param handle Socket handle + * @param data The buffer in which to store the data received from the host + * @param size The maximum length of the buffer + * @return Number of received bytes on success, negative on failure + * @note This call is not-blocking, if this call would block, must + * immediately return NSAPI_ERROR_WOULD_WAIT + */ + virtual int socket_recv(void *handle, void *data, unsigned size) = 0; + + /** Send a packet to a remote endpoint + * @param handle Socket handle + * @param address The remote SocketAddress + * @param data The packet to be sent + * @param size The length of the packet to be sent + * @return the number of written bytes on success, negative on failure + * @note This call is not-blocking, if this call would block, must + * immediately return NSAPI_ERROR_WOULD_WAIT + */ + virtual int socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size) = 0; + + /** Receive a packet from a remote endpoint + * @param handle Socket handle + * @param address Destination for the remote SocketAddress or null + * @param buffer The buffer for storing the incoming packet data + * If a packet is too long to fit in the supplied buffer, + * excess bytes are discarded + * @param size The length of the buffer + * @return the number of received bytes on success, negative on failure + * @note This call is not-blocking, if this call would block, must + * immediately return NSAPI_ERROR_WOULD_WAIT + */ + virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size) = 0; + + /** Close the socket + * @param handle Socket handle + * @param shutdown free the left-over data in message queues + */ + virtual int socket_close(void *handle, bool shutdown) = 0; + + /** Register a callback on when a new connection is ready + * @param handle Socket handle + * @param callback Function to call when accept will succeed, may be called in + * interrupt context. + * @param id Argument to pass to callback + */ + virtual void socket_attach_accept(void *handle, void (*callback)(void *), void *id) = 0; + + /** Register a callback on when send is ready + * @param handle Socket handle + * @param callback Function to call when accept will succeed, may be called in + * interrupt context. + * @param id Argument to pass to callback + */ + virtual void socket_attach_send(void *handle, void (*callback)(void *), void *id) = 0; + + /** Register a callback on when recv is ready + * @param handle Socket handle + * @param callback Function to call when accept will succeed, may be called in + * interrupt context. + * @param id Argument to pass to callback + */ + virtual void socket_attach_recv(void *handle, void (*callback)(void *), void *id) = 0; +}; + +#endif
--- a/Socket.cpp Wed Apr 06 13:50:19 2016 +0000 +++ b/Socket.cpp Tue Apr 19 18:20:38 2016 -0500 @@ -1,82 +1,82 @@ -/* Socket - * Copyright (c) 2015 ARM Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "Socket.h" - -Socket::Socket(NetworkInterface *iface, nsapi_protocol_t proto) - : _iface(iface) - , _blocking(true) - , _timeout(0) -{ - _socket = _iface->socket_create(proto); -} - -Socket::~Socket() -{ - if (_socket) { - close(false); - } -} - -void Socket::set_blocking(bool blocking) -{ - _blocking = blocking; -} - -void Socket::set_timeout(unsigned timeout) -{ - _timeout = timeout; -} - -int Socket::set_option(int optname, const void *optval, unsigned int optlen) -{ - if (!_socket) { - return NSAPI_ERROR_NO_SOCKET; - } - - return _iface->socket_set_option(_socket, optname, optval, optlen); -} - -int Socket::get_option(int optname, void *optval, unsigned int *optlen) -{ - if (!_socket) { - return NSAPI_ERROR_NO_SOCKET; - } - - return _iface->socket_get_option(_socket, optname, optval, optlen); -} - -int Socket::close(bool shutdown) -{ - if (!_socket) { - return 0; - } - - int err = _iface->socket_close(_socket, shutdown); - if (!err) { - void *socket = _socket; - _socket = 0; - _iface->socket_destroy(socket); - } - - return err; -} - -void Socket::thunk(void *p) -{ - mbed::FuncPtr<void()> *fptr = (mbed::FuncPtr<void()> *)p; - (*fptr)(); -} +/* Socket + * Copyright (c) 2015 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "Socket.h" + +Socket::Socket(NetworkInterface *iface, nsapi_protocol_t proto) + : _iface(iface) + , _blocking(true) + , _timeout(0) +{ + _socket = _iface->socket_create(proto); +} + +Socket::~Socket() +{ + if (_socket) { + close(false); + } +} + +void Socket::set_blocking(bool blocking) +{ + _blocking = blocking; +} + +void Socket::set_timeout(unsigned timeout) +{ + _timeout = timeout; +} + +int Socket::set_option(int optname, const void *optval, unsigned int optlen) +{ + if (!_socket) { + return NSAPI_ERROR_NO_SOCKET; + } + + return _iface->socket_set_option(_socket, optname, optval, optlen); +} + +int Socket::get_option(int optname, void *optval, unsigned int *optlen) +{ + if (!_socket) { + return NSAPI_ERROR_NO_SOCKET; + } + + return _iface->socket_get_option(_socket, optname, optval, optlen); +} + +int Socket::close(bool shutdown) +{ + if (!_socket) { + return 0; + } + + int err = _iface->socket_close(_socket, shutdown); + if (!err) { + void *socket = _socket; + _socket = 0; + _iface->socket_destroy(socket); + } + + return err; +} + +void Socket::thunk(void *p) +{ + FunctionPointer *fptr = (FunctionPointer *)p; + (*fptr)(); +}
--- a/Socket.h Wed Apr 06 13:50:19 2016 +0000 +++ b/Socket.h Tue Apr 19 18:20:38 2016 -0500 @@ -1,73 +1,73 @@ -/* Socket - * Copyright (c) 2015 ARM Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef SOCKET_H -#define SOCKET_H - -#include "SocketAddress.h" -#include "NetworkInterface.h" - -/** Abstract socket class - */ -class Socket { -public: - /** Socket lifetime - */ - virtual ~Socket(); - - /** Set blocking or non-blocking mode of the socket - \param blocking true for blocking mode, false for non-blocking mode. - */ - void set_blocking(bool blocking); - - /** Set timeout on a socket operation if blocking behaviour is enabled - \param timeout timeout in ms - */ - void set_timeout(unsigned int timeout); - - /** Set socket options - \param optname Option ID - \param optval Option value - \param optlen Length of the option value - \return 0 on success, negative on failure - */ - int set_option(int optname, const void *optval, unsigned optlen); - - /** Get socket options - \param optname Option ID - \param optval Buffer pointer where to write the option value - \param optlen Length of the option value - \return 0 on success, negative on failure - */ - int get_option(int optname, void *optval, unsigned *optlen); - - /** Close the socket - \param shutdown free the left-over data in message queues - */ - int close(bool shutdown=true); - -protected: - Socket(NetworkInterface *iface, nsapi_protocol_t proto); - - static void thunk(void *); - - NetworkInterface *_iface; - void *_socket; - bool _blocking; - unsigned _timeout; -}; - -#endif +/* Socket + * Copyright (c) 2015 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef SOCKET_H +#define SOCKET_H + +#include "SocketAddress.h" +#include "NetworkInterface.h" + +/** Abstract socket class + */ +class Socket { +public: + /** Socket lifetime + */ + virtual ~Socket(); + + /** Set blocking or non-blocking mode of the socket + * @param blocking true for blocking mode, false for non-blocking mode. + */ + void set_blocking(bool blocking); + + /** Set timeout on a socket operation if blocking behaviour is enabled + * @param timeout timeout in ms + */ + void set_timeout(unsigned int timeout); + + /* Set socket options + * @param optname Option ID + * @param optval Option value + * @param optlen Length of the option value + * @return 0 on success, negative on failure + */ + int set_option(int optname, const void *optval, unsigned optlen); + + /* Get socket options + * @param optname Option ID + * @param optval Buffer pointer where to write the option value + * @param optlen Length of the option value + * @return 0 on success, negative on failure + */ + int get_option(int optname, void *optval, unsigned *optlen); + + /** Close the socket + * @param shutdown free the left-over data in message queues + */ + int close(bool shutdown=true); + +protected: + Socket(NetworkInterface *iface, nsapi_protocol_t proto); + + static void thunk(void *); + + NetworkInterface *_iface; + void *_socket; + bool _blocking; + unsigned _timeout; +}; + +#endif
--- a/SocketAddress.cpp Wed Apr 06 13:50:19 2016 +0000 +++ b/SocketAddress.cpp Tue Apr 19 18:20:38 2016 -0500 @@ -1,67 +1,67 @@ -/* Socket - * Copyright (c) 2015 ARM Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "SocketAddress.h" -#include "NetworkInterface.h" -#include <string.h> -#include "mbed.h" - -SocketAddress::SocketAddress(NetworkInterface *iface, const char *host, uint16_t port) -{ - int err = iface->gethostbyname(host, _ip_address); - set_port(port); - - if (err) { - _ip_address[0] = '\0'; - _port = 0; - } -} - -SocketAddress::SocketAddress(const char *addr, uint16_t port) -{ - set_ip_address(addr); - set_port(port); -} - -SocketAddress::SocketAddress(const SocketAddress &addr) -{ - set_ip_address(addr.get_ip_address()); - set_port(addr.get_port()); -} - -void SocketAddress::set_ip_address(const char *addr) -{ - strncpy(_ip_address, addr, sizeof _ip_address); - _ip_address[sizeof _ip_address - 1] = '\0'; -} - -void SocketAddress::set_port(uint16_t port) -{ - _port = port; -} - -const char *SocketAddress::get_ip_address() const -{ - if (!_ip_address[0]) { - return 0; - } - return _ip_address; -} - -uint16_t SocketAddress::get_port() const -{ - return _port; -} +/* Socket + * Copyright (c) 2015 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "SocketAddress.h" +#include "NetworkInterface.h" +#include <string.h> +#include "mbed.h" + +SocketAddress::SocketAddress(NetworkInterface *iface, const char *host, uint16_t port) +{ + int err = iface->gethostbyname(host, _ip_address); + set_port(port); + + if (err) { + _ip_address[0] = '\0'; + _port = 0; + } +} + +SocketAddress::SocketAddress(const char *addr, uint16_t port) +{ + set_ip_address(addr); + set_port(port); +} + +SocketAddress::SocketAddress(const SocketAddress &addr) +{ + set_ip_address(addr.get_ip_address()); + set_port(addr.get_port()); +} + +void SocketAddress::set_ip_address(const char *addr) +{ + strncpy(_ip_address, addr, sizeof _ip_address); + _ip_address[sizeof _ip_address - 1] = '\0'; +} + +void SocketAddress::set_port(uint16_t port) +{ + _port = port; +} + +const char *SocketAddress::get_ip_address() const +{ + if (!_ip_address[0]) { + return 0; + } + return _ip_address; +} + +uint16_t SocketAddress::get_port() const +{ + return _port; +}
--- a/SocketAddress.h Wed Apr 06 13:50:19 2016 +0000 +++ b/SocketAddress.h Tue Apr 19 18:20:38 2016 -0500 @@ -1,83 +1,83 @@ -/* Socket - * Copyright (c) 2015 ARM Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef SOCKET_ADDRESS_H -#define SOCKET_ADDRESS_H - -#include <stdint.h> - -/** Maximum size of IP address -*/ -#define NSAPI_IP_SIZE 16 - -/** Maximum size of MAC address -*/ -#define NSAPI_MAC_SIZE 18 - -// Predeclared classes -class NetworkInterface; - -/** - * A general socket address composed of the IP address and port - */ -class SocketAddress { -public: - /** SocketAddress construction using DNS resolution - /param iface NetworkInterface to use for DNS resolution - /param addr Null-terminated hostname that will be resolved - /param port 16-bit port - /note on failure, IP address and port will be set to null - */ - SocketAddress(NetworkInterface *iface, const char *addr, uint16_t port = 0); - - /** SocketAddress construction - /param addr Null-terminated IP address - /param port 16-bit port - /note on failure, IP address and port will be set to null - */ - SocketAddress(const char *addr = 0, uint16_t port = 0); - - /** SocketAddress construction - /param addr SocketAddress to copy - */ - SocketAddress(const SocketAddress &addr); - - /** Set the IP address - \param addr Null-terminated string representing the IP address - */ - void set_ip_address(const char *addr); - - /** Set the port - \param port 16-bit port - */ - void set_port(uint16_t port); - - /** Get the IP address - \return The string representation of the IP Address - */ - const char *get_ip_address() const; - - /** Get the port - \return The 16-bit port - */ - uint16_t get_port(void) const; - -private: - char _ip_address[NSAPI_IP_SIZE]; - uint16_t _port; -}; - -#endif +/* Socket + * Copyright (c) 2015 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef SOCKET_ADDRESS_H +#define SOCKET_ADDRESS_H + +#include <stdint.h> + +/** Maximum size of IP address +*/ +#define NSAPI_IP_SIZE 16 + +/** Maximum size of MAC address +*/ +#define NSAPI_MAC_SIZE 18 + +// Predeclared classes +class NetworkInterface; + +/** + * A general socket address composed of the IP address and port + */ +class SocketAddress { +public: + /** SocketAddress construction using DNS resolution + * @param iface NetworkInterface to use for DNS resolution + * @param addr Null-terminated hostname that will be resolved + * @param port 16-bit port + * @note on failure, IP address and port will be set to null + */ + SocketAddress(NetworkInterface *iface, const char *addr, uint16_t port = 0); + + /** SocketAddress construction + * @param addr Null-terminated IP address + * @param port 16-bit port + * @note on failure, IP address and port will be set to null + */ + SocketAddress(const char *addr = 0, uint16_t port = 0); + + /** SocketAddress construction + * @param addr SocketAddress to copy + */ + SocketAddress(const SocketAddress &addr); + + /** Set the IP address + * @param addr Null-terminated string representing the IP address + */ + void set_ip_address(const char *addr); + + /** Set the port + * @param port 16-bit port + */ + void set_port(uint16_t port); + + /** Get the IP address + * @return The string representation of the IP Address + */ + const char *get_ip_address() const; + + /** Get the port + * @return The 16-bit port + */ + uint16_t get_port(void) const; + +private: + char _ip_address[NSAPI_IP_SIZE]; + uint16_t _port; +}; + +#endif
--- a/TCPServer.cpp Wed Apr 06 13:50:19 2016 +0000 +++ b/TCPServer.cpp Tue Apr 19 18:20:38 2016 -0500 @@ -1,87 +1,87 @@ -/* Socket - * Copyright (c) 2015 ARM Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "TCPServer.h" -#include "Timer.h" - -TCPServer::TCPServer(NetworkInterface *iface) - : Socket(iface, NSAPI_TCP) -{ -} - -int TCPServer::bind(uint16_t port) -{ - if (!_socket) { - return NSAPI_ERROR_NO_SOCKET; - } - - return _iface->socket_bind(_socket, port); -} - -int TCPServer::listen(int backlog) -{ - if (!_socket) { - return NSAPI_ERROR_NO_SOCKET; - } - - return _iface->socket_listen(_socket, backlog); -} - -int TCPServer::accept(TCPSocket *connection) -{ - mbed::Timer timer; - timer.start(); - - void *socket = connection->_socket; - connection->_socket = 0; - _iface->socket_destroy(socket); - - while (true) { - if (!_socket) { - return NSAPI_ERROR_NO_SOCKET; - } - - int err = _iface->socket_accept(_socket, &socket); - - if (err > 0) { - connection->_socket = socket; - } - - if (err != NSAPI_ERROR_WOULD_BLOCK || !_blocking || - (_timeout && timer.read_ms() > _timeout)) { - return err; - } - } -} - - -void TCPServer::attach_accept(mbed::FuncPtr<void()> callback) -{ - _accept_cb = callback; - - if (_socket && _accept_cb) { - return _iface->socket_attach_accept(_socket, Socket::thunk, &_accept_cb); - } else if (_socket) { - return _iface->socket_attach_accept(_socket, 0, 0); - } -} - -TCPServer::~TCPServer() -{ - if (_socket && _accept_cb) { - _iface->socket_attach_accept(_socket, 0, 0); - } -} +/* Socket + * Copyright (c) 2015 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "TCPServer.h" +#include "Timer.h" + +TCPServer::TCPServer(NetworkInterface *iface) + : Socket(iface, NSAPI_TCP) +{ +} + +int TCPServer::bind(uint16_t port) +{ + if (!_socket) { + return NSAPI_ERROR_NO_SOCKET; + } + + return _iface->socket_bind(_socket, port); +} + +int TCPServer::listen(int backlog) +{ + if (!_socket) { + return NSAPI_ERROR_NO_SOCKET; + } + + return _iface->socket_listen(_socket, backlog); +} + +int TCPServer::accept(TCPSocket *connection) +{ + mbed::Timer timer; + timer.start(); + + void *socket = connection->_socket; + connection->_socket = 0; + _iface->socket_destroy(socket); + + while (true) { + if (!_socket) { + return NSAPI_ERROR_NO_SOCKET; + } + + int err = _iface->socket_accept(_socket, &socket); + + if (err > 0) { + connection->_socket = socket; + } + + if (err != NSAPI_ERROR_WOULD_BLOCK || !_blocking || + (_timeout && timer.read_ms() > _timeout)) { + return err; + } + } +} + + +void TCPServer::attach_accept(FunctionPointer callback) +{ + _accept_cb = callback; + + if (_socket && _accept_cb) { + return _iface->socket_attach_accept(_socket, Socket::thunk, &_accept_cb); + } else if (_socket) { + return _iface->socket_attach_accept(_socket, 0, 0); + } +} + +TCPServer::~TCPServer() +{ + if (_socket && _accept_cb) { + _iface->socket_attach_accept(_socket, 0, 0); + } +}
--- a/TCPServer.h Wed Apr 06 13:50:19 2016 +0000 +++ b/TCPServer.h Tue Apr 19 18:20:38 2016 -0500 @@ -1,67 +1,67 @@ -/* Socket - * Copyright (c) 2015 ARM Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef TCPSERVER_H -#define TCPSERVER_H - -#include "Socket.h" -#include "TCPSocket.h" -#include "NetworkInterface.h" - -/** TCP Server. - */ -class TCPServer : public Socket { -public: - /** TCP Server lifetime - */ - TCPServer(NetworkInterface *iface); - virtual ~TCPServer(); - - /** Bind a socket to a specific port - \param port The port to listen for incoming connections on - \return 0 on success, negative on failure - */ - int bind(uint16_t port); - - /** Start listening for incoming connections - \param backlog Number of pending connections that can be queued up at any - one time [Default: 1] - \return 0 on success, negative on failure - */ - int listen(int backlog=1); - - /** Accept a new connection. - \param socket A TCPSocket instance that will handle the incoming connection. - \return 0 on success, negative on failure. - */ - int accept(TCPSocket *connection); - - /** Register a callback on when a new connection is ready - \param callback Function to call when accept will succeed, may be called in - interrupt context. - */ - void attach_accept(mbed::FuncPtr<void()> callback); - - template <typename T, typename M> - void attach_accept(T *tptr, M mptr) { - attach_accept(mbed::FuncPtr<void()>(tptr, mptr)); - } - -private: - mbed::FuncPtr<void()> _accept_cb; -}; - -#endif +/* Socket + * Copyright (c) 2015 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef TCPSERVER_H +#define TCPSERVER_H + +#include "Socket.h" +#include "TCPSocket.h" +#include "NetworkInterface.h" + +/** TCP Server. + */ +class TCPServer : public Socket { +public: + /** TCP Server lifetime + */ + TCPServer(NetworkInterface *iface); + virtual ~TCPServer(); + + /** Bind a socket to a specific port + * @param port The port to listen for incoming connections on + * @return 0 on success, negative on failure + */ + int bind(uint16_t port); + + /** Start listening for incoming connections + * @param backlog Number of pending connections that can be queued up at any + * one time [Default: 1] + * @return 0 on success, negative on failure + */ + int listen(int backlog=1); + + /** Accept a new connection. + * @param socket A TCPSocket instance that will handle the incoming connection. + * @return 0 on success, negative on failure. + */ + int accept(TCPSocket *connection); + + /** Register a callback on when a new connection is ready + * @param callback Function to call when accept will succeed, may be called in + * interrupt context. + */ + void attach_accept(FunctionPointer callback); + + template <typename T, typename M> + void attach_accept(T *tptr, M mptr) { + attach_accept(FunctionPointer(tptr, mptr)); + } + +private: + FunctionPointer _accept_cb; +}; + +#endif
--- a/TCPSocket.cpp Wed Apr 06 13:50:19 2016 +0000 +++ b/TCPSocket.cpp Tue Apr 19 18:20:38 2016 -0500 @@ -83,7 +83,7 @@ } -void TCPSocket::attach_send(mbed::FuncPtr<void()> callback) +void TCPSocket::attach_send(FunctionPointer callback) { _send_cb = callback; @@ -94,7 +94,7 @@ } } -void TCPSocket::attach_recv(mbed::FuncPtr<void()> callback) +void TCPSocket::attach_recv(FunctionPointer callback) { _recv_cb = callback;
--- a/TCPSocket.h Wed Apr 06 13:50:19 2016 +0000 +++ b/TCPSocket.h Tue Apr 19 18:20:38 2016 -0500 @@ -20,76 +20,75 @@ #include "Socket.h" #include "NetworkInterface.h" -/** -TCP socket connection -*/ +/** TCP socket connection + */ class TCPSocket : public Socket { public: /** TCP socket lifetime - */ + */ TCPSocket(NetworkInterface *iface); virtual ~TCPSocket(); /** Connects this TCP socket to the server - \param host The host to connect to. It can either be an IP Address - or a hostname that will be resolved with DNS - \param port The host's port to connect to - \return 0 on success, negative on failure - */ + * @param host The host to connect to. It can either be an IP Address + * or a hostname that will be resolved with DNS + * @param port The host's port to connect to + * @return 0 on success, negative on failure + */ int connect(const char *host, uint16_t port); /** Connects this TCP socket to the server - \param address SocketAddress to connect to - \return 0 on success, negative on failure - */ + * @param address SocketAddress to connect to + * @return 0 on success, negative on failure + */ int connect(const SocketAddress &address); /** Check if the socket is connected - \return true if connected, false otherwise - */ + * @return true if connected, false otherwise + */ bool is_connected(); /** Send data to the remote host - \param data The buffer to send to the host - \param size The length of the buffer to send - \return Number of written bytes on success, negative on failure - */ + * @param data The buffer to send to the host + * @param size The length of the buffer to send + * @return Number of written bytes on success, negative on failure + */ int send(const void *data, unsigned size); /** Receive data from the remote host - \param data The buffer in which to store the data received from the host - \param size The maximum length of the buffer - \return Number of received bytes on success, negative on failure - */ + * @param data The buffer in which to store the data received from the host + * @param size The maximum length of the buffer + * @return Number of received bytes on success, negative on failure + */ int recv(void *data, unsigned size); /** Register a callback on when send is ready - \param callback Function to call when send will succeed, may be called in - interrupt context. - */ - void attach_send(mbed::FuncPtr<void()> callback); + * @param callback Function to call when send will succeed, may be called in + * interrupt context. + */ + void attach_send(FunctionPointer callback); template <typename T, typename M> void attach_send(T *tptr, M mptr) { - attach_send(mbed::FuncPtr<void()>(tptr, mptr)); + attach_send(FunctionPointer(tptr, mptr)); } /** Register a callback on when recv is ready - \param callback Function to call when recv will succeed, may be called in - interrupt context. - */ - void attach_recv(mbed::FuncPtr<void()> callback); + * @param callback Function to call when recv will succeed, may be called in + * interrupt context. + */ + void attach_recv(FunctionPointer callback); template <typename T, typename M> void attach_recv(T *tptr, M mptr) { - attach_recv(mbed::FuncPtr<void()>(tptr, mptr)); + attach_recv(FunctionPointer(tptr, mptr)); } private: friend class TCPServer; - mbed::FuncPtr<void()> _send_cb; - mbed::FuncPtr<void()> _recv_cb; + FunctionPointer _send_cb; + FunctionPointer _recv_cb; }; #endif
--- a/UDPSocket.cpp Wed Apr 06 13:50:19 2016 +0000 +++ b/UDPSocket.cpp Tue Apr 19 18:20:38 2016 -0500 @@ -68,7 +68,7 @@ } -void UDPSocket::attach_send(mbed::FuncPtr<void()> callback) +void UDPSocket::attach_send(FunctionPointer callback) { _send_cb = callback; if (_socket && _send_cb) { @@ -78,7 +78,7 @@ } } -void UDPSocket::attach_recv(mbed::FuncPtr<void()> callback) +void UDPSocket::attach_recv(FunctionPointer callback) { _recv_cb = callback; if (_socket && _recv_cb) {
--- a/UDPSocket.h Wed Apr 06 13:50:19 2016 +0000 +++ b/UDPSocket.h Tue Apr 19 18:20:38 2016 -0500 @@ -20,65 +20,64 @@ #include "Socket.h" #include "NetworkInterface.h" -/** -UDP Socket -*/ +/** UDP Socket + */ class UDPSocket : public Socket { public: /** UDPSocket lifetime - */ + */ UDPSocket(NetworkInterface *iface); virtual ~UDPSocket(); /** Bind a UDP Server Socket to a specific port - \param port The port to listen for incoming connections on - \return 0 on success, negative on failure. - */ + * @param port The port to listen for incoming connections on + * @return 0 on success, negative on failure. + */ int bind(uint16_t port); /** Send a packet to a remote endpoint - \param address The remote SocketAddress - \param data The packet to be sent - \param size The length of the packet to be sent - \return the number of written bytes on success, negative on failure - */ + * @param address The remote SocketAddress + * @param data The packet to be sent + * @param size The length of the packet to be sent + * @return The number of written bytes on success, negative on failure + */ int sendto(const SocketAddress &address, const void *data, unsigned size); /** Receive a packet from a remote endpoint - \param address Destination for the remote SocketAddress or null - \param buffer The buffer for storing the incoming packet data - If a packet is too long to fit in the supplied buffer, - excess bytes are discarded - \param size The length of the buffer - \return the number of received bytes on success, negative on failure - */ + * @param address Destination for the remote SocketAddress or null + * @param buffer The buffer for storing the incoming packet data + * If a packet is too long to fit in the supplied buffer, + * excess bytes are discarded + * @param size The length of the buffer + * @return The number of received bytes on success, negative on failure + */ int recvfrom(SocketAddress *address, void *buffer, unsigned size); /** Register a callback on when send is ready - \param callback Function to call when send will succeed, may be called in - interrupt context. - */ - void attach_send(mbed::FuncPtr<void()> callback); + * @param callback Function to call when send will succeed, may be called in + * interrupt context. + */ + void attach_send(FunctionPointer callback); template <typename T, typename M> void attach_send(T *tptr, M mptr) { - attach_send(mbed::FuncPtr<void()>(tptr, mptr)); + attach_send(FunctionPointer(tptr, mptr)); } /** Register a callback on when recv is ready - \param callback Function to call when recv will succeed, may be called in - interrupt context. - */ - void attach_recv(mbed::FuncPtr<void()> callback); + * @param callback Function to call when recv will succeed, may be called in + * interrupt context. + */ + void attach_recv(FunctionPointer callback); template <typename T, typename M> void attach_recv(T *tptr, M mptr) { - attach_recv(mbed::FuncPtr<void()>(tptr, mptr)); + attach_recv(FunctionPointer(tptr, mptr)); } private: - mbed::FuncPtr<void()> _send_cb; - mbed::FuncPtr<void()> _recv_cb; + FunctionPointer _send_cb; + FunctionPointer _recv_cb; }; #endif
--- a/WiFiInterface.h Wed Apr 06 13:50:19 2016 +0000 +++ b/WiFiInterface.h Tue Apr 19 18:20:38 2016 -0500 @@ -1,51 +1,51 @@ -/* Socket - * Copyright (c) 2015 ARM Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef WIFI_INTERFACE_H -#define WIFI_INTERFACE_H - -#include "NetworkInterface.h" - -/** Enum for WiFi encryption types -*/ -enum nsapi_security_t { - NSAPI_SECURITY_NONE = 0, /*!< open access point */ - NSAPI_SECURITY_WEP, /*!< phrase conforms to WEP */ - NSAPI_SECURITY_WPA, /*!< phrase conforms to WPA */ - NSAPI_SECURITY_WPA2, /*!< phrase conforms to WPA2 */ -}; - -/** WiFiInterface class - * Common interface that is shared between WiFi devices - */ -class WiFiInterface : public NetworkInterface -{ -public: - /** Start the interface - /param ssid Name of the network to connect to - /param pass Security passphrase to connect to the network - /param security Type of encryption for connection - /return 0 on success, negative on failure - */ - virtual int connect(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE) = 0; - - /** Stop the interface - /return 0 on success, negative on failure - */ - virtual int disconnect() = 0; -}; - -#endif +/* Socket + * Copyright (c) 2015 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef WIFI_INTERFACE_H +#define WIFI_INTERFACE_H + +#include "NetworkInterface.h" + +/** Enum for WiFi encryption types + */ +enum nsapi_security_t { + NSAPI_SECURITY_NONE = 0, /*!< open access point */ + NSAPI_SECURITY_WEP, /*!< phrase conforms to WEP */ + NSAPI_SECURITY_WPA, /*!< phrase conforms to WPA */ + NSAPI_SECURITY_WPA2, /*!< phrase conforms to WPA2 */ +}; + +/** WiFiInterface class + * Common interface that is shared between WiFi devices + */ +class WiFiInterface : public NetworkInterface +{ +public: + /** Start the interface + * @param ssid Name of the network to connect to + * @param pass Security passphrase to connect to the network + * @param security Type of encryption for connection + * @return 0 on success, negative on failure + */ + virtual int connect(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE) = 0; + + /** Stop the interface + * @return 0 on success, negative on failure + */ + virtual int disconnect() = 0; +}; + +#endif