diff options
author | Brad Bishop <bradleyb@fuzziesquirrel.com> | 2016-05-28 18:41:04 -0400 |
---|---|---|
committer | Brad Bishop <bradleyb@fuzziesquirrel.com> | 2016-06-10 18:06:59 -0400 |
commit | 40a360c2a4feef97a8f7041e655b2a42e51e0224 (patch) | |
tree | 75dfea3064d7c3243788c72cb9f30e2ce6241dea /pytools | |
parent | a73122191a7aba80f97332687a2e03cfb0336981 (diff) | |
download | blackbird-skeleton-40a360c2a4feef97a8f7041e655b2a42e51e0224.tar.gz blackbird-skeleton-40a360c2a4feef97a8f7041e655b2a42e51e0224.zip |
Reorganize directory structure
Moving to directory per-application layout. This facilitates
building single applications which is useful in the Yocto build
environment since different applications satisfy different OpenBMC
build requirements.
A number of issues are also addressed:
- All applications were pulling in libsystemd and the gdbus libs
irrespective of whether or not they were needed.
- gpio.o duplicated in every application - moved to libopenbmc_intf
- Added install target
Signed-off-by: Brad Bishop <bradleyb@fuzziesquirrel.com>
Diffstat (limited to 'pytools')
l--------- | pytools/Makefile | 1 | ||||
-rw-r--r-- | pytools/gpioutil | 162 | ||||
-rw-r--r-- | pytools/obmcutil | 205 | ||||
l--------- | pytools/setup.cfg | 1 | ||||
-rw-r--r-- | pytools/setup.py | 6 |
5 files changed, 375 insertions, 0 deletions
diff --git a/pytools/Makefile b/pytools/Makefile new file mode 120000 index 0000000..76a90fc --- /dev/null +++ b/pytools/Makefile @@ -0,0 +1 @@ +../Makefile.python
\ No newline at end of file diff --git a/pytools/gpioutil b/pytools/gpioutil new file mode 100644 index 0000000..ab44b4d --- /dev/null +++ b/pytools/gpioutil @@ -0,0 +1,162 @@ +#!/usr/bin/env python + +import sys +import os +import getopt + + +def printUsage(): + print '\nUsage:' + print 'gpioutil SYSTEM_NAME -n GPIO_NAME [-v value]' + print 'gpioutil SYSTEM_NAME -i GPIO_NUM -d <DIRECTION = in,out,falling,rising,both> [-v value]' + print 'gpioutil SYSTEM_NAME -p PIN_NAME -d <DIRECTION = in,out,falling,rising,both> [-v value]' + print 'gpioutil SYSTEM_NAME -l PIN_NAME (lookup pin name only)' + exit(1) + + + +if (len(sys.argv) < 3): + printUsage() + +sys.argv.pop(0) +libr = sys.argv.pop(0) +System = __import__(libr) + +GPIO_SYSFS = '/sys/class/gpio/' + +class Gpio: + def __init__(self,gpio_num): + self.gpio_num = str(gpio_num) + self.direction = '' + self.interrupt = '' + self.exported = False + + def getPath(self,name): + return GPIO_SYSFS+'gpio'+self.gpio_num+'/'+name + + def export(self): + if (os.path.exists(GPIO_SYSFS+'export') == False): + raise Exception("ERROR - GPIO_SYSFS path does not exist. Does this platform support GPIOS?") + if (os.path.exists(GPIO_SYSFS+'gpio'+self.gpio_num) == False): + self.write(GPIO_SYSFS+'export',self.gpio_num) + + self.exported = True + + def setDirection(self,dir): + if (self.exported == False): + raise Exception("ERROR - Not exported: "+self.getPath()) + + self.direction = '' + self.interrupt = '' + if (dir == 'in' or dir == 'out'): + self.direction = dir + elif (dir == 'rising' or + dir == 'falling' or + dir == 'both'): + self.direction = 'in' + self.interrupt = dir + self.write(self.getPath('edge'),self.interrupt) + else: + raise Exception("ERROR - Invalid Direction: "+dir) + + + self.write(self.getPath('direction'),self.direction) + + def setValue(self,value): + if (value == '0'): + self.write(self.getPath('value'),'0') + elif (value == '1'): + self.write(self.getPath('value'),'1') + else: + raise Exception("ERROR - Invalid value: "+value) + + def getValue(self): + return self.read(self.getPath('value')) + + def write(self,path,data): + f = open(path,'w') + f.write(data) + f.close() + + + def read(self,path): + f = open(path,'r') + data = f.readline() + f.close() + return data + + + +if __name__ == '__main__': + + try: + opts, args = getopt.getopt(sys.argv,"hn:i:d:v:p:l:") + except getopt.GetoptError: + printUsage() + + + + lookup = False + gpio_name = "" + value = "" + direction = "" + for opt, arg in opts: + if opt == '-h': + printUsage() + + elif opt in ("-n"): + gpio_name = arg + lookup = True + elif opt in ("-i"): + gpio_name = arg + elif opt in ("-d"): + direction = arg + elif opt in ("-v"): + value = arg + elif opt in ("-p"): + gpio_name = System.convertGpio(arg) + elif opt in ("-l"): + gpio_name = System.convertGpio(arg) + print gpio_name + exit(0) + + gpio_info = {} + if (lookup == True): + if (System.GPIO_CONFIG.has_key(gpio_name) == False): + print "ERROR - GPIO Name doesn't exist" + print "Valid names: " + for n in System.GPIO_CONFIG: + print "\t"+n + exit(0) + gpio_info = System.GPIO_CONFIG[gpio_name] + direction = gpio_info['direction'] + if (gpio_info.has_key('gpio_num')): + gpio_name = str(gpio_info['gpio_num']) + else: + gpio_name = str(System.convertGpio(gpio_info['gpio_pin'])) + print "GPIO ID: "+gpio_name+"; DIRECTION: "+direction + + + ## Rules + if (gpio_name == ""): + print "ERROR - Gpio not specified" + printUsage() + + if (direction == "in" and value != ""): + print "ERROR - Value cannot be specified when direction = in" + printUsage() + + gpio = Gpio(gpio_name) + try: + gpio.export() + if (direction != ""): + gpio.setDirection(direction) + + if (value == ""): + print gpio.getValue() + else: + gpio.setValue(value) + + except Exception as e: + print e + diff --git a/pytools/obmcutil b/pytools/obmcutil new file mode 100644 index 0000000..bd753ca --- /dev/null +++ b/pytools/obmcutil @@ -0,0 +1,205 @@ +#!/usr/bin/python + +import sys +#import subprocess +import gobject +import dbus +import dbus.service +import dbus.mainloop.glib +import xml.etree.ElementTree as ET +import json + + +def fix_byte(it,key,parent): + if (isinstance(it,dbus.Array)): + for i in range(0,len(it)): + fix_byte(it[i],i,it) + elif (isinstance(it, dict)): + for key in it.keys(): + fix_byte(it[key],key,it) + elif (isinstance(it,dbus.Byte)): + if (key != None): + parent[key] = int(it) + else: + pass + + +def printDict(name,data): + if (isinstance(data, dict)): + print ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + print name + for p in sorted(data.keys()): + printDict(p,data[p]) + else: + print name+" = "+str(data) + +def introspect(bus_name,obj_path,intf_name,method_name): + obj = bus.get_object(bus_name,obj_path) + introspect_iface = dbus.Interface(obj,"org.freedesktop.DBus.Introspectable") + tree = ET.ElementTree(ET.fromstring(introspect_iface.Introspect())) + #print method_name + #print introspect_iface.Introspect() + root = tree.getroot() + found = False + for node in root.iter('node'): + for intf in node.iter('interface'): + if (intf.attrib['name'] == intf_name): + for method in intf.iter('method'): + if (method.attrib['name'] == method_name): + for ar in method.iter('arg'): + if (ar.attrib['direction'] == "in"): + print "\t"+ar.attrib['name']+" ("+ar.attrib['type']+")" + found = True + + return found + + +dbus_objects = { + 'power' : { + 'bus_name' : 'org.openbmc.control.Power', + 'object_name' : '/org/openbmc/control/power0', + 'interface_name' : 'org.openbmc.control.Power' + }, + 'identify_led' : { + 'bus_name' : 'org.openbmc.control.led', + 'object_name' : '/org/openbmc/control/led/identify', + 'interface_name' : 'org.openbmc.Led' + }, + 'chassis' : { + 'bus_name' : 'org.openbmc.control.Chassis', + 'object_name' : '/org/openbmc/control/chassis0', + 'interface_name' : 'org.openbmc.control.Chassis' + }, + 'poweron' : { + 'bus_name' : 'org.openbmc.control.Chassis', + 'object_name' : '/org/openbmc/control/chassis0', + 'interface_name' : 'org.openbmc.control.Chassis', + 'method' : 'powerOn', + }, + 'poweroff' : { + 'bus_name' : 'org.openbmc.control.Chassis', + 'object_name' : '/org/openbmc/control/chassis0', + 'interface_name' : 'org.openbmc.control.Chassis', + 'method' : 'powerOff', + }, + 'state' : { + 'bus_name' : 'org.openbmc.managers.System', + 'object_name' : '/org/openbmc/managers/System', + 'interface_name' : 'org.openbmc.managers.System', + 'method' : 'getSystemState', + }, + 'bootprogress' : { + 'bus_name' : 'org.openbmc.Sensors', + 'object_name' : '/org/openbmc/sensors/host/BootProgress', + 'interface_name' : 'org.openbmc.SensorValue' + }, + 'biosupdate' : { + 'bus_name' : 'org.openbmc.control.Flash', + 'object_name' : '/org/openbmc/control/flash/bios', + 'interface_name' : 'org.openbmc.Flash', + 'method' : 'updateViaTftp', + }, + 'biosflash' : { + 'bus_name' : 'org.openbmc.control.Flash', + 'object_name' : '/org/openbmc/control/flash/bios', + 'interface_name' : 'org.openbmc.Flash', + }, + 'bmcupdate' : { + 'bus_name' : 'org.openbmc.control.BmcFlash', + 'object_name' : '/org/openbmc/control/flash/bmc', + 'interface_name' : 'org.openbmc.control.BmcFlash', + 'method' : 'updateViaTftp', + }, + 'bmcflash' : { + 'bus_name' : 'org.openbmc.control.BmcFlash', + 'object_name' : '/org/openbmc/control/flash/bmc', + 'interface_name' : 'org.openbmc.control.BmcFlash', + }, + 'getinventory' : { + 'bus_name' : 'org.openbmc.Inventory', + 'object_name' : '/org/openbmc/inventory', + 'interface_name' : 'org.openbmc.Object.Enumerate', + 'method' : 'enumerate' + }, + 'getsensors' : { + 'bus_name' : 'org.openbmc.Sensors', + 'object_name' : '/org/openbmc/sensors', + 'interface_name' : 'org.openbmc.Object.Enumerate', + 'method' : 'enumerate' + }, + 'host' : { + 'bus_name' : 'org.openbmc.control.Host', + 'object_name' : '/org/openbmc/control/host0', + 'interface_name' : 'org.openbmc.control.Host', + }, + 'setdebugmode' : { + 'bus_name' : 'org.openbmc.control.Host', + 'object_name' : '/org/openbmc/control/host0', + 'interface_name' : 'org.openbmc.control.Host', + 'property' : 'debug_mode' + }, +} + +bus = dbus.SystemBus() + + +if (len(sys.argv) == 1 or sys.argv[1] == "-h" or dbus_objects.has_key(sys.argv[1])==False): + print "Usage: obmcutil [command] [[method] [*args]]" + print "\tIf [method] is blank, then all properties are printed\n" + print "Available commands:" + for name in sorted(dbus_objects.keys()): + m = "" + if (dbus_objects[name].has_key('method') == True): + m=" ("+dbus_objects[name]['interface_name']+"->"+dbus_objects[name]['method']+")" + elif (dbus_objects[name].has_key('property') == True): + m=" ("+dbus_objects[name]['interface_name']+"->"+dbus_objects[name]['property']+")" + + print "\t"+name+m + exit(0) + +method_name = "" +property_name = "" + +sys.argv.pop(0) +objinfo = dbus_objects[sys.argv.pop(0)] + +if (objinfo.has_key('method')): + method_name = objinfo['method'] +elif (objinfo.has_key('property')): + property_name = objinfo['property'] +elif (len(sys.argv)>0): + ## if command line args left and method not specified + ## then next arg must be method name + method_name = sys.argv.pop(0) + +bus_name = objinfo['bus_name'] +obj_path = objinfo['object_name'] +intf_name = objinfo['interface_name'] +obj = bus.get_object(bus_name,obj_path) + +if (method_name != ""): + methd = obj.get_dbus_method(method_name,intf_name) + try: + data = methd(*sys.argv) + fix_byte(data,None,None) + pydata = json.loads(json.dumps(data)) + printDict("",pydata) + except Exception as e: + print e + r = introspect(bus_name,obj_path,intf_name,method_name) + if (r == False): + print "ERROR: Invalid method: "+method_name + else: + print "ERROR: Incorrect arguments passed to method" +elif (property_name != ""): + intf = dbus.Interface(obj,"org.freedesktop.DBus.Properties") + property_value = eval(sys.argv.pop(0)) + intf.Set(intf_name,property_name,property_value) +else: + intf = dbus.Interface(obj,"org.freedesktop.DBus.Properties") + props = intf.GetAll(intf_name) + for p in props: + print p+" = "+str(props[p]) + + + diff --git a/pytools/setup.cfg b/pytools/setup.cfg new file mode 120000 index 0000000..29939b5 --- /dev/null +++ b/pytools/setup.cfg @@ -0,0 +1 @@ +../setup.cfg
\ No newline at end of file diff --git a/pytools/setup.py b/pytools/setup.py new file mode 100644 index 0000000..2736366 --- /dev/null +++ b/pytools/setup.py @@ -0,0 +1,6 @@ +from distutils.core import setup + +setup(name='pytools', + version='1.0', + scripts=['obmcutil', 'gpioutil'], + ) |