1# Lint as: python2, python3
2# Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""A Python library to interact with TTCI module for TPM testing.
7
8Background
9 - TTCI stands for TPM Test Controller Interface
10 - TTCI is a custom-designed hardware board that can be used to test TPM module
11 - TTCI board contains two modules: PCA9555 and INA219. This library provides
12   methods to interact with these modules programmatically
13
14Dependency
15 - This library depends on a new C shared library called "libsmogcheck.so".
16 - In order to run test cases built using this API, one needs a TTCI board
17
18Notes:
19 - An exception is raised if it doesn't make logical sense to continue program
20   flow (e.g. I/O error prevents test case from executing)
21 - An exception is caught and then converted to an error code if the caller
22   expects to check for error code per API definition
23"""
24
25import logging
26from autotest_lib.client.common_lib import smogcheck_ina219, smogcheck_pca9555
27
28
29# I2C slave addresses of INA219 module
30INA219_BPWR_SLV = 0x40  # Backup Power
31INA219_MPWR_SLV = 0x44  # Main Power
32
33
34class TtciError(Exception):
35    """Base class for all errors in this module."""
36
37
38class TtciController(object):
39    """Object to control TTCI board used for TPM module testing."""
40
41    def __init__(self):
42        """Constructor.
43
44        Mandatory params:
45          err: error string.
46          ina_backup_obj: an instance of InaController (for Backup Power port
47                          of INA219 module).
48          ina_main_obj: an instance of InaController (for Main Power port
49                        of INA219 module).
50          pca_obj: an instance of PcaController.
51
52        Raises:
53          TtciError: if error initializing TTCI controller.
54        """
55        self.err = None
56        try:
57            # Initialize PCA9555 module.
58            self.pca_obj = smogcheck_pca9555.PcaController()
59
60            # Initialize INA219 module.
61            self.ina_main_obj = smogcheck_ina219.InaController(
62                node_addr=INA219_MPWR_SLV)
63            self.ina_backup_obj = smogcheck_ina219.InaController(
64                node_addr=INA219_BPWR_SLV)
65        except smogcheck_pca9555.PcaError as e:
66            raise TtciError('Error initialize PCA9555 module: %s' % e)
67        except smogcheck_ina219.InaError as e:
68            raise TtciError('Error initialize INA219 module: %s' % e)
69
70    def TTCI_Get_Main_Power_Metrics(self):
71        """Gets voltage and current measurements from INA219 Main Power.
72
73        See docstring of getPowerMetrics() in smogcheck_ina219.py.
74        """
75        return self.ina_main_obj.getPowerMetrics()
76
77    def TTCI_Get_Backup_Power_Metrics(self):
78        """Gets voltage and current measurements from INA219 Backup Power.
79
80        See docstring of getPowerMetrics() in smogcheck_ina219.py.
81        """
82        return self.ina_backup_obj.getPowerMetrics()
83
84    def TTCI_Set_Main_Power_Control(self, turn_on):
85        """De/activated TPM Main Power.
86
87        Args:
88          turn_on: a boolean, on (true) = set bit to 1.
89
90        See docstring of setPCAcontrol() in smogcheck_pca9555.py.
91        """
92        return self.pca_obj.setPCAcontrol('main_power', turn_on=turn_on)
93
94    def TTCI_Set_Backup_Power_Control(self, turn_on):
95        """De/activated TPM Backup Power.
96
97        Args:
98          turn_on: a boolean, on (true) = set bit to 1.
99
100        See docstring of setPCAcontrol() in smogcheck_pca9555.py.
101        """
102        return self.pca_obj.setPCAcontrol('backup_power', turn_on=turn_on)
103
104    def TTCI_Set_Reset_Control(self, turn_on):
105        """De/activated TPM Reset.
106
107        Exception note:
108          for TPM Reset, true means setting bit value to 0 (not 1).
109
110        Args:
111          turn_on: a boolean, on (true) = set bit to 0.
112
113        See docstring of setPCAcontrol() in smogcheck_pca9555.py.
114        """
115        return self.pca_obj.setPCAcontrol('reset', turn_on=not(turn_on))
116
117    def TTCI_Set_PP_Control(self, turn_on):
118        """De/activated TPM Physical Presence.
119
120        Args:
121          turn_on: a boolean, on (true) = set bit to 1.
122
123        See docstring of setPCAcontrol() in smogcheck_pca9555.py.
124        """
125        return self.pca_obj.setPCAcontrol('pp', turn_on=turn_on)
126
127    def TTCI_Set_TPM_I2C_Control(self, turn_on):
128        """Enable/Disable I2C communication with TPM.
129
130        Args:
131          turn_on: a boolean, on (true) = set bit to 1.
132
133        See docstring of setPCAcontrol() in smogcheck_pca9555.py.
134        """
135        return self.pca_obj.setPCAcontrol('tpm_i2c', turn_on=turn_on)
136
137    def TTCI_Get_Main_Power_Status(self):
138        """Checks bit value of Main Power.
139
140        See docstring of getPCAbitStatus() in smogcheck_pca9555.py.
141        """
142        return self.pca_obj.getPCAbitStatus('main_power')
143
144    def TTCI_Get_Backup_Power_Status(self):
145        """Checks bit value of Backup Power.
146
147        See docstring of getPCAbitStatus() in smogcheck_pca9555.py.
148        """
149        return self.pca_obj.getPCAbitStatus('backup_power')
150
151    def TTCI_Get_PP_Status(self):
152        """Checks bit value of Physical Presence.
153
154        See docstring of getPCAbitStatus() in smogcheck_pca9555.py.
155        """
156        return self.pca_obj.getPCAbitStatus('pp')
157
158    def TTCI_Get_TPM_I2C_Status(self):
159        """Checks bit value of TPM I2C.
160
161        See docstring of getPCAbitStatus() in smogcheck_pca9555.py.
162        """
163        return self.pca_obj.getPCAbitStatus('tpm_i2c')
164
165    def TTCI_Set_LEDs(self, bit_value, failure, warning):
166        """De/activates PCA9555 LEDs.
167
168        See docstring of setLEDs() in smogcheck_pca9555.py.
169        """
170        return self.pca_obj.setLEDs(bit_value, failure, warning)
171
172    def TTCI_Get_Switch_Status(self):
173        """Checks status of DIP Switches (2-bit).
174
175        See docstring of getSwitchStatus() in smogcheck_pca9555.py.
176        """
177        return self.pca_obj.getSwitchStatus()
178
179    def TTCI_Get_LED_Status(self):
180        """Checks LED status.
181
182        See docstring of getLEDstatus() in smogcheck_pca9555.py.
183        """
184        return self.pca_obj.getLEDstatus()
185
186
187def computeTimeElapsed(end, start):
188    """Computes time difference in microseconds.
189
190    Args:
191      end: a datetime.datetime() object, end timestamp.
192      start: a datetime.datetime() object, start timestamp.
193
194    Returns:
195      usec: an integer.
196    """
197    t = end - start
198    usec = 1000000 * t.seconds + t.microseconds
199    logging.info('Elapsed time = %d usec', usec)
200    return usec
201