From 25c27d80a689c6c296b96a98214c8b986834f658 Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 29 Mar 2024 15:13:46 -0400 Subject: [PATCH] all old uint functions are now gone, + fixed bug in ptp_parse_prop_value, added to unit test --- src/data.c | 15 ++++++---- src/packet.c | 78 +++++++++++++--------------------------------------- test/test.c | 5 ++++ 3 files changed, 34 insertions(+), 64 deletions(-) diff --git a/src/data.c b/src/data.c index 516e808..8b3ef4c 100644 --- a/src/data.c +++ b/src/data.c @@ -77,17 +77,22 @@ int ptp_parse_data_u32(void *d, int type, int *out) { } int ptp_parse_prop_value(struct PtpRuntime *r) { - uint8_t *d = ptp_get_payload(r); + int type; switch (ptp_get_payload_length(r)) { case 1: - return (int)(d[0]); + type = PTP_TC_UINT8; break; case 2: - return (int)(d[0]); + type = PTP_TC_UINT16; break; case 4: - return (int)(d[0]); + type = PTP_TC_UINT32; break; + default: + ptp_panic("ptp_parse_prop_value: unknown data type"); } - return -1; + int out; + ptp_parse_data_u32(ptp_get_payload(r), type, &out); + + return out; } int parse_data_data_or_u32(uint8_t *d, int type, uint32_t *u32, void **data) { diff --git a/src/packet.c b/src/packet.c index 373835d..b550623 100644 --- a/src/packet.c +++ b/src/packet.c @@ -9,41 +9,6 @@ #include #include -static uint8_t ptp_read_uint8(void *dat) { - uint8_t **p = (uint8_t **)dat; - uint8_t x = (**p); - (*p)++; - return x; -} - -static uint16_t ptp_read_uint16(void *dat) { - uint16_t **p = (uint16_t **)dat; - uint16_t x = (**p); - (*p)++; - return x; -} - -static uint32_t ptp_read_uint32(void *dat) { - uint32_t **p = (uint32_t **)dat; - uint32_t x = (**p); - (*p)++; - return x; -} - -static void ptp_write_uint8(void *dat, uint8_t b) { - uint8_t **ptr = (uint8_t **)dat; - (**ptr) = b; - (*ptr)++; -} - -static int ptp_write_uint32(void *dat, uint32_t b) { - uint32_t **ptr = (uint32_t **)dat; - (**ptr) = b; - (*ptr)++; - - return 4; -} - // Read standard UTF16 string int ptp_read_string(uint8_t *d, char *string, int max) { int of = 0; @@ -66,44 +31,39 @@ int ptp_read_string(uint8_t *d, char *string, int max) { return of; } -static int ptp_read_uint16_array_(void *dat, uint16_t *buf, int max) { - int n = ptp_read_uint32(dat); +int ptp_read_uint16_array(uint8_t *dat, uint16_t *buf, int max, int *length) { + int of = 0; - for (int i = 0; i < n; i++) { + uint32_t n; + of += ptp_read_u32(dat + of, &n); + + for (uint32_t i = 0; i < n; i++) { if (i >= max) { - (void)ptp_read_uint16(dat); + ptp_panic("ptp_read_uint16_array overflow\n"); } else { - buf[i] = ptp_read_uint16(dat); + of += ptp_read_u16(buf + of, &buf[i]); } } - return n; + return of; } -int ptp_read_uint16_array(uint8_t *dat, uint16_t *buf, int max, int *length) { - uint8_t *two = dat; - (*length) = ptp_read_uint16_array_(&two, buf, max); - return two - dat; -} +// Write standard PTP wchar string +int ptp_write_string(uint8_t *dat, char *string) { + int of = 0; -static int ptp_write_string_(void *dat, char *string) { - int length = strlen(string); - ptp_write_uint8(dat, length); + uint32_t length = strlen(string); + of += ptp_write_u8(dat + of, length); for (int i = 0; i < length; i++) { - ptp_write_uint8(dat, string[i]); - ptp_write_uint8(dat, '\0'); + of += ptp_write_u8(dat + of, string[i]); + of += ptp_write_u8(dat + of, '\0'); } - ptp_write_uint8(dat, '\0'); - - return (length * 2) + 2; -} - -int ptp_write_string(uint8_t *dat, char *string) { - return ptp_write_string_(&dat, string); + return of; } +// Write normal UTF-8 string int ptp_write_utf8_string(void *dat, char *string) { char *o = (char *)dat; int x = 0; @@ -128,7 +88,7 @@ int ptp_write_unicode_string(char *dat, char *string) { return i; } -// Reaed null-terminated UTF16 string +// Read null-terminated UTF16 string int ptp_read_unicode_string(char *buffer, char *dat, int max) { int i; for (i = 0; dat[i] != '\0'; i += 2) { diff --git a/test/test.c b/test/test.c index b71434b..61db80e 100644 --- a/test/test.c +++ b/test/test.c @@ -144,6 +144,11 @@ int test_props() { int rc = test_setup_usb(&r); if (rc) return rc; + rc = ptp_get_prop_value(&r, PTP_PC_BatteryLevel); + if (rc) return rc; + printf("Test parse: %d\n", ptp_parse_prop_value(&r)); + assert(ptp_parse_prop_value(&r) == 50); + struct PtpPropDesc pd; rc = ptp_get_prop_desc(&r, PTP_PC_BatteryLevel, &pd); if (rc) return rc;