Barry debian version 0.18.5-1
[barry.git] / tools / breset_libusb_1_0.cc
blobff71dad1c378576a40665630b33c7e9b0dbdf953
1 ///
2 /// \file breset_libusb_1_0.cc
3 /// Attempt to reset all connected Blackberry devices via software
4 ///
5 /// This file is part of the Barry project:
6 ///
7 /// http://www.netdirect.ca/software/packages/barry
8 /// http://sourceforge.net/projects/barry
9 ///
10 /// Compile with the following command (needs libusb 1.0):
11 ///
12 /// g++ -o breset breset.cc -I/usr/include/libusb -lusb-1.0
13 ///
16 Copyright (C) 2007-2013, Net Direct Inc. (http://www.netdirect.ca/)
17 Portions Copyright (C) 2011, RealVNC Ltd.
19 This program is free software; you can redistribute it and/or modify
20 it under the terms of the GNU General Public License as published by
21 the Free Software Foundation; either version 2 of the License, or
22 (at your option) any later version.
24 This program is distributed in the hope that it will be useful,
25 but WITHOUT ANY WARRANTY; without even the implied warranty of
26 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28 See the GNU General Public License in the COPYING file at the
29 root directory of this project for more details.
32 #include <libusb.h>
33 #include <stdio.h>
34 #include <unistd.h>
35 #include "i18n.h"
37 #define VENDOR_RIM 0x0fca
38 #define PRODUCT_RIM_BLACKBERRY 0x0001
39 #define PRODUCT_RIM_PEARL_DUAL 0x0004
40 #define PRODUCT_RIM_PEARL_8120 0x8004
41 #define PRODUCT_RIM_PEARL 0x0006
43 #define BLACKBERRY_INTERFACE 0
44 #define BLACKBERRY_CONFIGURATION 1
46 #define BUSNAME_FORMAT_STR "libusb-%d"
47 // USB bus numbers can only go up to 256, i.e. 3 characters
48 #define MAX_BUSNAME_LENGTH (7 + 3 + 1)
50 bool reset(struct libusb_device *dev)
52 libusb_device_handle *handle = NULL;
53 int err = libusb_open(dev, &handle);
54 if( err != 0 )
55 return false;
57 #ifdef WIN32
58 // Attempt to set the configuration as a way to reset
59 libusb_set_configuration(handle, BLACKBERRY_CONFIGURATION);
60 #endif
62 bool ret = libusb_reset_device(handle) == 0;
63 libusb_close(handle);
64 return ret;
67 int main()
69 libusb_context *usbctx = NULL;
70 libusb_device **devices = NULL;
71 int found = 0;
72 int count = 0;
73 int i;
75 INIT_I18N(PACKAGE);
77 int err = libusb_init(&usbctx);
78 if( err != 0 ) {
79 printf(_("Failed to start USB: %d\n"), err);
82 printf(_("Scanning for Blackberry devices...\n"));
84 count = libusb_get_device_list(usbctx, &devices);
86 for(i = 0; i < count; ++i ) {
87 libusb_device *dev = devices[i];
88 uint8_t devnum = libusb_get_device_address(dev);
89 uint8_t busnum = libusb_get_bus_number(dev);
90 struct libusb_device_descriptor desc;
91 char busname[MAX_BUSNAME_LENGTH];
92 int err = libusb_get_device_descriptor(dev, &desc);
93 snprintf(busname, sizeof(busname),
94 BUSNAME_FORMAT_STR, busnum);
95 // Be sure it's nul terminated
96 busname[MAX_BUSNAME_LENGTH - 1] = 0;
98 // Is this a blackberry?
99 if( err == 0 &&
100 desc.idVendor == VENDOR_RIM &&
101 (desc.idProduct == PRODUCT_RIM_BLACKBERRY ||
102 desc.idProduct == PRODUCT_RIM_PEARL ||
103 desc.idProduct == PRODUCT_RIM_PEARL_8120 ||
104 desc.idProduct == PRODUCT_RIM_PEARL_DUAL ) ) {
105 printf(_("Found..."));
106 printf(_("attempting to reset.\n"));
107 if( reset(dev) )
108 found++;
109 else
110 printf(_("Can't reset device on bus %s, devnum %u\n"), busname, devnum);
114 printf(_("%d device%s reset.\n"), found, found > 1 ? "s" : "");
116 if( devices )
117 libusb_free_device_list(devices, 1);
118 libusb_exit(usbctx);