@@ -118,7 +118,6 @@ LOG_MODULE_REGISTER(usb_mtp_impl, CONFIG_USBD_MTP_LOG_LEVEL);
118
118
/* Helpers */
119
119
#define INTERNAL_STORAGE_ID (id ) (0x00010000 + id)
120
120
#define REMOVABLE_STORAGE_ID (id ) (0x00020000 + id)
121
- #define MTP_GB (x ) (x * 1ULL * 1024 * 1024 * 1024)
122
121
#define MTP_STR_LEN (str ) (strlen(str) + 1)
123
122
124
123
#define GET_OBJECT_ID (objID ) ((uint8_t)(objID & 0xFF))
@@ -194,6 +193,7 @@ struct fs_object_t {
194
193
uint32_t size ;
195
194
char name [MAX_FILE_NAME ];
196
195
};
196
+
197
197
struct storage_t {
198
198
const char mountpoint [10 ];
199
199
struct fs_object_t filelist [CONFIG_USBD_MTP_MAX_HANDLES ];
@@ -438,16 +438,11 @@ static void net_buf_pull_utf16le(struct net_buf *buf, char *strbuf, size_t len)
438
438
net_buf_pull_u8 (buf );
439
439
}
440
440
}
441
- typedef int (pending_fn_t )(struct mtp_context * ctx , struct net_buf * buf );
442
441
443
442
static int mtp_send_confirmation (struct mtp_context * ctx , struct net_buf * buf );
444
443
/* ================== Pending packet handling ================ */
445
- static void set_pending_packet (struct mtp_context * ctx , pending_fn_t * pend_fn )
446
- {
447
- ctx -> pending_fn = pend_fn ;
448
- }
449
444
450
- int send_pending_packet (struct mtp_context * ctx , struct net_buf * buf )
445
+ int mtp_get_pending_packet (struct mtp_context * ctx , struct net_buf * buf )
451
446
{
452
447
if (ctx -> pending_fn ) {
453
448
return ctx -> pending_fn (ctx , buf );
@@ -462,19 +457,6 @@ bool mtp_packet_pending(struct mtp_context* ctx)
462
457
}
463
458
464
459
/* ===================== Extra Data needed handling =============== */
465
- static void set_needs_more_data (struct mtp_context * ctx , bool more_data )
466
- {
467
- ctx -> more_data_needed = more_data ;
468
- }
469
-
470
- bool mtp_needs_more_data (struct mtp_context * ctx , struct net_buf * buf )
471
- {
472
- bool val = ctx -> more_data_needed ;
473
-
474
- ctx -> more_data_needed = false;
475
- return val ;
476
- }
477
-
478
460
int handle_extra_data (struct mtp_context * ctx , struct net_buf * buf , struct net_buf * buf_recv )
479
461
{
480
462
if (ctx -> extra_data_fn ) {
@@ -655,7 +637,7 @@ MTP_CMD_HANDLER(MTP_OP_GET_DEVICE_INFO)
655
637
/* Add the Packet Header */
656
638
data_header_push (buf , mtp_command , buf -> len );
657
639
658
- set_pending_packet ( ctx , mtp_send_confirmation ) ;
640
+ ctx -> pending_fn = mtp_send_confirmation ;
659
641
}
660
642
661
643
MTP_CMD_HANDLER (MTP_OP_OPEN_SESSION )
@@ -669,14 +651,16 @@ MTP_CMD_HANDLER(MTP_OP_OPEN_SESSION)
669
651
err_code = MTP_RESP_GENERAL_ERROR ;
670
652
break ;
671
653
}
672
- /* TODO: Fail the MTP command if dir_traverse fails */
673
- ctx -> session_opened = true;
674
654
}
675
655
} else {
676
656
LOG_ERR ("Session already open" );
677
657
err_code = MTP_RESP_SESSION_ALREADY_OPEN ;
678
658
}
679
659
660
+ if (err_code == MTP_RESP_OK ) {
661
+ ctx -> session_opened = true;
662
+ }
663
+
680
664
struct mtp_header mtp_response = {.length = sizeof (struct mtp_header ),
681
665
.type = MTP_CONTAINER_RESPONSE ,
682
666
.code = err_code ,
@@ -733,7 +717,7 @@ MTP_CMD_HANDLER(MTP_OP_GET_STORAGE_INFO)
733
717
/* Add the Packet Header */
734
718
data_header_push (buf , mtp_command , buf -> len );
735
719
736
- set_pending_packet ( ctx , mtp_send_confirmation ) ;
720
+ ctx -> pending_fn = mtp_send_confirmation ;
737
721
}
738
722
739
723
MTP_CMD_HANDLER (MTP_OP_GET_STORAGE_IDS )
@@ -748,7 +732,7 @@ MTP_CMD_HANDLER(MTP_OP_GET_STORAGE_IDS)
748
732
/* Add the Packet Header */
749
733
data_header_push (buf , mtp_command , buf -> len );
750
734
751
- set_pending_packet ( ctx , mtp_send_confirmation ) ;
735
+ ctx -> pending_fn = mtp_send_confirmation ;
752
736
}
753
737
754
738
MTP_CMD_HANDLER (MTP_OP_GET_OBJECT_HANDLES )
@@ -776,7 +760,7 @@ MTP_CMD_HANDLER(MTP_OP_GET_OBJECT_HANDLES)
776
760
/* Add the Packet Header */
777
761
data_header_push (buf , mtp_command , buf -> len );
778
762
779
- set_pending_packet ( ctx , mtp_send_confirmation ) ;
763
+ ctx -> pending_fn = mtp_send_confirmation ;
780
764
}
781
765
782
766
MTP_CMD_HANDLER (MTP_OP_GET_OBJECT_INFO )
@@ -848,7 +832,7 @@ MTP_CMD_HANDLER(MTP_OP_GET_OBJECT_INFO)
848
832
/* Add the Packet Header */
849
833
data_header_push (buf , mtp_command , buf -> len );
850
834
851
- set_pending_packet ( ctx , mtp_send_confirmation ) ;
835
+ ctx -> pending_fn = mtp_send_confirmation ;
852
836
}
853
837
854
838
static int continue_get_object (struct mtp_context * ctx , struct net_buf * buf )
@@ -879,10 +863,10 @@ static int continue_get_object(struct mtp_context* ctx, struct net_buf *buf)
879
863
LOG_DBG ("Done (%u), CONFIRMING" , read );
880
864
fs_close (& ctx -> filestate .file );
881
865
memset (& ctx -> filestate , 0x00 , sizeof (ctx -> filestate ));
882
- set_pending_packet ( ctx , mtp_send_confirmation ) ;
866
+ ctx -> pending_fn = mtp_send_confirmation ;
883
867
} else {
884
868
LOG_DBG ("Continue (%u) Next" , read );
885
- set_pending_packet ( ctx , continue_get_object ) ;
869
+ ctx -> pending_fn = continue_get_object ;
886
870
}
887
871
} else {
888
872
LOG_ERR ("shouldn't happen !" );
@@ -947,7 +931,7 @@ MTP_CMD_HANDLER(MTP_OP_GET_OBJECT)
947
931
ctx -> filestate .direction = 0 ;
948
932
ctx -> filestate .total_size = filesize ;
949
933
ctx -> filestate .transferred = read ;
950
- set_pending_packet ( ctx , continue_get_object ) ;
934
+ ctx -> pending_fn = continue_get_object ;
951
935
} else {
952
936
int read = fs_read (& ctx -> filestate .file , ctx -> filebuf , filesize );
953
937
@@ -957,7 +941,7 @@ MTP_CMD_HANDLER(MTP_OP_GET_OBJECT)
957
941
net_buf_add_mem (buf , ctx -> filebuf , read );
958
942
fs_close (& ctx -> filestate .file );
959
943
memset (& ctx -> filestate , 0x00 , sizeof (ctx -> filestate ));
960
- set_pending_packet ( ctx , mtp_send_confirmation ) ;
944
+ ctx -> pending_fn = mtp_send_confirmation ;
961
945
}
962
946
}
963
947
@@ -966,8 +950,8 @@ MTP_CMD_HANDLER(MTP_OP_SEND_OBJECT_INFO)
966
950
int ret = 0 ;
967
951
static struct fs_object_t * fs_obj ;
968
952
969
- /* first packet received from Host contains only, destination storageID and
970
- * Destination ParentID
953
+ /* first packet received from Host contains only:
954
+ * destination storageID and Destination ParentID
971
955
*/
972
956
if (fs_obj == NULL ) {
973
957
LOG_DBG ("\n\t\tDest StorageID: 0x%x"
@@ -995,7 +979,7 @@ MTP_CMD_HANDLER(MTP_OP_SEND_OBJECT_INFO)
995
979
fs_obj -> storage_id = dest_storage_id ;
996
980
997
981
LOG_INF ("New ObjID: 0x%08x" , fs_obj -> ID );
998
- set_needs_more_data ( ctx , true) ;
982
+ ctx -> filestate . direction = 1 ;
999
983
} else {
1000
984
LOG_ERR ("No file handle avaiable %u" ,
1001
985
storage [dest_storage_id ].files_count );
@@ -1067,13 +1051,13 @@ MTP_CMD_HANDLER(MTP_OP_SEND_OBJECT_INFO)
1067
1051
filename );
1068
1052
}
1069
1053
1070
- LOG_DBG ("\noFormat: %x, size: %x , parent: %x\n " , ObjectFormat , fs_obj -> size ,
1054
+ LOG_DBG ("\noFormat: %x, size: %d , parent: %x" , ObjectFormat , fs_obj -> size ,
1071
1055
ParentObject );
1072
- LOG_DBG ("mnt: %s\n " , storage [fs_obj -> storage_id ].mountpoint );
1073
- LOG_DBG ("fname: %s\n " , filename );
1074
- LOG_DBG ("path: %s ID:%x\n " , filepath , fs_obj -> ID );
1075
- LOG_DBG ("parentID: %u\n " , fs_obj -> parent_id );
1076
- LOG_DBG ("parentPath:%s\n " ,
1056
+ LOG_DBG ("mnt: %s" , storage [fs_obj -> storage_id ].mountpoint );
1057
+ LOG_DBG ("fname: %s" , filename );
1058
+ LOG_DBG ("path: %s ID:%x" , filepath , fs_obj -> ID );
1059
+ LOG_DBG ("parentID: %u" , fs_obj -> parent_id );
1060
+ LOG_DBG ("parentPath:%s" ,
1077
1061
storage [fs_obj -> storage_id ].filelist [fs_obj -> parent_id ].name );
1078
1062
1079
1063
if (fs_obj -> type == FS_DIR_ENTRY_DIR ) {
@@ -1136,7 +1120,6 @@ int extra_data_handler(struct mtp_context* ctx, struct net_buf *buf, struct net_
1136
1120
mtp_send_confirmation (ctx , buf );
1137
1121
} else {
1138
1122
ctx -> extra_data_fn = extra_data_handler ;
1139
- set_needs_more_data (ctx , true);
1140
1123
}
1141
1124
return 0 ;
1142
1125
}
@@ -1146,11 +1129,10 @@ MTP_CMD_HANDLER(MTP_OP_SEND_OBJECT)
1146
1129
if (mtp_command -> hdr .type == MTP_CONTAINER_COMMAND ) {
1147
1130
LOG_INF ("COMMAND RECEIVED len: %u" , payload -> len );
1148
1131
} else if (mtp_command -> hdr .type == MTP_CONTAINER_DATA ) {
1149
- LOG_INF ("DATA RECEIVED len: %u" , payload -> len ); /* SKIP The header */
1150
- net_buf_pull_mem (payload , sizeof (struct mtp_header ));
1132
+ LOG_INF ("DATA RECEIVED len: %u" , payload -> len );
1133
+ net_buf_pull_mem (payload , sizeof (struct mtp_header )); /* SKIP The header */
1151
1134
fs_write (& ctx -> filestate .file , payload -> data , payload -> len );
1152
1135
1153
- ctx -> filestate .direction = 1 ;
1154
1136
ctx -> filestate .chunks_sent ++ ;
1155
1137
ctx -> filestate .transferred += payload -> len ;
1156
1138
LOG_INF ("SEND_OBJECT: Data len: %u out of %u" , ctx -> filestate .transferred ,
@@ -1166,12 +1148,8 @@ MTP_CMD_HANDLER(MTP_OP_SEND_OBJECT)
1166
1148
memset (& ctx -> filestate , 0x00 , sizeof (ctx -> filestate ));
1167
1149
ctx -> extra_data_fn = NULL ;
1168
1150
mtp_send_confirmation (ctx , buf );
1169
- if (buf -> len <= 0 ) {
1170
- LOG_ERR ("Failed to send confirmation" );
1171
- }
1172
1151
} else {
1173
1152
ctx -> extra_data_fn = extra_data_handler ;
1174
- set_needs_more_data (ctx , true);
1175
1153
}
1176
1154
}
1177
1155
}
@@ -1218,7 +1196,7 @@ MTP_CMD_HANDLER(MTP_OP_GET_OBJECT_REFERENCES)
1218
1196
1219
1197
data_header_push (buf , mtp_command , buf -> len );
1220
1198
1221
- set_pending_packet ( ctx , mtp_send_confirmation ) ;
1199
+ ctx -> pending_fn = mtp_send_confirmation ;
1222
1200
}
1223
1201
1224
1202
int mtp_commands_handler (struct mtp_context * ctx , struct net_buf * buf_in , struct net_buf * buf )
@@ -1324,8 +1302,8 @@ int mtp_control_handler(struct mtp_context* ctx, uint8_t request, struct net_buf
1324
1302
memset (ctx -> filebuf , 0x00 , sizeof (ctx -> filebuf ));
1325
1303
memset (& ctx -> filestate , 0x00 , sizeof (ctx -> filestate ));
1326
1304
}
1327
- set_pending_packet ( ctx , NULL ) ;
1328
- set_needs_more_data ( ctx , false) ;
1305
+ ctx -> pending_fn = NULL ;
1306
+ ctx -> extra_data_fn = NULL ;
1329
1307
1330
1308
canceled = true;
1331
1309
//usbd_ep_set_halt(usbd_class_get_ctx(c_data), ep);
@@ -1351,16 +1329,19 @@ static int mtp_send_confirmation(struct mtp_context* ctx, struct net_buf *buf)
1351
1329
LOG_ERR ("%s: Null Buffer!" , __func__ );
1352
1330
return - EINVAL ;
1353
1331
}
1354
-
1332
+ LOG_INF ( "Sending Confirmation" );
1355
1333
struct mtp_container * mtp_command = (struct mtp_container * )buf -> data ;
1356
1334
struct mtp_header mtp_response = {.length = sizeof (struct mtp_header ),
1357
1335
.type = MTP_CONTAINER_RESPONSE ,
1358
1336
.code = MTP_RESP_OK ,
1359
1337
.transaction_id = mtp_command -> hdr .transaction_id };
1360
1338
1361
1339
net_buf_add_mem (buf , & mtp_response , sizeof (struct mtp_header ));
1362
- set_pending_packet (ctx , NULL );
1340
+ if (buf -> len <= 0 ) {
1341
+ LOG_ERR ("Failed to send confirmation" );
1342
+ }
1363
1343
1344
+ ctx -> pending_fn = NULL ;
1364
1345
return 0 ;
1365
1346
}
1366
1347
0 commit comments