blob: dc68f7c78abdf23e6620d8244291113cee289db5 [file] [log] [blame]
/* ptp.c
*
* Copyright (C) 2001-2004 Mariusz Woloszyn <emsi@ipartners.pl>
* Copyright (C) 2003-2012 Marcus Meissner <marcus@jet.franken.de>
* Copyright (C) 2006-2008 Linus Walleij <triad@df.lth.se>
* Copyright (C) 2007 Tero Saarni <tero.saarni@gmail.com>
* Copyright (C) 2009 Axel Waggershauser <awagger@web.de>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#define _BSD_SOURCE
#include "config.h"
#include "ptp.h"
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#ifdef ENABLE_NLS
# include <libintl.h>
# undef _
# define _(String) dgettext (PACKAGE, String)
# ifdef gettext_noop
# define N_(String) gettext_noop (String)
# else
# define N_(String) (String)
# endif
#else
# define textdomain(String) (String)
# define gettext(String) (String)
# define dgettext(Domain,Message) (Message)
# define dcgettext(Domain,Message,Type) (Message)
# define bindtextdomain(Domain,Directory) (Domain)
# define _(String) (String)
# define N_(String) (String)
#endif
#define CHECK_PTP_RC(result) {uint16_t r=(result); if (r!=PTP_RC_OK) return r;}
#define PTP_CNT_INIT(cnt) {memset(&cnt,0,sizeof(cnt));}
static uint16_t ptp_exit_recv_memory_handler (PTPDataHandler*,unsigned char**,unsigned long*);
static uint16_t ptp_init_recv_memory_handler(PTPDataHandler*);
static uint16_t ptp_init_send_memory_handler(PTPDataHandler*,unsigned char*,unsigned long len);
static uint16_t ptp_exit_send_memory_handler (PTPDataHandler *handler);
void
ptp_debug (PTPParams *params, const char *format, ...)
{
va_list args;
va_start (args, format);
if (params->debug_func!=NULL)
params->debug_func (params->data, format, args);
else
{
vfprintf (stderr, format, args);
fprintf (stderr,"\n");
fflush (stderr);
}
va_end (args);
}
void
ptp_error (PTPParams *params, const char *format, ...)
{
va_list args;
va_start (args, format);
if (params->error_func!=NULL)
params->error_func (params->data, format, args);
else
{
vfprintf (stderr, format, args);
fprintf (stderr,"\n");
fflush (stderr);
}
va_end (args);
}
/* Pack / unpack functions */
#include "ptp-pack.c"
/* major PTP functions */
/* Transaction data phase description */
#define PTP_DP_NODATA 0x0000 /* no data phase */
#define PTP_DP_SENDDATA 0x0001 /* sending data */
#define PTP_DP_GETDATA 0x0002 /* receiving data */
#define PTP_DP_DATA_MASK 0x00ff /* data phase mask */
/**
* ptp_transaction:
* params: PTPParams*
* PTPContainer* ptp - general ptp container
* uint16_t flags - lower 8 bits - data phase description
* unsigned int sendlen - senddata phase data length
* char** data - send or receive data buffer pointer
* int* recvlen - receive data length
*
* Performs PTP transaction. ptp is a PTPContainer with appropriate fields
* filled in (i.e. operation code and parameters). It's up to caller to do
* so.
* The flags decide thether the transaction has a data phase and what is its
* direction (send or receive).
* If transaction is sending data the sendlen should contain its length in
* bytes, otherwise it's ignored.
* The data should contain an address of a pointer to data going to be sent
* or is filled with such a pointer address if data are received depending
* od dataphase direction (send or received) or is beeing ignored (no
* dataphase).
* The memory for a pointer should be preserved by the caller, if data are
* beeing retreived the appropriate amount of memory is beeing allocated
* (the caller should handle that!).
*
* Return values: Some PTP_RC_* code.
* Upon success PTPContainer* ptp contains PTP Response Phase container with
* all fields filled in.
**/
static uint16_t
ptp_transaction_new (PTPParams* params, PTPContainer* ptp,
uint16_t flags, unsigned int sendlen,
PTPDataHandler *handler
) {
int tries;
uint16_t cmd;
if ((params==NULL) || (ptp==NULL))
return PTP_ERROR_BADPARAM;
cmd = ptp->Code;
ptp->Transaction_ID=params->transaction_id++;
ptp->SessionID=params->session_id;
/* send request */
CHECK_PTP_RC(params->sendreq_func (params, ptp));
/* is there a dataphase? */
switch (flags&PTP_DP_DATA_MASK) {
case PTP_DP_SENDDATA:
{
uint16_t ret;
ret = params->senddata_func(params, ptp,
sendlen, handler);
if (ret == PTP_ERROR_CANCEL) {
ret = params->cancelreq_func(params,
params->transaction_id-1);
if (ret == PTP_RC_OK)
ret = PTP_ERROR_CANCEL;
}
if (ret != PTP_RC_OK)
return ret;
}
break;
case PTP_DP_GETDATA:
{
uint16_t ret;
ret = params->getdata_func(params, ptp, handler);
if (ret == PTP_ERROR_CANCEL) {
ret = params->cancelreq_func(params,
params->transaction_id-1);
if (ret == PTP_RC_OK)
ret = PTP_ERROR_CANCEL;
}
if (ret != PTP_RC_OK)
return ret;
}
break;
case PTP_DP_NODATA:
break;
default:
return PTP_ERROR_BADPARAM;
}
tries = 3;
while (tries--) {
uint16_t ret;
/* get response */
ret = params->getresp_func(params, ptp);
if (ret == PTP_ERROR_RESP_EXPECTED) {
ptp_debug (params,"PTP: response expected but not got, retrying.");
tries++;
continue;
}
if (ret != PTP_RC_OK)
return ret;
if (ptp->Transaction_ID < params->transaction_id-1) {
tries++;
ptp_debug (params,
"PTP: Sequence number mismatch %d vs expected %d, suspecting old reply.",
ptp->Transaction_ID, params->transaction_id-1
);
continue;
}
if (ptp->Transaction_ID != params->transaction_id-1) {
/* try to clean up potential left overs from previous session */
if ((cmd == PTP_OC_OpenSession) && tries)
continue;
ptp_error (params,
"PTP: Sequence number mismatch %d vs expected %d.",
ptp->Transaction_ID, params->transaction_id-1
);
return PTP_ERROR_BADPARAM;
}
break;
}
return ptp->Code;
}
/* memory data get/put handler */
typedef struct {
unsigned char *data;
unsigned long size, curoff;
} PTPMemHandlerPrivate;
static uint16_t
memory_getfunc(PTPParams* params, void* private,
unsigned long wantlen, unsigned char *data,
unsigned long *gotlen
) {
PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)private;
unsigned long tocopy = wantlen;
if (priv->curoff + tocopy > priv->size)
tocopy = priv->size - priv->curoff;
memcpy (data, priv->data + priv->curoff, tocopy);
priv->curoff += tocopy;
*gotlen = tocopy;
return PTP_RC_OK;
}
static uint16_t
memory_putfunc(PTPParams* params, void* private,
unsigned long sendlen, unsigned char *data,
unsigned long *putlen
) {
PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)private;
if (priv->curoff + sendlen > priv->size) {
priv->data = realloc (priv->data, priv->curoff+sendlen);
if (!priv->data)
return PTP_RC_GeneralError;
priv->size = priv->curoff + sendlen;
}
memcpy (priv->data + priv->curoff, data, sendlen);
priv->curoff += sendlen;
*putlen = sendlen;
return PTP_RC_OK;
}
/* init private struct for receiving data. */
static uint16_t
ptp_init_recv_memory_handler(PTPDataHandler *handler) {
PTPMemHandlerPrivate* priv;
priv = malloc (sizeof(PTPMemHandlerPrivate));
if (!priv)
return PTP_RC_GeneralError;
handler->priv = priv;
handler->getfunc = memory_getfunc;
handler->putfunc = memory_putfunc;
priv->data = NULL;
priv->size = 0;
priv->curoff = 0;
return PTP_RC_OK;
}
/* init private struct and put data in for sending data.
* data is still owned by caller.
*/
static uint16_t
ptp_init_send_memory_handler(PTPDataHandler *handler,
unsigned char *data, unsigned long len
) {
PTPMemHandlerPrivate* priv;
priv = malloc (sizeof(PTPMemHandlerPrivate));
if (!priv)
return PTP_RC_GeneralError;
handler->priv = priv;
handler->getfunc = memory_getfunc;
handler->putfunc = memory_putfunc;
priv->data = data;
priv->size = len;
priv->curoff = 0;
return PTP_RC_OK;
}
/* free private struct + data */
static uint16_t
ptp_exit_send_memory_handler (PTPDataHandler *handler) {
PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)handler->priv;
/* data is owned by caller */
free (priv);
return PTP_RC_OK;
}
/* hand over our internal data to caller */
static uint16_t
ptp_exit_recv_memory_handler (PTPDataHandler *handler,
unsigned char **data, unsigned long *size
) {
PTPMemHandlerPrivate* priv = (PTPMemHandlerPrivate*)handler->priv;
*data = priv->data;
*size = priv->size;
free (priv);
return PTP_RC_OK;
}
/* fd data get/put handler */
typedef struct {
int fd;
} PTPFDHandlerPrivate;
static uint16_t
fd_getfunc(PTPParams* params, void* private,
unsigned long wantlen, unsigned char *data,
unsigned long *gotlen
) {
PTPFDHandlerPrivate* priv = (PTPFDHandlerPrivate*)private;
int got;
got = read (priv->fd, data, wantlen);
if (got != -1)
*gotlen = got;
else
return PTP_RC_GeneralError;
return PTP_RC_OK;
}
static uint16_t
fd_putfunc(PTPParams* params, void* private,
unsigned long sendlen, unsigned char *data,
unsigned long *putlen
) {
int written;
PTPFDHandlerPrivate* priv = (PTPFDHandlerPrivate*)private;
written = write (priv->fd, data, sendlen);
if (written != -1)
*putlen = written;
else
return PTP_RC_GeneralError;
return PTP_RC_OK;
}
static uint16_t
ptp_init_fd_handler(PTPDataHandler *handler, int fd) {
PTPFDHandlerPrivate* priv;
priv = malloc (sizeof(PTPFDHandlerPrivate));
if (!priv)
return PTP_RC_GeneralError;
handler->priv = priv;
handler->getfunc = fd_getfunc;
handler->putfunc = fd_putfunc;
priv->fd = fd;
return PTP_RC_OK;
}
static uint16_t
ptp_exit_fd_handler (PTPDataHandler *handler) {
PTPFDHandlerPrivate* priv = (PTPFDHandlerPrivate*)handler->priv;
free (priv);
return PTP_RC_OK;
}
/* Old style transaction, based on memory */
static uint16_t
ptp_transaction (PTPParams* params, PTPContainer* ptp,
uint16_t flags, unsigned int sendlen,
unsigned char **data, unsigned int *recvlen
) {
PTPDataHandler handler;
uint16_t ret;
switch (flags & PTP_DP_DATA_MASK) {
case PTP_DP_SENDDATA:
ptp_init_send_memory_handler (&handler, *data, sendlen);
break;
case PTP_DP_GETDATA:
ptp_init_recv_memory_handler (&handler);
break;
default:break;
}
ret = ptp_transaction_new (params, ptp, flags, sendlen, &handler);
switch (flags & PTP_DP_DATA_MASK) {
case PTP_DP_SENDDATA:
ptp_exit_send_memory_handler (&handler);
break;
case PTP_DP_GETDATA: {
unsigned long len;
ptp_exit_recv_memory_handler (&handler, data, &len);
if (recvlen)
*recvlen = len;
break;
}
default:break;
}
return ret;
}
/**
* PTP operation functions
*
* all ptp_ functions should take integer parameters
* in host byte order!
**/
/**
* ptp_getdeviceinfo:
* params: PTPParams*
*
* Gets device info dataset and fills deviceinfo structure.
*
* Return values: Some PTP_RC_* code.
**/
uint16_t
ptp_getdeviceinfo (PTPParams* params, PTPDeviceInfo* deviceinfo)
{
uint16_t ret;
unsigned long len;
PTPContainer ptp;
unsigned char* di=NULL;
PTPDataHandler handler;
ptp_init_recv_memory_handler (&handler);
PTP_CNT_INIT(ptp);
ptp.Code=PTP_OC_GetDeviceInfo;
ptp.Nparam=0;
len=0;
ret=ptp_transaction_new(params, &ptp, PTP_DP_GETDATA, 0, &handler);
ptp_exit_recv_memory_handler (&handler, &di, &len);
if (!di) ret = PTP_RC_GeneralError;
if (ret == PTP_RC_OK) ptp_unpack_DI(params, di, deviceinfo, len);
free(di);
return ret;
}
uint16_t
ptp_canon_eos_getdeviceinfo (PTPParams* params, PTPCanonEOSDeviceInfo*di)
{
uint16_t ret;
PTPContainer ptp;
PTPDataHandler handler;
unsigned long len;
unsigned char *data;
ptp_init_recv_memory_handler (&handler);
PTP_CNT_INIT(ptp);
ptp.Code=PTP_OC_CANON_EOS_GetDeviceInfoEx;
ptp.Nparam=0;
len=0;
data=NULL;
ret=ptp_transaction_new(params, &ptp, PTP_DP_GETDATA, 0, &handler);
ptp_exit_recv_memory_handler (&handler, &data, &len);
if (ret == PTP_RC_OK) ptp_unpack_EOS_DI(params, data, di, len);
free (data);
return ret;
}
/**
* ptp_generic_no_data:
* params: PTPParams*
* code PTP OP Code
* n_param count of parameters
* ... variable argument list ...
*
* Emits a generic PTP command without any data transfer.
*
* Return values: Some PTP_RC_* code.
**/
uint16_t
ptp_generic_no_data (PTPParams* params, uint16_t code, unsigned int n_param, ...)
{
PTPContainer ptp;
va_list args;
int i;
if( n_param > 5 )
return PTP_RC_InvalidParameter;
PTP_CNT_INIT(ptp);
ptp.Code=code;
ptp.Nparam=n_param;
va_start(args, n_param);
for( i=0; i<n_param; ++i )
(&ptp.Param1)[i] = va_arg(args, uint32_t);
va_end(args);
return ptp_transaction(params, &ptp, PTP_DP_NODATA, 0, NULL, NULL);
}
/**
* ptp_opensession:
* params: PTPParams*
* session - session number
*
* Establishes a new session.
*
* Return values: Some PTP_RC_* code.
**/
uint16_t
ptp_opensession (PTPParams* params, uint32_t session)
{
uint16_t ret;
PTPContainer ptp;
ptp_debug(params,"PTP: Opening session");
/* SessonID field of the operation dataset should always
be set to 0 for OpenSession request! */
params->session_id=0x00000000;
/* TransactionID should be set to 0 also! */
params->transaction_id=0x0000000;
/* zero out response packet buffer */
params->response_packet = NULL;
params->response_packet_size = 0;
/* no split headers */
params->split_header_data = 0;
PTP_CNT_INIT(ptp);
ptp.Code=PTP_OC_OpenSession;
ptp.Param1=session;
ptp.Nparam=1;
ret=ptp_transaction_new(params, &ptp, PTP_DP_NODATA, 0, NULL);
/* now set the global session id to current session number */
params->session_id=session;
return ret;
}
/**
* ptp_free_params:
* params: PTPParams*
*
* Frees all data within the PTPParams struct.
*
* Return values: Some PTP_RC_* code.
**/
void
ptp_free_params (PTPParams *params) {
int i;
if (params->cameraname) free (params->cameraname);
if (params->wifi_profiles) free (params->wifi_profiles);
for (i=0;i<params->nrofobjects;i++)
ptp_free_object (&params->objects[i]);
free (params->objects);
free (params->events);
for (i=0;i<params->nrofcanon_props;i++) {
free (params->canon_props[i].data);
ptp_free_devicepropdesc (&params->canon_props[i].dpd);
}
free (params->canon_props);
free (params->backlogentries);
ptp_free_DI (&params->deviceinfo);
}
/**
* ptp_getststorageids:
* params: PTPParams*
*
* Gets array of StorageIDs and fills the storageids structure.
*
* Return values: Some PTP_RC_* code.
**/
uint16_t
ptp_getstorageids (PTPParams* params, PTPStorageIDs* storageids)
{
uint16_t ret;
PTPContainer ptp;
unsigned int len;
unsigned char* sids=NULL;
PTP_CNT_INIT(ptp);
ptp.Code=PTP_OC_GetStorageIDs;
ptp.Nparam=0;
len=0;
ret=ptp_transaction(params, &ptp, PTP_DP_GETDATA, 0, &sids, &len);
if (ret == PTP_RC_OK) ptp_unpack_SIDs(params, sids, storageids, len);
free(sids);
return ret;
}
/**
* ptp_getststorageinfo:
* params: PTPParams*
* storageid - StorageID
*
* Gets StorageInfo dataset of desired storage and fills storageinfo
* structure.
*
* Return values: Some PTP_RC_* code.
**/
uint16_t
ptp_getstorageinfo (PTPParams* params, uint32_t storageid,
PTPStorageInfo* storageinfo)
{
uint16_t ret;
PTPContainer ptp;
unsigned char* si=NULL;
unsigned int len;
PTP_CNT_INIT(ptp);
ptp.Code=PTP_OC_GetStorageInfo;
ptp.Param1=storageid;
ptp.Nparam=1;
len=0;
ret=ptp_transaction(params, &ptp, PTP_DP_GETDATA, 0, &si, &len);
if (ret == PTP_RC_OK) ptp_unpack_SI(params, si, storageinfo, len);
free(si);
return ret;
}
/**
* ptp_getobjecthandles:
* params: PTPParams*
* storage - StorageID
* objectformatcode - ObjectFormatCode (optional)
* associationOH - ObjectHandle of Association for
* wich a list of children is desired
* (optional)
* objecthandles - pointer to structute
*
* Fills objecthandles with structure returned by device.
*
* Return values: Some PTP_RC_* code.
**/
uint16_t
ptp_getobjecthandles (PTPParams* params, uint32_t storage,
uint32_t objectformatcode, uint32_t associationOH,
PTPObjectHandles* objecthandles)
{
uint16_t ret;
PTPContainer ptp;
unsigned char* oh=NULL;
unsigned int len;
PTP_CNT_INIT(ptp);
ptp.Code=PTP_OC_GetObjectHandles;
ptp.Param1=storage;
ptp.Param2=objectformatcode;
ptp.Param3=associationOH;
ptp.Nparam=3;
len=0;
ret=ptp_transaction(params, &ptp, PTP_DP_GETDATA, 0, &oh, &len);
if (ret == PTP_RC_OK) {
ptp_unpack_OH(params, oh, objecthandles, len);
} else {
if ( (storage == 0xffffffff) &&
(objectformatcode == 0) &&
(associationOH == 0)
) {
/* When we query all object handles on all stores and
* get an error -> just handle it as "0 handles".
*/
objecthandles->Handler = NULL;
objecthandles->n = 0;
ret = PTP_RC_OK;
}
}
free(oh);
return ret;
}
uint16_t
ptp_getfilesystemmanifest (PTPParams* params, uint32_t storage,
uint32_t objectformatcode, uint32_t associationOH,
unsigned char** data)
{
uint16_t ret;
PTPContainer ptp;
unsigned int len;
PTP_CNT_INIT(ptp);
ptp.Code=PTP_OC_GetFilesystemManifest;
ptp.Param1=storage;
ptp.Param2=objectformatcode;
ptp.Param3=associationOH;
ptp.Nparam=3;
len=0;
ret=ptp_transaction(params, &ptp, PTP_DP_GETDATA, 0, data, &len);
return ret;
}
/**
* ptp_getnumobjects:
* params: PTPParams*
* storage - StorageID
* objectformatcode - ObjectFormatCode (optional)
* associationOH - ObjectHandle of Association for
* wich a list of children is desired
* (optional)
* numobs - pointer to uint32_t that takes number of objects
*
* Fills numobs with number of objects on device.
*
* Return values: Some PTP_RC_* code.
**/
uint16_t
ptp_getnumobjects (PTPParams* params, uint32_t storage,
uint32_t objectformatcode, uint32_t associationOH,
uint32_t* numobs)
{
uint16_t ret;
PTPContainer ptp;
PTP_CNT_INIT(ptp);
ptp.Code=PTP_OC_GetNumObjects;
ptp.Param1=storage;
ptp.Param2=objectformatcode;
ptp.Param3=associationOH;
ptp.Nparam=3;
ret=ptp_transaction(params, &ptp, PTP_DP_NODATA, 0, NULL, NULL);
if (ret == PTP_RC_OK) {
if (ptp.Nparam >= 1)
*numobs = ptp.Param1;
else
ret = PTP_RC_GeneralError;
}
return ret;
}
/**
* ptp_eos_bulbstart:
* params: PTPParams*
*
* Starts EOS Bulb capture.
*
* Return values: Some PTP_RC_* code.
**/
uint16_t
ptp_canon_eos_bulbstart (PTPParams* params)
{
uint16_t ret;
PTPContainer ptp;
PTP_CNT_INIT(ptp);
ptp.Code = PTP_OC_CANON_EOS_BulbStart;
ptp.Nparam = 0;
ret = ptp_transaction(params, &ptp, PTP_DP_NODATA, 0, NULL, NULL);
if ((ret == PTP_RC_OK) && (ptp.Nparam >= 1) && ((ptp.Param1 & 0x7000) == 0x2000))
ret = ptp.Param1;
return ret;
}
/**
* ptp_eos_capture:
* params: PTPParams*
* uint32_t* result
*
* This starts a EOS400D style capture. You have to use the
* get_eos_events to find out what resulted.
* The return value is "0" for all OK, and "1" for capture failed. (not fully confirmed)
*
* Return values: Some PTP_RC_* code.
**/
uint16_t
ptp_canon_eos_capture (PTPParams* params, uint32_t *result)
{
uint16_t ret;
PTPContainer ptp;
PTP_CNT_INIT(ptp);
ptp.Code = PTP_OC_CANON_EOS_RemoteRelease;
ptp.Nparam = 0;
*result = 0;
ret = ptp_transaction(params, &ptp, PTP_DP_NODATA, 0, NULL, NULL);
if ((ret == PTP_RC_OK) && (ptp.Nparam >= 1))
*result = ptp.Param1;
return ret;
}
/**
* ptp_canon_eos_bulbend:
* params: PTPParams*
*
* Starts EOS Bulb capture.
*
* Return values: Some PTP_RC_* code.
**/
uint16_t
ptp_canon_eos_bulbend (PTPParams* params)
{
uint16_t ret;
PTPContainer ptp;
PTP_CNT_INIT(ptp);
ptp.Code = PTP_OC_CANON_EOS_BulbEnd;
ptp.Nparam = 0;
ret = ptp_transaction(params, &ptp, PTP_DP_NODATA, 0, NULL, NULL);
if ((ret == PTP_RC_OK) && (ptp.Nparam >= 1) && ((ptp.Param1 & 0x7000) == 0x2000))
ret = ptp.Param1;
return ret;
}
/**
* ptp_getobjectinfo:
* params: PTPParams*
* handle - Object handle
* objectinfo - pointer to objectinfo that is returned
*
* Get objectinfo structure for handle from device.
*
* Return values: Some PTP_RC_* code.
**/
uint16_t
ptp_getobjectinfo (PTPParams* params, uint32_t handle,
PTPObjectInfo* objectinfo)
{
uint16_t ret;
PTPContainer ptp;
unsigned char* oi=NULL;
unsigned int len;
PTP_CNT_INIT(ptp);
ptp.Code=PTP_OC_GetObjectInfo;
ptp.Param1=handle;
ptp.Nparam=1;
len=0;
ret=ptp_transaction(params, &ptp, PTP_DP_GETDATA, 0, &oi, &len);
if (ret == PTP_RC_OK) ptp_unpack_OI(params, oi, objectinfo, len);
free(oi);
return ret;
}
/**
* ptp_getobject:
* params: PTPParams*
* handle - Object handle
* object - pointer to data area
*
* Get object 'handle' from device and store the data in newly
* allocated 'object'.
*
* Return values: Some PTP_RC_* code.
**/
uint16_t
ptp_getobject (PTPParams* params, uint32_t handle, unsigned char** object)
{
PTPContainer ptp;
unsigned int len;
PTP_CNT_INIT(ptp);
ptp.Code=PTP_OC_GetObject;
ptp.Param1=handle;
ptp.Nparam=1;
len=0;
return ptp_transaction(params, &ptp, PTP_DP_GETDATA, 0, object, &len);
}
/**
* ptp_getobject_to_handler:
* params: PTPParams*
* handle - Object handle
* PTPDataHandler* - pointer datahandler
*
* Get object 'handle' from device and store the data in newly
* allocated 'object'.
*
* Return values: Some PTP_RC_* code.
**/
uint16_t
ptp_getobject_to_handler (PTPParams* params, uint32_t handle, PTPDataHandler *handler)
{
PTPContainer ptp;
PTP_CNT_INIT(ptp);
ptp.Code=PTP_OC_GetObject;
ptp.Param1=handle;
ptp.Nparam=1;
return ptp_transaction_new(params, &ptp, PTP_DP_GETDATA, 0, handler);
}
/**
* ptp_getobject_tofd:
* params: PTPParams*
* handle - Object handle
* fd - File descriptor to write() to
*
* Get object 'handle' from device and write the data to the
* given file descriptor.
*
* Return values: Some PTP_RC_* code.
**/
uint16_t
ptp_getobject_tofd (PTPParams* params, uint32_t handle, int fd)
{
PTPContainer ptp;
PTPDataHandler handler;
uint16_t ret;
ptp_init_fd_handler (&handler, fd);
PTP_CNT_INIT(ptp);
ptp.Code=PTP_OC_GetObject;
ptp.Param1=handle;
ptp.Nparam=1;
ret = ptp_transaction_new(params, &ptp, PTP_DP_GETDATA, 0, &handler);
ptp_exit_fd_handler (&handler);
return ret;
}
/**
* ptp_getpartialobject:
* params: PTPParams*
* handle - Object handle
* offset - Offset into object
* maxbytes - Maximum of bytes to read
* object - pointer to data area
* len - pointer to returned length
*
* Get object 'handle' from device and store the data in newly
* allocated 'object'. Start from offset and read at most maxbytes.
*
* Return values: Some PTP_RC_* code.
**/
uint16_t
ptp_getpartialobject (PTPParams* params, uint32_t handle, uint32_t offset,
uint32_t maxbytes, unsigned char** object,
uint32_t *len)
{
PTPContainer ptp;
PTP_CNT_INIT(ptp);
ptp.Code=PTP_OC_GetPartialObject;
ptp.Param1=handle;
ptp.Param2=offset;
ptp.Param3=maxbytes;
ptp.Nparam=3;
*len=0;
return ptp_transaction(params, &ptp, PTP_DP_GETDATA, 0, object, len);
}
/**
* ptp_getthumb:
* params: PTPParams*
* handle - Object handle
* object - pointer to data area
*
* Get thumb for object 'handle' from device and store the data in newly
* allocated 'object'.
*
* Return values: Some PTP_RC_* code.
**/
uint16_t
ptp_getthumb (PTPParams* params, uint32_t handle, unsigned char** object, unsigned int *len)
{
PTPContainer ptp;
PTP_CNT_INIT(ptp);
ptp.Code=PTP_OC_GetThumb;
ptp.Param1=handle;
ptp.Nparam=1;
*len = 0;
return ptp_transaction(params, &ptp, PTP_DP_GETDATA, 0, object, len);
}
/**
* ptp_deleteobject:
* params: PTPParams*
* handle - object handle
* ofc - object format code (optional)
*
* Deletes desired objects.
*
* Return values: Some PTP_RC_* code.
**/
uint16_t
ptp_deleteobject (PTPParams* params, uint32_t handle, uint32_t ofc)
{
PTPContainer ptp;
uint16_t ret;
PTP_CNT_INIT(ptp);
ptp.Code=PTP_OC_DeleteObject;
ptp.Param1=handle;
ptp.Param2=ofc;
ptp.Nparam=2;
ret = ptp_transaction(params, &ptp, PTP_DP_NODATA, 0, NULL, NULL);
if (ret != PTP_RC_OK) {
return ret;
}
/* If the object is cached and could be removed, cleanse cache. */
ptp_remove_object_from_cache(params, handle);
return PTP_RC_OK;
}
/**
* ptp_sendobjectinfo:
* params: PTPParams*
* uint32_t* store - destination StorageID on Responder
* uint32_t* parenthandle - Parent ObjectHandle on responder
* uint32_t* handle - see Return values
* PTPObjectInfo* objectinfo- ObjectInfo that is to be sent
*
* Sends ObjectInfo of file that is to be sent via SendFileObject.
*
* Return values: Some PTP_RC_* code.
* Upon success : uint32_t* store - Responder StorageID in which
* object will be stored
* uint32_t* parenthandle- Responder Parent ObjectHandle
* in which the object will be stored
* uint32_t* handle - Responder's reserved ObjectHandle
* for the incoming object
**/
uint16_t
ptp_sendobjectinfo (PTPParams* params, uint32_t* store,
uint32_t* parenthandle, uint32_t* handle,
PTPObjectInfo* objectinfo)
{
uint16_t ret;
PTPContainer ptp;
unsigned char* oidata=NULL;
uint32_t size;
PTP_CNT_INIT(ptp);
ptp.Code=PTP_OC_SendObjectInfo;
ptp.Param1=*store;
ptp.Param2=*parenthandle;
ptp.Nparam=2;
size=ptp_pack_OI(params, objectinfo, &oidata);
ret = ptp_transaction(params, &ptp, PTP_DP_SENDDATA, size, &oidata, NULL);
free(oidata);
*store=ptp.Param1;
*parenthandle=ptp.Param2;
*handle=ptp.Param3;
return ret;
}
/**
* ptp_sendobject:
* params: PTPParams*
* char* object - contains the object that is to be sent
* uint32_t size - object size
*
* Sends object to Responder.
*
* Return values: Some PTP_RC_* code.
*
*/
uint16_t
ptp_sendobject (PTPParams* params, unsigned char* object, uint32_t size)
{
PTPContainer ptp;
PTP_CNT_INIT(ptp);
ptp.Code=PTP_OC_SendObject;
ptp.Nparam=0;
return ptp_transaction(params, &ptp, PTP_DP_SENDDATA, size, &object, NULL);
}
/**
* ptp_sendobject_from_handler:
* params: PTPParams*
* PTPDataHandler* - File descriptor to read() object from
* uint32_t size - File/object size
*
* Sends object from file descriptor by consecutive reads from this
* descriptor.
*
* Return values: Some PTP_RC_* code.
**/
uint16_t
ptp_sendobject_from_handler (PTPParams* params, PTPDataHandler *handler, uint32_t size)
{
PTPContainer ptp;
PTP_CNT_INIT(ptp);
ptp.Code=PTP_OC_SendObject;
ptp.Nparam=0;
return ptp_transaction_new(params, &ptp, PTP_DP_SENDDATA, size, handler);
}
/**
* ptp_sendobject_fromfd:
* params: PTPParams*
* fd - File descriptor to read() object from
* uint32_t size - File/object size
*
* Sends object from file descriptor by consecutive reads from this
* descriptor.
*
* Return values: Some PTP_RC_* code.
**/
uint16_t
ptp_sendobject_fromfd (PTPParams* params, int fd, uint32_t size)
{
PTPContainer ptp;
PTPDataHandler handler;
uint16_t ret;
ptp_init_fd_handler (&handler, fd);
PTP_CNT_INIT(ptp);
ptp.Code=PTP_OC_SendObject;
ptp.Nparam=0;
ret = ptp_transaction_new(params, &ptp, PTP_DP_SENDDATA, size, &handler);
ptp_exit_fd_handler (&handler);
return ret;
}
uint16_t
ptp_getdevicepropdesc (PTPParams* params, uint16_t propcode,
PTPDevicePropDesc* devicepropertydesc)
{
PTPContainer ptp;
uint16_t ret;
unsigned int len;
unsigned char* dpd=NULL;
PTP_CNT_INIT(ptp);
ptp.Code=PTP_OC_GetDevicePropDesc;
ptp.Param1=propcode;
ptp.Nparam=1;
len=0;
ret=ptp_transaction(params, &ptp, PTP_DP_GETDATA, 0, &dpd, &len);
if (ret == PTP_RC_OK) ptp_unpack_DPD(params, dpd, devicepropertydesc, len);
free(dpd);
return ret;
}
uint16_t
ptp_getdevicepropvalue (PTPParams* params, uint16_t propcode,
PTPPropertyValue* value, uint16_t datatype)
{
PTPContainer ptp;
uint16_t ret;
unsigned int len;
int offset;
unsigned char* dpv=NULL;
PTP_CNT_INIT(ptp);
ptp.Code=PTP_OC_GetDevicePropValue;
ptp.Param1=propcode;
ptp.Nparam=1;
len=offset=0;
ret=ptp_transaction(params, &ptp, PTP_DP_GETDATA, 0, &dpv, &len);
if (ret == PTP_RC_OK) ptp_unpack_DPV(params, dpv, &offset, len, value, datatype);
free(dpv);
return ret;
}
uint16_t
ptp_setdevicepropvalue (PTPParams* params, uint16_t propcode,
PTPPropertyValue *value, uint16_t datatype)
{
PTPContainer ptp;
uint16_t ret;
uint32_t size;
unsigned char* dpv=NULL;
PTP_CNT_INIT(ptp);
ptp.Code=PTP_OC_SetDevicePropValue;
ptp.Param1=propcode;
ptp.Nparam=1;
size=ptp_pack_DPV(params, value, &dpv, datatype);
ret=ptp_transaction(params, &ptp, PTP_DP_SENDDATA, size, &dpv, NULL);
free(dpv);
return ret;
}
/**
* ptp_ek_sendfileobjectinfo:
* params: PTPParams*
* uint32_t* store - destination StorageID on Responder
* uint32_t* parenthandle - Parent ObjectHandle on responder
* uint32_t* handle - see Return values
* PTPObjectInfo* objectinfo- ObjectInfo that is to be sent
*
* Sends ObjectInfo of file that is to be sent via SendFileObject.
*
* Return values: Some PTP_RC_* code.
* Upon success : uint32_t* store - Responder StorageID in which
* object will be stored
* uint32_t* parenthandle- Responder Parent ObjectHandle
* in which the object will be stored
* uint32_t* handle - Responder's reserved ObjectHandle
* for the incoming object
**/
uint16_t
ptp_ek_sendfileobjectinfo (PTPParams* params, uint32_t* store,
uint32_t* parenthandle, uint32_t* handle,
PTPObjectInfo* objectinfo)
{
uint16_t ret;
PTPContainer ptp;
unsigned char* oidata=NULL;
uint32_t size;
PTP_CNT_INIT(ptp);
ptp.Code=PTP_OC_EK_SendFileObjectInfo;
ptp.Param1=*store;
ptp.Param2=*parenthandle;
ptp.Nparam=2;
size=ptp_pack_OI(params, objectinfo, &oidata);
ret=ptp_transaction(params, &ptp, PTP_DP_SENDDATA, size, &oidata, NULL);
free(oidata);
*store=ptp.Param1;
*parenthandle=ptp.Param2;
*handle=ptp.Param3;
return ret;
}
/**
* ptp_ek_getserial:
* params: PTPParams*
* char** serial - contains the serial number of the camera
* uint32_t* size - contains the string length
*
* Gets the serial number from the device. (ptp serial)
*
* Return values: Some PTP_RC_* code.
*
*/
uint16_t
ptp_ek_getserial (PTPParams* params, unsigned char **data, unsigned int *size)
{
PTPContainer ptp;
PTP_CNT_INIT(ptp);
ptp.Code = PTP_OC_EK_GetSerial;
ptp.Nparam = 0;
return ptp_transaction(params, &ptp, PTP_DP_GETDATA, 0, data, size);
}
/**
* ptp_ek_setserial:
* params: PTPParams*
* char* serial - contains the new serial number
* uint32_t size - string length
*
* Sets the serial number of the device. (ptp serial)
*
* Return values: Some PTP_RC_* code.
*
*/
uint16_t
ptp_ek_setserial (PTPParams* params, unsigned char *data, unsigned int size)
{
PTPContainer ptp;
PTP_CNT_INIT(ptp);
ptp.Code = PTP_OC_EK_SetSerial;
ptp.Nparam = 0;
return ptp_transaction(params, &ptp, PTP_DP_SENDDATA, size, &data, NULL);
}
/* unclear what it does yet */
uint16_t
ptp_ek_9007 (PTPParams* params, unsigned char **data, unsigned int *size)
{
PTPContainer ptp;
PTP_CNT_INIT(ptp);
ptp.Code = 0x9007;
ptp.Nparam = 0;
return ptp_transaction(params, &ptp, PTP_DP_GETDATA, 0, data, size);
}
/* unclear what it does yet */
uint16_t
ptp_ek_9009 (PTPParams* params, uint32_t *p1, uint32_t *p2)
{
PTPContainer ptp;
uint16_t ret;
PTP_CNT_INIT(ptp);
ptp.Code = 0x9009;
ptp.Nparam = 0;
ret = ptp_transaction(params, &ptp, PTP_DP_NODATA, 0, NULL, NULL);
*p1 = ptp.Param1;
*p2 = ptp.Param2;
return ret;
}
/* unclear yet, but I guess it returns the info from 9008 */
uint16_t
ptp_ek_900c (PTPParams* params, unsigned char **data, unsigned int *size)
{
PTPContainer ptp;
PTP_CNT_INIT(ptp);
ptp.Code = 0x900c;
ptp.Nparam = 0;
return ptp_transaction(params, &ptp, PTP_DP_GETDATA, 0, data, size);
/* returned data is 16bit,16bit,32bit,32bit */
}
/**
* ptp_ek_settext:
* params: PTPParams*
* PTPEKTextParams* - contains the texts to display.
*
* Displays the specified texts on the TFT of the camera.
*
* Return values: Some PTP_RC_* code.
*
*/
uint16_t
ptp_ek_settext (PTPParams* params, PTPEKTextParams *text)
{
PTPContainer ptp;
uint16_t ret;
unsigned int size;
unsigned char *data;
PTP_CNT_INIT(ptp);
ptp.Code = PTP_OC_EK_SetText;
ptp.Nparam = 0;
if (0 == (size = ptp_pack_EK_text(params, text, &data)))
return PTP_ERROR_BADPARAM;
ret = ptp_transaction(params, &ptp, PTP_DP_SENDDATA, size, &data, NULL);
free(data);
return ret;
}
/**
* ptp_ek_sendfileobject:
* params: PTPParams*
* char* object - contains the object that is to be sent
* uint32_t size - object size
*
* Sends object to Responder.
*
* Return values: Some PTP_RC_* code.
*
*/
uint16_t
ptp_ek_sendfileobject (PTPParams* params, unsigned char* object, uint32_t size)
{
PTPContainer ptp;
PTP_CNT_INIT(ptp);
ptp.Code=PTP_OC_EK_SendFileObject;
ptp.Nparam=0;
return ptp_transaction(params, &ptp, PTP_DP_SENDDATA, size, &object, NULL);
}
/**
* ptp_ek_sendfileobject_from_handler:
* params: PTPParams*
* PTPDataHandler* handler - contains the handler of the object that is to be sent
* uint32_t size - object size
*
* Sends object to Responder.
*
* Return values: Some PTP_RC_* code.
*
*/
uint16_t
ptp_ek_sendfileobject_from_handler (PTPParams* params, PTPDataHandler*handler, uint32_t size)
{
PTPContainer ptp;
PTP_CNT_INIT(ptp);
ptp.Code=PTP_OC_EK_SendFileObject;
ptp.Nparam=0;
return ptp_transaction_new(params, &ptp, PTP_DP_SENDDATA, size, handler);
}
/*************************************************************************
*
* Canon PTP extensions support
*
* (C) Nikolai Kopanygin 2003
*
*************************************************************************/
/**
* ptp_canon_getpartialobjectinfo:
* params: PTPParams*
* uint32_t handle - ObjectHandle
* uint32_t p2 - Not fully understood parameter
* 0 - returns full size
* 1 - returns thumbnail size (or EXIF?)
*
* Gets form the responder the size of the specified object.
*
* Return values: Some PTP_RC_* code.
* Upon success : uint32_t* size - The object size
* uint32_t* rp2 - Still unknown return parameter
* (perhaps upper 32bit of size)
*
*
**/
uint16_t
ptp_canon_getpartialobjectinfo (PTPParams* params, uint32_t handle, uint32_t p2,
uint32_t* size, uint32_t* rp2)
{
uint16_t ret;
PTPContainer ptp;
PTP_CNT_INIT(ptp);
ptp.Code=PTP_OC_CANON_GetPartialObjectInfo;
ptp.Param1=handle;
ptp.Param2=p2;
ptp.Nparam=2;
ret=ptp_transaction(params, &ptp, PTP_DP_NODATA, 0, NULL, NULL);
*size=ptp.Param1;
*rp2=ptp.Param2;
return ret;
}
/**
* ptp_canon_get_mac_address:
* params: PTPParams*
* value 0 works.
* Gets the MAC address of the wireless transmitter.
*
* Return values: Some PTP_RC_* code.
* Upon success : unsigned char* mac - The MAC address
*
**/
uint16_t
ptp_canon_get_mac_address (PTPParams* params, unsigned char **mac)
{
PTPContainer ptp;
unsigned int size = 0;
PTP_CNT_INIT(ptp);
ptp.Code=PTP_OC_CANON_GetMACAddress;
ptp.Nparam=0;
*mac = NULL;
return ptp_transaction(params, &ptp, PTP_DP_GETDATA, 0, mac, &size);
}
/**
* ptp_canon_get_directory:
* params: PTPParams*
* Gets the full directory of the camera.
*
* Return values: Some PTP_RC_* code.
* Upon success : PTPObjectHandles *handles - filled out with handles
* PTPObjectInfo **oinfos - allocated array of PTP Object Infos
* uint32_t **flags - allocated array of CANON Flags
*
**/
uint16_t
ptp_canon_get_directory (PTPParams* params,
PTPObjectHandles *handles,
PTPObjectInfo **oinfos, /* size(handles->n) */
uint32_t **flags /* size(handles->n) */
) {
PTPContainer ptp;
unsigned char *dir = NULL;
unsigned int size = 0;
uint16_t ret;
PTP_CNT_INIT(ptp);
ptp.Code=PTP_OC_CANON_GetDirectory;
ptp.Nparam=0;
ret = ptp_transaction(params, &ptp, PTP_DP_GETDATA, 0, &dir, &size);
if (ret != PTP_RC_OK)
return ret;
ret = ptp_unpack_canon_directory(params, dir, ptp.Param1, handles, oinfos, flags);
free (dir);
return ret;
}
/**
* ptp_canon_gettreeinfo:
* params: PTPParams*
* uint32_t *out
*
* Switches the camera display to on and lets the user
* select what to transfer. Sends a 0xc011 event when started
* and 0xc013 if direct transfer aborted.
*
* Return values: Some PTP_RC_* code.
*
**/
uint16_t
ptp_canon_gettreeinfo (PTPParams* params, uint32_t *out)
{
PTPContainer ptp;
uint16_t ret;
PTP_CNT_INIT(ptp);
ptp.Code = PTP_OC_CANON_GetTreeInfo;
ptp.Nparam = 1;
ptp.Param1 = 0xf;
ret = ptp_transaction(params, &ptp, PTP_DP_NODATA, 0, NULL, NULL);
if ((ret == PTP_RC_OK) && (ptp.Nparam>0))
*out = ptp.Param1;
return ret;
}
/**
* ptp_canon_getpairinginfo:
* params: PTPParams*
* int nr
*
* Get the pairing information.
*
* Return values: Some PTP_RC_* code.
*
**/
uint16_t
ptp_canon_getpairinginfo (PTPParams* params, uint32_t nr, unsigned char **data, unsigned int *size)
{
PTPContainer ptp;
uint16_t ret;
PTP_CNT_INIT(ptp);
ptp.Code = PTP_OC_CANON_GetPairingInfo;
ptp.Nparam = 1;
ptp.Param1 = nr;
*data = NULL;
*size = 0;
ret = ptp_transaction(params, &ptp, PTP_DP_GETDATA, 0, data, size);
if (ret != PTP_RC_OK)
return ret;
return PTP_RC_OK;
}
/**
* ptp_canon_get_target_handles:
* params: PTPParams*
* PTPCanon_directtransfer_entry **out
* unsigned int *outsize
*
* Retrieves direct transfer entries specifying the images to transfer
* from the camera (to be retrieved after 0xc011 event).
*
* Return values: Some PTP_RC_* code.
*
**/
uint16_t
ptp_canon_gettreesize (PTPParams* params,
PTPCanon_directtransfer_entry **entries, unsigned int *cnt)
{
PTPContainer ptp;
uint16_t ret;
unsigned char *out = NULL, *cur;
int i;
unsigned int size;
PTP_CNT_INIT(ptp);
ptp.Code = PTP_OC_CANON_GetTreeSize;
ptp.Nparam = 0;
ret = ptp_transaction(params, &ptp, PTP_DP_GETDATA, 0, &out, &size);
if (ret != PTP_RC_OK)
return ret;
*cnt = dtoh32a(out);
*entries = malloc(sizeof(PTPCanon_directtransfer_entry)*(*cnt));
if (!*entries) {
free (out);
return PTP_RC_GeneralError;
}
cur = out+4;
for (i=0;i<*cnt;i++) {
unsigned char len;
(*entries)[i].oid = dtoh32a(cur);
(*entries)[i].str = ptp_unpack_string(params, cur, 4, &len);
cur += 4+(cur[4]*2+1);
}
free (out);
return PTP_RC_OK;
}
/**
* ptp_canon_checkevent:
* params: PTPParams*
*
* The camera has a FIFO stack, in which it accumulates events.
* Partially these events are communicated also via the USB interrupt pipe
* according to the PTP USB specification, partially not.
* This operation returns from the device a block of data, empty,
* if the event stack is empty, or filled with an event's data otherwise.
* The event is removed from the stack in the latter case.
* The Remote Capture app sends this command to the camera all the time
* of connection, filling with it the gaps between other operations.
*
* Return values: Some PTP_RC_* code.
* Upon success : PTPUSBEventContainer* event - is filled with the event data
* if any
* int *isevent - returns 1 in case of event
* or 0 otherwise
**/
uint16_t
ptp_canon_checkevent (PTPParams* params, PTPContainer* event, int* isevent)
{
uint16_t ret;
PTPContainer ptp;
unsigned char *evdata = NULL;
unsigned int len;
*isevent=0;
PTP_CNT_INIT(ptp);
ptp.Code=PTP_OC_CANON_CheckEvent;
ptp.Nparam=0;
len=0;
ret=ptp_transaction(params, &ptp, PTP_DP_GETDATA, 0, &evdata, &len);
if (evdata!=NULL) {
if (ret == PTP_RC_OK) {
ptp_unpack_EC(params, evdata, event, len);
*isevent=1;
}
free(evdata);
}
return ret;
}
uint16_t
ptp_check_event (PTPParams *params) {
PTPContainer event;
uint16_t ret;
if ( (params->deviceinfo.VendorExtensionID == PTP_VENDOR_NIKON) &&
ptp_operation_issupported(params, PTP_OC_NIKON_CheckEvent)
) {
int evtcnt;
PTPContainer *xevent = NULL;
ret = ptp_nikon_check_event(params, &xevent, &evtcnt);
if (ret != PTP_RC_OK)
return ret;
if (evtcnt) {
if (params->nrofevents)
params->events = realloc(params->events, sizeof(PTPContainer)*(evtcnt+params->nrofevents));
else
params->events = malloc(sizeof(PTPContainer)*evtcnt);
memcpy (&params->events[params->nrofevents],xevent,evtcnt*sizeof(PTPContainer));
params->nrofevents += evtcnt;
free (xevent);
}
return PTP_RC_OK;
}
/* should not get here ... EOS has no normal PTP events and another queue handling. */
if ( (params->deviceinfo.VendorExtensionID == PTP_VENDOR_CANON) &&
ptp_operation_issupported(params, PTP_OC_CANON_EOS_GetEvent)
) {
return PTP_RC_OK;
}
if ( (params->deviceinfo.VendorExtensionID == PTP_VENDOR_CANON) &&
ptp_operation_issupported(params, PTP_OC_CANON_CheckEvent)
) {
int isevent;
ret = ptp_canon_checkevent (params,&event,&isevent);
if (ret!=PTP_RC_OK)
return ret;
if (isevent)
goto store_event;
/* Event Emulate Mode 0 (unset) and 1-5 get interrupt events. 6-7 does not. */
if (params->canon_event_mode > 5)
return ret;
/* FIXME: fallthrough or return? */
#ifdef __APPLE__
/* the libusb 1 on darwin currently does not like polling
* for interrupts, they have no timeout for it. 2010/08/23
* Check back in 2011 or so. -Marcus
*/
return ret;
#endif
}
ret = params->event_check(params,&event);
store_event:
if (ret == PTP_RC_OK) {
ptp_debug (params, "event: nparams=0x%X, code=0x%X, trans_id=0x%X, p1=0x%X, p2=0x%X, p3=0x%X", event.Nparam,event.Code,event.Transaction_ID, event.Param1, event.Param2, event.Param3);
if (params->nrofevents)
params->events = realloc(params->events, sizeof(PTPContainer)*(params->nrofevents+1));
else
params->events = malloc(sizeof(PTPContainer)*1);
memcpy (&params->events[params->nrofevents],&event,1*sizeof(PTPContainer));
params->nrofevents += 1;
}
if (ret == PTP_ERROR_TIMEOUT) /* ok, just new events */
ret = PTP_RC_OK;
return ret;
}
int
ptp_get_one_event(PTPParams *params, PTPContainer *event) {
if (!params->nrofevents)
return 0;
memcpy (event, params->events, sizeof(PTPContainer));
memmove (params->events, params->events+1, sizeof(PTPContainer)*(params->nrofevents-1));
/* do not realloc on shrink. */
params->nrofevents--;
if (!params->nrofevents) {
free (params->events);
params->events = NULL;
}
return 1;
}
/**
* ptp_canon_eos_getevent:
*
* This retrieves configuration status/updates/changes
* on EOS cameras. It reads a datablock which has a list of variable
* sized structures.
*
* params: PTPParams*
*
* Return values: Some PTP_RC_* code.
*
**/
uint16_t
ptp_canon_eos_getevent (PTPParams* params, PTPCanon_changes_entry **entries, int *nrofentries)
{
PTPContainer ptp;
uint16_t ret;
unsigned int size = 0;
unsigned char *data = NULL;
*nrofentries = 0;
*entries = NULL;
PTP_CNT_INIT(ptp);
ptp.Code = PTP_OC_CANON_EOS_GetEvent;
ptp.Nparam = 0;
ret = ptp_transaction(params, &ptp, PTP_DP_GETDATA, 0, &data, &size);
if (ret != PTP_RC_OK) return ret;
*nrofentries = ptp_unpack_CANON_changes(params,data,size,entries);
free (data);
return PTP_RC_OK;
}
uint16_t
ptp_check_eos_events (PTPParams *params) {
uint16_t ret;
PTPCanon_changes_entry *entries = NULL, *nentries;
int nrofentries = 0;
while (1) { /* call it repeatedly until the camera does not report any */
ret = ptp_canon_eos_getevent (params, &entries, &nrofentries);
if (ret != PTP_RC_OK)
return ret;
if (!nrofentries)
return PTP_RC_OK;
if (params->nrofbacklogentries) {
nentries = realloc(params->backlogentries,sizeof(entries[0])*(params->nrofbacklogentries+nrofentries));
if (!nentries)
return PTP_RC_GeneralError;
params->backlogentries = nentries;
memcpy (nentries+params->nrofbacklogentries, entries, nrofentries*sizeof(entries[0]));
params->nrofbacklogentries += nrofentries;
free (entries);
} else {
params->backlogentries = entries;
params->nrofbacklogentries = nrofentries;
}
}
return PTP_RC_OK;
}
int
ptp_get_one_eos_event (PTPParams *params, PTPCanon_changes_entry *entry) {
if (!params->nrofbacklogentries)
return 0;
memcpy (entry, params->backlogentries, sizeof(*entry));
if (params->nrofbacklogentries > 1) {
memmove (params->backlogentries,params->backlogentries+1,sizeof(*entry)*(params->nrofbacklogentries-1));
params->nrofbacklogentries--;
} else {
free (params->backlogentries);
params->backlogentries = NULL;
params->nrofbacklogentries = 0;
}
return 1;
}
uint16_t
ptp_canon_eos_getdevicepropdesc (PTPParams* params, uint16_t propcode,
PTPDevicePropDesc *dpd)
{
int i;
for (i=0;i<params->nrofcanon_props;i++)
if (params->canon_props[i].proptype == propcode)
break;
if (params->nrofcanon_props == i)
return PTP_RC_Undefined;
memcpy (dpd, &params->canon_props[i].dpd, sizeof (*dpd));
if (dpd->FormFlag == PTP_DPFF_Enumeration) {
/* need to duplicate the Enumeration alloc */
dpd->FORM.Enum.SupportedValue = malloc (sizeof (PTPPropertyValue)*dpd->FORM.Enum.NumberOfValues);
memcpy (dpd->FORM.Enum.SupportedValue,
params->canon_props[i].dpd.FORM.Enum.SupportedValue,
sizeof (PTPPropertyValue)*dpd->FORM.Enum.NumberOfValues
);
}
if (dpd->DataType == PTP_DTC_STR) {
dpd->FactoryDefaultValue.str = strdup( params->canon_props[i].dpd.FactoryDefaultValue.str );
dpd->CurrentValue.str = strdup( params->canon_props[i].dpd.CurrentValue.str );
}
return PTP_RC_OK;
}
uint16_t
ptp_canon_eos_getstorageids (PTPParams* params, PTPStorageIDs* storageids)
{
PTPContainer ptp;
unsigned int len = 0;
uint16_t ret;
unsigned char* sids=NULL;
PTP_CNT_INIT(ptp);
ptp.Code = PTP_OC_CANON_EOS_GetStorageIDs;
ptp.Nparam = 0;
ret = ptp_transaction(params, &ptp, PTP_DP_GETDATA, 0, &sids, &len);
if (ret == PTP_RC_OK) ptp_unpack_SIDs(params, sids, storageids, len);
free(sids);
return ret;
}
uint16_t
ptp_canon_eos_getstorageinfo (PTPParams* params, uint32_t p1, unsigned char **data, unsigned int *size)
{
PTPContainer ptp;
uint16_t ret;
*size = 0;
*data = NULL;
PTP_CNT_INIT(ptp);
ptp.Code = PTP_OC_CANON_EOS_GetStorageInfo;
ptp.Nparam = 1;
ptp.Param1 = p1;
ret = ptp_transaction(params, &ptp, PTP_DP_GETDATA, 0, data, size);
/* FIXME: do stuff with data */
return ret;
}
uint16_t
ptp_canon_eos_getobjectinfoex (
PTPParams* params, uint32_t storageid, uint32_t oid, uint32_t unk,
PTPCANONFolderEntry **entries, unsigned int *nrofentries
) {
PTPContainer ptp;
unsigned int i, size = 0;
unsigned char *data, *xdata;
uint16_t ret;
data = NULL;
PTP_CNT_INIT(ptp);
ptp.Code = PTP_OC_CANON_EOS_GetObjectInfoEx;
ptp.Nparam = 3;
ptp.Param1 = storageid;
ptp.Param2 = oid;
ptp.Param3 = unk;
ret = ptp_transaction(params, &ptp, PTP_DP_GETDATA, 0, &data, &size);
if (ret != PTP_RC_OK)
return ret;
*nrofentries = dtoh32a(data);
*entries = malloc(*nrofentries * sizeof(PTPCANONFolderEntry));
if (!*entries)
return PTP_RC_GeneralError;
xdata = data+sizeof(uint32_t);
for (i=0;i<*nrofentries;i++) {
ptp_unpack_Canon_EOS_FE (params, &xdata[4], &((*entries)[i]));
xdata += dtoh32a(xdata);
}
return PTP_RC_OK;
}
/**
* ptp_canon_eos_getpartialobject:
*
* This retrieves a part of an PTP object which you specify as object id.
* The id originates from 0x9116 call.
* After finishing it, we seem to need to call ptp_canon_eos_enddirecttransfer.
*
* params: PTPParams*
* oid Object ID
* offset The offset where to start the data transfer
* xsize Size in bytes of the transfer to do
* data Pointer that receives the malloc()ed memory of the transfer.
*
* Return values: Some PTP_RC_* code.
*
*/
uint16_t
ptp_canon_eos_getpartialobject (PTPParams* params, uint32_t oid, uint32_t offset, uint32_t xsize, unsigned char**data)
{
PTPContainer ptp;
unsigned int size = 0;
*data = NULL;
PTP_CNT_INIT(ptp);
ptp.Code = PTP_OC_CANON_EOS_GetPartialObject;
ptp.Nparam = 3;
ptp.Param1 = oid;
ptp.Param2 = offset;
ptp.Param3 = xsize;
return ptp_transaction(params, &ptp, PTP_DP_GETDATA, 0, data, &size);
}
uint16_t
ptp_canon_eos_setdevicepropvalueex (PTPParams* params, unsigned char* data, unsigned int size)
{
PTPContainer ptp;
PTP_CNT_INIT(ptp);
ptp.Code = PTP_OC_CANON_EOS_SetDevicePropValueEx;
ptp.Nparam = 0;
return ptp_transaction(params, &ptp, PTP_DP_SENDDATA, size, &data, NULL);
}
#if 0
uint16_t
ptp_canon_eos_setdevicepropvalue (PTPParams* params,
uint16_t propcode, PTPPropertyValue *value, uint16_t datatype
) {
PTPContainer ptp;
uint16_t ret;
int i;
unsigned char *data;
unsigned int size;
PTP_CNT_INIT(ptp);
ptp.Code = PTP_OC_CANON_EOS_SetDevicePropValueEx;
ptp.Nparam = 0;
for (i=0;i<params->nrofcanon_props;i++)
if (params->canon_props[i].proptype == propcode)
break;
if (params->nrofcanon_props == i)
return PTP_RC_Undefined;
switch (propcode) {
case PTP_DPC_CANON_EOS_ImageFormat:
case PTP_DPC_CANON_EOS_ImageFormatCF:
case PTP_DPC_CANON_EOS_ImageFormatSD:
case PTP_DPC_CANON_EOS_ImageFormatExtHD:
/* special handling of ImageFormat properties */
size = 8 + ptp_pack_EOS_ImageFormat( params, NULL, value->u16 );
data = malloc( size );
if (!data) return PTP_RC_GeneralError;
params->canon_props[i].dpd.CurrentValue.u16 = value->u16;
ptp_pack_EOS_ImageFormat( params, data + 8, value->u16 );
break;
case PTP_DPC_CANON_EOS_CustomFuncEx:
/* special handling of CustomFuncEx properties */
ptp_debug (params, "ptp2/ptp_canon_eos_setdevicepropvalue: setting EOS prop %x to %s",propcode,value->str);
size = 8 + ptp_pack_EOS_CustomFuncEx( params, NULL, value->str );
data = malloc( size );
if (!data) return PTP_RC_GeneralError;
params->canon_props[i].dpd.CurrentValue.str = strdup( value->str );
ptp_pack_EOS_CustomFuncEx( params, data + 8, value->str );
break;
default:
if (datatype != PTP_DTC_STR) {
data = calloc(sizeof(uint32_t),3);
if (!data) return PTP_RC_GeneralError;
size = sizeof(uint32_t)*3;
} else {
size = strlen(value->str) + 1 + 8;
data = calloc(sizeof(char),size);
if (!data) return PTP_RC_GeneralError;
}
switch (datatype) {
case PTP_DTC_INT8:
case PTP_DTC_UINT8:
/*fprintf (stderr, "%x -> %d\n", propcode, value->u8);*/
htod8a(&data[8], value->u8);
params->canon_props[i].dpd.CurrentValue.u8 = value->u8;
break;
case PTP_DTC_UINT16:
case PTP_DTC_INT16:
/*fprintf (stderr, "%x -> %d\n", propcode, value->u16);*/
htod16a(&data[8], value->u16);
params->canon_props[i].dpd.CurrentValue.u16 = value->u16;
break;
case PTP_DTC_INT32:
case PTP_DTC_UINT32:
/*fprintf (stderr, "%x -> %d\n", propcode, value->u32);*/
htod32a(&data[8], value->u32);
params->canon_props[i].dpd.CurrentValue.u32 = value->u32;
break;
case PTP_DTC_STR:
strcpy((char*)data + 8, value->str);
free (params->canon_props[i].dpd.CurrentValue.str);
params->canon_props[i].dpd.CurrentValue.str = strdup(value->str);
break;
}
}
htod32a(&data[0], size);
htod32a(&data[4], propcode);
ret = ptp_transaction(params, &ptp, PTP_DP_SENDDATA, size, &data, NULL);
free (data);
return ret;
}
#endif
/**
* ptp_canon_getpartialobject:
*
* This operation is used to read from the device a data
* block of an object from a specified offset.
*
* params: PTPParams*
* uint32_t handle - the handle of the requested object
* uint32_t offset - the offset in bytes from the beginning of the object
* uint32_t size - the requested size of data block to read
* uint32_t pos - 1 for the first block, 2 - for a block in the middle,
* 3 - for the last block
*
* Return values: Some PTP_RC_* code.
* char **block - the pointer to the block of data read
* uint32_t* readnum - the number of bytes read
*
**/
uint16_t
ptp_canon_getpartialobject (PTPParams* params, uint32_t handle,
uint32_t offset, uint32_t size,
uint32_t pos, unsigned char** block,
uint32_t* readnum)
{
uint16_t ret;
PTPContainer ptp;
unsigned char *data=NULL;
unsigned int len;
PTP_CNT_INIT(ptp);
ptp.Code=PTP_OC_CANON_GetPartialObjectEx;
ptp.Param1=handle;
ptp.Param2=offset;
ptp.Param3=size;
ptp.Param4=pos;
ptp.Nparam=4;
len=0;
ret=ptp_transaction(params, &ptp, PTP_DP_GETDATA, 0, &data, &len);
if (ret==PTP_RC_OK) {
*block=data;
*readnum=ptp.Param1;
}
return ret;
}
/**
* ptp_canon_getviewfinderimage:
*
* This operation can be used to read the image which is currently
* in the camera's viewfinder. The image size is 320x240, format is JPEG.
* Of course, prior to calling this operation, one must turn the viewfinder
* on with the CANON_ViewfinderOn command.
* Invoking this operation many times, one can get live video from the camera!
*
* params: PTPParams*
*
* Return values: Some PTP_RC_* code.
* char **image - the pointer to the read image
* unit32_t *size - the size of the image in bytes
*
**/
uint16_t
ptp_canon_getviewfinderimage (PTPParams* params, unsigned char** image, uint32_t* size)
{
uint16_t ret;
PTPContainer ptp;
unsigned int len;
PTP_CNT_INIT(ptp);
ptp.Code=PTP_OC_CANON_GetViewfinderImage;
ptp.Nparam=0;
ret=ptp_transaction(params, &ptp, PTP_DP_GETDATA, 0, image, &len);
if (ret==PTP_RC_OK) *size=ptp.Param1;
return ret;
}
/**
* ptp_canon_getchanges:
*
* This is an interesting operation, about the effect of which I am not sure.
* This command is called every time when a device property has been changed
* with the SetDevicePropValue operation, and after some other operations.
* This operation reads the array of Device Properties which have been changed
* by the previous operation.
* Probably, this operation is even required to make those changes work.
*
* params: PTPParams*
*
* Return values: Some PTP_RC_* code.
* uint16_t** props - the pointer to the array of changed properties
* uint32_t* propnum - the number of elements in the *props array
*
**/
uint16_t
ptp_canon_getchanges (PTPParams* params, uint16_t** props, uint32_t* propnum)
{
uint16_t ret;
PTPContainer ptp;
unsigned char* data=NULL;
unsigned int len;
PTP_CNT_INIT(ptp);
ptp.Code=PTP_OC_CANON_GetChanges;
ptp.Nparam=0;
len=0;
ret=ptp_transaction(params, &ptp, PTP_DP_GETDATA, 0, &data, &len);
if (ret == PTP_RC_OK)
*propnum=ptp_unpack_uint16_t_array(params,data,0,props);
free(data);
return ret;
}
/**
* ptp_canon_getobjectinfo:
*
* This command reads a specified object's record in a device's filesystem,
* or the records of all objects belonging to a specified folder (association).
*
* params: PTPParams*
* uint32_t store - StorageID,
* uint32_t p2 - Yet unknown (0 value works OK)
* uint32_t parent - Parent Object Handle
* # If Parent Object Handle is 0xffffffff,
* # the Parent Object is the top level folder.
* uint32_t handle - Object Handle
* # If Object Handle is 0, the records of all objects
* # belonging to the Parent Object are read.
* # If Object Handle is not 0, only the record of this
* # Object is read.
*
* Return values: Some PTP_RC_* code.
* PTPCANONFolderEntry** entries - the pointer to the folder entry array
* uint32_t* entnum - the number of elements of the array
*
**/
uint16_t
ptp_canon_getobjectinfo (PTPParams* params, uint32_t store, uint32_t p2,
uint32_t parent, uint32_t handle,
PTPCANONFolderEntry** entries, uint32_t* entnum)
{
uint16_t ret;
PTPContainer ptp;
unsigned char *data = NULL;
unsigned int len;
PTP_CNT_INIT(ptp);
ptp.Code=PTP_OC_CANON_GetObjectInfoEx;
ptp.Param1=store;
ptp.Param2=p2;
ptp.Param3=parent;
ptp.Param4=handle;
ptp.Nparam=4;
len=0;
ret=ptp_transaction(params, &ptp, PTP_DP_GETDATA, 0, &data, &len);
if (ret == PTP_RC_OK) {
int i;
*entnum=ptp.Param1;
*entries=calloc(*entnum, sizeof(PTPCANONFolderEntry));
if (*entries!=NULL) {
for(i=0; i<(*entnum); i++)
ptp_unpack_Canon_FE(params,
data+i*PTP_CANON_FolderEntryLen,
&((*entries)[i]) );
} else {
ret=PTP_ERROR_IO; /* Cannot allocate memory */
}
}
free(data);
return ret;
}
/**
* ptp_canon_get_objecthandle_by_name:
*
* This command looks up the specified object on the camera.
*
* Format is "A:\\PATH".
*
* The 'A' is the VolumeLabel from GetStorageInfo,
* my IXUS has "A" for the card and "V" for internal memory.
*
* params: PTPParams*
* char* name - path name
*
* Return values: Some PTP_RC_* code.
* uint32_t *oid - PTP object id.
*
**/
uint16_t
ptp_canon_get_objecthandle_by_name (PTPParams* params, char* name, uint32_t* objectid)
{
uint16_t ret;
PTPContainer ptp;
unsigned char *data = NULL;
uint8_t len;
PTP_CNT_INIT (ptp);
ptp.Code=PTP_OC_CANON_GetObjectHandleByName;
ptp.Nparam=0;
len=0;
data = malloc (2*(strlen(name)+1)+2);
memset (data, 0, 2*(strlen(name)+1)+2);
ptp_pack_string (params, name, data, 0, &len);
ret=ptp_transaction (params, &ptp, PTP_DP_SENDDATA, (len+1)*2+1, &data, NULL);
free (data);
*objectid = ptp.Param1;
return ret;
}
/**
* ptp_canon_get_customize_data:
*
* This command downloads the specified theme slot, including jpegs
* and wav files.
*
* params: PTPParams*
* uint32_t themenr - nr of theme
*
* Return values: Some PTP_RC_* code.
* unsigned char **data - pointer to data pointer
* unsigned int *size - size of data returned
*
**/
uint16_t
ptp_canon_get_customize_data (PTPParams* params, uint32_t themenr,
unsigned char **data, unsigned int *size)
{
PTPContainer ptp;
*data = NULL;
*size = 0;
PTP_CNT_INIT(ptp);
ptp.Code = PTP_OC_CANON_GetCustomizeData;
ptp.Param1 = themenr;
ptp.Nparam = 1;
return ptp_transaction(params, &ptp, PTP_DP_GETDATA, 0, data, size);
}
uint16_t
ptp_nikon_curve_download (PTPParams* params, unsigned char **data, unsigned int *size) {
PTPContainer ptp;
*data = NULL;
*size = 0;
PTP_CNT_INIT(ptp);
ptp.Code = PTP_OC_NIKON_CurveDownload;
ptp.Nparam = 0;
return ptp_transaction(params, &ptp, PTP_DP_GETDATA, 0, data, size);
}
/**
* ptp_canon_get_vendorpropcodes:
*
* This command downloads the vendor specific property codes.
*
* params: PTPParams*
*
* Return values: Some PTP_RC_* code.
* unsigned char **data - pointer to data pointer
* unsigned int *size - size of data returned
*
**/
uint16_t
ptp_nikon_get_vendorpropcodes (PTPParams* params, uint16_t **props, unsigned int *size) {
PTPContainer ptp;
uint16_t ret;
unsigned char *xdata = NULL;
unsigned int xsize;
*props = NULL;
*size = 0;
PTP_CNT_INIT(ptp);
ptp.Code = PTP_OC_NIKON_GetVendorPropCodes;
ptp.Nparam = 0;
ret = ptp_transaction(params, &ptp, PTP_DP_GETDATA, 0, &xdata, &xsize);
if (ret == PTP_RC_OK)
*size = ptp_unpack_uint16_t_array(params,xdata,0,props);
free (xdata);
return ret;
}
uint16_t
ptp_nikon_getfileinfoinblock ( PTPParams* params,
uint32_t p1, uint32_t p2, uint32_t p3,
unsigned char **data, unsigned int *size
) {
PTPContainer ptp;
*data = NULL;
*size = 0;
PTP_CNT_INIT(ptp);
ptp.Code = PTP_OC_NIKON_GetFileInfoInBlock;
ptp.Nparam = 3;
ptp.Param1 = p1;
ptp.Param2 = p2;
ptp.Param3 = p3;
return ptp_transaction(params, &ptp, PTP_DP_GETDATA, 0, data, size);
}
/**
* ptp_nikon_get_liveview_image:
*
* This command gets a LiveView image from newer Nikons DSLRs.
*
* params: PTPParams*
*
* Return values: Some PTP_RC_* code.
*
**/
uint16_t
ptp_nikon_get_liveview_image (PTPParams* params, unsigned char **data, unsigned int *size)
{
PTPContainer ptp;
PTP_CNT_INIT(ptp);
ptp.Code=PTP_OC_NIKON_GetLiveViewImg;
ptp.Nparam=0;
return ptp_transaction(params, &ptp, PTP_DP_GETDATA, 0, data, size);
}
/**
* ptp_nikon_get_preview_image:
*
* This command gets a Preview image from newer Nikons DSLRs.
*
* params: PTPParams*
*
* Return values: Some PTP_RC_* code.
*
**/
uint16_t
ptp_nikon_get_preview_image (PTPParams* params, unsigned char **xdata, unsigned int *xsize,
uint32_t *handle)
{
PTPContainer ptp;
uint16_t ret;
PTP_CNT_INIT(ptp);
ptp.Code=PTP_OC_NIKON_GetPreviewImg;
ptp.Nparam=0;
ret = ptp_transaction(params, &ptp, PTP_DP_GETDATA, 0, xdata, xsize);
if (ret == PTP_RC_OK) {
if (ptp.Nparam > 0)
*handle = ptp.Param1;
}
return ret;
}
/**
* ptp_canon_eos_get_viewfinder_image:
*
* This command gets a Viewfinder image from newer Nikons DSLRs.
*
* params: PTPParams*
*
* Return values: Some PTP_RC_* code.
*
**/
uint16_t
ptp_canon_eos_get_viewfinder_image (PTPParams* params, unsigned char **data, unsigned int *size)
{
PTPContainer ptp;
PTP_CNT_INIT(ptp);
ptp.Code=PTP_OC_CANON_EOS_GetViewFinderData;
ptp.Nparam=1;
ptp.Param1=0x00100000; /* from trace */
return ptp_transaction(params, &ptp, PTP_DP_GETDATA, 0, data, size);
}
uint16_t
ptp_canon_eos_get_viewfinder_image_handler (PTPParams* params, PTPDataHandler*handler)
{
PTPContainer ptp;
PTP_CNT_INIT(ptp);
ptp.Code=PTP_OC_CANON_EOS_GetViewFinderData;
ptp.Nparam=1;
ptp.Param1=0x00100000; /* from trace */
return ptp_transaction_new(params, &ptp, PTP_DP_GETDATA, 0, handler);
}
/**
* ptp_nikon_check_event:
*
* This command checks the event queue on the Nikon.
*
* params: PTPParams*
* PTPUSBEventContainer **event - list of usb events.
* int *evtcnt - number of usb events in event structure.
*
* Return values: Some PTP_RC_* code.
*
**/
uint16_t
ptp_nikon_check_event (PTPParams* params, PTPContainer** event, int* evtcnt)
{
PTPContainer ptp;
uint16_t ret;
unsigned char *data = NULL;
unsigned int size = 0;
PTP_CNT_INIT(ptp);
ptp.Code=PTP_OC_NIKON_CheckEvent;
ptp.Nparam=0;
*evtcnt = 0;
ret = ptp_transaction (params, &ptp, PTP_DP_GETDATA, 0, &data, &size);
if (ret == PTP_RC_OK) {
ptp_unpack_Nikon_EC (params, data, size, event, evtcnt);
free (data);
}
return ret;
}
/**
* ptp_nikon_getptpipinfo:
*
* This command gets the ptpip info data.
*
* params: PTPParams*
* unsigned char *data - data
* unsigned int size - size of returned data
*
* Return values: Some PTP_RC_* code.
*
**/
uint16_t
ptp_nikon_getptpipinfo (PTPParams* params, unsigned char **data, unsigned int *size)
{
PTPContainer ptp;
PTP_CNT_INIT(ptp);
ptp.Code=PTP_OC_NIKON_GetDevicePTPIPInfo;
ptp.Nparam=0;
return ptp_transaction(params, &ptp, PTP_DP_GETDATA, 0, data, size);
}
/**
* ptp_nikon_getwifiprofilelist:
*
* This command gets the wifi profile list.
*
* params: PTPParams*
*
* Return values: Some PTP_RC_* code.
*
**/
uint16_t
ptp_nikon_getwifiprofilelist (PTPParams* params)
{
PTPContainer ptp;
unsigned char* data;
unsigned int size;
unsigned int pos;
unsigned int profn;
unsigned int n;
char* buffer;
uint8_t len;
PTP_CNT_INIT(ptp);
ptp.Code=PTP_OC_NIKON_GetProfileAllData;
ptp.Nparam=0;
size = 0;
data = NULL;
CHECK_PTP_RC(ptp_transaction(params, &ptp, PTP_DP_GETDATA, 0, &data, &size));
if (size < 2) return PTP_RC_Undefined; /* FIXME: Add more precise error code */
params->wifi_profiles_version = data[0];
params->wifi_profiles_number = data[1];
if (params->wifi_profiles)
free(params->wifi_profiles);
params->wifi_profiles = malloc(params->wifi_profiles_number*sizeof(PTPNIKONWifiProfile));
memset(params->wifi_profiles, 0, params->wifi_profiles_number*sizeof(PTPNIKONWifiProfile));
pos = 2;
profn = 0;
while (profn < params->wifi_profiles_number && pos < size) {
if (pos+6 >= size) return PTP_RC_Undefined;
params->wifi_profiles[profn].id = data[pos++];
params->wifi_profiles[profn].valid = data[pos++];
n = dtoh32a(&data[pos]);
pos += 4;
if (pos+n+4 >= size) return PTP_RC_Undefined;
strncpy(params->wifi_profiles[profn].profile_name, (char*)&data[pos], n);
params->wifi_profiles[profn].profile_name[16] = '\0';
pos += n;
params->wifi_profiles[profn].display_order = data[pos++];
params->wifi_profiles[profn].device_type = data[pos++];
params->wifi_profiles[profn].icon_type = data[pos++];
buffer = ptp_unpack_string(params, data, pos, &len);
strncpy(params->wifi_profiles[profn].creation_date, buffer, sizeof(params->wifi_profiles[profn].creation_date));
free (buffer);
pos += (len*2+1);
if (pos+1 >= size) return PTP_RC_Undefined;
/* FIXME: check if it is really last usage date */
buffer = ptp_unpack_string(params, data, pos, &len);
strncpy(params->wifi_profiles[profn].lastusage_date, buffer, sizeof(params->wifi_profiles[profn].lastusage_date));
free (buffer);
pos += (len*2+1);
if (pos+5 >= size) return PTP_RC_Undefined;
n = dtoh32a(&data[pos]);
pos += 4;
if (pos+n >= size) return PTP_RC_Undefined;
strncpy(params->wifi_profiles[profn].essid, (char*)&data[pos], n);
params->wifi_profiles[profn].essid[32] = '\0';
pos += n;
pos += 1;
profn++;
}
#if 0
PTPNIKONWifiProfile test;
memset(&test, 0, sizeof(PTPNIKONWifiProfile));
strcpy(test.profile_name, "MyTest");
test.icon_type = 1;
strcpy(test.essid, "nikon");
test.ip_address = 10 + 11 << 16 + 11 << 24;
test.subnet_mask = 24;
test.access_mode = 1;
test.wifi_channel = 1;
test.key_nr = 1;
ptp_nikon_writewifiprofile(params, &test);
#endif
return PTP_RC_OK;
}
/**
* ptp_nikon_writewifiprofile:
*
* This command gets the ptpip info data.
*
* params: PTPParams*
* unsigned int profilenr - profile number
* unsigned char *data - data
* unsigned int size - size of returned data
*
* Return values: Some PTP_RC_* code.
*
**/
uint16_t
ptp_nikon_writewifiprofile (PTPParams* params, PTPNIKONWifiProfile* profile)
{
unsigned char guid[16];
PTPContainer ptp;
unsigned char buffer[1024];
unsigned char* data = buffer;
int size = 0;
int i;
uint8_t len;
int profilenr = -1;
ptp_nikon_getptpipguid(guid);
if (!params->wifi_profiles)
CHECK_PTP_RC(ptp_nikon_getwifiprofilelist(params));
for (i = 0; i < params->wifi_profiles_number; i++) {
if (!params->wifi_profiles[i].valid) {
profilenr = params->wifi_profiles[i].id;
break;
}
}
if (profilenr == -1) {
/* No free profile! */
return PTP_RC_StoreFull;
}
memset(buffer, 0, 1024);
buffer[0x00] = 0x64; /* Version */
/* Profile name */
htod32a(&buffer[0x01], 17);
/* 16 as third parameter, so there will always be a null-byte in the end */
strncpy((char*)&buffer[0x05], profile->profile_name, 16);
buffer[0x16] = 0x00; /* Display order */
buffer[0x17] = profile->device_type;
buffer[0x18] = profile->icon_type;
/* FIXME: Creation date: put a real date here */
ptp_pack_string(params, "19990909T090909", data, 0x19, &len);
/* IP parameters */
*((unsigned int*)&buffer[0x3A]) = profile->ip_address; /* Do not reverse bytes */
buffer[0x3E] = profile->subnet_mask;
*((unsigned int*)&buffer[0x3F]) = profile->gateway_address; /* Do not reverse bytes */
buffer[0x43] = profile->address_mode;
/* Wifi parameters */
buffer[0x44] = profile->access_mode;
buffer[0x45] = profile->wifi_channel;
htod32a(&buffer[0x46], 33); /* essid */
/* 32 as third parameter, so there will always be a null-byte in the end */
strncpy((char*)&buffer[0x4A], profile->essid, 32);
buffer[0x6B] = profile->authentification;
buffer[0x6C] = profile->encryption;
htod32a(&buffer[0x6D], 64);
for (i = 0; i < 64; i++) {
buffer[0x71+i] = profile->key[i];
}
buffer[0xB1] = profile->key_nr;
memcpy(&buffer[0xB2], guid, 16);
switch(profile->encryption) {
case 1: /* WEP 64bit */
htod16a(&buffer[0xC2], 5); /* (64-24)/8 = 5 */
break;
case 2: /* WEP 128bit */
htod16a(&buffer[0xC2], 13); /* (128-24)/8 = 13 */
break;
default:
htod16a(&buffer[0xC2], 0);
}
size = 0xC4;
PTP_CNT_INIT(ptp);
ptp.Code=PTP_OC_NIKON_SendProfileData;
ptp.Nparam=1;
ptp.Param1=profilenr;
return ptp_transaction(params, &ptp, PTP_DP_SENDDATA, size, &data, NULL);
}
/**
* ptp_mtp_getobjectpropssupported:
*
* This command gets the object properties possible from the device.
*
* params: PTPParams*
* uint ofc - object format code
* unsigned int *propnum - number of elements in returned array
* uint16_t *props - array of supported properties
*
* Return values: Some PTP_RC_* code.
*
**/
uint16_t
ptp_mtp_getobjectpropssupported (PTPParams* params, uint16_t ofc,
uint32_t *propnum, uint16_t **props
) {
PTPContainer ptp;
uint16_t ret;
unsigned char *data = NULL;
unsigned int size = 0;
PTP_CNT_INIT(ptp);
ptp.Code=PTP_OC_MTP_GetObjectPropsSupported;
ptp.Nparam = 1;
ptp.Param1 = ofc;
ret = ptp_transaction(params, &ptp, PTP_DP_GETDATA, 0, &data, &size);
if (ret == PTP_RC_OK)
*propnum=ptp_unpack_uint16_t_array(params,data,0,props);
free(data);
return ret;
}
/**
* ptp_mtp_getobjectpropdesc:
*
* This command gets the object property description.
*
* params: PTPParams*
* uint16_t opc - object property code
* uint16_t ofc - object format code
*
* Return values: Some PTP_RC_* code.
*
**/
uint16_t
ptp_mtp_getobjectpropdesc (
PTPParams* params, uint16_t opc, uint16_t ofc, PTPObjectPropDesc *opd
) {
PTPContainer ptp;
uint16_t ret;
unsigned char *data = NULL;
unsigned int size = 0;
PTP_CNT_INIT(ptp);
ptp.Code=PTP_OC_MTP_GetObjectPropDesc;
ptp.Nparam = 2;
ptp.Param1 = opc;
ptp.Param2 = ofc;
ret = ptp_transaction(params, &ptp, PTP_DP_GETDATA, 0, &data, &size);
if (ret == PTP_RC_OK)
ptp_unpack_OPD (params, data, opd, size);
free(data);
return ret;
}
/**
* ptp_mtp_getobjectpropvalue:
*
* This command gets the object properties of an object handle.
*
* params: PTPParams*
* uint32_t objectid - object format code
* uint16_t opc - object prop code
*
* Return values: Some PTP_RC_* code.
*
**/
uint16_t
ptp_mtp_getobjectpropvalue (
PTPParams* params, uint32_t oid, uint16_t opc,
PTPPropertyValue *value, uint16_t datatype
) {
PTPContainer ptp;
uint16_t ret;
unsigned char *data = NULL;
unsigned int size = 0;
int offset = 0;
PTP_CNT_INIT(ptp);
ptp.Code=PTP_OC_MTP_GetObjectPropValue;
ptp.Nparam = 2;
ptp.Param1 = oid;
ptp.Param2 = opc;
ret = ptp_transaction(params, &ptp, PTP_DP_GETDATA, 0, &data, &size);
if (ret == PTP_RC_OK)
ptp_unpack_DPV(params, data, &offset, size, value, datatype);
free(data);
return ret;
}
/**
* ptp_mtp_setobjectpropvalue:
*
* This command gets the object properties of an object handle.
*
* params: PTPParams*
* uint32_t objectid - object format code
* uint16_t opc - object prop code
*
* Return values: Some PTP_RC_* code.
*
**/
uint16_t
ptp_mtp_setobjectpropvalue (
PTPParams* params, uint32_t oid, uint16_t opc,
PTPPropertyValue *value, uint16_t datatype
) {
PTPContainer ptp;
uint16_t ret;
unsigned char *data = NULL;
unsigned int size ;
PTP_CNT_INIT(ptp);
ptp.Code=PTP_OC_MTP_SetObjectPropValue;
ptp.Nparam = 2;
ptp.Param1 = oid;
ptp.Param2 = opc;
size = ptp_pack_DPV(params, value, &data, datatype);
ret = ptp_transaction(params, &ptp, PTP_DP_SENDDATA, size, &data, NULL);
free(data);
return ret;
}
uint16_t
ptp_mtp_getobjectreferences (PTPParams* params, uint32_t handle, uint32_t** ohArray, uint32_t* arraylen)
{
PTPContainer ptp;
uint16_t ret;
unsigned char* dpv=NULL;
unsigned int dpvlen = 0;
PTP_CNT_INIT(ptp);
ptp.Code=PTP_OC_MTP_GetObjectReferences;
ptp.Param1=handle;
ptp.Nparam=1;
ret=ptp_transaction(params, &ptp, PTP_DP_GETDATA, 0, &dpv, &dpvlen);
if (ret == PTP_RC_OK) {
/* Sandisk Sansa skips the DATA phase, but returns OK as response.
* this will gives us a NULL here. Handle it. -Marcus */
if ((dpv == NULL) || (dpvlen == 0)) {
*arraylen = 0;
*ohArray = NULL;
} else {
*arraylen = ptp_unpack_uint32_t_array(params, dpv, 0, ohArray);
}
}
free(dpv);
return ret;
}
uint16_t
ptp_mtp_setobjectreferences (PTPParams* params, uint32_t handle, uint32_t* ohArray, uint32_t arraylen)
{
PTPContainer ptp;
uint16_t ret;
uint32_t size;
unsigned char* dpv=NULL;
PTP_CNT_INIT(ptp);
ptp.Code = PTP_OC_MTP_SetObjectReferences;
ptp.Param1 = handle;
ptp.Nparam = 1;
size = ptp_pack_uint32_t_array(params, ohArray, arraylen, &dpv);
ret = ptp_transaction(params, &ptp, PTP_DP_SENDDATA, size, (unsigned char **)&dpv, NULL);
free(dpv);
return ret;
}