00001
00023 #include "common.h"
00024 #include <unistd.h>
00025 #include <stdlib.h>
00026 #include <stdio.h>
00027 #include <string.h>
00028 #include <errno.h>
00029
00030 #define XML_BUFSIZE 0x10000
00031
00032 static void dump_xml_fragment(uint8_t *buf, uint32_t len)
00033 {
00034 static int endianness = 0;
00035 uint32_t bp = 0;
00036
00037 while (bp < len) {
00038 if (buf[bp+0] == 0xFF && buf[bp+1] == 0xFE) {
00039 endianness = 0;
00040 } else if (buf[bp+0] == 0xFE && buf[bp+1] == 0xff) {
00041 endianness = 1;
00042 } else {
00043 uint16_t tmp;
00044
00045 if (endianness == 0) {
00046 tmp = buf[bp+1] << 8 | buf[bp+0];
00047 } else {
00048 tmp = buf[bp+0] << 8 | buf[bp+1];
00049 }
00050
00051
00052 printf("%c", (uint8_t) tmp);
00053 }
00054 bp += 2;
00055 }
00056 printf("\n");
00057 }
00058
00059 int main (int argc, char **argv)
00060 {
00061 LIBMTP_raw_device_t * rawdevices;
00062 int numrawdevices;
00063 LIBMTP_error_number_t err;
00064 int i;
00065
00066 LIBMTP_Init();
00067
00068 fprintf(stdout, "libmtp version: " LIBMTP_VERSION_STRING "\n\n");
00069
00070 fprintf(stdout, "Listing raw device(s)\n");
00071 err = LIBMTP_Detect_Raw_Devices(&rawdevices, &numrawdevices);
00072 switch(err) {
00073 case LIBMTP_ERROR_NO_DEVICE_ATTACHED:
00074 fprintf(stdout, " No raw devices found.\n");
00075 return 0;
00076 case LIBMTP_ERROR_CONNECTING:
00077 fprintf(stderr, "Detect: There has been an error connecting. Exiting\n");
00078 return 1;
00079 case LIBMTP_ERROR_MEMORY_ALLOCATION:
00080 fprintf(stderr, "Detect: Encountered a Memory Allocation Error. Exiting\n");
00081 return 1;
00082 case LIBMTP_ERROR_NONE:
00083 {
00084 int i;
00085
00086 fprintf(stdout, " Found %d device(s):\n", numrawdevices);
00087 for (i = 0; i < numrawdevices; i++) {
00088 if (rawdevices[i].device_entry.vendor != NULL ||
00089 rawdevices[i].device_entry.product != NULL) {
00090 fprintf(stdout, " %s: %s (%04x:%04x) @ bus %d, dev %d\n",
00091 rawdevices[i].device_entry.vendor,
00092 rawdevices[i].device_entry.product,
00093 rawdevices[i].device_entry.vendor_id,
00094 rawdevices[i].device_entry.product_id,
00095 rawdevices[i].bus_location,
00096 rawdevices[i].devnum);
00097 } else {
00098 fprintf(stdout, " %04x:%04x @ bus %d, dev %d\n",
00099 rawdevices[i].device_entry.vendor_id,
00100 rawdevices[i].device_entry.product_id,
00101 rawdevices[i].bus_location,
00102 rawdevices[i].devnum);
00103 }
00104 }
00105 }
00106 break;
00107 case LIBMTP_ERROR_GENERAL:
00108 default:
00109 fprintf(stderr, "Unknown connection error.\n");
00110 return 1;
00111 }
00112
00113
00114 fprintf(stdout, "Attempting to connect device(s)\n");
00115 for (i = 0; i < numrawdevices; i++) {
00116 LIBMTP_mtpdevice_t *device;
00117 LIBMTP_file_t *files;
00118 uint32_t xmlfileid = 0;
00119 char *friendlyname;
00120 char *syncpartner;
00121 char *sectime;
00122 char *devcert;
00123 uint16_t *filetypes;
00124 uint16_t filetypes_len;
00125 uint8_t maxbattlevel;
00126 uint8_t currbattlevel;
00127 int ret;
00128
00129 device = LIBMTP_Open_Raw_Device(&rawdevices[i]);
00130 if (device == NULL) {
00131 fprintf(stderr, "Unable to open raw device %d\n", i);
00132 continue;
00133 }
00134
00135 LIBMTP_Dump_Errorstack(device);
00136 LIBMTP_Clear_Errorstack(device);
00137 LIBMTP_Dump_Device_Info(device);
00138
00139 printf("MTP-specific device properties:\n");
00140
00141 friendlyname = LIBMTP_Get_Friendlyname(device);
00142 if (friendlyname == NULL) {
00143 fprintf(stdout, " Friendly name: (NULL)\n");
00144 } else {
00145 fprintf(stdout, " Friendly name: %s\n", friendlyname);
00146 free(friendlyname);
00147 }
00148 syncpartner = LIBMTP_Get_Syncpartner(device);
00149 if (syncpartner == NULL) {
00150 fprintf(stdout, " Synchronization partner: (NULL)\n");
00151 } else {
00152 fprintf(stdout, " Synchronization partner: %s\n", syncpartner);
00153 free(syncpartner);
00154 }
00155
00156
00157 ret = LIBMTP_Get_Batterylevel(device, &maxbattlevel, &currbattlevel);
00158 if (ret == 0) {
00159 fprintf(stdout, " Battery level %d of %d (%d%%)\n",currbattlevel, maxbattlevel,
00160 (int) ((float) currbattlevel/ (float) maxbattlevel * 100.0));
00161 } else {
00162
00163
00164 LIBMTP_Clear_Errorstack(device);
00165 }
00166
00167 ret = LIBMTP_Get_Supported_Filetypes(device, &filetypes, &filetypes_len);
00168 if (ret == 0) {
00169 uint16_t i;
00170
00171 printf("libmtp supported (playable) filetypes:\n");
00172 for (i = 0; i < filetypes_len; i++) {
00173 fprintf(stdout, " %s\n", LIBMTP_Get_Filetype_Description(filetypes[i]));
00174 }
00175 } else {
00176 LIBMTP_Dump_Errorstack(device);
00177 LIBMTP_Clear_Errorstack(device);
00178 }
00179
00180
00181 ret = LIBMTP_Get_Secure_Time(device, §ime);
00182 if (ret == 0 && sectime != NULL) {
00183 fprintf(stdout, "\nSecure Time:\n%s\n", sectime);
00184 free(sectime);
00185 } else {
00186
00187 LIBMTP_Clear_Errorstack(device);
00188 }
00189
00190
00191 ret = LIBMTP_Get_Device_Certificate(device, &devcert);
00192 if (ret == 0 && devcert != NULL) {
00193 fprintf(stdout, "\nDevice Certificate:\n%s\n", devcert);
00194 free(devcert);
00195 } else {
00196 fprintf(stdout, "Unable to acquire device certificate, perhaps this device "
00197 "does not support this\n");
00198 LIBMTP_Dump_Errorstack(device);
00199 LIBMTP_Clear_Errorstack(device);
00200 }
00201
00202
00203 files = LIBMTP_Get_Filelisting_With_Callback(device, NULL, NULL);
00204 if (files != NULL) {
00205 LIBMTP_file_t *file, *tmp;
00206 file = files;
00207 while (file != NULL) {
00208 if (!strcmp(file->filename, "WMPInfo.xml") ||
00209 !strcmp(file->filename, "WMPinfo.xml"))
00210 {
00211 xmlfileid = file->item_id;
00212 }
00213 tmp = file;
00214 file = file->next;
00215 LIBMTP_destroy_file_t(tmp);
00216 }
00217 }
00218 if (xmlfileid == 0)
00219 fprintf(stdout, "WMPInfo.xml Does not exist on this device\n");
00220 if (xmlfileid != 0) {
00221 FILE *xmltmp = tmpfile();
00222 int tmpfiledescriptor = fileno(xmltmp);
00223
00224 if (tmpfiledescriptor != -1) {
00225 int ret = LIBMTP_Get_Track_To_File_Descriptor(device,
00226 xmlfileid,
00227 tmpfiledescriptor,
00228 NULL,
00229 NULL);
00230 if (ret == 0) {
00231 uint8_t *buf = NULL;
00232 uint32_t readbytes;
00233
00234 buf = malloc(XML_BUFSIZE);
00235 if (buf == NULL) {
00236 printf("Could not allocate %08x bytes...\n", XML_BUFSIZE);
00237 LIBMTP_Dump_Errorstack(device);
00238 LIBMTP_Clear_Errorstack(device);
00239 free(rawdevices);
00240 return 1;
00241 }
00242
00243 lseek(tmpfiledescriptor, 0, SEEK_SET);
00244 readbytes = read(tmpfiledescriptor, (void*) buf, XML_BUFSIZE);
00245
00246 if (readbytes >= 2 && readbytes < XML_BUFSIZE) {
00247 fprintf(stdout, "\nDevice description WMPInfo.xml file:\n");
00248 dump_xml_fragment(buf, readbytes);
00249 } else {
00250 perror("Unable to read WMPInfo.xml");
00251 LIBMTP_Dump_Errorstack(device);
00252 LIBMTP_Clear_Errorstack(device);
00253 }
00254 free(buf);
00255 } else {
00256 LIBMTP_Dump_Errorstack(device);
00257 LIBMTP_Clear_Errorstack(device);
00258 }
00259 fclose(xmltmp);
00260 }
00261 }
00262 LIBMTP_Release_Device(device);
00263 }
00264
00265 free(rawdevices);
00266
00267 printf("OK.\n");
00268
00269 return 0;
00270 }