bool MTPDevice::listDevices() { int raw_devices_cnt; LIBMTP_raw_device_t *raw_devices; // Do not output LIBMTP debug stuff StreamHelper::off(); LIBMTP_error_number_t err = LIBMTP_Detect_Raw_Devices( &raw_devices, &raw_devices_cnt); StreamHelper::on(); if (err != 0) { if (err == LIBMTP_ERROR_NO_DEVICE_ATTACHED) std::cerr << "No raw devices found.\n"; return false; } for (int i = 0; i < raw_devices_cnt; ++i) { std::cout << i + 1 << ": " << raw_devices[i].device_entry.vendor << raw_devices[i].device_entry.product << "\n"; } free(static_cast<void*>(raw_devices)); return true; }
/** * Get the first (as in "first in the list of") connected Vita MTP device. * @return a device pointer. NULL if error, no connected device, or no connected Vita * @see LIBMTP_Get_Connected_Devices() */ LIBMTP_mtpdevice_t *LIBVitaMTP_Get_First_Vita(void){ LIBMTP_mtpdevice_t *first_device = NULL; LIBMTP_raw_device_t *devices; int numdevs; int i; LIBMTP_error_number_t ret; ret = LIBMTP_Detect_Raw_Devices(&devices, &numdevs); if (ret != LIBMTP_ERROR_NONE){ return NULL; } if (devices == NULL || numdevs == 0){ return NULL; } for (i = 0; i < numdevs; i++){ if(devices[i].device_entry.vendor_id == VITA_VID && devices[i].device_entry.product_id == VITA_PID){ first_device = LIBMTP_Open_Raw_Device(&devices[i]); break; } LIBMTP_Release_Device(first_device); } free(devices); return first_device; // NULL if no vita }
static void rb_mtp_plugin_device_added (LibHalContext *context, const char *udi) { RBMtpPlugin *plugin = (RBMtpPlugin *) libhal_ctx_get_user_data (context); LIBMTP_raw_device_t *mtp_devices; int num_mtp_devices; LIBMTP_Detect_Raw_Devices (&mtp_devices, &num_mtp_devices); if (mtp_devices != NULL) { rb_mtp_plugin_maybe_add_source (plugin, udi, mtp_devices, num_mtp_devices); free (mtp_devices); } }
void Plugin::Upload (const QString& localPath, const QString& origPath, const QByteArray& devId, const QByteArray& storageId) { qDebug () << Q_FUNC_INFO << IsPolling_ << localPath << devId; if (IsPolling_) { UploadQueue_.append ({ localPath, origPath, devId, storageId }); return; } if (!DevicesCache_.contains (devId)) { qDebug () << "device not in cache, opening..."; bool found = false; LIBMTP_raw_device_t *rawDevices; int numRawDevices = 0; LIBMTP_Detect_Raw_Devices (&rawDevices, &numRawDevices); for (int i = 0; i < numRawDevices; ++i) { std::shared_ptr<LIBMTP_mtpdevice_t> device (LIBMTP_Open_Raw_Device (&rawDevices [i]), LIBMTP_Release_Device); if (!device) continue; const auto& serial = LIBMTP_Get_Serialnumber (device.get ()); qDebug () << "matching against" << serial; if (serial == devId) { DevicesCache_ [devId] = DeviceCacheEntry { std::move (device) }; found = true; break; } } free (rawDevices); if (!found) { qWarning () << Q_FUNC_INFO << "unable to find device" << devId; emit uploadFinished (localPath, QFile::ResourceError, tr ("Unable to find the requested device.")); return; } } const auto& entry = DevicesCache_ [devId]; UploadTo (entry.Device_.get (), storageId, localPath, origPath); }
bool MTPDevice::listDevices(bool verbose, const std::string &dev_file) { int raw_devices_cnt; LIBMTP_raw_device_t *raw_devices; // Do not output LIBMTP debug stuff StreamHelper::off(); LIBMTP_error_number_t err = LIBMTP_Detect_Raw_Devices( &raw_devices, &raw_devices_cnt); StreamHelper::on(); if (err != 0) { if (err == LIBMTP_ERROR_NO_DEVICE_ATTACHED) std::cerr << "No raw devices found.\n"; return false; } uint8_t bnum, dnum; if (!dev_file.empty() && !smtpfs_usb_devpath(dev_file, &bnum, &dnum)) { std::cerr << "Can not open such device '" << dev_file << "'.\n"; return false; } for (int i = 0; i < raw_devices_cnt; ++i) { if (!dev_file.empty() && !(bnum == raw_devices[i].bus_location && dnum == raw_devices[i].devnum)) continue; std::cout << i + 1 << ": " << (raw_devices[i].device_entry.vendor ? raw_devices[i].device_entry.vendor : "Unknown vendor ") << (raw_devices[i].device_entry.product ? raw_devices[i].device_entry.product : "Unknown product") << std::endl; #ifdef HAVE_LIBMTP_CHECK_CAPABILITY MTPDevice dev; if (verbose) { if (!dev.connect(&raw_devices[i])) return false; const MTPDevice::Capabilities &cap = dev.getCapabilities(); std::cout << " - can get partial object: " << (cap.canGetPartialObject() ? "yes" : "no") << std::endl; std::cout << " - can send partial object: " << (cap.canSendPartialObject() ? "yes" : "no") << std::endl; std::cout << " - can edit objects : " << (cap.canEditObjects() ? "yes" : "no") << std::endl; dev.disconnect(); } #endif } free(static_cast<void*>(raw_devices)); return true; }
/***************************************************************************** * Run: main thread *****************************************************************************/ static void *Run( void *data ) { LIBMTP_raw_device_t *p_rawdevices; int i_numrawdevices; int i_ret; int i_status = 0; services_discovery_t *p_sd = data; for(;;) { int canc = vlc_savecancel(); i_ret = LIBMTP_Detect_Raw_Devices( &p_rawdevices, &i_numrawdevices ); if ( i_ret == 0 && i_numrawdevices > 0 && p_rawdevices != NULL && i_status == 0 ) { /* Found a new device, add it */ msg_Dbg( p_sd, "New device found" ); if( AddDevice( p_sd, &p_rawdevices[0] ) == VLC_SUCCESS ) i_status = 1; else i_status = 2; } else { if ( ( i_ret != 0 || i_numrawdevices == 0 || p_rawdevices == NULL ) && i_status == 1) { /* The device is not connected anymore, delete it */ msg_Info( p_sd, "Device disconnected" ); CloseDevice( p_sd ); i_status = 0; } } free( p_rawdevices ); vlc_restorecancel(canc); if( i_status == 2 ) { msleep( 5000000 ); i_status = 0; } else msleep( 500000 ); } return NULL; }
int main (int argc, char **argv) { LIBMTP_raw_device_t *rawdevices; int numrawdevices; int i; LIBMTP_Init(); printf("Attempting to connect device(s)\n"); switch (LIBMTP_Detect_Raw_Devices(&rawdevices, &numrawdevices)) { case LIBMTP_ERROR_NO_DEVICE_ATTACHED: printf("mtp-folders: no devices found\n"); return 0; case LIBMTP_ERROR_CONNECTING: fprintf(stderr, "mtp-folders: There has been an error connecting. Exit\n"); return 1; case LIBMTP_ERROR_MEMORY_ALLOCATION: fprintf(stderr, "mtp-folders: Memory Allocation Error. Exit\n"); return 1; /* Unknown general errors - This should never execute */ case LIBMTP_ERROR_GENERAL: default: fprintf(stderr, "mtp-folders: Unknown error, please report " "this to the libmtp developers\n"); return 1; /* Successfully connected at least one device, so continue */ case LIBMTP_ERROR_NONE: printf("mtp-folders: Successfully connected\n"); } /* iterate through connected MTP devices */ for (i = 0; i < numrawdevices; i++) { LIBMTP_mtpdevice_t *device; LIBMTP_devicestorage_t *storage; char *friendlyname; int ret; device = LIBMTP_Open_Raw_Device(&rawdevices[i]); if (device == NULL) { fprintf(stderr, "Unable to open raw device %d\n", i); continue; } /* Echo the friendly name so we know which device we are working with */ friendlyname = LIBMTP_Get_Friendlyname(device); if (friendlyname == NULL) { printf("Friendly name: (NULL)\n"); } else { printf("Friendly name: %s\n", friendlyname); free(friendlyname); } LIBMTP_Dump_Errorstack(device); LIBMTP_Clear_Errorstack(device); /* Get all storages for this device */ ret = LIBMTP_Get_Storage(device, LIBMTP_STORAGE_SORTBY_NOTSORTED); if (ret != 0) { perror("LIBMTP_Get_Storage()\n"); LIBMTP_Dump_Errorstack(device); LIBMTP_Clear_Errorstack(device); continue; } /* Loop over storages, dump folder for each one */ for (storage = device->storage; storage != 0; storage = storage->next) { LIBMTP_folder_t *folders; printf("Storage: %s\n", storage->StorageDescription); folders = LIBMTP_Get_Folder_List_For_Storage(device, storage->id); if (folders == NULL) { fprintf(stdout, "No folders found\n"); LIBMTP_Dump_Errorstack(device); LIBMTP_Clear_Errorstack(device); } else { dump_folder_list(folders,0); } LIBMTP_destroy_folder_t(folders); } LIBMTP_Release_Device(device); } free(rawdevices); printf("OK.\n"); return 0; }
static void impl_activate (PeasActivatable *bplugin) { RBMtpPlugin *plugin = RB_MTP_PLUGIN (bplugin); GtkUIManager *uimanager = NULL; RBRemovableMediaManager *rmm; char *file = NULL; RBShell *shell; #if defined(HAVE_GUDEV) gboolean rmm_scanned = FALSE; #else int num_mtp_devices; LIBMTP_raw_device_t *mtp_devices; #endif g_object_get (plugin, "object", &shell, NULL); g_object_get (shell, "ui-manager", &uimanager, "removable-media-manager", &rmm, NULL); /* ui */ rb_media_player_source_init_actions (shell); plugin->action_group = gtk_action_group_new ("MTPActions"); gtk_action_group_set_translation_domain (plugin->action_group, GETTEXT_PACKAGE); _rb_action_group_add_display_page_actions (plugin->action_group, G_OBJECT (shell), rb_mtp_plugin_actions, G_N_ELEMENTS (rb_mtp_plugin_actions)); gtk_ui_manager_insert_action_group (uimanager, plugin->action_group, 0); file = rb_find_plugin_data_file (G_OBJECT (bplugin), "mtp-ui.xml"); plugin->ui_merge_id = gtk_ui_manager_add_ui_from_file (uimanager, file, NULL); g_object_unref (uimanager); g_object_unref (shell); /* device detection */ #if defined(HAVE_GUDEV) plugin->create_device_source_id = g_signal_connect_object (rmm, "create-source-device", G_CALLBACK (create_source_device_cb), plugin, 0); /* only scan if we're being loaded after the initial scan has been done */ g_object_get (rmm, "scanned", &rmm_scanned, NULL); if (rmm_scanned) rb_removable_media_manager_scan (rmm); #else if (rb_mtp_plugin_setup_dbus_hal_connection (plugin) == FALSE) { rb_debug ("not scanning for MTP devices because we couldn't get a HAL context"); g_object_unref (rmm); return; } rb_profile_start ("scanning for MTP devices"); LIBMTP_Detect_Raw_Devices (&mtp_devices, &num_mtp_devices); if (num_mtp_devices > 0) { int num_hal_devices; char **hal_devices; int i; rb_debug ("%d MTP devices found", num_mtp_devices); hal_devices = libhal_get_all_devices (plugin->hal_context, &num_hal_devices, NULL); for (i = 0; i < num_hal_devices; i++) { /* should narrow this down a bit - usb only, for a start */ rb_mtp_plugin_maybe_add_source (plugin, hal_devices[i], mtp_devices, num_mtp_devices); } libhal_free_string_array (hal_devices); } if (mtp_devices != NULL) { free (mtp_devices); } rb_profile_end ("scanning for MTP devices"); #endif g_object_unref (rmm); }
int main (int argc, char **argv) { LIBMTP_raw_device_t * rawdevices; int numrawdevices; LIBMTP_error_number_t err; int i; int opt; extern int optind; extern char *optarg; while ((opt = getopt(argc, argv, "d")) != -1 ) { switch (opt) { case 'd': LIBMTP_Set_Debug(LIBMTP_DEBUG_PTP | LIBMTP_DEBUG_DATA); break; } } argc -= optind; argv += optind; LIBMTP_Init(); err = LIBMTP_Detect_Raw_Devices(&rawdevices, &numrawdevices); switch(err) { case LIBMTP_ERROR_NO_DEVICE_ATTACHED: fprintf(stdout, " No raw devices found.\n"); return 0; case LIBMTP_ERROR_CONNECTING: fprintf(stderr, "Detect: There has been an error connecting. Exiting\n"); return 1; case LIBMTP_ERROR_MEMORY_ALLOCATION: fprintf(stderr, "Detect: Encountered a Memory Allocation Error. Exiting\n"); return 1; case LIBMTP_ERROR_NONE: break; case LIBMTP_ERROR_GENERAL: default: fprintf(stderr, "Unknown connection error.\n"); return 1; } /* Iterate over connected MTP devices */ fprintf(stdout, "Attempting to connect device(s)\n"); for (i = 0; i < numrawdevices; i++) { LIBMTP_mtpdevice_t *device; LIBMTP_devicestorage_t *storage; char *friendlyname; int ret; device = LIBMTP_Open_Raw_Device_Uncached(&rawdevices[i]); if (device == NULL) { fprintf(stderr, "Unable to open raw device %d\n", i); continue; } LIBMTP_Dump_Errorstack(device); LIBMTP_Clear_Errorstack(device); friendlyname = LIBMTP_Get_Friendlyname(device); if (friendlyname == NULL) { printf("Device: (NULL)\n"); } else { printf("Device: %s\n", friendlyname); free(friendlyname); } /* Get all storages for this device */ ret = LIBMTP_Get_Storage(device, LIBMTP_STORAGE_SORTBY_NOTSORTED); if (ret != 0) { perror("LIBMTP_Get_Storage()"); goto bailout; } /* Loop over storages */ for (storage = device->storage; storage != 0; storage = storage->next) { fprintf(stdout, "Storage: %s\n", storage->StorageDescription); recursive_file_tree(device, storage, 0, 0); } bailout: LIBMTP_Release_Device(device); } /* End For Loop */ free(rawdevices); printf("OK.\n"); return 0; }
int main(int argc, char **argv) { int mainret = 0; if (argc < 2) { fprintf(stderr, "usage: %s [-d] mountpoint\n", argv[0]); exit(1); } char *mount_point = argv[1]; argc -= 1; argv += 1; // LIBMTP_Set_Debug(LIBMTP_DEBUG_PTP | LIBMTP_DEBUG_DATA); LIBMTP_Init(); LIBMTP_error_number_t mtperr; LIBMTP_raw_device_t *rawdevs; int nrawdev; mtperr = LIBMTP_Detect_Raw_Devices(&rawdevs, &nrawdev); switch (mtperr) { case LIBMTP_ERROR_NO_DEVICE_ATTACHED: fprintf(stderr, "no raw devices\n"); exit(0); case LIBMTP_ERROR_CONNECTING: fprintf(stderr, "ERROR connecting\n"); exit(1); case LIBMTP_ERROR_MEMORY_ALLOCATION: fprintf(stderr, "ERROR memory allocation\n"); exit(1); case LIBMTP_ERROR_NONE: break; default: fprintf(stderr, "ERROR unknown\n"); exit(1); } printf("%d raw devices:\n", nrawdev); int i; for (i = 0; i < nrawdev; ++i) { printf(" device #%d: %s: %s (%04X:%04X) @ bus %d, dev %d\n", i, rawdevs[i].device_entry.vendor, rawdevs[i].device_entry.product, rawdevs[i].device_entry.vendor_id, rawdevs[i].device_entry.product_id, rawdevs[i].bus_location, rawdevs[i].devnum); } mtpfuse_t ctx; memset(&ctx, 0, sizeof(ctx)); printf("opening raw device #0...\n"); ctx.dev = LIBMTP_Open_Raw_Device_Uncached(&rawdevs[0]); if (!ctx.dev) { fprintf(stderr, "ERROR opening raw device #0\n"); exit(1); } LIBMTP_Dump_Errorstack(ctx.dev); LIBMTP_Clear_Errorstack(ctx.dev); char *name = LIBMTP_Get_Friendlyname(ctx.dev); printf("opened raw device %s.\n", name); int ret = LIBMTP_Get_Storage(ctx.dev, LIBMTP_STORAGE_SORTBY_NOTSORTED); if (ret != 0) { fprintf(stderr, "ERROR get %s's storage:\n", name); LIBMTP_Dump_Errorstack(ctx.dev); mainret = 1; goto done; } printf("device's storage:\n"); LIBMTP_devicestorage_t *storage; for (storage = ctx.dev->storage, i = 0; storage; storage = storage->next, ++i) { printf(" storage #%d: %d - %s\n", i, storage->id, storage->StorageDescription); } if (i <= 0) { fprintf(stderr, "there is no storage that is usable.\n"); mainret = 1; goto done; } mainret = fuse_main(argc, argv, &mtpfuse_ops, &ctx); done: LIBMTP_Release_Device(ctx.dev); return mainret; }
static int Device_init(Device *self, PyObject *args, PyObject *kwds) { unsigned long busnum; unsigned char devnum; unsigned short vendor_id, product_id; PyObject *usb_serialnum; char *vendor, *product, *friendly_name, *manufacturer_name, *model_name, *serial_number, *device_version; LIBMTP_raw_device_t *rawdevs = NULL, rdev; int numdevs, c, tried_count = 0; LIBMTP_mtpdevice_t *dev = NULL; LIBMTP_error_number_t err; if (!PyArg_ParseTuple(args, "kBHHssO", &busnum, &devnum, &vendor_id, &product_id, &vendor, &product, &usb_serialnum)) return -1; // We have to build and search the rawdevice list instead of creating a // rawdevice directly as otherwise, dynamic bug flag assignment in libmtp // does not work Py_BEGIN_ALLOW_THREADS; err = LIBMTP_Detect_Raw_Devices(&rawdevs, &numdevs); Py_END_ALLOW_THREADS; if (err == LIBMTP_ERROR_NO_DEVICE_ATTACHED) { PyErr_SetString(MTPError, "No raw devices found"); return -1; } if (err == LIBMTP_ERROR_CONNECTING) { PyErr_SetString(MTPError, "There has been an error connecting"); return -1; } if (err == LIBMTP_ERROR_MEMORY_ALLOCATION) { PyErr_NoMemory(); return -1; } if (err != LIBMTP_ERROR_NONE) { PyErr_SetString(MTPError, "Failed to detect raw MTP devices"); return -1; } for (c = 0; c < numdevs; c++) { rdev = rawdevs[c]; if (rdev.bus_location == (uint32_t)busnum && rdev.devnum == (uint8_t)devnum) { tried_count += 1; Py_BEGIN_ALLOW_THREADS; dev = LIBMTP_Open_Raw_Device_Uncached(&rdev); Py_END_ALLOW_THREADS; if (dev != NULL) break; } } if (rawdevs != NULL) free(rawdevs); if (dev == NULL) { if (tried_count == 0) PyErr_Format(MTPError, "No device with busnum=%lu and devnum=%u found", busnum, devnum); else PyErr_Format(MTPError, "Unable to open MTP device with busnum=%lu and devnum=%u, tried %d such devices", busnum, devnum, tried_count); return -1; } self->device = dev; self->ids = Py_BuildValue("kBHHO", busnum, devnum, vendor_id, product_id, usb_serialnum); if (self->ids == NULL) return -1; Py_BEGIN_ALLOW_THREADS; friendly_name = LIBMTP_Get_Friendlyname(self->device); manufacturer_name = LIBMTP_Get_Manufacturername(self->device); model_name = LIBMTP_Get_Modelname(self->device); serial_number = LIBMTP_Get_Serialnumber(self->device); device_version = LIBMTP_Get_Deviceversion(self->device); Py_END_ALLOW_THREADS; if (friendly_name != NULL) { self->friendly_name = PyUnicode_FromString(friendly_name); free(friendly_name); } if (self->friendly_name == NULL) { self->friendly_name = Py_None; Py_INCREF(Py_None); } if (manufacturer_name != NULL) { self->manufacturer_name = PyUnicode_FromString(manufacturer_name); free(manufacturer_name); } if (self->manufacturer_name == NULL) { self->manufacturer_name = Py_None; Py_INCREF(Py_None); } if (model_name != NULL) { self->model_name = PyUnicode_FromString(model_name); free(model_name); } if (self->model_name == NULL) { self->model_name = Py_None; Py_INCREF(Py_None); } if (serial_number != NULL) { self->serial_number = PyUnicode_FromString(serial_number); free(serial_number); } if (self->serial_number == NULL) { self->serial_number = Py_None; Py_INCREF(Py_None); } if (device_version != NULL) { self->device_version = PyUnicode_FromString(device_version); free(device_version); } if (self->device_version == NULL) { self->device_version = Py_None; Py_INCREF(Py_None); } return 0; }
bool MTPDevice::connect_priv(int dev_no, const std::string &dev_file) { if (m_device) { logerr("Already connected.\n"); return true; } int raw_devices_cnt; LIBMTP_raw_device_t *raw_devices; // Do not output LIBMTP debug stuff StreamHelper::off(); LIBMTP_error_number_t err = LIBMTP_Detect_Raw_Devices( &raw_devices, &raw_devices_cnt); StreamHelper::on(); if (err != LIBMTP_ERROR_NONE) { switch(err) { case LIBMTP_ERROR_NO_DEVICE_ATTACHED: logerr("No raw devices found.\n"); break; case LIBMTP_ERROR_CONNECTING: logerr("There has been an error connecting. Exiting.\n"); break; case LIBMTP_ERROR_MEMORY_ALLOCATION: logerr("Encountered a Memory Allocation Error. Exiting.\n"); break; case LIBMTP_ERROR_GENERAL: logerr("General error occured. Exiting.\n"); break; case LIBMTP_ERROR_USB_LAYER: logerr("USB Layer error occured. Exiting.\n"); break; default: break; } return false; } #ifndef HAVE_LIBUSB1 if (!dev_file.empty()) { uint8_t bnum, dnum; dev_no = raw_devices_cnt; if (smtpfs_usb_devpath(dev_file, &bnum, &dnum)) for (dev_no = 0; dev_no < raw_devices_cnt; ++dev_no) if (bnum == raw_devices[dev_no].bus_location && dnum == raw_devices[dev_no].devnum) break; if (dev_no == raw_devices_cnt) { logerr("Can not open such device '", dev_file, "'.\n"); free(static_cast<void*>(raw_devices)); return false; } } #endif // !HAVE_LIBUSB1 if (dev_no < 0 || dev_no >= raw_devices_cnt) { logerr("Can not connect to device no. ", dev_no + 1, ".\n"); free(static_cast<void*>(raw_devices)); return false; } LIBMTP_raw_device_t *raw_device = &raw_devices[dev_no]; // Do not output LIBMTP debug stuff StreamHelper::off(); m_device = LIBMTP_Open_Raw_Device_Uncached(raw_device); StreamHelper::on(); free(static_cast<void*>(raw_devices)); if (!m_device) { LIBMTP_Dump_Errorstack(m_device); return false; } if (!enumStorages()) return false; // Retrieve capabilities. m_capabilities = MTPDevice::getCapabilities(*this); logmsg("Connected.\n"); return true; }
int main (int argc, char **argv) { LIBMTP_raw_device_t * rawdevices; int numrawdevices; LIBMTP_error_number_t err; int i; int opt; extern int optind; extern char *optarg; while ((opt = getopt(argc, argv, "d")) != -1 ) { switch (opt) { case 'd': LIBMTP_Set_Debug(LIBMTP_DEBUG_PTP | LIBMTP_DEBUG_DATA); break; } } argc -= optind; argv += optind; LIBMTP_Init(); fprintf(stdout, "libmtp version: " LIBMTP_VERSION_STRING "\n\n"); fprintf(stdout, "Listing raw device(s)\n"); err = LIBMTP_Detect_Raw_Devices(&rawdevices, &numrawdevices); switch(err) { case LIBMTP_ERROR_NO_DEVICE_ATTACHED: fprintf(stdout, " No raw devices found.\n"); return 0; case LIBMTP_ERROR_CONNECTING: fprintf(stderr, "Detect: There has been an error connecting. Exiting\n"); return 1; case LIBMTP_ERROR_MEMORY_ALLOCATION: fprintf(stderr, "Detect: Encountered a Memory Allocation Error. Exiting\n"); return 1; case LIBMTP_ERROR_NONE: { int i; fprintf(stdout, " Found %d device(s):\n", numrawdevices); for (i = 0; i < numrawdevices; i++) { if (rawdevices[i].device_entry.vendor != NULL || rawdevices[i].device_entry.product != NULL) { fprintf(stdout, " %s: %s (%04x:%04x) @ bus %d, dev %d\n", rawdevices[i].device_entry.vendor, rawdevices[i].device_entry.product, rawdevices[i].device_entry.vendor_id, rawdevices[i].device_entry.product_id, rawdevices[i].bus_location, rawdevices[i].devnum); } else { fprintf(stdout, " %04x:%04x @ bus %d, dev %d\n", rawdevices[i].device_entry.vendor_id, rawdevices[i].device_entry.product_id, rawdevices[i].bus_location, rawdevices[i].devnum); } } } break; case LIBMTP_ERROR_GENERAL: default: fprintf(stderr, "Unknown connection error.\n"); return 1; } /* Iterate over connected MTP devices */ fprintf(stdout, "Attempting to connect device(s)\n"); for (i = 0; i < numrawdevices; i++) { LIBMTP_mtpdevice_t *device; LIBMTP_devicestorage_t *storage; char *friendlyname; char *syncpartner; char *sectime; char *devcert; uint16_t *filetypes; uint16_t filetypes_len; uint8_t maxbattlevel; uint8_t currbattlevel; int ret; device = LIBMTP_Open_Raw_Device_Uncached(&rawdevices[i]); if (device == NULL) { fprintf(stderr, "Unable to open raw device %d\n", i); continue; } LIBMTP_Dump_Errorstack(device); LIBMTP_Clear_Errorstack(device); LIBMTP_Dump_Device_Info(device); printf("MTP-specific device properties:\n"); // The friendly name friendlyname = LIBMTP_Get_Friendlyname(device); if (friendlyname == NULL) { fprintf(stdout, " Friendly name: (NULL)\n"); } else { fprintf(stdout, " Friendly name: %s\n", friendlyname); free(friendlyname); } syncpartner = LIBMTP_Get_Syncpartner(device); if (syncpartner == NULL) { fprintf(stdout, " Synchronization partner: (NULL)\n"); } else { fprintf(stdout, " Synchronization partner: %s\n", syncpartner); free(syncpartner); } // Some battery info ret = LIBMTP_Get_Batterylevel(device, &maxbattlevel, &currbattlevel); if (ret == 0) { fprintf(stdout, " Battery level %d of %d (%d%%)\n",currbattlevel, maxbattlevel, (int) ((float) currbattlevel/ (float) maxbattlevel * 100.0)); } else { // Silently ignore. Some devices does not support getting the // battery level. LIBMTP_Clear_Errorstack(device); } ret = LIBMTP_Get_Supported_Filetypes(device, &filetypes, &filetypes_len); if (ret == 0) { uint16_t i; printf("libmtp supported (playable) filetypes:\n"); for (i = 0; i < filetypes_len; i++) { fprintf(stdout, " %s\n", LIBMTP_Get_Filetype_Description(filetypes[i])); } } else { LIBMTP_Dump_Errorstack(device); LIBMTP_Clear_Errorstack(device); } // Secure time XML fragment ret = LIBMTP_Get_Secure_Time(device, §ime); if (ret == 0 && sectime != NULL) { fprintf(stdout, "\nSecure Time:\n%s\n", sectime); free(sectime); } else { // Silently ignore - there may be devices not supporting secure time. LIBMTP_Clear_Errorstack(device); } // Device certificate XML fragment if (rawdevices[i].device_entry.vendor_id == 0x041e) { /* * This code is currently disabled except for vendors we * know does support it: all devices say that * they support getting a device certificate but a lot of * them obviously doesn't, instead they crash when you try * to obtain it. */ ret = LIBMTP_Get_Device_Certificate(device, &devcert); if (ret == 0 && devcert != NULL) { fprintf(stdout, "\nDevice Certificate:\n%s\n", devcert); free(devcert); } else { fprintf(stdout, "Unable to acquire device certificate, perhaps this device " "does not support this\n"); LIBMTP_Dump_Errorstack(device); LIBMTP_Clear_Errorstack(device); } } /* Try to get Media player device info XML file... */ /* Loop over storages */ for (storage = device->storage; storage != 0; storage = storage->next) { LIBMTP_file_t *files; /* Get file listing for the root directory, no other dirs */ files = LIBMTP_Get_Files_And_Folders(device, storage->id, 0); if (files != NULL) { LIBMTP_file_t *file, *tmp; file = files; while (file != NULL) { if (!strcmp(file->filename, "WMPInfo.xml") || !strcmp(file->filename, "WMPinfo.xml") || !strcmp(file->filename, "default-capabilities.xml")) { if (file->item_id != 0) { /* Dump this file */ FILE *xmltmp = tmpfile(); int tmpfiledescriptor = fileno(xmltmp); if (tmpfiledescriptor != -1) { int ret = LIBMTP_Get_Track_To_File_Descriptor(device, file->item_id, tmpfiledescriptor, NULL, NULL); if (ret == 0) { uint8_t *buf = NULL; uint32_t readbytes; buf = malloc(XML_BUFSIZE); if (buf == NULL) { printf("Could not allocate %08x bytes...\n", XML_BUFSIZE); LIBMTP_Dump_Errorstack(device); LIBMTP_Clear_Errorstack(device); free(rawdevices); return 1; } lseek(tmpfiledescriptor, 0, SEEK_SET); readbytes = read(tmpfiledescriptor, (void*) buf, XML_BUFSIZE); if (readbytes >= 2 && readbytes < XML_BUFSIZE) { fprintf(stdout, "\n%s file contents:\n", file->filename); dump_xml_fragment(buf, readbytes); } else { perror("Unable to read file"); LIBMTP_Dump_Errorstack(device); LIBMTP_Clear_Errorstack(device); } free(buf); } else { LIBMTP_Dump_Errorstack(device); LIBMTP_Clear_Errorstack(device); } fclose(xmltmp); } } } tmp = file; file = file->next; LIBMTP_destroy_file_t(tmp); } } } LIBMTP_Release_Device(device); } /* End For Loop */ free(rawdevices); printf("OK.\n"); return 0; }
bool MTPDevice::connect(int dev_no) { if (m_device) { logerr("Already connected.\n"); return true; } int raw_devices_cnt; LIBMTP_raw_device_t *raw_devices; // Do not output LIBMTP debug stuff StreamHelper::off(); LIBMTP_error_number_t err = LIBMTP_Detect_Raw_Devices( &raw_devices, &raw_devices_cnt); StreamHelper::on(); if (dev_no < 0 || dev_no >= raw_devices_cnt) { logerr("Can not connect to device no. ", dev_no + 1, ".\n"); free(static_cast<void*>(raw_devices)); return false; } if (err != LIBMTP_ERROR_NONE) { switch(err) { case LIBMTP_ERROR_NO_DEVICE_ATTACHED: logerr("No raw devices found.\n"); break; case LIBMTP_ERROR_CONNECTING: logerr("There has been an error connecting. Exiting.\n"); break; case LIBMTP_ERROR_MEMORY_ALLOCATION: logerr("Encountered a Memory Allocation Error. Exiting.\n"); break; case LIBMTP_ERROR_GENERAL: logerr("General error occured. Exiting.\n"); break; case LIBMTP_ERROR_USB_LAYER: logerr("USB Layer error occured. Exiting.\n"); break; default: break; } return false; } LIBMTP_raw_device_t *raw_device = &raw_devices[dev_no]; #ifdef HAVE_LIBUSB1 // Try to reset USB device, so we don't wait until LIBMTP times out. // We do this every time we are about to mount a device, but better // connect on first try, than wait for 60s timeout. smtpfs_reset_device(raw_device); #endif // HAVE_LIBUSB1 // Do not output LIBMTP debug stuff StreamHelper::off(); m_device = LIBMTP_Open_Raw_Device_Uncached(raw_device); StreamHelper::on(); free(static_cast<void*>(raw_devices)); if (!m_device) { LIBMTP_Dump_Errorstack(m_device); return false; } if (!enumStorages()) return false; logmsg("Connected.\n"); return true; }