Skip to content

Commit

Permalink
Remove some unneccessary lines of code, do a bunch of old TODOs
Browse files Browse the repository at this point in the history
  • Loading branch information
petabyt committed Oct 12, 2023
1 parent 9d9971a commit a8a4757
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 48 deletions.
3 changes: 2 additions & 1 deletion src/data.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Parse/pack data and convert to (and from?) JSON
// Data structure unpacking and packing functions
// Copyright 2022 by Daniel C (https://github.com/petabyt/camlib)

#include <stdio.h>
Expand All @@ -9,6 +9,7 @@
#include <camlib.h>

// Custom snprint with offset - for safer string building
// Eventually, this should be used for all JSON string builders
static int osnprintf(char *str, int cur, int size, const char *format, ...) {
if (size - cur < 0) {
return 0;
Expand Down
82 changes: 36 additions & 46 deletions src/operations.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,15 @@ int ptpip_init_command_request(struct PtpRuntime *r, char *device_name) {
x = ptpip_cmd_read(r, r->data + 4, p->length - 4);
if (x < 0) return PTP_IO_ERR;

// TODO: check for PTPIP_INIT_FAIL
struct PtpIpHeader *hdr = (struct PtpIpHeader *)r->data;
if (hdr->type == PTPIP_INIT_FAIL) {
return PTP_CHECK_CODE;
}

return 0;
}

// Technically not an OC, but fits snug here
// Experimental, not for use yet - none of my devices seem to use this endpoint
int ptp_get_event(struct PtpRuntime *r, struct PtpEventContainer *ec) {
int x = ptp_read_int(r, r->data, r->max_packet_size);
if (x < 0) {
Expand All @@ -55,10 +58,11 @@ int ptpip_init_events(struct PtpRuntime *r) {
h.length = 12;
h.type = PTPIP_INIT_EVENT_REQ;
h.params[0] = 1;
if (ptpip_event_send(r, &h, 12) != 12) {
if (ptpip_event_send(r, &h, h.length) != h.length) {
return PTP_IO_ERR;
}

// ack is always 4 bytes
if (ptpip_event_read(r, r->data, 8) != 8) {
return PTP_IO_ERR;
}
Expand All @@ -75,18 +79,10 @@ int ptp_open_session(struct PtpRuntime *r) {
cmd.param_length = 1;
cmd.data_length = 0;

int length = ptp_new_cmd_packet(r, &cmd);

// PTP open session transaction ID is always 0
r->transaction = 0;

if (ptp_send_bulk_packets(r, length) != length) return PTP_IO_ERR;

// Set transaction ID back to start
r->transaction = 1;

if (ptp_receive_bulk_packets(r) < 0) return PTP_IO_ERR;
return 0;
return ptp_generic_send(r, &cmd);
}

int ptp_close_session(struct PtpRuntime *r) {
Expand All @@ -97,19 +93,13 @@ int ptp_close_session(struct PtpRuntime *r) {
}

int ptp_get_device_info(struct PtpRuntime *r, struct PtpDeviceInfo *di) {
// Assumes di is allocated - will be used during runtime later
//r->di = di;

struct PtpCommand cmd;
cmd.code = PTP_OC_GetDeviceInfo;
cmd.param_length = 0;
int x = ptp_generic_send(r, &cmd);
if (x) {
return x;
} else {
// Interfere with errors?
return ptp_parse_device_info(r, di);
}
int rc = ptp_generic_send(r, &cmd);
if (rc) return rc;

return ptp_parse_device_info(r, di);
}

int ptp_init_capture(struct PtpRuntime *r, int storage_id, int object_format) {
Expand Down Expand Up @@ -146,9 +136,9 @@ int ptp_get_storage_ids(struct PtpRuntime *r, struct UintArray **a) {
cmd.code = PTP_OC_GetStorageIDs;
cmd.param_length = 0;

int x = ptp_generic_send(r, &cmd);
int rc = ptp_generic_send(r, &cmd);
*a = (void*)ptp_get_payload(r);
return x;
return rc;
}

int ptp_get_storage_info(struct PtpRuntime *r, int id, struct PtpStorageInfo *si) {
Expand All @@ -157,13 +147,11 @@ int ptp_get_storage_info(struct PtpRuntime *r, int id, struct PtpStorageInfo *si
cmd.param_length = 1;
cmd.params[0] = id;

int x = ptp_generic_send(r, &cmd);
if (x) {
return x;
} else {
memcpy(si, ptp_get_payload(r), sizeof(struct PtpStorageInfo));
return 0;
}
int rc = ptp_generic_send(r, &cmd);
if (rc) return rc;

memcpy(si, ptp_get_payload(r), sizeof(struct PtpStorageInfo));
return 0;
}

int ptp_get_partial_object(struct PtpRuntime *r, uint32_t handle, int offset, int max) {
Expand All @@ -174,8 +162,7 @@ int ptp_get_partial_object(struct PtpRuntime *r, uint32_t handle, int offset, in
cmd.params[1] = offset;
cmd.params[2] = max;

int x = ptp_generic_send(r, &cmd);
return x;
return ptp_generic_send(r, &cmd);
}

int ptp_get_object_info(struct PtpRuntime *r, uint32_t handle, struct PtpObjectInfo *oi) {
Expand All @@ -184,12 +171,11 @@ int ptp_get_object_info(struct PtpRuntime *r, uint32_t handle, struct PtpObjectI
cmd.param_length = 1;
cmd.params[0] = handle;

int x = ptp_generic_send(r, &cmd);
if (x) {
return x;
} else {
return ptp_parse_object_info(r, oi);
}
int rc = ptp_generic_send(r, &cmd);
if (rc) return rc;

ptp_parse_object_info(r, oi);
return 0;
}

int ptp_send_object_info(struct PtpRuntime *r, int storage_id, int handle, struct PtpObjectInfo *oi) {
Expand Down Expand Up @@ -217,9 +203,11 @@ int ptp_get_object_handles(struct PtpRuntime *r, int id, int format, int in, str
cmd.params[1] = format;
cmd.params[2] = in;

int x = ptp_generic_send(r, &cmd);
*a = (void*)ptp_get_payload(r);
return x;
int rc = ptp_generic_send(r, &cmd);

(*a) = ptp_dup_uint_array((void *)ptp_get_payload(r));

return rc;
}

int ptp_get_num_objects(struct PtpRuntime *r, int id, int format, int in) {
Expand Down Expand Up @@ -255,13 +243,14 @@ int ptp_get_prop_desc(struct PtpRuntime *r, int code, struct PtpDevPropDesc *pd)
return x;
}

// NOTE: raw JPEG contents is directly in the payload
int ptp_get_thumbnail(struct PtpRuntime *r, int handle) {
struct PtpCommand cmd;
cmd.code = PTP_OC_GetThumb;
cmd.param_length = 1;
cmd.params[0] = handle;

// NOTE: raw JPEG contents is directly in the payload

return ptp_generic_send(r, &cmd);
}

Expand Down Expand Up @@ -316,7 +305,7 @@ int ptp_get_object(struct PtpRuntime *r, int handle) {
}

int ptp_download_file(struct PtpRuntime *r, int handle, char *file) {
int max = r->data_length - 12;
int max = ptp_get_payload_length(r);

struct PtpObjectInfo oi;
if (ptp_get_object_info(r, handle, &oi)) {
Expand Down Expand Up @@ -357,9 +346,9 @@ int ptp_download_file(struct PtpRuntime *r, int handle, char *file) {
int ptp_get_all_known(struct PtpRuntime *r, struct PtpGenericEvent **s, int *length) {
uint16_t *props = r->di->props_supported;
int plength = r->di->props_supported_length;
*length = plength;
(*length) = plength;

*s = malloc(sizeof(struct PtpGenericEvent) * plength);
(*s) = malloc(sizeof(struct PtpGenericEvent) * plength);

for (int i = 0; i < plength; i++) {
struct PtpGenericEvent *cur = &((*s)[i]);
Expand All @@ -376,6 +365,7 @@ int ptp_get_all_known(struct PtpRuntime *r, struct PtpGenericEvent **s, int *len
continue;
}

// TODO: Get more props
switch (props[i]) {
case PTP_PC_BatteryLevel:
cur->name = "battery";
Expand Down
14 changes: 13 additions & 1 deletion src/util.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Helper/convenient functions
// Library frontend functions
// Copyright 2022 by Daniel C (https://github.com/petabyt/camlib)

#include <stdlib.h>
Expand Down Expand Up @@ -77,6 +77,12 @@ int ptp_generic_send(struct PtpRuntime *r, struct PtpCommand *cmd) {
return PTP_IO_ERR;
}

if (ptp_get_last_transaction(r) != r->transaction) {
ptp_verbose_log("Mismatch transaction ID\n");
ptp_mutex_unlock(r);
return PTP_IO_ERR;
}

r->transaction++;

if (ptp_get_return_code(r) == PTP_RC_OK) {
Expand Down Expand Up @@ -139,6 +145,12 @@ int ptp_generic_send_data(struct PtpRuntime *r, struct PtpCommand *cmd, void *da
return PTP_IO_ERR;
}

if (ptp_get_last_transaction(r) != r->transaction) {
ptp_verbose_log("Mismatch transaction ID\n");
ptp_mutex_unlock(r);
return PTP_IO_ERR;
}

r->transaction++;

if (ptp_get_return_code(r) == PTP_RC_OK) {
Expand Down

0 comments on commit a8a4757

Please sign in to comment.