freemyipod r401 - Code Review

Jump to: navigation, search
Repository:freemyipod
Revision:r400‎ | r401 | r402 >
Date:04:08, 30 December 2010
Author:farthen
Status:new
Tags:
Comment:
libembios.py: Enable logging and include some debug strings.
Modified paths:
  • /embios/trunk/tools/embios.py (modified) (history)
  • /embios/trunk/tools/libembios.py (modified) (history)
  • /embios/trunk/tools/misc.py (modified) (history)

Diff [purge]

Index: embios/trunk/tools/misc.py
@@ -32,10 +32,11 @@
3333 class Logger(object):
3434 """
3535 Simple stdout logger.
36 - Loglevel 4 is most verbose, Loglevel 0: Only say something if there is an error.
 36+ Loglevel 3 is most verbose, Loglevel 0: Only log something if there is an error.
 37+ Loglevel -1 means that nothing is logged.
3738 The log function doesn't care about the loglevel and always logs to stdout.
3839 """
39 - def __init__(self, loglevel = 3, logfile = "tools.log", target = "stdout"):
 40+ def __init__(self, loglevel = 2, target = "stdout", logfile = "tools.log"):
4041 """
4142 loglevel: Possible values: 0 (only errors), 1 (warnings), 2 (info,
4243 recommended for production use), 3 and more (debug)
@@ -47,17 +48,18 @@
4849 self.target = target
4950
5051 def log(self, text, indent = 0, target = None):
51 - if target is None: target = self.target
52 - text = (indent * " ") + text
53 - text = text.replace("\n", "\n" + (indent * " "), text.count("\n") - 1)
54 - if target == "stdout":
55 - sys.stdout.write(text)
56 - elif target == "file":
57 - with open(self.logfile, 'a') as f:
58 - f.write(text)
59 - f.close()
60 - elif target == "string":
61 - return text
 52+ if self.loglevel >= 0:
 53+ if target is None: target = self.target
 54+ text = (indent * " ") + text
 55+ text = text.replace("\n", "\n" + (indent * " "), text.count("\n") - 1)
 56+ if target == "stdout":
 57+ sys.stdout.write(text)
 58+ elif target == "file":
 59+ with open(self.logfile, 'a') as f:
 60+ f.write(text)
 61+ f.close()
 62+ elif target == "string":
 63+ return text
6264
6365 def debug(self, text, indent = 0, target = None):
6466 if self.loglevel >= 3:
@@ -72,7 +74,8 @@
7375 self.log("WARNING: " + text, indent, target)
7476
7577 def error(self, text, indent = 0, target = None):
76 - self.log("ERROR: " + text, indent, target)
 78+ if self.loglevel >= 0:
 79+ self.log("ERROR: " + text, indent, target)
7780
7881
7982 class Bunch(dict):
Index: embios/trunk/tools/embios.py
@@ -124,7 +124,7 @@
125125 def __init__(self):
126126 self.logger = Logger()
127127 try:
128 - self.embios = libembios.Embios()
 128+ self.embios = libembios.Embios(loglevel = 2)
129129 except libembios.DeviceNotFoundError:
130130 self.logger.error("No emBIOS device found!")
131131 exit(1)
Index: embios/trunk/tools/libembios.py
@@ -26,7 +26,7 @@
2727 import usb.core
2828 import libembiosdata
2929
30 -from misc import Bunch, Error, gethwname
 30+from misc import Logger, Bunch, Error, gethwname
3131 from functools import wraps
3232
3333 class ArgumentError(Error):
@@ -92,8 +92,10 @@
9393 feature from external. So DON'T EVER use a parameter called 'timeout'
9494 in your commands. Variables are ok.
9595 """
96 - def __init__(self):
97 - self.lib = Lib()
 96+ def __init__(self, loglevel = 2, logtarget = "stdout", logfile = "tools.log"):
 97+ self.logger = Logger(loglevel, logtarget, logfile)
 98+ self.logger.debug("Initializing Embios object\n")
 99+ self.lib = Lib(self.logger)
98100
99101 self.getversioninfo()
100102 self.getpacketsizeinfo()
@@ -152,7 +154,9 @@
153155 self.lib.dev.version.majorv = resp.majorv
154156 self.lib.dev.version.minorv = resp.minorv
155157 self.lib.dev.version.patchv = resp.patchv
 158+ self.logger.debug("Device Software Type ID = " + str(resp.swtypeid) + "\n")
156159 self.lib.dev.swtypeid = resp.swtypeid
 160+ self.logger.debug("Device Hardware Type ID = " + str(resp.hwtypeid) + "\n")
157161 self.lib.dev.hwtypeid = resp.hwtypeid
158162 return resp
159163
@@ -162,9 +166,13 @@
163167 It also sets the properties of the device object accordingly.
164168 """
165169 resp = self.lib.monitorcommand(struct.pack("IIII", 1, 1, 0, 0), "HHII", ("coutmax", "cinmax", "doutmax", "dinmax"))
 170+ self.logger.debug("Device cout packet size limit = " + str(resp.coutmax) + "\n")
166171 self.lib.dev.packetsizelimit.cout = resp.coutmax
 172+ self.logger.debug("Device cin packet size limit = " + str(resp.cinmax) + "\n")
167173 self.lib.dev.packetsizelimit.cin = resp.cinmax
 174+ self.logger.debug("Device din packet size limit = " + str(resp.doutmax) + "\n")
168175 self.lib.dev.packetsizelimit.din = resp.dinmax
 176+ self.logger.debug("Device dout packet size limit = " + str(resp.dinmax) + "\n")
169177 self.lib.dev.packetsizelimit.dout = resp.doutmax
170178 return resp
171179
@@ -172,6 +180,7 @@
173181 def getusermemrange(self):
174182 """ This returns the memory range the user has access to. """
175183 resp = self.lib.monitorcommand(struct.pack("IIII", 1, 2, 0, 0), "III", ("lower", "upper", None))
 184+ self.logger.debug("Device user memory = 0x%x - 0x%x\n" % (resp.lower, resp.upper))
176185 self.lib.dev.usermem.lower = resp.lower
177186 self.lib.dev.usermem.upper = resp.upper
178187 return resp
@@ -802,7 +811,9 @@
803812
804813
805814 class Lib(object):
806 - def __init__(self):
 815+ def __init__(self, logger):
 816+ self.logger = logger
 817+ self.logger.debug("Initializing Lib object\n")
807818 self.idVendor = 0xFFFF
808819 self.idProduct = 0xE000
809820
@@ -811,10 +822,11 @@
812823 self.connect()
813824
814825 def connect(self):
815 - self.dev = Dev(self.idVendor, self.idProduct)
 826+ self.dev = Dev(self.idVendor, self.idProduct, self.logger)
816827 self.connected = True
817828
818829 def monitorcommand(self, cmd, rcvdatatypes=None, rcvstruct=None):
 830+ self.logger.debug("Sending monitorcommand\n")
819831 writelen = self.dev.cout(cmd)
820832 if rcvdatatypes:
821833 rcvdatatypes = "I" + rcvdatatypes # add the response
@@ -822,6 +834,7 @@
823835 data = struct.unpack(rcvdatatypes, data)
824836 response = data[0]
825837 if libembiosdata.responsecodes[response] == "ok":
 838+ self.logger.debug("Response: OK\n")
826839 if rcvstruct:
827840 datadict = Bunch()
828841 counter = 1 # start with 1, 0 is the id
@@ -833,20 +846,29 @@
834847 else:
835848 return data
836849 elif libembiosdata.responsecodes[response] == "unsupported":
 850+ self.logger.debug("Response: UNSUPPORTED\n")
837851 raise DeviceError("The device does not support this command.")
838852 elif libembiosdata.responsecodes[response] == "invalid":
 853+ self.logger.debug("Response: INVALID\n")
839854 raise DeviceError("Invalid command! This should NOT happen!")
840855 elif libembiosdata.responsecodes[response] == "busy":
 856+ self.logger.debug("Response: BUSY\n")
841857 raise DeviceError("Device busy")
 858+ else:
 859+ self.logger.debug("Response: UNKOWN\n")
 860+ raise DeviceError("Invalid response! This should NOT happen!")
842861 else:
843862 return writelen
844863
845864
846865 class Dev(object):
847 - def __init__(self, idVendor, idProduct):
 866+ def __init__(self, idVendor, idProduct, logger):
848867 self.idVendor = idVendor
849868 self.idProduct = idProduct
850869
 870+ self.logger = logger
 871+ self.logger.debug("Initializing Dev object\n")
 872+
851873 self.interface = 0
852874 self.timeout = 100
853875
@@ -853,6 +875,7 @@
854876 self.connect()
855877 self.findEndpoints()
856878
 879+ self.logger.debug("Successfully connected to device\n")
857880
858881 # Device properties
859882 self.packetsizelimit = Bunch()
@@ -877,6 +900,7 @@
878901 self.disconnect()
879902
880903 def findEndpoints(self):
 904+ self.logger.debug("Searching for device endpoints:\n")
881905 epcounter = 0
882906 self.endpoint = Bunch()
883907 for cfg in self.dev:
@@ -883,12 +907,16 @@
884908 for intf in cfg:
885909 for ep in intf:
886910 if epcounter == 0:
 911+ self.logger.debug("Found cout endpoint at 0x%x\n" % ep.bEndpointAddress)
887912 self.endpoint.cout = ep.bEndpointAddress
888913 elif epcounter == 1:
 914+ self.logger.debug("Found cin endpoint at 0x%x\n" % ep.bEndpointAddress)
889915 self.endpoint.cin = ep.bEndpointAddress
890916 elif epcounter == 2:
 917+ self.logger.debug("Found dout endpoint at 0x%x\n" % ep.bEndpointAddress)
891918 self.endpoint.dout = ep.bEndpointAddress
892919 elif epcounter == 3:
 920+ self.logger.debug("Found din endpoint at 0x%x\n" % ep.bEndpointAddress)
893921 self.endpoint.din = ep.bEndpointAddress
894922 epcounter += 1
895923 if epcounter <= 3:
@@ -895,9 +923,12 @@
896924 raise DeviceError("Not all endpoints found in the descriptor. Only "+str(epcounter)+" found, we need 4")
897925
898926 def connect(self):
 927+ self.logger.debug("Looking for emBIOS device\n")
899928 self.dev = usb.core.find(idVendor=self.idVendor, idProduct=self.idProduct)
900929 if self.dev is None:
901930 raise DeviceNotFoundError()
 931+ self.logger.debug("Device Found!\n")
 932+ self.logger.debug("Setting first configuration\n")
902933 self.dev.set_configuration()
903934
904935 def disconnect(self):
@@ -916,21 +947,25 @@
917948 return read
918949
919950 def cout(self, data):
 951+ self.logger.debug("Sending data to cout endpoint with the size " + str(len(data)) + "\n")
920952 if self.packetsizelimit.cout and len(data) > self.packetsizelimit.cout:
921953 raise SendError("Packet too big")
922954 return self.send(self.endpoint.cout, data)
923955
924956 def cin(self, size):
 957+ self.logger.debug("Receiving data on the cin endpoint with the size " + str(size) + "\n")
925958 if self.packetsizelimit.cin and size > self.packetsizelimit.cin:
926959 raise ReceiveError("Packet too big")
927960 return self.receive(self.endpoint.cin, size)
928961
929962 def dout(self, data):
 963+ self.logger.debug("Sending data to cout endpoint with the size " + str(len(data)) + "\n")
930964 if self.packetsizelimit.dout and len(data) > self.packetsizelimit.dout:
931965 raise SendError("Packet too big")
932966 return self.send(self.endpoint.dout, data)
933967
934968 def din(self, size):
 969+ self.logger.debug("Receiving data on the din endpoint with the size " + str(size) + "\n")
935970 if self.packetsizelimit.din and size > self.packetsizelimit.din:
936971 raise ReceiveError("Packet too big")
937972 return self.receive(self.endpoint.din, size)