diff --git a/Makefile b/Makefile index d84399d..1a79456 100644 --- a/Makefile +++ b/Makefile @@ -69,8 +69,8 @@ install: libcamlib.so -mkdir /usr/include/camlib cp src/*.h /usr/include/camlib/ -test-ci: test/test.o $(UNIX_LIB_FILES) - $(CC) test/test.o $(UNIX_LIB_FILES) -lusb-vcam -lexif $(CFLAGS) -o test-ci +test-ci: test/test.o test/data.o $(UNIX_LIB_FILES) + $(CC) test/test.o test/data.o $(UNIX_LIB_FILES) -lusb-vcam -lexif $(CFLAGS) -o test-ci test: test-ci ./test-ci diff --git a/src/data.c b/src/data.c index b6e42a3..0941f9c 100644 --- a/src/data.c +++ b/src/data.c @@ -100,6 +100,20 @@ int ptp_parse_prop_value(struct PtpRuntime *r) { return out; } +static int get_arr_item_size(int t) { + switch (t) { + case PTP_TC_UINT8ARRAY: + return 1; + case PTP_TC_UINT16ARRAY: + return 2; + case PTP_TC_UINT32ARRAY: + return 4; + case PTP_TC_UINT64ARRAY: + return 8; + } + ptp_panic("Unknown arr type\n"); +} + int parse_data_data_or_u32(uint8_t *d, int type, uint32_t *u32, void **data) { int size; uint32_t length32; @@ -119,17 +133,14 @@ int parse_data_data_or_u32(uint8_t *d, int type, uint32_t *u32, void **data) { memcpy((*data), d, 8); return 8; case PTP_TC_UINT8ARRAY: - size = 1; case PTP_TC_UINT16ARRAY: - size = 2; case PTP_TC_UINT32ARRAY: - size = 4; case PTP_TC_UINT64ARRAY: - size = 8; + size = get_arr_item_size(type); ptp_read_u32(d, &length32); - (*data) = malloc(4 + length32 * size); - memcpy((*data), d, 4 + length32 * size); - return 4 + length32 * size; + (*data) = malloc(4 + (int)length32 * size); + memcpy((*data), d, 4 + (int)length32 * size); + return 4 + (int)length32 * size; case PTP_TC_STRING: ptp_read_u8(d, &length8); len_total = 1 + (length8 * 2); @@ -182,24 +193,26 @@ int ptp_prop_desc_json(const struct PtpPropDesc *pd, char *buffer, int max) { curr += osnprintf(buffer, curr, max, "\"type\": %d,\n", pd->data_type); switch (pd->data_type) { - case PTP_TC_INT8: - case PTP_TC_UINT8: - case PTP_TC_INT16: - case PTP_TC_UINT16: - case PTP_TC_INT32: - case PTP_TC_UINT32: - case PTP_TC_INT64: - case PTP_TC_UINT64: - curr += osnprintf(buffer, curr, max, "\"currentValue32\": %d,\n", pd->current_value32); - curr += osnprintf(buffer, curr, max, "\"defaultValue32\": %u,\n", pd->default_value32); - break; - case PTP_TC_UINT8ARRAY: - case PTP_TC_UINT16ARRAY: - case PTP_TC_UINT32ARRAY: - case PTP_TC_UINT64ARRAY: - curr += osnprintf(buffer, curr, max, "\"currentValueArray\": %d,\n", 0); - break; -// case PTP_TC_STRING: + case PTP_TC_INT8: + case PTP_TC_UINT8: + case PTP_TC_INT16: + case PTP_TC_UINT16: + case PTP_TC_INT32: + case PTP_TC_UINT32: + case PTP_TC_INT64: + case PTP_TC_UINT64: + curr += osnprintf(buffer, curr, max, "\"currentValue32\": %d,\n", pd->current_value32); + curr += osnprintf(buffer, curr, max, "\"defaultValue32\": %u,\n", pd->default_value32); + break; + case PTP_TC_UINT8ARRAY: + case PTP_TC_UINT16ARRAY: + case PTP_TC_UINT32ARRAY: + case PTP_TC_UINT64ARRAY: // TODO: + ptp_panic("Writeme\n"); + break; + case PTP_TC_STRING: + ptp_panic("Writeme\n"); + break; } if (pd->form_type == PTP_RangeForm) { diff --git a/src/lib.c b/src/lib.c index 37bcc86..44338ee 100644 --- a/src/lib.c +++ b/src/lib.c @@ -144,7 +144,9 @@ void ptp_close(struct PtpRuntime *r) { void ptp_mutex_unlock_thread(struct PtpRuntime *r) { if (r->mutex == NULL) return; // Wait until we get EPERM (we do not own the mutex anymore) - while (pthread_mutex_unlock(r->mutex) == 0); + while (pthread_mutex_unlock(r->mutex) == 0) { + ptp_verbose_log("WARN: pid %d had mutex locked\n", getpid()); + } } static int ptp_check_rc(struct PtpRuntime *r) { diff --git a/src/packet.c b/src/packet.c index 379c628..40b8edf 100644 --- a/src/packet.c +++ b/src/packet.c @@ -31,7 +31,7 @@ int ptp_read_string(uint8_t *d, char *string, int max) { else if (wchr != '\0' && wchr < 32) wchr = ' '; string[i] = (char)wchr; i++; - if (i >= max) break; + if (i >= max - 1) break; } string[i] = '\0'; @@ -40,6 +40,7 @@ int ptp_read_string(uint8_t *d, char *string, int max) { } int ptp_read_uint16_array(const uint8_t *dat, uint16_t *buf, int max, int *length) { + ptp_panic("Unfinished\n"); int of = 0; uint32_t n; @@ -117,7 +118,7 @@ int ptp_read_unicode_string(char *buffer, char *dat, int max) { int i; for (i = 0; dat[i] != '\0'; i += 2) { buffer[i / 2] = dat[i]; - if (i >= max) { + if (i >= max - 2) { buffer[(i / 2) + 1] = '\0'; return i; } @@ -283,7 +284,6 @@ int ptp_get_return_code(struct PtpRuntime *r) { } } -// Get ptr to payload uint8_t *ptp_get_payload(struct PtpRuntime *r) { if (r->connection_type == PTP_IP) { // For IP, payload is in the DATA_END packet diff --git a/src/transport.c b/src/transport.c index d1090e7..70ee5d2 100644 --- a/src/transport.c +++ b/src/transport.c @@ -42,7 +42,7 @@ int ptp_send_packet(struct PtpRuntime *r, int length) { int x = ptpip_cmd_write(r, r->data + sent, length); if (x < 0) { - ptp_verbose_log("send_bulk_packet: %d\n", __func__, x); + ptp_verbose_log("%s: %d\n", __func__, x); return PTP_IO_ERR; } diff --git a/test/data.c b/test/data.c new file mode 100644 index 0000000..d470cc1 --- /dev/null +++ b/test/data.c @@ -0,0 +1,24 @@ +#include +#include +#include +#include + +int test_data() { + { + uint8_t buffer[] = {05, 'H', 00, 'e', 00, 'l', 00, 'l', 00 ,'o', 00}; + char string[64]; + int rc = ptp_read_string(buffer, string, sizeof(string)); + assert(!strcmp(string, "Hello")); + assert(rc == 11); + } + + { + uint8_t buffer[64]; + uint8_t ref[] = {05, 'H', 00, 'e', 00, 'l', 00, 'l', 00 ,'o', 00}; + int rc = ptp_write_string(buffer, "Hello"); + assert(!memcmp(buffer, ref, sizeof(ref))); + assert(rc == 11); + } + + return 0; +} diff --git a/test/test.c b/test/test.c index 8bdc662..f1a8623 100644 --- a/test/test.c +++ b/test/test.c @@ -257,9 +257,14 @@ static int test_multithread() { return 0; } +int test_data(); + int main() { int rc; + rc = test_data(); + if (rc) return rc; + rc = test_multithread(); printf("Return code: %d\n", rc); if (rc) return rc;