Skip to content

Commit 5bb4761

Browse files
committed
Encapsulate pending_fn and extra_data_handler_fn
Define 2 macros to encapsulate setting of pending_fn and extra_data_handler_fn. Also adding comments for both macros for clarity
1 parent 7ed9888 commit 5bb4761

File tree

1 file changed

+44
-32
lines changed

1 file changed

+44
-32
lines changed

subsys/usb/device_next/class/usbd_mtp_class.c

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,18 @@ LOG_MODULE_REGISTER(usb_mtp_impl, CONFIG_USBD_MTP_LOG_LEVEL);
133133
IF_ENABLED(DT_PROP(node_id, mtp_enabled), \
134134
({.mountpoint = DT_PROP(node_id, mount_point), .files_count = 1, .read_only = DT_PROP(node_id, read_only)},))
135135

136+
/* Used by MTP commands which reply to host over multiple packets
137+
* like GetObject (Sending file to host) or a command sending its result
138+
* in a separate packet
139+
*/
140+
#define DEV_TO_HOST_SET_PENDING_PACKET(ctx, fn) ctx->pending_fn = fn
141+
142+
/* Used by MTP commands which receive data from host over multiple packets
143+
* like SendObject (Receiving file from host) or a command receiving all required
144+
* info over multiple packets like SendObjectInfo
145+
*/
146+
#define HOST_TO_DEV_SET_CONT_DATA_HANDLER(ctx, fn) ctx->extra_data_fn = fn
147+
136148
/* Types */
137149
enum mtp_container_type {
138150
MTP_CONTAINER_UNDEFINED = 0x00,
@@ -422,6 +434,19 @@ const char *mtp_code_to_string(uint16_t code)
422434
return str;
423435
}
424436
#endif
437+
int mtp_get_pending_packet(struct mtp_context* ctx, struct net_buf *buf)
438+
{
439+
if (ctx->pending_fn) {
440+
return ctx->pending_fn(ctx, buf);
441+
} else {
442+
return -EINVAL;
443+
}
444+
}
445+
446+
bool mtp_packet_pending(struct mtp_context* ctx)
447+
{
448+
return (ctx->pending_fn != NULL);
449+
}
425450

426451
static void net_buf_add_utf16le(struct net_buf *buf, const char *str)
427452
{
@@ -443,20 +468,6 @@ static void net_buf_pull_utf16le(struct net_buf *buf, char *strbuf, size_t len)
443468
}
444469
}
445470

446-
int mtp_get_pending_packet(struct mtp_context* ctx, struct net_buf *buf)
447-
{
448-
if (ctx->pending_fn) {
449-
return ctx->pending_fn(ctx, buf);
450-
} else {
451-
return -EINVAL;
452-
}
453-
}
454-
455-
bool mtp_packet_pending(struct mtp_context* ctx)
456-
{
457-
return (ctx->pending_fn != NULL);
458-
}
459-
460471
static void data_header_push(struct net_buf *buf, struct mtp_container *mtp_command, uint32_t data_len)
461472
{
462473
/* DATA Block Header */
@@ -627,7 +638,7 @@ MTP_CMD_HANDLER(MTP_OP_GET_DEVICE_INFO)
627638
/* Add the Packet Header */
628639
data_header_push(buf, mtp_command, buf->len);
629640

630-
ctx->pending_fn = mtp_send_confirmation;
641+
DEV_TO_HOST_SET_PENDING_PACKET(ctx, mtp_send_confirmation);
631642
}
632643

633644
MTP_CMD_HANDLER(MTP_OP_OPEN_SESSION)
@@ -712,7 +723,7 @@ MTP_CMD_HANDLER(MTP_OP_GET_STORAGE_INFO)
712723
/* Add the Packet Header */
713724
data_header_push(buf, mtp_command, buf->len);
714725

715-
ctx->pending_fn = mtp_send_confirmation;
726+
DEV_TO_HOST_SET_PENDING_PACKET(ctx, mtp_send_confirmation);
716727
}
717728

718729
MTP_CMD_HANDLER(MTP_OP_GET_STORAGE_IDS)
@@ -727,7 +738,7 @@ MTP_CMD_HANDLER(MTP_OP_GET_STORAGE_IDS)
727738
/* Add the Packet Header */
728739
data_header_push(buf, mtp_command, buf->len);
729740

730-
ctx->pending_fn = mtp_send_confirmation;
741+
DEV_TO_HOST_SET_PENDING_PACKET(ctx, mtp_send_confirmation);
731742
}
732743

733744
MTP_CMD_HANDLER(MTP_OP_GET_OBJECT_HANDLES)
@@ -755,7 +766,7 @@ MTP_CMD_HANDLER(MTP_OP_GET_OBJECT_HANDLES)
755766
/* Add the Packet Header */
756767
data_header_push(buf, mtp_command, buf->len);
757768

758-
ctx->pending_fn = mtp_send_confirmation;
769+
DEV_TO_HOST_SET_PENDING_PACKET(ctx, mtp_send_confirmation);
759770
}
760771

761772
MTP_CMD_HANDLER(MTP_OP_GET_OBJECT_INFO)
@@ -827,7 +838,7 @@ MTP_CMD_HANDLER(MTP_OP_GET_OBJECT_INFO)
827838
/* Add the Packet Header */
828839
data_header_push(buf, mtp_command, buf->len);
829840

830-
ctx->pending_fn = mtp_send_confirmation;
841+
DEV_TO_HOST_SET_PENDING_PACKET(ctx, mtp_send_confirmation);
831842
}
832843

833844
static int continue_get_object(struct mtp_context* ctx, struct net_buf *buf)
@@ -858,10 +869,10 @@ static int continue_get_object(struct mtp_context* ctx, struct net_buf *buf)
858869
LOG_DBG("Done (%u), CONFIRMING", read);
859870
fs_close(&ctx->filestate.file);
860871
memset(&ctx->filestate, 0x00, sizeof(ctx->filestate));
861-
ctx->pending_fn = mtp_send_confirmation;
872+
DEV_TO_HOST_SET_PENDING_PACKET(ctx, mtp_send_confirmation);
862873
} else {
863874
LOG_DBG("Continue (%u) Next", read);
864-
ctx->pending_fn = continue_get_object;
875+
DEV_TO_HOST_SET_PENDING_PACKET(ctx, continue_get_object);
865876
}
866877
} else {
867878
LOG_ERR("Shouldn't happen");
@@ -927,7 +938,7 @@ MTP_CMD_HANDLER(MTP_OP_GET_OBJECT)
927938
ctx->cmd = MTP_OP_GET_OBJECT;
928939
ctx->filestate.total_size = filesize;
929940
ctx->filestate.transferred = read;
930-
ctx->pending_fn = continue_get_object;
941+
DEV_TO_HOST_SET_PENDING_PACKET(ctx, continue_get_object);
931942
} else {
932943
int read = fs_read(&ctx->filestate.file, ctx->filebuf, filesize);
933944

@@ -938,7 +949,7 @@ MTP_CMD_HANDLER(MTP_OP_GET_OBJECT)
938949

939950
fs_close(&ctx->filestate.file);
940951
memset(&ctx->filestate, 0x00, sizeof(ctx->filestate));
941-
ctx->pending_fn = mtp_send_confirmation;
952+
DEV_TO_HOST_SET_PENDING_PACKET(ctx, mtp_send_confirmation);
942953
}
943954
}
944955

@@ -1118,12 +1129,12 @@ static int continue_send_object(struct mtp_context* ctx, struct net_buf *buf_in)
11181129
LOG_DBG("Sending Confirmation after reciving data (Total len: %u)",
11191130
ctx->filestate.transferred);
11201131

1121-
ctx->extra_data_fn = NULL;
1132+
HOST_TO_DEV_SET_CONT_DATA_HANDLER(ctx, NULL);
11221133
memset(&ctx->filestate, 0x00, sizeof(ctx->filestate));
1123-
ctx->pending_fn = mtp_send_confirmation;
1134+
DEV_TO_HOST_SET_PENDING_PACKET(ctx, mtp_send_confirmation);
11241135
return 1;
11251136
} else {
1126-
ctx->extra_data_fn = continue_send_object;
1137+
HOST_TO_DEV_SET_CONT_DATA_HANDLER(ctx, continue_send_object);
11271138
return 0;
11281139
}
11291140
}
@@ -1149,10 +1160,10 @@ MTP_CMD_HANDLER(MTP_OP_SEND_OBJECT)
11491160
ctx->filestate.transferred);
11501161

11511162
memset(&ctx->filestate, 0x00, sizeof(ctx->filestate));
1152-
ctx->extra_data_fn = NULL;
1163+
HOST_TO_DEV_SET_CONT_DATA_HANDLER(ctx, NULL);
11531164
mtp_send_confirmation(ctx, buf);
11541165
} else {
1155-
ctx->extra_data_fn = continue_send_object;
1166+
HOST_TO_DEV_SET_CONT_DATA_HANDLER(ctx, continue_send_object);
11561167
}
11571168
}
11581169
}
@@ -1205,7 +1216,7 @@ MTP_CMD_HANDLER(MTP_OP_GET_OBJECT_REFERENCES)
12051216

12061217
data_header_push(buf, mtp_command, buf->len);
12071218

1208-
ctx->pending_fn = mtp_send_confirmation;
1219+
DEV_TO_HOST_SET_PENDING_PACKET(ctx, mtp_send_confirmation);
12091220
}
12101221

12111222
/*
@@ -1240,6 +1251,7 @@ int mtp_commands_handler(struct mtp_context* ctx, struct net_buf *buf_in, struct
12401251
}
12411252
}
12421253

1254+
ctx->cmd = mtp_command->hdr.code;
12431255
LOG_DBG(GREEN "[%s]" RESET, mtp_code_to_string(mtp_command->hdr.code));
12441256

12451257
switch (mtp_command->hdr.code) {
@@ -1330,8 +1342,8 @@ int mtp_control_handler(struct mtp_context* ctx, uint8_t request, struct net_buf
13301342
memset(ctx->filebuf, 0x00, sizeof(ctx->filebuf));
13311343
memset(&ctx->filestate, 0x00, sizeof(ctx->filestate));
13321344
}
1333-
ctx->pending_fn = NULL;
1334-
ctx->extra_data_fn = NULL;
1345+
DEV_TO_HOST_SET_PENDING_PACKET(ctx, NULL);
1346+
HOST_TO_DEV_SET_CONT_DATA_HANDLER(ctx, NULL);
13351347

13361348
canceled = true;
13371349
break;
@@ -1367,7 +1379,7 @@ static int mtp_send_confirmation(struct mtp_context* ctx, struct net_buf *buf)
13671379
LOG_ERR("Failed to send MTP confirmation");
13681380
}
13691381

1370-
ctx->pending_fn = NULL;
1382+
DEV_TO_HOST_SET_PENDING_PACKET(ctx, NULL);
13711383
return 0;
13721384
}
13731385

0 commit comments

Comments
 (0)