Part of Slepp's ProjectsPastebinTURLImagebinFilebin
Feedback -- English French German Japanese
Create Upload Newest Tools Donate
Sign In | Create Account

sendfirm.c
Thursday, November 22nd, 2007 at 5:12:57pm UTC 

  1. /**
  2. * \file sendfile.c
  3. * Example program to send an arbitrary file to a device.
  4. *
  5. * Copyright (C) 2005-2007 Linus Walleij <[email protected]>
  6. * Copyright (C) 2006 Chris A. Debenham <[email protected]>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the
  20. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  21. * Boston, MA 02111-1307, USA.
  22. */
  23. #define _LARGEFILE_SOURCE
  24. #define _LARGEFILE64_SOURCE
  25.  
  26. #include <string.h>
  27. #include <libgen.h>
  28. #include <sys/stat.h>
  29. #include <sys/types.h>
  30. #include <fcntl.h>
  31. #include "common.h"
  32. #include "libmtp.h"
  33.  
  34. LIBMTP_folder_t *folders;
  35. LIBMTP_file_t *files;
  36. LIBMTP_mtpdevice_t *device;
  37.  
  38. int sendfile_function(char *);
  39. void sendfile_command(int, char **);
  40. void sendfile_usage(void);
  41.  
  42. static int progress (u_int64_t const sent, u_int64_t const total, void const * const data)
  43. {
  44.   int percent = (sent*100)/total;
  45. #ifdef __WIN32__
  46.   printf("Progress: %I64u of %I64u (%d%%)\r", sent, total, percent);
  47. #else
  48.   printf("Progress: %llu of %llu (%d%%)\r", sent, total, percent);
  49. #endif
  50.   fflush(stdout);
  51.   return 0;
  52. }
  53.  
  54. void sendfile_usage(void)
  55. {
  56.   fprintf(stderr, "usage: upgrade <local filename>\n");
  57. }
  58.  
  59. int sendfile_function(char * from_path)
  60. {
  61.   printf("Sending %s\n",from_path);
  62.   char *filename;
  63.   uint64_t filesize;
  64. #ifdef __USE_LARGEFILE64
  65.   struct stat64 sb;
  66. #else
  67.   struct stat sb;
  68. #endif
  69.   LIBMTP_file_t *genfile;
  70.   int ret;
  71.   uint32_t parent_id = 0;
  72.  
  73. #ifdef __USE_LARGEFILE64
  74.   if ( stat64(from_path, &sb) == -1 ) {
  75. #else
  76.   if ( stat(from_path, &sb) == -1 ) {
  77. #endif
  78.     fprintf(stderr, "%s: ", from_path);
  79.     perror("stat");
  80.     exit(1);
  81.   }
  82.  
  83. #ifdef __USE_LARGEFILE64
  84.   filesize = sb.st_size;
  85. #else
  86.   filesize = (uint64_t) sb.st_size;
  87. #endif
  88.   filename = basename(from_path);
  89.  
  90.   genfile = LIBMTP_new_file_t();
  91.   genfile->filesize = filesize;
  92.   genfile->filename = strdup("nk.bin");
  93.   genfile->filetype = LIBMTP_FILETYPE_FIRMWARE;
  94.  
  95.   printf("Sending file...\n");
  96.   ret = LIBMTP_Send_File_From_File(device, from_path, genfile, progress, NULL, parent_id);
  97.   printf("\n");
  98.   if (ret != 0) {
  99.     printf("Error sending file.\n");
  100.     LIBMTP_Dump_Errorstack(device);
  101.     LIBMTP_Clear_Errorstack(device);
  102.   } else {
  103.     printf("New file ID: %d\n", genfile->item_id);
  104.   }
  105.  
  106.   LIBMTP_destroy_file_t(genfile);
  107.  
  108.   return 0;
  109. }
  110.  
  111. int main (int argc, char **argv)
  112. {
  113.   if ( argc < 2 ) {
  114.     sendfile_usage();
  115.     return 1;
  116.   }
  117.  
  118.   /*
  119.    * Check environment variables $LANG and $LC_CTYPE
  120.    * to see if we want to support UTF-8 unicode
  121.    */
  122.   char * lang = getenv("LANG");
  123.   if (lang != NULL) {
  124.     if (strlen(lang) > 5) {
  125.       char *langsuff = &lang[strlen(lang)-5];
  126.       if (strcmp(langsuff, "UTF-8")) {
  127.         printf("Your system does not appear to have UTF-8 enabled ($LANG=\"%s\")\n", lang);
  128.         printf("If you want to have support for diacritics and Unicode characters,\n");
  129.         printf("please switch your locale to an UTF-8 locale, e.g. \"en_US.UTF-8\".\n");
  130.       }
  131.     }
  132.   }
  133.  
  134.   LIBMTP_Init();
  135.  
  136.   fprintf(stdout, "libmtp version: " LIBMTP_VERSION_STRING "\n\n");
  137.  
  138.   device = LIBMTP_Get_First_Device();
  139.   if (device == NULL) {
  140.     printf("No devices.\n");
  141.     return 0;
  142.   }
  143.   files = LIBMTP_Get_Filelisting_With_Callback (device, NULL, NULL);
  144.   folders = LIBMTP_Get_Folder_List (device);
  145.  
  146.   sendfile_function(argv[1]);
  147.  
  148.   LIBMTP_Release_Device(device);
  149.  
  150.   exit (0);
  151. }

Update the Post

Either update this post and resubmit it with changes, or make a new post.

You may also comment on this post.

update paste below
details of the post (optional)

Note: Only the paste content is required, though the following information can be useful to others.

Save name / title?

(space separated, optional)



Please note that information posted here will expire by default in one month. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.

comments powered by Disqus
worth-right
worth-right
worth-right