Implementation of the NetworkSocketAPI for LWIP

Dependencies:   lwip-eth lwip-sys lwip

Dependents:   HelloLWIPInterface HelloLWIPInterfaceNonBlocking LWIPInterfaceTests SimpleHTTPExample ... more

Committer:
geky
Date:
Wed Apr 06 00:20:35 2016 +0000
Revision:
15:0d8d1dafe064
Parent:
13:57d9e1721826
Child:
17:7db2f5d503d4
Refactored enums

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
Christopher Haster 13:57d9e1721826 17 #ifndef LWIP_INTERFACE_H
geky 1:2fbcfc9c12dd 18 #define LWIP_INTERFACE_H
geky 1:2fbcfc9c12dd 19
geky 1:2fbcfc9c12dd 20 #include "EthernetInterface.h"
Christopher Haster 2:7fb7e78cb17f 21 #include "rtos.h"
Christopher Haster 2:7fb7e78cb17f 22 #include "lwip/netif.h"
geky 1:2fbcfc9c12dd 23
geky 1:2fbcfc9c12dd 24
geky 1:2fbcfc9c12dd 25 /** LWIPInterface class
geky 1:2fbcfc9c12dd 26 * Implementation of the NetworkInterface for LWIP
geky 1:2fbcfc9c12dd 27 */
geky 1:2fbcfc9c12dd 28 class LWIPInterface : public EthernetInterface
geky 1:2fbcfc9c12dd 29 {
geky 1:2fbcfc9c12dd 30 public:
Christopher Haster 13:57d9e1721826 31 /** Start the interface
Christopher Haster 13:57d9e1721826 32 * @return 0 on success, negative on failure
Christopher Haster 13:57d9e1721826 33 */
Christopher Haster 13:57d9e1721826 34 virtual int connect();
Christopher Haster 13:57d9e1721826 35
Christopher Haster 13:57d9e1721826 36 /** Stop the interface
Christopher Haster 13:57d9e1721826 37 * @return 0 on success, negative on failure
Christopher Haster 13:57d9e1721826 38 */
Christopher Haster 13:57d9e1721826 39 virtual int disconnect();
sam_grove 7:08d5a40ae448 40
geky 12:899403b675fe 41 /** Get the internally stored IP address
geky 12:899403b675fe 42 /return IP address of the interface or null if not yet connected
geky 12:899403b675fe 43 */
Christopher Haster 13:57d9e1721826 44 virtual const char *get_ip_address();
geky 12:899403b675fe 45
geky 12:899403b675fe 46 /** Get the internally stored MAC address
geky 12:899403b675fe 47 /return MAC address of the interface
geky 12:899403b675fe 48 */
Christopher Haster 13:57d9e1721826 49 virtual const char *get_mac_address();
geky 12:899403b675fe 50
geky 12:899403b675fe 51 protected:
geky 12:899403b675fe 52 /** Create a socket
geky 12:899403b675fe 53 /param proto The type of socket to open, TCP or UDP
geky 12:899403b675fe 54 /return The alocated socket or null on failure
geky 12:899403b675fe 55 */
geky 15:0d8d1dafe064 56 virtual void *socket_create(nsapi_protocol_t proto);
geky 12:899403b675fe 57
geky 12:899403b675fe 58 /** Destroy a socket
geky 12:899403b675fe 59 /param socket Previously allocated socket
geky 12:899403b675fe 60 */
Christopher Haster 13:57d9e1721826 61 virtual void socket_destroy(void *handle);
geky 12:899403b675fe 62
geky 12:899403b675fe 63 /** Set socket options
geky 12:899403b675fe 64 \param handle Socket handle
geky 12:899403b675fe 65 \param optname Option ID
geky 12:899403b675fe 66 \param optval Option value
geky 12:899403b675fe 67 \param optlen Length of the option value
geky 12:899403b675fe 68 \return 0 on success, negative on failure
geky 12:899403b675fe 69 */
Christopher Haster 13:57d9e1721826 70 virtual int socket_set_option(void *handle, int optname, const void *optval, unsigned int optlen);
geky 1:2fbcfc9c12dd 71
geky 12:899403b675fe 72 /** Get socket options
geky 12:899403b675fe 73 \param handle Socket handle
geky 12:899403b675fe 74 \param optname Option ID
geky 12:899403b675fe 75 \param optval Buffer pointer where to write the option value
geky 12:899403b675fe 76 \param optlen Length of the option value
geky 12:899403b675fe 77 \return 0 on success, negative on failure
geky 12:899403b675fe 78 */
Christopher Haster 13:57d9e1721826 79 virtual int socket_get_option(void *handle, int optname, void *optval, unsigned int *optlen);
sam_grove 7:08d5a40ae448 80
geky 12:899403b675fe 81 /** Bind a server socket to a specific port
geky 12:899403b675fe 82 \param handle Socket handle
geky 12:899403b675fe 83 \param port The port to listen for incoming connections on
geky 12:899403b675fe 84 \return 0 on success, negative on failure.
geky 12:899403b675fe 85 */
Christopher Haster 13:57d9e1721826 86 virtual int socket_bind(void *handle, int port);
geky 12:899403b675fe 87
geky 12:899403b675fe 88 /** Start listening for incoming connections
geky 12:899403b675fe 89 \param handle Socket handle
geky 12:899403b675fe 90 \param backlog Number of pending connections that can be queued up at any
geky 12:899403b675fe 91 one time [Default: 1]
geky 12:899403b675fe 92 \return 0 on success, negative on failure
geky 12:899403b675fe 93 */
Christopher Haster 13:57d9e1721826 94 virtual int socket_listen(void *handle, int backlog);
sam_grove 8:cef01e812975 95
geky 12:899403b675fe 96 /** Connects this TCP socket to the server
geky 12:899403b675fe 97 \param handle Socket handle
geky 12:899403b675fe 98 \param address SocketAddress to connect to
geky 12:899403b675fe 99 \return 0 on success, negative on failure
geky 12:899403b675fe 100 */
Christopher Haster 13:57d9e1721826 101 virtual int socket_connect(void *handle, const SocketAddress &address);
geky 12:899403b675fe 102
geky 12:899403b675fe 103 /** Check if the socket is connected
geky 12:899403b675fe 104 \param handle Socket handle
geky 12:899403b675fe 105 \return true if connected, false otherwise
geky 12:899403b675fe 106 */
Christopher Haster 13:57d9e1721826 107 virtual bool socket_is_connected(void *handle);
sam_grove 8:cef01e812975 108
geky 12:899403b675fe 109 /** Accept a new connection.
geky 12:899403b675fe 110 \param handle Socket handle
geky 12:899403b675fe 111 \param socket A TCPSocket instance that will handle the incoming connection.
geky 12:899403b675fe 112 \return 0 on success, negative on failure.
geky 12:899403b675fe 113 \note This call is not-blocking, if this call would block, must
geky 12:899403b675fe 114 immediately return NSAPI_ERROR_WOULD_WAIT
geky 12:899403b675fe 115 */
Christopher Haster 13:57d9e1721826 116 virtual int socket_accept(void *handle, void **connection);
geky 12:899403b675fe 117
geky 12:899403b675fe 118 /** Send data to the remote host
geky 12:899403b675fe 119 \param handle Socket handle
geky 12:899403b675fe 120 \param data The buffer to send to the host
geky 12:899403b675fe 121 \param size The length of the buffer to send
geky 12:899403b675fe 122 \return Number of written bytes on success, negative on failure
geky 12:899403b675fe 123 \note This call is not-blocking, if this call would block, must
geky 12:899403b675fe 124 immediately return NSAPI_ERROR_WOULD_WAIT
geky 12:899403b675fe 125 */
Christopher Haster 13:57d9e1721826 126 virtual int socket_send(void *handle, const void *data, unsigned size);
geky 12:899403b675fe 127
geky 12:899403b675fe 128 /** Receive data from the remote host
geky 12:899403b675fe 129 \param handle Socket handle
geky 12:899403b675fe 130 \param data The buffer in which to store the data received from the host
geky 12:899403b675fe 131 \param size The maximum length of the buffer
geky 12:899403b675fe 132 \return Number of received bytes 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 */
Christopher Haster 13:57d9e1721826 136 virtual int socket_recv(void *handle, void *data, unsigned size);
geky 4:a7349bd7776c 137
geky 12:899403b675fe 138 /** Send a packet to a remote endpoint
geky 12:899403b675fe 139 \param handle Socket handle
geky 12:899403b675fe 140 \param address The remote SocketAddress
geky 12:899403b675fe 141 \param data The packet to be sent
geky 12:899403b675fe 142 \param size The length of the packet to be sent
geky 12:899403b675fe 143 \return the number of written bytes on success, negative on failure
geky 12:899403b675fe 144 \note This call is not-blocking, if this call would block, must
geky 12:899403b675fe 145 immediately return NSAPI_ERROR_WOULD_WAIT
geky 12:899403b675fe 146 */
Christopher Haster 13:57d9e1721826 147 virtual int socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size);
geky 12:899403b675fe 148
geky 12:899403b675fe 149 /** Receive a packet from a remote endpoint
geky 12:899403b675fe 150 \param handle Socket handle
geky 12:899403b675fe 151 \param address Destination for the remote SocketAddress or null
geky 12:899403b675fe 152 \param buffer The buffer for storing the incoming packet data
geky 12:899403b675fe 153 If a packet is too long to fit in the supplied buffer,
geky 12:899403b675fe 154 excess bytes are discarded
geky 12:899403b675fe 155 \param size The length of the buffer
geky 12:899403b675fe 156 \return the number of received bytes on success, negative on failure
geky 12:899403b675fe 157 \note This call is not-blocking, if this call would block, must
geky 12:899403b675fe 158 immediately return NSAPI_ERROR_WOULD_WAIT
geky 12:899403b675fe 159 */
Christopher Haster 13:57d9e1721826 160 virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size);
geky 4:a7349bd7776c 161
geky 12:899403b675fe 162 /** Close the socket
geky 12:899403b675fe 163 \param handle Socket handle
geky 12:899403b675fe 164 \param shutdown free the left-over data in message queues
geky 12:899403b675fe 165 */
Christopher Haster 13:57d9e1721826 166 virtual int socket_close(void *handle, bool shutdown);
geky 12:899403b675fe 167
geky 12:899403b675fe 168 /** Register a callback on when a new connection is ready
geky 12:899403b675fe 169 \param handle Socket handle
geky 12:899403b675fe 170 \param callback Function to call when accept will succeed, may be called in
geky 12:899403b675fe 171 interrupt context.
geky 12:899403b675fe 172 \param id Argument to pass to callback
geky 12:899403b675fe 173 */
Christopher Haster 13:57d9e1721826 174 virtual void socket_attach_accept(void *handle, void (*callback)(void *), void *id);
geky 12:899403b675fe 175
geky 12:899403b675fe 176 /** Register a callback on when send is ready
geky 12:899403b675fe 177 \param handle Socket handle
geky 12:899403b675fe 178 \param callback Function to call when accept will succeed, may be called in
geky 12:899403b675fe 179 interrupt context.
geky 12:899403b675fe 180 \param id Argument to pass to callback
geky 12:899403b675fe 181 */
Christopher Haster 13:57d9e1721826 182 virtual void socket_attach_send(void *handle, void (*callback)(void *), void *id);
geky 12:899403b675fe 183
geky 12:899403b675fe 184 /** Register a callback on when recv is ready
geky 12:899403b675fe 185 \param handle Socket handle
geky 12:899403b675fe 186 \param callback Function to call when accept will succeed, may be called in
geky 12:899403b675fe 187 interrupt context.
geky 12:899403b675fe 188 \param id Argument to pass to callback
geky 12:899403b675fe 189 */
Christopher Haster 13:57d9e1721826 190 virtual void socket_attach_recv(void *handle, void (*callback)(void *), void *id);
geky 1:2fbcfc9c12dd 191 };
geky 1:2fbcfc9c12dd 192
geky 1:2fbcfc9c12dd 193 #endif