Skip to content

Commit

Permalink
Convert EOS event unpacking to new packet functions
Browse files Browse the repository at this point in the history
  • Loading branch information
petabyt committed Mar 24, 2024
1 parent a2c6899 commit d576994
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 30 deletions.
3 changes: 0 additions & 3 deletions src/camlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,6 @@ uint32_t ptp_read_uint32(void *dat);
void ptp_write_uint8(void *dat, uint8_t b);
int ptp_write_uint32(void *dat, uint32_t b);

//void ptp_read_string(void *dat, char *string, int max);
//int ptp_read_uint16_array(void *dat, uint16_t *buf, int max);
//int ptp_write_string(void *dat, char *string);
int ptp_write_unicode_string(char *dat, char *string);
int ptp_read_unicode_string(char *buffer, char *dat, int max);
int ptp_read_utf8_string(void *dat, char *string, int max);
Expand Down
61 changes: 35 additions & 26 deletions src/data.c
Original file line number Diff line number Diff line change
Expand Up @@ -388,9 +388,12 @@ int ptp_storage_info_json(struct PtpStorageInfo *so, char *buffer, int max) {
return len;
}

int ptp_eos_prop_next(void **d, struct PtpGenericEvent *p) {
uint32_t code = ptp_read_uint32(d);
uint32_t value = ptp_read_uint32(d);
int ptp_eos_prop_next(void *d, struct PtpGenericEvent *p) {
uint32_t code, value, tmp;

int of = 0;
of += ptp_read_u32(d + of, &code);
of += ptp_read_u32(d + of, &value);

const char *name = ptp_get_enum_all(code);
const char *str_value = NULL;
Expand All @@ -414,8 +417,11 @@ int ptp_eos_prop_next(void **d, struct PtpGenericEvent *p) {
name = "battery";
break;
case PTP_PC_EOS_ImageFormat: {
int data[5] = {value, ptp_read_uint32(d), ptp_read_uint32(d),
ptp_read_uint32(d), ptp_read_uint32(d)};
int data[5] = {value};
of += ptp_read_u32(d + of, &data[1]);
of += ptp_read_u32(d + of, &data[2]);
of += ptp_read_u32(d + of, &data[3]);
of += ptp_read_u32(d + of, &data[4]);
if (value == 1) {
value = ptp_eos_get_imgformat_value(data);
} else {
Expand Down Expand Up @@ -481,9 +487,10 @@ int ptp_eos_events_length(struct PtpRuntime *r) {
int length = 0;
while (dp != NULL) {
if (dp >= (uint8_t *)ptp_get_payload(r) + ptp_get_payload_length(r)) break;
void *d = dp;
uint32_t size = ptp_read_uint32(&d);
uint32_t type = ptp_read_uint32(&d);
uint8_t *d = dp;
uint32_t size, type;
d += ptp_read_u32(d, &size);
d += ptp_read_u32(d, &type);

dp += size;

Expand All @@ -505,32 +512,34 @@ int ptp_eos_events(struct PtpRuntime *r, struct PtpGenericEvent **p) {
if (length < 0) return length;

(*p) = malloc(sizeof(struct PtpGenericEvent) * length);
struct PtpGenericEvent *p_base = (*p);

uint8_t *dp = ptp_get_payload(r);
for (int i = 0; i < length; i++) {
// TODO: Simplify these triple pointers
struct PtpGenericEvent *cur = &((*p)[i]);
struct PtpGenericEvent *cur = &p_base[i];
memset(cur, 0, sizeof(struct PtpGenericEvent));

// Read header
void *d = dp;
uint32_t size = ptp_read_uint32(&d);
uint32_t type = ptp_read_uint32(&d);

uint32_t size, type;
d += ptp_read_u32(d, &size);
d += ptp_read_u32(d, &type);

// Detect termination or overflow
if (type == 0) break;

switch (type) {
case PTP_EC_EOS_PropValueChanged:
ptp_eos_prop_next(&d, cur);
d += ptp_eos_prop_next(d, cur);
break;
case PTP_EC_EOS_InfoCheckComplete:
case PTP_PC_EOS_FocusInfoEx:
cur->name = ptp_get_enum_all(type);
break;
case PTP_EC_EOS_RequestObjectTransfer: {
uint32_t a = ptp_read_uint32(&d);
uint32_t b = ptp_read_uint32(&d);
uint32_t a, b;
d += ptp_read_u32(d, &a);
d += ptp_read_u32(d, &b);
cur->name = "request object transfer";
cur->code = a;
cur->value = b;
Expand All @@ -541,16 +550,16 @@ int ptp_eos_events(struct PtpRuntime *r, struct PtpGenericEvent **p) {
cur->value = obj->a;
} break;
case PTP_EC_EOS_AvailListChanged: {
uint32_t code = ptp_read_uint32(&d);
uint32_t dat_type = ptp_read_uint32(&d);
uint32_t count = ptp_read_uint32(&d);
uint32_t code, dat_type, count;
d += ptp_read_u32(d, &code);
d += ptp_read_u32(d, &dat_type);
d += ptp_read_u32(d, &count);

int payload_size = (size - 20);

// Make sure to not divide by zero :)
if (payload_size != 0 && count != 0) {
int memb_size = payload_size / count;

ptp_set_prop_avail_info(r, code, memb_size, count, d);
}
} break;
Expand All @@ -574,7 +583,6 @@ int ptp_eos_events_json(struct PtpRuntime *r, char *buffer, int max) {

int curr = osnprintf(buffer, 0, max, "[");
for (int i = 0; i < length; i++) {
// Don't put comma for last entry
char *end = ",";
if (i - 1 == length) end = "";

Expand All @@ -601,12 +609,12 @@ int ptp_eos_events_json(struct PtpRuntime *r, char *buffer, int max) {

// TODO: move to fudge
int ptp_fuji_get_init_info(struct PtpRuntime *r, struct PtpFujiInitResp *resp) {
void *dat = r->data + 12;
uint8_t *d = ptp_get_payload(r);

resp->x1 = ptp_read_uint32(&dat);
resp->x2 = ptp_read_uint32(&dat);
resp->x3 = ptp_read_uint32(&dat);
resp->x4 = ptp_read_uint32(&dat);
d += ptp_read_u32(d, &resp->x1);
d += ptp_read_u32(d, &resp->x2);
d += ptp_read_u32(d, &resp->x3);
d += ptp_read_u32(d, &resp->x4);

ptp_read_unicode_string(resp->cam_name, (char *)(dat), sizeof(resp->cam_name));

Expand All @@ -629,3 +637,4 @@ int ptp_fuji_parse_object_info(struct PtpRuntime *r, struct PtpFujiObjectInfo *o

return 0;
}

2 changes: 1 addition & 1 deletion src/lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ int ptp_send_data(struct PtpRuntime *r, struct PtpCommand *cmd, void *data, int

int ptp_device_type(struct PtpRuntime *r) {
struct PtpDeviceInfo *di = r->di;
if (di == NULL) return PTP_DEV_EMPTY;
if (di == NULL) return PTP_DEV_EMPTY; // TODO: panic?
if (!strcmp(di->manufacturer, "Canon Inc.")) {
if (ptp_check_opcode(r, PTP_OC_EOS_GetStorageIDs)) {
return PTP_DEV_EOS;
Expand Down

0 comments on commit d576994

Please sign in to comment.