Home | History | Annotate | Download | only in examples
      1 /**
      2  * \file reset.c
      3  * Example program that resets the device.
      4  *
      5  * Copyright (C) 2007 Linus Walleij <triad (at) df.lth.se>
      6  *
      7  * This library is free software; you can redistribute it and/or
      8  * modify it under the terms of the GNU Lesser General Public
      9  * License as published by the Free Software Foundation; either
     10  * version 2 of the License, or (at your option) any later version.
     11  *
     12  * This library is distributed in the hope that it will be useful,
     13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     15  * Lesser General Public License for more details.
     16  *
     17  * You should have received a copy of the GNU Lesser General Public
     18  * License along with this library; if not, write to the
     19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     20  * Boston, MA 02111-1307, USA.
     21  */
     22 #include "common.h"
     23 
     24 /**
     25  * Returns 0 if OK (yes), 1 if not OK (no)
     26  */
     27 static int prompt()
     28 {
     29   char buff[2];
     30 
     31   while (1) {
     32     fprintf(stdout, "> ");
     33     if ( fgets(buff, sizeof(buff), stdin) == NULL ) {
     34       if (ferror(stdin)) {
     35         fprintf(stderr, "File error on stdin\n");
     36       } else {
     37         fprintf(stderr, "EOF on stdin\n");
     38       }
     39       return 1;
     40     }
     41     if (buff[0] == 'y') {
     42       return 0;
     43     } else if (buff[0] == 'n') {
     44       return 1;
     45     }
     46   }
     47 }
     48 
     49 int main (int argc, char **argv)
     50 {
     51   LIBMTP_mtpdevice_t *device;
     52   int ret;
     53 
     54   fprintf(stdout, "libmtp version: " LIBMTP_VERSION_STRING "\n\n");
     55 
     56   LIBMTP_Init();
     57   device = LIBMTP_Get_First_Device();
     58   if (device == NULL) {
     59     printf("No devices.\n");
     60     return 0;
     61   }
     62 
     63   printf("I will now reset your device. This means that\n");
     64   printf("the device may go inactive immediately and may report errors.\n");
     65   printf("Continue? (y/n)\n");
     66   if (prompt() == 0) {
     67     ret = LIBMTP_Reset_Device(device);
     68   } else {
     69     printf("Aborted.\n");
     70     ret = 0;
     71   }
     72 
     73   if ( ret != 0 ) {
     74     printf("Failed to reset device.\n");
     75     LIBMTP_Dump_Errorstack(device);
     76     LIBMTP_Clear_Errorstack(device);
     77     LIBMTP_Release_Device(device);
     78     return 1;
     79   }
     80 
     81   // It is not possible to release the device after successful reset!
     82   // LIBMTP_Release_Device(device);
     83   printf("OK.\n");
     84   return 0;
     85 }
     86