summaryrefslogtreecommitdiff
path: root/test-cli/test/tests/qeeprom.py
blob: 79003819860b83977122b46cb891ce3aba716801 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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 ""