diff options
author | Linus Walleij <triad@df.lth.se> | 2008-05-31 23:22:28 +0000 |
---|---|---|
committer | Linus Walleij <triad@df.lth.se> | 2008-05-31 23:22:28 +0000 |
commit | 3c643259ce83980ab8b1917fe5bbbb0b5aeeb06e (patch) | |
tree | ad66601dabe357b5cbaa070ceeea0bc62301d615 | |
parent | 64691b7096cef300b374cf5b241e4dc1a97107e3 (diff) | |
download | libmtp-3c643259ce83980ab8b1917fe5bbbb0b5aeeb06e.tar.gz |
Move params creation away from usb layer.
-rw-r--r-- | ChangeLog | 5 | ||||
-rw-r--r-- | src/libmtp.c | 51 | ||||
-rw-r--r-- | src/libusb-glue.c | 36 | ||||
-rw-r--r-- | src/libusb-glue.h | 6 |
4 files changed, 54 insertions, 44 deletions
@@ -2,7 +2,10 @@ * src/libmtp.c: use LIBMTP_Detect_Raw_Devices() and LIBMTP_Open_Raw_Device() inside - LIBMTP_Get_First_Device() refactoring. + LIBMTP_Get_First_Device() refactoring. Move + params creation into this file. + * src/libusb-glue.c: move params creation away + from here. 2008-05-30 Linus Walleij <triad@df.lth.se> diff --git a/src/libmtp.c b/src/libmtp.c index f6323d0..a4c5733 100644 --- a/src/libmtp.c +++ b/src/libmtp.c @@ -859,6 +859,7 @@ LIBMTP_mtpdevice_t *LIBMTP_Open_Raw_Device(LIBMTP_raw_device_t *rawdevice) LIBMTP_mtpdevice_t *mtp_device; uint8_t bs = 0; PTPParams *current_params; + PTP_USB *ptp_usb; LIBMTP_error_number_t err; int i; @@ -876,20 +877,47 @@ LIBMTP_mtpdevice_t *LIBMTP_Open_Raw_Device(LIBMTP_raw_device_t *rawdevice) return NULL; } - - /* Create params and usbinfo */ - err = configure_usb_device(rawdevice, - (PTPParams **) &mtp_device->params, - &mtp_device->usbinfo); - if (err != LIBMTP_ERROR_NONE) + + /* Create PTP params */ + current_params = (PTPParams *) malloc(sizeof(PTPParams)); + if (current_params == NULL) { + free(mtp_device); return NULL; - - current_params = mtp_device->params; - - /* Clear any handlers */ + } + memset(current_params, 0, sizeof(PTPParams)); + /* Clear all handlers */ current_params->handles.Handler = NULL; current_params->objectinfo = NULL; current_params->props = NULL; + /* TODO: Will this always be little endian? */ + current_params->byteorder = PTP_DL_LE; + current_params->cd_locale_to_ucs2 = iconv_open("UCS-2LE", "UTF-8"); + current_params->cd_ucs2_to_locale = iconv_open("UTF-8", "UCS-2LE"); + + if(current_params->cd_locale_to_ucs2 == (iconv_t) -1 || + current_params->cd_ucs2_to_locale == (iconv_t) -1) { + fprintf(stderr, "LIBMTP PANIC: Cannot open iconv() converters to/from UCS-2!\n" + "Too old stdlibc, glibc and libiconv?\n"); + free(current_params); + free(mtp_device); + return NULL; + } + mtp_device->params = current_params; + + + /* Create usbinfo, this also opens the session */ + err = configure_usb_device(rawdevice, + current_params, + &mtp_device->usbinfo); + if (err != LIBMTP_ERROR_NONE) { + free(current_params); + free(mtp_device); + return NULL; + } + ptp_usb = (PTP_USB*) mtp_device->usbinfo; + /* Set pointer back to params */ + ptp_usb->params = current_params; + /* Cache the device information for later use */ if (ptp_getdeviceinfo(current_params, @@ -902,8 +930,7 @@ LIBMTP_mtpdevice_t *LIBMTP_Open_Raw_Device(LIBMTP_raw_device_t *rawdevice) free(mtp_device->usbinfo); free(mtp_device->params); current_params = NULL; - free(mtp_device); - + free(mtp_device); return NULL; } diff --git a/src/libusb-glue.c b/src/libusb-glue.c index b87409d..54367df 100644 --- a/src/libusb-glue.c +++ b/src/libusb-glue.c @@ -1580,15 +1580,13 @@ static void find_interface_and_endpoints(struct usb_device *dev, * This function assigns params and usbinfo given a raw device * as input. * @param device the device to be assigned. - * @param params a pointer to the new params. * @param usbinfo a pointer to the new usbinfo. * @return an error code. */ LIBMTP_error_number_t configure_usb_device(LIBMTP_raw_device_t *device, - PTPParams **params, + PTPParams *params, void **usbinfo) { - PTPParams *locparams; PTP_USB *ptp_usb; struct usb_device *libusb_device; uint16_t ret = 0; @@ -1620,33 +1618,16 @@ LIBMTP_error_number_t configure_usb_device(LIBMTP_raw_device_t *device, } /* Allocate structs */ - locparams = (PTPParams *) malloc(sizeof(PTPParams)); ptp_usb = (PTP_USB *) malloc(sizeof(PTP_USB)); - if (locparams == NULL || ptp_usb == NULL) { + if (ptp_usb == NULL) { return LIBMTP_ERROR_MEMORY_ALLOCATION; } /* Start with a blank slate (includes setting device_flags to 0) */ - memset(locparams, 0, sizeof(PTPParams)); memset(ptp_usb, 0, sizeof(PTP_USB)); - /* Pointer back to params */ - ptp_usb->params = locparams; - /* Copy flags, TODO: move the entire device_entry into PTP_USB instead? */ ptp_usb->device_flags = device->device_entry.device_flags; - /* TODO: Will this always be little endian? */ - locparams->byteorder = PTP_DL_LE; - locparams->cd_locale_to_ucs2 = iconv_open("UCS-2LE", "UTF-8"); - locparams->cd_ucs2_to_locale = iconv_open("UTF-8", "UCS-2LE"); - - if(locparams->cd_locale_to_ucs2 == (iconv_t) -1 || - locparams->cd_ucs2_to_locale == (iconv_t) -1) { - fprintf(stderr, "LIBMTP PANIC: Cannot open iconv() converters to/from UCS-2!\n" - "Too old stdlibc, glibc and libiconv?\n"); - return LIBMTP_ERROR_CONNECTING; - } - /* Assign endpoints to usbinfo... */ find_interface_and_endpoints(libusb_device, &ptp_usb->interface, @@ -1657,7 +1638,7 @@ LIBMTP_error_number_t configure_usb_device(LIBMTP_raw_device_t *device, &ptp_usb->intep); /* Attempt to initialize this device */ - if (init_ptp_usb(locparams, ptp_usb, libusb_device) < 0) { + if (init_ptp_usb(params, ptp_usb, libusb_device) < 0) { fprintf(stderr, "LIBMTP PANIC: Unable to initialize device\n"); return LIBMTP_ERROR_CONNECTING; } @@ -1666,24 +1647,24 @@ LIBMTP_error_number_t configure_usb_device(LIBMTP_raw_device_t *device, * This works in situations where previous bad applications * have not used LIBMTP_Release_Device on exit */ - if ((ret = ptp_opensession(locparams, 1)) == PTP_ERROR_IO) { + if ((ret = ptp_opensession(params, 1)) == PTP_ERROR_IO) { fprintf(stderr, "PTP_ERROR_IO: Trying again after re-initializing USB interface\n"); close_usb(ptp_usb); - if(init_ptp_usb(locparams, ptp_usb, libusb_device) <0) { + if(init_ptp_usb(params, ptp_usb, libusb_device) <0) { fprintf(stderr, "LIBMTP PANIC: Could not open session on device\n"); return LIBMTP_ERROR_CONNECTING; } /* Device has been reset, try again */ - ret = ptp_opensession(locparams, 1); + ret = ptp_opensession(params, 1); } /* Was the transaction id invalid? Try again */ if (ret == PTP_RC_InvalidTransactionID) { fprintf(stderr, "LIBMTP WARNING: Transaction ID was invalid, increment and try again\n"); - locparams->transaction_id += 10; - ret = ptp_opensession(locparams, 1); + params->transaction_id += 10; + ret = ptp_opensession(params, 1); } if (ret != PTP_RC_SessionAlreadyOpened && ret != PTP_RC_OK) { @@ -1696,7 +1677,6 @@ LIBMTP_error_number_t configure_usb_device(LIBMTP_raw_device_t *device, } /* OK configured properly */ - *params = locparams; *usbinfo = (void *) ptp_usb; return LIBMTP_ERROR_NONE; } diff --git a/src/libusb-glue.h b/src/libusb-glue.h index 0b677c2..3d4aa7a 100644 --- a/src/libusb-glue.h +++ b/src/libusb-glue.h @@ -60,9 +60,9 @@ struct _PTP_USB { int open_device (int busn, int devn, short force, PTP_USB *ptp_usb, PTPParams *params, struct usb_device **dev); void dump_usbinfo(PTP_USB *ptp_usb); char const * const get_playlist_extension(PTP_USB *ptp_usb); -void close_device (PTP_USB *ptp_usb, PTPParams *params); -LIBMTP_error_number_t configure_usb_device(LIBMTP_raw_device_t *device, - PTPParams **params, +void close_device(PTP_USB *ptp_usb, PTPParams *params); +LIBMTP_error_number_t configure_usb_device(LIBMTP_raw_device_t *device, + PTPParams *params, void **usbinfo); /* connect_first_device return codes */ |