#!/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 RMM data

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

import sys, os, os.path, popen2
from systemMgr import openSysInfo, closeSysInfo, addProperty, queryProperty
from logMgr import addLogRecord,CRITICAL,ERROR,WARNING,INFO,DEBUG
from config import getstatusoutput, DeployPath
try:
    __file__
except:
    __file__ = __name__

def runkiratool(args):
    if os.environ.has_key('RMMuser'):
        user = os.environ['RMMuser']
    else:
        user = 'admin'
    if os.environ.has_key('RMMpassword'):
        password = os.environ['RMMpassword']
    else:
        password = 'password'

    cmd = "kiratool -u %s -p %s %s"%(user,password,args)

    status = 0
    output = ''
    if sys.platform == 'win32':
        kiradir = os.path.join(DeployPath,'win32','kiratool')
        if not os.path.exists(kiradir):
            return 1, ""
        cwd = os.getcwd()
        os.chdir(kiradir)
        status, output = getstatusoutput(cmd)
        os.chdir(cwd)
    
    elif sys.platform == 'linux2':
        kiratool = os.path.join(DeployPath,'Linux','kiratool')
        if not os.path.exists(kiratool):
            return 1, ""
        status, output = getstatusoutput(cmd)
    return status, output

kiratoolargs = ['fw','info','mac','ipsrc','ip','netmask','gw']

def readRMMdata():
    sInfo = openSysInfo()
    ret, family = queryProperty(sInfo,'IntelBIOSID','BoardFamilyID','',{})
    if family == 'S3000':
        return
    ret, lines = runkiratool('fw')
    if ret:
        return
    for arg in kiratoolargs:
        ret, lines = runkiratool(arg)
        if ret:
            addLogRecord(sInfo,__file__, ERROR,'%s command failed: status = 0x%x, %s'%(cmd,status,lines))
            break
        for line in lines:
            if line.find(":") < 0:
                continue
            parts = line.split(":",1)
            if len(parts) < 2:
                continue
            addProperty(sInfo,'RMM',parts[0].strip(),parts[1].strip(),{})
    closeSysInfo(sInfo)
    return ret

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

