Update of W5500 Interface for mbed-os

Dependents:   PwrCond_mbed5

Committer:
dgriffin65
Date:
Fri Jun 02 22:00:49 2017 +0000
Revision:
0:77e050d1fb12
Child:
1:2dee44ea52a9
Many updates for implementing for W5500 support in mbed os

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dgriffin65 0:77e050d1fb12 1 /* W5500 implementation of NetworkInterfaceAPI
dgriffin65 0:77e050d1fb12 2 * Copyright (c) 2015 ARM Limited
dgriffin65 0:77e050d1fb12 3 *
dgriffin65 0:77e050d1fb12 4 * Licensed under the Apache License, Version 2.0 (the "License");
dgriffin65 0:77e050d1fb12 5 * you may not use this file except in compliance with the License.
dgriffin65 0:77e050d1fb12 6 * You may obtain a copy of the License at
dgriffin65 0:77e050d1fb12 7 *
dgriffin65 0:77e050d1fb12 8 * http://www.apache.org/licenses/LICENSE-2.0
dgriffin65 0:77e050d1fb12 9 *
dgriffin65 0:77e050d1fb12 10 * Unless required by applicable law or agreed to in writing, software
dgriffin65 0:77e050d1fb12 11 * distributed under the License is distributed on an "AS IS" BASIS,
dgriffin65 0:77e050d1fb12 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
dgriffin65 0:77e050d1fb12 13 * See the License for the specific language governing permissions and
dgriffin65 0:77e050d1fb12 14 * limitations under the License.
dgriffin65 0:77e050d1fb12 15 */
dgriffin65 0:77e050d1fb12 16
dgriffin65 0:77e050d1fb12 17 #ifndef W5500_INTERFACE_H
dgriffin65 0:77e050d1fb12 18 #define W5500_INTERFACE_H
dgriffin65 0:77e050d1fb12 19
dgriffin65 0:77e050d1fb12 20 #include "NetworkStack.h"
dgriffin65 0:77e050d1fb12 21 #include "EthInterface.h"
dgriffin65 0:77e050d1fb12 22
dgriffin65 0:77e050d1fb12 23 #include "rtos.h"
dgriffin65 0:77e050d1fb12 24 #include "WIZnet/W5500.h"
dgriffin65 0:77e050d1fb12 25
dgriffin65 0:77e050d1fb12 26 /** W5500Interface class
dgriffin65 0:77e050d1fb12 27 * Implementation of the NetworkStack for the W5500
dgriffin65 0:77e050d1fb12 28 */
dgriffin65 0:77e050d1fb12 29 class W5500Interface : public NetworkStack, public EthInterface, public WIZnet_Chip
dgriffin65 0:77e050d1fb12 30 {
dgriffin65 0:77e050d1fb12 31 public:
dgriffin65 0:77e050d1fb12 32 W5500Interface(SPI* spi, PinName cs, PinName reset);
dgriffin65 0:77e050d1fb12 33 W5500Interface(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset);
dgriffin65 0:77e050d1fb12 34
dgriffin65 0:77e050d1fb12 35 int init();
dgriffin65 0:77e050d1fb12 36 int init(uint8_t * mac);
dgriffin65 0:77e050d1fb12 37 int init(const char* ip, const char* mask, const char* gateway);
dgriffin65 0:77e050d1fb12 38 int init(uint8_t * mac, const char* ip, const char* mask, const char* gateway);
dgriffin65 0:77e050d1fb12 39
dgriffin65 0:77e050d1fb12 40 /** Start the interface
dgriffin65 0:77e050d1fb12 41 * @return 0 on success, negative on failure
dgriffin65 0:77e050d1fb12 42 */
dgriffin65 0:77e050d1fb12 43 virtual int connect();
dgriffin65 0:77e050d1fb12 44
dgriffin65 0:77e050d1fb12 45 /** Stop the interface
dgriffin65 0:77e050d1fb12 46 * @return 0 on success, negative on failure
dgriffin65 0:77e050d1fb12 47 */
dgriffin65 0:77e050d1fb12 48 virtual int disconnect();
dgriffin65 0:77e050d1fb12 49
dgriffin65 0:77e050d1fb12 50 /** Get the internally stored IP address
dgriffin65 0:77e050d1fb12 51 * @return IP address of the interface or null if not yet connected
dgriffin65 0:77e050d1fb12 52 */
dgriffin65 0:77e050d1fb12 53 virtual const char *get_ip_address();
dgriffin65 0:77e050d1fb12 54
dgriffin65 0:77e050d1fb12 55 /** Get the internally stored MAC address
dgriffin65 0:77e050d1fb12 56 * @return MAC address of the interface
dgriffin65 0:77e050d1fb12 57 */
dgriffin65 0:77e050d1fb12 58 virtual const char *get_mac_address();
dgriffin65 0:77e050d1fb12 59
dgriffin65 0:77e050d1fb12 60 protected:
dgriffin65 0:77e050d1fb12 61 /** Opens a socket
dgriffin65 0:77e050d1fb12 62 *
dgriffin65 0:77e050d1fb12 63 * Creates a network socket and stores it in the specified handle.
dgriffin65 0:77e050d1fb12 64 * The handle must be passed to following calls on the socket.
dgriffin65 0:77e050d1fb12 65 *
dgriffin65 0:77e050d1fb12 66 * A stack may have a finite number of sockets, in this case
dgriffin65 0:77e050d1fb12 67 * NSAPI_ERROR_NO_SOCKET is returned if no socket is available.
dgriffin65 0:77e050d1fb12 68 *
dgriffin65 0:77e050d1fb12 69 * @param handle Destination for the handle to a newly created socket
dgriffin65 0:77e050d1fb12 70 * @param proto Protocol of socket to open, NSAPI_TCP or NSAPI_UDP
dgriffin65 0:77e050d1fb12 71 * @return 0 on success, negative error code on failure
dgriffin65 0:77e050d1fb12 72 */
dgriffin65 0:77e050d1fb12 73 virtual nsapi_error_t socket_open(nsapi_socket_t *handle, nsapi_protocol_t proto);
dgriffin65 0:77e050d1fb12 74
dgriffin65 0:77e050d1fb12 75 /** Close the socket
dgriffin65 0:77e050d1fb12 76 *
dgriffin65 0:77e050d1fb12 77 * Closes any open connection and deallocates any memory associated
dgriffin65 0:77e050d1fb12 78 * with the socket.
dgriffin65 0:77e050d1fb12 79 *
dgriffin65 0:77e050d1fb12 80 * @param handle Socket handle
dgriffin65 0:77e050d1fb12 81 * @return 0 on success, negative error code on failure
dgriffin65 0:77e050d1fb12 82 */
dgriffin65 0:77e050d1fb12 83 virtual nsapi_error_t socket_close(nsapi_socket_t handle);
dgriffin65 0:77e050d1fb12 84
dgriffin65 0:77e050d1fb12 85 /** Bind a specific address to a socket
dgriffin65 0:77e050d1fb12 86 *
dgriffin65 0:77e050d1fb12 87 * Binding a socket specifies the address and port on which to recieve
dgriffin65 0:77e050d1fb12 88 * data. If the IP address is zeroed, only the port is bound.
dgriffin65 0:77e050d1fb12 89 *
dgriffin65 0:77e050d1fb12 90 * @param handle Socket handle
dgriffin65 0:77e050d1fb12 91 * @param address Local address to bind
dgriffin65 0:77e050d1fb12 92 * @return 0 on success, negative error code on failure.
dgriffin65 0:77e050d1fb12 93 */
dgriffin65 0:77e050d1fb12 94 virtual nsapi_error_t socket_bind(nsapi_socket_t handle, const SocketAddress &address);
dgriffin65 0:77e050d1fb12 95
dgriffin65 0:77e050d1fb12 96 /** Listen for connections on a TCP socket
dgriffin65 0:77e050d1fb12 97 *
dgriffin65 0:77e050d1fb12 98 * Marks the socket as a passive socket that can be used to accept
dgriffin65 0:77e050d1fb12 99 * incoming connections.
dgriffin65 0:77e050d1fb12 100 *
dgriffin65 0:77e050d1fb12 101 * @param handle Socket handle
dgriffin65 0:77e050d1fb12 102 * @param backlog Number of pending connections that can be queued
dgriffin65 0:77e050d1fb12 103 * simultaneously
dgriffin65 0:77e050d1fb12 104 * @return 0 on success, negative error code on failure
dgriffin65 0:77e050d1fb12 105 */
dgriffin65 0:77e050d1fb12 106 virtual nsapi_error_t socket_listen(nsapi_socket_t handle, int backlog);
dgriffin65 0:77e050d1fb12 107
dgriffin65 0:77e050d1fb12 108 /** Connects TCP socket to a remote host
dgriffin65 0:77e050d1fb12 109 *
dgriffin65 0:77e050d1fb12 110 * Initiates a connection to a remote server specified by the
dgriffin65 0:77e050d1fb12 111 * indicated address.
dgriffin65 0:77e050d1fb12 112 *
dgriffin65 0:77e050d1fb12 113 * @param handle Socket handle
dgriffin65 0:77e050d1fb12 114 * @param address The SocketAddress of the remote host
dgriffin65 0:77e050d1fb12 115 * @return 0 on success, negative error code on failure
dgriffin65 0:77e050d1fb12 116 */
dgriffin65 0:77e050d1fb12 117 virtual nsapi_error_t socket_connect(nsapi_socket_t handle, const SocketAddress &address);
dgriffin65 0:77e050d1fb12 118
dgriffin65 0:77e050d1fb12 119 /** Accepts a connection on a TCP socket
dgriffin65 0:77e050d1fb12 120 *
dgriffin65 0:77e050d1fb12 121 * The server socket must be bound and set to listen for connections.
dgriffin65 0:77e050d1fb12 122 * On a new connection, creates a network socket and stores it in the
dgriffin65 0:77e050d1fb12 123 * specified handle. The handle must be passed to following calls on
dgriffin65 0:77e050d1fb12 124 * the socket.
dgriffin65 0:77e050d1fb12 125 *
dgriffin65 0:77e050d1fb12 126 * A stack may have a finite number of sockets, in this case
dgriffin65 0:77e050d1fb12 127 * NSAPI_ERROR_NO_SOCKET is returned if no socket is available.
dgriffin65 0:77e050d1fb12 128 *
dgriffin65 0:77e050d1fb12 129 * This call is non-blocking. If accept would block,
dgriffin65 0:77e050d1fb12 130 * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
dgriffin65 0:77e050d1fb12 131 *
dgriffin65 0:77e050d1fb12 132 * @param server Socket handle to server to accept from
dgriffin65 0:77e050d1fb12 133 * @param handle Destination for a handle to the newly created socket
dgriffin65 0:77e050d1fb12 134 * @param address Destination for the remote address or NULL
dgriffin65 0:77e050d1fb12 135 * @return 0 on success, negative error code on failure
dgriffin65 0:77e050d1fb12 136 */
dgriffin65 0:77e050d1fb12 137 virtual nsapi_error_t socket_accept(nsapi_socket_t server,
dgriffin65 0:77e050d1fb12 138 nsapi_socket_t *handle, SocketAddress *address=0);
dgriffin65 0:77e050d1fb12 139
dgriffin65 0:77e050d1fb12 140 /** Send data over a TCP socket
dgriffin65 0:77e050d1fb12 141 *
dgriffin65 0:77e050d1fb12 142 * The socket must be connected to a remote host. Returns the number of
dgriffin65 0:77e050d1fb12 143 * bytes sent from the buffer.
dgriffin65 0:77e050d1fb12 144 *
dgriffin65 0:77e050d1fb12 145 * This call is non-blocking. If send would block,
dgriffin65 0:77e050d1fb12 146 * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
dgriffin65 0:77e050d1fb12 147 *
dgriffin65 0:77e050d1fb12 148 * @param handle Socket handle
dgriffin65 0:77e050d1fb12 149 * @param data Buffer of data to send to the host
dgriffin65 0:77e050d1fb12 150 * @param size Size of the buffer in bytes
dgriffin65 0:77e050d1fb12 151 * @return Number of sent bytes on success, negative error
dgriffin65 0:77e050d1fb12 152 * code on failure
dgriffin65 0:77e050d1fb12 153 */
dgriffin65 0:77e050d1fb12 154 virtual nsapi_size_or_error_t socket_send(nsapi_socket_t handle,
dgriffin65 0:77e050d1fb12 155 const void *data, nsapi_size_t size);
dgriffin65 0:77e050d1fb12 156
dgriffin65 0:77e050d1fb12 157 /** Receive data over a TCP socket
dgriffin65 0:77e050d1fb12 158 *
dgriffin65 0:77e050d1fb12 159 * The socket must be connected to a remote host. Returns the number of
dgriffin65 0:77e050d1fb12 160 * bytes received into the buffer.
dgriffin65 0:77e050d1fb12 161 *
dgriffin65 0:77e050d1fb12 162 * This call is non-blocking. If recv would block,
dgriffin65 0:77e050d1fb12 163 * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
dgriffin65 0:77e050d1fb12 164 *
dgriffin65 0:77e050d1fb12 165 * @param handle Socket handle
dgriffin65 0:77e050d1fb12 166 * @param data Destination buffer for data received from the host
dgriffin65 0:77e050d1fb12 167 * @param size Size of the buffer in bytes
dgriffin65 0:77e050d1fb12 168 * @return Number of received bytes on success, negative error
dgriffin65 0:77e050d1fb12 169 * code on failure
dgriffin65 0:77e050d1fb12 170 */
dgriffin65 0:77e050d1fb12 171 virtual nsapi_size_or_error_t socket_recv(nsapi_socket_t handle,
dgriffin65 0:77e050d1fb12 172 void *data, nsapi_size_t size);
dgriffin65 0:77e050d1fb12 173
dgriffin65 0:77e050d1fb12 174 /** Send a packet over a UDP socket
dgriffin65 0:77e050d1fb12 175 *
dgriffin65 0:77e050d1fb12 176 * Sends data to the specified address. Returns the number of bytes
dgriffin65 0:77e050d1fb12 177 * sent from the buffer.
dgriffin65 0:77e050d1fb12 178 *
dgriffin65 0:77e050d1fb12 179 * This call is non-blocking. If sendto would block,
dgriffin65 0:77e050d1fb12 180 * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
dgriffin65 0:77e050d1fb12 181 *
dgriffin65 0:77e050d1fb12 182 * @param handle Socket handle
dgriffin65 0:77e050d1fb12 183 * @param address The SocketAddress of the remote host
dgriffin65 0:77e050d1fb12 184 * @param data Buffer of data to send to the host
dgriffin65 0:77e050d1fb12 185 * @param size Size of the buffer in bytes
dgriffin65 0:77e050d1fb12 186 * @return Number of sent bytes on success, negative error
dgriffin65 0:77e050d1fb12 187 * code on failure
dgriffin65 0:77e050d1fb12 188 */
dgriffin65 0:77e050d1fb12 189 virtual nsapi_size_or_error_t socket_sendto(nsapi_socket_t handle, const SocketAddress &address,
dgriffin65 0:77e050d1fb12 190 const void *data, nsapi_size_t size);
dgriffin65 0:77e050d1fb12 191
dgriffin65 0:77e050d1fb12 192 /** Receive a packet over a UDP socket
dgriffin65 0:77e050d1fb12 193 *
dgriffin65 0:77e050d1fb12 194 * Receives data and stores the source address in address if address
dgriffin65 0:77e050d1fb12 195 * is not NULL. Returns the number of bytes received into the buffer.
dgriffin65 0:77e050d1fb12 196 *
dgriffin65 0:77e050d1fb12 197 * This call is non-blocking. If recvfrom would block,
dgriffin65 0:77e050d1fb12 198 * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
dgriffin65 0:77e050d1fb12 199 *
dgriffin65 0:77e050d1fb12 200 * @param handle Socket handle
dgriffin65 0:77e050d1fb12 201 * @param address Destination for the source address or NULL
dgriffin65 0:77e050d1fb12 202 * @param data Destination buffer for data received from the host
dgriffin65 0:77e050d1fb12 203 * @param size Size of the buffer in bytes
dgriffin65 0:77e050d1fb12 204 * @return Number of received bytes on success, negative error
dgriffin65 0:77e050d1fb12 205 * code on failure
dgriffin65 0:77e050d1fb12 206 */
dgriffin65 0:77e050d1fb12 207 virtual nsapi_size_or_error_t socket_recvfrom(nsapi_socket_t handle, SocketAddress *address,
dgriffin65 0:77e050d1fb12 208 void *buffer, nsapi_size_t size);
dgriffin65 0:77e050d1fb12 209
dgriffin65 0:77e050d1fb12 210 /** Register a callback on state change of the socket
dgriffin65 0:77e050d1fb12 211 *
dgriffin65 0:77e050d1fb12 212 * The specified callback will be called on state changes such as when
dgriffin65 0:77e050d1fb12 213 * the socket can recv/send/accept successfully and on when an error
dgriffin65 0:77e050d1fb12 214 * occurs. The callback may also be called spuriously without reason.
dgriffin65 0:77e050d1fb12 215 *
dgriffin65 0:77e050d1fb12 216 * The callback may be called in an interrupt context and should not
dgriffin65 0:77e050d1fb12 217 * perform expensive operations such as recv/send calls.
dgriffin65 0:77e050d1fb12 218 *
dgriffin65 0:77e050d1fb12 219 * @param handle Socket handle
dgriffin65 0:77e050d1fb12 220 * @param callback Function to call on state change
dgriffin65 0:77e050d1fb12 221 * @param data Argument to pass to callback
dgriffin65 0:77e050d1fb12 222 */
dgriffin65 0:77e050d1fb12 223 virtual void socket_attach(nsapi_socket_t handle, void (*callback)(void *), void *data);
dgriffin65 0:77e050d1fb12 224
dgriffin65 0:77e050d1fb12 225 virtual NetworkStack* get_stack() {return this;}
dgriffin65 0:77e050d1fb12 226
dgriffin65 0:77e050d1fb12 227 private:
dgriffin65 0:77e050d1fb12 228 char ip_string[20];
dgriffin65 0:77e050d1fb12 229 char mask_string[20];
dgriffin65 0:77e050d1fb12 230 char gw_string[20];
dgriffin65 0:77e050d1fb12 231 char mac_string[20];
dgriffin65 0:77e050d1fb12 232 bool ip_set;
dgriffin65 0:77e050d1fb12 233
dgriffin65 0:77e050d1fb12 234 int listen_port;
dgriffin65 0:77e050d1fb12 235 };
dgriffin65 0:77e050d1fb12 236
dgriffin65 0:77e050d1fb12 237 #endif