Multiple Interfaces Sample Application
- This example show how to initialize µC/TCP-IP with multiple interface:
- Initialize Stack tasks and objects
- Initialize an Ethernet Interface
- Start that Ethernet Interface
- Configure IP addresses of the Ethernet Interface
- Initialize an Wireless Interface
- Start that Wireless Interface
- Scan for Wireless networks available
- Analyze scan result
- Join a Wireless network
- Configure IP addresses of that Wireless Interface
This example is based on template files so some modifications will be required, insert the appropriate project/board specific code to perform the stated actions. Note that the file init_multiple_if.c
, located in the folder $/Micrium/Software/uC-TCPIP/Examples/Init
, contains this sample application:
#include <cpu_core.h> #include <lib_mem.h> #include <Source/net.h> #include <Source/net_ascii.h> #include <IF/net_if.h> #include <IF/net_if_wifi.h> #ifdef NET_IPv4_MODULE_EN #include <IP/IPv4/net_ipv4.h> #endif #ifdef NET_IPv6_MODULE_EN #include <IP/IPv6/net_ipv6.h> #endif #include <Cfg/Template/net_dev_cfg.h> /* Device configuration header. */ #include <Dev/Ether/Template/net_dev_ether_template_dma.h> /* Device driver header. */ #include <Dev/WiFi/Template/net_dev_wifi_template_spi.h> /* Device driver header. */ #include <Dev/Ether/PHY/Generic/net_phy.h> /* PHY driver header. */ #include <Dev/WiFi/Manager/Generic/net_wifi_mgr.h> #include <BSP/Template/net_bsp_ether.h> /* BSP header. */ #include <BSP/Template/net_bsp_wifi.h> /* BSP header. */ CPU_BOOLEAN AppInit_TCPIP_MultipleIF (void) { #ifdef NET_IPv4_MODULE_EN NET_IPv4_ADDR addr_ipv4; NET_IPv4_ADDR msk_ipv4; NET_IPv4_ADDR gateway_ipv4; #endif #ifdef NET_IPv6_MODULE_EN CPU_BOOLEAN cfg_result; #if (NET_IPv6_CFG_ADDR_AUTO_CFG_EN == DEF_DISABLED) NET_FLAGS ipv6_flags; NET_IPv6_ADDR addr_ipv6; #endif #endif NET_IF_NBR if_nbr_ether; NET_IF_NBR if_nbr_wifi; NET_IF_WIFI_AP ap[10]; NET_IF_WIFI_SSID *p_ssid; NET_IF_WIFI_SSID ssid; NET_IF_WIFI_PSK psk; CPU_INT16U ctn; CPU_INT16U i; CPU_INT16S result; CPU_BOOLEAN found; NET_ERR err_net; /* ---- INIT NETWORK TASKS & OBJECTS ---- */ err_net = Net_Init(&NetRxTaskCfg, &NetTxDeallocTaskCfg, &NetTmrTaskCfg); if (err_net != NET_ERR_NONE) { return (DEF_FAIL); } /* ------- ADD ETHERNET INTERFACE ------- */ if_nbr_ether = NetIF_Add(&NetIF_API_Ether, &NetDev_API_TemplateEtherDMA, /* Device driver API */ &NetDev_BSP_BoardDev_Nbr, /* BSP API */ &NetDev_Cfg_Ether_1, /* Device configuration */ &NetPhy_API_Generic, /* PHY driver API */ &NetPhy_Cfg_Ether_1, /* PHY configuration */ &err_net); if (err_net != NET_IF_ERR_NONE) { return (DEF_FAIL); } /* ------ START ETHERNET INTERFACE ------ */ NetIF_Start(if_nbr_ether, &err_net); /* Makes the IF ready to RX and TX. */ if (err_net != NET_IF_ERR_NONE) { return (DEF_FAIL); } #ifdef NET_IPv4_MODULE_EN /* ------- CFG IPV4 STATIC ADDR --------- */ /* For Dynamic IPv4 cfg, DHCPc is required*/ /* Update IPv4 Addr following your ... */ /* ... network requirements. */ NetASCII_Str_to_IP("10.10.10.64", /* Convert IPv4 string addr to 32 bit addr*/ &addr_ipv4, NET_IPv4_ADDR_SIZE, &err_net); NetASCII_Str_to_IP("255.255.255.0", /* Convert IPv4 mask to 32 bit addr. */ &msk_ipv4, NET_IPv4_ADDR_SIZE, &err_net); NetASCII_Str_to_IP("10.10.10.1", /* Convert Gateway string to 32 bit addr. */ &gateway_ipv4, NET_IPv4_ADDR_SIZE, &err_net); NetIPv4_CfgAddrAdd(if_nbr_ether, /* Add a statically-configured IPv4 ... */ addr_ipv4, /* ... host addr, subnet mask, & ... */ msk_ipv4, /* ... default gateway to the IF. */ gateway_ipv4, &err_net); if (err_net != NET_IPv4_ERR_NONE) { return (DEF_FAIL); } #endif #ifdef NET_IPv6_MODULE_EN #if (NET_IPv6_CFG_ADDR_AUTO_CFG_EN == DEF_ENABLED) /* --- START IPV6 STATELESS AUTO-CFG ---- */ NetIPv6_AddrAutoCfgHookSet(if_nbr_ether, /* Set Hook to received Auto-Cfg result. */ &App_AutoCfgResult, &err_net); cfg_result = NetIPv6_AddrAutoCfgEn(if_nbr_ether, /* Enable and Start Auto-Cfg process. */ DEF_YES, &err_net); if (cfg_result == DEF_FAIL) { return (DEF_FAIL); } #else /* --- CFG IPV6 STATIC LINK LOCAL ADDR -- */ /* DHCPv6c is not yet available. */ /* TODO Update IPv6 Addr following your...*/ /* ... network requirements. */ NetASCII_Str_to_IP("fe80::1111:1111", /* Convert IPv6 string to 128 bit addr. */ &addr_ipv6, NET_IPv6_ADDR_SIZE, &err_net); ipv6_flags = 0; DEF_BIT_SET(ipv6_flags, NET_IPv6_FLAG_BLOCK_EN); /* Set Addr Cfg as blocking. */ DEF_BIT_SET(ipv6_flags, NET_IPv6_FLAG_DAD_EN); /* Enable DAD with Addr Configuration. */ cfg_result = NetIPv6_CfgAddrAdd(if_nbr_ether, /* Add a statically-configured IPv6 ... */ &addr_ipv6, /* ... host address to the interface. */ 64, ipv6_flags, &err_net); if (cfg_result == DEF_FAIL) { return (DEF_FAIL); } #endif #endif /* ------- ADD WIRELESS INTERFACE ------- */ if_nbr_wifi = NetIF_Add(&NetIF_API_WiFi, &NetDev_API_TemplateWiFiSpi, /* Change following your Device driver API*/ &NetDev_BSP_WiFi, /* Change for your BSP API. */ &NetDev_Cfg_WiFi_1, /* Change for Device configuration. */ &NetWiFiMgr_API_Generic, DEF_NULL, &err_net); if (err_net != NET_IF_ERR_NONE) { return (DEF_FAIL); } /* ------ START WIRELESS INTERFACE ------ */ NetIF_Start(if_nbr_wifi, &err_net); /* Makes the IF ready to RX and TX. */ if (err_net != NET_IF_ERR_NONE) { return (DEF_FAIL); } /* ----- SCAN FOR WIRELESS NETWORKS ----- */ ctn = NetIF_WiFi_Scan(if_nbr_wifi, ap, /* Access point table. */ 10, /* Access point table size. */ DEF_NULL, /* Hidden SSID. */ NET_IF_WIFI_CH_ALL, /* Channel to scan. */ &err_net); if (err_net != NET_IF_WIFI_ERR_NONE) { return (DEF_FAIL); } /* --- ANALYSE WIRELESS NETWORKS FOUND -- */ found = DEF_NO; for (i = 0; i < ctn - 1; i++) { /* Browse table of access point found. */ p_ssid = &ap[i].SSID; result = Str_Cmp_N((CPU_CHAR *)p_ssid, /* Search for a specific WiFi Network SSID*/ "Wifi_AP_SSID", /* Change for your WiFi Network SSID. */ NET_IF_WIFI_STR_LEN_MAX_SSID); if (result == 0) { found = DEF_YES; break; } } if (found == DEF_NO) { return (DEF_FAIL); } /* ------ JOIN A WIRELESS NETWORK ------- */ Mem_Clr(&ssid, sizeof(ssid)); Mem_Clr(&psk, sizeof(psk)); Str_Copy_N((CPU_CHAR *)&ssid, "Wifi_AP_SSID", /* Change for your WiFi Network SSID. */ 12); /* SSID string length. */ Str_Copy_N((CPU_CHAR *)&psk, "Password", /* Change for your WiFi Network Password. */ 8); /* PSK string length. */ NetIF_WiFi_Join(if_nbr_wifi, ap[i].NetType, /* WiFi Network type. */ NET_IF_WIFI_DATA_RATE_AUTO, /* Data rate. */ ap[i].SecurityType, /* WiFi Network security type. */ NET_IF_WIFI_PWR_LEVEL_HI, /* Power level. */ ssid, /* WiFi Network SSID. */ psk, /* WiFi Network PSK. */ &err_net); if (err_net != NET_IF_WIFI_ERR_NONE) { return (DEF_FAIL); } #ifdef NET_IPv4_MODULE_EN /* -------- CFG IPV4 STATIC ADDR -------- */ /* For Dynamic IPv4 cfg, DHCPc is required*/ /* Update IPv4 Addr following your ... */ /* ... network requirements. */ NetASCII_Str_to_IP("192.168.1.10", /* Convert IPv4 string addr to 32 bit addr*/ &addr_ipv4, NET_IPv4_ADDR_SIZE, &err_net); NetASCII_Str_to_IP("255.255.255.0", /* Convert Mask string to 32 bit addr. */ &msk_ipv4, NET_IPv4_ADDR_SIZE, &err_net); NetASCII_Str_to_IP("192.168.1.1", /* Convert Gateway string to 32 bit addr. */ &gateway_ipv4, NET_IPv4_ADDR_SIZE, &err_net); NetIPv4_CfgAddrAdd(if_nbr_wifi, /* Add a statically-configured IPv4 ... */ addr_ipv4, /* ... host addr, subnet mask, & ... */ msk_ipv4, /* ... default gateway to the IF. */ gateway_ipv4, &err_net); if (err_net != NET_IPv4_ERR_NONE) { return (DEF_FAIL); } #endif #ifdef NET_IPv6_MODULE_EN #if (NET_IPv6_CFG_ADDR_AUTO_CFG_EN == DEF_ENABLED) /* ---- START IPV6 STATELESS AUTO-CFG --- */ NetIPv6_AddrAutoCfgHookSet(if_nbr_wifi, /* Set hook to received Auto-Cfg result. */ &App_AutoCfgResult, /* TODO update ptr to hook defined in App.*/ &err_net); cfg_result = NetIPv6_AddrAutoCfgEn(if_nbr_wifi, /* Enable and Start Auto-Cfg process. */ DEF_YES, &err_net); if (cfg_result == DEF_FAIL) { return (DEF_FAIL); } #else /* --- CFG IPV6 STATIC LINK LOCAL ADDR -- */ /* DHCPv6c is not yet available. */ /* Update IPv6 Addr following your ... */ /* ... network requirements. */ NetASCII_Str_to_IP("fe80::4444:1111", /* Convert IPv6 string to 128 bit addr. */ &addr_ipv6, NET_IPv6_ADDR_SIZE, &err_net); ipv6_flags = 0; DEF_BIT_SET(ipv6_flags, NET_IPv6_FLAG_BLOCK_EN); /* Set Address Configuration as blocking. */ DEF_BIT_SET(ipv6_flags, NET_IPv6_FLAG_DAD_EN); /* Enable DAD with Address Configuration. */ cfg_result = NetIPv6_CfgAddrAdd(if_nbr_wifi, /* Add a statically-configured IPv6 ... */ &addr_ipv6, /* ... host address to the interface. */ 64, ipv6_flags, &err_net); if (cfg_result == DEF_FAIL) { return (DEF_FAIL); } #endif return (DEF_OK); }
Refer to the sample codes in section Ethernet Sample Application and WiFi Sample Application for more details on the diverse function calls.