lib: moved mimedump.h from tools/ into the library (mimeio.h)
[barry/progweb.git] / tools / breset_libusb_1_0.cc
blob05e58c74987c1b13cc52cb4926c21e27ca2af060
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-2012, 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 bool ret = libusb_reset_device(handle) == 0;
58 libusb_close(handle);
59 return ret;
62 int main()
64 libusb_context *usbctx = NULL;
65 libusb_device **devices = NULL;
66 int found = 0;
67 int count = 0;
68 int i;
70 INIT_I18N(PACKAGE);
72 int err = libusb_init(&usbctx);
73 if( err != 0 ) {
74 printf(_("Failed to start USB: %d\n"), err);
77 printf(_("Scanning for Blackberry devices...\n"));
79 count = libusb_get_device_list(usbctx, &devices);
81 for(i = 0; i < count; ++i ) {
82 libusb_device *dev = devices[i];
83 uint8_t devnum = libusb_get_device_address(dev);
84 uint8_t busnum = libusb_get_bus_number(dev);
85 struct libusb_device_descriptor desc;
86 char busname[MAX_BUSNAME_LENGTH];
87 int err = libusb_get_device_descriptor(dev, &desc);
88 snprintf(busname, sizeof(busname),
89 BUSNAME_FORMAT_STR, busnum);
90 // Be sure it's nul terminated
91 busname[MAX_BUSNAME_LENGTH - 1] = 0;
93 // Is this a blackberry?
94 if( err == 0 &&
95 desc.idVendor == VENDOR_RIM &&
96 (desc.idProduct == PRODUCT_RIM_BLACKBERRY ||
97 desc.idProduct == PRODUCT_RIM_PEARL ||
98 desc.idProduct == PRODUCT_RIM_PEARL_8120 ||
99 desc.idProduct == PRODUCT_RIM_PEARL_DUAL ) ) {
100 printf(_("Found..."));
101 printf(_("attempting to reset.\n"));
102 if( reset(dev) )
103 found++;
104 else
105 printf(_("Can't reset device on bus %s, devnum %u\n"), busname, devnum);
109 printf(_("%d device%s reset.\n"), found, found > 1 ? "s" : "");
111 if( devices )
112 libusb_free_device_list(devices, 1);
113 libusb_exit(usbctx);