3535
3636STATIC bool inited = false;
3737
38- void mdns_server_construct (mdns_server_obj_t * self ) {
38+ void mdns_server_construct (mdns_server_obj_t * self , bool workflow ) {
3939 if (inited ) {
4040 return ;
4141 }
@@ -46,19 +46,21 @@ void mdns_server_construct(mdns_server_obj_t *self) {
4646 snprintf (self -> default_hostname , sizeof (self -> default_hostname ), "cpy-%02x%02x%02x" , mac [3 ], mac [4 ], mac [5 ]);
4747 common_hal_mdns_server_set_hostname (self , self -> default_hostname );
4848
49- // Set a delegated entry to ourselves. This allows us to respond to "circuitpython.local"
50- // queries as well.
51- // TODO: Allow for disabling this with `supervisor.disable_web_workflow()`.
52- mdns_ip_addr_t our_ip ;
53- esp_netif_get_ip_info (common_hal_wifi_radio_obj .netif , & common_hal_wifi_radio_obj .ip_info );
54- our_ip .next = NULL ;
55- our_ip .addr .type = ESP_IPADDR_TYPE_V4 ;
56- our_ip .addr .u_addr .ip4 = common_hal_wifi_radio_obj .ip_info .ip ;
57- our_ip .addr .u_addr .ip6 .addr [1 ] = 0 ;
58- our_ip .addr .u_addr .ip6 .addr [2 ] = 0 ;
59- our_ip .addr .u_addr .ip6 .addr [3 ] = 0 ;
60- our_ip .addr .u_addr .ip6 .zone = 0 ;
61- mdns_delegate_hostname_add ("circuitpython" , & our_ip );
49+ if (workflow ) {
50+ // Set a delegated entry to ourselves. This allows us to respond to "circuitpython.local"
51+ // queries as well.
52+ // TODO: Allow for disabling this with `supervisor.disable_web_workflow()`.
53+ mdns_ip_addr_t our_ip ;
54+ esp_netif_get_ip_info (common_hal_wifi_radio_obj .netif , & common_hal_wifi_radio_obj .ip_info );
55+ our_ip .next = NULL ;
56+ our_ip .addr .type = ESP_IPADDR_TYPE_V4 ;
57+ our_ip .addr .u_addr .ip4 = common_hal_wifi_radio_obj .ip_info .ip ;
58+ our_ip .addr .u_addr .ip6 .addr [1 ] = 0 ;
59+ our_ip .addr .u_addr .ip6 .addr [2 ] = 0 ;
60+ our_ip .addr .u_addr .ip6 .addr [3 ] = 0 ;
61+ our_ip .addr .u_addr .ip6 .zone = 0 ;
62+ mdns_delegate_hostname_add ("circuitpython" , & our_ip );
63+ }
6264}
6365
6466void common_hal_mdns_server_construct (mdns_server_obj_t * self , mp_obj_t network_interface ) {
@@ -69,7 +71,7 @@ void common_hal_mdns_server_construct(mdns_server_obj_t *self, mp_obj_t network_
6971 if (inited ) {
7072 mp_raise_RuntimeError (translate ("mDNS already initialized" ));
7173 }
72- mdns_server_construct (self );
74+ mdns_server_construct (self , false );
7375}
7476
7577void common_hal_mdns_server_deinit (mdns_server_obj_t * self ) {
@@ -104,6 +106,48 @@ void common_hal_mdns_server_set_instance_name(mdns_server_obj_t *self, const cha
104106 self -> instance_name = instance_name ;
105107}
106108
109+ size_t mdns_server_find (mdns_server_obj_t * self , const char * service_type , const char * protocol ,
110+ mp_float_t timeout , mdns_remoteservice_obj_t * out , size_t out_len ) {
111+ mdns_search_once_t * search = mdns_query_async_new (NULL , service_type , protocol , MDNS_TYPE_PTR , timeout * 1000 , 255 , NULL );
112+ if (search == NULL ) {
113+ return 0 ;
114+ }
115+ mdns_result_t * results ;
116+ while (!mdns_query_async_get_results (search , 1 , & results )) {
117+ RUN_BACKGROUND_TASKS ;
118+ }
119+ mdns_query_async_delete (search );
120+ // Count how many results we got.
121+ // TODO: Remove this loop when moving off 4.4. Newer APIs will give us num_results
122+ // back directly.
123+ mdns_result_t * next = results ;
124+ uint8_t num_results = 0 ;
125+ while (next != NULL ) {
126+ num_results ++ ;
127+ next = next -> next ;
128+ }
129+
130+ next = results ;
131+ // Don't error if we're out of memory. Instead, truncate the tuple.
132+ uint8_t added = 0 ;
133+ while (next != NULL && added < out_len ) {
134+ mdns_remoteservice_obj_t * service = & out [added ];
135+
136+ service -> result = next ;
137+ service -> base .type = & mdns_remoteservice_type ;
138+ next = next -> next ;
139+ // Break the linked list so we free each result separately.
140+ service -> result -> next = NULL ;
141+ added ++ ;
142+ }
143+ if (added < out_len ) {
144+ // Free the remaining results from the IDF because we don't have
145+ // enough space in Python.
146+ mdns_query_results_free (next );
147+ }
148+ return num_results ;
149+ }
150+
107151mp_obj_t common_hal_mdns_server_find (mdns_server_obj_t * self , const char * service_type , const char * protocol , mp_float_t timeout ) {
108152 mdns_search_once_t * search = mdns_query_async_new (NULL , service_type , protocol , MDNS_TYPE_PTR , timeout * 1000 , 255 , NULL );
109153 if (search == NULL ) {
0 commit comments