Skip to content

Commit

Permalink
Fix data bugs, fix multithreading test, fix transport
Browse files Browse the repository at this point in the history
transport.c sucks, needs a lot of work
  • Loading branch information
petabyt committed Nov 27, 2024
1 parent 365de61 commit 1d40fd5
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 39 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ clean:
src/lua/*.o src/lua/lua-cjson/*.o src/*.d examples/*.d src/lua/*.d src/lua/lua-cjson/*.d *.a
cd examples && make clean

install: libcamlib.so
cp libcamlib.so /usr/lib/
install: libcamlib.a
cp libcamlib.a /usr/lib/
-mkdir /usr/include/camlib
cp src/*.h /usr/include/camlib/

Expand Down
1 change: 1 addition & 0 deletions src/cl_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ struct PtpPropDesc {
uint16_t data_type;
uint8_t read_only; // (get/set)

// uint32_t variables will be used for all types <= 4 bytes
uint32_t default_value32;
uint32_t current_value32;
void *current_value;
Expand Down
19 changes: 13 additions & 6 deletions src/data.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ static int osnprintf(char *str, int cur, int size, const char *format, ...) {
return r;
}

int ptp_get_data_size(void *d, int type) {
int ptp_get_prop_size(uint8_t *d, int type) {
uint32_t length32;
uint8_t length8;
switch (type) {
Expand All @@ -42,17 +42,24 @@ int ptp_get_data_size(void *d, int type) {
case PTP_TC_UINT64:
return 8;
case PTP_TC_UINT8ARRAY:
ptp_read_u32(d, &length32);
return 4 + ((int)length32 * 1);
case PTP_TC_UINT16ARRAY:
ptp_read_u32(d, &length32);
return 4 + ((int)length32 * 2);
case PTP_TC_UINT32ARRAY:
ptp_read_u32(d, &length32);
return 4 + ((int)length32 * 4);
case PTP_TC_UINT64ARRAY:
ptp_read_u32(d, &length32);
return length32;
return 4 + ((int)length32 * 8);
case PTP_TC_STRING:
ptp_read_u8(d, &length8);
return length8;
return 1 + ((int)length8 * 2);
}

ptp_panic("Invalid size read");
ptp_panic("Unknown data type %d", type);
abort();
return 0;
}

Expand All @@ -76,7 +83,7 @@ int ptp_parse_data_u32(void *d, int type, int *out) {
// TODO: function to preserve signedness

// skip the array
return ptp_get_data_size(d, type);
return ptp_get_prop_size(d, type);
}

int ptp_parse_prop_value(struct PtpRuntime *r) {
Expand Down Expand Up @@ -173,7 +180,7 @@ int ptp_parse_prop_desc(struct PtpRuntime *r, struct PtpPropDesc *oi) {
d += ptp_read_u16(d, &num_values);
int length = 0;
for (uint32_t i = 0; i < num_values; i++) {
length += ptp_get_data_size(d + length, oi->data_type);
length += ptp_get_prop_size(d + length, oi->data_type);
}
struct PtpEnumerationForm *form = (struct PtpEnumerationForm *)malloc(sizeof(struct PtpEnumerationForm) + length);
form->length = num_values;
Expand Down
2 changes: 1 addition & 1 deletion src/lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void ptp_reset(struct PtpRuntime *r) {
r->connection_type = PTP_USB;
r->response_wait_default = 1;
r->wait_for_response = 1;
r->comm_backend = NULL;
r->comm_backend = NULL; // leak
}

void ptp_init(struct PtpRuntime *r) {
Expand Down
1 change: 1 addition & 0 deletions src/libusb.c
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ int ptp_cmd_write(struct PtpRuntime *r, void *to, int length) {

int ptp_cmd_read(struct PtpRuntime *r, void *to, int length) {
const struct LibUSBBackend *backend = (struct LibUSBBackend *)r->comm_backend;

if (backend == NULL || r->io_kill_switch) return -1;
int transferred = 0;
int rc = libusb_bulk_transfer(
Expand Down
101 changes: 72 additions & 29 deletions src/transport.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@

#include <camlib.h>

/*
Rewrite of backend
ptpip_read_packets:
ptpip_read_packet()
ptpusb_read_all_packets():
while read(512) != 512
ptpusb_check_packets()
ptpusbip_read_all_packets():
while read(512) != 512
ptpusb_check_packets()
*/

int ptpusb_send_bulk_packets(struct PtpRuntime *r, int length) {
int sent = 0;
int x;
Expand Down Expand Up @@ -173,7 +188,8 @@ int ptpip_write_packet(struct PtpRuntime *r, int of) {
int ptpusb_read_all_packets(struct PtpRuntime *r) {
int read = 0;

// Try and get the first 512 bytes
//int max_packet_size = r->max_packet_size;

int rc = 0;
while (r->wait_for_response) {
rc = ptp_cmd_read(r, r->data + read, r->max_packet_size);
Expand Down Expand Up @@ -203,40 +219,67 @@ int ptpusb_read_all_packets(struct PtpRuntime *r) {

struct PtpBulkContainer *c = (struct PtpBulkContainer *)(r->data);

// 512 is always enough for the response packet
// Response packet is read
if (c->type == PTP_PACKET_TYPE_RESPONSE) {
return 0;
}

// Reallocate enough to fill the rest of data and response packet
if (c->type == PTP_PACKET_TYPE_DATA && r->data_length < (c->length + r->max_packet_size)) {
rc = ptp_buffer_resize(r, c->length + r->max_packet_size);
c = (struct PtpBulkContainer *)(r->data);
if (rc) return rc;
}
if (read == c->length) {
return 0;
} else {
ptp_verbose_log("Read too much of packet: %d\n", read);
return PTP_IO_ERR;
}
} else {
// Reallocate enough to fill the rest of data and response packet
if (r->data_length < (c->length + r->max_packet_size)) {
rc = ptp_buffer_resize(r, c->length + r->max_packet_size);
if (rc) return rc;
}

while (read < c->length) {
rc = ptp_cmd_read(r, r->data + read, r->max_packet_size);
if (rc < 0) return PTP_IO_ERR;
read += rc;
}
// Make sure to read all of data packet
while (read <= c->length) {
rc = ptp_cmd_read(r, r->data + read, r->max_packet_size);
if (rc < 0) {
ptp_verbose_log("error reading data packet\n");
return PTP_IO_ERR;
}
read += rc;
}

// Read response packet
rc = ptp_cmd_read(r, r->data + read, r->max_packet_size);
if (rc == 0) {
CAMLIB_SLEEP(100);
rc = ptp_cmd_read(r, r->data + read, r->max_packet_size);
}
if (rc < 0) return PTP_IO_ERR;
read += rc;
// Read response packet
int resp_len = read - (int)c->length;
c = (struct PtpBulkContainer *)(r->data + c->length);
if (resp_len >= 4) {
int rest = c->length - resp_len;
if (rest != 0) {
rc = ptp_cmd_read(r, r->data + read, rest);
if (rc < 0) {
ptp_verbose_log("error reading resposne packet\n");
return PTP_IO_ERR;
}
read += rc;
}

if (resp_len > c->length) {
ptp_verbose_log("Read too much of packet\n");
return PTP_IO_ERR;
}
} else if (read - c->length >= 32) {
// packet read
} else {
// Read response packet
rc = ptp_cmd_read(r, r->data + read, r->max_packet_size);
if (rc == 0) {
CAMLIB_SLEEP(100);
rc = ptp_cmd_read(r, r->data + read, r->max_packet_size);
}
if (rc < 0) {
printf("Error reading response packet 2\n");
return PTP_IO_ERR;
}
read += rc;
}

if (c->type == PTP_PACKET_TYPE_DATA && read < (c->length + 12)) {
ptp_verbose_log("Response packet missing (%d %d)\n", read, c->length + 12);
return PTP_IO_ERR;
}

ptp_verbose_log("Read %d bytes\n", read);

return 0;
}

Expand Down
6 changes: 5 additions & 1 deletion test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ int test_fs() {

static void *thread(void *arg) {
struct PtpRuntime *r = (struct PtpRuntime *)arg;
if (r->operation_kill_switch) return NULL;

if ((rand() & 1) == 0) {
ptp_mutex_lock(r);
Expand All @@ -234,7 +235,7 @@ static void *thread(void *arg) {
pthread_exit(0);
err:;
printf("Error in thread %d\n", getpid());
ptp_close(r);
r->operation_kill_switch = 1;
exit(1);
}

Expand Down Expand Up @@ -265,6 +266,7 @@ int main(void) {
rc = test_data();
if (rc) return rc;

printf("Testing multithreading\n");
rc = test_multithread();
printf("Return code: %d\n", rc);
if (rc) return rc;
Expand All @@ -277,10 +279,12 @@ int main(void) {
printf("Return code: %d\n", rc);
if (rc) return rc;

printf("Testing vcam magic\n");
rc = ptp_vcam_magic();
printf("Return code: %d\n", rc);
if (rc) return rc;

printf("Testing props...\n");
rc = test_props();
printf("Return code: %d\n", rc);
if (rc) return rc;
Expand Down

0 comments on commit 1d40fd5

Please sign in to comment.