2727
2828#include <string.h>
2929
30+ #include "py/obj.h"
3031#include "py/objproperty.h"
32+ #include "py/objstr.h"
3133#include "py/runtime.h"
3234#include "shared-bindings/mdns/__init__.h"
3335#include "shared-bindings/mdns/Server.h"
@@ -173,20 +175,26 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(mdns_server_find_obj, 1, _mdns_server_find);
173175//|
174176//| If web workflow is active, the port it uses can't also be used to advertise a service.
175177//|
178+ //| **Limitations**: Publishing up to 32 TXT records is only supported on the RP2040 Pico W board at
179+ //| this time.
180+ //|
176181//| :param str service_type: The service type such as "_http"
177182//| :param str protocol: The service protocol such as "_tcp"
178- //| :param int port: The port used by the service"""
183+ //| :param int port: The port used by the service
184+ //| :param Sequence[str] txt_records: An optional sequence of strings to serve as TXT records along with the service
185+ //| """
179186//| ...
180187//|
181188STATIC mp_obj_t mdns_server_advertise_service (mp_uint_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
182189 mdns_server_obj_t * self = MP_OBJ_TO_PTR (pos_args [0 ]);
183190 check_for_deinit (self );
184191
185- enum { ARG_service_type , ARG_protocol , ARG_port };
192+ enum { ARG_service_type , ARG_protocol , ARG_port , ARG_txt_records };
186193 static const mp_arg_t allowed_args [] = {
187194 { MP_QSTR_service_type , MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_OBJ },
188195 { MP_QSTR_protocol , MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_OBJ },
189196 { MP_QSTR_port , MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_INT },
197+ { MP_QSTR_txt_records , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_obj = mp_const_none } },
190198 };
191199
192200 mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
@@ -195,7 +203,21 @@ STATIC mp_obj_t mdns_server_advertise_service(mp_uint_t n_args, const mp_obj_t *
195203 const char * service_type = mp_obj_str_get_str (args [ARG_service_type ].u_obj );
196204 const char * protocol = mp_obj_str_get_str (args [ARG_protocol ].u_obj );
197205
198- common_hal_mdns_server_advertise_service (self , service_type , protocol , args [ARG_port ].u_int );
206+ const mp_obj_t txt_records = args [ARG_txt_records ].u_obj ;
207+ const size_t num_txt_records = txt_records == mp_const_none
208+ ? 0
209+ : (size_t )MP_OBJ_SMALL_INT_VALUE (mp_obj_len (txt_records ));
210+
211+ const char * txt_records_array [num_txt_records ];
212+ for (size_t i = 0 ; i < num_txt_records ; i ++ ) {
213+ mp_obj_t txt_record = mp_obj_subscr (txt_records , MP_OBJ_NEW_SMALL_INT (i ), MP_OBJ_SENTINEL );
214+ if (!mp_obj_is_str_or_bytes (txt_record )) {
215+ mp_raise_ValueError (MP_ERROR_TEXT ("Failed to add service TXT record; non-string or bytes found in txt_records" ));
216+ }
217+ txt_records_array [i ] = mp_obj_str_get_str (txt_record );
218+ }
219+
220+ common_hal_mdns_server_advertise_service (self , service_type , protocol , args [ARG_port ].u_int , txt_records_array , num_txt_records );
199221 return mp_const_none ;
200222}
201223STATIC MP_DEFINE_CONST_FUN_OBJ_KW (mdns_server_advertise_service_obj , 1 , mdns_server_advertise_service );
0 commit comments