W5500 driver for mbed OS 5
Dependents: http-webserver-example mbed-os-example-sockets
Fork of W5500Interface by
W5500Interface.h@18:afec30f0922a, 2018-08-16 (annotated)
- Committer:
- Bongjun
- Date:
- Thu Aug 16 07:33:40 2018 +0000
- Revision:
- 18:afec30f0922a
- Parent:
- 17:60f75e78f35d
change spi frame bits : 32->8
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Bongjun | 6:e2ab76b2be07 | 1 | /** |
Bongjun | 6:e2ab76b2be07 | 2 | ****************************************************************************** |
Bongjun | 6:e2ab76b2be07 | 3 | * @file W5500Interface.h |
Bongjun | 6:e2ab76b2be07 | 4 | * @author Bongjun Hur (modified version from Sergei G (https://os.mbed.com/users/sgnezdov/)) |
Bongjun | 6:e2ab76b2be07 | 5 | * @brief Header file of the NetworkStack for the W5500 Device |
Bongjun | 6:e2ab76b2be07 | 6 | ****************************************************************************** |
Bongjun | 6:e2ab76b2be07 | 7 | * @attention |
Bongjun | 6:e2ab76b2be07 | 8 | * |
Bongjun | 6:e2ab76b2be07 | 9 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS |
Bongjun | 6:e2ab76b2be07 | 10 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE |
Bongjun | 6:e2ab76b2be07 | 11 | * TIME. AS A RESULT, WIZnet SHALL NOT BE HELD LIABLE FOR ANY |
Bongjun | 6:e2ab76b2be07 | 12 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING |
Bongjun | 6:e2ab76b2be07 | 13 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE |
Bongjun | 6:e2ab76b2be07 | 14 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. |
Bongjun | 6:e2ab76b2be07 | 15 | * |
Bongjun | 6:e2ab76b2be07 | 16 | * <h2><center>© COPYRIGHT 2017,2018 WIZnet Co.,Ltd.</center></h2> |
Bongjun | 6:e2ab76b2be07 | 17 | ****************************************************************************** |
Bongjun | 6:e2ab76b2be07 | 18 | */ |
Bongjun | 6:e2ab76b2be07 | 19 | |
Bongjun | 6:e2ab76b2be07 | 20 | #ifndef W5500_INTERFACE_H |
Bongjun | 6:e2ab76b2be07 | 21 | #define W5500_INTERFACE_H |
Bongjun | 6:e2ab76b2be07 | 22 | |
Bongjun | 6:e2ab76b2be07 | 23 | #include "mbed.h" |
Bongjun | 6:e2ab76b2be07 | 24 | #include "W5500.h" |
Bongjun | 6:e2ab76b2be07 | 25 | //#include "rtos.h" |
Bongjun | 8:c71c66d43703 | 26 | #include "PinNames.h" |
Bongjun | 12:3a5f5b5ae5f8 | 27 | #include "DHCPClient.h" |
Bongjun | 11:5118c2bff025 | 28 | #include "DNSClient.h" |
Bongjun | 8:c71c66d43703 | 29 | |
Bongjun | 8:c71c66d43703 | 30 | // Arduino pin defaults for convenience |
Bongjun | 8:c71c66d43703 | 31 | #if !defined(W5500_SPI_MOSI) |
Bongjun | 8:c71c66d43703 | 32 | #define W5500_SPI_MOSI D11 |
Bongjun | 8:c71c66d43703 | 33 | #endif |
Bongjun | 8:c71c66d43703 | 34 | #if !defined(W5500_SPI_MISO) |
Bongjun | 8:c71c66d43703 | 35 | #define W5500_SPI_MISO D12 |
Bongjun | 8:c71c66d43703 | 36 | #endif |
Bongjun | 8:c71c66d43703 | 37 | #if !defined(W5500_SPI_SCLK) |
Bongjun | 8:c71c66d43703 | 38 | #define W5500_SPI_SCLK D13 |
Bongjun | 8:c71c66d43703 | 39 | #endif |
Bongjun | 8:c71c66d43703 | 40 | #if !defined(W5500_SPI_CS) |
Bongjun | 8:c71c66d43703 | 41 | #define W5500_SPI_CS D10 |
Bongjun | 8:c71c66d43703 | 42 | #endif |
Bongjun | 8:c71c66d43703 | 43 | #if !defined(W5500_SPI_RST) |
Bongjun | 8:c71c66d43703 | 44 | #define W5500_SPI_RST NC |
Bongjun | 8:c71c66d43703 | 45 | #endif |
Bongjun | 8:c71c66d43703 | 46 | |
Bongjun | 6:e2ab76b2be07 | 47 | |
Bongjun | 6:e2ab76b2be07 | 48 | /** w5500_socket struct |
Bongjun | 6:e2ab76b2be07 | 49 | * W5500 socket |
Bongjun | 6:e2ab76b2be07 | 50 | */ |
Bongjun | 6:e2ab76b2be07 | 51 | |
Bongjun | 6:e2ab76b2be07 | 52 | struct w5500_socket { |
Bongjun | 6:e2ab76b2be07 | 53 | int fd; |
Bongjun | 6:e2ab76b2be07 | 54 | nsapi_protocol_t proto; |
Bongjun | 6:e2ab76b2be07 | 55 | bool connected; |
Bongjun | 6:e2ab76b2be07 | 56 | void (*callback)(void *); |
Bongjun | 6:e2ab76b2be07 | 57 | void *callback_data; |
Bongjun | 6:e2ab76b2be07 | 58 | }; |
Bongjun | 6:e2ab76b2be07 | 59 | |
Bongjun | 6:e2ab76b2be07 | 60 | /** W5500Interface class |
Bongjun | 6:e2ab76b2be07 | 61 | * Implementation of the NetworkStack for the W5500 |
Bongjun | 6:e2ab76b2be07 | 62 | */ |
Bongjun | 6:e2ab76b2be07 | 63 | |
Bongjun | 6:e2ab76b2be07 | 64 | class W5500Interface : public NetworkStack, public EthInterface |
Bongjun | 6:e2ab76b2be07 | 65 | { |
Bongjun | 6:e2ab76b2be07 | 66 | public: |
Bongjun | 8:c71c66d43703 | 67 | //W5500Interface(SPI* spi, PinName cs, PinName reset); |
Bongjun | 6:e2ab76b2be07 | 68 | W5500Interface(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset); |
Bongjun | 6:e2ab76b2be07 | 69 | |
Bongjun | 6:e2ab76b2be07 | 70 | int init(); |
Bongjun | 6:e2ab76b2be07 | 71 | int init(uint8_t * mac); |
Bongjun | 6:e2ab76b2be07 | 72 | int init(const char* ip, const char* mask, const char* gateway); |
Bongjun | 6:e2ab76b2be07 | 73 | int init(uint8_t * mac, const char* ip, const char* mask, const char* gateway); |
Bongjun | 6:e2ab76b2be07 | 74 | |
Bongjun | 6:e2ab76b2be07 | 75 | /** Start the interface |
Bongjun | 6:e2ab76b2be07 | 76 | * @return 0 on success, negative on failure |
Bongjun | 6:e2ab76b2be07 | 77 | */ |
Bongjun | 6:e2ab76b2be07 | 78 | virtual int connect(); |
Bongjun | 6:e2ab76b2be07 | 79 | |
Bongjun | 6:e2ab76b2be07 | 80 | /** Stop the interface |
Bongjun | 6:e2ab76b2be07 | 81 | * @return 0 on success, negative on failure |
Bongjun | 6:e2ab76b2be07 | 82 | */ |
Bongjun | 6:e2ab76b2be07 | 83 | virtual int disconnect(); |
Bongjun | 6:e2ab76b2be07 | 84 | |
Bongjun | 6:e2ab76b2be07 | 85 | /** Get the internally stored IP address |
Bongjun | 6:e2ab76b2be07 | 86 | * @return IP address of the interface or null if not yet connected |
Bongjun | 6:e2ab76b2be07 | 87 | */ |
Bongjun | 6:e2ab76b2be07 | 88 | virtual const char *get_ip_address(); |
Bongjun | 11:5118c2bff025 | 89 | /** Get the local gateway |
Bongjun | 11:5118c2bff025 | 90 | * |
Bongjun | 11:5118c2bff025 | 91 | * @return Null-terminated representation of the local gateway |
Bongjun | 11:5118c2bff025 | 92 | * or null if no network mask has been recieved |
Bongjun | 11:5118c2bff025 | 93 | */ |
Bongjun | 10:925201b1603f | 94 | virtual const char *get_gateway(); |
Bongjun | 10:925201b1603f | 95 | |
Bongjun | 11:5118c2bff025 | 96 | /** Get the local network mask |
Bongjun | 11:5118c2bff025 | 97 | * |
Bongjun | 11:5118c2bff025 | 98 | * @return Null-terminated representation of the local network mask |
Bongjun | 11:5118c2bff025 | 99 | * or null if no network mask has been recieved |
Bongjun | 11:5118c2bff025 | 100 | */ |
Bongjun | 11:5118c2bff025 | 101 | virtual const char *get_netmask(); |
Bongjun | 6:e2ab76b2be07 | 102 | |
Bongjun | 6:e2ab76b2be07 | 103 | /** Get MAC address and fill mac with it. |
Bongjun | 6:e2ab76b2be07 | 104 | */ |
Bongjun | 6:e2ab76b2be07 | 105 | void get_mac(uint8_t mac[6]) ; |
Bongjun | 6:e2ab76b2be07 | 106 | |
Bongjun | 6:e2ab76b2be07 | 107 | /** Get the internally stored MAC address |
Bongjun | 6:e2ab76b2be07 | 108 | * @return MAC address of the interface |
Bongjun | 6:e2ab76b2be07 | 109 | */ |
Bongjun | 6:e2ab76b2be07 | 110 | virtual const char *get_mac_address(); |
Bongjun | 11:5118c2bff025 | 111 | |
Bongjun | 18:afec30f0922a | 112 | /** Set a static IP address |
Bongjun | 18:afec30f0922a | 113 | * |
Bongjun | 18:afec30f0922a | 114 | * Configures this network interface to use a static IP address. |
Bongjun | 18:afec30f0922a | 115 | * Implicitly disables DHCP, which can be enabled in set_dhcp. |
Bongjun | 18:afec30f0922a | 116 | * Requires that the network is disconnected. |
Bongjun | 18:afec30f0922a | 117 | * |
Bongjun | 18:afec30f0922a | 118 | * @param ip_address Null-terminated representation of the local IP address |
Bongjun | 18:afec30f0922a | 119 | * @param netmask Null-terminated representation of the local network mask |
Bongjun | 18:afec30f0922a | 120 | * @param gateway Null-terminated representation of the local gateway |
Bongjun | 18:afec30f0922a | 121 | * @return 0 on success, negative error code on failure |
Bongjun | 18:afec30f0922a | 122 | */ |
Bongjun | 18:afec30f0922a | 123 | virtual nsapi_error_t set_network(const char *ip_address, const char *netmask, const char *gateway); |
Bongjun | 18:afec30f0922a | 124 | |
Bongjun | 18:afec30f0922a | 125 | /** Enable or disable DHCP on the network |
Bongjun | 18:afec30f0922a | 126 | * |
Bongjun | 18:afec30f0922a | 127 | * Enables DHCP on connecting the network. Defaults to enabled unless |
Bongjun | 18:afec30f0922a | 128 | * a static IP address has been assigned. Requires that the network is |
Bongjun | 18:afec30f0922a | 129 | * disconnected. |
Bongjun | 18:afec30f0922a | 130 | * |
Bongjun | 18:afec30f0922a | 131 | * @param dhcp True to enable DHCP |
Bongjun | 18:afec30f0922a | 132 | * @return 0 on success, negative error code on failure |
Bongjun | 18:afec30f0922a | 133 | */ |
Bongjun | 18:afec30f0922a | 134 | virtual nsapi_error_t set_dhcp(bool dhcp); |
Bongjun | 11:5118c2bff025 | 135 | |
Bongjun | 11:5118c2bff025 | 136 | bool setDnsServerIP(const char* ip_address); |
Bongjun | 11:5118c2bff025 | 137 | |
Bongjun | 11:5118c2bff025 | 138 | /** Translates a hostname to an IP address with specific version |
Bongjun | 11:5118c2bff025 | 139 | * |
Bongjun | 11:5118c2bff025 | 140 | * The hostname may be either a domain name or an IP address. If the |
Bongjun | 11:5118c2bff025 | 141 | * hostname is an IP address, no network transactions will be performed. |
Bongjun | 11:5118c2bff025 | 142 | * |
Bongjun | 11:5118c2bff025 | 143 | * If no stack-specific DNS resolution is provided, the hostname |
Bongjun | 11:5118c2bff025 | 144 | * will be resolve using a UDP socket on the stack. |
Bongjun | 11:5118c2bff025 | 145 | * |
Bongjun | 11:5118c2bff025 | 146 | * @param address Destination for the host SocketAddress |
Bongjun | 11:5118c2bff025 | 147 | * @param host Hostname to resolve |
Bongjun | 11:5118c2bff025 | 148 | * @param version IP version of address to resolve, NSAPI_UNSPEC indicates |
Bongjun | 11:5118c2bff025 | 149 | * version is chosen by the stack (defaults to NSAPI_UNSPEC) |
Bongjun | 11:5118c2bff025 | 150 | * @return 0 on success, negative error code on failure |
Bongjun | 11:5118c2bff025 | 151 | */ |
Bongjun | 11:5118c2bff025 | 152 | //using NetworkInterface::gethostbyname; |
Bongjun | 11:5118c2bff025 | 153 | virtual nsapi_error_t gethostbyname(const char *host, |
Bongjun | 11:5118c2bff025 | 154 | SocketAddress *address, nsapi_version_t version = NSAPI_UNSPEC); |
Bongjun | 16:380b4a629c4f | 155 | |
Bongjun | 16:380b4a629c4f | 156 | int IPrenew(int timeout_ms = 15*1000); |
Bongjun | 17:60f75e78f35d | 157 | uint32_t str_to_ip(const char* str); |
Bongjun | 6:e2ab76b2be07 | 158 | |
Bongjun | 6:e2ab76b2be07 | 159 | protected: |
Bongjun | 6:e2ab76b2be07 | 160 | /** Opens a socket |
Bongjun | 6:e2ab76b2be07 | 161 | * |
Bongjun | 6:e2ab76b2be07 | 162 | * Creates a network socket and stores it in the specified handle. |
Bongjun | 6:e2ab76b2be07 | 163 | * The handle must be passed to following calls on the socket. |
Bongjun | 6:e2ab76b2be07 | 164 | * |
Bongjun | 6:e2ab76b2be07 | 165 | * A stack may have a finite number of sockets, in this case |
Bongjun | 6:e2ab76b2be07 | 166 | * NSAPI_ERROR_NO_SOCKET is returned if no socket is available. |
Bongjun | 6:e2ab76b2be07 | 167 | * |
Bongjun | 6:e2ab76b2be07 | 168 | * @param handle Destination for the handle to a newly created socket |
Bongjun | 6:e2ab76b2be07 | 169 | * @param proto Protocol of socket to open, NSAPI_TCP or NSAPI_UDP |
Bongjun | 6:e2ab76b2be07 | 170 | * @return 0 on success, negative error code on failure |
Bongjun | 6:e2ab76b2be07 | 171 | */ |
Bongjun | 6:e2ab76b2be07 | 172 | virtual nsapi_error_t socket_open(nsapi_socket_t *handle, nsapi_protocol_t proto); |
Bongjun | 6:e2ab76b2be07 | 173 | // virtual int socket_open(void **handle, nsapi_protocol_t proto); |
Bongjun | 6:e2ab76b2be07 | 174 | |
Bongjun | 6:e2ab76b2be07 | 175 | /** Close the socket |
Bongjun | 6:e2ab76b2be07 | 176 | * |
Bongjun | 6:e2ab76b2be07 | 177 | * Closes any open connection and deallocates any memory associated |
Bongjun | 6:e2ab76b2be07 | 178 | * with the socket. |
Bongjun | 6:e2ab76b2be07 | 179 | * |
Bongjun | 6:e2ab76b2be07 | 180 | * @param handle Socket handle |
Bongjun | 6:e2ab76b2be07 | 181 | * @return 0 on success, negative error code on failure |
Bongjun | 6:e2ab76b2be07 | 182 | */ |
Bongjun | 6:e2ab76b2be07 | 183 | virtual nsapi_error_t socket_close(nsapi_socket_t handle); |
Bongjun | 6:e2ab76b2be07 | 184 | |
Bongjun | 6:e2ab76b2be07 | 185 | /** Bind a specific address to a socket |
Bongjun | 6:e2ab76b2be07 | 186 | * |
Bongjun | 6:e2ab76b2be07 | 187 | * Binding a socket specifies the address and port on which to recieve |
Bongjun | 6:e2ab76b2be07 | 188 | * data. If the IP address is zeroed, only the port is bound. |
Bongjun | 6:e2ab76b2be07 | 189 | * |
Bongjun | 6:e2ab76b2be07 | 190 | * @param handle Socket handle |
Bongjun | 6:e2ab76b2be07 | 191 | * @param address Local address to bind |
Bongjun | 6:e2ab76b2be07 | 192 | * @return 0 on success, negative error code on failure. |
Bongjun | 6:e2ab76b2be07 | 193 | */ |
Bongjun | 6:e2ab76b2be07 | 194 | virtual nsapi_error_t socket_bind(nsapi_socket_t handle, const SocketAddress &address); |
Bongjun | 6:e2ab76b2be07 | 195 | |
Bongjun | 6:e2ab76b2be07 | 196 | /** Listen for connections on a TCP socket |
Bongjun | 6:e2ab76b2be07 | 197 | * |
Bongjun | 6:e2ab76b2be07 | 198 | * Marks the socket as a passive socket that can be used to accept |
Bongjun | 6:e2ab76b2be07 | 199 | * incoming connections. |
Bongjun | 6:e2ab76b2be07 | 200 | * |
Bongjun | 6:e2ab76b2be07 | 201 | * @param handle Socket handle |
Bongjun | 6:e2ab76b2be07 | 202 | * @param backlog Number of pending connections that can be queued |
Bongjun | 6:e2ab76b2be07 | 203 | * simultaneously |
Bongjun | 6:e2ab76b2be07 | 204 | * @return 0 on success, negative error code on failure |
Bongjun | 6:e2ab76b2be07 | 205 | */ |
Bongjun | 6:e2ab76b2be07 | 206 | virtual nsapi_error_t socket_listen(nsapi_socket_t handle, int backlog); |
Bongjun | 6:e2ab76b2be07 | 207 | |
Bongjun | 6:e2ab76b2be07 | 208 | /** Connects TCP socket to a remote host |
Bongjun | 6:e2ab76b2be07 | 209 | * |
Bongjun | 6:e2ab76b2be07 | 210 | * Initiates a connection to a remote server specified by the |
Bongjun | 6:e2ab76b2be07 | 211 | * indicated address. |
Bongjun | 6:e2ab76b2be07 | 212 | * |
Bongjun | 6:e2ab76b2be07 | 213 | * @param handle Socket handle |
Bongjun | 6:e2ab76b2be07 | 214 | * @param address The SocketAddress of the remote host |
Bongjun | 6:e2ab76b2be07 | 215 | * @return 0 on success, negative error code on failure |
Bongjun | 6:e2ab76b2be07 | 216 | */ |
Bongjun | 6:e2ab76b2be07 | 217 | virtual nsapi_error_t socket_connect(nsapi_socket_t handle, const SocketAddress &address); |
Bongjun | 6:e2ab76b2be07 | 218 | |
Bongjun | 6:e2ab76b2be07 | 219 | /** Accepts a connection on a TCP socket |
Bongjun | 6:e2ab76b2be07 | 220 | * |
Bongjun | 6:e2ab76b2be07 | 221 | * The server socket must be bound and set to listen for connections. |
Bongjun | 6:e2ab76b2be07 | 222 | * On a new connection, creates a network socket and stores it in the |
Bongjun | 6:e2ab76b2be07 | 223 | * specified handle. The handle must be passed to following calls on |
Bongjun | 6:e2ab76b2be07 | 224 | * the socket. |
Bongjun | 6:e2ab76b2be07 | 225 | * |
Bongjun | 6:e2ab76b2be07 | 226 | * A stack may have a finite number of sockets, in this case |
Bongjun | 6:e2ab76b2be07 | 227 | * NSAPI_ERROR_NO_SOCKET is returned if no socket is available. |
Bongjun | 6:e2ab76b2be07 | 228 | * |
Bongjun | 6:e2ab76b2be07 | 229 | * This call is non-blocking. If accept would block, |
Bongjun | 6:e2ab76b2be07 | 230 | * NSAPI_ERROR_WOULD_BLOCK is returned immediately. |
Bongjun | 6:e2ab76b2be07 | 231 | * |
Bongjun | 6:e2ab76b2be07 | 232 | * @param server Socket handle to server to accept from |
Bongjun | 6:e2ab76b2be07 | 233 | * @param handle Destination for a handle to the newly created socket |
Bongjun | 6:e2ab76b2be07 | 234 | * @param address Destination for the remote address or NULL |
Bongjun | 6:e2ab76b2be07 | 235 | * @return 0 on success, negative error code on failure |
Bongjun | 6:e2ab76b2be07 | 236 | */ |
Bongjun | 6:e2ab76b2be07 | 237 | virtual nsapi_error_t socket_accept(nsapi_socket_t server, |
Bongjun | 6:e2ab76b2be07 | 238 | nsapi_socket_t *handle, SocketAddress *address=0); |
Bongjun | 6:e2ab76b2be07 | 239 | |
Bongjun | 6:e2ab76b2be07 | 240 | /** Send data over a TCP socket |
Bongjun | 6:e2ab76b2be07 | 241 | * |
Bongjun | 6:e2ab76b2be07 | 242 | * The socket must be connected to a remote host. Returns the number of |
Bongjun | 6:e2ab76b2be07 | 243 | * bytes sent from the buffer. |
Bongjun | 6:e2ab76b2be07 | 244 | * |
Bongjun | 6:e2ab76b2be07 | 245 | * This call is non-blocking. If send would block, |
Bongjun | 6:e2ab76b2be07 | 246 | * NSAPI_ERROR_WOULD_BLOCK is returned immediately. |
Bongjun | 6:e2ab76b2be07 | 247 | * |
Bongjun | 6:e2ab76b2be07 | 248 | * @param handle Socket handle |
Bongjun | 6:e2ab76b2be07 | 249 | * @param data Buffer of data to send to the host |
Bongjun | 6:e2ab76b2be07 | 250 | * @param size Size of the buffer in bytes |
Bongjun | 6:e2ab76b2be07 | 251 | * @return Number of sent bytes on success, negative error |
Bongjun | 6:e2ab76b2be07 | 252 | * code on failure |
Bongjun | 6:e2ab76b2be07 | 253 | */ |
Bongjun | 6:e2ab76b2be07 | 254 | virtual nsapi_size_or_error_t socket_send(nsapi_socket_t handle, |
Bongjun | 6:e2ab76b2be07 | 255 | const void *data, nsapi_size_t size); |
Bongjun | 6:e2ab76b2be07 | 256 | |
Bongjun | 6:e2ab76b2be07 | 257 | /** Receive data over a TCP socket |
Bongjun | 6:e2ab76b2be07 | 258 | * |
Bongjun | 6:e2ab76b2be07 | 259 | * The socket must be connected to a remote host. Returns the number of |
Bongjun | 6:e2ab76b2be07 | 260 | * bytes received into the buffer. |
Bongjun | 6:e2ab76b2be07 | 261 | * |
Bongjun | 6:e2ab76b2be07 | 262 | * This call is non-blocking. If recv would block, |
Bongjun | 6:e2ab76b2be07 | 263 | * NSAPI_ERROR_WOULD_BLOCK is returned immediately. |
Bongjun | 6:e2ab76b2be07 | 264 | * |
Bongjun | 6:e2ab76b2be07 | 265 | * @param handle Socket handle |
Bongjun | 6:e2ab76b2be07 | 266 | * @param data Destination buffer for data received from the host |
Bongjun | 6:e2ab76b2be07 | 267 | * @param size Size of the buffer in bytes |
Bongjun | 6:e2ab76b2be07 | 268 | * @return Number of received bytes on success, negative error |
Bongjun | 6:e2ab76b2be07 | 269 | * code on failure |
Bongjun | 6:e2ab76b2be07 | 270 | */ |
Bongjun | 6:e2ab76b2be07 | 271 | virtual nsapi_size_or_error_t socket_recv(nsapi_socket_t handle, |
Bongjun | 6:e2ab76b2be07 | 272 | void *data, nsapi_size_t size); |
Bongjun | 6:e2ab76b2be07 | 273 | |
Bongjun | 6:e2ab76b2be07 | 274 | /** Send a packet over a UDP socket |
Bongjun | 6:e2ab76b2be07 | 275 | * |
Bongjun | 6:e2ab76b2be07 | 276 | * Sends data to the specified address. Returns the number of bytes |
Bongjun | 6:e2ab76b2be07 | 277 | * sent from the buffer. |
Bongjun | 6:e2ab76b2be07 | 278 | * |
Bongjun | 6:e2ab76b2be07 | 279 | * This call is non-blocking. If sendto would block, |
Bongjun | 6:e2ab76b2be07 | 280 | * NSAPI_ERROR_WOULD_BLOCK is returned immediately. |
Bongjun | 6:e2ab76b2be07 | 281 | * |
Bongjun | 6:e2ab76b2be07 | 282 | * @param handle Socket handle |
Bongjun | 6:e2ab76b2be07 | 283 | * @param address The SocketAddress of the remote host |
Bongjun | 6:e2ab76b2be07 | 284 | * @param data Buffer of data to send to the host |
Bongjun | 6:e2ab76b2be07 | 285 | * @param size Size of the buffer in bytes |
Bongjun | 6:e2ab76b2be07 | 286 | * @return Number of sent bytes on success, negative error |
Bongjun | 6:e2ab76b2be07 | 287 | * code on failure |
Bongjun | 6:e2ab76b2be07 | 288 | */ |
Bongjun | 6:e2ab76b2be07 | 289 | virtual nsapi_size_or_error_t socket_sendto(nsapi_socket_t handle, const SocketAddress &address, |
Bongjun | 6:e2ab76b2be07 | 290 | const void *data, nsapi_size_t size); |
Bongjun | 6:e2ab76b2be07 | 291 | |
Bongjun | 6:e2ab76b2be07 | 292 | /** Receive a packet over a UDP socket |
Bongjun | 6:e2ab76b2be07 | 293 | * |
Bongjun | 6:e2ab76b2be07 | 294 | * Receives data and stores the source address in address if address |
Bongjun | 6:e2ab76b2be07 | 295 | * is not NULL. Returns the number of bytes received into the buffer. |
Bongjun | 6:e2ab76b2be07 | 296 | * |
Bongjun | 6:e2ab76b2be07 | 297 | * This call is non-blocking. If recvfrom would block, |
Bongjun | 6:e2ab76b2be07 | 298 | * NSAPI_ERROR_WOULD_BLOCK is returned immediately. |
Bongjun | 6:e2ab76b2be07 | 299 | * |
Bongjun | 6:e2ab76b2be07 | 300 | * @param handle Socket handle |
Bongjun | 6:e2ab76b2be07 | 301 | * @param address Destination for the source address or NULL |
Bongjun | 6:e2ab76b2be07 | 302 | * @param data Destination buffer for data received from the host |
Bongjun | 6:e2ab76b2be07 | 303 | * @param size Size of the buffer in bytes |
Bongjun | 6:e2ab76b2be07 | 304 | * @return Number of received bytes on success, negative error |
Bongjun | 6:e2ab76b2be07 | 305 | * code on failure |
Bongjun | 6:e2ab76b2be07 | 306 | */ |
Bongjun | 6:e2ab76b2be07 | 307 | virtual nsapi_size_or_error_t socket_recvfrom(nsapi_socket_t handle, SocketAddress *address, |
Bongjun | 6:e2ab76b2be07 | 308 | void *buffer, nsapi_size_t size); |
Bongjun | 6:e2ab76b2be07 | 309 | |
Bongjun | 6:e2ab76b2be07 | 310 | /** Register a callback on state change of the socket |
Bongjun | 6:e2ab76b2be07 | 311 | * |
Bongjun | 6:e2ab76b2be07 | 312 | * The specified callback will be called on state changes such as when |
Bongjun | 6:e2ab76b2be07 | 313 | * the socket can recv/send/accept successfully and on when an error |
Bongjun | 6:e2ab76b2be07 | 314 | * occurs. The callback may also be called spuriously without reason. |
Bongjun | 6:e2ab76b2be07 | 315 | * |
Bongjun | 6:e2ab76b2be07 | 316 | * The callback may be called in an interrupt context and should not |
Bongjun | 6:e2ab76b2be07 | 317 | * perform expensive operations such as recv/send calls. |
Bongjun | 6:e2ab76b2be07 | 318 | * |
Bongjun | 6:e2ab76b2be07 | 319 | * @param handle Socket handle |
Bongjun | 6:e2ab76b2be07 | 320 | * @param callback Function to call on state change |
Bongjun | 6:e2ab76b2be07 | 321 | * @param data Argument to pass to callback |
Bongjun | 6:e2ab76b2be07 | 322 | */ |
Bongjun | 6:e2ab76b2be07 | 323 | virtual void socket_attach(nsapi_socket_t handle, void (*callback)(void *), void *data); |
Bongjun | 6:e2ab76b2be07 | 324 | |
Bongjun | 6:e2ab76b2be07 | 325 | virtual NetworkStack* get_stack() {return this;} |
Bongjun | 6:e2ab76b2be07 | 326 | |
Bongjun | 6:e2ab76b2be07 | 327 | private: |
Bongjun | 6:e2ab76b2be07 | 328 | WIZnet_Chip _w5500; |
Bongjun | 6:e2ab76b2be07 | 329 | |
Bongjun | 18:afec30f0922a | 330 | char ip_string[NSAPI_IPv4_SIZE]; |
Bongjun | 18:afec30f0922a | 331 | char netmask_string[NSAPI_IPv4_SIZE]; |
Bongjun | 18:afec30f0922a | 332 | char gateway_string[NSAPI_IPv4_SIZE]; |
Bongjun | 18:afec30f0922a | 333 | char mac_string[NSAPI_MAC_SIZE]; |
Bongjun | 6:e2ab76b2be07 | 334 | bool ip_set; |
Bongjun | 6:e2ab76b2be07 | 335 | |
Bongjun | 6:e2ab76b2be07 | 336 | int listen_port; |
Bongjun | 6:e2ab76b2be07 | 337 | |
Bongjun | 6:e2ab76b2be07 | 338 | //void signal_event(nsapi_socket_t handle); |
Bongjun | 6:e2ab76b2be07 | 339 | //void event(); |
Bongjun | 6:e2ab76b2be07 | 340 | |
Bongjun | 6:e2ab76b2be07 | 341 | //w5500 socket management |
Bongjun | 6:e2ab76b2be07 | 342 | struct w5500_socket w5500_sockets[MAX_SOCK_NUM]; |
Bongjun | 6:e2ab76b2be07 | 343 | w5500_socket* get_sock(int fd); |
Bongjun | 6:e2ab76b2be07 | 344 | void init_socks(); |
Bongjun | 11:5118c2bff025 | 345 | |
Bongjun | 12:3a5f5b5ae5f8 | 346 | DHCPClient dhcp; |
Bongjun | 16:380b4a629c4f | 347 | bool _dhcp_enable; |
Bongjun | 11:5118c2bff025 | 348 | DNSClient dns; |
Bongjun | 6:e2ab76b2be07 | 349 | /* |
Bongjun | 6:e2ab76b2be07 | 350 | Thread *_daemon; |
Bongjun | 6:e2ab76b2be07 | 351 | void daemon(); |
Bongjun | 6:e2ab76b2be07 | 352 | */ |
Bongjun | 6:e2ab76b2be07 | 353 | |
Bongjun | 6:e2ab76b2be07 | 354 | }; |
Bongjun | 6:e2ab76b2be07 | 355 | |
Bongjun | 6:e2ab76b2be07 | 356 | #endif |