#!/usr/local/bin/python

import re
import sys
import isi.app.lib.procs as procs
import isi.hw.bootstrap as bootstrap

from isi.hw.hwmon.utility import run_ipmicmc
from isi.hw.fw.modules.epost import get_infinity_type

# I2C Access commands NetFn: 0x30 Cmd: 0xB0
# The command list is the data to pass into the command for DPOL6 and DPOL10
# DPOL6 (Page 14/0xe)
# DPOL10 (Page 18/0x12)
CMD_LIST = [
    ("DPOL6 Vout", [0x0, 0x1, 0x0, 0xe, 0xff, 0x2, 0x21], 0x9e, [0x0, 0x9e]),
    ("DPOL6 OV", [0x0, 0x1, 0x0, 0xe, 0xff, 0x2, 0xec], 0xa7 , [0x0, 0xa7]),
    ("DPOL6 UV", [0x0, 0x1, 0x0, 0xe, 0xff, 0x2, 0xed], 0x95, [0x0, 0x95]),
    ("DPOL10 Vout", [0x0, 0x1, 0x0, 0x12, 0xff, 0x2, 0x21], 0xa0, [0x0, 0xa0]),
    ("DPOL10 OV", [0x0, 0x1, 0x0, 0x12, 0xff, 0x2, 0xec], 0xaa , [0x0, 0xaa]),
    ("DPOL10 UV", [0x0, 0x1, 0x0, 0x12, 0xff, 0x2, 0xed], 0x96, [0x0, 0x96]),
]

def main():
    result = 0
    if not bootstrap.isInfinityMotherboard():
        print "This script is not supported on this platform"
        return 0
    node_type = get_infinity_type()
    if node_type is None:
        print "Unable to get node type"
        return 1
    elif node_type == "DE":
        print "This script is not supported on this Gen6 node type"
        return 0
    for cmd in CMD_LIST:
        (name, read, check, write) = cmd
        output = run_ipmicmc(0x30, 0xB0, read)
        if not output:
            print "ERROR: Unable to read %s" % name
            result = 1
            break
        if output[-1] != check:
            data = read + write
            output = run_ipmicmc(0x30, 0xB0, data)
            if not output:
                print "ERROR: Unable to run: %s"  % cmd
                result = 1
                break
            output = run_ipmicmc(0x30, 0xB0, read)
            if not output:
                print "ERROR: Unable to validate %s" % name
                result = 1
                break
            if output[-1] != check:
                print "ERROR: Unable to write value for %s" % name
                result = 1
                break
    print ""
    if result:
        print "FAILED"
        return 1
    else:
        data = [0x0, 0x1, 0x0, 0xff, 0xff, 0x0, 0x15, 0x55]
        output = run_ipmicmc(0x30, 0xB0, data)
        if not output:
            print "ERROR: Failed to push configuration to CMD config table"
        else:
            print "SUCCESS"

if __name__ == "__main__":
    sys.exit(main())
