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_mtpdevice_t *device, *iter;
00062 LIBMTP_file_t *files;
00063 uint32_t xmlfileid = 0;
00064 char *friendlyname;
00065 char *syncpartner;
00066 char *sectime;
00067 char *devcert;
00068 uint16_t *filetypes;
00069 uint16_t filetypes_len;
00070 uint8_t maxbattlevel;
00071 uint8_t currbattlevel;
00072 uint32_t numdevices;
00073 int ret;
00074 int probeonly = 0;
00075
00076 LIBMTP_Init();
00077
00078 fprintf(stdout, "libmtp version: " LIBMTP_VERSION_STRING "\n\n");
00079 fprintf(stdout, "Attempting to connect device(s)\n");
00080
00081 switch(LIBMTP_Get_Connected_Devices(&device))
00082 {
00083 case LIBMTP_ERROR_NO_DEVICE_ATTACHED:
00084 fprintf(stdout, "Detect: No Devices have been found\n");
00085 return 0;
00086 case LIBMTP_ERROR_CONNECTING:
00087 fprintf(stderr, "Detect: There has been an error connecting. Exiting\n");
00088 return 1;
00089 case LIBMTP_ERROR_MEMORY_ALLOCATION:
00090 fprintf(stderr, "Detect: Encountered a Memory Allocation Error. Exiting\n");
00091 return 1;
00092
00093
00094 case LIBMTP_ERROR_GENERAL:
00095 default:
00096 fprintf(stderr, "Detect: There has been an unknown error, please report "
00097 "this to the libmtp developers\n");
00098 return 1;
00099
00100
00101 case LIBMTP_ERROR_NONE:
00102 numdevices = LIBMTP_Number_Devices_In_List(device);
00103 fprintf(stdout, "Detect: Successfully connected %u devices\n", numdevices);
00104 }
00105
00106
00107 for(iter = device; iter != NULL; iter = iter->next)
00108 {
00109
00110 LIBMTP_Dump_Errorstack(iter);
00111 LIBMTP_Clear_Errorstack(iter);
00112 LIBMTP_Dump_Device_Info(iter);
00113
00114 printf("MTP-specific device properties:\n");
00115
00116 friendlyname = LIBMTP_Get_Friendlyname(iter);
00117 if (friendlyname == NULL) {
00118 fprintf(stdout, " Friendly name: (NULL)\n");
00119 } else {
00120 fprintf(stdout, " Friendly name: %s\n", friendlyname);
00121 free(friendlyname);
00122 }
00123 syncpartner = LIBMTP_Get_Syncpartner(iter);
00124 if (syncpartner == NULL) {
00125 fprintf(stdout, " Synchronization partner: (NULL)\n");
00126 } else {
00127 fprintf(stdout, " Synchronization partner: %s\n", syncpartner);
00128 free(syncpartner);
00129 }
00130
00131
00132 ret = LIBMTP_Get_Batterylevel(iter, &maxbattlevel, &currbattlevel);
00133 if (ret == 0) {
00134 fprintf(stdout, " Battery level %d of %d (%d%%)\n",currbattlevel, maxbattlevel,
00135 (int) ((float) currbattlevel/ (float) maxbattlevel * 100.0));
00136 } else {
00137
00138
00139 LIBMTP_Clear_Errorstack(iter);
00140 }
00141
00142 ret = LIBMTP_Get_Supported_Filetypes(iter, &filetypes, &filetypes_len);
00143 if (ret == 0) {
00144 uint16_t i;
00145
00146 printf("libmtp supported (playable) filetypes:\n");
00147 for (i = 0; i < filetypes_len; i++) {
00148 fprintf(stdout, " %s\n", LIBMTP_Get_Filetype_Description(filetypes[i]));
00149 }
00150 } else {
00151 LIBMTP_Dump_Errorstack(iter);
00152 LIBMTP_Clear_Errorstack(iter);
00153 }
00154
00155
00156 ret = LIBMTP_Get_Secure_Time(iter, §ime);
00157 if (ret == 0 && sectime != NULL) {
00158 fprintf(stdout, "\nSecure Time:\n%s\n", sectime);
00159 free(sectime);
00160 } else {
00161
00162 LIBMTP_Clear_Errorstack(iter);
00163 }
00164
00165
00166 ret = LIBMTP_Get_Device_Certificate(iter, &devcert);
00167 if (ret == 0 && devcert != NULL) {
00168 fprintf(stdout, "\nDevice Certificate:\n%s\n", devcert);
00169 free(devcert);
00170 } else {
00171 fprintf(stdout, "Unable to acquire device certificate, perhaps this device "
00172 "does not support this\n");
00173 LIBMTP_Dump_Errorstack(iter);
00174 LIBMTP_Clear_Errorstack(iter);
00175 }
00176
00177
00178 files = LIBMTP_Get_Filelisting_With_Callback(iter, NULL, NULL);
00179 if (files != NULL) {
00180 LIBMTP_file_t *file, *tmp;
00181 file = files;
00182 while (file != NULL) {
00183 if (!strcmp(file->filename, "WMPInfo.xml") ||
00184 !strcmp(file->filename, "WMPinfo.xml"))
00185 {
00186 xmlfileid = file->item_id;
00187 }
00188 tmp = file;
00189 file = file->next;
00190 LIBMTP_destroy_file_t(tmp);
00191 }
00192 }
00193 if (xmlfileid == 0)
00194 fprintf(stdout, "WMPInfo.xml Does not exist on this device\n");
00195 if (xmlfileid != 0)
00196 {
00197 FILE *xmltmp = tmpfile();
00198 int tmpfiledescriptor = fileno(xmltmp);
00199
00200 if (tmpfiledescriptor != -1)
00201 {
00202 int ret = LIBMTP_Get_Track_To_File_Descriptor(iter,
00203 xmlfileid,
00204 tmpfiledescriptor,
00205 NULL,
00206 NULL);
00207 if (ret == 0)
00208 {
00209 uint8_t *buf = NULL;
00210 uint32_t readbytes;
00211
00212 buf = malloc(XML_BUFSIZE);
00213 if (buf == NULL)
00214 {
00215 printf("Could not allocate %08x bytes...\n", XML_BUFSIZE);
00216 LIBMTP_Dump_Errorstack(iter);
00217 LIBMTP_Clear_Errorstack(iter);
00218 LIBMTP_Release_Device_List(device);
00219 return 1;
00220 }
00221
00222 lseek(tmpfiledescriptor, 0, SEEK_SET);
00223 readbytes = read(tmpfiledescriptor, (void*) buf, XML_BUFSIZE);
00224
00225 if (readbytes >= 2 && readbytes < XML_BUFSIZE)
00226 {
00227 fprintf(stdout, "\nDevice description WMPInfo.xml file:\n");
00228 dump_xml_fragment(buf, readbytes);
00229 }
00230 else
00231 {
00232 perror("Unable to read WMPInfo.xml");
00233 LIBMTP_Dump_Errorstack(iter);
00234 LIBMTP_Clear_Errorstack(iter);
00235 }
00236 free(buf);
00237 }
00238 else
00239 {
00240 LIBMTP_Dump_Errorstack(iter);
00241 LIBMTP_Clear_Errorstack(iter);
00242 }
00243 fclose(xmltmp);
00244 }
00245 }
00246
00247 }
00248
00249 LIBMTP_Release_Device_List(device);
00250 printf("OK.\n");
00251
00252 return 0;
00253 }