diff --git a/src/camlib.h b/src/camlib.h index c350d56..ed820c5 100644 --- a/src/camlib.h +++ b/src/camlib.h @@ -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; diff --git a/src/cl_data.h b/src/cl_data.h index 0c8df9b..22ad540 100644 --- a/src/cl_data.h +++ b/src/cl_data.h @@ -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]; @@ -82,7 +82,7 @@ struct PtpObjectInfo { struct PtpEnumerationForm { uint16_t length; - char data[]; + uint8_t data[]; }; struct PtpRangeForm { @@ -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); diff --git a/src/data.c b/src/data.c index 32adcfb..8f40b59 100644 --- a/src/data.c +++ b/src/data.c @@ -65,6 +65,7 @@ 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: @@ -72,6 +73,8 @@ int ptp_parse_data_u32(void *d, int type, int *out) { 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); } @@ -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); diff --git a/src/packet.c b/src/packet.c index e1498ca..ef3a371 100644 --- a/src/packet.c +++ b/src/packet.c @@ -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]); }