Skip to content

Commit cd71356

Browse files
committed
Added support for mDNS in AP Mode for Raspberry Pi
1 parent 203c999 commit cd71356

File tree

1 file changed

+56
-14
lines changed
  • ports/raspberrypi/common-hal/mdns

1 file changed

+56
-14
lines changed

ports/raspberrypi/common-hal/mdns/Server.c

Lines changed: 56 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include "shared-bindings/mdns/Server.h"
1414
#include "shared-bindings/wifi/__init__.h"
1515
#include "supervisor/shared/tick.h"
16-
1716
#include "lwip/apps/mdns.h"
1817
#include "lwip/prot/dns.h"
1918

@@ -38,7 +37,11 @@ void mdns_server_construct(mdns_server_obj_t *self, bool workflow) {
3837
mdns_resp_init();
3938
inited = true;
4039
} else {
41-
mdns_resp_restart(NETIF_STA);
40+
if (common_hal_wifi_radio_get_ap_active(&common_hal_wifi_radio_obj)) {
41+
mdns_resp_restart(NETIF_AP);
42+
} else {
43+
mdns_resp_restart(NETIF_STA);
44+
}
4245
}
4346
self->inited = true;
4447

@@ -73,7 +76,11 @@ void common_hal_mdns_server_deinit(mdns_server_obj_t *self) {
7376
}
7477
self->inited = false;
7578
object_inited = false;
76-
mdns_resp_remove_netif(NETIF_STA);
79+
if (common_hal_wifi_radio_get_ap_active(&common_hal_wifi_radio_obj)) {
80+
mdns_resp_remove_netif(NETIF_AP);
81+
} else {
82+
mdns_resp_remove_netif(NETIF_STA);
83+
}
7784
}
7885

7986
bool common_hal_mdns_server_deinited(mdns_server_obj_t *self) {
@@ -85,10 +92,18 @@ const char *common_hal_mdns_server_get_hostname(mdns_server_obj_t *self) {
8592
}
8693

8794
void common_hal_mdns_server_set_hostname(mdns_server_obj_t *self, const char *hostname) {
88-
if (mdns_resp_netif_active(NETIF_STA)) {
89-
mdns_resp_rename_netif(NETIF_STA, hostname);
95+
if (common_hal_wifi_radio_get_ap_active(&common_hal_wifi_radio_obj)) {
96+
if (mdns_resp_netif_active(NETIF_AP)) {
97+
mdns_resp_rename_netif(NETIF_AP, hostname);
98+
} else {
99+
mdns_resp_add_netif(NETIF_AP, hostname);
100+
}
90101
} else {
91-
mdns_resp_add_netif(NETIF_STA, hostname);
102+
if (mdns_resp_netif_active(NETIF_STA)) {
103+
mdns_resp_rename_netif(NETIF_STA, hostname);
104+
} else {
105+
mdns_resp_add_netif(NETIF_STA, hostname);
106+
}
92107
}
93108

94109
self->hostname = hostname;
@@ -177,9 +192,18 @@ size_t mdns_server_find(mdns_server_obj_t *self, const char *service_type, const
177192
state.out = out;
178193
state.out_len = out_len;
179194

180-
err_t err = mdns_search_service(NULL, service_type, proto,
181-
NETIF_STA, &search_result_cb, &state,
182-
&state.request_id);
195+
err_t err;
196+
197+
if (common_hal_wifi_radio_get_ap_active(&common_hal_wifi_radio_obj)) {
198+
err = mdns_search_service(NULL, service_type, proto,
199+
NETIF_AP, &search_result_cb, &state,
200+
&state.request_id);
201+
} else {
202+
err = mdns_search_service(NULL, service_type, proto,
203+
NETIF_STA, &search_result_cb, &state,
204+
&state.request_id);
205+
}
206+
183207
if (err != ERR_OK) {
184208
return 0;
185209
}
@@ -240,9 +264,18 @@ mp_obj_t common_hal_mdns_server_find(mdns_server_obj_t *self, const char *servic
240264
state.count = 0;
241265
state.head = NULL;
242266

243-
err_t err = mdns_search_service(NULL, service_type, proto,
244-
NETIF_STA, &alloc_search_result_cb, &state,
245-
&state.request_id);
267+
err_t err;
268+
269+
if (common_hal_wifi_radio_get_ap_active(&common_hal_wifi_radio_obj)) {
270+
err = mdns_search_service(NULL, service_type, proto,
271+
NETIF_AP, &alloc_search_result_cb, &state,
272+
&state.request_id);
273+
} else {
274+
err = mdns_search_service(NULL, service_type, proto,
275+
NETIF_STA, &alloc_search_result_cb, &state,
276+
&state.request_id);
277+
}
278+
246279
if (err != ERR_OK) {
247280
mp_raise_RuntimeError(MP_ERROR_TEXT("Unable to start mDNS query"));
248281
}
@@ -312,11 +345,20 @@ void common_hal_mdns_server_advertise_service(mdns_server_obj_t *self, const cha
312345
}
313346
}
314347
if (existing_slot < MDNS_MAX_SERVICES) {
315-
mdns_resp_del_service(NETIF_STA, existing_slot);
348+
if (common_hal_wifi_radio_get_ap_active(&common_hal_wifi_radio_obj)) {
349+
mdns_resp_del_service(NETIF_AP, existing_slot);
350+
} else {
351+
mdns_resp_del_service(NETIF_STA, existing_slot);
352+
}
316353
}
317354

318355
assign_txt_records(self, txt_records, num_txt_records);
319-
int8_t slot = mdns_resp_add_service(NETIF_STA, self->instance_name, service_type, proto, port, srv_txt_cb, self);
356+
int8_t slot;
357+
if (common_hal_wifi_radio_get_ap_active(&common_hal_wifi_radio_obj)) {
358+
slot = mdns_resp_add_service(NETIF_AP, self->instance_name, service_type, proto, port, srv_txt_cb, self);
359+
} else {
360+
slot = mdns_resp_add_service(NETIF_STA, self->instance_name, service_type, proto, port, srv_txt_cb, self);
361+
}
320362
if (slot < 0) {
321363
mp_raise_RuntimeError(MP_ERROR_TEXT("Out of MDNS service slots"));
322364
return;

0 commit comments

Comments
 (0)