W5500 from SeeedStudio on NUCLEO-L476RG

Dependents:   coap-example Borsch coap-example

Fork of W5500Interface by AMETEK Powervar

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers W5500Interface.h Source File

W5500Interface.h

00001 /* W5500 implementation of NetworkInterfaceAPI
00002  * Copyright (c) 2015 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef W5500_INTERFACE_H
00018 #define W5500_INTERFACE_H
00019 
00020 #include "NetworkStack.h"
00021 #include "EthInterface.h"
00022 
00023 #include "rtos.h"
00024 #include "WIZnet/W5500.h"
00025 
00026 /** w5500_socket struct
00027  *  W5500 socket 
00028  */
00029  
00030 struct w5500_socket {
00031    int   fd;
00032    nsapi_protocol_t proto;
00033    void  (*callback)(void *);
00034    void  *callback_data;
00035    bool  connected;
00036 }; 
00037 
00038 /** W5500Interface class
00039  *  Implementation of the NetworkStack for the W5500
00040  */
00041 
00042 class W5500Interface : public NetworkStack, public EthInterface, public WIZnet_Chip 
00043 {
00044 public:
00045     W5500Interface(SPI* spi, PinName cs, PinName reset);
00046     W5500Interface(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset);
00047     
00048     int init();
00049     int init(uint8_t * mac);
00050     int init(const char* ip, const char* mask, const char* gateway);
00051     int init(uint8_t * mac, const char* ip, const char* mask, const char* gateway);
00052 
00053     /** Start the interface
00054      *  @return             0 on success, negative on failure
00055      */
00056     virtual int connect();
00057 
00058     /** Stop the interface
00059      *  @return             0 on success, negative on failure
00060      */
00061     virtual int disconnect();
00062 
00063     /** Get the internally stored IP address
00064      *  @return             IP address of the interface or null if not yet connected
00065      */
00066     virtual const char *get_ip_address();
00067 
00068     /** Get MAC address and fill mac with it. 
00069     */
00070     void get_mac(uint8_t mac[6]) ;
00071 
00072     /** Get the internally stored MAC address
00073      *  @return             MAC address of the interface
00074      */
00075     virtual const char *get_mac_address();
00076 
00077 protected:
00078     /** Opens a socket
00079      *
00080      *  Creates a network socket and stores it in the specified handle.
00081      *  The handle must be passed to following calls on the socket.
00082      *
00083      *  A stack may have a finite number of sockets, in this case
00084      *  NSAPI_ERROR_NO_SOCKET is returned if no socket is available.
00085      *
00086      *  @param handle   Destination for the handle to a newly created socket
00087      *  @param proto    Protocol of socket to open, NSAPI_TCP or NSAPI_UDP
00088      *  @return         0 on success, negative error code on failure
00089      */
00090     virtual nsapi_error_t socket_open(nsapi_socket_t *handle, nsapi_protocol_t proto);
00091 
00092     /** Close the socket
00093      *
00094      *  Closes any open connection and deallocates any memory associated
00095      *  with the socket.
00096      *
00097      *  @param handle   Socket handle
00098      *  @return         0 on success, negative error code on failure
00099      */
00100     virtual nsapi_error_t socket_close(nsapi_socket_t handle);
00101 
00102     /** Bind a specific address to a socket
00103      *
00104      *  Binding a socket specifies the address and port on which to recieve
00105      *  data. If the IP address is zeroed, only the port is bound.
00106      *
00107      *  @param handle   Socket handle
00108      *  @param address  Local address to bind
00109      *  @return         0 on success, negative error code on failure.
00110      */
00111     virtual nsapi_error_t socket_bind(nsapi_socket_t handle, const SocketAddress &address);
00112 
00113     /** Listen for connections on a TCP socket
00114      *
00115      *  Marks the socket as a passive socket that can be used to accept
00116      *  incoming connections.
00117      *
00118      *  @param handle   Socket handle
00119      *  @param backlog  Number of pending connections that can be queued
00120      *                  simultaneously
00121      *  @return         0 on success, negative error code on failure
00122      */
00123     virtual nsapi_error_t socket_listen(nsapi_socket_t handle, int backlog);
00124 
00125     /** Connects TCP socket to a remote host
00126      *
00127      *  Initiates a connection to a remote server specified by the
00128      *  indicated address.
00129      *
00130      *  @param handle   Socket handle
00131      *  @param address  The SocketAddress of the remote host
00132      *  @return         0 on success, negative error code on failure
00133      */
00134     virtual nsapi_error_t socket_connect(nsapi_socket_t handle, const SocketAddress &address);
00135 
00136     /** Accepts a connection on a TCP socket
00137      *
00138      *  The server socket must be bound and set to listen for connections.
00139      *  On a new connection, creates a network socket and stores it in the
00140      *  specified handle. The handle must be passed to following calls on
00141      *  the socket.
00142      *
00143      *  A stack may have a finite number of sockets, in this case
00144      *  NSAPI_ERROR_NO_SOCKET is returned if no socket is available.
00145      *
00146      *  This call is non-blocking. If accept would block,
00147      *  NSAPI_ERROR_WOULD_BLOCK is returned immediately.
00148      *
00149      *  @param server   Socket handle to server to accept from
00150      *  @param handle   Destination for a handle to the newly created socket
00151      *  @param address  Destination for the remote address or NULL
00152      *  @return         0 on success, negative error code on failure
00153      */
00154     virtual nsapi_error_t socket_accept(nsapi_socket_t server,
00155             nsapi_socket_t *handle, SocketAddress *address=0);
00156      
00157     /** Send data over a TCP socket
00158      *
00159      *  The socket must be connected to a remote host. Returns the number of
00160      *  bytes sent from the buffer.
00161      *
00162      *  This call is non-blocking. If send would block,
00163      *  NSAPI_ERROR_WOULD_BLOCK is returned immediately.
00164      *
00165      *  @param handle   Socket handle
00166      *  @param data     Buffer of data to send to the host
00167      *  @param size     Size of the buffer in bytes
00168      *  @return         Number of sent bytes on success, negative error
00169      *                  code on failure
00170      */
00171     virtual nsapi_size_or_error_t socket_send(nsapi_socket_t handle,
00172             const void *data, nsapi_size_t size);
00173 
00174     /** Receive data over a TCP socket
00175      *
00176      *  The socket must be connected to a remote host. Returns the number of
00177      *  bytes received into the buffer.
00178      *
00179      *  This call is non-blocking. If recv would block,
00180      *  NSAPI_ERROR_WOULD_BLOCK is returned immediately.
00181      *
00182      *  @param handle   Socket handle
00183      *  @param data     Destination buffer for data received from the host
00184      *  @param size     Size of the buffer in bytes
00185      *  @return         Number of received bytes on success, negative error
00186      *                  code on failure
00187      */
00188     virtual nsapi_size_or_error_t socket_recv(nsapi_socket_t handle,
00189             void *data, nsapi_size_t size);
00190 
00191     /** Send a packet over a UDP socket
00192      *
00193      *  Sends data to the specified address. Returns the number of bytes
00194      *  sent from the buffer.
00195      *
00196      *  This call is non-blocking. If sendto would block,
00197      *  NSAPI_ERROR_WOULD_BLOCK is returned immediately.
00198      *
00199      *  @param handle   Socket handle
00200      *  @param address  The SocketAddress of the remote host
00201      *  @param data     Buffer of data to send to the host
00202      *  @param size     Size of the buffer in bytes
00203      *  @return         Number of sent bytes on success, negative error
00204      *                  code on failure
00205      */
00206     virtual nsapi_size_or_error_t socket_sendto(nsapi_socket_t handle, const SocketAddress &address,
00207             const void *data, nsapi_size_t size);
00208 
00209     /** Receive a packet over a UDP socket
00210      *
00211      *  Receives data and stores the source address in address if address
00212      *  is not NULL. Returns the number of bytes received into the buffer.
00213      *
00214      *  This call is non-blocking. If recvfrom would block,
00215      *  NSAPI_ERROR_WOULD_BLOCK is returned immediately.
00216      *
00217      *  @param handle   Socket handle
00218      *  @param address  Destination for the source address or NULL
00219      *  @param data     Destination buffer for data received from the host
00220      *  @param size     Size of the buffer in bytes
00221      *  @return         Number of received bytes on success, negative error
00222      *                  code on failure
00223      */
00224     virtual nsapi_size_or_error_t socket_recvfrom(nsapi_socket_t handle, SocketAddress *address,
00225             void *buffer, nsapi_size_t size);
00226 
00227     /** Register a callback on state change of the socket
00228      *
00229      *  The specified callback will be called on state changes such as when
00230      *  the socket can recv/send/accept successfully and on when an error
00231      *  occurs. The callback may also be called spuriously without reason.
00232      *
00233      *  The callback may be called in an interrupt context and should not
00234      *  perform expensive operations such as recv/send calls.
00235      *
00236      *  @param handle   Socket handle
00237      *  @param callback Function to call on state change
00238      *  @param data     Argument to pass to callback
00239      */
00240     virtual void socket_attach(nsapi_socket_t handle, void (*callback)(void *), void *data);
00241     
00242     virtual NetworkStack* get_stack() {return this;}
00243     
00244 private:
00245     char ip_string[20];
00246     char mask_string[20];
00247     char gw_string[20];
00248     char mac_string[20];
00249     bool ip_set;    
00250     
00251     int listen_port;
00252     
00253     void signal_event(nsapi_socket_t handle);
00254     
00255     //w5500 socket management
00256     struct w5500_socket w5500_sockets[MAX_SOCK_NUM];
00257     w5500_socket* get_sock(int fd);
00258     void init_socks();
00259 
00260     Thread *_daemon;
00261     void daemon();
00262 
00263 };
00264 
00265 #endif