Skip to content

Commit a352097

Browse files
committed
Merge branch 'example/multi_interface' into 'master'
feat(example): Add tcp client example for multiple interface. See merge request espressif/esp-idf!6918
2 parents 514596c + 761c3a3 commit a352097

File tree

18 files changed

+582
-68
lines changed

18 files changed

+582
-68
lines changed

components/esp_netif/include/esp_netif.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,22 @@ esp_err_t esp_netif_set_old_ip_info(esp_netif_t *esp_netif, const esp_netif_ip_i
402402
*/
403403
int esp_netif_get_netif_impl_index(esp_netif_t *esp_netif);
404404

405+
/**
406+
* @brief Get net interface name from network stack implementation
407+
*
408+
* @note This name could be used in `setsockopt()` to bind socket with appropriate interface
409+
*
410+
* @param[in] esp_netif Handle to esp-netif instance
411+
* @param[out] name Interface name as specified in underlying TCP/IP stack. Note that the
412+
* actual name will be copied to the specified buffer, which must be allocated to hold
413+
* maximum interface name size (6 characters for lwIP)
414+
*
415+
* @return
416+
* - ESP_OK
417+
* - ESP_ERR_ESP_NETIF_INVALID_PARAMS
418+
*/
419+
esp_err_t esp_netif_get_netif_impl_name(esp_netif_t *esp_netif, char* name);
420+
405421
/**
406422
* @}
407423
*/
@@ -648,6 +664,17 @@ esp_err_t esp_netif_get_ip6_linklocal(esp_netif_t *esp_netif, esp_ip6_addr_t *if
648664
*/
649665
esp_err_t esp_netif_get_ip6_global(esp_netif_t *esp_netif, esp_ip6_addr_t *if_ip6);
650666

667+
/**
668+
* @brief Get all IPv6 addresses of the specified interface
669+
*
670+
* @param[in] esp_netif Handle to esp-netif instance
671+
* @param[out] if_ip6 Array of IPv6 addresses will be copied to the argument
672+
*
673+
* @return
674+
* number of returned IPv6 addresses
675+
*/
676+
int esp_netif_get_all_ip6(esp_netif_t *esp_netif, esp_ip6_addr_t if_ip6[]);
677+
651678
/**
652679
* @brief Sets IPv4 address to the specified octets
653680
*

components/esp_netif/lwip/esp_netif_lwip.c

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1446,7 +1446,7 @@ static esp_err_t esp_netif_create_ip6_linklocal_api(esp_netif_api_msg_t *msg)
14461446
{
14471447
esp_netif_t *esp_netif = msg->esp_netif;
14481448

1449-
ESP_LOGD(TAG, "%s esp-netif:%p", __func__, esp_netif);
1449+
ESP_LOGV(TAG, "%s esp-netif:%p", __func__, esp_netif);
14501450

14511451
struct netif *p_netif = esp_netif->lwip_netif;
14521452
if (p_netif != NULL && netif_is_up(p_netif)) {
@@ -1462,7 +1462,7 @@ esp_err_t esp_netif_create_ip6_linklocal(esp_netif_t *esp_netif) _RUN_IN_LWIP_TA
14621462

14631463
esp_err_t esp_netif_get_ip6_linklocal(esp_netif_t *esp_netif, esp_ip6_addr_t *if_ip6)
14641464
{
1465-
ESP_LOGD(TAG, "%s esp-netif:%p", __func__, esp_netif);
1465+
ESP_LOGV(TAG, "%s esp-netif:%p", __func__, esp_netif);
14661466

14671467
if (esp_netif == NULL || if_ip6 == NULL || esp_netif->is_ppp_netif) {
14681468
return ESP_ERR_ESP_NETIF_INVALID_PARAMS;
@@ -1479,7 +1479,7 @@ esp_err_t esp_netif_get_ip6_linklocal(esp_netif_t *esp_netif, esp_ip6_addr_t *if
14791479

14801480
esp_err_t esp_netif_get_ip6_global(esp_netif_t *esp_netif, esp_ip6_addr_t *if_ip6)
14811481
{
1482-
ESP_LOGD(TAG, "%s esp-netif:%p", __func__, esp_netif);
1482+
ESP_LOGV(TAG, "%s esp-netif:%p", __func__, esp_netif);
14831483

14841484
if (esp_netif == NULL || if_ip6 == NULL) {
14851485
return ESP_ERR_ESP_NETIF_INVALID_PARAMS;
@@ -1496,10 +1496,31 @@ esp_err_t esp_netif_get_ip6_global(esp_netif_t *esp_netif, esp_ip6_addr_t *if_ip
14961496
}
14971497
}
14981498
}
1499-
1499+
15001500
return ESP_FAIL;
15011501
}
15021502

1503+
int esp_netif_get_all_ip6(esp_netif_t *esp_netif, esp_ip6_addr_t if_ip6[])
1504+
{
1505+
ESP_LOGV(TAG, "%s esp-netif:%p", __func__, esp_netif);
1506+
1507+
if (esp_netif == NULL || if_ip6 == NULL) {
1508+
return 0;
1509+
}
1510+
1511+
int addr_count = 0;
1512+
struct netif *p_netif = esp_netif->lwip_netif;
1513+
1514+
if (p_netif != NULL && netif_is_up(p_netif)) {
1515+
for (int i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
1516+
if (!ip_addr_cmp(&p_netif->ip6_addr[i], IP6_ADDR_ANY)) {
1517+
memcpy(&if_ip6[addr_count++], &p_netif->ip6_addr[i], sizeof(ip6_addr_t));
1518+
}
1519+
}
1520+
}
1521+
return addr_count;
1522+
}
1523+
15031524
esp_netif_flags_t esp_netif_get_flags(esp_netif_t *esp_netif)
15041525
{
15051526
return esp_netif->flags;
@@ -1715,4 +1736,15 @@ int esp_netif_get_netif_impl_index(esp_netif_t *esp_netif)
17151736
return netif_get_index(esp_netif->lwip_netif);
17161737
}
17171738

1739+
esp_err_t esp_netif_get_netif_impl_name(esp_netif_t *esp_netif, char* name)
1740+
{
1741+
ESP_LOGD(TAG, "%s esp_netif:%p", __func__, esp_netif);
1742+
1743+
if (esp_netif == NULL || esp_netif->lwip_netif == NULL) {
1744+
return ESP_ERR_ESP_NETIF_INVALID_PARAMS;
1745+
}
1746+
netif_index_to_name(netif_get_index(esp_netif->lwip_netif), name);
1747+
return ESP_OK;
1748+
}
1749+
17181750
#endif /* CONFIG_ESP_NETIF_TCPIP_LWIP */

examples/common_components/protocol_examples_common/Kconfig.projbuild

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
11
menu "Example Connection Configuration"
2-
choice EXAMPLE_CONNECT_INTERFACE
3-
prompt "Connect using"
4-
default EXAMPLE_CONNECT_WIFI
5-
help
6-
Protocol examples can use Wi-Fi or Ethernet to connect to the network.
7-
Choose which interface to use.
8-
9-
config EXAMPLE_CONNECT_WIFI
10-
bool "Wi-Fi"
112

12-
config EXAMPLE_CONNECT_ETHERNET
13-
bool "Ethernet"
14-
15-
endchoice
3+
config EXAMPLE_CONNECT_WIFI
4+
bool "connect using WiFi interface"
5+
default y
6+
help
7+
Protocol examples can use Wi-Fi and/or Ethernet to connect to the network.
8+
Choose this option to connect with WiFi
169

1710
if EXAMPLE_CONNECT_WIFI
1811
config EXAMPLE_WIFI_SSID
@@ -29,6 +22,13 @@ menu "Example Connection Configuration"
2922
Can be left blank if the network has no security set.
3023
endif
3124

25+
config EXAMPLE_CONNECT_ETHERNET
26+
bool "connect using Ethernet interface"
27+
default n
28+
help
29+
Protocol examples can use Wi-Fi and/or Ethernet to connect to the network.
30+
Choose this option to connect with Ethernet
31+
3232
if EXAMPLE_CONNECT_ETHERNET
3333
choice EXAMPLE_USE_ETHERNET
3434
prompt "Ethernet Type"

0 commit comments

Comments
 (0)