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
5import logging, os, glob
6from autotest_lib.client.bin import test, utils
7from autotest_lib.client.common_lib import error
8
9LIGHT_SENSOR_LOCATION = "/sys/bus/iio/devices/*/"
10# Match sensors files from
11# src/platform2/power_manager/powerd/system/ambient_light_sensor.cc
12LIGHT_SENSOR_FILES = [ "in_illuminance0_input",
13                       "in_illuminance_input",
14                       "in_illuminance0_raw",
15                       "in_illuminance_raw",
16                       "illuminance0_input",
17                     ]
18
19class hardware_LightSensor(test.test):
20    """
21    Test the system's Light Sensor device.
22    Failure to find the device likely indicates the kernel module is not loaded.
23    Or it could mean the I2C probe for the device failed because of an incorrect
24    I2C address or bus specification.
25    The ebuild scripts should properly load the udev rules for light sensor so
26    we can find its files in LIGHT_SENSOR_LOCATIONS depending
27    on the kernel version.
28    """
29    version = 1
30
31    def _waiver(self):
32        path = os.path.join(self.job.testdir, "hardware_LightSensor",
33                            "no_light_sensor_ok")
34        if os.path.exists(path):
35            return True
36        return False
37
38
39    def run_once(self):
40        if self._waiver():
41            raise error.TestNAError("Light sensor not required for this device")
42
43        found_light_sensor = 0
44        for location in glob.glob(LIGHT_SENSOR_LOCATION):
45            for fname in LIGHT_SENSOR_FILES:
46                path = location + fname
47                if os.path.exists(path):
48                    found_light_sensor = 1
49                    break
50                else:
51                    logging.info("Did not find light sensor reading at " + path)
52
53            if found_light_sensor:
54                break
55
56        if not found_light_sensor:
57            raise error.TestFail("No light sensor reading found.")
58        else:
59            logging.info("Found light sensor at " + path)
60
61        val = utils.read_one_line(path)
62        reading = int(val)
63        if reading < 0:
64            raise error.TestFail("Invalid light sensor reading (%s)" % val)
65        logging.debug("light sensor reading is %d", reading)
66