Implementation of the NetworkSocketAPI for LWIP

Dependencies:   lwip-eth lwip-sys lwip

Dependents:   HelloLWIPInterface HelloLWIPInterfaceNonBlocking LWIPInterfaceTests SimpleHTTPExample ... more

Committer:
Christopher Haster
Date:
Thu Apr 21 18:23:35 2016 -0500
Revision:
21:9600bd29a088
Parent:
19:366181e36f99
Add rudimentary support for server side

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Christopher Haster 17:7db2f5d503d4 1 /* LWIP implementation of NetworkInterfaceAPI
Christopher Haster 17:7db2f5d503d4 2 * Copyright (c) 2015 ARM Limited
Christopher Haster 17:7db2f5d503d4 3 *
Christopher Haster 17:7db2f5d503d4 4 * Licensed under the Apache License, Version 2.0 (the "License");
Christopher Haster 17:7db2f5d503d4 5 * you may not use this file except in compliance with the License.
Christopher Haster 17:7db2f5d503d4 6 * You may obtain a copy of the License at
Christopher Haster 17:7db2f5d503d4 7 *
Christopher Haster 17:7db2f5d503d4 8 * http://www.apache.org/licenses/LICENSE-2.0
Christopher Haster 17:7db2f5d503d4 9 *
Christopher Haster 17:7db2f5d503d4 10 * Unless required by applicable law or agreed to in writing, software
Christopher Haster 17:7db2f5d503d4 11 * distributed under the License is distributed on an "AS IS" BASIS,
Christopher Haster 17:7db2f5d503d4 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Christopher Haster 17:7db2f5d503d4 13 * See the License for the specific language governing permissions and
Christopher Haster 17:7db2f5d503d4 14 * limitations under the License.
Christopher Haster 17:7db2f5d503d4 15 */
Christopher Haster 17:7db2f5d503d4 16
Christopher Haster 17:7db2f5d503d4 17 #ifndef LWIP_INTERFACE_H
Christopher Haster 17:7db2f5d503d4 18 #define LWIP_INTERFACE_H
Christopher Haster 17:7db2f5d503d4 19
Christopher Haster 19:366181e36f99 20 #include "EthernetInterface.h"
Christopher Haster 17:7db2f5d503d4 21 #include "rtos.h"
Christopher Haster 17:7db2f5d503d4 22 #include "lwip/netif.h"
Christopher Haster 17:7db2f5d503d4 23
Christopher Haster 17:7db2f5d503d4 24
Christopher Haster 17:7db2f5d503d4 25 /** LWIPInterface class
Christopher Haster 17:7db2f5d503d4 26 * Implementation of the NetworkStack for LWIP
Christopher Haster 17:7db2f5d503d4 27 */
Christopher Haster 19:366181e36f99 28 class LWIPInterface : public NetworkStack, public EthernetInterface
Christopher Haster 17:7db2f5d503d4 29 {
Christopher Haster 17:7db2f5d503d4 30 public:
Christopher Haster 17:7db2f5d503d4 31 /** Start the interface
Christopher Haster 17:7db2f5d503d4 32 * @return 0 on success, negative on failure
Christopher Haster 17:7db2f5d503d4 33 */
Christopher Haster 17:7db2f5d503d4 34 virtual int connect();
Christopher Haster 17:7db2f5d503d4 35
Christopher Haster 17:7db2f5d503d4 36 /** Stop the interface
Christopher Haster 17:7db2f5d503d4 37 * @return 0 on success, negative on failure
Christopher Haster 17:7db2f5d503d4 38 */
Christopher Haster 17:7db2f5d503d4 39 virtual int disconnect();
Christopher Haster 17:7db2f5d503d4 40
Christopher Haster 17:7db2f5d503d4 41 /** Get the internally stored IP address
Christopher Haster 17:7db2f5d503d4 42 * @return IP address of the interface or null if not yet connected
Christopher Haster 17:7db2f5d503d4 43 */
Christopher Haster 17:7db2f5d503d4 44 virtual const char *get_ip_address();
Christopher Haster 17:7db2f5d503d4 45
Christopher Haster 17:7db2f5d503d4 46 /** Get the internally stored MAC address
Christopher Haster 17:7db2f5d503d4 47 * @return MAC address of the interface
Christopher Haster 17:7db2f5d503d4 48 */
Christopher Haster 17:7db2f5d503d4 49 virtual const char *get_mac_address();
Christopher Haster 17:7db2f5d503d4 50
Christopher Haster 17:7db2f5d503d4 51 protected:
Christopher Haster 17:7db2f5d503d4 52 /** Open a socket
Christopher Haster 17:7db2f5d503d4 53 * @param handle Handle in which to store new socket
Christopher Haster 17:7db2f5d503d4 54 * @param proto Type of socket to open, NSAPI_TCP or NSAPI_UDP
Christopher Haster 17:7db2f5d503d4 55 * @return 0 on success, negative on failure
Christopher Haster 17:7db2f5d503d4 56 */
Christopher Haster 17:7db2f5d503d4 57 virtual int socket_open(void **handle, nsapi_protocol_t proto);
Christopher Haster 17:7db2f5d503d4 58
Christopher Haster 17:7db2f5d503d4 59 /** Close the socket
Christopher Haster 17:7db2f5d503d4 60 * @param handle Socket handle
Christopher Haster 17:7db2f5d503d4 61 * @return 0 on success, negative on failure
Christopher Haster 17:7db2f5d503d4 62 * @note On failure, any memory associated with the socket must still
Christopher Haster 17:7db2f5d503d4 63 * be cleaned up
Christopher Haster 17:7db2f5d503d4 64 */
Christopher Haster 17:7db2f5d503d4 65 virtual int socket_close(void *handle);
Christopher Haster 17:7db2f5d503d4 66
Christopher Haster 17:7db2f5d503d4 67 /** Bind a server socket to a specific port
Christopher Haster 17:7db2f5d503d4 68 * @param handle Socket handle
Christopher Haster 17:7db2f5d503d4 69 * @param address Local address to listen for incoming connections on
Christopher Haster 17:7db2f5d503d4 70 * @return 0 on success, negative on failure.
Christopher Haster 17:7db2f5d503d4 71 */
Christopher Haster 17:7db2f5d503d4 72 virtual int socket_bind(void *handle, const SocketAddress &address);
Christopher Haster 17:7db2f5d503d4 73
Christopher Haster 17:7db2f5d503d4 74 /** Start listening for incoming connections
Christopher Haster 17:7db2f5d503d4 75 * @param handle Socket handle
Christopher Haster 17:7db2f5d503d4 76 * @param backlog Number of pending connections that can be queued up at any
Christopher Haster 17:7db2f5d503d4 77 * one time [Default: 1]
Christopher Haster 17:7db2f5d503d4 78 * @return 0 on success, negative on failure
Christopher Haster 17:7db2f5d503d4 79 */
Christopher Haster 17:7db2f5d503d4 80 virtual int socket_listen(void *handle, int backlog);
Christopher Haster 17:7db2f5d503d4 81
Christopher Haster 17:7db2f5d503d4 82 /** Connects this TCP socket to the server
Christopher Haster 17:7db2f5d503d4 83 * @param handle Socket handle
Christopher Haster 17:7db2f5d503d4 84 * @param address SocketAddress to connect to
Christopher Haster 17:7db2f5d503d4 85 * @return 0 on success, negative on failure
Christopher Haster 17:7db2f5d503d4 86 */
Christopher Haster 17:7db2f5d503d4 87 virtual int socket_connect(void *handle, const SocketAddress &address);
Christopher Haster 17:7db2f5d503d4 88
Christopher Haster 17:7db2f5d503d4 89 /** Accept a new connection.
Christopher Haster 17:7db2f5d503d4 90 * @param handle Handle in which to store new socket
Christopher Haster 17:7db2f5d503d4 91 * @param server Socket handle to server to accept from
Christopher Haster 17:7db2f5d503d4 92 * @return 0 on success, negative on failure
Christopher Haster 17:7db2f5d503d4 93 * @note This call is not-blocking, if this call would block, must
Christopher Haster 17:7db2f5d503d4 94 * immediately return NSAPI_ERROR_WOULD_WAIT
Christopher Haster 17:7db2f5d503d4 95 */
Christopher Haster 17:7db2f5d503d4 96 virtual int socket_accept(void **handle, void *server);
Christopher Haster 17:7db2f5d503d4 97
Christopher Haster 17:7db2f5d503d4 98 /** Send data to the remote host
Christopher Haster 17:7db2f5d503d4 99 * @param handle Socket handle
Christopher Haster 17:7db2f5d503d4 100 * @param data The buffer to send to the host
Christopher Haster 17:7db2f5d503d4 101 * @param size The length of the buffer to send
Christopher Haster 17:7db2f5d503d4 102 * @return Number of written bytes on success, negative on failure
Christopher Haster 17:7db2f5d503d4 103 * @note This call is not-blocking, if this call would block, must
Christopher Haster 17:7db2f5d503d4 104 * immediately return NSAPI_ERROR_WOULD_WAIT
Christopher Haster 17:7db2f5d503d4 105 */
Christopher Haster 17:7db2f5d503d4 106 virtual int socket_send(void *handle, const void *data, unsigned size);
Christopher Haster 17:7db2f5d503d4 107
Christopher Haster 17:7db2f5d503d4 108 /** Receive data from the remote host
Christopher Haster 17:7db2f5d503d4 109 * @param handle Socket handle
Christopher Haster 17:7db2f5d503d4 110 * @param data The buffer in which to store the data received from the host
Christopher Haster 17:7db2f5d503d4 111 * @param size The maximum length of the buffer
Christopher Haster 17:7db2f5d503d4 112 * @return Number of received bytes on success, negative on failure
Christopher Haster 17:7db2f5d503d4 113 * @note This call is not-blocking, if this call would block, must
Christopher Haster 17:7db2f5d503d4 114 * immediately return NSAPI_ERROR_WOULD_WAIT
Christopher Haster 17:7db2f5d503d4 115 */
Christopher Haster 17:7db2f5d503d4 116 virtual int socket_recv(void *handle, void *data, unsigned size);
Christopher Haster 17:7db2f5d503d4 117
Christopher Haster 17:7db2f5d503d4 118 /** Send a packet to a remote endpoint
Christopher Haster 17:7db2f5d503d4 119 * @param handle Socket handle
Christopher Haster 17:7db2f5d503d4 120 * @param address The remote SocketAddress
Christopher Haster 17:7db2f5d503d4 121 * @param data The packet to be sent
Christopher Haster 17:7db2f5d503d4 122 * @param size The length of the packet to be sent
Christopher Haster 17:7db2f5d503d4 123 * @return the number of written bytes on success, negative on failure
Christopher Haster 17:7db2f5d503d4 124 * @note This call is not-blocking, if this call would block, must
Christopher Haster 17:7db2f5d503d4 125 * immediately return NSAPI_ERROR_WOULD_WAIT
Christopher Haster 17:7db2f5d503d4 126 */
Christopher Haster 17:7db2f5d503d4 127 virtual int socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size);
Christopher Haster 17:7db2f5d503d4 128
Christopher Haster 17:7db2f5d503d4 129 /** Receive a packet from a remote endpoint
Christopher Haster 17:7db2f5d503d4 130 * @param handle Socket handle
Christopher Haster 17:7db2f5d503d4 131 * @param address Destination for the remote SocketAddress or null
Christopher Haster 17:7db2f5d503d4 132 * @param buffer The buffer for storing the incoming packet data
Christopher Haster 17:7db2f5d503d4 133 * If a packet is too long to fit in the supplied buffer,
Christopher Haster 17:7db2f5d503d4 134 * excess bytes are discarded
Christopher Haster 17:7db2f5d503d4 135 * @param size The length of the buffer
Christopher Haster 17:7db2f5d503d4 136 * @return the number of received bytes on success, negative on failure
Christopher Haster 17:7db2f5d503d4 137 * @note This call is not-blocking, if this call would block, must
Christopher Haster 17:7db2f5d503d4 138 * immediately return NSAPI_ERROR_WOULD_WAIT
Christopher Haster 17:7db2f5d503d4 139 */
Christopher Haster 17:7db2f5d503d4 140 virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size);
Christopher Haster 17:7db2f5d503d4 141
Christopher Haster 17:7db2f5d503d4 142 /** Register a callback on state change of the socket
Christopher Haster 17:7db2f5d503d4 143 * @param handle Socket handle
Christopher Haster 17:7db2f5d503d4 144 * @param callback Function to call on state change
Christopher Haster 17:7db2f5d503d4 145 * @param data Argument to pass to callback
Christopher Haster 17:7db2f5d503d4 146 * @note Callback may be called in an interrupt context.
Christopher Haster 17:7db2f5d503d4 147 */
Christopher Haster 17:7db2f5d503d4 148 virtual void socket_attach(void *handle, void (*callback)(void *), void *data);
Christopher Haster 17:7db2f5d503d4 149 };
Christopher Haster 17:7db2f5d503d4 150
Christopher Haster 17:7db2f5d503d4 151 #endif