1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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
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 ****************************************************************************/
44 #include <sys/types.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
));
63 int mtp_finished(struct mtp_info_t
* mtp_info
)
65 LIBMTP_Release_Device(mtp_info
->device
);
70 int mtp_scan(struct mtp_info_t
* mtp_info
)
74 mtp_info
->device
= LIBMTP_Get_First_Device();
76 if (mtp_info
->device
== NULL
)
82 /* NOTE: These strings are filled with zeros in mtp_init() */
84 if ((str
= LIBMTP_Get_Manufacturername(mtp_info
->device
)))
86 strncpy(mtp_info
->manufacturer
, str
, sizeof(mtp_info
->manufacturer
)-1);
89 strcpy(mtp_info
->manufacturer
, "(unknown manufacturer)");
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);
106 static int progress(uint64_t const sent
, uint64_t const total
,
107 void const *const data
)
111 int percent
= (sent
* 100) / total
;
113 printf("Progress: %"PRIu64
" of %"PRIu64
" (%d%%)\r", sent
, total
, percent
);
119 int mtp_send_firmware(struct mtp_info_t
* mtp_info
, unsigned char* fwbuf
,
126 /* Open a temporary file - this will be automatically deleted when closed */
131 fprintf(stderr
,"[ERR] Could not create temporary file.\n");
135 n
= fwrite(fwbuf
, 1, fwsize
, fwfile
);
138 fprintf(stderr
,"[ERR] Could not write to temporary file - n = %d.\n",(int)n
);
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. */
155 static int mtp_send_fileptr(struct mtp_info_t
* mtp_info
, FILE* fwfile
, size_t fwsize
)
157 LIBMTP_file_t
* genfile
;
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
;
167 ret
= LIBMTP_Send_File_From_File_Descriptor(mtp_info
->device
,
168 fileno(fwfile
), genfile
, progress
, NULL
, 0);
170 ret
= LIBMTP_Send_File_From_File_Descriptor(mtp_info
->device
,
171 fileno(fwfile
), genfile
, progress
, NULL
);
174 /* Keep the progress line onscreen */
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
183 LIBMTP_destroy_file_t(genfile
);
189 int mtp_send_file(struct mtp_info_t
* mtp_info
, const char* filename
)
193 #ifdef _LARGEFILE64_SOURCE
195 ret
= stat64(filename
, &sb
);
198 ret
= stat(filename
, &sb
);
205 fwfile
= fopen(filename
, "r");
208 fprintf(stderr
,"[ERR] Could not open file.\n");
211 ret
= mtp_send_fileptr(mtp_info
, fwfile
, sb
.st_size
);