@@ -105,7 +105,7 @@ typedef struct {
105105 uint8_t right [3 ];
106106 uint8_t center ;
107107 lwp3_hub_kind_t hub_kind ;
108- // Name used to filter advertisements and responses.
108+ // Null-terminated name used to filter advertisements and responses.
109109 // Also used as the name of the device when setting the name, since this
110110 // is not updated in the driver until the next time it connects.
111111 char name [LWP3_MAX_HUB_PROPERTY_NAME_SIZE + 1 ];
@@ -168,8 +168,9 @@ static pbdrv_bluetooth_ad_match_result_flags_t lwp3_advertisement_response_match
168168 flags |= PBDRV_BLUETOOTH_AD_MATCH_ADDRESS ;
169169 }
170170
171- // Compare name to user-provided name if given.
172- if (lwp3device -> name [0 ] != '\0' && strncmp (name , lwp3device -> name , 20 ) != 0 ) {
171+ // Compare name to user-provided name if given, checking only up to the
172+ // user provided name length.
173+ if (lwp3device -> name [0 ] != '\0' && strncmp (name , lwp3device -> name , strlen (lwp3device -> name )) != 0 ) {
173174 flags |= PBDRV_BLUETOOTH_AD_MATCH_NAME_FAILED ;
174175 }
175176
@@ -182,7 +183,7 @@ static void pb_lwp3device_assert_connected(void) {
182183 }
183184}
184185
185- static void pb_lwp3device_connect (const char * name , mp_int_t timeout , lwp3_hub_kind_t hub_kind , pbdrv_bluetooth_receive_handler_t notification_handler , bool pair ) {
186+ static void pb_lwp3device_connect (mp_obj_t name_in , mp_obj_t timeout_in , lwp3_hub_kind_t hub_kind , pbdrv_bluetooth_receive_handler_t notification_handler , bool pair ) {
186187 pb_lwp3device_t * lwp3device = & pb_lwp3device_singleton ;
187188
188189 // REVISIT: for now, we only allow a single connection to a LWP3 device.
@@ -203,7 +204,15 @@ static void pb_lwp3device_connect(const char *name, mp_int_t timeout, lwp3_hub_k
203204
204205 // Hub kind and name are set to filter advertisements and responses.
205206 lwp3device -> hub_kind = hub_kind ;
206- if (name ) {
207+ if (name_in == mp_const_none ) {
208+ lwp3device -> name [0 ] = '\0' ;
209+ } else {
210+ // Guaranteed to be zero-terminated when using this getter.
211+ const char * name = mp_obj_str_get_str (name_in );
212+ size_t len = strlen (name );
213+ if (len > sizeof (lwp3device -> name ) - 1 ) {
214+ mp_raise_ValueError (MP_ERROR_TEXT ("Name too long" ));
215+ }
207216 strncpy (lwp3device -> name , name , sizeof (lwp3device -> name ));
208217 }
209218
@@ -213,6 +222,8 @@ static void pb_lwp3device_connect(const char *name, mp_int_t timeout, lwp3_hub_k
213222 options |= PBDRV_BLUETOOTH_PERIPHERAL_OPTIONS_PAIR ;
214223 }
215224
225+ mp_int_t timeout = timeout_in == mp_const_none ? -1 : pb_obj_get_positive_int (timeout_in );
226+
216227 pbdrv_bluetooth_peripheral_scan_and_connect (& lwp3device -> task ,
217228 lwp3_advertisement_matches ,
218229 lwp3_advertisement_response_matches ,
@@ -397,9 +408,7 @@ static mp_obj_t pb_type_pupdevices_Remote_make_new(const mp_obj_type_t *type, si
397408
398409 pb_type_pupdevices_Remote_obj_t * self = mp_obj_malloc (pb_type_pupdevices_Remote_obj_t , type );
399410
400- const char * name = name_in == mp_const_none ? NULL : mp_obj_str_get_str (name_in );
401- mp_int_t timeout = timeout_in == mp_const_none ? -1 : pb_obj_get_positive_int (timeout_in );
402- pb_lwp3device_connect (name , timeout , LWP3_HUB_KIND_HANDSET , handle_remote_notification , false);
411+ pb_lwp3device_connect (name_in , timeout_in , LWP3_HUB_KIND_HANDSET , handle_remote_notification , false);
403412 pb_lwp3device_configure_remote ();
404413
405414 self -> buttons = pb_type_Keypad_obj_new (pb_type_remote_button_pressed );
@@ -507,8 +516,6 @@ static mp_obj_t pb_type_iodevices_LWP3Device_make_new(const mp_obj_type_t *type,
507516 mp_obj_base_t * obj = mp_obj_malloc (mp_obj_base_t , type );
508517 pb_lwp3device_t * self = & pb_lwp3device_singleton ;
509518
510- const char * name = name_in == mp_const_none ? NULL : mp_obj_str_get_str (name_in );
511- mp_int_t timeout = timeout_in == mp_const_none ? -1 : pb_obj_get_positive_int (timeout_in );
512519 uint8_t hub_kind = pb_obj_get_positive_int (hub_kind_in );
513520 bool pair = mp_obj_is_true (pair_in );
514521
@@ -523,7 +530,7 @@ static mp_obj_t pb_type_iodevices_LWP3Device_make_new(const mp_obj_type_t *type,
523530 self -> noti_idx_read = 0 ;
524531 self -> noti_idx_write = 0 ;
525532
526- pb_lwp3device_connect (name , timeout , hub_kind , handle_lwp3_generic_notification , pair );
533+ pb_lwp3device_connect (name_in , timeout_in , hub_kind , handle_lwp3_generic_notification , pair );
527534
528535 return MP_OBJ_FROM_PTR (obj );
529536}
0 commit comments