Skip to content

Commit

Permalink
Partially implement ptp_prop_desc_json
Browse files Browse the repository at this point in the history
  • Loading branch information
petabyt committed Jul 15, 2024
1 parent 2af2e50 commit af007b8
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/camlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ struct PtpRuntime {
/// @brief For session comm/io structures (holds backend instance pointers)
void *comm_backend;

void *userdata;

/// @brief Optional (see CAMLIB_DONT_USE_MUTEX)
pthread_mutex_t *mutex;

Expand Down
7 changes: 4 additions & 3 deletions src/cl_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ struct PtpDeviceInfo {
uint16_t ops_supported[256];

int events_supported_length;
uint16_t events_supported[128];
uint16_t events_supported[256];

int props_supported_length;
uint16_t props_supported[128];
uint16_t props_supported[256];

int capture_formats_length;
uint16_t capture_formats[32];
Expand Down Expand Up @@ -82,7 +82,7 @@ struct PtpObjectInfo {

struct PtpEnumerationForm {
uint16_t length;
char data[];
uint8_t data[];
};

struct PtpRangeForm {
Expand Down Expand Up @@ -169,6 +169,7 @@ int ptp_parse_prop_value(struct PtpRuntime *r);
int ptp_parse_device_info(struct PtpRuntime *r, struct PtpDeviceInfo *di);
int ptp_device_info_json(struct PtpDeviceInfo *di, char *buffer, int max);
int ptp_parse_prop_desc(struct PtpRuntime *r, struct PtpPropDesc *oi);
int ptp_prop_desc_json(struct PtpPropDesc *pd, char *buffer, int max);
int ptp_parse_object_info(struct PtpRuntime *r, struct PtpObjectInfo *oi);
int ptp_storage_info_json(struct PtpStorageInfo *so, char *buffer, int max);
int ptp_object_info_json(struct PtpObjectInfo *so, char *buffer, int max);
Expand Down
57 changes: 57 additions & 0 deletions src/data.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,16 @@ int ptp_parse_data_u32(void *d, int type, int *out) {
case PTP_TC_UINT8:
ptp_read_u8(d, &a); (*out) = (int)a; return 1;
case PTP_TC_INT16:
ptp_read_u16(d, &b); (*out) = (int)((short)b); return 2;
case PTP_TC_UINT16:
ptp_read_u16(d, &b); (*out) = (int)b; return 2;
case PTP_TC_INT32:
case PTP_TC_UINT32:
ptp_read_u32(d, &c); (*out) = (int)c; return 4;
}

// TODO: function to preserve signedness

// skip the array
return ptp_get_data_size(d, type);
}
Expand Down Expand Up @@ -173,6 +176,60 @@ int ptp_parse_prop_desc(struct PtpRuntime *r, struct PtpPropDesc *oi) {
return 0;
}

int ptp_prop_desc_json(struct PtpPropDesc *pd, char *buffer, int max) {
int curr = osnprintf(buffer, curr, max, "{\n");
curr += osnprintf(buffer, curr, max, "\"code\": %d,\n", pd->code);
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:
}

if (pd->form_type == PTP_RangeForm) {
curr += osnprintf(buffer, curr, max, "\"validEntries\": [");
for (int i = pd->range_form.min; i < pd->range_form.max; i += pd->range_form.step) {
char *end = ", ";
if (i >= pd->range_form.max - pd->range_form.step) end = "";
curr += osnprintf(buffer, curr, max, "%d%s", i, end);
}
curr += osnprintf(buffer, curr, max, "],\n");
} else {
curr += osnprintf(buffer, curr, max, "\"validEntries\": [");
int of = 0;
for (int i = 0; i < pd->enum_form->length; i++) {
char *end = ", ";
if (i >= pd->enum_form->length - 1) end = "";
uint32_t value;
void *data = NULL;
of += parse_data_data_or_u32(pd->enum_form->data + of, pd->data_type, &value, &data);
curr += osnprintf(buffer, curr, max, "%d%s", value, end);
}
curr += osnprintf(buffer, curr, max, "],\n");
}

curr += osnprintf(buffer, curr, max, "\"form_type\": %d\n", pd->form_type);

curr += osnprintf(buffer, curr, max, "}");
return curr;
}

int ptp_parse_object_info(struct PtpRuntime *r, struct PtpObjectInfo *oi) {
uint8_t *d = ptp_get_payload(r);

Expand Down
2 changes: 1 addition & 1 deletion src/packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ int ptp_read_uint16_array_s(uint8_t *bs, uint8_t *be, uint16_t *buf, int max, in
boundcheck(bs, be, 4 * n + 4);
for (uint32_t i = 0; i < n; i++) {
if (i >= max) {
ptp_panic("ptp_read_uint16_array overflow\n");
ptp_panic("ptp_read_uint16_array overflow %i >= %d\n", i, n);
} else {
of += ptp_read_u16(bs + of, &buf[i]);
}
Expand Down

0 comments on commit af007b8

Please sign in to comment.