Build doom on clipv2 and clip+
[kugel-rb.git] / utils / MTP / beastpatcher / mtp_libmtp.c
blob67b009b923b0bb0e803408347bf3c701e54fbed3
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 *
9 * $Id$
11 * Copyright (c) 2009, Dave Chapman
12 * All rights reserved.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met:
18 * * Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
21 * * Redistributions in binary form must reproduce the above
22 * copyright notice, this list of conditions and the following
23 * disclaimer in the documentation and/or other materials provided
24 * with the distribution.
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 ****************************************************************************/
41 #include <string.h>
42 #include <libgen.h>
43 #include <sys/stat.h>
44 #include <sys/types.h>
45 #include <fcntl.h>
46 #include <inttypes.h>
47 #include "libmtp.h"
48 #include "mtp_common.h"
50 static int mtp_send_fileptr(struct mtp_info_t* mtp_info, FILE* fwfile, size_t fwsize);
52 int mtp_init(struct mtp_info_t* mtp_info)
54 /* Fill the info struct with zeros - mainly for the strings */
55 memset(mtp_info, 0, sizeof(struct mtp_info_t));
57 LIBMTP_Init();
59 return 0;
63 int mtp_finished(struct mtp_info_t* mtp_info)
65 LIBMTP_Release_Device(mtp_info->device);
67 return 0;
70 int mtp_scan(struct mtp_info_t* mtp_info)
72 char* str;
74 mtp_info->device = LIBMTP_Get_First_Device();
76 if (mtp_info->device == NULL)
78 return -1;
80 else
82 /* NOTE: These strings are filled with zeros in mtp_init() */
83 #ifndef REALLYOLDMTP
84 if ((str = LIBMTP_Get_Manufacturername(mtp_info->device)))
86 strncpy(mtp_info->manufacturer, str, sizeof(mtp_info->manufacturer)-1);
88 #else
89 strcpy(mtp_info->manufacturer, "(unknown manufacturer)");
90 #endif
92 if ((str = LIBMTP_Get_Modelname(mtp_info->device)))
94 strncpy(mtp_info->modelname, str, sizeof(mtp_info->modelname)-1);
97 if ((str = LIBMTP_Get_Deviceversion(mtp_info->device)))
99 strncpy(mtp_info->version, str, sizeof(mtp_info->version)-1);
102 return 0;
106 static int progress(uint64_t const sent, uint64_t const total,
107 void const *const data)
109 (void)data;
111 int percent = (sent * 100) / total;
113 printf("Progress: %"PRIu64" of %"PRIu64" (%d%%)\r", sent, total, percent);
114 fflush(stdout);
115 return 0;
119 int mtp_send_firmware(struct mtp_info_t* mtp_info, unsigned char* fwbuf,
120 int fwsize)
122 int ret;
123 size_t n;
124 FILE* fwfile;
126 /* Open a temporary file - this will be automatically deleted when closed */
127 fwfile = tmpfile();
129 if (fwfile == NULL)
131 fprintf(stderr,"[ERR] Could not create temporary file.\n");
132 return -1;
135 n = fwrite(fwbuf, 1, fwsize, fwfile);
136 if ((int)n < fwsize)
138 fprintf(stderr,"[ERR] Could not write to temporary file - n = %d.\n",(int)n);
139 fclose(fwfile);
140 return -1;
143 /* Reset file pointer */
144 fseek(fwfile, SEEK_SET, 0);
146 ret = mtp_send_fileptr(mtp_info, fwfile, fwsize);
148 /* Close the temporary file - this also deletes it. */
149 fclose(fwfile);
151 return 0;
155 static int mtp_send_fileptr(struct mtp_info_t* mtp_info, FILE* fwfile, size_t fwsize)
157 LIBMTP_file_t* genfile;
158 int ret;
160 /* Prepare for uploading firmware */
161 genfile = LIBMTP_new_file_t();
162 genfile->filetype = LIBMTP_FILETYPE_FIRMWARE;
163 genfile->filename = strdup("nk.bin");
164 genfile->filesize = fwsize;
166 #ifdef OLDMTP
167 ret = LIBMTP_Send_File_From_File_Descriptor(mtp_info->device,
168 fileno(fwfile), genfile, progress, NULL, 0);
169 #else
170 ret = LIBMTP_Send_File_From_File_Descriptor(mtp_info->device,
171 fileno(fwfile), genfile, progress, NULL);
172 #endif
174 /* Keep the progress line onscreen */
175 printf("\n");
177 /* NOTE: According to the docs, a value of ret != 0 means error, but libMTP
178 seems to return that even when successful. So we can't check the return
179 code.
182 /* Cleanup */
183 LIBMTP_destroy_file_t(genfile);
185 return ret;
189 int mtp_send_file(struct mtp_info_t* mtp_info, const char* filename)
191 FILE* fwfile;
192 int ret;
193 #ifdef _LARGEFILE64_SOURCE
194 struct stat64 sb;
195 ret = stat64(filename, &sb);
196 #else
197 struct stat sb;
198 ret = stat(filename, &sb);
199 #endif
200 if (ret == -1)
202 perror("[ERR] ");
205 fwfile = fopen(filename, "r");
206 if (fwfile == NULL)
208 fprintf(stderr,"[ERR] Could not open file.\n");
209 return -1;
211 ret = mtp_send_fileptr(mtp_info, fwfile, sb.st_size);
213 fclose(fwfile);
215 return ret;