From 8dfc6c1a4de13b5f7f1a3f86e30269d9e569d526 Mon Sep 17 00:00:00 2001
From: Juliana Motira <jmotira@lenovo.com>
Date: Thu, 3 Sep 2015 08:49:49 -0300
Subject: [PATCH] Add custom OEM PSU Based Power Capping config for Lenovo
 ThinkServers

Change-Id: I0ee2d42d3c066befe7abbf958df3952c1b43e0a6
---
 pyghmi/ipmi/command.py            | 16 ++++++++++++++++
 pyghmi/ipmi/oem/generic.py        | 14 ++++++++++++++
 pyghmi/ipmi/oem/lenovo/handler.py | 27 +++++++++++++++++++++++++++
 3 files changed, 57 insertions(+)

diff --git a/pyghmi/ipmi/command.py b/pyghmi/ipmi/command.py
index a2e217da..b16176ac 100644
--- a/pyghmi/ipmi/command.py
+++ b/pyghmi/ipmi/command.py
@@ -1548,3 +1548,19 @@ class Command(object):
         """
         self.oem_init()
         return self._oem.get_oem_firmware()
+
+    def get_capping_enabled(self):
+        """Get PSU based power capping status
+
+        :return: True if enabled and False if disabled
+        """
+        self.oem_init()
+        return self._oem.get_oem_capping_enabled()
+
+    def set_capping_enabled(self, enable):
+        """Set PSU based power capping
+
+        :param enable: True for enable and False for disable
+        """
+        self.oem_init()
+        return self._oem.set_oem_capping_enabled(enable)
diff --git a/pyghmi/ipmi/oem/generic.py b/pyghmi/ipmi/oem/generic.py
index 86fbc9d3..56e5082e 100644
--- a/pyghmi/ipmi/oem/generic.py
+++ b/pyghmi/ipmi/oem/generic.py
@@ -127,3 +127,17 @@ class OEMHandler(object):
         """Get Firmware information.
         """
         return ()
+
+    def get_oem_capping_enabled(self):
+        """Get PSU based power capping status
+
+        :return: True if enabled and False if disabled
+        """
+        return ()
+
+    def set_oem_capping_enabled(self, enable):
+        """Set PSU based power capping
+
+        :param enable: True for enable and False for disable
+        """
+        return ()
diff --git a/pyghmi/ipmi/oem/lenovo/handler.py b/pyghmi/ipmi/oem/lenovo/handler.py
index d48b3c1b..2b06c389 100755
--- a/pyghmi/ipmi/oem/lenovo/handler.py
+++ b/pyghmi/ipmi/oem/lenovo/handler.py
@@ -292,3 +292,30 @@ class OEMHandler(generic.OEMHandler):
             command = firmware.get_categories()["firmware"]
             rsp = self.ipmicmd.xraw_command(**command["command"])
             return command["parser"](rsp["data"])
+
+    def get_oem_capping_enabled(self):
+        if self.has_tsm:
+            rsp = self.ipmicmd.xraw_command(netfn=0x3a, command=0x1b,
+                                            data=(3,))
+            # disabled
+            if rsp['data'][0] == '\x00':
+                return False
+            # enabled
+            else:
+                return True
+
+    def set_oem_capping_enabled(self, enable):
+        """Set PSU based power capping
+
+        :param enable: True for enable and False for disable
+        """
+        # 1 – Enable power capping(default)
+        if enable:
+            statecode = 1
+        # 0 – Disable power capping
+        else:
+            statecode = 0
+        if self.has_tsm:
+            self.ipmicmd.xraw_command(netfn=0x3a, command=0x1a,
+                                      data=(3, statecode))
+            return True