FS#12662 by Manuel Flury - update French translation
[maemo-rb.git] / utils / MTP / beastpatcher / mtp_win32.c
blob626467ecbe59ce9868c5a2e713a5e053ac88a6d1
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
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 <stdio.h>
42 #include <string.h>
43 #include <stddef.h>
44 #include <stdlib.h>
45 #include <wchar.h>
46 #include <windows.h>
47 #include <sys/types.h>
48 #include <sys/stat.h>
49 #include <tchar.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));
64 return 0;
68 int mtp_finished(struct mtp_info_t* mtp_info)
70 (void)mtp_info;
72 return 0;
75 int mtp_scan(struct mtp_info_t* mtp_info)
77 wchar_t name[256];
78 wchar_t manufacturer[256];
79 DWORD version;
80 int num = 0;
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);
97 fflush(stdout);
101 int mtp_send_firmware(struct mtp_info_t* mtp_info, unsigned char* fwbuf,
102 int fwsize)
104 HANDLE hTempFile;
105 DWORD dwRetVal;
106 DWORD dwBytesWritten;
107 UINT uRetVal;
108 TCHAR szTempName[1024];
109 TCHAR lpPathBuffer[1024];
110 BOOL fSuccess;
111 wchar_t *tmp;
112 int ret;
114 (void)mtp_info;
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());
121 return -1;
124 /* Create the temporary file */
125 uRetVal = GetTempFileName(lpPathBuffer, TEXT("NKBIN"), 0, szTempName);
126 if (uRetVal == 0)
128 fprintf(stderr, "[ERR] GetTempFileName failed (%d)\n", (int)GetLastError());
129 return -1;
132 /* Now create the file */
133 hTempFile = CreateFile((LPTSTR) szTempName, // file name
134 GENERIC_READ | GENERIC_WRITE, // open r-w
135 0, // do not share
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);
143 return -1;
146 fSuccess = WriteFile(hTempFile, fwbuf, fwsize, &dwBytesWritten, NULL);
147 if (!fSuccess)
149 fprintf(stderr, "[ERR] WriteFile failed (%d)\n", (int)GetLastError());
150 return -1;
153 fSuccess = CloseHandle (hTempFile);
154 if (!fSuccess)
156 fprintf(stderr, "[ERR] CloseHandle failed (%d)\n", (int)GetLastError());
157 return -1;
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");
168 ret = 0;
170 else
172 fprintf(stderr, "\n");
173 fprintf(stderr, "[ERR] Error occured during sending.\n");
174 ret = -1;
176 free(tmp);
178 if (!DeleteFile(szTempName))
179 fprintf(stderr,"[WARN] Could not remove temporary file %s\n",szTempName);
181 return ret;
185 int mtp_send_file(struct mtp_info_t* mtp_info, const char* filename)
187 wchar_t *fn;
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");
194 return 1;
196 /* Scan for attached MTP devices. */
197 if (mtp_scan(mtp_info) < 0)
199 fprintf(stderr,"[ERR] No devices found\n");
200 return 1;
203 fprintf(stderr, "[INFO] Sending firmware...\n");
204 if (mtp_sendnk(fn, filesize(filename), &callback))
206 /* keep progress on screen */
207 printf("\n");
208 fprintf(stderr, "[INFO] Firmware sent successfully\n");
209 return 0;
211 else
213 fprintf(stderr, "[ERR] Error occured during sending.\n");
214 return -1;
216 mtp_finished(mtp_info);
220 static int filesize(const char* filename)
222 struct _stat sb;
223 int res;
225 res = _stat(filename, &sb);
226 if(res == -1) {
227 fprintf(stderr, "Error getting filesize!\n");
228 return -1;
230 return sb.st_size;
233 /* Retrieve version of WMP as described in
234 * http://msdn.microsoft.com/en-us/library/dd562731%28VS.85%29.aspx
235 * Since we're after MTP support checking for the WMP6 key is not necessary.
237 int mtp_wmp_version(void)
239 DWORD ret;
240 HKEY hk;
241 DWORD type;
242 DWORD enable = -1;
243 DWORD enablelen = sizeof(DWORD);
244 char buf[32];
245 DWORD buflen = sizeof(buf);
246 int version = 0;
248 ret = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
249 "SOFTWARE\\Microsoft\\Active Setup\\Installed Components\\{6BF52A52-394A-11d3-B153-00C04F79FAA6}",
250 0, KEY_QUERY_VALUE, &hk);
252 if(ret != ERROR_SUCCESS) {
253 return 0;
255 type = REG_DWORD;
256 ret = RegQueryValueExA(hk, "IsInstalled", NULL, &type, (LPBYTE)&enable, &enablelen);
257 if(ret != ERROR_SUCCESS) {
258 RegCloseKey(hk);
259 return 0;
261 if(enable) {
262 type = REG_SZ;
263 ret = RegQueryValueExA(hk, "Version", NULL, &type, (LPBYTE)buf, &buflen);
265 if(ret == ERROR_SUCCESS) {
266 /* get major version from registry value */
267 buf[31] = '\0';
268 version = atoi(buf);
270 RegCloseKey(hk);
272 return version;