Skip to content

Commit

Permalink
Fix some casting/const issues, add some mutex locks
Browse files Browse the repository at this point in the history
  • Loading branch information
petabyt committed Apr 21, 2024
1 parent a41f5bd commit d47cbe4
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/cl_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ int ptp_fsend_packets(struct PtpRuntime *r, int length, FILE *stream);
// Reads the incoming packet to file, starting after an optional offset
int ptp_freceive_bulk_packets(struct PtpRuntime *r, FILE *stream, int of);

int ptpip_connect(struct PtpRuntime *r, char *addr, int port);
int ptpip_connect(struct PtpRuntime *r, const char *addr, int port);
int ptpip_cmd_write(struct PtpRuntime *r, void *data, int size);
int ptpip_cmd_read(struct PtpRuntime *r, void *data, int size);

int ptpip_connect_events(struct PtpRuntime *r, char *addr, int port);
int ptpip_connect_events(struct PtpRuntime *r, const char *addr, int port);
int ptpip_event_send(struct PtpRuntime *r, void *data, int size);
int ptpip_event_read(struct PtpRuntime *r, void *data, int size);

Expand Down
2 changes: 1 addition & 1 deletion src/cl_ops.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/// @brief Set a generic property - abstraction over SetDeviceProp
/// @note May reject writes if an invalid property is found (see event code)
/// @memberof PtpRuntime
int ptp_set_generic_property(struct PtpRuntime *r, char *name, int value);
int ptp_set_generic_property(struct PtpRuntime *r, const char *name, int value);

/// @brief Call before taking a picture - this is generally for 'focusing'
/// On some cameras this does nothing.
Expand Down
2 changes: 1 addition & 1 deletion src/data.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ int parse_data_data_or_u32(uint8_t *d, int type, uint32_t *u32, void **data) {
case PTP_TC_UINT16:
case PTP_TC_INT32:
case PTP_TC_UINT32:
return ptp_parse_data_u32(d, type, u32);
return ptp_parse_data_u32(d, type, (int *)u32);
case PTP_TC_INT64:
case PTP_TC_UINT64:
(*data) = malloc(8);
Expand Down
2 changes: 1 addition & 1 deletion src/generic.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ static int ptp_eos_set_validate_prop(struct PtpRuntime *r, int prop_code, uint32
return ptp_eos_set_prop_value(r, prop_code, value);
}

int ptp_set_generic_property(struct PtpRuntime *r, char *name, int value) {
int ptp_set_generic_property(struct PtpRuntime *r, const char *name, int value) {
int dev = ptp_device_type(r);
int rc = 0;
if (!strcmp(name, "aperture")) {
Expand Down
5 changes: 3 additions & 2 deletions src/ip.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,21 +123,22 @@ static struct PtpIpBackend *init_comm(struct PtpRuntime *r) {
return (struct PtpIpBackend *)r->comm_backend;
}

int ptpip_connect(struct PtpRuntime *r, char *addr, int port) {
int ptpip_connect(struct PtpRuntime *r, const char *addr, int port) {
int fd = ptpip_new_timeout_socket(addr, port);

struct PtpIpBackend *b = init_comm(r);

if (fd > 0) {
b->fd = fd;
r->io_kill_switch = 0;
return 0;
} else {
b->fd = 0;
return fd;
}
}

int ptpip_connect_events(struct PtpRuntime *r, char *addr, int port) {
int ptpip_connect_events(struct PtpRuntime *r, const char *addr, int port) {
int fd = ptpip_new_timeout_socket(addr, port);

struct PtpIpBackend *b = init_comm(r);
Expand Down
2 changes: 1 addition & 1 deletion src/lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ void ptpusb_free_device_list(struct PtpDeviceEntry *e) {

int ptp_buffer_resize(struct PtpRuntime *r, size_t size) {
if (size < r->data_length) {
ptp_panic("You cannot downside the data buffer");
ptp_panic("You cannot downsize the data buffer (%u -> %u)", r->data_length, size);
}
// realloc with a little extra space to minimize reallocs later on
static int extra = 100;
Expand Down
4 changes: 2 additions & 2 deletions src/no_ip.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ int ptpip_new_timeout_socket(char *addr, int port) {
return -1;
}

int ptpip_connect(struct PtpRuntime *r, char *addr, int port) {
int ptpip_connect(struct PtpRuntime *r, const char *addr, int port) {
return -1;
}

int ptpip_connect_events(struct PtpRuntime *r, char *addr, int port) {
int ptpip_connect_events(struct PtpRuntime *r, const char *addr, int port) {
return -1;
}

Expand Down
15 changes: 13 additions & 2 deletions src/operations.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,16 @@ int ptp_get_object_handles(struct PtpRuntime *r, int id, int format, int in, str
cmd.params[1] = format;
cmd.params[2] = in;

ptp_mutex_keep_locked(r);

int rc = ptp_send(r, &cmd);
if (rc) goto end;

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

end:;
ptp_mutex_unlock(r);

return rc;
}

Expand Down Expand Up @@ -253,11 +259,16 @@ int ptp_get_prop_desc(struct PtpRuntime *r, int code, struct PtpPropDesc *pd) {
cmd.param_length = 1;
cmd.params[0] = code;

int x = ptp_send(r, &cmd);
ptp_mutex_keep_locked(r);
int rc = ptp_send(r, &cmd);
if (rc) goto end;

ptp_parse_prop_desc(r, pd);

return x;
end:;
ptp_mutex_unlock(r);

return rc;
}

int ptp_get_thumbnail(struct PtpRuntime *r, int handle) {
Expand Down
13 changes: 7 additions & 6 deletions src/transport.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ int ptpip_read_packet(struct PtpRuntime *r, int of) {
int rc = 0;
int read = 0;

while (rc <= 0 && r->wait_for_response) {
while (r->wait_for_response) {
rc = ptpip_cmd_read(r, r->data + of + read, 4);

r->wait_for_response--;

if (rc > 0) break;

if (r->wait_for_response) {
ptp_verbose_log("Trying again...");
ptp_verbose_log("Trying again...\n");
CAMLIB_SLEEP(CAMLIB_WAIT_MS);
}
}
Expand Down Expand Up @@ -126,7 +126,7 @@ int ptpip_receive_bulk_packets(struct PtpRuntime *r) {
return PTP_IO_ERR;
}
} else if (h->type == PTPIP_COMMAND_RESPONSE) {
ptp_verbose_log("Recieved response packet\n");
ptp_verbose_log("Received response packet\n");
} else {
ptp_verbose_log("Unexpected packet: %X\n", h->type);
return PTP_IO_ERR;
Expand Down Expand Up @@ -167,7 +167,7 @@ int ptpusb_read_all_packets(struct PtpRuntime *r) {
if (rc < 0) break;

if (r->wait_for_response) {
ptp_verbose_log("Trying again...");
ptp_verbose_log("Trying again...\n");
CAMLIB_SLEEP(CAMLIB_WAIT_MS);
}
}
Expand Down Expand Up @@ -218,15 +218,15 @@ int ptpipusb_read_packet(struct PtpRuntime *r, int of) {
int rc = 0;
int read = 0;

while (rc <= 0 && r->wait_for_response) {
while (r->wait_for_response) {
rc = ptpip_cmd_read(r, r->data + of + read, 4);

r->wait_for_response--;

if (rc > 0) break;

if (r->wait_for_response) {
ptp_verbose_log("Trying again...");
ptp_verbose_log("Trying again...\n");
CAMLIB_SLEEP(CAMLIB_WAIT_MS);
}
}
Expand Down Expand Up @@ -308,6 +308,7 @@ int ptpipusb_receive_bulk_packets(struct PtpRuntime *r) {
}

int ptp_receive_bulk_packets(struct PtpRuntime *r) {
if (r->io_kill_switch) return -1;
if (r->connection_type == PTP_IP) {
return ptpip_receive_bulk_packets(r);
} else if (r->connection_type == PTP_USB) {
Expand Down

0 comments on commit d47cbe4

Please sign in to comment.