summaryrefslogtreecommitdiff
path: root/utils/MTP/beastpatcher/mtp_libmtp.c
diff options
context:
space:
mode:
authorDave Chapman <dave@dchapman.com>2009-02-22 13:54:46 +0000
committerDave Chapman <dave@dchapman.com>2009-02-22 13:54:46 +0000
commitc06071e2e705095e49207f92b941edd3b5ada46c (patch)
tree28e6b326720a12f26c56ff098ea12fea50838808 /utils/MTP/beastpatcher/mtp_libmtp.c
parent65d404ff6a78c6e2135f3e4f1f9d5634bed0dfce (diff)
downloadrockbox-c06071e2e705095e49207f92b941edd3b5ada46c.tar.gz
rockbox-c06071e2e705095e49207f92b941edd3b5ada46c.zip
Initial version of a BSD-licensed beastpatcher utility for Gigabeat S installation. Currently only compiles on Linux, but Windows and OS X support are planned.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@20083 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'utils/MTP/beastpatcher/mtp_libmtp.c')
-rw-r--r--utils/MTP/beastpatcher/mtp_libmtp.c174
1 files changed, 174 insertions, 0 deletions
diff --git a/utils/MTP/beastpatcher/mtp_libmtp.c b/utils/MTP/beastpatcher/mtp_libmtp.c
new file mode 100644
index 0000000000..7e8579ac99
--- /dev/null
+++ b/utils/MTP/beastpatcher/mtp_libmtp.c
@@ -0,0 +1,174 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 *
9 * $Id:$
10 *
11 * Copyright (c) 2009, Dave Chapman
12 * All rights reserved.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met:
17 *
18 * * Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 *
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.
25 *
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.
37 *
38 *
39 ****************************************************************************/
40
41#include <string.h>
42#include <libgen.h>
43#include <sys/stat.h>
44#include <sys/types.h>
45#include <fcntl.h>
46#include <inttypes.h>
47#include "libmtp.h"
48#include "mtp_common.h"
49
50int mtp_init(struct mtp_info_t* mtp_info)
51{
52 /* Fill the info struct with zeros - mainly for the strings */
53 memset(mtp_info, 0, sizeof(struct mtp_info_t));
54
55 LIBMTP_Init();
56
57 return 0;
58
59}
60
61int mtp_finished(struct mtp_info_t* mtp_info)
62{
63 LIBMTP_Release_Device(mtp_info->device);
64
65 return 0;
66}
67
68int mtp_scan(struct mtp_info_t* mtp_info)
69{
70 char* str;
71
72 mtp_info->device = LIBMTP_Get_First_Device();
73
74 if (mtp_info->device == NULL)
75 {
76 return -1;
77 }
78 else
79 {
80 /* NOTE: These strings are filled with zeros in mtp_init() */
81
82 if ((str = LIBMTP_Get_Manufacturername(mtp_info->device)))
83 {
84 strncpy(mtp_info->manufacturer, str, sizeof(mtp_info->manufacturer)-1);
85 }
86
87 if ((str = LIBMTP_Get_Modelname(mtp_info->device)))
88 {
89 strncpy(mtp_info->modelname, str, sizeof(mtp_info->modelname)-1);
90 }
91
92 if ((str = LIBMTP_Get_Deviceversion(mtp_info->device)))
93 {
94 strncpy(mtp_info->version, str, sizeof(mtp_info->version)-1);
95 }
96
97 return 0;
98 }
99}
100
101static int progress(uint64_t const sent, uint64_t const total,
102 void const *const data)
103{
104 (void)data;
105
106 int percent = (sent * 100) / total;
107#ifdef __WIN32__
108 printf("Progress: %I64u of %I64u (%d%%)\r", sent, total, percent);
109#else
110 printf("Progress: %"PRIu64" of %"PRIu64" (%d%%)\r", sent, total, percent);
111#endif
112 fflush(stdout);
113 return 0;
114}
115
116
117int mtp_send_firmware(struct mtp_info_t* mtp_info, unsigned char* fwbuf,
118 int fwsize)
119{
120 LIBMTP_file_t *genfile;
121 int ret;
122 size_t n;
123 FILE* fwfile;
124
125 /* Open a temporary file - this will be automatically deleted when closed */
126 fwfile = tmpfile();
127
128 if (fwfile == NULL)
129 {
130 fprintf(stderr,"[ERR] Could not create temporary file.\n");
131 return -1;
132 }
133
134 n = fwrite(fwbuf, 1, fwsize, fwfile);
135 if ((int)n < fwsize)
136 {
137 fprintf(stderr,"[ERR] Could not write to temporary file - n = %d.\n",(int)n);
138 fclose(fwfile);
139 return -1;
140 }
141
142 /* Reset file pointer */
143 fseek(fwfile, SEEK_SET, 0);
144
145 /* Prepare for uploading firmware */
146 genfile = LIBMTP_new_file_t();
147 genfile->filetype = LIBMTP_FILETYPE_FIRMWARE;
148 genfile->filename = strdup("nk.bin");
149 genfile->filesize = fwsize;
150
151#ifdef OLDMTP
152 ret = LIBMTP_Send_File_From_File_Descriptor(mtp_info->device,
153 fileno(fwfile), genfile, progress, NULL, 0);
154#else
155 ret = LIBMTP_Send_File_From_File_Descriptor(mtp_info->device,
156 fileno(fwfile), genfile, progress, NULL);
157#endif
158
159 /* Keep the progress line onscreen */
160 printf("\n");
161
162 /* NOTE: According to the docs, a value of ret != 0 means error, but libMTP
163 seems to return that even when successful. So we can't check the return
164 code.
165 */
166
167 /* Cleanup */
168 LIBMTP_destroy_file_t(genfile);
169
170 /* Close the temporary file - this also deletes it. */
171 fclose(fwfile);
172
173 return 0;
174}