breset_libusb_1_0.cc

Go to the documentation of this file.
00001 ///
00002 /// \file       breset_libusb_1_0.cc
00003 ///             Attempt to reset all connected Blackberry devices via software
00004 ///
00005 ///             This file is part of the Barry project:
00006 ///
00007 ///             http://www.netdirect.ca/software/packages/barry
00008 ///             http://sourceforge.net/projects/barry
00009 ///
00010 ///             Compile with the following command (needs libusb 1.0):
00011 ///
00012 ///             g++ -o breset breset.cc -I/usr/include/libusb -lusb-1.0
00013 ///
00014 
00015 /*
00016     Copyright (C) 2007-2012, Net Direct Inc. (http://www.netdirect.ca/)
00017     Portions Copyright (C) 2011, RealVNC Ltd.
00018 
00019     This program is free software; you can redistribute it and/or modify
00020     it under the terms of the GNU General Public License as published by
00021     the Free Software Foundation; either version 2 of the License, or
00022     (at your option) any later version.
00023 
00024     This program is distributed in the hope that it will be useful,
00025     but WITHOUT ANY WARRANTY; without even the implied warranty of
00026     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00027 
00028     See the GNU General Public License in the COPYING file at the
00029     root directory of this project for more details.
00030 */
00031 
00032 #include <libusb.h>
00033 #include <stdio.h>
00034 #include <unistd.h>
00035 #include "i18n.h"
00036 
00037 #define VENDOR_RIM              0x0fca
00038 #define PRODUCT_RIM_BLACKBERRY  0x0001
00039 #define PRODUCT_RIM_PEARL_DUAL  0x0004
00040 #define PRODUCT_RIM_PEARL_8120  0x8004
00041 #define PRODUCT_RIM_PEARL       0x0006
00042 
00043 #define BLACKBERRY_INTERFACE            0
00044 #define BLACKBERRY_CONFIGURATION        1
00045 
00046 #define BUSNAME_FORMAT_STR "libusb-%d"
00047 // USB bus numbers can only go up to 256, i.e. 3 characters
00048 #define MAX_BUSNAME_LENGTH (7 + 3 + 1)
00049 
00050 bool reset(struct libusb_device *dev)
00051 {
00052         libusb_device_handle *handle = NULL;
00053         int err = libusb_open(dev, &handle);
00054         if( err != 0 )
00055                 return false;
00056 
00057         bool ret = libusb_reset_device(handle) == 0;
00058         libusb_close(handle);
00059         return ret;
00060 }
00061 
00062 int main()
00063 {
00064         libusb_context *usbctx = NULL;
00065         libusb_device **devices = NULL;
00066         int found = 0;
00067         int count = 0;
00068         int i;
00069 
00070         INIT_I18N(PACKAGE);
00071 
00072         int err = libusb_init(&usbctx);
00073         if( err != 0 ) {
00074                 printf(_("Failed to start USB: %d\n"), err);
00075         }
00076 
00077         printf(_("Scanning for Blackberry devices...\n"));
00078 
00079         count = libusb_get_device_list(usbctx, &devices);
00080 
00081         for(i = 0; i < count; ++i ) {
00082                 libusb_device *dev = devices[i];
00083                 uint8_t devnum = libusb_get_device_address(dev);
00084                 uint8_t busnum = libusb_get_bus_number(dev);
00085                 struct libusb_device_descriptor desc;
00086                 char busname[MAX_BUSNAME_LENGTH];
00087                 int err = libusb_get_device_descriptor(dev, &desc);
00088                 snprintf(busname, sizeof(busname),
00089                          BUSNAME_FORMAT_STR, busnum);
00090                 // Be sure it's nul terminated
00091                 busname[MAX_BUSNAME_LENGTH - 1] = 0;
00092                 
00093                 // Is this a blackberry?
00094                 if( err == 0 &&
00095                     desc.idVendor == VENDOR_RIM &&
00096                     (desc.idProduct == PRODUCT_RIM_BLACKBERRY ||
00097                      desc.idProduct == PRODUCT_RIM_PEARL ||
00098                      desc.idProduct == PRODUCT_RIM_PEARL_8120 ||
00099                      desc.idProduct == PRODUCT_RIM_PEARL_DUAL ) ) {
00100                         printf(_("Found..."));
00101                         printf(_("attempting to reset.\n"));
00102                         if( reset(dev) )
00103                                 found++;
00104                         else
00105                                 printf(_("Can't reset device on bus %s, devnum %u\n"), busname, devnum);
00106                 }
00107         }
00108 
00109         printf(_("%d device%s reset.\n"), found, found > 1 ? "s" : "");
00110         
00111         if( devices )
00112                 libusb_free_device_list(devices, 1);
00113         libusb_exit(usbctx);
00114 }
00115