27
27
28
28
#include <string.h>
29
29
30
+ #include "py/obj.h"
30
31
#include "py/objproperty.h"
32
+ #include "py/objstr.h"
31
33
#include "py/runtime.h"
32
34
#include "shared-bindings/mdns/__init__.h"
33
35
#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);
173
175
//|
174
176
//| If web workflow is active, the port it uses can't also be used to advertise a service.
175
177
//|
178
+ //| **Limitations**: Publishing up to 32 TXT records is only supported on the RP2040 Pico W board at
179
+ //| this time.
180
+ //|
176
181
//| :param str service_type: The service type such as "_http"
177
182
//| :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
+ //| """
179
186
//| ...
180
187
//|
181
188
STATIC mp_obj_t mdns_server_advertise_service (mp_uint_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
182
189
mdns_server_obj_t * self = MP_OBJ_TO_PTR (pos_args [0 ]);
183
190
check_for_deinit (self );
184
191
185
- enum { ARG_service_type , ARG_protocol , ARG_port };
192
+ enum { ARG_service_type , ARG_protocol , ARG_port , ARG_txt_records };
186
193
static const mp_arg_t allowed_args [] = {
187
194
{ MP_QSTR_service_type , MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_OBJ },
188
195
{ MP_QSTR_protocol , MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_OBJ },
189
196
{ 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 } },
190
198
};
191
199
192
200
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 *
195
203
const char * service_type = mp_obj_str_get_str (args [ARG_service_type ].u_obj );
196
204
const char * protocol = mp_obj_str_get_str (args [ARG_protocol ].u_obj );
197
205
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 );
199
221
return mp_const_none ;
200
222
}
201
223
STATIC MP_DEFINE_CONST_FUN_OBJ_KW (mdns_server_advertise_service_obj , 1 , mdns_server_advertise_service );
0 commit comments