1#!/usr/bin/python 2 3# Copyright (c) 2013 The Chromium OS Authors. All rights reserved. 4# Use of this source code is governed by a BSD-style license that can be 5# found in the LICENSE file. 6 7import pprint 8import sys 9 10import common 11from autotest_lib.client.cros.networking import shill_proxy 12 13def usage(): 14 """ Prints a script usage message. """ 15 cmd = sys.argv[0] 16 print 'Usage: %s <command> [more parameters]' % cmd 17 print 'Example uses:' 18 print cmd, 'list - List devices and their properties.' 19 print cmd, 'get-property <devname> [propname] - List device property.' 20 print cmd, 'set-property <devname> <propname> <value>' 21 print ' Set property on devname to value' 22 return False 23 24 25def set_device_property(device, property_key, value): 26 """Sets a property on a device 27 28 @param device Interface representing a device 29 @param property_key string name of property 30 @param value string value of property to set 31 32 """ 33 shill_proxy.ShillProxy.set_dbus_property(device, property_key, value) 34 return True 35 36 37def print_device_properties(device, property_key): 38 """Prints one or all properties on a device 39 40 @param device Interface representing a device 41 @param property_key string name of property or None 42 43 """ 44 shill = shill_proxy.ShillProxy() 45 if property_key is None: 46 pprint.pprint( 47 shill.dbus2primitive(device.GetProperties(utf8_strings=True)), 48 indent=2) 49 else: 50 pprint.pprint({property_key: 51 shill_proxy.ShillProxy.get_dbus_property(device, property_key)}, 52 indent=2) 53 return True 54 55 56def list_devices(): 57 """ Display detailed device information. """ 58 shill = shill_proxy.ShillProxy() 59 for device in shill.get_devices(): 60 print 'Device: %s' % device.object_path 61 print_device_properties(device, None) 62 print 63 return True 64 65 66def main(): 67 """ Main entry point for the device script. """ 68 if len(sys.argv) < 2: 69 return usage() 70 71 command = sys.argv[1] 72 73 if command == 'list': 74 return list_devices() 75 76 if len(sys.argv) > 2: 77 shill = shill_proxy.ShillProxy() 78 device = shill.find_object('Device', {'Name': sys.argv[2]}) 79 if device is None: 80 print "No device named %s found" % sys.argv[2] 81 return usage() 82 83 if command == 'get-property': 84 return print_device_properties( 85 device, 86 None if len(sys.argv) < 4 else sys.argv[3]) 87 88 if command == 'set-property' and len(sys.argv) == 5: 89 return set_device_property( 90 device, 91 sys.argv[3], 92 sys.argv[4]) 93 94 return usage() 95 96 97if __name__ == '__main__': 98 if not main(): 99 sys.exit(1) 100