| Index: embios/trunk/tools/misc.py |
| — | — | @@ -32,10 +32,11 @@ |
| 33 | 33 | class Logger(object):
|
| 34 | 34 | """
|
| 35 | 35 | 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.
|
| 37 | 38 | The log function doesn't care about the loglevel and always logs to stdout.
|
| 38 | 39 | """
|
| 39 | | - def __init__(self, loglevel = 3, logfile = "tools.log", target = "stdout"):
|
| | 40 | + def __init__(self, loglevel = 2, target = "stdout", logfile = "tools.log"):
|
| 40 | 41 | """
|
| 41 | 42 | loglevel: Possible values: 0 (only errors), 1 (warnings), 2 (info,
|
| 42 | 43 | recommended for production use), 3 and more (debug)
|
| — | — | @@ -47,17 +48,18 @@ |
| 48 | 49 | self.target = target
|
| 49 | 50 |
|
| 50 | 51 | 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
|
| 62 | 64 |
|
| 63 | 65 | def debug(self, text, indent = 0, target = None):
|
| 64 | 66 | if self.loglevel >= 3:
|
| — | — | @@ -72,7 +74,8 @@ |
| 73 | 75 | self.log("WARNING: " + text, indent, target)
|
| 74 | 76 |
|
| 75 | 77 | 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)
|
| 77 | 80 |
|
| 78 | 81 |
|
| 79 | 82 | class Bunch(dict):
|
| Index: embios/trunk/tools/embios.py |
| — | — | @@ -124,7 +124,7 @@ |
| 125 | 125 | def __init__(self):
|
| 126 | 126 | self.logger = Logger()
|
| 127 | 127 | try:
|
| 128 | | - self.embios = libembios.Embios()
|
| | 128 | + self.embios = libembios.Embios(loglevel = 2)
|
| 129 | 129 | except libembios.DeviceNotFoundError:
|
| 130 | 130 | self.logger.error("No emBIOS device found!")
|
| 131 | 131 | exit(1)
|
| Index: embios/trunk/tools/libembios.py |
| — | — | @@ -26,7 +26,7 @@ |
| 27 | 27 | import usb.core
|
| 28 | 28 | import libembiosdata
|
| 29 | 29 |
|
| 30 | | -from misc import Bunch, Error, gethwname
|
| | 30 | +from misc import Logger, Bunch, Error, gethwname
|
| 31 | 31 | from functools import wraps
|
| 32 | 32 |
|
| 33 | 33 | class ArgumentError(Error):
|
| — | — | @@ -92,8 +92,10 @@ |
| 93 | 93 | feature from external. So DON'T EVER use a parameter called 'timeout'
|
| 94 | 94 | in your commands. Variables are ok.
|
| 95 | 95 | """
|
| 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)
|
| 98 | 100 |
|
| 99 | 101 | self.getversioninfo()
|
| 100 | 102 | self.getpacketsizeinfo()
|
| — | — | @@ -152,7 +154,9 @@ |
| 153 | 155 | self.lib.dev.version.majorv = resp.majorv
|
| 154 | 156 | self.lib.dev.version.minorv = resp.minorv
|
| 155 | 157 | self.lib.dev.version.patchv = resp.patchv
|
| | 158 | + self.logger.debug("Device Software Type ID = " + str(resp.swtypeid) + "\n")
|
| 156 | 159 | self.lib.dev.swtypeid = resp.swtypeid
|
| | 160 | + self.logger.debug("Device Hardware Type ID = " + str(resp.hwtypeid) + "\n")
|
| 157 | 161 | self.lib.dev.hwtypeid = resp.hwtypeid
|
| 158 | 162 | return resp
|
| 159 | 163 |
|
| — | — | @@ -162,9 +166,13 @@ |
| 163 | 167 | It also sets the properties of the device object accordingly.
|
| 164 | 168 | """
|
| 165 | 169 | 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")
|
| 166 | 171 | self.lib.dev.packetsizelimit.cout = resp.coutmax
|
| | 172 | + self.logger.debug("Device cin packet size limit = " + str(resp.cinmax) + "\n")
|
| 167 | 173 | self.lib.dev.packetsizelimit.cin = resp.cinmax
|
| | 174 | + self.logger.debug("Device din packet size limit = " + str(resp.doutmax) + "\n")
|
| 168 | 175 | self.lib.dev.packetsizelimit.din = resp.dinmax
|
| | 176 | + self.logger.debug("Device dout packet size limit = " + str(resp.dinmax) + "\n")
|
| 169 | 177 | self.lib.dev.packetsizelimit.dout = resp.doutmax
|
| 170 | 178 | return resp
|
| 171 | 179 |
|
| — | — | @@ -172,6 +180,7 @@ |
| 173 | 181 | def getusermemrange(self):
|
| 174 | 182 | """ This returns the memory range the user has access to. """
|
| 175 | 183 | 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))
|
| 176 | 185 | self.lib.dev.usermem.lower = resp.lower
|
| 177 | 186 | self.lib.dev.usermem.upper = resp.upper
|
| 178 | 187 | return resp
|
| — | — | @@ -802,7 +811,9 @@ |
| 803 | 812 |
|
| 804 | 813 |
|
| 805 | 814 | class Lib(object):
|
| 806 | | - def __init__(self):
|
| | 815 | + def __init__(self, logger):
|
| | 816 | + self.logger = logger
|
| | 817 | + self.logger.debug("Initializing Lib object\n")
|
| 807 | 818 | self.idVendor = 0xFFFF
|
| 808 | 819 | self.idProduct = 0xE000
|
| 809 | 820 |
|
| — | — | @@ -811,10 +822,11 @@ |
| 812 | 823 | self.connect()
|
| 813 | 824 |
|
| 814 | 825 | def connect(self):
|
| 815 | | - self.dev = Dev(self.idVendor, self.idProduct)
|
| | 826 | + self.dev = Dev(self.idVendor, self.idProduct, self.logger)
|
| 816 | 827 | self.connected = True
|
| 817 | 828 |
|
| 818 | 829 | def monitorcommand(self, cmd, rcvdatatypes=None, rcvstruct=None):
|
| | 830 | + self.logger.debug("Sending monitorcommand\n")
|
| 819 | 831 | writelen = self.dev.cout(cmd)
|
| 820 | 832 | if rcvdatatypes:
|
| 821 | 833 | rcvdatatypes = "I" + rcvdatatypes # add the response
|
| — | — | @@ -822,6 +834,7 @@ |
| 823 | 835 | data = struct.unpack(rcvdatatypes, data)
|
| 824 | 836 | response = data[0]
|
| 825 | 837 | if libembiosdata.responsecodes[response] == "ok":
|
| | 838 | + self.logger.debug("Response: OK\n")
|
| 826 | 839 | if rcvstruct:
|
| 827 | 840 | datadict = Bunch()
|
| 828 | 841 | counter = 1 # start with 1, 0 is the id
|
| — | — | @@ -833,20 +846,29 @@ |
| 834 | 847 | else:
|
| 835 | 848 | return data
|
| 836 | 849 | elif libembiosdata.responsecodes[response] == "unsupported":
|
| | 850 | + self.logger.debug("Response: UNSUPPORTED\n")
|
| 837 | 851 | raise DeviceError("The device does not support this command.")
|
| 838 | 852 | elif libembiosdata.responsecodes[response] == "invalid":
|
| | 853 | + self.logger.debug("Response: INVALID\n")
|
| 839 | 854 | raise DeviceError("Invalid command! This should NOT happen!")
|
| 840 | 855 | elif libembiosdata.responsecodes[response] == "busy":
|
| | 856 | + self.logger.debug("Response: BUSY\n")
|
| 841 | 857 | raise DeviceError("Device busy")
|
| | 858 | + else:
|
| | 859 | + self.logger.debug("Response: UNKOWN\n")
|
| | 860 | + raise DeviceError("Invalid response! This should NOT happen!")
|
| 842 | 861 | else:
|
| 843 | 862 | return writelen
|
| 844 | 863 |
|
| 845 | 864 |
|
| 846 | 865 | class Dev(object):
|
| 847 | | - def __init__(self, idVendor, idProduct):
|
| | 866 | + def __init__(self, idVendor, idProduct, logger):
|
| 848 | 867 | self.idVendor = idVendor
|
| 849 | 868 | self.idProduct = idProduct
|
| 850 | 869 |
|
| | 870 | + self.logger = logger
|
| | 871 | + self.logger.debug("Initializing Dev object\n")
|
| | 872 | +
|
| 851 | 873 | self.interface = 0
|
| 852 | 874 | self.timeout = 100
|
| 853 | 875 |
|
| — | — | @@ -853,6 +875,7 @@ |
| 854 | 876 | self.connect()
|
| 855 | 877 | self.findEndpoints()
|
| 856 | 878 |
|
| | 879 | + self.logger.debug("Successfully connected to device\n")
|
| 857 | 880 |
|
| 858 | 881 | # Device properties
|
| 859 | 882 | self.packetsizelimit = Bunch()
|
| — | — | @@ -877,6 +900,7 @@ |
| 878 | 901 | self.disconnect()
|
| 879 | 902 |
|
| 880 | 903 | def findEndpoints(self):
|
| | 904 | + self.logger.debug("Searching for device endpoints:\n")
|
| 881 | 905 | epcounter = 0
|
| 882 | 906 | self.endpoint = Bunch()
|
| 883 | 907 | for cfg in self.dev:
|
| — | — | @@ -883,12 +907,16 @@ |
| 884 | 908 | for intf in cfg:
|
| 885 | 909 | for ep in intf:
|
| 886 | 910 | if epcounter == 0:
|
| | 911 | + self.logger.debug("Found cout endpoint at 0x%x\n" % ep.bEndpointAddress)
|
| 887 | 912 | self.endpoint.cout = ep.bEndpointAddress
|
| 888 | 913 | elif epcounter == 1:
|
| | 914 | + self.logger.debug("Found cin endpoint at 0x%x\n" % ep.bEndpointAddress)
|
| 889 | 915 | self.endpoint.cin = ep.bEndpointAddress
|
| 890 | 916 | elif epcounter == 2:
|
| | 917 | + self.logger.debug("Found dout endpoint at 0x%x\n" % ep.bEndpointAddress)
|
| 891 | 918 | self.endpoint.dout = ep.bEndpointAddress
|
| 892 | 919 | elif epcounter == 3:
|
| | 920 | + self.logger.debug("Found din endpoint at 0x%x\n" % ep.bEndpointAddress)
|
| 893 | 921 | self.endpoint.din = ep.bEndpointAddress
|
| 894 | 922 | epcounter += 1
|
| 895 | 923 | if epcounter <= 3:
|
| — | — | @@ -895,9 +923,12 @@ |
| 896 | 924 | raise DeviceError("Not all endpoints found in the descriptor. Only "+str(epcounter)+" found, we need 4")
|
| 897 | 925 |
|
| 898 | 926 | def connect(self):
|
| | 927 | + self.logger.debug("Looking for emBIOS device\n")
|
| 899 | 928 | self.dev = usb.core.find(idVendor=self.idVendor, idProduct=self.idProduct)
|
| 900 | 929 | if self.dev is None:
|
| 901 | 930 | raise DeviceNotFoundError()
|
| | 931 | + self.logger.debug("Device Found!\n")
|
| | 932 | + self.logger.debug("Setting first configuration\n")
|
| 902 | 933 | self.dev.set_configuration()
|
| 903 | 934 |
|
| 904 | 935 | def disconnect(self):
|
| — | — | @@ -916,21 +947,25 @@ |
| 917 | 948 | return read
|
| 918 | 949 |
|
| 919 | 950 | def cout(self, data):
|
| | 951 | + self.logger.debug("Sending data to cout endpoint with the size " + str(len(data)) + "\n")
|
| 920 | 952 | if self.packetsizelimit.cout and len(data) > self.packetsizelimit.cout:
|
| 921 | 953 | raise SendError("Packet too big")
|
| 922 | 954 | return self.send(self.endpoint.cout, data)
|
| 923 | 955 |
|
| 924 | 956 | def cin(self, size):
|
| | 957 | + self.logger.debug("Receiving data on the cin endpoint with the size " + str(size) + "\n")
|
| 925 | 958 | if self.packetsizelimit.cin and size > self.packetsizelimit.cin:
|
| 926 | 959 | raise ReceiveError("Packet too big")
|
| 927 | 960 | return self.receive(self.endpoint.cin, size)
|
| 928 | 961 |
|
| 929 | 962 | def dout(self, data):
|
| | 963 | + self.logger.debug("Sending data to cout endpoint with the size " + str(len(data)) + "\n")
|
| 930 | 964 | if self.packetsizelimit.dout and len(data) > self.packetsizelimit.dout:
|
| 931 | 965 | raise SendError("Packet too big")
|
| 932 | 966 | return self.send(self.endpoint.dout, data)
|
| 933 | 967 |
|
| 934 | 968 | def din(self, size):
|
| | 969 | + self.logger.debug("Receiving data on the din endpoint with the size " + str(size) + "\n")
|
| 935 | 970 | if self.packetsizelimit.din and size > self.packetsizelimit.din:
|
| 936 | 971 | raise ReceiveError("Packet too big")
|
| 937 | 972 | return self.receive(self.endpoint.din, size)
|