summaryrefslogtreecommitdiff
path: root/test-cli/test/tests/qi2c.py
blob: 3afedfa1fea2ba3241c5fcb60d7b6bf6da0f6a21 (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
from test.helpers.syscmd import SysCommand
import unittest


class Qi2c(unittest.TestCase):
    params = None
    __resultlist = None  # resultlist is a python list of python dictionaries

    def __init__(self, testname, testfunc, varlist):
        self.params = varlist
        super(Qi2c, self).__init__(testfunc)
        if "busnum" in varlist:
            self.__busnum = varlist["busnum"]
        else:
            raise Exception('busnum param inside Qi2c must be defined')
        if "register" in varlist:
            self.__register = varlist["register"].split("/")
        else:
            raise Exception('register param inside Qi2c must be defined')
        self.__devices = []
        self._testMethodDoc = testname
        self.__resultlist = []

    def execute(self):
        str_cmd = "i2cdetect -a -y -r {}".format(self.__busnum)
        i2c_command = SysCommand("i2cdetect", str_cmd)
        if i2c_command.execute() == 0:
            self.__raw_out = i2c_command.getOutput()
            if self.__raw_out == "":
                return -1
            lines = self.__raw_out.decode('ascii').splitlines()
            for i in range(len(lines)):
                if lines[i].count('UU'):
                    if lines[i].find("UU"):
                        self.__devices.append(
                            "0x{}{}".format((i - 1), hex(int((lines[i].find("UU") - 4) / 3)).split('x')[-1]))
            for i in range(len(self.__register)):
                if not (self.__register[i] in self.__devices):
                    self.__resultlist.append(
                        {
                            "desc": "Test result",
                            "data": "FAILED: device {} not found in bus i2c-{}".format(self.__register[i], self.__busnum),
                            "type": "string"
                        }
                    )
                    self.fail("failed: device {} not found in bus i2c-{}".format(self.__register[i], self.__busnum))
        else:
            self.__resultlist.append(
                {
                    "desc": "Test result",
                    "data": "FAILED: could not complete i2cdedtect command",
                    "type": "string"
                }
            )
            self.fail("failed: could not complete i2cdedtect command.")

        # Test successful
        self.__resultlist.append(
            {
                "desc": "Test result",
                "data": "OK",
                "type": "string"
            }
        )

    def getresults(self):
        return self.__resultlist