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 ****************************************************************************/
47 #include <sys/types.h>
51 #include "mtp_common.h"
53 #include "../MTP_DLL/MTP_DLL.h"
56 static int filesize(const char* filename
);
59 int mtp_init(struct mtp_info_t
* mtp_info
)
61 /* Fill the info struct with zeros - mainly for the strings */
62 memset(mtp_info
, 0, sizeof(struct mtp_info_t
));
68 int mtp_finished(struct mtp_info_t
* mtp_info
)
75 int mtp_scan(struct mtp_info_t
* mtp_info
)
78 wchar_t manufacturer
[256];
82 num
= mtp_description(name
, manufacturer
, &version
);
84 wcstombs(mtp_info
->manufacturer
, manufacturer
, 200);
85 wcstombs(mtp_info
->modelname
, name
, 200);
87 sprintf(mtp_info
->version
, "%x", (unsigned int)version
);
88 return (num
> 0) ? num
: -1;
92 static void callback(unsigned int progress
, unsigned int max
)
94 int percent
= (progress
* 100) / max
;
96 printf("[INFO] Progress: %u of %u (%d%%)\r", progress
, max
, percent
);
101 int mtp_send_firmware(struct mtp_info_t
* mtp_info
, unsigned char* fwbuf
,
106 DWORD dwBytesWritten
;
108 TCHAR szTempName
[1024];
109 TCHAR lpPathBuffer
[1024];
116 /* Get the path for temporary files */
117 dwRetVal
= GetTempPath(sizeof(lpPathBuffer
), lpPathBuffer
);
118 if (dwRetVal
> sizeof(lpPathBuffer
) || (dwRetVal
== 0))
120 fprintf(stderr
, "[ERR] GetTempPath failed (%d)\n", (int)GetLastError());
124 /* Create the temporary file */
125 uRetVal
= GetTempFileName(lpPathBuffer
, TEXT("NKBIN"), 0, szTempName
);
128 fprintf(stderr
, "[ERR] GetTempFileName failed (%d)\n", (int)GetLastError());
132 /* Now create the file */
133 hTempFile
= CreateFile((LPTSTR
) szTempName
, // file name
134 GENERIC_READ
| GENERIC_WRITE
, // open r-w
136 NULL
, // default security
137 CREATE_ALWAYS
, // overwrite existing
138 FILE_ATTRIBUTE_NORMAL
,// normal file
139 NULL
); // no template
140 if (hTempFile
== INVALID_HANDLE_VALUE
)
142 fprintf(stderr
, "[ERR] Could not create %s\n", szTempName
);
146 fSuccess
= WriteFile(hTempFile
, fwbuf
, fwsize
, &dwBytesWritten
, NULL
);
149 fprintf(stderr
, "[ERR] WriteFile failed (%d)\n", (int)GetLastError());
153 fSuccess
= CloseHandle (hTempFile
);
156 fprintf(stderr
, "[ERR] CloseHandle failed (%d)\n", (int)GetLastError());
160 tmp
= (LPWSTR
)malloc(_tcslen(szTempName
)*2+1);
161 mbstowcs(tmp
, (char*)szTempName
, _tcslen(szTempName
)*2+1);
163 fprintf(stderr
, "[INFO] Sending firmware...\n");
164 if (mtp_sendnk(tmp
, fwsize
, &callback
))
166 fprintf(stderr
, "\n");
167 fprintf(stderr
, "[INFO] Firmware sent successfully\n");
172 fprintf(stderr
, "\n");
173 fprintf(stderr
, "[ERR] Error occured during sending.\n");
178 if (!DeleteFile(szTempName
))
179 fprintf(stderr
,"[WARN] Could not remove temporary file %s\n",szTempName
);
185 int mtp_send_file(struct mtp_info_t
* mtp_info
, const char* filename
)
189 fn
= (LPWSTR
)malloc(strlen(filename
)*2+1);
190 mbstowcs(fn
, filename
, strlen(filename
)*2+1);
192 if (mtp_init(mtp_info
) < 0) {
193 fprintf(stderr
,"[ERR] Can not init MTP\n");
196 /* Scan for attached MTP devices. */
197 if (mtp_scan(mtp_info
) < 0)
199 fprintf(stderr
,"[ERR] No devices found\n");
203 fprintf(stderr
, "[INFO] Sending firmware...\n");
204 if (mtp_sendnk(fn
, filesize(filename
), &callback
))
206 /* keep progress on screen */
208 fprintf(stderr
, "[INFO] Firmware sent successfully\n");
213 fprintf(stderr
, "[ERR] Error occured during sending.\n");
216 mtp_finished(mtp_info
);
220 static int filesize(const char* filename
)
225 res
= _stat(filename
, &sb
);
227 fprintf(stderr
, "Error getting filesize!\n");