Skip to content

Commit 16ae874

Browse files
committed
Polishing 3
Do some polishing
1 parent 00a530f commit 16ae874

File tree

3 files changed

+31
-16
lines changed

3 files changed

+31
-16
lines changed

subsys/usb/device_next/class/usbd_mtp.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ static int usbd_mtp_control_to_host(struct usbd_class_data *c_data,
132132

133133
//FIXME: Handle Cancel transaction properly
134134
// Reply to host with the status
135-
mtp_control_handler(mtp_ctx, setup->bRequest, buf);
135+
mtp_control_to_host(mtp_ctx, setup->bRequest, buf);
136136

137137
return 0;
138138
}
@@ -144,12 +144,12 @@ static int usbd_mtp_control_to_dev(struct usbd_class_data *c_data,
144144
struct mtp_data *data = usbd_class_get_private(c_data);
145145
struct mtp_context *mtp_ctx = &data->mtp_ctx;
146146

147-
LOG_WRN("%s: Class request 0x%x (Recipient: %x)", __func__,
147+
LOG_DBG("%s: Class request 0x%x (Recipient: %x)", __func__,
148148
setup->bRequest, setup->RequestType.recipient);
149149

150150
//FIXME: Handle Cancel transaction properly
151151
// here transaction is canceled without any reply back
152-
mtp_control_handler(mtp_ctx, setup->bRequest, buf);
152+
mtp_control_to_dev(mtp_ctx, setup->bRequest, buf);
153153

154154
return 0;
155155
}

subsys/usb/device_next/class/usbd_mtp_class.c

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -934,7 +934,6 @@ MTP_CMD_HANDLER(MTP_OP_GET_OBJECT)
934934
}
935935
net_buf_add_mem(buf, ctx->filebuf, read);
936936

937-
ctx->cmd = MTP_OP_GET_OBJECT;
938937
ctx->filestate.total_size = filesize;
939938
ctx->filestate.transferred = read;
940939
DEV_TO_HOST_SET_PENDING_PACKET(ctx, continue_get_object);
@@ -986,7 +985,6 @@ MTP_CMD_HANDLER(MTP_OP_SEND_OBJECT_INFO)
986985
fs_obj->storage_id = dest_storage_id;
987986

988987
LOG_DBG("New ObjID: 0x%08x", fs_obj->ID);
989-
ctx->cmd = MTP_OP_SEND_OBJECT;
990988
} else {
991989
LOG_ERR("No file handle avaiable %u",
992990
storage[dest_storage_id].files_count);
@@ -1123,6 +1121,7 @@ static int continue_send_object(struct mtp_context* ctx, struct net_buf *buf_in)
11231121
fs_write(&ctx->filestate.file, buf_in->data, buf_in->len);
11241122
LOG_DBG("EXTRA: Data len: %u out of %u", ctx->filestate.transferred,
11251123
ctx->filestate.total_size);
1124+
11261125
if (ctx->filestate.transferred >= ctx->filestate.total_size) {
11271126
fs_close(&ctx->filestate.file);
11281127
LOG_DBG("Sending Confirmation after reciving data (Total len: %u)",
@@ -1241,16 +1240,15 @@ int mtp_commands_handler(struct mtp_context* ctx, struct net_buf *buf_in, struct
12411240
if (ret >= 0) {
12421241
if (ret == 1) {
12431242
ctx->pending_fn(ctx, buf_resp);
1244-
return buf_resp->len;
12451243
}
1246-
return 0;
1244+
return buf_resp->len;
12471245
} else {
12481246
LOG_ERR("Failed to handle extra data");
12491247
return -EINVAL;
12501248
}
12511249
}
12521250

1253-
ctx->cmd = mtp_command->hdr.code;
1251+
ctx->op_code = mtp_command->hdr.code;
12541252
LOG_DBG(GREEN "[%s]" RESET, mtp_code_to_string(mtp_command->hdr.code));
12551253

12561254
switch (mtp_command->hdr.code) {
@@ -1306,15 +1304,14 @@ int mtp_commands_handler(struct mtp_context* ctx, struct net_buf *buf_in, struct
13061304
return buf_resp->len;
13071305
}
13081306

1309-
int mtp_control_handler(struct mtp_context* ctx, uint8_t request, struct net_buf *buf)
1307+
int mtp_control_to_host(struct mtp_context* ctx, uint8_t request, struct net_buf *const buf)
13101308
{
13111309
struct mtp_device_status_t dev_status;
1312-
static bool canceled = false;
13131310
switch (request)
13141311
{
13151312
case MTP_REQUEST_GET_DEVICE_STATUS:
13161313
LOG_WRN("MTP_REQUEST_GET_DEVICE_STATUS");
1317-
if (canceled){
1314+
if (ctx->op_canceled){
13181315
dev_status.len = 6;
13191316
dev_status.code = MTP_RESP_TRANSACTION_CANCELLED;
13201317
dev_status.epIn = 0x81;
@@ -1325,16 +1322,29 @@ int mtp_control_handler(struct mtp_context* ctx, uint8_t request, struct net_buf
13251322
dev_status.code = MTP_RESP_OK;
13261323
net_buf_add_mem(buf, &dev_status, dev_status.len);
13271324
}
1328-
canceled = false;
1325+
1326+
ctx->op_canceled = false;
1327+
break;
1328+
default:
1329+
LOG_ERR("Unknown Host request 0x%x!", request);
13291330
break;
13301331

1332+
}
1333+
1334+
return 0;
1335+
}
1336+
1337+
int mtp_control_to_dev(struct mtp_context* ctx, uint8_t request, const struct net_buf *const buf)
1338+
{
1339+
switch (request)
1340+
{
13311341
case MTP_REQUEST_CANCEL:
13321342
LOG_WRN("Closing incomplete file %s", ctx->filestate.filepath);
13331343
if (strlen(ctx->filestate.filepath) > 0) {
13341344
fs_close(&ctx->filestate.file);
13351345

13361346
/* Delete the opened file only when downloading from Host */
1337-
if (ctx->cmd == MTP_OP_SEND_OBJECT) {
1347+
if (ctx->op_code == MTP_OP_SEND_OBJECT) {
13381348
fs_unlink(ctx->filestate.filepath);
13391349
}
13401350

@@ -1344,7 +1354,7 @@ int mtp_control_handler(struct mtp_context* ctx, uint8_t request, struct net_buf
13441354
DEV_TO_HOST_SET_PENDING_PACKET(ctx, NULL);
13451355
HOST_TO_DEV_SET_CONT_DATA_HANDLER(ctx, NULL);
13461356

1347-
canceled = true;
1357+
ctx->op_canceled = true;
13481358
break;
13491359

13501360
case MTP_REQUEST_DEVICE_RESET:
@@ -1353,7 +1363,7 @@ int mtp_control_handler(struct mtp_context* ctx, uint8_t request, struct net_buf
13531363
break;
13541364

13551365
default:
1356-
LOG_ERR("Unknown request 0x%x!", request);
1366+
LOG_ERR("Unknown Dev request 0x%x!", request);
13571367
break;
13581368
}
13591369

@@ -1389,6 +1399,10 @@ static void mtp_reset(struct mtp_context* ctx)
13891399
storage[i].files_count = 0;
13901400
}
13911401
ctx->session_opened = false;
1402+
ctx->op_code = 0;
1403+
ctx->op_canceled = false;
1404+
HOST_TO_DEV_SET_CONT_DATA_HANDLER(ctx, NULL);
1405+
DEV_TO_HOST_SET_PENDING_PACKET(ctx, NULL);
13921406
}
13931407

13941408
int mtp_init(struct mtp_context* ctx, const char *manufacturer, const char *model, const char *device_version,

subsys/usb/device_next/class/usbd_mtp_class.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313

1414
struct mtp_context {
1515
bool session_opened;
16+
bool op_canceled;
17+
uint16_t op_code;
1618
uint16_t max_packet_size;
1719
uint8_t filebuf[512]; /* Use USB HS MPS */
18-
uint16_t cmd;
1920

2021
bool more_data_needed;
2122
int (*extra_data_fn)(struct mtp_context* ctx, struct net_buf *buf_in);

0 commit comments

Comments
 (0)