Sergei G / Mbed OS Borsch

Dependencies:   DataStore JobScheduler NetworkServices W5500Interface nanopb protocol

Committer:
sgnezdov
Date:
Fri Aug 11 19:07:20 2017 +0000
Revision:
28:7214f7806526
Parent:
27:60c12f3f3430
fixed compilation bug

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sgnezdov 2:661c545d718e 1 #include "netstack.h"
sgnezdov 2:661c545d718e 2
sgnezdov 2:661c545d718e 3 #include "mbed-trace/mbed_trace.h"
sgnezdov 2:661c545d718e 4 #define TRACE_GROUP "net"
sgnezdov 2:661c545d718e 5
sgnezdov 2:661c545d718e 6 #include "mbed.h"
sgnezdov 2:661c545d718e 7
sgnezdov 2:661c545d718e 8 #include "nsapi_dns.h" // standard DNS
sgnezdov 2:661c545d718e 9
sgnezdov 2:661c545d718e 10 #define ETH_W5500 1
sgnezdov 2:661c545d718e 11
sgnezdov 2:661c545d718e 12 #if defined(ETH_W5500)
sgnezdov 5:8972e5de8b8c 13
sgnezdov 5:8972e5de8b8c 14 #include "W5500Interface.h" // W5500 driver for mbed
sgnezdov 5:8972e5de8b8c 15 #include "DHCPClient.h" // DHCPClient from W5500 driver
sgnezdov 2:661c545d718e 16 DHCPClient dhcp;
sgnezdov 5:8972e5de8b8c 17
sgnezdov 2:661c545d718e 18 #if defined(TARGET_NUCLEO_L476RG)
sgnezdov 2:661c545d718e 19 SPI spi(PA_7,PA_6,PA_5); // mosi, miso, sclk
sgnezdov 2:661c545d718e 20 W5500Interface eth(&spi, PB_6, NC); // mosi, miso, sclk, cs, reset
sgnezdov 5:8972e5de8b8c 21 #endif // of TARGET_NUCLEO_L476RG
sgnezdov 5:8972e5de8b8c 22
sgnezdov 5:8972e5de8b8c 23 #endif // of ETH_W5500
sgnezdov 2:661c545d718e 24
sgnezdov 2:661c545d718e 25 /* \brief print_MAC - print_MAC - helper function to print out MAC address
sgnezdov 2:661c545d718e 26 * in: network_interface - pointer to network i/f
sgnezdov 2:661c545d718e 27 * bool log-messages print out logs or not
sgnezdov 2:661c545d718e 28 * MAC address is print, if it can be acquired & log_messages is true.
sgnezdov 2:661c545d718e 29 *
sgnezdov 2:661c545d718e 30 */
sgnezdov 2:661c545d718e 31 void print_MAC(NetworkInterface* network_interface) {
sgnezdov 2:661c545d718e 32 const char *mac_addr = network_interface->get_mac_address();
sgnezdov 2:661c545d718e 33 if (mac_addr == NULL) {
sgnezdov 2:661c545d718e 34 tr_debug("No MAC address");
sgnezdov 2:661c545d718e 35 return;
sgnezdov 2:661c545d718e 36 }
sgnezdov 2:661c545d718e 37 tr_debug("MAC: %s", mac_addr);
sgnezdov 2:661c545d718e 38 }
sgnezdov 2:661c545d718e 39
sgnezdov 2:661c545d718e 40 NetworkInterface* initNetworkStack(uint8_t mac_addr[6]) {
sgnezdov 2:661c545d718e 41 NetworkInterface* network_interface = 0;
sgnezdov 2:661c545d718e 42 int connect_success = -1;
sgnezdov 27:60c12f3f3430 43 // This should be removed once mbedOS supports proper dual-stack
sgnezdov 2:661c545d718e 44 #if (MBED_CONF_LWIP_IPV6_ENABLED==true)
sgnezdov 2:661c545d718e 45 tr_error("IPv6 mode");
sgnezdov 2:661c545d718e 46 #else
sgnezdov 2:661c545d718e 47 tr_debug("IPv4 mode");
sgnezdov 2:661c545d718e 48 #endif
sgnezdov 2:661c545d718e 49
sgnezdov 2:661c545d718e 50 char ip[24], gateway[24], netmask[24], dnsaddr[24];
sgnezdov 2:661c545d718e 51 #if defined(ETH_W5500)
sgnezdov 2:661c545d718e 52 spi.format(32, 0);
sgnezdov 2:661c545d718e 53 spi.frequency(100000);
sgnezdov 2:661c545d718e 54 wait(1);
sgnezdov 2:661c545d718e 55
sgnezdov 2:661c545d718e 56 tr_debug("Using W5500");
sgnezdov 2:661c545d718e 57 eth.init(mac_addr);
sgnezdov 2:661c545d718e 58 int timeout_ms = 15*1000;
sgnezdov 2:661c545d718e 59 int err = dhcp.setup(&eth, mac_addr, timeout_ms);
sgnezdov 2:661c545d718e 60 if (err == (-1)) {
sgnezdov 2:661c545d718e 61 tr_error("DHCP Timeout.");
sgnezdov 2:661c545d718e 62 return NULL;
sgnezdov 2:661c545d718e 63 }
sgnezdov 2:661c545d718e 64 tr_debug("Connected; IP: %d.%d.%d.%d", dhcp.yiaddr[0], dhcp.yiaddr[1], dhcp.yiaddr[2], dhcp.yiaddr[3]);
sgnezdov 2:661c545d718e 65 if (0==dhcp.yiaddr[0] && 0==dhcp.yiaddr[1] && 0==dhcp.yiaddr[2] && 0==dhcp.yiaddr[3]) {
sgnezdov 2:661c545d718e 66 tr_error("IP is 0.0.0.0 after DHCP");
sgnezdov 2:661c545d718e 67 return NULL;
sgnezdov 2:661c545d718e 68 }
sgnezdov 2:661c545d718e 69 sprintf(ip, "%d.%d.%d.%d", dhcp.yiaddr[0], dhcp.yiaddr[1], dhcp.yiaddr[2], dhcp.yiaddr[3]);
sgnezdov 2:661c545d718e 70 sprintf(gateway, "%d.%d.%d.%d", dhcp.gateway[0], dhcp.gateway[1], dhcp.gateway[2], dhcp.gateway[3]);
sgnezdov 2:661c545d718e 71 sprintf(netmask, "%d.%d.%d.%d", dhcp.netmask[0], dhcp.netmask[1], dhcp.netmask[2], dhcp.netmask[3]);
sgnezdov 2:661c545d718e 72 sprintf(dnsaddr, "%d.%d.%d.%d", dhcp.dnsaddr[0], dhcp.dnsaddr[1], dhcp.dnsaddr[2], dhcp.dnsaddr[3]);
sgnezdov 2:661c545d718e 73 eth.init(ip, netmask, gateway);
sgnezdov 2:661c545d718e 74 #endif
sgnezdov 2:661c545d718e 75
sgnezdov 2:661c545d718e 76 const SocketAddress dns_sa = SocketAddress((const void *)&dhcp.dnsaddr[0], NSAPI_IPv4, 53);
sgnezdov 2:661c545d718e 77 nsapi_error_t nsErr = nsapi_dns_add_server(dns_sa);
sgnezdov 2:661c545d718e 78 if (nsErr) {
sgnezdov 2:661c545d718e 79 tr_error("error adding DNS entry: %d", nsErr);
sgnezdov 2:661c545d718e 80 return NULL;
sgnezdov 2:661c545d718e 81 }
sgnezdov 4:b360d4f0bf34 82 tr_debug("DNS IP: %s", dns_sa.get_ip_address());
sgnezdov 2:661c545d718e 83
sgnezdov 2:661c545d718e 84 // SocketAddress coap_sa;
sgnezdov 2:661c545d718e 85 // nsErr = nsapi_dns_query(&eth, "coap.me", &coap_sa, NSAPI_IPv4);
sgnezdov 2:661c545d718e 86 // if (nsErr) {
sgnezdov 2:661c545d718e 87 // tr_debug("[EasyConnect] error resolving coap.me: %d\n", nsErr);
sgnezdov 2:661c545d718e 88 // }
sgnezdov 2:661c545d718e 89
sgnezdov 2:661c545d718e 90 network_interface = ð
sgnezdov 2:661c545d718e 91 connect_success = eth.connect();
sgnezdov 2:661c545d718e 92
sgnezdov 2:661c545d718e 93 print_MAC(network_interface);
sgnezdov 2:661c545d718e 94 if(connect_success != 0) {
sgnezdov 2:661c545d718e 95 tr_error("Failed to connect: %d!", connect_success);
sgnezdov 2:661c545d718e 96 return NULL;
sgnezdov 2:661c545d718e 97 }
sgnezdov 2:661c545d718e 98 tr_debug("Connected to Network successfully");
sgnezdov 2:661c545d718e 99
sgnezdov 2:661c545d718e 100 const char *ip_addr = network_interface->get_ip_address();
sgnezdov 2:661c545d718e 101 if (ip_addr == NULL) {
sgnezdov 2:661c545d718e 102 tr_error("There is NO IP address");
sgnezdov 2:661c545d718e 103 return NULL;
sgnezdov 2:661c545d718e 104 }
sgnezdov 2:661c545d718e 105 tr_debug("IP: %s", ip_addr);
sgnezdov 2:661c545d718e 106
sgnezdov 2:661c545d718e 107 return network_interface;
sgnezdov 2:661c545d718e 108 }