Commits

tcolar  committed 960be69

0.2g

  • Participants
  • Parent commits a220133

Comments (0)

Files changed (4)

File ChangeLog.txt

 Thibaut Colar
 tcolar AT colar d.o.t NET
 
+0.2g: 6/12/2009:
+	Made charging mode switch (more power) optional, as this does not work/not needed on storm.
+	Replaced SHA1 library(deprecated) by the new Hashlib
+	Only reset device if needed (no reply to modem start - Pearl)
+	Try modem init cycle multiple times (incl. reset) until good to go.
+	Added "Data Mode" flag option (-m)
+
 0.2f: 3/10/2009
 	Added password protected device support (tested on Pearl)
 	Added (to be tested) support for ppp that sends end of line as 0xD rather than 0xD,0xA

File src/bbtether/bb_modem.py

 
 import bb_usb
 import bb_util
+import hashlib
 import os
 import random
-import sha
 import subprocess
 import threading
 import usb
 		# clear endpoints
 		bb_usb.clear_halt(self.device,self.device.modem_readpt)
 		bb_usb.clear_halt(self.device,self.device.modem_writept)
+
 		# reset modem
-		self.write(MODEM_STOP)
-		self.read()
-		self.write(MODEM_STOP)
-		self.read()
-		self.write(MODEM_START)
-		answer=self.read()
+		# ok, on pearl it will fail the first time, because after querying the modem (hello commands) it's broke and need a reset
+		# on newer devices (storm), the modem query works, so rest is not needed (and maybe causes issues)
+		answer=""
+		resetted=False
+		reset_time=0
+		while True:
+			self.write(MODEM_STOP)
+			self.read()
+			self.write(MODEM_STOP)
+			self.read()
+			self.write(MODEM_START)
+			try:
+				answer=self.read()
+			except OSError, error:
+				print "Read failed: "+error.message
+				answer=""
+			if len(answer) == 0:
+				reset_time+=5
+				if not resetted:
+					print "No answer to modem start command ... will try a reset"
+					bb_usb.reset(self.device)
+					resetted=True
+				if reset_time > 5*60:
+					print "Timeout while trying to init modem, exiting."
+					os._exit(0)
+				print "Waiting for reset completion"
+				time.sleep(5)
+			else:
+				break
+
 		# check for password request (newer devices)
 		if len(answer)>0 and answer[0]==0x2 and bb_util.end_with_tuple(answer,RIM_PACKET_TAIL):
 			triesLeft=answer[8]
 
 	def send_password(self, seed):
 		seed_bytes=array.array("B",seed)
-		digest=sha.new(self.password).digest()
+		sha1=hashlib.sha1()
+		digest=sha1.update(self.password).digest()
 		digest_bytes=array.array("B",digest)
 		seed_bytes.extend(digest_bytes)
-		digest2=sha.new(seed_bytes.tostring()).digest()
+		sha1=hashlib.sha1()
+		digest2=sha1.update(seed_bytes.tostring()).digest()
 		digest_list=array.array("B",digest2).tolist()
 		response=[0x3, 0, 0, 0 ]+digest_list+RIM_PACKET_TAIL
 		bb_util.debug("Sending password digest: ")

File src/bbtether/bb_usb.py

 		device.handle.controlMsg(0xc0, 0xa5, buffer, 0 , 1)
 		buffer = []
 		device.handle.controlMsg(0x40, 0xA2, buffer, 0 , 1)
-		device.handle.reset()
+		# reset
+		reset()
 	except usb.USBError, error:
 		print "Error increasing power ",error.message,", continuing anyway."
-	#print "Switching Device to data only mode"
-	#try:
-	#	buffer= [0,0]
-	#	device.handle.controlMsg(0xc0, 0xa9, buffer, 0 , 1)
-	#except usb.USBError, error:
-	#print "Error setting device to data mode ",error.message,", continuing anyway."
+
+def set_data_mode(device):
+	print "Switching Device to data only mode"
+	try:
+		buffer= [0,0]
+		device.handle.controlMsg(0xc0, 0xa9, buffer, 0 , 1)
+	except usb.USBError, error:
+		print "Error setting device to data mode ",error.message,", continuing anyway."
+
+def reset(device):
+	print "Resetting device"
+	device.handle.reset()
 
 def get_pin(device):
 	pin=0x0;

File src/bbtether/bbtether.py

 from optparse import OptionParser
 import os
 
-VERSION = "0.2f"
+VERSION = "0.2g"
 
 ''' Main Class '''
 class BBTether:
 	def parse_cmd(self):
 		usage = usage = "usage: %prog [options] [pppscript]\n	If [pppscript] is there (ppp script in conf/ folder, ex: tmobile) then will start modem and connect using that ppp script. Otherwise just opens the modem and you will have to start pppd manually."
 		parser = OptionParser(usage)
-		parser.add_option("-P", "--password", dest="password", help="Blackberry password (if passewrd protected) ex: -P abc123")
+		parser.add_option("-P", "--password", dest="password", help="Blackberry password (if passowrd protected) ex: -P abc123")
 		parser.add_option("-l", "--list", action="store_true", dest="listonly", help="Only detect and list Device, do nothing more")
 		parser.add_option("-p", "--pppd", dest="pppd", help="Path To pppd binary (default: /usr/sbin/pppd)")
 		parser.add_option("-v", "--verbose", action="store_true", dest="verbose", help="Verbose: Show I/O data and other infos")
-		parser.add_option("-c", "--charge", action="store_true", dest="chargeonly", help="Put the device in Charging mode and does NOT start modem.")
+		parser.add_option("-c", "--charge", action="store_true", dest="charge", help="Put the device in Charging mode (ex: Pearl) and reset it.")
+		parser.add_option("-m", "--dmode", action="store_true", dest="dmode", help="Put the device in data mode, might help on some devices.")
 		group = OptionGroup(parser, "Advanced Options", "Don't use unless you know what you are doing.")
 		group.add_option("-w", "--drp", dest="drp", help="Force Data read endpoint - Hex(ex -w 0x84)")
 		group.add_option("-x", "--dwp", dest="dwp", help="Force Data write endpoint - Hex (ex -x 0x6)")
 			#bb_usbfs.find_kernel_driver(berry)
 
 			# lookup endpoints
-			# IMPORTANT: We need to do this BEFDRE RESET, otherwise modem will be screwed
-			# folowing "test" hello packet
+			# IMPORTANT: We need to do this BEFORE RESET, otherwise modem will be screwed
+			# folowing "test" hello packet (fail on Pearl, ok on storm)
 			if not (options.drp and options.dwp and options.mrp and options.mwp):
 				berry.read_endpoints(options.interface)
 
 				print "Listing only requested, stopping here."
 				os._exit(0)
 
-			# set power & reset
-			bb_usb.set_bb_power(berry)
+			bb_util.remove_berry_charge()
 
-			time.sleep(1)
+			# set power & reset (only if '-c' requested)
+			# Only needed with BB os < 4.5 ?
+			if options.charge:
+				bb_usb.set_bb_power(berry)
+				print ("Waiting few seconds, for mode to change")
+				time.sleep(1.5)
 
-			bb_util.remove_berry_charge()
-			
-			if options.chargeonly:
-				print "Charge only requested, stopping now."
-				os._exit(0)
-			
-			# reopen
-			print ("Waiting few seconds, for mode to change")
-			time.sleep(1.5)
-			
+			# set to datamode (ony if requested)
+			if options.dmode:
+				bb_usb.set_data_mode(berry)
+
 			# overwrite found endpoints with user endpoints if specified
 			if options.drp:
 				berry.readpt = int(options.drp, 16)
 				print "\nNo good Data Endpoint pair, bailing out !";
 			else:
 				print "\nUsing Data Endpoint Pair:", hex(berry.readpt), "/", hex(berry.writept);
-				print "Using first pair after Data pair as Modem pair: ", hex(berry.modem_readpt), "/", hex(berry.modem_writept), "\n"
+				print "Using Modem pair: ", hex(berry.modem_readpt), "/", hex(berry.modem_writept), "\n"
 				
 				print "Claiming interface ",berry.interface
 				berry.claim_interface()