W5500 from SeeedStudio on NUCLEO-L476RG

Dependents:   coap-example Borsch coap-example

Fork of W5500Interface by AMETEK Powervar

Committer:
sgnezdov
Date:
Sat Jul 01 07:29:02 2017 +0000
Revision:
2:06b6f9f7c071
Parent:
1:2dee44ea52a9
UDP packets are sent now; ; problem of blocking state tracking is exposed.

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 1:2dee44ea52a9 26 /** w5500_socket struct
dgriffin65 1:2dee44ea52a9 27 * W5500 socket
dgriffin65 1:2dee44ea52a9 28 */
dgriffin65 1:2dee44ea52a9 29
dgriffin65 1:2dee44ea52a9 30 struct w5500_socket {
dgriffin65 1:2dee44ea52a9 31 int fd;
dgriffin65 1:2dee44ea52a9 32 nsapi_protocol_t proto;
dgriffin65 1:2dee44ea52a9 33 void (*callback)(void *);
dgriffin65 1:2dee44ea52a9 34 void *callback_data;
dgriffin65 1:2dee44ea52a9 35 bool connected;
dgriffin65 1:2dee44ea52a9 36 };
dgriffin65 1:2dee44ea52a9 37
dgriffin65 0:77e050d1fb12 38 /** W5500Interface class
dgriffin65 0:77e050d1fb12 39 * Implementation of the NetworkStack for the W5500
dgriffin65 0:77e050d1fb12 40 */
dgriffin65 1:2dee44ea52a9 41
dgriffin65 0:77e050d1fb12 42 class W5500Interface : public NetworkStack, public EthInterface, public WIZnet_Chip
dgriffin65 0:77e050d1fb12 43 {
dgriffin65 0:77e050d1fb12 44 public:
dgriffin65 0:77e050d1fb12 45 W5500Interface(SPI* spi, PinName cs, PinName reset);
dgriffin65 0:77e050d1fb12 46 W5500Interface(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset);
dgriffin65 0:77e050d1fb12 47
dgriffin65 0:77e050d1fb12 48 int init();
dgriffin65 0:77e050d1fb12 49 int init(uint8_t * mac);
dgriffin65 0:77e050d1fb12 50 int init(const char* ip, const char* mask, const char* gateway);
dgriffin65 0:77e050d1fb12 51 int init(uint8_t * mac, const char* ip, const char* mask, const char* gateway);
dgriffin65 0:77e050d1fb12 52
dgriffin65 0:77e050d1fb12 53 /** Start the interface
dgriffin65 0:77e050d1fb12 54 * @return 0 on success, negative on failure
dgriffin65 0:77e050d1fb12 55 */
dgriffin65 0:77e050d1fb12 56 virtual int connect();
dgriffin65 0:77e050d1fb12 57
dgriffin65 0:77e050d1fb12 58 /** Stop the interface
dgriffin65 0:77e050d1fb12 59 * @return 0 on success, negative on failure
dgriffin65 0:77e050d1fb12 60 */
dgriffin65 0:77e050d1fb12 61 virtual int disconnect();
dgriffin65 0:77e050d1fb12 62
dgriffin65 0:77e050d1fb12 63 /** Get the internally stored IP address
dgriffin65 0:77e050d1fb12 64 * @return IP address of the interface or null if not yet connected
dgriffin65 0:77e050d1fb12 65 */
dgriffin65 0:77e050d1fb12 66 virtual const char *get_ip_address();
dgriffin65 0:77e050d1fb12 67
sgnezdov 2:06b6f9f7c071 68 /** Get MAC address and fill mac with it.
sgnezdov 2:06b6f9f7c071 69 */
sgnezdov 2:06b6f9f7c071 70 void get_mac(uint8_t mac[6]) ;
sgnezdov 2:06b6f9f7c071 71
dgriffin65 0:77e050d1fb12 72 /** Get the internally stored MAC address
dgriffin65 0:77e050d1fb12 73 * @return MAC address of the interface
dgriffin65 0:77e050d1fb12 74 */
dgriffin65 0:77e050d1fb12 75 virtual const char *get_mac_address();
dgriffin65 0:77e050d1fb12 76
dgriffin65 0:77e050d1fb12 77 protected:
dgriffin65 0:77e050d1fb12 78 /** Opens a socket
dgriffin65 0:77e050d1fb12 79 *
dgriffin65 0:77e050d1fb12 80 * Creates a network socket and stores it in the specified handle.
dgriffin65 0:77e050d1fb12 81 * The handle must be passed to following calls on the socket.
dgriffin65 0:77e050d1fb12 82 *
dgriffin65 0:77e050d1fb12 83 * A stack may have a finite number of sockets, in this case
dgriffin65 0:77e050d1fb12 84 * NSAPI_ERROR_NO_SOCKET is returned if no socket is available.
dgriffin65 0:77e050d1fb12 85 *
dgriffin65 0:77e050d1fb12 86 * @param handle Destination for the handle to a newly created socket
dgriffin65 0:77e050d1fb12 87 * @param proto Protocol of socket to open, NSAPI_TCP or NSAPI_UDP
dgriffin65 0:77e050d1fb12 88 * @return 0 on success, negative error code on failure
dgriffin65 0:77e050d1fb12 89 */
dgriffin65 0:77e050d1fb12 90 virtual nsapi_error_t socket_open(nsapi_socket_t *handle, nsapi_protocol_t proto);
dgriffin65 0:77e050d1fb12 91
dgriffin65 0:77e050d1fb12 92 /** Close the socket
dgriffin65 0:77e050d1fb12 93 *
dgriffin65 0:77e050d1fb12 94 * Closes any open connection and deallocates any memory associated
dgriffin65 0:77e050d1fb12 95 * with the socket.
dgriffin65 0:77e050d1fb12 96 *
dgriffin65 0:77e050d1fb12 97 * @param handle Socket handle
dgriffin65 0:77e050d1fb12 98 * @return 0 on success, negative error code on failure
dgriffin65 0:77e050d1fb12 99 */
dgriffin65 0:77e050d1fb12 100 virtual nsapi_error_t socket_close(nsapi_socket_t handle);
dgriffin65 0:77e050d1fb12 101
dgriffin65 0:77e050d1fb12 102 /** Bind a specific address to a socket
dgriffin65 0:77e050d1fb12 103 *
dgriffin65 0:77e050d1fb12 104 * Binding a socket specifies the address and port on which to recieve
dgriffin65 0:77e050d1fb12 105 * data. If the IP address is zeroed, only the port is bound.
dgriffin65 0:77e050d1fb12 106 *
dgriffin65 0:77e050d1fb12 107 * @param handle Socket handle
dgriffin65 0:77e050d1fb12 108 * @param address Local address to bind
dgriffin65 0:77e050d1fb12 109 * @return 0 on success, negative error code on failure.
dgriffin65 0:77e050d1fb12 110 */
dgriffin65 0:77e050d1fb12 111 virtual nsapi_error_t socket_bind(nsapi_socket_t handle, const SocketAddress &address);
dgriffin65 0:77e050d1fb12 112
dgriffin65 0:77e050d1fb12 113 /** Listen for connections on a TCP socket
dgriffin65 0:77e050d1fb12 114 *
dgriffin65 0:77e050d1fb12 115 * Marks the socket as a passive socket that can be used to accept
dgriffin65 0:77e050d1fb12 116 * incoming connections.
dgriffin65 0:77e050d1fb12 117 *
dgriffin65 0:77e050d1fb12 118 * @param handle Socket handle
dgriffin65 0:77e050d1fb12 119 * @param backlog Number of pending connections that can be queued
dgriffin65 0:77e050d1fb12 120 * simultaneously
dgriffin65 0:77e050d1fb12 121 * @return 0 on success, negative error code on failure
dgriffin65 0:77e050d1fb12 122 */
dgriffin65 0:77e050d1fb12 123 virtual nsapi_error_t socket_listen(nsapi_socket_t handle, int backlog);
dgriffin65 0:77e050d1fb12 124
dgriffin65 0:77e050d1fb12 125 /** Connects TCP socket to a remote host
dgriffin65 0:77e050d1fb12 126 *
dgriffin65 0:77e050d1fb12 127 * Initiates a connection to a remote server specified by the
dgriffin65 0:77e050d1fb12 128 * indicated address.
dgriffin65 0:77e050d1fb12 129 *
dgriffin65 0:77e050d1fb12 130 * @param handle Socket handle
dgriffin65 0:77e050d1fb12 131 * @param address The SocketAddress of the remote host
dgriffin65 0:77e050d1fb12 132 * @return 0 on success, negative error code on failure
dgriffin65 0:77e050d1fb12 133 */
dgriffin65 0:77e050d1fb12 134 virtual nsapi_error_t socket_connect(nsapi_socket_t handle, const SocketAddress &address);
dgriffin65 0:77e050d1fb12 135
dgriffin65 0:77e050d1fb12 136 /** Accepts a connection on a TCP socket
dgriffin65 0:77e050d1fb12 137 *
dgriffin65 0:77e050d1fb12 138 * The server socket must be bound and set to listen for connections.
dgriffin65 0:77e050d1fb12 139 * On a new connection, creates a network socket and stores it in the
dgriffin65 0:77e050d1fb12 140 * specified handle. The handle must be passed to following calls on
dgriffin65 0:77e050d1fb12 141 * the socket.
dgriffin65 0:77e050d1fb12 142 *
dgriffin65 0:77e050d1fb12 143 * A stack may have a finite number of sockets, in this case
dgriffin65 0:77e050d1fb12 144 * NSAPI_ERROR_NO_SOCKET is returned if no socket is available.
dgriffin65 0:77e050d1fb12 145 *
dgriffin65 0:77e050d1fb12 146 * This call is non-blocking. If accept would block,
dgriffin65 0:77e050d1fb12 147 * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
dgriffin65 0:77e050d1fb12 148 *
dgriffin65 0:77e050d1fb12 149 * @param server Socket handle to server to accept from
dgriffin65 0:77e050d1fb12 150 * @param handle Destination for a handle to the newly created socket
dgriffin65 0:77e050d1fb12 151 * @param address Destination for the remote address or NULL
dgriffin65 0:77e050d1fb12 152 * @return 0 on success, negative error code on failure
dgriffin65 0:77e050d1fb12 153 */
dgriffin65 0:77e050d1fb12 154 virtual nsapi_error_t socket_accept(nsapi_socket_t server,
dgriffin65 0:77e050d1fb12 155 nsapi_socket_t *handle, SocketAddress *address=0);
dgriffin65 0:77e050d1fb12 156
dgriffin65 0:77e050d1fb12 157 /** Send 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 sent from the buffer.
dgriffin65 0:77e050d1fb12 161 *
dgriffin65 0:77e050d1fb12 162 * This call is non-blocking. If send 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 Buffer of data to send to the host
dgriffin65 0:77e050d1fb12 167 * @param size Size of the buffer in bytes
dgriffin65 0:77e050d1fb12 168 * @return Number of sent 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_send(nsapi_socket_t handle,
dgriffin65 0:77e050d1fb12 172 const void *data, nsapi_size_t size);
dgriffin65 0:77e050d1fb12 173
dgriffin65 0:77e050d1fb12 174 /** Receive data over a TCP socket
dgriffin65 0:77e050d1fb12 175 *
dgriffin65 0:77e050d1fb12 176 * The socket must be connected to a remote host. Returns the number of
dgriffin65 0:77e050d1fb12 177 * bytes received into the buffer.
dgriffin65 0:77e050d1fb12 178 *
dgriffin65 0:77e050d1fb12 179 * This call is non-blocking. If recv 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 data Destination buffer for data received from the host
dgriffin65 0:77e050d1fb12 184 * @param size Size of the buffer in bytes
dgriffin65 0:77e050d1fb12 185 * @return Number of received bytes on success, negative error
dgriffin65 0:77e050d1fb12 186 * code on failure
dgriffin65 0:77e050d1fb12 187 */
dgriffin65 0:77e050d1fb12 188 virtual nsapi_size_or_error_t socket_recv(nsapi_socket_t handle,
dgriffin65 0:77e050d1fb12 189 void *data, nsapi_size_t size);
dgriffin65 0:77e050d1fb12 190
dgriffin65 0:77e050d1fb12 191 /** Send a packet over a UDP socket
dgriffin65 0:77e050d1fb12 192 *
dgriffin65 0:77e050d1fb12 193 * Sends data to the specified address. Returns the number of bytes
dgriffin65 0:77e050d1fb12 194 * sent from the buffer.
dgriffin65 0:77e050d1fb12 195 *
dgriffin65 0:77e050d1fb12 196 * This call is non-blocking. If sendto would block,
dgriffin65 0:77e050d1fb12 197 * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
dgriffin65 0:77e050d1fb12 198 *
dgriffin65 0:77e050d1fb12 199 * @param handle Socket handle
dgriffin65 0:77e050d1fb12 200 * @param address The SocketAddress of the remote host
dgriffin65 0:77e050d1fb12 201 * @param data Buffer of data to send to the host
dgriffin65 0:77e050d1fb12 202 * @param size Size of the buffer in bytes
dgriffin65 0:77e050d1fb12 203 * @return Number of sent bytes on success, negative error
dgriffin65 0:77e050d1fb12 204 * code on failure
dgriffin65 0:77e050d1fb12 205 */
dgriffin65 0:77e050d1fb12 206 virtual nsapi_size_or_error_t socket_sendto(nsapi_socket_t handle, const SocketAddress &address,
dgriffin65 0:77e050d1fb12 207 const void *data, nsapi_size_t size);
dgriffin65 0:77e050d1fb12 208
dgriffin65 0:77e050d1fb12 209 /** Receive a packet over a UDP socket
dgriffin65 0:77e050d1fb12 210 *
dgriffin65 0:77e050d1fb12 211 * Receives data and stores the source address in address if address
dgriffin65 0:77e050d1fb12 212 * is not NULL. Returns the number of bytes received into the buffer.
dgriffin65 0:77e050d1fb12 213 *
dgriffin65 0:77e050d1fb12 214 * This call is non-blocking. If recvfrom would block,
dgriffin65 0:77e050d1fb12 215 * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
dgriffin65 0:77e050d1fb12 216 *
dgriffin65 0:77e050d1fb12 217 * @param handle Socket handle
dgriffin65 0:77e050d1fb12 218 * @param address Destination for the source address or NULL
dgriffin65 0:77e050d1fb12 219 * @param data Destination buffer for data received from the host
dgriffin65 0:77e050d1fb12 220 * @param size Size of the buffer in bytes
dgriffin65 0:77e050d1fb12 221 * @return Number of received bytes on success, negative error
dgriffin65 0:77e050d1fb12 222 * code on failure
dgriffin65 0:77e050d1fb12 223 */
dgriffin65 0:77e050d1fb12 224 virtual nsapi_size_or_error_t socket_recvfrom(nsapi_socket_t handle, SocketAddress *address,
dgriffin65 0:77e050d1fb12 225 void *buffer, nsapi_size_t size);
dgriffin65 0:77e050d1fb12 226
dgriffin65 0:77e050d1fb12 227 /** Register a callback on state change of the socket
dgriffin65 0:77e050d1fb12 228 *
dgriffin65 0:77e050d1fb12 229 * The specified callback will be called on state changes such as when
dgriffin65 0:77e050d1fb12 230 * the socket can recv/send/accept successfully and on when an error
dgriffin65 0:77e050d1fb12 231 * occurs. The callback may also be called spuriously without reason.
dgriffin65 0:77e050d1fb12 232 *
dgriffin65 0:77e050d1fb12 233 * The callback may be called in an interrupt context and should not
dgriffin65 0:77e050d1fb12 234 * perform expensive operations such as recv/send calls.
dgriffin65 0:77e050d1fb12 235 *
dgriffin65 0:77e050d1fb12 236 * @param handle Socket handle
dgriffin65 0:77e050d1fb12 237 * @param callback Function to call on state change
dgriffin65 0:77e050d1fb12 238 * @param data Argument to pass to callback
dgriffin65 0:77e050d1fb12 239 */
dgriffin65 0:77e050d1fb12 240 virtual void socket_attach(nsapi_socket_t handle, void (*callback)(void *), void *data);
dgriffin65 0:77e050d1fb12 241
dgriffin65 0:77e050d1fb12 242 virtual NetworkStack* get_stack() {return this;}
dgriffin65 0:77e050d1fb12 243
dgriffin65 0:77e050d1fb12 244 private:
dgriffin65 0:77e050d1fb12 245 char ip_string[20];
dgriffin65 0:77e050d1fb12 246 char mask_string[20];
dgriffin65 0:77e050d1fb12 247 char gw_string[20];
dgriffin65 0:77e050d1fb12 248 char mac_string[20];
dgriffin65 0:77e050d1fb12 249 bool ip_set;
dgriffin65 0:77e050d1fb12 250
dgriffin65 0:77e050d1fb12 251 int listen_port;
dgriffin65 1:2dee44ea52a9 252
dgriffin65 1:2dee44ea52a9 253 void signal_event(nsapi_socket_t handle);
dgriffin65 1:2dee44ea52a9 254
dgriffin65 1:2dee44ea52a9 255 //w5500 socket management
dgriffin65 1:2dee44ea52a9 256 struct w5500_socket w5500_sockets[MAX_SOCK_NUM];
dgriffin65 1:2dee44ea52a9 257 w5500_socket* get_sock(int fd);
dgriffin65 1:2dee44ea52a9 258 void init_socks();
dgriffin65 1:2dee44ea52a9 259
dgriffin65 1:2dee44ea52a9 260 Thread *_daemon;
dgriffin65 1:2dee44ea52a9 261 void daemon();
dgriffin65 1:2dee44ea52a9 262
dgriffin65 0:77e050d1fb12 263 };
dgriffin65 0:77e050d1fb12 264
dgriffin65 0:77e050d1fb12 265 #endif