@@ -525,18 +525,33 @@ STATIC uint8_t _process_mkdir(const uint8_t *raw_buf, size_t command_len) {
525
525
return ANY_COMMAND ;
526
526
}
527
527
528
+ STATIC void send_listdir_entry_header (const struct listdir_entry * entry , mp_int_t max_packet_size ) {
529
+ mp_int_t response_size = sizeof (struct listdir_entry );
530
+ if (max_packet_size >= response_size ) {
531
+ common_hal_bleio_packet_buffer_write (& _transfer_packet_buffer , (const uint8_t * )entry , response_size , NULL , 0 );
532
+ return ;
533
+ }
534
+ // Split into 16 + 12 size packets to fit into 20 byte minimum packet size.
535
+ common_hal_bleio_packet_buffer_write (& _transfer_packet_buffer , (const uint8_t * )entry , 16 , NULL , 0 );
536
+ common_hal_bleio_packet_buffer_write (& _transfer_packet_buffer , ((const uint8_t * )entry ) + 16 , response_size - 16 , NULL , 0 );
537
+ }
538
+
528
539
STATIC uint8_t _process_listdir (uint8_t * raw_buf , size_t command_len ) {
529
540
const struct listdir_command * command = (struct listdir_command * )raw_buf ;
530
541
struct listdir_entry * entry = (struct listdir_entry * )raw_buf ;
531
542
size_t header_size = sizeof (struct listdir_command );
532
- size_t response_size = sizeof (struct listdir_entry );
543
+ mp_int_t max_packet_size = common_hal_bleio_packet_buffer_get_outgoing_packet_length (& _transfer_packet_buffer );
544
+ if (max_packet_size < 0 ) {
545
+ // -1 means we're disconnected
546
+ return ANY_COMMAND ;
547
+ }
533
548
// We reuse the command buffer so that we can produce long packets without
534
549
// making the stack large.
535
550
if (command -> path_length > (COMMAND_SIZE - header_size - 1 )) { // -1 for the null we'll write
536
551
// TODO: throw away any more packets of path.
537
552
entry -> command = LISTDIR_ENTRY ;
538
553
entry -> status = STATUS_ERROR ;
539
- common_hal_bleio_packet_buffer_write ( & _transfer_packet_buffer , ( const uint8_t * ) entry , response_size , NULL , 0 );
554
+ send_listdir_entry_header ( entry , max_packet_size );
540
555
return ANY_COMMAND ;
541
556
}
542
557
// We need to receive another packet to have the full path.
@@ -560,7 +575,7 @@ STATIC uint8_t _process_listdir(uint8_t *raw_buf, size_t command_len) {
560
575
561
576
if (res != FR_OK ) {
562
577
entry -> status = STATUS_ERROR_NO_FILE ;
563
- common_hal_bleio_packet_buffer_write ( & _transfer_packet_buffer , ( const uint8_t * ) entry , response_size , NULL , 0 );
578
+ send_listdir_entry_header ( entry , max_packet_size );
564
579
return ANY_COMMAND ;
565
580
}
566
581
FILINFO file_info ;
@@ -594,7 +609,7 @@ STATIC uint8_t _process_listdir(uint8_t *raw_buf, size_t command_len) {
594
609
595
610
size_t name_length = strlen (file_info .fname );
596
611
entry -> path_length = name_length ;
597
- common_hal_bleio_packet_buffer_write ( & _transfer_packet_buffer , ( const uint8_t * ) entry , response_size , NULL , 0 );
612
+ send_listdir_entry_header ( entry , max_packet_size );
598
613
size_t fn_offset = 0 ;
599
614
while (fn_offset < name_length ) {
600
615
size_t fn_size = MIN (name_length - fn_offset , 4 );
@@ -607,7 +622,7 @@ STATIC uint8_t _process_listdir(uint8_t *raw_buf, size_t command_len) {
607
622
entry -> entry_number = entry -> entry_count ;
608
623
entry -> flags = 0 ;
609
624
entry -> file_size = 0 ;
610
- common_hal_bleio_packet_buffer_write ( & _transfer_packet_buffer , ( const uint8_t * ) entry , response_size , NULL , 0 );
625
+ send_listdir_entry_header ( entry , max_packet_size );
611
626
return ANY_COMMAND ;
612
627
}
613
628
0 commit comments