
#include "Util.h"
#include <sys/sysinfo.h> 


/*----------------------------------------
 * GetBits
 *----------------------------------------*/
uint8_t
GetBits (uint8_t Val, uint8_t Mask)
{
    uint8_t ExtVal = 0;
    while (0 != Mask)
    {
        if (0 != (Mask & 0x80))
        {
            ExtVal <<= 1;
            if (0 != (Val & 0x80)) { ExtVal += 0x01; }
        }

        Val <<= 1;
        Mask <<= 1;
    }

    return ExtVal;
}

/*------------------------------------------
 * SetBits
 *------------------------------------------*/
uint8_t
SetBits (uint8_t Mask, uint8_t Val)
{
    uint8_t FinVal = 0;
    uint8_t i;

    for (i = 0; i < 8; i++)
    {
        FinVal >>= 1;
        if (0 != (Mask & 0x01))
        {
            if (0 != (Val & 0x01)) { FinVal |= 0x80; }
                Val >>= 1;
        }
        Mask   >>= 1;
    }

    return FinVal;
}

/*------------------------------------------
 * CalculateCheckSum
 *------------------------------------------*/
uint8_t
CalculateCheckSum (uint8_t* Data, uint16_t Len)
{
    uint8_t   Sum;
    uint16_t  i;

    Sum = 0;
    for (i = 0; i < Len; i++)
    {
        Sum += Data [i];
    }
    return (0x100 - Sum);
}

/*
* @fn TimeUpdate
* @return Returns the system uptime
*/
uint32_t TimeUpdate()
{
    struct sysinfo time;
    sysinfo(&time);
    return time.uptime;
}

// /**
// *@fn GetSysCtlvalue
// *@brief This function retrieves the values from Sysctl
// *@param TagName -Tagname form which value has to be retrieved
// *@param SysVal - Where the value is stored
// *@return Returns 0 on success
// *            Returns 1 on failure
// */
// int GetJiffySysCtlvalue (const char *TagName, long long *SysVal)
// {
//     unsigned long RetVal;
//     FILE* SysFile = fopen (TagName, "r");
//     unsigned long Hertz = sysconf(_SC_CLK_TCK);

//     if ((!SysFile) || (!SysVal))
//         return 1;

//     fscanf (SysFile, "%lu", &RetVal);
//     fclose (SysFile);

//     *SysVal = (RetVal*1000)/Hertz;
//     return 0;
// }


/*
    index = 0,1,2,3,4,5
*/
uint8_t mac2hex(const char *strMac, uint8_t index)
{
    int i = 0;
    char    *pStr = (char *)strMac;
    char    *pTmp = NULL;
    uint8_t macByte = 0;;
    char    tmp[3] = {0};

    if(index > 5)
    {
        printf("Invalid index %d\n", index);
        return -1;
    }

    //find
    while(pStr)
    {
        if(i == index)
            break;

        if(*pStr == ':')
            i++;
        
        pStr++;
    }

    if(*(pStr+1) == ':')
    {
        tmp[0] = *pStr;
        tmp[1] = '\0';
        tmp[2] = '\0';
    }
    else
    {
        tmp[0] = *pStr;
        tmp[1] = *(pStr+1);
        tmp[2] = '\0';
    }

    i=0;
    macByte = 0;
    while(tmp[i])
    {
        macByte = macByte<<4;
        if((tmp[i]>='0') && (tmp[i]<='9'))
            macByte += (tmp[i]-'0');
        else if((tmp[i]>='a') && (tmp[i]<='f'))
            macByte += (tmp[i]-'a') + 10;
        else if((tmp[i]>='A') && (tmp[i]<='F'))
            macByte += (tmp[i]-'A') + 10;

        i++;
    }

    return macByte;     
}

/*
    index = 0,1,2,3
*/
uint8_t ip2dec(const char *strIp, uint8_t index)
{
    int i = 0;
    char    *pStr = (char *)strIp;
    uint8_t ipByte = 0;;
    char    tmp[4] = {0};

    if(index > 3)
    {
        printf("Invalid index %d\n", index);
        return -1;
    }

    //find
    while(pStr)
    {
        if(i == index)
            break;

        if(*pStr == '.')
            i++;
        
        pStr++;
    }

    if(*(pStr+1) == '.')
    {
        tmp[0] = *pStr;
        tmp[1] = '\0';
        tmp[2] = '\0';
        tmp[3] = '\0';
    }
    else if(*(pStr+2) == '.')
    {
        tmp[0] = *pStr;
        tmp[1] = *(pStr+1);
        tmp[2] = '\0';
        tmp[3] = '\0';
    }
    else
    {
        tmp[0] = *pStr;
        tmp[1] = *(pStr+1);
        tmp[2] = *(pStr+2);
        tmp[3] = '\0';
    }

    ipByte = atoi(tmp);

    return ipByte;     
}


int FlushUserInfo(void)
{
    //TODO:
    return 0;
}

int FlushFRU(void)
{
    //TODO:
    return 0;
}

int FlushSDR(void)
{
    //TODO:
    return 0;
}

int FlushIpmiConfig(void)
{
    //TODO:
    return 0;
}

int FlushSEL(void)
{
    //TODO:
    return 0;
}

