Skip to content

Commit

Permalink
Fix data packet reading bugs
Browse files Browse the repository at this point in the history
wait_for_response would wait too long
wrong value put into ptp_send_bulk_packets
  • Loading branch information
petabyt committed Oct 5, 2023
1 parent 5144f9d commit 1cb29ac
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 26 deletions.
46 changes: 21 additions & 25 deletions src/backend.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ int ptpip_receive_bulk_packets(struct PtpRuntime *r) {
rc = ptpip_read_packet(r, pk2_of);
h = (struct PtpIpHeader *)(r->data + pk2_of);
if (h->type != PTPIP_COMMAND_RESPONSE) {
printf("%d\n", h->length);
ptp_verbose_log("Non response packet after data end packet (%d)\n", h->type);
return PTP_IO_ERR;
}
Expand Down Expand Up @@ -138,15 +137,18 @@ int ptpusb_read_packet(struct PtpRuntime *r, int of) {

r->wait_for_response--;

if (rc > 0) break;

if (r->wait_for_response) {
ptp_verbose_log("Trying again...");
CAMLIB_SLEEP(1000);
}
}

r->wait_for_response = 1;

if (rc < 0) {
ptp_verbose_log("USB Read error: %d\n", rc);
ptp_verbose_log("Failed to read packet length: %d\n", rc);
return PTP_IO_ERR;
}

Expand Down Expand Up @@ -218,35 +220,29 @@ int ptpusb_read_packet(struct PtpRuntime *r, int of) {

int ptpusb_receive_bulk_packets(struct PtpRuntime *r) {
int read = 0;
while (1) {
int rc = ptpusb_read_packet(r, read);
if (rc < 0) {
return rc;
}
int rc = ptpusb_read_packet(r, read);
if (rc < 0) return rc;

struct PtpBulkContainer *c = (struct PtpBulkContainer *)(r->data + read);
if (c->length < rc) {
ptp_verbose_log("Already read enough bytes\n");
return read;
}
struct PtpBulkContainer *c = (struct PtpBulkContainer *)(r->data + read);
if (c->length < rc) {
ptp_verbose_log("Already read enough bytes\n");
return read;
}

read += rc;
read += rc;

// Handle data phase
if (c->type == PTP_PACKET_TYPE_DATA) {
rc = ptpusb_read_packet(r, read);
if (rc < 0) {
return rc;
}
// Handle data phase
if (c->type == PTP_PACKET_TYPE_DATA) {
rc = ptpusb_read_packet(r, read);
if (rc < 0) return rc;

read += rc;
}
read += rc;
}

ptp_verbose_log("receive_bulk_packets: Read %d bytes\n", read);
ptp_verbose_log("receive_bulk_packets: Return code: 0x%X\n", ptp_get_return_code(r));
ptp_verbose_log("receive_bulk_packets: Read %d bytes\n", read);
ptp_verbose_log("receive_bulk_packets: Return code: 0x%X\n", ptp_get_return_code(r));

return read;
}
return read;
}

int ptp_receive_bulk_packets(struct PtpRuntime *r) {
Expand Down
2 changes: 1 addition & 1 deletion src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ int ptp_generic_send_data(struct PtpRuntime *r, struct PtpCommand *cmd, void *da
} else {
// Single data packet
plength = ptp_new_data_packet(r, cmd, data, length);
if (ptp_send_bulk_packets(r, plength) != plength + length) {
if (ptp_send_bulk_packets(r, plength) != plength) {
ptp_mutex_unlock(r);
return PTP_IO_ERR;
}
Expand Down

0 comments on commit 1cb29ac

Please sign in to comment.