Skip to content

Commit

Permalink
Various bug fixes in packet/data functions
Browse files Browse the repository at this point in the history
  • Loading branch information
petabyt committed Oct 10, 2024
1 parent 674c985 commit e1d6eb2
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 32 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
63 changes: 38 additions & 25 deletions src/data.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion src/lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions src/packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/transport.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
24 changes: 24 additions & 0 deletions test/data.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <assert.h>
#include <string.h>
#include <camlib.h>
#include <stdint.h>

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;
}
5 changes: 5 additions & 0 deletions test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit e1d6eb2

Please sign in to comment.