Update of W5500 Interface for mbed-os
Embed:
(wiki syntax)
Show/hide line numbers
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 the internally stored MAC address 00069 * @return MAC address of the interface 00070 */ 00071 virtual const char *get_mac_address(); 00072 00073 protected: 00074 /** Opens a socket 00075 * 00076 * Creates a network socket and stores it in the specified handle. 00077 * The handle must be passed to following calls on the socket. 00078 * 00079 * A stack may have a finite number of sockets, in this case 00080 * NSAPI_ERROR_NO_SOCKET is returned if no socket is available. 00081 * 00082 * @param handle Destination for the handle to a newly created socket 00083 * @param proto Protocol of socket to open, NSAPI_TCP or NSAPI_UDP 00084 * @return 0 on success, negative error code on failure 00085 */ 00086 virtual nsapi_error_t socket_open(nsapi_socket_t *handle, nsapi_protocol_t proto); 00087 00088 /** Close the socket 00089 * 00090 * Closes any open connection and deallocates any memory associated 00091 * with the socket. 00092 * 00093 * @param handle Socket handle 00094 * @return 0 on success, negative error code on failure 00095 */ 00096 virtual nsapi_error_t socket_close(nsapi_socket_t handle); 00097 00098 /** Bind a specific address to a socket 00099 * 00100 * Binding a socket specifies the address and port on which to recieve 00101 * data. If the IP address is zeroed, only the port is bound. 00102 * 00103 * @param handle Socket handle 00104 * @param address Local address to bind 00105 * @return 0 on success, negative error code on failure. 00106 */ 00107 virtual nsapi_error_t socket_bind(nsapi_socket_t handle, const SocketAddress &address); 00108 00109 /** Listen for connections on a TCP socket 00110 * 00111 * Marks the socket as a passive socket that can be used to accept 00112 * incoming connections. 00113 * 00114 * @param handle Socket handle 00115 * @param backlog Number of pending connections that can be queued 00116 * simultaneously 00117 * @return 0 on success, negative error code on failure 00118 */ 00119 virtual nsapi_error_t socket_listen(nsapi_socket_t handle, int backlog); 00120 00121 /** Connects TCP socket to a remote host 00122 * 00123 * Initiates a connection to a remote server specified by the 00124 * indicated address. 00125 * 00126 * @param handle Socket handle 00127 * @param address The SocketAddress of the remote host 00128 * @return 0 on success, negative error code on failure 00129 */ 00130 virtual nsapi_error_t socket_connect(nsapi_socket_t handle, const SocketAddress &address); 00131 00132 /** Accepts a connection on a TCP socket 00133 * 00134 * The server socket must be bound and set to listen for connections. 00135 * On a new connection, creates a network socket and stores it in the 00136 * specified handle. The handle must be passed to following calls on 00137 * the socket. 00138 * 00139 * A stack may have a finite number of sockets, in this case 00140 * NSAPI_ERROR_NO_SOCKET is returned if no socket is available. 00141 * 00142 * This call is non-blocking. If accept would block, 00143 * NSAPI_ERROR_WOULD_BLOCK is returned immediately. 00144 * 00145 * @param server Socket handle to server to accept from 00146 * @param handle Destination for a handle to the newly created socket 00147 * @param address Destination for the remote address or NULL 00148 * @return 0 on success, negative error code on failure 00149 */ 00150 virtual nsapi_error_t socket_accept(nsapi_socket_t server, 00151 nsapi_socket_t *handle, SocketAddress *address=0); 00152 00153 /** Send data over a TCP socket 00154 * 00155 * The socket must be connected to a remote host. Returns the number of 00156 * bytes sent from the buffer. 00157 * 00158 * This call is non-blocking. If send would block, 00159 * NSAPI_ERROR_WOULD_BLOCK is returned immediately. 00160 * 00161 * @param handle Socket handle 00162 * @param data Buffer of data to send to the host 00163 * @param size Size of the buffer in bytes 00164 * @return Number of sent bytes on success, negative error 00165 * code on failure 00166 */ 00167 virtual nsapi_size_or_error_t socket_send(nsapi_socket_t handle, 00168 const void *data, nsapi_size_t size); 00169 00170 /** Receive data over a TCP socket 00171 * 00172 * The socket must be connected to a remote host. Returns the number of 00173 * bytes received into the buffer. 00174 * 00175 * This call is non-blocking. If recv would block, 00176 * NSAPI_ERROR_WOULD_BLOCK is returned immediately. 00177 * 00178 * @param handle Socket handle 00179 * @param data Destination buffer for data received from the host 00180 * @param size Size of the buffer in bytes 00181 * @return Number of received bytes on success, negative error 00182 * code on failure 00183 */ 00184 virtual nsapi_size_or_error_t socket_recv(nsapi_socket_t handle, 00185 void *data, nsapi_size_t size); 00186 00187 /** Send a packet over a UDP socket 00188 * 00189 * Sends data to the specified address. Returns the number of bytes 00190 * sent from the buffer. 00191 * 00192 * This call is non-blocking. If sendto would block, 00193 * NSAPI_ERROR_WOULD_BLOCK is returned immediately. 00194 * 00195 * @param handle Socket handle 00196 * @param address The SocketAddress of the remote host 00197 * @param data Buffer of data to send to the host 00198 * @param size Size of the buffer in bytes 00199 * @return Number of sent bytes on success, negative error 00200 * code on failure 00201 */ 00202 virtual nsapi_size_or_error_t socket_sendto(nsapi_socket_t handle, const SocketAddress &address, 00203 const void *data, nsapi_size_t size); 00204 00205 /** Receive a packet over a UDP socket 00206 * 00207 * Receives data and stores the source address in address if address 00208 * is not NULL. Returns the number of bytes received into the buffer. 00209 * 00210 * This call is non-blocking. If recvfrom would block, 00211 * NSAPI_ERROR_WOULD_BLOCK is returned immediately. 00212 * 00213 * @param handle Socket handle 00214 * @param address Destination for the source address or NULL 00215 * @param data Destination buffer for data received from the host 00216 * @param size Size of the buffer in bytes 00217 * @return Number of received bytes on success, negative error 00218 * code on failure 00219 */ 00220 virtual nsapi_size_or_error_t socket_recvfrom(nsapi_socket_t handle, SocketAddress *address, 00221 void *buffer, nsapi_size_t size); 00222 00223 /** Register a callback on state change of the socket 00224 * 00225 * The specified callback will be called on state changes such as when 00226 * the socket can recv/send/accept successfully and on when an error 00227 * occurs. The callback may also be called spuriously without reason. 00228 * 00229 * The callback may be called in an interrupt context and should not 00230 * perform expensive operations such as recv/send calls. 00231 * 00232 * @param handle Socket handle 00233 * @param callback Function to call on state change 00234 * @param data Argument to pass to callback 00235 */ 00236 virtual void socket_attach(nsapi_socket_t handle, void (*callback)(void *), void *data); 00237 00238 virtual NetworkStack* get_stack() {return this;} 00239 00240 private: 00241 char ip_string[20]; 00242 char mask_string[20]; 00243 char gw_string[20]; 00244 char mac_string[20]; 00245 bool ip_set; 00246 00247 int listen_port; 00248 00249 void signal_event(nsapi_socket_t handle); 00250 00251 //w5500 socket management 00252 struct w5500_socket w5500_sockets[MAX_SOCK_NUM]; 00253 w5500_socket* get_sock(int fd); 00254 void init_socks(); 00255 00256 Thread *_daemon; 00257 void daemon(); 00258 00259 }; 00260 00261 #endif
Generated on Wed Jul 13 2022 00:54:44 by 1.7.2