import unittest import os class Qeeprom(unittest.TestCase): params = None __resultlist = None # resultlist is a python list of python dictionaries __QEepromName = None __i2cBus = None __device = None # varlist: position, eeprompath def __init__(self, testname, testfunc, varlist): self.params = varlist super(Qeeprom, self).__init__(testfunc) self._testMethodDoc = testname self.__xmlObj = varlist["xml"] self.__QEepromName = varlist.get('name', 'qeeprom') self.__i2cAddress = varlist.get('i2c_address', self.__xmlObj.getKeyVal(self.__QEepromName, "i2c_address", "0050")) self.__i2cBus = varlist.get('i2c_bus', self.__xmlObj.getKeyVal(self.__QEepromName, "i2c_bus", "0")) self.__device = '/sys/bus/i2c/devices/{}-{}/eeprom'.format(self.__i2cBus, self.__i2cAddress) self.__resultlist = [] def __check_device(self): if os.path.isfile(self.__device): return True return False def __eeprom_write(self, data): try: f = open(self.__device, "wb") f.write(data) f.close() except IOError as Error: return False, '{}'.format(Error.errno) return True, '' def __eeprom_read(self, size): try: f = open(self.__device, "rb") data = f.read(size) f.close() except IOError as Error: return False, '{}'.format(Error.errno) return True, data def execute(self): if not self.__check_device(): self.fail("cannot open device {}".format(self.__device)) data = bytes('AbcDeFgHijK098521', 'utf-8') v, w = self.__eeprom_write(data) if not v: self.fail("eeprom: {} write test failed".format(w)) v, r = self.__eeprom_read(len(data)) if not v: self.fail("eeprom: {} read test failed".format(r)) if r != data: self.fail("mismatch between write and read bytes") def getresults(self): return self.__resultlist def gettextresult(self): return ""