Skip to content

Commit 87fdf15

Browse files
PelsinCopilot
andauthored
implement mDNS support and hostname configuration (#1632)
Co-authored-by: Copilot <copilot@github.com>
1 parent 87277e6 commit 87fdf15

6 files changed

Lines changed: 29 additions & 8 deletions

File tree

headers/gp2040.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
#ifndef GP2040_H_
77
#define GP2040_H_
88

9+
#ifndef WEB_CONFIG_HOSTNAME
10+
#define WEB_CONFIG_HOSTNAME "gp2040-ce"
11+
#endif
12+
913
#include <map>
1014

1115
// GP2040 Classes

lib/lwip-port/CMakeLists.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@ add_library(lwip-port
22
${PICO_SDK_PATH}/lib/tinyusb/lib/networking/dhserver.c
33
${PICO_SDK_PATH}/lib/tinyusb/lib/networking/dnserver.c
44
)
5-
target_include_directories(lwip-port INTERFACE
5+
target_include_directories(lwip-port INTERFACE
66
.
77
arch
88
${PICO_SDK_PATH}/lib/tinyusb/lib/networking
99
)
10-
target_link_libraries(lwip-port
10+
target_link_libraries(lwip-port
1111
pico_stdlib
1212
pico_lwip
13+
pico_lwip_mdns
14+
pico_rand
1315
)
1416
target_include_directories(pico_lwip INTERFACE
1517
.
16-
)
18+
)

lib/lwip-port/lwipopts.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
#ifndef __LWIPOPTS_H__
3333
#define __LWIPOPTS_H__
3434

35+
#include "pico/rand.h"
36+
3537
/* Prevent having to link sys_arch.c (we don't test the API layers in unit tests) */
3638
#define NO_SYS 1
3739
#define MEM_ALIGNMENT 4
@@ -63,4 +65,11 @@
6365

6466
#define LWIP_SINGLE_NETIF 1
6567

68+
#define LWIP_IGMP 1
69+
#define LWIP_MDNS_RESPONDER 1
70+
#define MDNS_MAX_SERVICES 1
71+
#define LWIP_NUM_NETIF_CLIENT_DATA 1
72+
73+
#define LWIP_RAND() ((uint32_t)get_rand_32())
74+
6675
#endif /* __LWIPOPTS_H__ */

lib/rndis/rndis.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ try changing the first byte of tud_network_mac_address[] below from 0x02 to 0x00
4949
#include "lwip/init.h"
5050
#include "lwip/timeouts.h"
5151
#include "lwip/apps/httpd.h"
52+
#include "lwip/apps/mdns.h"
5253

5354
#define INIT_IP4(a,b,c,d) { PP_HTONL(LWIP_MAKEU32(a,b,c,d)) }
5455

@@ -117,7 +118,7 @@ static err_t netif_init_cb(struct netif *netif)
117118
{
118119
LWIP_ASSERT("netif != NULL", (netif != NULL));
119120
netif->mtu = CFG_TUD_NET_MTU;
120-
netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP | NETIF_FLAG_UP;
121+
netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP | NETIF_FLAG_UP | NETIF_FLAG_IGMP;
121122
netif->state = NULL;
122123
netif->name[0] = 'E';
123124
netif->name[1] = 'X';
@@ -154,7 +155,7 @@ bool dns_query_proc(const char *name, ip4_addr_t *addr)
154155

155156
bool tud_network_recv_cb(const uint8_t *src, uint16_t size)
156157
{
157-
/* this shouldn't happen, but if we get another packet before
158+
/* this shouldn't happen, but if we get another packet before
158159
parsing the previous, we must signal our inability to accept it */
159160
if (received_frame)
160161
return false;
@@ -209,9 +210,12 @@ void tud_network_init_cb(void)
209210
pbuf_free(received_frame);
210211
received_frame = NULL;
211212
}
213+
214+
/* re-announce mDNS, so hostname resolves after reconnect, getting issues on windows on hard-refresh without this */
215+
mdns_resp_announce(&netif_data);
212216
}
213217

214-
int rndis_init(void)
218+
int rndis_init(const char *hostname)
215219
{
216220
// Removed as rndis defect 07-08-2025 as we need to setup TinyUSB to non-default values
217221
///* initialize TinyUSB */
@@ -225,6 +229,8 @@ int rndis_init(void)
225229
;
226230
while (dnserv_init(&ipaddr, 53, dns_query_proc) != ERR_OK)
227231
;
232+
mdns_resp_init();
233+
mdns_resp_add_netif(&netif_data, hostname);
228234
httpd_init();
229235

230236
return 0;

lib/rndis/rndis.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
extern "C" {
66
#endif
77

8-
int rndis_init(void);
8+
int rndis_init(const char *hostname);
99
void rndis_task(void);
1010

1111
#ifdef __cplusplus

src/gp2040.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ void GP2040::run() {
245245
USBHostManager::getInstance().start();
246246

247247
if (configMode == true ) {
248-
rndis_init();
248+
rndis_init(WEB_CONFIG_HOSTNAME);
249249
}
250250

251251
while (1) { // LOOP

0 commit comments

Comments
 (0)