Refactor sendfirm functionality in beastpatcher and do some code police.


git-svn-id: svn://svn.rockbox.org/rockbox/trunk@21271 a1c6a512-1295-4272-9138-f99709370657
diff --git a/utils/MTP/beastpatcher/beastpatcher.c b/utils/MTP/beastpatcher/beastpatcher.c
index 0599353..72de1b5 100644
--- a/utils/MTP/beastpatcher/beastpatcher.c
+++ b/utils/MTP/beastpatcher/beastpatcher.c
@@ -56,7 +56,7 @@
 #include "bootimg.h"
 
 /* Code to create a single-boot bootloader.
-   Based on tools/gigabeats.c by Will Robertson.  
+   Based on tools/gigabeats.c by Will Robertson.
 */
 
 /* Entry point (and load address) for the main Rockbox bootloader */
@@ -83,7 +83,7 @@
 {
     unsigned char* buf;
 
-    /* 15 bytes for header, 16 for signature bypass, 
+    /* 15 bytes for header, 16 for signature bypass,
      * 12 for record header, size of bootloader, 12 for footer */
     *fwsize = 15 + 16 + 12 + bootlen + 12;
     *fwbuf = malloc(*fwsize);
@@ -106,7 +106,7 @@
 
     /* If the value below is too small, the update will attempt to flash.
      * Be careful when changing this (leaving it as is won't cause issues) */
-    put_uint32le(0xCC0CD8, buf+11); 
+    put_uint32le(0xCC0CD8, buf+11);
 
     /* Step 3: Add the signature bypass record */
     put_uint32le(0x88065A10, buf+15);
@@ -186,3 +186,30 @@
     return 0;
 }
 
+
+int sendfirm(const char* filename)
+{
+    struct mtp_info_t mtp_info;
+
+    if (mtp_init(&mtp_info) < 0) {
+        fprintf(stderr,"[ERR]  Can not init MTP\n");
+        return 1;
+    }
+
+    /* Scan for attached MTP devices. */
+    if (mtp_scan(&mtp_info) < 0)
+    {
+        fprintf(stderr,"[ERR]  No devices found\n");
+        return 1;
+    }
+
+    printf("[INFO] Found device \"%s - %s\"\n", mtp_info.manufacturer,
+                                                mtp_info.modelname);
+    printf("[INFO] Device version: \"%s\"\n",mtp_info.version);
+
+    mtp_send_file(&mtp_info, filename);
+
+    mtp_finished(&mtp_info);
+    return 0;
+}
+
diff --git a/utils/MTP/beastpatcher/beastpatcher.h b/utils/MTP/beastpatcher/beastpatcher.h
index c73ae91..d5a4eb0 100644
--- a/utils/MTP/beastpatcher/beastpatcher.h
+++ b/utils/MTP/beastpatcher/beastpatcher.h
@@ -10,19 +10,19 @@
  *
  * Copyright (c) 2009, Dave Chapman
  * All rights reserved.
- * 
+ *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
  * met:
- * 
+ *
  *     * Redistributions of source code must retain the above copyright
  *       notice, this list of conditions and the following disclaimer.
- * 
+ *
  *     * Redistributions in binary form must reproduce the above
  *       copyright notice, this list of conditions and the following
  *       disclaimer in the documentation and/or other materials provided
  *       with the distribution.
- * 
+ *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
@@ -41,5 +41,6 @@
 #define BEASTPATCHER_H
 
 int beastpatcher(void);
+int sendfirm(const char* filename);
 
 #endif
diff --git a/utils/MTP/beastpatcher/main.c b/utils/MTP/beastpatcher/main.c
index 1ccff72..6632b7a 100644
--- a/utils/MTP/beastpatcher/main.c
+++ b/utils/MTP/beastpatcher/main.c
@@ -66,7 +66,6 @@
 {
     int res;
     char yesno[4];
-    struct mtp_info_t mtp_info;
 
     fprintf(stderr,"beastpatcher v" VERSION " - (C) 2009 by the Rockbox developers\n");
     fprintf(stderr,"This is free software; see the source for copying conditions.  There is NO\n");
@@ -81,10 +80,7 @@
         }
     }
     else if((argc > 2) && ((strcmp(argv[1],"-s")==0) || (strcmp(argv[1],"--send")==0))) {
-        mtp_init(&mtp_info);
-        mtp_scan(&mtp_info);
-        res = mtp_send_file(&mtp_info, argv[2]);
-        mtp_finished(&mtp_info);
+        res = sendfirm(argv[2]);
     }
     else {
         print_usage();
diff --git a/utils/MTP/beastpatcher/mtp_libmtp.c b/utils/MTP/beastpatcher/mtp_libmtp.c
index 89b374c..67b009b 100644
--- a/utils/MTP/beastpatcher/mtp_libmtp.c
+++ b/utils/MTP/beastpatcher/mtp_libmtp.c
@@ -205,7 +205,7 @@
     fwfile = fopen(filename, "r");
     if (fwfile == NULL)
     {
-        fprintf(stderr,"[ERR]  Could not create temporary file.\n");
+        fprintf(stderr,"[ERR]  Could not open file.\n");
         return -1;
     }
     ret = mtp_send_fileptr(mtp_info, fwfile, sb.st_size);
diff --git a/utils/MTP/beastpatcher/mtp_win32.c b/utils/MTP/beastpatcher/mtp_win32.c
index 1d6105f..7a7c6db 100644
--- a/utils/MTP/beastpatcher/mtp_win32.c
+++ b/utils/MTP/beastpatcher/mtp_win32.c
@@ -5,24 +5,24 @@
  *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <

  *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \

  *                     \/            \/     \/    \/            \/

- * 

+ *

  * $Id$

  *

  * Copyright (c) 2009, Dave Chapman

  * All rights reserved.

- * 

+ *

  * Redistribution and use in source and binary forms, with or without

  * modification, are permitted provided that the following conditions are

  * met:

- * 

+ *

  *     * Redistributions of source code must retain the above copyright

  *       notice, this list of conditions and the following disclaimer.

- * 

+ *

  *     * Redistributions in binary form must reproduce the above

  *       copyright notice, this list of conditions and the following

  *       disclaimer in the documentation and/or other materials provided

  *       with the distribution.

- * 

+ *

  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS

  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT

  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR

@@ -93,7 +93,7 @@
 {

     int percent = (progress * 100) / max;

 

-	printf("[INFO] Progress: %u of %u (%d%%)\r", progress, max, percent);

+    printf("[INFO] Progress: %u of %u (%d%%)\r", progress, max, percent);

     fflush(stdout);

 }

 

@@ -101,19 +101,19 @@
 int mtp_send_firmware(struct mtp_info_t* mtp_info, unsigned char* fwbuf,

                       int fwsize)

 {

-    HANDLE hTempFile; 

+    HANDLE hTempFile;

     DWORD dwRetVal;

-    DWORD dwBytesWritten; 

+    DWORD dwBytesWritten;

     UINT uRetVal;

     TCHAR szTempName[1024];

     TCHAR lpPathBuffer[1024];

     BOOL fSuccess;

     wchar_t *tmp;

     int ret;

-    

+

     (void)mtp_info;

 

-    /* Get the path for temporary files */    

+    /* Get the path for temporary files */

     dwRetVal = GetTempPath(sizeof(lpPathBuffer), lpPathBuffer);

     if (dwRetVal > sizeof(lpPathBuffer) || (dwRetVal == 0))

     {

@@ -122,7 +122,7 @@
     }

 

     /* Create the temporary file */

-    uRetVal = GetTempFileName(lpPathBuffer, TEXT("NKBIN"), 0, szTempName);  

+    uRetVal = GetTempFileName(lpPathBuffer, TEXT("NKBIN"), 0, szTempName);

     if (uRetVal == 0)

     {

         fprintf(stderr, "[ERR]  GetTempFileName failed (%d)\n", (int)GetLastError());

@@ -130,28 +130,28 @@
     }

 

     /* Now create the file */

-    hTempFile = CreateFile((LPTSTR) szTempName, // file name 

-                           GENERIC_READ | GENERIC_WRITE, // open r-w 

-                           0,                    // do not share 

-                           NULL,                 // default security 

+    hTempFile = CreateFile((LPTSTR) szTempName, // file name

+                           GENERIC_READ | GENERIC_WRITE, // open r-w

+                           0,                    // do not share

+                           NULL,                 // default security

                            CREATE_ALWAYS,        // overwrite existing

-                           FILE_ATTRIBUTE_NORMAL,// normal file 

-                           NULL);                // no template 

-    if (hTempFile == INVALID_HANDLE_VALUE) 

-    { 

+                           FILE_ATTRIBUTE_NORMAL,// normal file

+                           NULL);                // no template

+    if (hTempFile == INVALID_HANDLE_VALUE)

+    {

         fprintf(stderr, "[ERR]  Could not create %s\n", szTempName);

         return -1;

-    } 

+    }

 

-    fSuccess = WriteFile(hTempFile, fwbuf, fwsize, &dwBytesWritten, NULL); 

-    if (!fSuccess) 

+    fSuccess = WriteFile(hTempFile, fwbuf, fwsize, &dwBytesWritten, NULL);

+    if (!fSuccess)

     {

         fprintf(stderr, "[ERR]  WriteFile failed (%d)\n", (int)GetLastError());

         return -1;

     }

 

     fSuccess = CloseHandle (hTempFile);

-    if (!fSuccess) 

+    if (!fSuccess)

     {

        fprintf(stderr, "[ERR]  CloseHandle failed (%d)\n", (int)GetLastError());

        return -1;

@@ -159,17 +159,17 @@
 

     tmp = (LPWSTR)malloc(_tcslen(szTempName)*2+1);

     mbstowcs(tmp, (char*)szTempName, _tcslen(szTempName)*2+1);

-    

+

     fprintf(stderr, "[INFO] Sending firmware...\n");

     if (mtp_sendnk(tmp, fwsize, &callback))

     {

-		fprintf(stderr, "\n");

+        fprintf(stderr, "\n");

         fprintf(stderr, "[INFO] Firmware sent successfully\n");

         ret = 0;

     }

     else

     {

-		fprintf(stderr, "\n");

+        fprintf(stderr, "\n");

         fprintf(stderr, "[ERR]  Error occured during sending.\n");

         ret = -1;

     }

@@ -184,10 +184,10 @@
 

 int mtp_send_file(struct mtp_info_t* mtp_info, const char* filename)

 {

-	wchar_t *fn;

+    wchar_t *fn;

 

-	fn = (LPWSTR)malloc(strlen(filename)*2+1);

-	mbstowcs(fn, filename, strlen(filename)*2+1);

+    fn = (LPWSTR)malloc(strlen(filename)*2+1);

+    mbstowcs(fn, filename, strlen(filename)*2+1);

 

     if (mtp_init(mtp_info) < 0) {

         fprintf(stderr,"[ERR]  Can not init MTP\n");

@@ -200,32 +200,33 @@
         return 1;

     }

 

-	fprintf(stderr, "[INFO] Sending firmware...\n");

-	if (mtp_sendnk(fn, filesize(filename), &callback))

-	{

-		/* keep progress on screen */

-		printf("\n");

-		fprintf(stderr, "[INFO] Firmware sent successfully\n");

-		return 0;

-	}

-	else

-	{

-		fprintf(stderr, "[ERR]  Error occured during sending.\n");

-		return -1;

-	}

-	mtp_finished(mtp_info);

+    fprintf(stderr, "[INFO] Sending firmware...\n");

+    if (mtp_sendnk(fn, filesize(filename), &callback))

+    {

+        /* keep progress on screen */

+        printf("\n");

+        fprintf(stderr, "[INFO] Firmware sent successfully\n");

+        return 0;

+    }

+    else

+    {

+        fprintf(stderr, "[ERR]  Error occured during sending.\n");

+        return -1;

+    }

+    mtp_finished(mtp_info);

 }

 

 

 static int filesize(const char* filename)

 {

-	struct _stat sb;

-	int res;

+    struct _stat sb;

+    int res;

 

-	res = _stat(filename, &sb);

-	if(res == -1) {

-		fprintf(stderr, "Error getting filesize!\n");

-		return -1;

-	}

-	return sb.st_size;

+    res = _stat(filename, &sb);

+    if(res == -1) {

+        fprintf(stderr, "Error getting filesize!\n");

+        return -1;

+    }

+    return sb.st_size;

 }

+