

/**
@file shell_inode.c
@brief Shell inode process.
*/
/********************************************************************************************
Debug Switch Definition Section
********************************************************************************************/


/********************************************************************************************
Include File Section
********************************************************************************************/
#include "shell_mem.h"
#include "shell_inode.h"
//#include "shell_char.h"
#include "main.h"
#include "ecarx_queue.h"
/********************************************************************************************
Macro Definition Section
********************************************************************************************/
#define MAX_INODE_COUNT             (64)
#define MAX_BIT_MAP_CNT             (MAX_INODE_COUNT/MEM_BIT_MAP_WIDTH)
/********************************************************************************************
Prototype Declaration Section
********************************************************************************************/
QUEUE * pxShell_GetInodeList(void);
INODE_STATUS xShell_InodeMemCreate(void);
void vShellInodeInit(void);
INODE_STATUS xShellInodeCreate(const uint8_t *name, NODE_MODE *mode, SHELL_FILE_OPS *f_ops);
SHELL_INODE * pxShellInodelookup(const uint8_t *name, INODE_STATUS *rslt);
INODE_STATUS xShellInodeDelete(const uint8_t *name);

/********************************************************************************************
Const Global Variable Definition Section
********************************************************************************************/


/********************************************************************************************
Global Variable Definition Section
********************************************************************************************/

/********************************************************************************************
Static Variable Definition Section
********************************************************************************************/

static uint8_t s_aucInodeMemBlock[MAX_INODE_COUNT * sizeof(SHELL_INODE)];
static MEM_BIT_MAP_TYPE s_axInodeMemBlockMap[MAX_BIT_MAP_CNT];

static SHELL_MEM_CB s_xInodeMemCb;
static QUEUE s_xInodeList;


/********************************************************************************************
Structure Type Definition Section
********************************************************************************************/


/********************************************************************************************
Function Definition Section
********************************************************************************************/

/**
 * xShell_InodeMemCreate
 * Author qiuxf
 * @param *lock pointer to lock
 * @return status
 */

QUEUE * pxShell_GetInodeList(void)
{
	return &s_xInodeList;
}
/**
 * xShell_InodeMemCreate
 * Author qiuxf
 * @param *lock pointer to lock
 * @return status
 */

INODE_STATUS xShell_InodeMemCreate(void)
{
    INODE_STATUS ret = INODE_SUCESS;
    if(PROC_SUCESS != xShellMemInit(&s_xInodeMemCb, &s_aucInodeMemBlock[0], MAX_INODE_COUNT, sizeof(SHELL_INODE), \
                                      &s_axInodeMemBlockMap[0], MAX_BIT_MAP_CNT))
    {
        ret = INODE_MEM_ERR;
    }
    else
    {
        ; /*do nothing*/
    }
    return ret;
}

/**
 * vShellInodeInit
 * Author qiuxf
 * @param none
 * @return status
 */

void vShellInodeInit(void)
{
    vQueueInit(&s_xInodeList);
    xShell_InodeMemCreate();
   // Shell_InodeDoInitCall();
}

/**
 * xShellInodeCreate
 * Author qiuxf
 * @param none
 * @return status
 */
INODE_STATUS xShellInodeCreate(const uint8_t *name, NODE_MODE *mode, SHELL_FILE_OPS *f_ops)
{
    SHELL_STATUS status = PROC_SUCESS;
    SHELL_INODE *inode = SAF_NULL;
    INODE_STATUS ret = INODE_SUCESS;

    inode = pxShellInodelookup(name, &ret);
    if(inode != SAF_NULL)
    {
        /*node assist*/
        ret = INODE_ASSIST;
       // return ret;
    }
    else
    {
        ret = INODE_SUCESS;
        inode = (SHELL_INODE *)pvShellMemGet(&s_xInodeMemCb, &status);
        if((SAF_NULL != inode)&&(PROC_SUCESS == status))
        {
            inode->f_ops = f_ops;
            inode->mode.i_mode_root = mode->i_mode_root;
            inode->mode.i_mode_user = mode->i_mode_user;
            inode->name = name;
            /*add to inode list*/
            vQueueInsert(&inode->i_list, &s_xInodeList);
        }
        else
        {
            ret = INODE_MEM_ERR;
        }
    }
    return ret;
}

/**
 * pxShellInodelookup
 * Author qiuxf
 * @param none
 * @return status
 */
SHELL_INODE * pxShellInodelookup(const uint8_t *name, INODE_STATUS *rslt)
{
    QUEUE *entry;
    SHELL_INODE *inode = SAF_NULL;

    *rslt = INODE_NO_NAME;
    /*find entry*/
    for(entry = s_xInodeList.next; entry != &s_xInodeList; entry = entry->next)
    {
        inode = (SHELL_INODE *)entry;
        if(0 == strncmp((const char *)inode->name, (const char *)name, MAX_FILE_NAME))
        {
            *rslt = INODE_SUCESS;
            break;
        }
        else
        {
        	inode = SAF_NULL; /*continue*/
        }
    }
    return inode;
}

/**
 * xShellInodeDelete
 * Author qiuxf
 * @param none
 * @return status
 */
INODE_STATUS xShellInodeDelete(const uint8_t *name)
{
    SHELL_INODE *inode = SAF_NULL;
    INODE_STATUS rslt = INODE_SUCESS;

    inode = pxShellInodelookup(name, &rslt);
    QueueDelete(&inode->i_list);

    if(PROC_SUCESS != xShellMemPut(&s_xInodeMemCb, inode))
    {
        rslt = INODE_E_PUTMEM;
    }
    else
    {
        ;
    }

    return rslt;
}


/**
 * xShellInodeDelete
 * Author qiuxf
 * @param none
 * @return status
 */

#if 0
INODE_STATUS Shell_InodeDoInitCall(void)
{
	INODE_STATUS ret = INODE_SUCESS;
	uint32_t funcCnt = ((uint32_t)INODE_INITCALL_END - (uint32_t)INODE_INITCALL_START) / sizeof(initcall_t);
	uint32_t i = 0;
	initcall_t *callPtr = (initcall_t *)INODE_INITCALL_START;
	int callret = 0;
	//_inode_initcall_SAF_NULL_inode_init();
	for(i = 0; i < funcCnt; i++)
	{
		callret = (*callPtr)();
		callPtr++;
		if(0 != callret)
		{
			printk("do init call error\n");
			ret = INODE_E_INIT;
			break;
		}
	}

	return ret;
}
#endif



