/*****************************************************************
******************************************************************
***                                                            ***
***        (C)Copyright 2008, American Megatrends Inc.         ***
***                                                            ***
***                    All Rights Reserved                     ***
***                                                            ***
***       5555 Oakbrook Parkway, Norcross, GA 30093, USA       ***
***                                                            ***
***                     Phone 770.246.8600                     ***
***                                                            ***
******************************************************************
******************************************************************
******************************************************************
* 
* Filename: libipmi_usermgmt.c
*
******************************************************************/
#include "libipmi_session.h"
#include "libipmi_errorcodes.h"
#include "libipmi_api.h"
#include "libipmi_usermgmt.h"

#include "string.h"


uint16_t LIBIPMI_SetUser( IPMI20_SESSION_T *pSession, char *pszUsername, uint8_t byID, int timeout )
{
	SetUser_T	UserInfo;
	int			nUserNameLen;
	char		byRes[MAX_RESPONSE_SIZE];
	uint32_t		dwResLen;
	uint16_t		wRet;
	
	
	if(byID	>= MAX_USERS)
	{
		printf("Invalid UserID\n");
		return STATUS_CODE(0, LIBIPMI_E_INVALID_USER_ID);
	}
	else if( (nUserNameLen=strlen(pszUsername)) == 0 )
	{
		printf("Invalid UserName\n");
		return STATUS_CODE(0, LIBIPMI_E_INVALID_USER_NAME);
	}

	memset(&UserInfo, 0, sizeof(SetUser_T) );
	
	/* Truncate the username to 16 chars */
	(nUserNameLen > MAX_USER_NAME_LEN)?nUserNameLen = MAX_USER_NAME_LEN:nUserNameLen;
	
	/* IPMI Spec Table 22-33 */
	UserInfo.byUserID = byID & ~(0xC0); 
	memcpy(UserInfo.szUserName, pszUsername, nUserNameLen);

	dwResLen = 1;
	/* Call RAW IPMI Command */
	wRet = LIBIPMI_Send_RAW_IPMI2_0_Command(pSession, PAYLOAD_TYPE_IPMI,
											DEFAULT_NET_FN_LUN, CMD_SET_USER_NAME,
											(uint8_t*)&UserInfo, sizeof(SetUser_T),
											byRes, &dwResLen,
											timeout);
	if(wRet == LIBIPMI_E_SUCCESS)
		return STATUS_CODE(LIBIPMI_STATUS_SUCCESS, byRes[0]);
	
	return wRet;
}

uint16_t LIBIPMI_GetUser( IPMI20_SESSION_T *pSession, char *pszUsername, uint8_t byID, int timeout )
{
	GetUser_T	UserInfo;
	uint32_t		dwRetLen;
	uint16_t		wRet;
	
	if(byID	>= MAX_USERS)
	{
		printf("Invalid UserID\n");
		return STATUS_CODE(0, LIBIPMI_E_INVALID_USER_ID);
	}

	memset(&UserInfo, 0, sizeof(GetUser_T) );

	dwRetLen = sizeof(GetUser_T);
	byID &= ~(0xC0);
	/* Call RAW IPMI Command */
	wRet = LIBIPMI_Send_RAW_IPMI2_0_Command(pSession, PAYLOAD_TYPE_IPMI,
											DEFAULT_NET_FN_LUN, CMD_GET_USER_NAME,
											&byID, sizeof(uint8_t),
											(uint8_t *)&UserInfo, &dwRetLen,
											timeout);

	if(wRet == LIBIPMI_E_SUCCESS)
	{
		strcpy(pszUsername, UserInfo.szUserName);
		return STATUS_CODE(LIBIPMI_STATUS_SUCCESS, UserInfo.byCompletionCode);
	}
	else
	{
		pszUsername[0] = (char)0;
	}
	
	return wRet;
}

