Skip to content

Commit

Permalink
ptp2: use new array abstraction for PTPParams->eos_events
Browse files Browse the repository at this point in the history
  • Loading branch information
axxel authored and msmeissn committed Oct 14, 2024
1 parent b443036 commit 84e14fe
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 45 deletions.
10 changes: 4 additions & 6 deletions camlibs/ptp2/library.c
Original file line number Diff line number Diff line change
Expand Up @@ -3218,7 +3218,7 @@ camera_exit (Camera *camera, GPContext *context)
if (camera->pl->checkevents) {
if ((exit_result = ptp_check_eos_events (params)) != PTP_RC_OK)
goto exitfailed;
GP_LOG_D ("missed %d EOS events", params->eos_events_len);
GP_LOG_D ("missed %d EOS events", params->eos_events.len);
camera->pl->checkevents = 0;
}
if (params->inliveview && ptp_operation_issupported(params, PTP_OC_CANON_EOS_TerminateViewfinder))
Expand Down Expand Up @@ -5998,10 +5998,8 @@ camera_trigger_canon_eos_capture (Camera *camera, GPContext *context)
ptp_check_eos_events (params);

/* Discard all collected events before starting the next capture. */
GP_LOG_D("discarding %d EOS events", params->eos_events_len);
free (params->eos_events);
params->eos_events = NULL;
params->eos_events_len = 0;
GP_LOG_D("discarding %d EOS events", params->eos_events.len);
free_array (&params->eos_events);

if (params->eos_camerastatus == 1)
return GP_ERROR_CAMERA_BUSY;
Expand Down Expand Up @@ -6620,7 +6618,7 @@ camera_wait_for_event (Camera *camera, int timeout,

CR (camera_keep_device_on (camera));

if (params->eos_events_len == 0)
if (params->eos_events.len == 0)
C_PTP_REP_MSG (ptp_check_eos_events (params), _("Canon EOS Get Changes failed"));

while (ptp_get_one_eos_event (params, &eos_event)) {
Expand Down
5 changes: 3 additions & 2 deletions camlibs/ptp2/ptp-pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -1729,7 +1729,7 @@ _lookup_or_allocate_canon_prop(PTPParams *params, uint32_t dpc)
} while (0)

static inline int
ptp_unpack_EOS_events (PTPParams *params, const unsigned char* data, unsigned int datasize, PTPCanonEOSEvent **events)
ptp_unpack_EOS_events (PTPParams *params, const unsigned char* data, unsigned int datasize, PTPCanonEOSEvents *events)
{
int i = 0, event_count = 0;
const unsigned char *curdata = data;
Expand Down Expand Up @@ -2540,7 +2540,8 @@ static unsigned int olcsizes[0x15][13] = {
free (e);
e = NULL;
}
*events = e;
events->val = e;
events->len = i;
return i;
#undef INDENT
}
Expand Down
42 changes: 11 additions & 31 deletions camlibs/ptp2/ptp.c
Original file line number Diff line number Diff line change
Expand Up @@ -2130,7 +2130,7 @@ ptp_free_params (PTPParams *params)
ptp_free_devicepropdesc (&params->canon_props[i].dpd);
}
free (params->canon_props);
free (params->eos_events);
free_array (&params->eos_events);

for (i=0;i<params->dpd_cache_len;i++)
ptp_free_devicepropdesc (&params->dpd_cache[i].desc);
Expand Down Expand Up @@ -3312,7 +3312,7 @@ ptp_list_folder_eos (PTPParams *params, uint32_t storage, uint32_t handle) {
if (ret != PTP_RC_OK)
return ret;
} else {
array_append_value(&storageids, storage);
array_push_back(&storageids, storage);
}
last = changed = 0;

Expand Down Expand Up @@ -3824,61 +3824,41 @@ ptp_get_one_event_by_type(PTPParams *params, uint16_t code, PTPContainer *event)
*
**/
uint16_t
ptp_canon_eos_getevent (PTPParams* params, PTPCanonEOSEvent **eos_events, int *eos_events_len)
ptp_canon_eos_getevent (PTPParams* params, PTPCanonEOSEvents *eos_events)
{
PTPContainer ptp;
unsigned char *data = NULL;
unsigned int size;

PTP_CNT_INIT(ptp, PTP_OC_CANON_EOS_GetEvent);
*eos_events_len = 0;
*eos_events = NULL;
CHECK_PTP_RC(ptp_transaction(params, &ptp, PTP_DP_GETDATA, 0, &data, &size));
*eos_events_len = ptp_unpack_EOS_events(params, data, size, eos_events);
ptp_unpack_EOS_events(params, data, size, eos_events);
free (data);
return PTP_RC_OK;
}

uint16_t
ptp_check_eos_events (PTPParams *params)
{
PTPCanonEOSEvent *events = NULL;
int events_len = 0;
PTPCanonEOSEvents events = {0};

while (1) { /* call it repeatedly until the camera does not report any */
CHECK_PTP_RC(ptp_canon_eos_getevent (params, &events, &events_len));
if (!events_len)
CHECK_PTP_RC(ptp_canon_eos_getevent (params, &events));
if (!events.len)
return PTP_RC_OK;

if (params->eos_events_len) {
params->eos_events = realloc(params->eos_events,sizeof(events[0])*(params->eos_events_len+events_len));
if (!params->eos_events)
return PTP_RC_GeneralError;
memcpy (params->eos_events+params->eos_events_len, events, events_len*sizeof(events[0]));
params->eos_events_len += events_len;
free (events);
} else {
params->eos_events = events;
params->eos_events_len = events_len;
}
array_append(&params->eos_events, &events);
}
return PTP_RC_OK;
}

int
ptp_get_one_eos_event (PTPParams *params, PTPCanonEOSEvent *eos_event)
{
if (!params->eos_events_len)
if (!params->eos_events.len)
return 0;
memcpy (eos_event, params->eos_events, sizeof(*eos_event));
if (params->eos_events_len > 1) {
memmove (params->eos_events, params->eos_events+1, sizeof(*eos_event)*(params->eos_events_len-1));
params->eos_events_len--;
} else {
free (params->eos_events);
params->eos_events = NULL;
params->eos_events_len = 0;
}

array_pop_front(&params->eos_events, eos_event);
return 1;
}

Expand Down
42 changes: 36 additions & 6 deletions camlibs/ptp2/ptp.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ static inline uint32_t _post_inc(uint32_t* o, int n)
*
* typedef ARRAY_OF(PTPObject) PTPObjects;
* PTPObjects objects;
* array_append_value(&objects, some_value);
* array_push_back(&objects, some_value);
* for_each(PTPObject*, pobj, objects)
* pobj->call_some_func();
* free_array(&objects);
Expand All @@ -100,11 +100,41 @@ static inline uint32_t _post_inc(uint32_t* o, int n)
(ARRAY)->len = 0; \
} while (0)

#define array_append_value(ARRAY, VAL) do { \
(ARRAY)->val = realloc((ARRAY)->val, ((ARRAY)->len + 1) * sizeof((ARRAY)->val[0])); \
#define array_extend(ARRAY, LEN) do { \
(ARRAY)->val = realloc((ARRAY)->val, ((ARRAY)->len + (LEN)) * sizeof((ARRAY)->val[0])); \
if (!(ARRAY)->val) \
ptp_error(params, "PANIC: realloc failed"); \
} while(0)

#define array_push_back(ARRAY, VAL) do { \
array_extend(ARRAY, 1); \
(ARRAY)->val[(ARRAY)->len++] = VAL; \
} while(0)

#define array_append_copy(DST, SRC) do { \
array_extend(DST, (SRC)->len); \
memcpy((DST)->val + (DST)->len, (SRC)->val, (SRC)->len * sizeof((SRC)->val[0])); \
(DST)->len += (SRC)->len; \
} while(0)

#define array_append(DST, SRC) do { \
if ((DST)->len) { \
array_append_copy(DST, SRC); \
free_array (SRC); \
} else { \
free_array (DST); \
*(DST) = *(SRC); \
} \
} while(0)

#define array_pop_front(ARRAY, VAL) do { \
if ((ARRAY)->len == 0) \
break; \
*VAL = (ARRAY)->val[0]; \
memmove ((ARRAY)->val, (ARRAY)->val + 1, ((ARRAY)->len - 1) * sizeof((ARRAY)->val[0])); \
(ARRAY)->len--; \
} while(0)

#define for_each(TYPE, PTR, ARRAY) \
for (TYPE PTR = ARRAY.val; PTR != ARRAY.val + ARRAY.len; ++PTR)

Expand Down Expand Up @@ -3823,6 +3853,7 @@ typedef void (* PTPDebugFunc) (void *data, const char *format, va_list args)
;

typedef ARRAY_OF(MTPObjectProp) MTPObjectProps;
typedef ARRAY_OF(PTPCanonEOSEvent) PTPCanonEOSEvents;

struct _PTPObject {
uint32_t oid;
Expand Down Expand Up @@ -3955,8 +3986,7 @@ struct _PTPParams {
int canon_event_mode;

/* PTP: Canon EOS event queue */
PTPCanonEOSEvent *eos_events;
unsigned int eos_events_len;
PTPCanonEOSEvents eos_events;
int eos_captureenabled;
int eos_camerastatus;
int eos_uilocked;
Expand Down Expand Up @@ -4445,7 +4475,7 @@ uint16_t ptp_canon_checkevent (PTPParams* params,
#define ptp_canon_eos_setrequestrollingpitchinglevel(params,onoff) ptp_generic_no_data(params,PTP_OC_CANON_EOS_SetRequestRollingPitchingLevel,1,onoff)
uint16_t ptp_canon_eos_getremotemode (PTPParams*, uint32_t *);
uint16_t ptp_canon_eos_capture (PTPParams* params, uint32_t *result);
uint16_t ptp_canon_eos_getevent (PTPParams* params, PTPCanonEOSEvent **events, int *events_len);
uint16_t ptp_canon_eos_getevent (PTPParams* params, PTPCanonEOSEvents *events);
uint16_t ptp_canon_getpartialobject (PTPParams* params, uint32_t handle,
uint32_t offset, uint32_t size,
uint32_t pos, unsigned char** block,
Expand Down

0 comments on commit 84e14fe

Please sign in to comment.