Implementation of the NetworkSocketAPI for LWIP

Dependencies:   lwip-eth lwip-sys lwip

Dependents:   HelloLWIPInterface HelloLWIPInterfaceNonBlocking LWIPInterfaceTests SimpleHTTPExample ... more

Committer:
geky
Date:
Tue Apr 05 19:20:42 2016 +0000
Revision:
12:899403b675fe
Parent:
8:cef01e812975
Child:
13:57d9e1721826
Added compilable pieces

Who changed what in which revision?

UserRevisionLine numberNew contents of line
geky 1:2fbcfc9c12dd 1 /* LWIP implementation of NetworkInterfaceAPI
geky 1:2fbcfc9c12dd 2 * Copyright (c) 2015 ARM Limited
geky 1:2fbcfc9c12dd 3 *
geky 1:2fbcfc9c12dd 4 * Licensed under the Apache License, Version 2.0 (the "License");
geky 1:2fbcfc9c12dd 5 * you may not use this file except in compliance with the License.
geky 1:2fbcfc9c12dd 6 * You may obtain a copy of the License at
geky 1:2fbcfc9c12dd 7 *
geky 1:2fbcfc9c12dd 8 * http://www.apache.org/licenses/LICENSE-2.0
geky 1:2fbcfc9c12dd 9 *
geky 1:2fbcfc9c12dd 10 * Unless required by applicable law or agreed to in writing, software
geky 1:2fbcfc9c12dd 11 * distributed under the License is distributed on an "AS IS" BASIS,
geky 1:2fbcfc9c12dd 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
geky 1:2fbcfc9c12dd 13 * See the License for the specific language governing permissions and
geky 1:2fbcfc9c12dd 14 * limitations under the License.
geky 1:2fbcfc9c12dd 15 */
geky 1:2fbcfc9c12dd 16
geky 12:899403b675fe 17 #if 0
geky 12:899403b675fe 18 //#ifndef LWIP_INTERFACE_H
geky 1:2fbcfc9c12dd 19 #define LWIP_INTERFACE_H
geky 1:2fbcfc9c12dd 20
geky 1:2fbcfc9c12dd 21 #include "EthernetInterface.h"
Christopher Haster 2:7fb7e78cb17f 22 #include "rtos.h"
Christopher Haster 2:7fb7e78cb17f 23 #include "lwip/netif.h"
geky 1:2fbcfc9c12dd 24
geky 1:2fbcfc9c12dd 25
geky 1:2fbcfc9c12dd 26 /** LWIPInterface class
geky 1:2fbcfc9c12dd 27 * Implementation of the NetworkInterface for LWIP
geky 1:2fbcfc9c12dd 28 */
geky 1:2fbcfc9c12dd 29 class LWIPInterface : public EthernetInterface
geky 1:2fbcfc9c12dd 30 {
geky 1:2fbcfc9c12dd 31 public:
geky 12:899403b675fe 32 virtual ~NetworkInterface() {};
sam_grove 7:08d5a40ae448 33
geky 12:899403b675fe 34 /** Get the internally stored IP address
geky 12:899403b675fe 35 /return IP address of the interface or null if not yet connected
geky 12:899403b675fe 36 */
geky 12:899403b675fe 37 virtual const char *get_ip_address() = 0;
geky 12:899403b675fe 38
geky 12:899403b675fe 39 /** Get the internally stored MAC address
geky 12:899403b675fe 40 /return MAC address of the interface
geky 12:899403b675fe 41 */
geky 12:899403b675fe 42 virtual const char *get_mac_address() = 0;
geky 12:899403b675fe 43
geky 12:899403b675fe 44 /** Get the current status of the interface
geky 12:899403b675fe 45 /return true if connected
geky 12:899403b675fe 46 */
geky 12:899403b675fe 47 virtual bool is_connected() {
geky 12:899403b675fe 48 return get_ip_address() != NULL;
geky 12:899403b675fe 49 }
geky 12:899403b675fe 50
geky 12:899403b675fe 51 /** Looks up the specified host's IP address
geky 12:899403b675fe 52 /param name Hostname to lookup
geky 12:899403b675fe 53 /param dest Destination for IP address, must have space for SocketAddress::IP_SIZE
geky 12:899403b675fe 54 /return 0 on success, negative on failure
geky 12:899403b675fe 55 */
geky 12:899403b675fe 56 virtual int gethostbyname(const char *name, char *dest);
geky 12:899403b675fe 57
geky 12:899403b675fe 58 protected:
geky 12:899403b675fe 59 friend class Socket;
geky 12:899403b675fe 60 friend class UDPSocket;
geky 12:899403b675fe 61 friend class TCPSocket;
geky 12:899403b675fe 62 friend class TCPServer;
geky 12:899403b675fe 63
geky 12:899403b675fe 64 /** Enum of socket protocols
geky 12:899403b675fe 65 /enum protocol_t
geky 12:899403b675fe 66 */
geky 12:899403b675fe 67 enum protocol_t {
geky 12:899403b675fe 68 TCP, /*!< Socket is of TCP type */
geky 12:899403b675fe 69 UDP, /*!< Socket is of UDP type */
geky 12:899403b675fe 70 };
geky 1:2fbcfc9c12dd 71
geky 12:899403b675fe 72 /** Create a socket
geky 12:899403b675fe 73 /param proto The type of socket to open, TCP or UDP
geky 12:899403b675fe 74 /return The alocated socket or null on failure
geky 12:899403b675fe 75 */
geky 12:899403b675fe 76 virtual void *socket_create(protocol_t proto) = 0;
geky 12:899403b675fe 77
geky 12:899403b675fe 78 /** Destroy a socket
geky 12:899403b675fe 79 /param socket Previously allocated socket
geky 12:899403b675fe 80 */
geky 12:899403b675fe 81 virtual void socket_destroy(void *handle) = 0;
geky 12:899403b675fe 82
geky 12:899403b675fe 83 /** Set socket options
geky 12:899403b675fe 84 \param handle Socket handle
geky 12:899403b675fe 85 \param optname Option ID
geky 12:899403b675fe 86 \param optval Option value
geky 12:899403b675fe 87 \param optlen Length of the option value
geky 12:899403b675fe 88 \return 0 on success, negative on failure
geky 12:899403b675fe 89 */
geky 12:899403b675fe 90 virtual int socket_set_option(void *handle, int optname, const void *optval, unsigned int optlen) = 0;
geky 1:2fbcfc9c12dd 91
geky 12:899403b675fe 92 /** Get socket options
geky 12:899403b675fe 93 \param handle Socket handle
geky 12:899403b675fe 94 \param optname Option ID
geky 12:899403b675fe 95 \param optval Buffer pointer where to write the option value
geky 12:899403b675fe 96 \param optlen Length of the option value
geky 12:899403b675fe 97 \return 0 on success, negative on failure
geky 12:899403b675fe 98 */
geky 12:899403b675fe 99 virtual int socket_get_option(void *handle, int optname, void *optval, unsigned int *optlen) = 0;
sam_grove 7:08d5a40ae448 100
geky 12:899403b675fe 101 /** Bind a server socket to a specific port
geky 12:899403b675fe 102 \param handle Socket handle
geky 12:899403b675fe 103 \param port The port to listen for incoming connections on
geky 12:899403b675fe 104 \return 0 on success, negative on failure.
geky 12:899403b675fe 105 */
geky 12:899403b675fe 106 virtual int socket_bind(void *handle, int port) = 0;
geky 12:899403b675fe 107
geky 12:899403b675fe 108 /** Start listening for incoming connections
geky 12:899403b675fe 109 \param handle Socket handle
geky 12:899403b675fe 110 \param backlog Number of pending connections that can be queued up at any
geky 12:899403b675fe 111 one time [Default: 1]
geky 12:899403b675fe 112 \return 0 on success, negative on failure
geky 12:899403b675fe 113 */
geky 12:899403b675fe 114 virtual int socket_listen(void *handle, int backlog) = 0;
sam_grove 8:cef01e812975 115
geky 12:899403b675fe 116 /** Connects this TCP socket to the server
geky 12:899403b675fe 117 \param handle Socket handle
geky 12:899403b675fe 118 \param address SocketAddress to connect to
geky 12:899403b675fe 119 \return 0 on success, negative on failure
geky 12:899403b675fe 120 */
geky 12:899403b675fe 121 virtual int socket_connect(void *handle, const SocketAddress &address) = 0;
geky 12:899403b675fe 122
geky 12:899403b675fe 123 /** Check if the socket is connected
geky 12:899403b675fe 124 \param handle Socket handle
geky 12:899403b675fe 125 \return true if connected, false otherwise
geky 12:899403b675fe 126 */
geky 12:899403b675fe 127 virtual bool socket_is_connected(void *handle) = 0;
sam_grove 8:cef01e812975 128
geky 12:899403b675fe 129 /** Accept a new connection.
geky 12:899403b675fe 130 \param handle Socket handle
geky 12:899403b675fe 131 \param socket A TCPSocket instance that will handle the incoming connection.
geky 12:899403b675fe 132 \return 0 on success, negative on failure.
geky 12:899403b675fe 133 \note This call is not-blocking, if this call would block, must
geky 12:899403b675fe 134 immediately return NSAPI_ERROR_WOULD_WAIT
geky 12:899403b675fe 135 */
geky 12:899403b675fe 136 virtual int socket_accept(void *handle, void **connection) = 0;
geky 12:899403b675fe 137
geky 12:899403b675fe 138 /** Send data to the remote host
geky 12:899403b675fe 139 \param handle Socket handle
geky 12:899403b675fe 140 \param data The buffer to send to the host
geky 12:899403b675fe 141 \param size The length of the buffer to send
geky 12:899403b675fe 142 \return Number of written bytes on success, negative on failure
geky 12:899403b675fe 143 \note This call is not-blocking, if this call would block, must
geky 12:899403b675fe 144 immediately return NSAPI_ERROR_WOULD_WAIT
geky 12:899403b675fe 145 */
geky 12:899403b675fe 146 virtual int socket_send(void *handle, const void *data, unsigned size) = 0;
geky 12:899403b675fe 147
geky 12:899403b675fe 148 /** Receive data from the remote host
geky 12:899403b675fe 149 \param handle Socket handle
geky 12:899403b675fe 150 \param data The buffer in which to store the data received from the host
geky 12:899403b675fe 151 \param size The maximum length of the buffer
geky 12:899403b675fe 152 \return Number of received bytes on success, negative on failure
geky 12:899403b675fe 153 \note This call is not-blocking, if this call would block, must
geky 12:899403b675fe 154 immediately return NSAPI_ERROR_WOULD_WAIT
geky 12:899403b675fe 155 */
geky 12:899403b675fe 156 virtual int socket_recv(void *handle, void *data, unsigned size) = 0;
geky 4:a7349bd7776c 157
geky 12:899403b675fe 158 /** Send a packet to a remote endpoint
geky 12:899403b675fe 159 \param handle Socket handle
geky 12:899403b675fe 160 \param address The remote SocketAddress
geky 12:899403b675fe 161 \param data The packet to be sent
geky 12:899403b675fe 162 \param size The length of the packet to be sent
geky 12:899403b675fe 163 \return the number of written bytes on success, negative on failure
geky 12:899403b675fe 164 \note This call is not-blocking, if this call would block, must
geky 12:899403b675fe 165 immediately return NSAPI_ERROR_WOULD_WAIT
geky 12:899403b675fe 166 */
geky 12:899403b675fe 167 virtual int socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size) = 0;
geky 12:899403b675fe 168
geky 12:899403b675fe 169 /** Receive a packet from a remote endpoint
geky 12:899403b675fe 170 \param handle Socket handle
geky 12:899403b675fe 171 \param address Destination for the remote SocketAddress or null
geky 12:899403b675fe 172 \param buffer The buffer for storing the incoming packet data
geky 12:899403b675fe 173 If a packet is too long to fit in the supplied buffer,
geky 12:899403b675fe 174 excess bytes are discarded
geky 12:899403b675fe 175 \param size The length of the buffer
geky 12:899403b675fe 176 \return the number of received bytes on success, negative on failure
geky 12:899403b675fe 177 \note This call is not-blocking, if this call would block, must
geky 12:899403b675fe 178 immediately return NSAPI_ERROR_WOULD_WAIT
geky 12:899403b675fe 179 */
geky 12:899403b675fe 180 virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size) = 0;
geky 4:a7349bd7776c 181
geky 12:899403b675fe 182 /** Close the socket
geky 12:899403b675fe 183 \param handle Socket handle
geky 12:899403b675fe 184 \param shutdown free the left-over data in message queues
geky 12:899403b675fe 185 */
geky 12:899403b675fe 186 virtual int socket_close(void *handle, bool shutdown) = 0;
geky 12:899403b675fe 187
geky 12:899403b675fe 188 /** Register a callback on when a new connection is ready
geky 12:899403b675fe 189 \param handle Socket handle
geky 12:899403b675fe 190 \param callback Function to call when accept will succeed, may be called in
geky 12:899403b675fe 191 interrupt context.
geky 12:899403b675fe 192 \param id Argument to pass to callback
geky 12:899403b675fe 193 */
geky 12:899403b675fe 194 virtual void socket_attach_accept(void *handle, void (*callback)(void *), void *id) = 0;
geky 12:899403b675fe 195
geky 12:899403b675fe 196 /** Register a callback on when send is ready
geky 12:899403b675fe 197 \param handle Socket handle
geky 12:899403b675fe 198 \param callback Function to call when accept will succeed, may be called in
geky 12:899403b675fe 199 interrupt context.
geky 12:899403b675fe 200 \param id Argument to pass to callback
geky 12:899403b675fe 201 */
geky 12:899403b675fe 202 virtual void socket_attach_send(void *handle, void (*callback)(void *), void *id) = 0;
geky 12:899403b675fe 203
geky 12:899403b675fe 204 /** Register a callback on when recv is ready
geky 12:899403b675fe 205 \param handle Socket handle
geky 12:899403b675fe 206 \param callback Function to call when accept will succeed, may be called in
geky 12:899403b675fe 207 interrupt context.
geky 12:899403b675fe 208 \param id Argument to pass to callback
geky 12:899403b675fe 209 */
geky 12:899403b675fe 210 virtual void socket_attach_recv(void *handle, void (*callback)(void *), void *id) = 0;
geky 1:2fbcfc9c12dd 211 };
geky 1:2fbcfc9c12dd 212
geky 1:2fbcfc9c12dd 213 #endif