#!/usr/bin/python
# -*- coding: latin-1 -*-
# INTEL CONFIDENTIAL
# Copyright 2007 Intel Corporation All Rights Reserved.
#
# The source code contained or described herein and all documents related to the
# source code (“Material”) are owned by Intel Corporation or its suppliers or
# licensors. Title to the Material remains with Intel Corporation or its
# suppliers and licensors. The Material may contain trade secrets and
# proprietary and confidential information of Intel Corporation and its
# suppliers and licensors, and is protected by worldwide copyright and trade
# secret laws and treaty provisions. No part of the Material may be used,
# copied, reproduced, modified, published, uploaded, posted, transmitted,
# distributed, or disclosed in any way without Intel’s prior express written
# permission. 
# No license under any patent, copyright, trade secret or other intellectual
# property right is granted to or conferred upon you by disclosure or delivery
# of the Materials, either expressly, by implication, inducement, estoppel or
# otherwise. Any license under such intellectual property rights must be express
# and approved by Intel in writing.
#
# Include any supplier copyright notices as supplier requires Intel to use.
#
# Include supplier trademarks or logos as supplier requires Intel to use, preceded
# by an asterisk. An asterisked footnote can be added as follows:
# *Third Party trademarks are the property of their respective owners.
#
# Unless otherwise agreed by Intel in writing, you may not remove or alter this
# notice or any other notice embedded in Materials by Intel or Intel’s suppliers
# or licensors in any way.

"""This module contains the methods to read FRU data

"""
__author__ = 'Todd C Davis'
__Product__ = 'Intel_PDK'

import sys, os, os.path, popen2
from systemMgr import openSysInfo, closeSysInfo, addProperty

handledRecords= ['Front Panel FRU','Power Dist FRU','Pwr Supply 1 FRU','Pwr Supply 2 FRU','Mainboard FRU']

def handleRecord(sInfo, t,i, record):
    if not i in handledRecords:
        return
    attr = {}
    #attr['type'] = t
    attr["info"] = i
    #print "appending records for %s %s" % (t,i)
    for line in record:
        parts = line.split(':')
        if len(parts) != 2:
            continue
        if not parts[0].find(' FRU Size') < 0:
            continue
        if not parts[0].find('fruconfig') < 0:
            continue
        n = parts[0].strip()
        v = parts[1].strip()
        if n and v:
            #print attr,
            #print "%s : %s" % (n,v)
            addProperty(sInfo,'FRU',n,v,attr)

def readFRUrecords():
    if sys.platform == 'win32':
        binfile = 'ipmiutil.exe fru'
    elif sys.platform == 'linux2':
        binfile = 'ipmiutil fru'
    r, w, e = popen2.popen3("%s"%binfile,-1,'b')
    #elines = e.readlines()
    e.close()
    lines = r.readlines()
    r.close()
    w.close()
    #print "lines: %d" % len(lines)
    sInfo = openSysInfo()
    record = []
    t= ''
    for line in lines:
        if line.startswith("SDR"):
            if t:
                #print "t = %s ; i = %s" % (t,i)
                handleRecord(sInfo,t,i,record)
                record = []
            if line.find('error') > 0 and line.find('ccode') > 0:
                continue
            parts = line.split()
            assert len(parts) >= 6
            #assert parts[-1] == 'FRU'
            t = parts[0][4:8]
            if len(parts) > 6:
                i = ' '.join(parts[6:])
            else:
                i = ' unknown'
        elif line.startswith('Mainboard FRU Size  :'):
            if t:
                #print "t = %s ; i = %s" % (t,i)
                handleRecord(sInfo,t,i,record)
                record = []
            t = '9999'
            i = 'Mainboard FRU'
        elif t:
            l = line.strip()
            if l:
                record.append(l)
    if t:
        #print "t = %s ; i = %s" % (t,i)
        handleRecord(sInfo,t,i,record)
    if sys.platform == 'win32':
        binfile = 'ipmiutil.exe sensor'
    elif sys.platform == 'linux2':
        binfile = 'ipmiutil sensor'
    r, w, e = popen2.popen3("%s"%binfile,-1,'b')
    #elines = e.readlines()
    e.close()
    lines = r.readlines()
    r.close()
    w.close()
    #print "lines: %d" % len(lines)
    for line in lines:
        if line.startswith('-- BMC'):
            parts = line.split()
            if len(parts) < 7:
                continue
            addProperty(sInfo,'SDR',parts[1]+' '+parts[2],parts[3][:-1],{})
            addProperty(sInfo,'SDR',parts[4]+' '+parts[5],parts[6],{})
            continue
        if line.find("Intel: SDR File") >= 0:
            addProperty(sInfo,'SDR',"File Version",line.split()[-1].strip(),{})
        if line.find("Intel: SDR Package") >= 0:
            addProperty(sInfo,'SDR',"Package Version",line.split()[-1].strip(),{})
    closeSysInfo(sInfo)
    return 0

if __name__ == '__main__':
    sys.exit(readFRUrecords())

