NetworkServices with NUCLEO-L476RG and W5500 by SeeedStudio stack.
Dependents: coap-example Borsch coap-example
Fork of NetworkServices by
Revision 17:c976088bf39d, committed 2017-07-05
- Comitter:
- sgnezdov
- Date:
- Wed Jul 05 18:34:39 2017 +0000
- Parent:
- 16:82e7a0bf58d8
- Commit message:
- Changed setup interface to take MAC address, because W5500 may use software defined MAC when obtaining IP.
Changed in this revision
DHCPClient/DHCPClient.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/DHCPClient/DHCPClient.cpp Sat Jul 01 07:20:29 2017 +0000 +++ b/DHCPClient/DHCPClient.cpp Wed Jul 05 18:34:39 2017 +0000 @@ -4,7 +4,7 @@ #include "UDPSocket.h" #include "DHCPClient.h" -#define DBG_DHCP 1 +#define DBG_DHCP 0 #if DBG_DHCP #define DBG(...) do{debug("[%s:%d]", __PRETTY_FUNCTION__,__LINE__);debug(__VA_ARGS__);} while(0); @@ -110,24 +110,20 @@ void DHCPClient::callback() { - //DBG("DHCPClient callback\n"); + DBG("DHCPClient callback\n"); SocketAddress host; int recv_len = m_udp->recvfrom(&host, (char*)m_buf, sizeof(m_buf)); if (recv_len < 0) { - //DBG("recv_len < 0\n"); return; } if (!verify(m_buf, recv_len)) { - DBG("!verify(m_buf, recv_len)\n"); return; } int r = offer(m_buf, recv_len); if (r == DHCPOFFER) { - DBG("r == DHCPOFFER\n"); int send_size = request(); m_udp->sendto(m_server, (char*)m_buf, send_size); } else if (r == DHCPACK) { - DBG("r == DHCPACK\n"); exit_flag = true; } } @@ -171,42 +167,38 @@ UDPSocket udp_sock; m_udp = &udp_sock; - - nsapi_error_t err; - err = udp_sock.open(ns); - if (err) { - DBG("setup failed to open UDP socket."); - return err; + { + nsapi_error_t err = udp_sock.open(ns); + if (err) { + DBG("setup failed to open UDP socket."); + return err; + } + udp_sock.set_blocking(false); + err = udp_sock.bind(68); // local port + if (err) { + DBG("setup failed in bind: %d", err); + return err; + } } - udp_sock.set_blocking(false); - err = udp_sock.bind(68); // local port - if (err) { - DBG("setup failed in bind: %d", err); - return err; - } + m_server.set_ip_address("255.255.255.255"); // DHCP broadcast m_server.set_port(67); // DHCP broadcast exit_flag = false; - err = 0; + int err = 0; int seq = 0; int send_size; while(!exit_flag) { switch(seq) { case 0: - DBG("case 0\n"); m_retry = 0; seq++; break; case 1: - DBG("case 1\n"); send_size = discover(); - DBG("udp sock sendto begins\n"); - nsapi_size_or_error_t err; - err = udp_sock.sendto(m_server, (char*)m_buf, send_size); - DBG("udp sock sendto ends\n"); - if (err < 0) { - DBG("udp sendto error: %d\n", err); - return err; + nsapi_size_or_error_t err2 = udp_sock.sendto(m_server, (char*)m_buf, send_size); + if (err2 < 0) { + DBG("setup sendto error: %d\n", err2); + return err2; } m_interval.reset(); m_interval.start();