def usb_write(device,endpt,bytes,timeout=TIMEOUT,msg="\t-> "): bb_util.debug_bytes(bytes,msg) try: bb_util.debug2(">bulkwrite") device.handle.bulkWrite(endpt, bytes, timeout) bb_util.debug2("<bulkwrite") except usb.USBError, error: bb_util.debug2("<bulkwrite(exc)") # ! osx returns an empty error (no errorno) so we justcan't check anything :-( if str(error) != "No error" and not (bb_osx.is_osx() and error.errno == None): bb_messenging.log("error: "+str(error)) raise
def usb_read(device,endpt,size=BUF_SIZE,timeout=TIMEOUT,msg="\t<- "): bytes=[] try: bb_util.debug2(">bulkread") bytes=device.handle.bulkRead(endpt, size, timeout) bb_util.debug2("<bulkread") bb_util.debug_bytes(bytes,msg) except usb.USBError, error: bb_util.debug2("<bulkread (exc)") # ! osx returns an empty error (no errorno) so we justcan't check anything :-( if str(error) != "No error" and not (bb_osx.is_osx() and error.errno == None): bb_messenging.log("error: "+str(error)) raise
def usb_write(device, endpt, bytes, timeout=TIMEOUT, msg="\t-> "): bb_util.debug_bytes(bytes, msg) try: bb_util.debug2(">bulkwrite") device.handle.bulkWrite(endpt, bytes, timeout) bb_util.debug2("<bulkwrite") except usb.USBError, error: bb_util.debug2("<bulkwrite(exc)") # ! osx returns an empty error (no errorno) so we justcan't check anything :-( if str(error) != "No error" and not (bb_osx.is_osx() and error.errno == None): bb_messenging.log("error: " + str(error)) raise
def usb_read(device, endpt, size=BUF_SIZE, timeout=TIMEOUT, msg="\t<- "): bytes = [] try: bb_util.debug2(">bulkread") bytes = device.handle.bulkRead(endpt, size, timeout) bb_util.debug2("<bulkread") bb_util.debug_bytes(bytes, msg) except usb.USBError, error: bb_util.debug2("<bulkread (exc)") # ! osx returns an empty error (no errorno) so we justcan't check anything :-( if str(error) != "No error" and not (bb_osx.is_osx() and error.errno == None): bb_messenging.log("error: " + str(error)) raise
def remove_berry_charge(): # Remove module berry_charge if present, cause it causes problems if (not bb_osx.is_osx()) and module_loaded("berry_charge"): msg=[" * Module berry_charge is loaded, this might cause problems","\t -> Will try to unload it now"] bb_messenging.warn(msg) unload_module("berry_charge") if module_loaded("berry_charge"): msg=["************************************************************", "Could NOT unload module berry_charge ! (must be in use)", "You should probably blacklist it", "create a file as root called: /etc/modprobe.d/blackberry", "inside it write 'blacklist berry_charge' (without the quotes)", "save it, then unplug the blackberry", "run \"sudo /etc/init.d/udev restart\"", "replug the blackberry and try again", "************************************************************\n" ] bb_messenging.warn(msg) else: print "\t -> OK.\n"
def remove_berry_charge(): # Remove module berry_charge if present, cause it causes problems if (not bb_osx.is_osx()) and module_loaded("berry_charge"): msg = [ " * Module berry_charge is loaded, this might cause problems", "\t -> Will try to unload it now" ] bb_messenging.warn(msg) unload_module("berry_charge") if module_loaded("berry_charge"): msg = [ "************************************************************", "Could NOT unload module berry_charge ! (must be in use)", "You should probably blacklist it", "create a file as root called: /etc/modprobe.d/blackberry", "inside it write 'blacklist berry_charge' (without the quotes)", "save it, then unplug the blackberry", "run \"sudo /etc/init.d/udev restart\"", "replug the blackberry and try again", "************************************************************\n" ] bb_messenging.warn(msg) else: print "\t -> OK.\n"
def read_bb_endpoints(device, userInterface): ''' Read the device endpoints and stores them in the device data structure device was created from find_berry and device.open_handle should have been called already Once we found endpoints, we save them as some devices (esp. Bold) don't like being probed. ''' #look for previously saved endpoints config=bb_prefs.get_prefs() if config.has_section(SECTION_SCANNED_EP): device.interface=bb_prefs.get_def_int(bb_prefs.SECTION_SCANNED_EP,'interface',-1) device.readpt=bb_prefs.get_def_int(bb_prefs.SECTION_SCANNED_EP,'readpt',-1) device.writept=bb_prefs.get_def_int(bb_prefs.SECTION_SCANNED_EP,'writept',-1) device.modem_readpt=bb_prefs.get_def_int(bb_prefs.SECTION_SCANNED_EP,'modem_readpt',-1) device.modem_writept=bb_prefs.get_def_int(bb_prefs.SECTION_SCANNED_EP,'modem_writept',-1) bb_messenging.log("Using saved EP data: "+str(device.interface)+", "+str(device.readpt)+", "+str(device.writept)+", "+str(device.modem_readpt)+", "+str(device.modem_writept)) # return saved data if good (earlier version saved bad ones) if device.readpt != -1 and device.writept !=-1: return device else: print "Invalid saved endpoints, will rescan." readpt=-1 writept=-1 modem_readpt=-1 modem_writept=-1 next_readpt=-1 next_writept=-1 # List device Infos for information and find USB endpair handle=device.handle berry=device.usbdev config=berry.configurations[0] type="" if(berry.idProduct == PRODUCT_DATA): type="Data Mode" if(berry.idProduct == PRODUCT_NEW_DUAL): type="Dual Mode" if(berry.idProduct == PRODUCT_NEW_8120): type="8120" if(berry.idProduct == PRODUCT_NEW_MASS_ONLY): type="Storage Mode" bb_messenging.log("\nFound RIM device ("+type+")") bb_messenging.log(" Manufacturer:"+handle.getString(berry.iManufacturer,100)) bb_messenging.log(" Product:"+handle.getString(berry.iProduct,100)) #print " Serial:",handle.getString(berry.iSerialNumber,100) bb_messenging.log(" Device:"+berry.filename) bb_messenging.log(" VendorId: %04x" % berry.idVendor) bb_messenging.log(" ProductId: %04x" % berry.idProduct) bb_messenging.log(" Version:"+berry.deviceVersion) bb_messenging.log(" Class:"+str(berry.deviceClass)+" "+str(berry.deviceSubClass)) bb_messenging.log(" Protocol:"+str(berry.deviceProtocol)) bb_messenging.log(" Max packet size:"+str(berry.maxPacketSize)) bb_messenging.log(" Self Powered:"+str(config.selfPowered)) bb_messenging.log(" Max Power:"+str(config.maxPower)) for inter in config.interfaces: if len(inter) == 0: bb_messenging.log("Skipping Interface -> empty array !") continue bb_messenging.log("\n *Interface:"+str(inter[0].interfaceNumber)) if userInterface!=None and int(userInterface)!=inter[0].interfaceNumber: bb_messenging.log("Skipping interface (-i flag used)") continue if readpt != -1: bb_messenging.log("Skipping interface (valid endpoints already found), use -i flag to force") continue try: try: handle.claimInterface(inter[0].interfaceNumber) except usb.USBError, error: bb_messenging.log("Failed to claim interface: "+str(error)+"\nMust be in use.") if not bb_osx.is_osx(): #Only implemented on libusb Linux ! #For mac we need the kext stuff. bb_messenging.log("Will try to release it.") detach_kernel_driver(device,inter[0].interfaceNumber) try: handle.claimInterface(inter[0].interfaceNumber) bb_messenging.log("Interface is now claimed !") except usb.USBError, error: bb_messenging.log("Still could not claim the interface: "+str(error)) bb_messenging.log(" Interface class:"+str(inter[0].interfaceClass)+"/"+str(inter[0].interfaceSubClass)) bb_messenging.log(" Interface protocol:"+str(inter[0].interfaceProtocol)) for att in inter: i=0 # check endpoint pairs while i < len(att.endpoints): isDataPair=False red=att.endpoints[i].address writ=att.endpoints[i+1].address i+=2 bb_messenging.log(" EndPoint Pair:"+hex(red)+"/"+hex(writ)) try: usb_write(device,writ,COMMAND_HELLO) try: bytes=usb_read(device,red) if len(bytes) == 0: raise usb.USBError # on some devices, the modem replies to hello with (others, read fails): # [0x7 0x0 0x0 0x0 0xc 0x0 0x0 0x0 0x78 0x56 0x34 0x12 ] [........xV4.] if bb_util.is_same_tuple(bytes, MODEM_HELLO_REPLY): if modem_readpt==-1: modem_readpt=red modem_writept=writ bb_messenging.log(" Found Modem endpoints: "+hex(red)+"/"+hex(writ)) else: if readpt == -1 : # Use first valid data point found device.interface=inter[0].interfaceNumber bb_util.debug("Setting interface to: "+str(device.interface)) readpt=red writept=writ isDataPair=True bb_messenging.log(" Found Data endpoints: "+hex(red)+"/"+hex(writ)) except usb.USBError: bb_messenging.log(" Not Data Pair (Read failed)") except usb.USBError: bb_messenging.log(" Not Data Pair (Write failed)") if (isDataPair==False) and readpt != -1 and next_readpt == -1: next_readpt=red next_writept=writ bb_messenging.log(" Next endpoints:"+hex(red)+"/"+hex(writ)) handle.releaseInterface()
''' Deal with user preferences / settings Thibaut Colar ''' import ConfigParser from ConfigParser import NoSectionError, NoOptionError import bb_messenging import bb_osx import os SECTION_MAIN = "Global" SECTION_USER_EP = "User_EndPoints" SECTION_SCANNED_EP = "Scanned_EndPoints" PREF_FILE = os.environ['HOME'] + "/.bbtether.conf" if bb_osx.is_osx(): PREF_FILE = os.environ['HOME'] + "/Library/Preferences/bbtether.conf" my_config = None def set(section, option, value): config = get_prefs() if not config.has_section(section): config.add_section(section) # Note: we use str(value), because otherwise if we re-read the value it will fail if not a string config.set(section, option, str(value)) def get_def_string(section, option, default): config = get_prefs()
''' Deal with user preferences / settings Thibaut Colar ''' import ConfigParser from ConfigParser import NoSectionError, NoOptionError import bb_messenging import bb_osx import os SECTION_MAIN="Global" SECTION_USER_EP="User_EndPoints" SECTION_SCANNED_EP="Scanned_EndPoints" PREF_FILE=os.environ['HOME']+"/.bbtether.conf" if bb_osx.is_osx(): PREF_FILE=os.environ['HOME']+"/Library/Preferences/bbtether.conf" my_config=None def set(section,option,value): config=get_prefs() if not config.has_section(section): config.add_section(section) # Note: we use str(value), because otherwise if we re-read the value it will fail if not a string config.set(section, option, str(value)) def get_def_string(section, option, default): config=get_prefs() try: return config.get(section, option)
def read_bb_endpoints(device, userInterface): ''' Read the device endpoints and stores them in the device data structure device was created from find_berry and device.open_handle should have been called already Once we found endpoints, we save them as some devices (esp. Bold) don't like being probed. ''' #look for previously saved endpoints config = bb_prefs.get_prefs() if config.has_section(SECTION_SCANNED_EP): device.interface = bb_prefs.get_def_int(bb_prefs.SECTION_SCANNED_EP, 'interface', -1) device.readpt = bb_prefs.get_def_int(bb_prefs.SECTION_SCANNED_EP, 'readpt', -1) device.writept = bb_prefs.get_def_int(bb_prefs.SECTION_SCANNED_EP, 'writept', -1) device.modem_readpt = bb_prefs.get_def_int(bb_prefs.SECTION_SCANNED_EP, 'modem_readpt', -1) device.modem_writept = bb_prefs.get_def_int( bb_prefs.SECTION_SCANNED_EP, 'modem_writept', -1) bb_messenging.log("Using saved EP data: " + str(device.interface) + ", " + str(device.readpt) + ", " + str(device.writept) + ", " + str(device.modem_readpt) + ", " + str(device.modem_writept)) # return saved data if good (earlier version saved bad ones) if device.readpt != -1 and device.writept != -1: return device else: print "Invalid saved endpoints, will rescan." readpt = -1 writept = -1 modem_readpt = -1 modem_writept = -1 next_readpt = -1 next_writept = -1 # List device Infos for information and find USB endpair handle = device.handle berry = device.usbdev config = berry.configurations[0] type = "" if (berry.idProduct == PRODUCT_DATA): type = "Data Mode" if (berry.idProduct == PRODUCT_NEW_DUAL): type = "Dual Mode" if (berry.idProduct == PRODUCT_NEW_8120): type = "8120" if (berry.idProduct == PRODUCT_NEW_MASS_ONLY): type = "Storage Mode" bb_messenging.log("\nFound RIM device (" + type + ")") bb_messenging.log(" Manufacturer:" + handle.getString(berry.iManufacturer, 100)) bb_messenging.log(" Product:" + handle.getString(berry.iProduct, 100)) #print " Serial:",handle.getString(berry.iSerialNumber,100) bb_messenging.log(" Device:" + berry.filename) bb_messenging.log(" VendorId: %04x" % berry.idVendor) bb_messenging.log(" ProductId: %04x" % berry.idProduct) bb_messenging.log(" Version:" + berry.deviceVersion) bb_messenging.log(" Class:" + str(berry.deviceClass) + " " + str(berry.deviceSubClass)) bb_messenging.log(" Protocol:" + str(berry.deviceProtocol)) bb_messenging.log(" Max packet size:" + str(berry.maxPacketSize)) bb_messenging.log(" Self Powered:" + str(config.selfPowered)) bb_messenging.log(" Max Power:" + str(config.maxPower)) for inter in config.interfaces: if len(inter) == 0: bb_messenging.log("Skipping Interface -> empty array !") continue bb_messenging.log("\n *Interface:" + str(inter[0].interfaceNumber)) if userInterface != None and int( userInterface) != inter[0].interfaceNumber: bb_messenging.log("Skipping interface (-i flag used)") continue if readpt != -1: bb_messenging.log( "Skipping interface (valid endpoints already found), use -i flag to force" ) continue try: try: handle.claimInterface(inter[0].interfaceNumber) except usb.USBError, error: bb_messenging.log("Failed to claim interface: " + str(error) + "\nMust be in use.") if not bb_osx.is_osx(): #Only implemented on libusb Linux ! #For mac we need the kext stuff. bb_messenging.log("Will try to release it.") detach_kernel_driver(device, inter[0].interfaceNumber) try: handle.claimInterface(inter[0].interfaceNumber) bb_messenging.log("Interface is now claimed !") except usb.USBError, error: bb_messenging.log( "Still could not claim the interface: " + str(error)) bb_messenging.log(" Interface class:" + str(inter[0].interfaceClass) + "/" + str(inter[0].interfaceSubClass)) bb_messenging.log(" Interface protocol:" + str(inter[0].interfaceProtocol)) for att in inter: i = 0 # check endpoint pairs while i < len(att.endpoints): isDataPair = False red = att.endpoints[i].address writ = att.endpoints[i + 1].address i += 2 bb_messenging.log(" EndPoint Pair:" + hex(red) + "/" + hex(writ)) try: usb_write(device, writ, COMMAND_HELLO) try: bytes = usb_read(device, red) if len(bytes) == 0: raise usb.USBError # on some devices, the modem replies to hello with (others, read fails): # [0x7 0x0 0x0 0x0 0xc 0x0 0x0 0x0 0x78 0x56 0x34 0x12 ] [........xV4.] if bb_util.is_same_tuple(bytes, MODEM_HELLO_REPLY): if modem_readpt == -1: modem_readpt = red modem_writept = writ bb_messenging.log( " Found Modem endpoints: " + hex(red) + "/" + hex(writ)) else: if readpt == -1: # Use first valid data point found device.interface = inter[0].interfaceNumber bb_util.debug("Setting interface to: " + str(device.interface)) readpt = red writept = writ isDataPair = True bb_messenging.log( " Found Data endpoints: " + hex(red) + "/" + hex(writ)) except usb.USBError: bb_messenging.log( " Not Data Pair (Read failed)") except usb.USBError: bb_messenging.log( " Not Data Pair (Write failed)") if (isDataPair == False) and readpt != -1 and next_readpt == -1: next_readpt = red next_writept = writ bb_messenging.log(" Next endpoints:" + hex(red) + "/" + hex(writ)) handle.releaseInterface()