W5500 driver for mbed OS 5

Dependents:   http-webserver-example mbed-os-example-sockets

Fork of W5500Interface by Sergei G

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?

UserRevisionLine numberNew contents of line
Bongjun 6:e2ab76b2be07 1
Bongjun 6:e2ab76b2be07 2 /**
Bongjun 6:e2ab76b2be07 3 ******************************************************************************
Bongjun 6:e2ab76b2be07 4 * @file W5500Interface.h
Bongjun 6:e2ab76b2be07 5 * @author Bongjun Hur (modified version from Sergei G (https://os.mbed.com/users/sgnezdov/))
Bongjun 6:e2ab76b2be07 6 * @brief Implementation file of the NetworkStack for the W5500 Device
Bongjun 6:e2ab76b2be07 7 ******************************************************************************
Bongjun 6:e2ab76b2be07 8 * @attention
Bongjun 6:e2ab76b2be07 9 *
Bongjun 6:e2ab76b2be07 10 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
Bongjun 6:e2ab76b2be07 11 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
Bongjun 6:e2ab76b2be07 12 * TIME. AS A RESULT, WIZnet SHALL NOT BE HELD LIABLE FOR ANY
Bongjun 6:e2ab76b2be07 13 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
Bongjun 6:e2ab76b2be07 14 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
Bongjun 6:e2ab76b2be07 15 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
Bongjun 6:e2ab76b2be07 16 *
Bongjun 6:e2ab76b2be07 17 * <h2><center>&copy; COPYRIGHT 2017 WIZnet Co.,Ltd.</center></h2>
Bongjun 6:e2ab76b2be07 18 ******************************************************************************
Bongjun 6:e2ab76b2be07 19 */
Bongjun 6:e2ab76b2be07 20
Bongjun 6:e2ab76b2be07 21 #include "mbed.h"
Bongjun 6:e2ab76b2be07 22 #include "W5500Interface.h"
Bongjun 6:e2ab76b2be07 23
Bongjun 13:9e2a7c8da30c 24 static uint8_t W5500_DEFAULT_TESTMAC[6] = {0x00, 0x08, 0xdc, 0x45, 0x56, 0x67};
Bongjun 6:e2ab76b2be07 25 static int udp_local_port = 0;
Bongjun 6:e2ab76b2be07 26
Bongjun 6:e2ab76b2be07 27 #define SKT(h) ((w5500_socket*)h)
Bongjun 6:e2ab76b2be07 28 #define w5500_WAIT_TIMEOUT 1500
Bongjun 14:2f03158e52c3 29 #define w5500_ACCEPT_TIMEOUT 300000 //5 mins timeout, retrun NSAPI_ERROR_WOULD_BLOCK if there is no connection during 5 mins
Bongjun 6:e2ab76b2be07 30
Bongjun 15:9280e5bdd248 31 #define w5500_INTF_DBG 0
Bongjun 6:e2ab76b2be07 32
Bongjun 6:e2ab76b2be07 33 #if w5500_INTF_DBG
Bongjun 6:e2ab76b2be07 34 #define DBG(...) do{debug("[%s:%d]", __PRETTY_FUNCTION__,__LINE__);debug(__VA_ARGS__);} while(0);
Bongjun 6:e2ab76b2be07 35 #else
Bongjun 6:e2ab76b2be07 36 #define DBG(...) while(0);
Bongjun 6:e2ab76b2be07 37 #endif
Bongjun 6:e2ab76b2be07 38
Bongjun 8:c71c66d43703 39 /**
Bongjun 8:c71c66d43703 40 * @brief Defines a custom MAC address
Bongjun 8:c71c66d43703 41 * @note Have to be unique within the connected network!
Bongjun 8:c71c66d43703 42 * Modify the mac array items as needed.
Bongjun 8:c71c66d43703 43 * @param mac A 6-byte array defining the MAC address
Bongjun 8:c71c66d43703 44 * @retval
Bongjun 8:c71c66d43703 45 */
Bongjun 6:e2ab76b2be07 46 /* Interface implementation */
Bongjun 6:e2ab76b2be07 47
Bongjun 6:e2ab76b2be07 48 W5500Interface::W5500Interface(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset) :
Bongjun 6:e2ab76b2be07 49 _w5500(mosi, miso, sclk, cs, reset)
Bongjun 6:e2ab76b2be07 50 {
Bongjun 6:e2ab76b2be07 51 ip_set = false;
Bongjun 16:380b4a629c4f 52 _dhcp_enable = true;
Bongjun 6:e2ab76b2be07 53 }
Bongjun 6:e2ab76b2be07 54
Bongjun 8:c71c66d43703 55 /*
Bongjun 6:e2ab76b2be07 56 W5500Interface::W5500Interface(SPI* spi, PinName cs, PinName reset) :
Bongjun 6:e2ab76b2be07 57 _w5500(spi, cs, reset)
Bongjun 6:e2ab76b2be07 58 {
Bongjun 6:e2ab76b2be07 59 ip_set = false;
Bongjun 6:e2ab76b2be07 60 }
Bongjun 8:c71c66d43703 61 */
Bongjun 6:e2ab76b2be07 62
Bongjun 6:e2ab76b2be07 63 w5500_socket* W5500Interface::get_sock(int fd)
Bongjun 6:e2ab76b2be07 64 {
Bongjun 6:e2ab76b2be07 65 for (int i=0; i<MAX_SOCK_NUM ; i++) {
Bongjun 6:e2ab76b2be07 66 if (w5500_sockets[i].fd == -1) {
Bongjun 6:e2ab76b2be07 67 w5500_sockets[i].fd = fd;
Bongjun 6:e2ab76b2be07 68 w5500_sockets[i].proto = NSAPI_TCP;
Bongjun 6:e2ab76b2be07 69 w5500_sockets[i].connected = false;
Bongjun 6:e2ab76b2be07 70 w5500_sockets[i].callback = NULL;
Bongjun 6:e2ab76b2be07 71 w5500_sockets[i].callback_data = NULL;
Bongjun 6:e2ab76b2be07 72 return &w5500_sockets[i];
Bongjun 6:e2ab76b2be07 73 }
Bongjun 6:e2ab76b2be07 74 }
Bongjun 6:e2ab76b2be07 75 return NULL;
Bongjun 6:e2ab76b2be07 76 }
Bongjun 6:e2ab76b2be07 77
Bongjun 6:e2ab76b2be07 78 void W5500Interface::init_socks()
Bongjun 6:e2ab76b2be07 79 {
Bongjun 6:e2ab76b2be07 80 for (int i=0; i<MAX_SOCK_NUM ; i++) {
Bongjun 6:e2ab76b2be07 81 w5500_sockets[i].fd = -1;
Bongjun 6:e2ab76b2be07 82 w5500_sockets[i].proto = NSAPI_TCP;
Bongjun 6:e2ab76b2be07 83 w5500_sockets[i].connected = false;
Bongjun 6:e2ab76b2be07 84 w5500_sockets[i].callback = NULL;
Bongjun 6:e2ab76b2be07 85 w5500_sockets[i].callback_data = NULL;
Bongjun 6:e2ab76b2be07 86 }
Bongjun 6:e2ab76b2be07 87
Bongjun 11:5118c2bff025 88 dns.setup(get_stack());
Bongjun 6:e2ab76b2be07 89 //initialize the socket isr
Bongjun 6:e2ab76b2be07 90 //_daemon = new Thread(osPriorityNormal, 1024);
Bongjun 6:e2ab76b2be07 91 //_daemon->start(callback(this, &W5500Interface::daemon));
Bongjun 6:e2ab76b2be07 92 }
Bongjun 6:e2ab76b2be07 93
Bongjun 17:60f75e78f35d 94 uint32_t W5500Interface:: str_to_ip(const char* str)
Bongjun 17:60f75e78f35d 95 {
Bongjun 17:60f75e78f35d 96 uint32_t ip = 0;
Bongjun 17:60f75e78f35d 97 char* p = (char*)str;
Bongjun 17:60f75e78f35d 98 for(int i = 0; i < 4; i++) {
Bongjun 17:60f75e78f35d 99 ip |= atoi(p);
Bongjun 17:60f75e78f35d 100 p = strchr(p, '.');
Bongjun 17:60f75e78f35d 101 if (p == NULL) {
Bongjun 17:60f75e78f35d 102 break;
Bongjun 17:60f75e78f35d 103 }
Bongjun 17:60f75e78f35d 104 ip <<= 8;
Bongjun 17:60f75e78f35d 105 p++;
Bongjun 17:60f75e78f35d 106 }
Bongjun 17:60f75e78f35d 107 return ip;
Bongjun 17:60f75e78f35d 108 }
Bongjun 17:60f75e78f35d 109
Bongjun 6:e2ab76b2be07 110 int W5500Interface::init()
Bongjun 6:e2ab76b2be07 111 {
Bongjun 16:380b4a629c4f 112 _dhcp_enable = true;
Bongjun 6:e2ab76b2be07 113 _w5500.reg_wr<uint32_t>(SIPR, 0x00000000); // local ip "0.0.0.0"
Bongjun 6:e2ab76b2be07 114 //_w5500.reg_wr<uint8_t>(SIMR, 0xFF); //
Bongjun 13:9e2a7c8da30c 115 for (int i =0; i < 6; i++) _w5500.mac[i] = W5500_DEFAULT_TESTMAC[i];
Bongjun 13:9e2a7c8da30c 116 _w5500.setmac();
Bongjun 6:e2ab76b2be07 117 _w5500.reset();
Bongjun 6:e2ab76b2be07 118 init_socks();
Bongjun 6:e2ab76b2be07 119 return 0;
Bongjun 6:e2ab76b2be07 120 }
Bongjun 6:e2ab76b2be07 121
Bongjun 6:e2ab76b2be07 122 int W5500Interface::init(uint8_t * mac)
Bongjun 6:e2ab76b2be07 123 {
Bongjun 16:380b4a629c4f 124 _dhcp_enable = true;
Bongjun 6:e2ab76b2be07 125 _w5500.reg_wr<uint32_t>(SIPR, 0x00000000); // local ip "0.0.0.0"
Bongjun 8:c71c66d43703 126 // should set the mac address and keep the value in this class
Bongjun 6:e2ab76b2be07 127 for (int i =0; i < 6; i++) _w5500.mac[i] = mac[i];
Bongjun 6:e2ab76b2be07 128 _w5500.setmac();
Bongjun 8:c71c66d43703 129 _w5500.reset(); // reset chip and write mac address
Bongjun 6:e2ab76b2be07 130 init_socks();
Bongjun 6:e2ab76b2be07 131 return 0;
Bongjun 6:e2ab76b2be07 132 }
Bongjun 6:e2ab76b2be07 133
Bongjun 6:e2ab76b2be07 134 // add this function, because sometimes no needed MAC address in init calling.
Bongjun 6:e2ab76b2be07 135 int W5500Interface::init(const char* ip, const char* mask, const char* gateway)
Bongjun 6:e2ab76b2be07 136 {
Bongjun 16:380b4a629c4f 137 _dhcp_enable = false;
Bongjun 18:afec30f0922a 138
Bongjun 18:afec30f0922a 139
Bongjun 6:e2ab76b2be07 140 _w5500.ip = str_to_ip(ip);
Bongjun 6:e2ab76b2be07 141 strcpy(ip_string, ip);
Bongjun 6:e2ab76b2be07 142 ip_set = true;
Bongjun 6:e2ab76b2be07 143 _w5500.netmask = str_to_ip(mask);
Bongjun 6:e2ab76b2be07 144 _w5500.gateway = str_to_ip(gateway);
Bongjun 6:e2ab76b2be07 145 _w5500.reset();
Bongjun 6:e2ab76b2be07 146
Bongjun 6:e2ab76b2be07 147 // @Jul. 8. 2014 add code. should be called to write chip.
Bongjun 6:e2ab76b2be07 148 _w5500.setip();
Bongjun 6:e2ab76b2be07 149 init_socks();
Bongjun 6:e2ab76b2be07 150
Bongjun 6:e2ab76b2be07 151 return 0;
Bongjun 6:e2ab76b2be07 152 }
Bongjun 6:e2ab76b2be07 153
Bongjun 6:e2ab76b2be07 154 int W5500Interface::init(uint8_t * mac, const char* ip, const char* mask, const char* gateway)
Bongjun 6:e2ab76b2be07 155 {
Bongjun 16:380b4a629c4f 156 _dhcp_enable = false;
Bongjun 6:e2ab76b2be07 157 //
Bongjun 6:e2ab76b2be07 158 for (int i =0; i < 6; i++) _w5500.mac[i] = mac[i];
Bongjun 6:e2ab76b2be07 159 //
Bongjun 6:e2ab76b2be07 160 _w5500.ip = str_to_ip(ip);
Bongjun 6:e2ab76b2be07 161 strcpy(ip_string, ip);
Bongjun 6:e2ab76b2be07 162 ip_set = true;
Bongjun 6:e2ab76b2be07 163 _w5500.netmask = str_to_ip(mask);
Bongjun 6:e2ab76b2be07 164 _w5500.gateway = str_to_ip(gateway);
Bongjun 6:e2ab76b2be07 165 _w5500.reset();
Bongjun 6:e2ab76b2be07 166
Bongjun 6:e2ab76b2be07 167 // @Jul. 8. 2014 add code. should be called to write chip.
Bongjun 6:e2ab76b2be07 168 _w5500.setmac();
Bongjun 6:e2ab76b2be07 169 _w5500.setip();
Bongjun 6:e2ab76b2be07 170 init_socks();
Bongjun 6:e2ab76b2be07 171
Bongjun 6:e2ab76b2be07 172 return 0;
Bongjun 6:e2ab76b2be07 173 }
Bongjun 6:e2ab76b2be07 174
Bongjun 6:e2ab76b2be07 175 /*
Bongjun 6:e2ab76b2be07 176 void W5500Interface::daemon () {
Bongjun 6:e2ab76b2be07 177 for (;;) {
Bongjun 6:e2ab76b2be07 178 for (int i=0; i<MAX_SOCK_NUM ; i++) {
Bongjun 6:e2ab76b2be07 179 if (w5500_sockets[i].fd > 0 && w5500_sockets[i].callback) {
Bongjun 6:e2ab76b2be07 180 int size = _w5500.sreg<uint16_t>(w5500_sockets[i].fd, Sn_RX_RSR);
Bongjun 6:e2ab76b2be07 181 if (size > 0) {
Bongjun 6:e2ab76b2be07 182 led1 = !led1;
Bongjun 6:e2ab76b2be07 183 w5500_sockets[i].callback(w5500_sockets[i].callback_data);
Bongjun 6:e2ab76b2be07 184 }
Bongjun 6:e2ab76b2be07 185 }
Bongjun 6:e2ab76b2be07 186 }
Bongjun 6:e2ab76b2be07 187 wait(0.2);
Bongjun 6:e2ab76b2be07 188 }
Bongjun 6:e2ab76b2be07 189 }
Bongjun 6:e2ab76b2be07 190 */
Bongjun 6:e2ab76b2be07 191
Bongjun 16:380b4a629c4f 192 int W5500Interface::IPrenew(int timeout_ms)
Bongjun 6:e2ab76b2be07 193 {
Bongjun 16:380b4a629c4f 194 DBG("[EasyConnect] DHCP start\n");
Bongjun 12:3a5f5b5ae5f8 195 int err = dhcp.setup(get_stack(), _w5500.mac, timeout_ms);
Bongjun 8:c71c66d43703 196 if (err == (-1)) {
Bongjun 16:380b4a629c4f 197 DBG("[EasyConnect] Timeout.\n");
Bongjun 16:380b4a629c4f 198 return NSAPI_ERROR_DHCP_FAILURE;
Bongjun 8:c71c66d43703 199 }
Bongjun 16:380b4a629c4f 200 DBG("[EasyConnect] DHCP completed\n");
Bongjun 16:380b4a629c4f 201 DBG("[EasyConnect] Connected, IP: %d.%d.%d.%d\r\n", dhcp.yiaddr[0], dhcp.yiaddr[1], dhcp.yiaddr[2], dhcp.yiaddr[3]);
Bongjun 8:c71c66d43703 202
Bongjun 8:c71c66d43703 203 char ip[24], gateway[24], netmask[24], dnsaddr[24];
Bongjun 8:c71c66d43703 204 sprintf(ip, "%d.%d.%d.%d", dhcp.yiaddr[0], dhcp.yiaddr[1], dhcp.yiaddr[2], dhcp.yiaddr[3]);
Bongjun 8:c71c66d43703 205 sprintf(gateway, "%d.%d.%d.%d", dhcp.gateway[0], dhcp.gateway[1], dhcp.gateway[2], dhcp.gateway[3]);
Bongjun 8:c71c66d43703 206 sprintf(netmask, "%d.%d.%d.%d", dhcp.netmask[0], dhcp.netmask[1], dhcp.netmask[2], dhcp.netmask[3]);
Bongjun 8:c71c66d43703 207 sprintf(dnsaddr, "%d.%d.%d.%d", dhcp.dnsaddr[0], dhcp.dnsaddr[1], dhcp.dnsaddr[2], dhcp.dnsaddr[3]);
Bongjun 16:380b4a629c4f 208
Bongjun 8:c71c66d43703 209 init(ip, netmask, gateway);
Bongjun 12:3a5f5b5ae5f8 210 setDnsServerIP(dnsaddr);
Bongjun 16:380b4a629c4f 211
Bongjun 16:380b4a629c4f 212 _dhcp_enable = true; // because this value was changed in init(ip, netmask, gateway).
Bongjun 16:380b4a629c4f 213
Bongjun 16:380b4a629c4f 214 return 0;
Bongjun 16:380b4a629c4f 215 }
Bongjun 16:380b4a629c4f 216
Bongjun 16:380b4a629c4f 217
Bongjun 16:380b4a629c4f 218 int W5500Interface::connect()
Bongjun 16:380b4a629c4f 219 {
Bongjun 16:380b4a629c4f 220 if (_dhcp_enable) {
Bongjun 16:380b4a629c4f 221 init(); // init default mac address
Bongjun 16:380b4a629c4f 222 int err = IPrenew(15000);
Bongjun 18:afec30f0922a 223 if (err < 0) return err;
Bongjun 16:380b4a629c4f 224 }
Bongjun 16:380b4a629c4f 225
Bongjun 6:e2ab76b2be07 226 if (_w5500.setip() == false) return NSAPI_ERROR_DHCP_FAILURE;
Bongjun 6:e2ab76b2be07 227 return 0;
Bongjun 6:e2ab76b2be07 228 }
Bongjun 6:e2ab76b2be07 229
Bongjun 11:5118c2bff025 230 bool W5500Interface::setDnsServerIP(const char* ip_address)
Bongjun 11:5118c2bff025 231 {
Bongjun 16:380b4a629c4f 232 return dns.set_server(ip_address);
Bongjun 11:5118c2bff025 233 }
Bongjun 16:380b4a629c4f 234
Bongjun 6:e2ab76b2be07 235 int W5500Interface::disconnect()
Bongjun 6:e2ab76b2be07 236 {
Bongjun 6:e2ab76b2be07 237 _w5500.disconnect();
Bongjun 6:e2ab76b2be07 238 return 0;
Bongjun 6:e2ab76b2be07 239 }
Bongjun 6:e2ab76b2be07 240
Bongjun 6:e2ab76b2be07 241 const char *W5500Interface::get_ip_address()
Bongjun 6:e2ab76b2be07 242 {
Bongjun 6:e2ab76b2be07 243 uint32_t ip = _w5500.reg_rd<uint32_t>(SIPR);
Bongjun 6:e2ab76b2be07 244 snprintf(ip_string, sizeof(ip_string), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff);
Bongjun 6:e2ab76b2be07 245 return ip_string;
Bongjun 6:e2ab76b2be07 246 }
Bongjun 6:e2ab76b2be07 247
Bongjun 10:925201b1603f 248 const char *W5500Interface::get_netmask()
Bongjun 10:925201b1603f 249 {
Bongjun 10:925201b1603f 250 uint32_t netmask = _w5500.reg_rd<uint32_t>(SUBR);
Bongjun 10:925201b1603f 251 snprintf(netmask_string, sizeof(netmask_string), "%d.%d.%d.%d", (netmask>>24)&0xff, (netmask>>16)&0xff, (netmask>>8)&0xff, netmask&0xff);
Bongjun 10:925201b1603f 252 return netmask_string;
Bongjun 10:925201b1603f 253 }
Bongjun 10:925201b1603f 254
Bongjun 10:925201b1603f 255 const char *W5500Interface::get_gateway()
Bongjun 10:925201b1603f 256 {
Bongjun 10:925201b1603f 257 uint32_t gateway = _w5500.reg_rd<uint32_t>(GAR);
Bongjun 10:925201b1603f 258 snprintf(gateway_string, sizeof(gateway_string), "%d.%d.%d.%d", (gateway>>24)&0xff, (gateway>>16)&0xff, (gateway>>8)&0xff, gateway&0xff);
Bongjun 10:925201b1603f 259 return gateway_string;
Bongjun 10:925201b1603f 260 }
Bongjun 10:925201b1603f 261
Bongjun 6:e2ab76b2be07 262 const char *W5500Interface::get_mac_address()
Bongjun 6:e2ab76b2be07 263 {
Bongjun 6:e2ab76b2be07 264 uint8_t mac[6];
Bongjun 6:e2ab76b2be07 265 _w5500.reg_rd_mac(SHAR, mac);
Bongjun 6:e2ab76b2be07 266 snprintf(mac_string, sizeof(mac_string), "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
Bongjun 6:e2ab76b2be07 267 return mac_string;
Bongjun 6:e2ab76b2be07 268 }
Bongjun 6:e2ab76b2be07 269
Bongjun 6:e2ab76b2be07 270 void W5500Interface::get_mac(uint8_t mac[6])
Bongjun 6:e2ab76b2be07 271 {
Bongjun 6:e2ab76b2be07 272 _w5500.reg_rd_mac(SHAR, mac);
Bongjun 6:e2ab76b2be07 273 }
Bongjun 6:e2ab76b2be07 274
Bongjun 6:e2ab76b2be07 275 nsapi_error_t W5500Interface::socket_open(nsapi_socket_t *handle, nsapi_protocol_t proto)
Bongjun 6:e2ab76b2be07 276 {
Bongjun 6:e2ab76b2be07 277 //a socket is created the same way regardless of the protocol
Bongjun 6:e2ab76b2be07 278 int sock_fd = _w5500.new_socket();
Bongjun 6:e2ab76b2be07 279 if (sock_fd < 0) {
Bongjun 6:e2ab76b2be07 280 return NSAPI_ERROR_NO_SOCKET;
Bongjun 6:e2ab76b2be07 281 }
Bongjun 6:e2ab76b2be07 282
Bongjun 6:e2ab76b2be07 283 w5500_socket *h = get_sock(sock_fd);
Bongjun 6:e2ab76b2be07 284
Bongjun 6:e2ab76b2be07 285 if (!h) {
Bongjun 6:e2ab76b2be07 286 return NSAPI_ERROR_NO_SOCKET;
Bongjun 6:e2ab76b2be07 287 }
Bongjun 6:e2ab76b2be07 288
Bongjun 6:e2ab76b2be07 289 h->proto = proto;
Bongjun 6:e2ab76b2be07 290 h->connected = false;
Bongjun 6:e2ab76b2be07 291 h->callback = NULL;
Bongjun 6:e2ab76b2be07 292 h->callback_data = NULL;
Bongjun 6:e2ab76b2be07 293
Bongjun 6:e2ab76b2be07 294 //new up an int to store the socket fd
Bongjun 6:e2ab76b2be07 295 *handle = h;
Bongjun 6:e2ab76b2be07 296 DBG("fd: %d\n", sock_fd);
Bongjun 6:e2ab76b2be07 297 return 0;
Bongjun 6:e2ab76b2be07 298 }
Bongjun 6:e2ab76b2be07 299
Bongjun 6:e2ab76b2be07 300 /*
Bongjun 6:e2ab76b2be07 301 void W5500Interface::signal_event(nsapi_socket_t handle)
Bongjun 6:e2ab76b2be07 302 {
Bongjun 6:e2ab76b2be07 303 DBG("fd: %d\n", SKT(handle)->fd);
Bongjun 6:e2ab76b2be07 304 if (SKT(handle)->callback != NULL) {
Bongjun 6:e2ab76b2be07 305 SKT(handle)->callback(SKT(handle)->callback_data);
Bongjun 6:e2ab76b2be07 306 }
Bongjun 6:e2ab76b2be07 307 }
Bongjun 6:e2ab76b2be07 308 */
Bongjun 6:e2ab76b2be07 309
Bongjun 6:e2ab76b2be07 310 nsapi_error_t W5500Interface::socket_close(nsapi_socket_t handle)
Bongjun 6:e2ab76b2be07 311 {
Bongjun 6:e2ab76b2be07 312 if (handle == NULL) return 0;
Bongjun 6:e2ab76b2be07 313 DBG("fd: %d\n", SKT(handle)->fd);
Bongjun 6:e2ab76b2be07 314 _w5500.close(SKT(handle)->fd);
Bongjun 6:e2ab76b2be07 315
Bongjun 6:e2ab76b2be07 316 SKT(handle)->fd = -1;
Bongjun 6:e2ab76b2be07 317
Bongjun 6:e2ab76b2be07 318 return 0;
Bongjun 6:e2ab76b2be07 319 }
Bongjun 6:e2ab76b2be07 320
Bongjun 6:e2ab76b2be07 321 nsapi_error_t W5500Interface::socket_bind(nsapi_socket_t handle, const SocketAddress &address)
Bongjun 6:e2ab76b2be07 322 {
Bongjun 6:e2ab76b2be07 323 if (handle < 0) {
Bongjun 6:e2ab76b2be07 324 return NSAPI_ERROR_DEVICE_ERROR;
Bongjun 6:e2ab76b2be07 325 }
Bongjun 6:e2ab76b2be07 326 DBG("fd: %d, port: %d\n", SKT(handle)->fd, address.get_port());
Bongjun 6:e2ab76b2be07 327
Bongjun 6:e2ab76b2be07 328 switch (SKT(handle)->proto) {
Bongjun 6:e2ab76b2be07 329 case NSAPI_UDP:
Bongjun 6:e2ab76b2be07 330 // set local port
Bongjun 6:e2ab76b2be07 331 if (address.get_port() != 0) {
Bongjun 6:e2ab76b2be07 332 _w5500.setLocalPort( SKT(handle)->fd, address.get_port() );
Bongjun 6:e2ab76b2be07 333 } else {
Bongjun 6:e2ab76b2be07 334 udp_local_port++;
Bongjun 6:e2ab76b2be07 335 _w5500.setLocalPort( SKT(handle)->fd, udp_local_port );
Bongjun 6:e2ab76b2be07 336 }
Bongjun 6:e2ab76b2be07 337 // set udp protocol
Bongjun 6:e2ab76b2be07 338 _w5500.setProtocol(SKT(handle)->fd, UDP);
Bongjun 6:e2ab76b2be07 339 _w5500.scmd(SKT(handle)->fd, OPEN);
Bongjun 16:380b4a629c4f 340 /*
Bongjun 16:380b4a629c4f 341 uint8_t tmpSn_SR;
Bongjun 16:380b4a629c4f 342 tmpSn_SR = _w5500.sreg<uint8_t>(SKT(handle)->fd, Sn_SR);
Bongjun 16:380b4a629c4f 343 DBG("open socket status: %2x\n", tmpSn_SR);
Bongjun 16:380b4a629c4f 344 */
Bongjun 6:e2ab76b2be07 345 return 0;
Bongjun 6:e2ab76b2be07 346 case NSAPI_TCP:
Bongjun 6:e2ab76b2be07 347 listen_port = address.get_port();
Bongjun 6:e2ab76b2be07 348 // set TCP protocol
Bongjun 6:e2ab76b2be07 349 _w5500.setProtocol(SKT(handle)->fd, TCP);
Bongjun 6:e2ab76b2be07 350 // set local port
Bongjun 16:380b4a629c4f 351 _w5500.setLocalPort( SKT(handle)->fd, address.get_port() );
Bongjun 6:e2ab76b2be07 352 // connect the network
Bongjun 6:e2ab76b2be07 353 _w5500.scmd(SKT(handle)->fd, OPEN);
Bongjun 6:e2ab76b2be07 354 return 0;
Bongjun 6:e2ab76b2be07 355 }
Bongjun 6:e2ab76b2be07 356
Bongjun 6:e2ab76b2be07 357 return NSAPI_ERROR_DEVICE_ERROR;
Bongjun 6:e2ab76b2be07 358 }
Bongjun 6:e2ab76b2be07 359
Bongjun 6:e2ab76b2be07 360 nsapi_error_t W5500Interface::socket_listen(nsapi_socket_t handle, int backlog)
Bongjun 6:e2ab76b2be07 361 {
Bongjun 6:e2ab76b2be07 362 DBG("fd: %d\n", SKT(handle)->fd);
Bongjun 6:e2ab76b2be07 363 if (SKT(handle)->fd < 0) {
Bongjun 6:e2ab76b2be07 364 return NSAPI_ERROR_NO_SOCKET;
Bongjun 6:e2ab76b2be07 365 }
Bongjun 16:380b4a629c4f 366 /* if (backlog != 1) {
Bongjun 16:380b4a629c4f 367 return NSAPI_ERROR_NO_SOCKET;
Bongjun 16:380b4a629c4f 368 }
Bongjun 16:380b4a629c4f 369 */
Bongjun 6:e2ab76b2be07 370 _w5500.scmd(SKT(handle)->fd, LISTEN);
Bongjun 6:e2ab76b2be07 371 return 0;
Bongjun 6:e2ab76b2be07 372 }
Bongjun 6:e2ab76b2be07 373
Bongjun 6:e2ab76b2be07 374 nsapi_size_or_error_t W5500Interface::socket_connect(nsapi_socket_t handle, const SocketAddress &address)
Bongjun 6:e2ab76b2be07 375 {
Bongjun 6:e2ab76b2be07 376 DBG("fd: %d\n", SKT(handle)->fd);
Bongjun 6:e2ab76b2be07 377 //check for a valid socket
Bongjun 6:e2ab76b2be07 378 if (SKT(handle)->fd < 0) {
Bongjun 6:e2ab76b2be07 379 return NSAPI_ERROR_NO_SOCKET;
Bongjun 6:e2ab76b2be07 380 }
Bongjun 6:e2ab76b2be07 381
Bongjun 6:e2ab76b2be07 382 //before we attempt to connect, we are not connected
Bongjun 6:e2ab76b2be07 383 SKT(handle)->connected = false;
Bongjun 6:e2ab76b2be07 384
Bongjun 6:e2ab76b2be07 385 //try to connect
Bongjun 11:5118c2bff025 386 if (!_w5500.connect(SKT(handle)->fd, address.get_ip_address(), address.get_port(), w5500_WAIT_TIMEOUT)) {
Bongjun 6:e2ab76b2be07 387 return -1;
Bongjun 6:e2ab76b2be07 388 }
Bongjun 6:e2ab76b2be07 389
Bongjun 6:e2ab76b2be07 390 //we are now connected
Bongjun 6:e2ab76b2be07 391 SKT(handle)->connected = true;
Bongjun 6:e2ab76b2be07 392
Bongjun 6:e2ab76b2be07 393 return 0;
Bongjun 6:e2ab76b2be07 394 }
Bongjun 6:e2ab76b2be07 395
Bongjun 6:e2ab76b2be07 396 nsapi_error_t W5500Interface::socket_accept(nsapi_socket_t server, nsapi_socket_t *handle, SocketAddress *address)
Bongjun 6:e2ab76b2be07 397 {
Bongjun 16:380b4a629c4f 398 SocketAddress _addr;
Bongjun 6:e2ab76b2be07 399
Bongjun 6:e2ab76b2be07 400 DBG("fd: %d\n", SKT(handle)->fd);
Bongjun 6:e2ab76b2be07 401 if (SKT(server)->fd < 0) {
Bongjun 6:e2ab76b2be07 402 return NSAPI_ERROR_NO_SOCKET;
Bongjun 6:e2ab76b2be07 403 }
Bongjun 6:e2ab76b2be07 404
Bongjun 6:e2ab76b2be07 405 SKT(server)->connected = false;
Bongjun 6:e2ab76b2be07 406
Bongjun 6:e2ab76b2be07 407 Timer t;
Bongjun 6:e2ab76b2be07 408 t.reset();
Bongjun 6:e2ab76b2be07 409 t.start();
Bongjun 6:e2ab76b2be07 410
Bongjun 6:e2ab76b2be07 411 while(1) {
Bongjun 6:e2ab76b2be07 412 if (t.read_ms() > w5500_ACCEPT_TIMEOUT) {
Bongjun 13:9e2a7c8da30c 413 DBG("W5500Interface::socket_accept, timed out\r\n");
Bongjun 6:e2ab76b2be07 414 return NSAPI_ERROR_WOULD_BLOCK;
Bongjun 6:e2ab76b2be07 415 }
Bongjun 6:e2ab76b2be07 416 if (_w5500.is_connected(SKT(server)->fd)) break;
Bongjun 6:e2ab76b2be07 417 }
Bongjun 6:e2ab76b2be07 418
Bongjun 6:e2ab76b2be07 419 //get socket for the connection
Bongjun 6:e2ab76b2be07 420 *handle = get_sock(SKT(server)->fd);
Bongjun 6:e2ab76b2be07 421
Bongjun 6:e2ab76b2be07 422 if (!(*handle)) {
Bongjun 6:e2ab76b2be07 423 error("No more sockets for binding");
Bongjun 6:e2ab76b2be07 424 return NSAPI_ERROR_NO_SOCKET;
Bongjun 6:e2ab76b2be07 425 }
Bongjun 6:e2ab76b2be07 426
Bongjun 6:e2ab76b2be07 427 //give it all of the socket info from the server
Bongjun 6:e2ab76b2be07 428 SKT(*handle)->proto = SKT(server)->proto;
Bongjun 6:e2ab76b2be07 429 SKT(*handle)->connected = true;
Bongjun 6:e2ab76b2be07 430
Bongjun 6:e2ab76b2be07 431 if (address) {
Bongjun 6:e2ab76b2be07 432 uint32_t ip = _w5500.sreg<uint32_t>(SKT(*handle)->fd, Sn_DIPR);
Bongjun 6:e2ab76b2be07 433 char host[17];
Bongjun 6:e2ab76b2be07 434 snprintf(host, sizeof(host), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff);
Bongjun 6:e2ab76b2be07 435 int port = _w5500.sreg<uint16_t>(SKT(*handle)->fd, Sn_DPORT);
Bongjun 6:e2ab76b2be07 436
Bongjun 6:e2ab76b2be07 437 _addr.set_ip_address(host);
Bongjun 6:e2ab76b2be07 438 _addr.set_port(port);
Bongjun 6:e2ab76b2be07 439 *address = _addr;
Bongjun 6:e2ab76b2be07 440 }
Bongjun 16:380b4a629c4f 441
Bongjun 16:380b4a629c4f 442
Bongjun 6:e2ab76b2be07 443 //create a new tcp socket for the server
Bongjun 6:e2ab76b2be07 444 SKT(server)->fd = _w5500.new_socket();
Bongjun 6:e2ab76b2be07 445 if (SKT(server)->fd < 0) {
Bongjun 6:e2ab76b2be07 446 error("No more sockets for listening");
Bongjun 6:e2ab76b2be07 447 //return NSAPI_ERROR_NO_SOCKET;
Bongjun 16:380b4a629c4f 448 // already accepted socket, so return 0, but there is no listen socket anymore.
Bongjun 16:380b4a629c4f 449 return 0;
Bongjun 16:380b4a629c4f 450 }
Bongjun 6:e2ab76b2be07 451
Bongjun 16:380b4a629c4f 452 SKT(server)->proto = NSAPI_TCP;
Bongjun 16:380b4a629c4f 453 SKT(server)->connected = false;
Bongjun 6:e2ab76b2be07 454
Bongjun 16:380b4a629c4f 455 _addr.set_port(listen_port);
Bongjun 6:e2ab76b2be07 456
Bongjun 6:e2ab76b2be07 457 // and then, for the next connection, server socket should be assigned new one.
Bongjun 6:e2ab76b2be07 458 if (socket_bind(server, _addr) < 0) {
Bongjun 6:e2ab76b2be07 459 error("No more sockets for listening");
Bongjun 6:e2ab76b2be07 460 //return NSAPI_ERROR_NO_SOCKET;
Bongjun 16:380b4a629c4f 461 // already accepted socket, so return 0, but there is no listen socket anymore.
Bongjun 16:380b4a629c4f 462 return 0;
Bongjun 6:e2ab76b2be07 463 }
Bongjun 6:e2ab76b2be07 464
Bongjun 6:e2ab76b2be07 465 if (socket_listen(server, 1) < 0) {
Bongjun 6:e2ab76b2be07 466 error("No more sockets for listening");
Bongjun 16:380b4a629c4f 467 // already accepted socket, so return 0, but there is no listen socket anymore.
Bongjun 16:380b4a629c4f 468 return 0;
Bongjun 6:e2ab76b2be07 469 }
Bongjun 6:e2ab76b2be07 470
Bongjun 6:e2ab76b2be07 471 return 0;
Bongjun 6:e2ab76b2be07 472 }
Bongjun 6:e2ab76b2be07 473
Bongjun 6:e2ab76b2be07 474 nsapi_size_or_error_t W5500Interface::socket_send(nsapi_socket_t handle, const void *data, nsapi_size_t size)
Bongjun 6:e2ab76b2be07 475 {
Bongjun 6:e2ab76b2be07 476 DBG("fd: %d\n", SKT(handle)->fd);
Bongjun 6:e2ab76b2be07 477 int writtenLen = 0;
Bongjun 6:e2ab76b2be07 478 while (writtenLen < size) {
Bongjun 6:e2ab76b2be07 479 int _size = _w5500.wait_writeable(SKT(handle)->fd, w5500_WAIT_TIMEOUT);
Bongjun 6:e2ab76b2be07 480 if (_size < 0) {
Bongjun 6:e2ab76b2be07 481 return NSAPI_ERROR_WOULD_BLOCK;
Bongjun 6:e2ab76b2be07 482 }
Bongjun 6:e2ab76b2be07 483 if (_size > (size-writtenLen)) {
Bongjun 6:e2ab76b2be07 484 _size = (size-writtenLen);
Bongjun 6:e2ab76b2be07 485 }
Bongjun 6:e2ab76b2be07 486 int ret = _w5500.send(SKT(handle)->fd, (char*)data, (int)_size);
Bongjun 6:e2ab76b2be07 487 if (ret < 0) {
Bongjun 6:e2ab76b2be07 488 DBG("returning error -1\n");
Bongjun 6:e2ab76b2be07 489 return -1;
Bongjun 6:e2ab76b2be07 490 }
Bongjun 6:e2ab76b2be07 491 writtenLen += ret;
Bongjun 6:e2ab76b2be07 492 }
Bongjun 6:e2ab76b2be07 493 return writtenLen;
Bongjun 6:e2ab76b2be07 494 }
Bongjun 6:e2ab76b2be07 495
Bongjun 6:e2ab76b2be07 496 nsapi_size_or_error_t W5500Interface::socket_recv(nsapi_socket_t handle, void *data, nsapi_size_t size)
Bongjun 6:e2ab76b2be07 497 {
Bongjun 6:e2ab76b2be07 498 DBG("fd: %d\n", SKT(handle)->fd);
Bongjun 6:e2ab76b2be07 499 // add to cover exception.
Bongjun 6:e2ab76b2be07 500 if ((SKT(handle)->fd < 0) || !SKT(handle)->connected) {
Bongjun 6:e2ab76b2be07 501 return -1;
Bongjun 6:e2ab76b2be07 502 }
Bongjun 11:5118c2bff025 503 DBG("fd: connected is %d\n", SKT(handle)->connected);
Bongjun 6:e2ab76b2be07 504
Bongjun 6:e2ab76b2be07 505 int _size = _w5500.wait_readable(SKT(handle)->fd, w5500_WAIT_TIMEOUT);
Bongjun 11:5118c2bff025 506 DBG("fd: size is %d\n", _size);
Bongjun 6:e2ab76b2be07 507
Bongjun 6:e2ab76b2be07 508 if (_size < 0) {
Bongjun 6:e2ab76b2be07 509 return NSAPI_ERROR_WOULD_BLOCK;
Bongjun 6:e2ab76b2be07 510 }
Bongjun 6:e2ab76b2be07 511
Bongjun 6:e2ab76b2be07 512 if (_size > size) {
Bongjun 6:e2ab76b2be07 513 _size = size;
Bongjun 6:e2ab76b2be07 514 }
Bongjun 11:5118c2bff025 515
Bongjun 11:5118c2bff025 516 nsapi_size_or_error_t err = _w5500.recv(SKT(handle)->fd, (char*)data, (int)_size);
Bongjun 11:5118c2bff025 517 DBG("rv: %d\n", err);
Bongjun 11:5118c2bff025 518
Bongjun 11:5118c2bff025 519 #if w5500_INTF_DBG
Bongjun 11:5118c2bff025 520 if (err > 0) {
Bongjun 11:5118c2bff025 521 debug("[socket_recv] buffer:");
Bongjun 11:5118c2bff025 522 for(int i = 0; i < err; i++) {
Bongjun 11:5118c2bff025 523 if ((i%16) == 0) {
Bongjun 11:5118c2bff025 524 debug("\n");
Bongjun 11:5118c2bff025 525 }
Bongjun 11:5118c2bff025 526 debug(" %02x", ((uint8_t*)data)[i]);
Bongjun 11:5118c2bff025 527 }
Bongjun 11:5118c2bff025 528 if ((err-1%16) != 0) {
Bongjun 11:5118c2bff025 529 debug("\n");
Bongjun 11:5118c2bff025 530 }
Bongjun 11:5118c2bff025 531 }
Bongjun 11:5118c2bff025 532 #endif
Bongjun 11:5118c2bff025 533 return err;
Bongjun 6:e2ab76b2be07 534 }
Bongjun 6:e2ab76b2be07 535
Bongjun 6:e2ab76b2be07 536 nsapi_size_or_error_t W5500Interface::socket_sendto(nsapi_socket_t handle, const SocketAddress &address,
Bongjun 6:e2ab76b2be07 537 const void *data, nsapi_size_t size)
Bongjun 6:e2ab76b2be07 538 {
Bongjun 6:e2ab76b2be07 539 DBG("fd: %d, ip: %s:%d\n", SKT(handle)->fd, address.get_ip_address(), address.get_port());
Bongjun 6:e2ab76b2be07 540 if (_w5500.is_closed(SKT(handle)->fd)) {
Bongjun 6:e2ab76b2be07 541 nsapi_error_t err = socket_bind(handle, address);
Bongjun 6:e2ab76b2be07 542 if (err < 0 ) {
Bongjun 6:e2ab76b2be07 543 DBG("failed to bind socket: %d\n", err);
Bongjun 6:e2ab76b2be07 544 return err;
Bongjun 6:e2ab76b2be07 545 }
Bongjun 6:e2ab76b2be07 546 }
Bongjun 6:e2ab76b2be07 547 //compare with original: int size = eth->wait_writeable(_sock_fd, _blocking ? -1 : _timeout, length-1);
Bongjun 6:e2ab76b2be07 548 int len = _w5500.wait_writeable(SKT(handle)->fd, w5500_WAIT_TIMEOUT, size-1);
Bongjun 6:e2ab76b2be07 549 if (len < 0) {
Bongjun 6:e2ab76b2be07 550 DBG("error: NSAPI_ERROR_WOULD_BLOCK\n");
Bongjun 6:e2ab76b2be07 551 return NSAPI_ERROR_WOULD_BLOCK;;
Bongjun 6:e2ab76b2be07 552 }
Bongjun 6:e2ab76b2be07 553
Bongjun 6:e2ab76b2be07 554 // set remote host
Bongjun 6:e2ab76b2be07 555 _w5500.sreg_ip(SKT(handle)->fd, Sn_DIPR, address.get_ip_address());
Bongjun 6:e2ab76b2be07 556 // set remote port
Bongjun 6:e2ab76b2be07 557 _w5500.sreg<uint16_t>(SKT(handle)->fd, Sn_DPORT, address.get_port());
Bongjun 6:e2ab76b2be07 558
Bongjun 6:e2ab76b2be07 559 nsapi_size_or_error_t err = _w5500.send(SKT(handle)->fd, (const char*)data, size);
Bongjun 6:e2ab76b2be07 560 DBG("rv: %d, size: %d\n", err, size);
Bongjun 6:e2ab76b2be07 561
Bongjun 6:e2ab76b2be07 562 #if w5500_INTF_DBG
Bongjun 6:e2ab76b2be07 563 if (err > 0) {
Bongjun 6:e2ab76b2be07 564 debug("[socket_sendto] data: ");
Bongjun 6:e2ab76b2be07 565 for(int i = 0; i < err; i++) {
Bongjun 6:e2ab76b2be07 566 if ((i%16) == 0) {
Bongjun 6:e2ab76b2be07 567 debug("\n");
Bongjun 6:e2ab76b2be07 568 }
Bongjun 6:e2ab76b2be07 569 debug(" %02x", ((uint8_t*)data)[i]);
Bongjun 6:e2ab76b2be07 570 }
Bongjun 6:e2ab76b2be07 571 if ((err-1%16) != 0) {
Bongjun 6:e2ab76b2be07 572 debug("\n");
Bongjun 6:e2ab76b2be07 573 }
Bongjun 6:e2ab76b2be07 574 }
Bongjun 6:e2ab76b2be07 575 #endif
Bongjun 6:e2ab76b2be07 576 return err;
Bongjun 6:e2ab76b2be07 577 }
Bongjun 6:e2ab76b2be07 578
Bongjun 6:e2ab76b2be07 579 nsapi_size_or_error_t W5500Interface::socket_recvfrom(nsapi_socket_t handle, SocketAddress *address,
Bongjun 6:e2ab76b2be07 580 void *buffer, nsapi_size_t size)
Bongjun 6:e2ab76b2be07 581 {
Bongjun 6:e2ab76b2be07 582 DBG("fd: %d\n", SKT(handle)->fd);
Bongjun 6:e2ab76b2be07 583 //check for null pointers
Bongjun 6:e2ab76b2be07 584 if (buffer == NULL) {
Bongjun 6:e2ab76b2be07 585 DBG("buffer is NULL; receive is ABORTED\n");
Bongjun 6:e2ab76b2be07 586 return -1;
Bongjun 6:e2ab76b2be07 587 }
Bongjun 6:e2ab76b2be07 588
Bongjun 6:e2ab76b2be07 589 uint8_t info[8];
Bongjun 6:e2ab76b2be07 590 int len = _w5500.wait_readable(SKT(handle)->fd, w5500_WAIT_TIMEOUT, sizeof(info));
Bongjun 6:e2ab76b2be07 591 if (len < 0) {
Bongjun 6:e2ab76b2be07 592 DBG("error: NSAPI_ERROR_WOULD_BLOCK\n");
Bongjun 6:e2ab76b2be07 593 return NSAPI_ERROR_WOULD_BLOCK;
Bongjun 6:e2ab76b2be07 594 }
Bongjun 6:e2ab76b2be07 595
Bongjun 6:e2ab76b2be07 596 //receive endpoint information
Bongjun 6:e2ab76b2be07 597 _w5500.recv(SKT(handle)->fd, (char*)info, sizeof(info));
Bongjun 6:e2ab76b2be07 598
Bongjun 6:e2ab76b2be07 599 char addr[17];
Bongjun 6:e2ab76b2be07 600 snprintf(addr, sizeof(addr), "%d.%d.%d.%d", info[0], info[1], info[2], info[3]);
Bongjun 6:e2ab76b2be07 601 uint16_t port = info[4]<<8|info[5];
Bongjun 6:e2ab76b2be07 602 // original behavior was to terminate execution if address is NULL
Bongjun 6:e2ab76b2be07 603 if (address != NULL) {
Bongjun 6:e2ab76b2be07 604 //DBG("[socket_recvfrom] warn: addressis NULL");
Bongjun 6:e2ab76b2be07 605 address->set_ip_address(addr);
Bongjun 6:e2ab76b2be07 606 address->set_port(port);
Bongjun 6:e2ab76b2be07 607 }
Bongjun 6:e2ab76b2be07 608
Bongjun 6:e2ab76b2be07 609 int udp_size = info[6]<<8|info[7];
Bongjun 6:e2ab76b2be07 610
Bongjun 6:e2ab76b2be07 611 if (udp_size > (len-sizeof(info))) {
Bongjun 6:e2ab76b2be07 612 DBG("error: udp_size > (len-sizeof(info))\n");
Bongjun 6:e2ab76b2be07 613 return -1;
Bongjun 6:e2ab76b2be07 614 }
Bongjun 6:e2ab76b2be07 615
Bongjun 6:e2ab76b2be07 616 //receive from socket
Bongjun 6:e2ab76b2be07 617 nsapi_size_or_error_t err = _w5500.recv(SKT(handle)->fd, (char*)buffer, udp_size);
Bongjun 6:e2ab76b2be07 618 DBG("rv: %d\n", err);
Bongjun 11:5118c2bff025 619
Bongjun 6:e2ab76b2be07 620 #if w5500_INTF_DBG
Bongjun 6:e2ab76b2be07 621 if (err > 0) {
Bongjun 6:e2ab76b2be07 622 debug("[socket_recvfrom] buffer:");
Bongjun 6:e2ab76b2be07 623 for(int i = 0; i < err; i++) {
Bongjun 6:e2ab76b2be07 624 if ((i%16) == 0) {
Bongjun 6:e2ab76b2be07 625 debug("\n");
Bongjun 6:e2ab76b2be07 626 }
Bongjun 6:e2ab76b2be07 627 debug(" %02x", ((uint8_t*)buffer)[i]);
Bongjun 6:e2ab76b2be07 628 }
Bongjun 6:e2ab76b2be07 629 if ((err-1%16) != 0) {
Bongjun 6:e2ab76b2be07 630 debug("\n");
Bongjun 6:e2ab76b2be07 631 }
Bongjun 6:e2ab76b2be07 632 }
Bongjun 6:e2ab76b2be07 633 #endif
Bongjun 6:e2ab76b2be07 634 return err;
Bongjun 6:e2ab76b2be07 635 }
Bongjun 6:e2ab76b2be07 636
Bongjun 6:e2ab76b2be07 637 void W5500Interface::socket_attach(void *handle, void (*callback)(void *), void *data)
Bongjun 6:e2ab76b2be07 638 {
Bongjun 16:380b4a629c4f 639 /*
Bongjun 16:380b4a629c4f 640 if (handle == NULL) return;
Bongjun 16:380b4a629c4f 641 DBG("fd: %d, callback: %p\n", SKT(handle)->fd, callback);
Bongjun 16:380b4a629c4f 642 SKT(handle)->callback = callback;
Bongjun 16:380b4a629c4f 643 SKT(handle)->callback_data = data;
Bongjun 16:380b4a629c4f 644 */
Bongjun 6:e2ab76b2be07 645 if (handle == NULL) return;
Bongjun 6:e2ab76b2be07 646 w5500_socket *socket = (w5500_socket *)handle;
Bongjun 6:e2ab76b2be07 647 w5500_sockets[socket->fd].callback = callback;
Bongjun 6:e2ab76b2be07 648 w5500_sockets[socket->fd].callback_data = data;
Bongjun 6:e2ab76b2be07 649 }
Bongjun 6:e2ab76b2be07 650
Bongjun 6:e2ab76b2be07 651 /*
Bongjun 6:e2ab76b2be07 652 void W5500Interface::event()
Bongjun 6:e2ab76b2be07 653 {
Bongjun 6:e2ab76b2be07 654 for(int i=0; i<MAX_SOCK_NUM; i++){
Bongjun 6:e2ab76b2be07 655 if (w5500_sockets[i].callback) {
Bongjun 6:e2ab76b2be07 656 w5500_sockets[i].callback(w5500_sockets[i].data);
Bongjun 6:e2ab76b2be07 657 }
Bongjun 6:e2ab76b2be07 658 }
Bongjun 6:e2ab76b2be07 659 }
Bongjun 6:e2ab76b2be07 660 */
Bongjun 11:5118c2bff025 661
Bongjun 11:5118c2bff025 662 nsapi_error_t W5500Interface::gethostbyname(const char *host,
Bongjun 16:380b4a629c4f 663 SocketAddress *address, nsapi_version_t version)
Bongjun 11:5118c2bff025 664 {
Bongjun 16:380b4a629c4f 665 DBG("DNS process %s", host);
Bongjun 11:5118c2bff025 666 bool isOK = dns.lookup(host);
Bongjun 11:5118c2bff025 667 if (isOK) {
Bongjun 13:9e2a7c8da30c 668 DBG("is ok\n");
Bongjun 13:9e2a7c8da30c 669 DBG(" IP: %s\n", dns.get_ip_address());
Bongjun 11:5118c2bff025 670 } else {
Bongjun 13:9e2a7c8da30c 671 DBG(" IP is not ok\n");
Bongjun 11:5118c2bff025 672 return NSAPI_ERROR_DNS_FAILURE;
Bongjun 11:5118c2bff025 673 }
Bongjun 16:380b4a629c4f 674
Bongjun 16:380b4a629c4f 675 if ( !address->set_ip_address(dns.get_ip_address()) ) {
Bongjun 16:380b4a629c4f 676 return NSAPI_ERROR_DNS_FAILURE;
Bongjun 16:380b4a629c4f 677 }
Bongjun 16:380b4a629c4f 678 return NSAPI_ERROR_OK;
Bongjun 11:5118c2bff025 679 }
Bongjun 18:afec30f0922a 680
Bongjun 18:afec30f0922a 681 nsapi_error_t W5500Interface::set_network(const char *ip_address, const char *netmask, const char *gateway)
Bongjun 18:afec30f0922a 682 {
Bongjun 18:afec30f0922a 683 init(ip_address, netmask, gateway);
Bongjun 18:afec30f0922a 684 return NSAPI_ERROR_OK;
Bongjun 18:afec30f0922a 685 }
Bongjun 18:afec30f0922a 686
Bongjun 18:afec30f0922a 687 nsapi_error_t W5500Interface::set_dhcp(bool dhcp)
Bongjun 18:afec30f0922a 688 {
Bongjun 18:afec30f0922a 689 _dhcp_enable = dhcp;
Bongjun 18:afec30f0922a 690 return NSAPI_ERROR_OK;
Bongjun 18:afec30f0922a 691 }