Skip to content

Commit

Permalink
Add ptp_error_log, add some things to stuff.c for CLI usage
Browse files Browse the repository at this point in the history
  • Loading branch information
petabyt committed Dec 20, 2024
1 parent 5d30fac commit 01e5362
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/camlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

// Logging+panic mechanism, define it yourself or link in log.c
void ptp_verbose_log(char *fmt, ...);
void ptp_error_log(char *fmt, ...);
__attribute__ ((noreturn)) void ptp_panic(char *fmt, ...);

// 1mb default buffer size
Expand Down
10 changes: 10 additions & 0 deletions src/cl_stuff.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef CL_STUFF_H
#define CL_STUFF_H

int ptp_list_devices(void);

struct PtpRuntime *ptp_connect_from_id(int id);

int ptp_dump_device(int dev_id);

#endif
1 change: 1 addition & 0 deletions src/lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ void ptp_mutex_unlock(struct PtpRuntime *r) {

void ptp_close(struct PtpRuntime *r) {
free(r->data);
// TODO: This should free the entire structure!
}

void ptp_mutex_unlock_thread(struct PtpRuntime *r) {
Expand Down
8 changes: 8 additions & 0 deletions src/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ void ptp_verbose_log(char *fmt, ...) {
}
}

__attribute__((weak))
void ptp_error_log(char *fmt, ...) {
va_list args;
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
}

__attribute__ ((noreturn))
__attribute__((weak))
void ptp_panic(char *fmt, ...) {
Expand Down
22 changes: 14 additions & 8 deletions src/stuff.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,25 @@ int ptp_list_devices(void) {
return 0;
}

int i = 0;
for (; list != NULL; list = list->next) {
printf("product id: %04x\n", list->product_id);
printf("vendor id: %04x\n", list->vendor_id);
printf("Vendor friendly name: '%s'\n", list->manufacturer);
printf("Model friendly name: '%s'\n", list->name);
printf("Device #%d:\n", i);
printf(" product id: %04x\n", list->product_id);
printf(" vendor id: %04x\n", list->vendor_id);
printf(" Vendor friendly name: '%s'\n", list->manufacturer);
printf(" Model friendly name: '%s'\n", list->name);
i++;
}

ptp_close(r);

return 0;
}

static struct PtpRuntime *ptp_connect_id(int id) {
struct PtpRuntime *ptp_connect_from_id(int id) {
if (id == -1) {
ptp_panic("%s Bug: -1", __func__);
}
struct PtpRuntime *r = ptp_new(PTP_USB);
struct PtpDeviceEntry *list = ptpusb_device_list(r);

Expand All @@ -50,14 +56,14 @@ static struct PtpRuntime *ptp_connect_id(int id) {
return NULL;
}

int ptp_dump_device(void) {
struct PtpRuntime *r = ptp_connect_id(0);
int ptp_dump_device(int dev_id) {
struct PtpRuntime *r = ptp_connect_from_id(dev_id);

struct PtpDeviceInfo di;
char buffer[4096];
ptp_get_device_info(r, &di);
ptp_device_info_json(&di, buffer, sizeof(buffer));
printf("%s\n", (char*)buffer);
printf("%s\n", (char *)buffer);

return 0;
}
22 changes: 11 additions & 11 deletions src/transport.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ int ptp_send_packet(struct PtpRuntime *r, int length) {
sent += rc;

if (sent > length) {
ptp_verbose_log("%s: Sent too many bytes: %d\n", __func__, sent);
return sent;
ptp_panic("BUG: Sent too many bytes (?)");
} else if (sent == length) {
ptp_verbose_log("%s: Sent %d/%d bytes\n", __func__, sent, length);
return sent;
Expand All @@ -52,20 +51,20 @@ int ptpip_read_packet(struct PtpRuntime *r, int of) {
if (rc > 0) break;

if (r->wait_for_response) {
ptp_verbose_log("Trying again...\n");
ptp_error_log("Trying again...\n");
CAMLIB_SLEEP(CAMLIB_WAIT_MS);
}
}

r->wait_for_response = r->response_wait_default;

if (rc < 0) {
ptp_verbose_log("Failed to read packet length: %d\n", rc);
ptp_error_log("Failed to read packet length: %d\n", rc);
return PTP_COMMAND_IGNORED;
}

if (rc < 4) {
ptp_verbose_log("Failed to read at least packet length: %d\n", rc);
ptp_error_log("Failed to read at least packet length: %d\n", rc);
return PTP_IO_ERR;
}

Expand Down Expand Up @@ -113,9 +112,10 @@ int ptpip_receive_bulk_packets(struct PtpRuntime *r) {

if (h->type == PTPIP_DATA_PACKET_START) {
rc = ptpip_read_packet(r, pk1_of);
if (rc < 0) return rc;
h = (struct PtpIpHeader *)(r->data + pk1_of);
if (h->type != PTPIP_DATA_PACKET_END) {
ptp_verbose_log("Didn't receive an END DATA packet (%d)\n", h->type);
ptp_error_log("Didn't receive an END DATA packet (%d)\n", h->type);
return PTP_IO_ERR;
}

Expand All @@ -125,13 +125,13 @@ int ptpip_receive_bulk_packets(struct PtpRuntime *r) {
if (rc < 0) return rc;
h = (struct PtpIpHeader *)(r->data + pk2_of);
if (h->type != PTPIP_COMMAND_RESPONSE) {
ptp_verbose_log("Non response packet after data end packet (%d)\n", h->type);
ptp_error_log("Non response packet after data end packet (%d)\n", h->type);
return PTP_IO_ERR;
}
} else if (h->type == PTPIP_COMMAND_RESPONSE) {
ptp_verbose_log("Received response packet\n");
} else {
ptp_verbose_log("Unexpected packet: %X\n", h->type);
ptp_error_log("Unexpected packet: %X\n", h->type);
return PTP_IO_ERR;
}

Expand All @@ -154,7 +154,7 @@ int ptpusb_read_all_packets(struct PtpRuntime *r) {
ptp_panic("illegal connection type");
}
if (rc < 0 && r->wait_for_response) {
ptp_verbose_log("Response error %d, trying again", rc);
ptp_error_log("Response error %d, trying again", rc);
r->wait_for_response--;
continue;
} else if (rc < 0) {
Expand Down Expand Up @@ -187,13 +187,13 @@ int ptpusb_read_all_packets(struct PtpRuntime *r) {
ptp_read_u32(&c->length, &data_length);
ptp_read_u16(&c->type, &type);
if (type != PTP_PACKET_TYPE_RESPONSE) {
ptp_verbose_log("Expected response packet but got %d\n", type);
ptp_error_log("Expected response packet but got %d\n", type);
return PTP_IO_ERR;
}
if (read == data_length + length) {
break;
} else if (read > data_length + length) {
ptp_verbose_log("Read too much data %d\n", read);
ptp_error_log("Read too much data %d\n", read);
return PTP_IO_ERR;
}
}
Expand Down

0 comments on commit 01e5362

Please sign in to comment.