pytools: Remove dependency to obmc.system

The system manager (obmc.system) is being deprecated and
the GPIOs has moved out of D-Bus (see openbmc/openbmc#3332).
The pytools still depend on the convertGpio function provided
by system manager, so copy that function into the tools that
use it to remove the dependency.

Tested: obmcutil power functions still work, and gpioutil
-l and -p work the same.

Change-Id: Ia09f95312438040908eb15f93ff6d525665f5ab3
Signed-off-by: Adriana Kobylak <anoo@us.ibm.com>
diff --git a/pytools/gpioutil b/pytools/gpioutil
index 6589060..d06a9a4 100644
--- a/pytools/gpioutil
+++ b/pytools/gpioutil
@@ -3,8 +3,9 @@
 import sys
 import os
 import getopt
+from glob import glob
+from os.path import join
 import obmc_system_config as System
-import obmc.system
 
 
 def printUsage():
@@ -25,6 +26,32 @@
 
 GPIO_SYSFS = '/sys/class/gpio/'
 
+
+def find_gpio_base(path="/sys/class/gpio/"):
+	pattern = "gpiochip*"
+	for gc in glob(join(path, pattern)):
+		with open(join(gc, "label")) as f:
+			label = f.readline().strip()
+		if label == "1e780000.gpio":
+			with open(join(gc, "base")) as f:
+				return int(f.readline().strip())
+	# trigger a file not found exception
+	open(join(path, "gpiochip"))
+
+
+GPIO_BASE = find_gpio_base()
+
+
+def convertGpio(name):
+	offset = int(''.join(list(filter(str.isdigit, name))))
+	port = list(filter(str.isalpha, name.upper()))
+	a = ord(port[-1]) - ord('A')
+	if len(port) > 1:
+		a += 26
+	base = a * 8 + GPIO_BASE
+	return base + offset
+
+
 class Gpio:
 	def __init__(self,gpio_num):
 		self.gpio_num = str(gpio_num)
@@ -116,9 +143,9 @@
  		elif opt in ("-v"):
 			value = arg
 		elif opt in ("-p"):
-			gpio_name = obmc.system.convertGpio(arg)
+			gpio_name = convertGpio(arg)
 		elif opt in ("-l"):
-			gpio_name = obmc.system.convertGpio(arg)
+			gpio_name = convertGpio(arg)
 			print gpio_name
 			exit(0) 
 
@@ -135,7 +162,7 @@
 		if (gpio_info.has_key('gpio_num')):
 			gpio_name = str(gpio_info['gpio_num'])
 		else:
-			gpio_name = str(obmc.system.convertGpio(gpio_info['gpio_pin']))
+			gpio_name = str(convertGpio(gpio_info['gpio_pin']))
 		print "GPIO ID: "+gpio_name+"; DIRECTION: "+direction
 
 
diff --git a/pytools/obmcutil b/pytools/obmcutil
index 0a0d8d1..0aa40a9 100644
--- a/pytools/obmcutil
+++ b/pytools/obmcutil
@@ -10,10 +10,11 @@
 import os
 import signal
 import time
+from glob import glob
+from os.path import join
 from subprocess import Popen
 
 import obmc_system_config
-import obmc.system
 
 descriptors = {
     'power': {
@@ -83,6 +84,32 @@
 
 GPIO_DEFS_FILE = '/etc/default/obmc/gpio/gpio_defs.json'
 
+
+def find_gpio_base(path="/sys/class/gpio/"):
+    pattern = "gpiochip*"
+    for gc in glob(join(path, pattern)):
+        with open(join(gc, "label")) as f:
+            label = f.readline().strip()
+        if label == "1e780000.gpio":
+            with open(join(gc, "base")) as f:
+                return int(f.readline().strip())
+    # trigger a file not found exception
+    open(join(path, "gpiochip"))
+
+
+GPIO_BASE = find_gpio_base()
+
+
+def convertGpio(name):
+    offset = int(''.join(list(filter(str.isdigit, name))))
+    port = list(filter(str.isalpha, name.upper()))
+    a = ord(port[-1]) - ord('A')
+    if len(port) > 1:
+        a += 26
+    base = a * 8 + GPIO_BASE
+    return base + offset
+
+
 def run_set_property(dbus_bus, dbus_iface, descriptor, args):
     mainloop = gobject.MainLoop()
 
@@ -186,7 +213,7 @@
     return True
 
 def gpio_set_value(gpio_name, active_low, asserted):
-    gpio_id = obmc.system.convertGpio(gpio_name)
+    gpio_id = convertGpio(gpio_name)
     gpio_value_path = "/sys/class/gpio/gpio{}/value".format(gpio_id)
 
     with open(gpio_value_path, 'w') as gpio: