blob: e216037efcb02d3f7ea19c17812e31f22d3f7f2c [file] [log] [blame]
Nicolas Pennequin3e2c51d2007-11-22 18:07:40 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Based on sendfile.c from libmtp: http://libmtp.sourceforge.net
11 * Modified by Maurus Cuelenaere and Nicolas Pennequin.
12 *
13 * Copyright (C) 2005-2007 Linus Walleij
14 * Copyright (C) 2006 Chris A. Debenham
15 *
16 * All files in this archive are subject to the GNU General Public License.
17 * See the file COPYING in the source tree root for full license agreement.
18 *
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
21 *
22 ****************************************************************************/
23
24#define _LARGEFILE_SOURCE
25#define _LARGEFILE64_SOURCE
26
27#include <string.h>
28#include <libgen.h>
29#include <sys/stat.h>
30#include <sys/types.h>
31#include <fcntl.h>
32#include "libmtp.h"
33
34LIBMTP_mtpdevice_t *device;
35
36static int progress(u_int64_t const sent, u_int64_t const total,
37 void const *const data)
38{
39 int percent = (sent * 100) / total;
40#ifdef __WIN32__
41 printf("Progress: %I64u of %I64u (%d%%)\r", sent, total, percent);
42#else
43 printf("Progress: %llu of %llu (%d%%)\r", sent, total, percent);
44#endif
45 fflush(stdout);
46 return 0;
47}
48
49void usage(void)
50{
51 fprintf(stderr, "usage: sendfirm <local filename>\n");
52}
53
54int sendfile_function(char *from_path)
55{
56 printf("Sending %s\n", from_path);
57 char *filename;
58 uint64_t filesize;
59#ifdef __USE_LARGEFILE64
60 struct stat64 sb;
61#else
62 struct stat sb;
63#endif
64 LIBMTP_file_t *genfile;
65 int ret;
66 uint32_t parent_id = 0;
67
68#ifdef __USE_LARGEFILE64
69 if (stat64(from_path, &sb) == -1)
70 {
71#else
72 if (stat(from_path, &sb) == -1)
73 {
74#endif
75 fprintf(stderr, "%s: ", from_path);
76 perror("stat");
77 exit(1);
78 }
79
80#ifdef __USE_LARGEFILE64
81 filesize = sb.st_size;
82#else
83 filesize = (uint64_t) sb.st_size;
84#endif
85 filename = basename(from_path);
86
87 genfile = LIBMTP_new_file_t();
88 genfile->filesize = filesize;
89 genfile->filename = strdup("nk.bin");
90 genfile->filetype = LIBMTP_FILETYPE_FIRMWARE;
91
92 printf("Sending file...\n");
93 ret = LIBMTP_Send_File_From_File(device, from_path, genfile, progress,
94 NULL, parent_id);
95 printf("\n");
96 if (ret != 0)
97 {
98 printf("Error sending file.\n");
99 LIBMTP_Dump_Errorstack(device);
100 LIBMTP_Clear_Errorstack(device);
101 }
102 else
103 {
104 printf("New file ID: %d\n", genfile->item_id);
105 }
106
107 LIBMTP_destroy_file_t(genfile);
108
109 return 0;
110}
111
112int main(int argc, char **argv)
113{
114 if (argc < 2)
115 {
116 usage();
117 return 1;
118 }
119
120 LIBMTP_Init();
121
122 fprintf(stdout, "libmtp version: " LIBMTP_VERSION_STRING "\n\n");
123
124 device = LIBMTP_Get_First_Device();
125 if (device == NULL)
126 {
127 printf("No devices.\n");
128 return 0;
129 }
130
131 sendfile_function(argv[1]);
132
133 LIBMTP_Release_Device(device);
134
135 exit(0);
136}