a bit of code cleanup.. use a single function to get the statusbar height (or lack...
[Rockbox.git] / utils / MTP / sendfirm.c
blobd281513450a876a624126e028ea358761424b07e
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Based on sendfile.c from libmtp: http://libmtp.sourceforge.net
11 * Modified by Maurus Cuelenaere and Nicolas Pennequin.
13 * Copyright (C) 2005-2007 Linus Walleij
14 * Copyright (C) 2006 Chris A. Debenham
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.
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
22 ****************************************************************************/
24 #define _LARGEFILE_SOURCE
25 #define _LARGEFILE64_SOURCE
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"
34 LIBMTP_mtpdevice_t *device;
36 static int progress(u_int64_t const sent, u_int64_t const total,
37 void const *const data)
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;
49 void usage(void)
51 fprintf(stderr, "usage: sendfirm <local filename>\n");
54 int sendfile_function(char *from_path)
56 printf("Sending %s\n", from_path);
57 uint64_t filesize;
58 #ifdef __USE_LARGEFILE64
59 struct stat64 sb;
60 #else
61 struct stat sb;
62 #endif
63 LIBMTP_file_t *genfile;
64 int ret;
65 uint32_t parent_id = 0;
67 #ifdef __USE_LARGEFILE64
68 if (stat64(from_path, &sb) == -1)
70 #else
71 if (stat(from_path, &sb) == -1)
73 #endif
74 fprintf(stderr, "%s: ", from_path);
75 perror("stat");
76 exit(1);
79 #ifdef __USE_LARGEFILE64
80 filesize = sb.st_size;
81 #else
82 filesize = (uint64_t) sb.st_size;
83 #endif
85 genfile = LIBMTP_new_file_t();
86 genfile->filesize = filesize;
87 genfile->filename = strdup("nk.bin");
88 genfile->filetype = LIBMTP_FILETYPE_FIRMWARE;
90 printf("Sending file...\n");
91 ret = LIBMTP_Send_File_From_File(device, from_path, genfile, progress,
92 NULL, parent_id);
93 printf("\n");
94 if (ret != 0)
96 printf("Error sending file.\n");
97 LIBMTP_Dump_Errorstack(device);
98 LIBMTP_Clear_Errorstack(device);
100 else
102 printf("New file ID: %d\n", genfile->item_id);
105 LIBMTP_destroy_file_t(genfile);
107 return 0;
110 int main(int argc, char **argv)
112 if (argc < 2)
114 usage();
115 return 1;
118 LIBMTP_Init();
120 fprintf(stdout, "libmtp version: " LIBMTP_VERSION_STRING "\n\n");
122 device = LIBMTP_Get_First_Device();
123 if (device == NULL)
125 printf("No devices.\n");
126 return 0;
129 sendfile_function(argv[1]);
131 LIBMTP_Release_Device(device);
133 exit(0);