From e891a8bd24d51f9dc2bae1406c7bb2e601d1b9f3 Mon Sep 17 00:00:00 2001 From: Jonathan Cave Date: Fri, 24 Jan 2020 16:26:23 +0000 Subject: i2c: rename test script and use argparse --- bin/i2c_driver_test | 107 ------------------------------------------------- bin/i2c_driver_test.py | 100 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 107 deletions(-) delete mode 100755 bin/i2c_driver_test create mode 100755 bin/i2c_driver_test.py (limited to 'bin') diff --git a/bin/i2c_driver_test b/bin/i2c_driver_test deleted file mode 100755 index 4af3bf8..0000000 --- a/bin/i2c_driver_test +++ /dev/null @@ -1,107 +0,0 @@ -#!/usr/bin/env python3 -# Copyright 2016 Canonical Ltd. -# All rights reserved. -# -# Written by: -# Authors: Gavin Lin -# Sylvain Pineau - -""" -This script will check number of detected I2C buses or devices - -To see how to use, please run "./i2c_driver_test" -""" - -import os -import subprocess - -from guacamole import Command - - -class Bus(Command): - - """Detect I2C bus.""" - - def invoked(self, ctx): - """Method called when the command is invoked.""" - # Detect I2C buses and calculate number of them - result = subprocess.check_output(['i2cdetect', '-l'], - universal_newlines=True) - print(result) - bus_number = len(result.splitlines()) - print('Detected bus number: {}'.format(bus_number)) - - # Test failed if no I2C bus detected - if bus_number == 0: - raise SystemExit('Test failed, no bus detected.') - - # Verify if detected number of buses is as expected - else: - if ctx.args.bus != 0: - if bus_number == ctx.args.bus: - print('Test passed') - else: - raise SystemExit('Test failed, expecting {} I2C ' - 'buses.'.format(ctx.args.bus)) - - def register_arguments(self, parser): - """Register command line arguments for the bus sub-command.""" - parser.add_argument( - '-b', '--bus', type=int, help='Expected number of I2C bus.', - default=0) - - -class Device(Command): - - """Detect I2C device.""" - - def invoked(self, ctx): - # Make sure that we have root privileges - if os.geteuid() != 0: - raise SystemExit('Error: please run this command as root') - # Calculate number of buses - result = subprocess.check_output(['i2cdetect', '-l'], - universal_newlines=True) - detected_i2c_bus = [] - for line in result.splitlines(): - detected_i2c_bus.append(line.split('\t')[0].split('-')[1]) - print('Detected buses: {}'.format(detected_i2c_bus)) - - # Detect device on each bus - exit_code = 1 - for i in detected_i2c_bus: - print('Checking I2C bus {}'.format(i)) - result = subprocess.check_output(['i2cdetect', '-y', '-r', str(i)], - universal_newlines=True) - print(result) - result_line = result.splitlines()[1:] - for l in result_line: - address_value = l.strip('\n').split(':')[1].split() - for v in address_value: - if v != '--': exit_code = 0 - if exit_code == 1: - raise SystemExit('No I2C device detected on any I2C bus') - else: - print('I2C device detected') - return exit_code - - -class I2cDriverTest(Command): - - """I2C driver test.""" - - sub_commands = ( - ('bus', Bus), - ('device', Device) - ) - - def invoked(self, ctx): - """Method called when the command is invoked.""" - if not ctx.early_args.rest: - ctx.parser.print_help() - return 1 - - -if __name__ == '__main__': - I2cDriverTest().main() - diff --git a/bin/i2c_driver_test.py b/bin/i2c_driver_test.py new file mode 100755 index 0000000..c6aae78 --- /dev/null +++ b/bin/i2c_driver_test.py @@ -0,0 +1,100 @@ +#!/usr/bin/env python3 +# Copyright 2016-2020 Canonical Ltd. +# All rights reserved. +# +# Written by: +# Authors: Gavin Lin +# Sylvain Pineau +# Jonathan Cave + +""" +This script will check number of detected I2C buses or devices + +To see how to use, please run "./i2c_driver_test.py" +""" + +import argparse +import os +import subprocess + + +class Bus(): + + """Detect I2C bus.""" + + def invoked(self, args): + """Method called when the command is invoked.""" + # Detect I2C buses and calculate number of them + result = subprocess.check_output(['i2cdetect', '-l'], + universal_newlines=True) + print(result) + bus_number = len(result.splitlines()) + print('Detected bus number: {}'.format(bus_number)) + + # Test failed if no I2C bus detected + if bus_number == 0: + raise SystemExit('Test failed, no bus detected.') + + # Verify if detected number of buses is as expected + else: + if args.bus != 0: + if bus_number == args.bus: + print('Test passed') + else: + raise SystemExit('Test failed, expecting {} I2C ' + 'buses.'.format(args.bus)) + + +class Device(): + + """Detect I2C device.""" + + def invoked(self, args): + # Make sure that we have root privileges + if os.geteuid() != 0: + raise SystemExit('Error: please run this command as root') + # Calculate number of buses + result = subprocess.check_output(['i2cdetect', '-l'], + universal_newlines=True) + detected_i2c_bus = [] + for line in result.splitlines(): + detected_i2c_bus.append(line.split('\t')[0].split('-')[1]) + print('Detected buses: {}'.format(detected_i2c_bus)) + + # Detect device on each bus + exit_code = 1 + for i in detected_i2c_bus: + print('Checking I2C bus {}'.format(i)) + result = subprocess.check_output(['i2cdetect', '-y', '-r', str(i)], + universal_newlines=True) + print(result) + result_line = result.splitlines()[1:] + for l in result_line: + address_value = l.strip('\n').split(':')[1].split() + for v in address_value: + if v != '--': + exit_code = 0 + if exit_code == 1: + raise SystemExit('No I2C device detected on any I2C bus') + print('I2C device detected') + + +class I2cDriverTest(): + + """I2C driver test.""" + + def main(self): + subcommands = { + 'bus': Bus, + 'device': Device + } + parser = argparse.ArgumentParser() + parser.add_argument('subcommand', type=str, choices=subcommands) + parser.add_argument('-b', '--bus', type=int, default=0, + help='Expected number of I2C bus.') + args = parser.parse_args() + subcommands[args.subcommand]().invoked(args) + + +if __name__ == '__main__': + I2cDriverTest().main() -- cgit v1.2.3