1# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5
6# DESCRIPTION :
7#
8# This is a hardware test for Probing I2C device. The test uses i2cdetect
9# utility to check if there's an device on specific bus.
10
11
12import re
13import logging
14
15from autotest_lib.client.bin import test, utils
16from autotest_lib.client.common_lib import error
17
18
19def i2c_detect(bus, addr):
20    full_cmd = 'i2cdetect -y %d 0x%x 0x%x' % (bus, addr, addr)
21    result = utils.system_output(full_cmd)
22    logging.debug('Command: %s', full_cmd)
23    logging.debug('Result: %s', result)
24    return result
25
26def i2c_probe(bus, addr):
27    response = i2c_detect(bus, addr)
28    return (re.search('^\d\d:\s+(UU|[0-9a-f]{2})', response, re.MULTILINE) is
29            not None)
30
31class hardware_I2CProbe(test.test):
32    version = 1
33
34    def run_once(self, bus, addr):
35        if not i2c_probe(bus, addr):
36            raise error.TestError('No I2C device on bus %d addr 0x%x' %
37                    (bus, addr))
38